2022-04-12 11:41:18 +00:00
|
|
|
const Api = {
|
|
|
|
/**
|
|
|
|
* get lots id
|
|
|
|
* @returns get lots
|
|
|
|
*/
|
|
|
|
async get_lots() {
|
2022-04-20 10:04:53 +00:00
|
|
|
const request = await this.doRequest(API_URLS.lots, "GET", null);
|
2022-04-25 09:49:11 +00:00
|
|
|
if (request != undefined) {
|
|
|
|
request.items = request.items.filter(itm => !itm.trade) // Avoid show outgoing or incomming trades
|
|
|
|
return request.items;
|
|
|
|
}
|
2022-04-12 11:41:18 +00:00
|
|
|
throw request;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get filtered devices info
|
|
|
|
* @param {number[]} ids devices ids
|
|
|
|
* @returns full detailed device list
|
|
|
|
*/
|
|
|
|
async get_devices(ids) {
|
2022-04-20 10:04:53 +00:00
|
|
|
const request = await this.doRequest(`${API_URLS.devices }?filter={"id": [${ ids.toString() }]}`, "GET", null);
|
2022-04-12 11:41:18 +00:00
|
|
|
if (request != undefined) return request.items;
|
|
|
|
throw request;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get filtered devices info
|
|
|
|
* @param {number[]} ids devices ids
|
|
|
|
* @returns full detailed device list
|
|
|
|
*/
|
|
|
|
async search_device(id) {
|
2022-04-20 10:04:53 +00:00
|
|
|
const request = await this.doRequest(`${API_URLS.devices }?filter={"devicehub_id": ["${ id }"]}`, "GET", null)
|
2022-04-12 11:41:18 +00:00
|
|
|
if (request != undefined) return request.items
|
|
|
|
throw request
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add devices to lot
|
|
|
|
* @param {number} lotID lot id
|
|
|
|
* @param {number[]} listDevices list devices id
|
|
|
|
*/
|
|
|
|
async devices_add(lotID, listDevices) {
|
2022-04-20 10:04:53 +00:00
|
|
|
const queryURL = `${API_URLS.devices_modify.replace("UUID", lotID) }?${ listDevices.map(deviceID => `id=${ deviceID}`).join("&")}`;
|
2022-04-20 10:09:56 +00:00
|
|
|
return Api.doRequest(queryURL, "POST", null);
|
2022-04-12 11:41:18 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove devices from a lot
|
|
|
|
* @param {number} lotID lot id
|
|
|
|
* @param {number[]} listDevices list devices id
|
|
|
|
*/
|
|
|
|
async devices_remove(lotID, listDevices) {
|
2022-04-20 10:04:53 +00:00
|
|
|
const queryURL = `${API_URLS.devices_modify.replace("UUID", lotID) }?${ listDevices.map(deviceID => `id=${ deviceID}`).join("&")}`;
|
2022-04-20 10:09:56 +00:00
|
|
|
return Api.doRequest(queryURL, "DELETE", null);
|
2022-04-12 11:41:18 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} url URL to be requested
|
|
|
|
* @param {String} type Action type
|
|
|
|
* @param {String | Object} body body content
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
async doRequest(url, type, body) {
|
2022-04-20 10:04:53 +00:00
|
|
|
let result;
|
2022-04-12 11:41:18 +00:00
|
|
|
try {
|
|
|
|
result = await $.ajax({
|
2022-04-20 10:04:53 +00:00
|
|
|
url,
|
|
|
|
type,
|
2022-04-12 11:41:18 +00:00
|
|
|
headers: { "Authorization": API_URLS.Auth_Token },
|
2022-04-20 10:04:53 +00:00
|
|
|
body
|
2022-04-12 11:41:18 +00:00
|
|
|
});
|
|
|
|
return result;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|