import { sidebarTemplate } from "./devices-funcs.js";
import { deviceSetupFormPopup } from "./device-setup-form-popup.js";
function prepareData(data) {
const preparedData = [];
for(let device of data.devices) {
device = Helper.unification.deviceFieldsUnification(device);
preparedData.push({
deviceId: device.device_id,
deviceName: device.name,
deviceType: device.type,
status: `<span class="badge badge-primary">${device.status}</span>`,
ip: `<code class="code">${device.ip}</code>`,
mac: `<code class="code">${device.mac}</code>`,
wifiSignal: device.wifi_signal,
actions: device.status == "setup" ? `
<button
class="btn btn-secondary btn-small setup-btn"
data-device='${JSON.stringify(device)}'
type="button"
>Setup</button>
` : ""
});
}
return preparedData;
}
function renderingMainTable(scr, data, total) {
scr.currentScreen.DOMObject.querySelector(".main-container").innerHTML = Helper.template.table(
"Found devices",
{
deviceId: "Device ID",
deviceName: "Device name",
deviceType: "Type",
status: "Status",
ip: "IP",
mac: "Mac",
wifiSignal: "Signal",
actions: "Actions"
},
data,
`<span class="table-meta">Total: <span class="total">${total}</span> devices</span>`
);
}
function initMainTableInteractiveElements(scr, sh_api) {
scr.currentScreen.DOMObject.querySelectorAll(".setup-btn").forEach(btn => {
btn.addEventListener("click", e => {
const device = JSON.parse(e.currentTarget.dataset.device);
deviceSetupFormPopup(device, sh_api).show();
});
});
}
function scanning(sh_api) {
return {
alias: "devices-scanning",
renderer: () => {
return Helper.template.mainTemplate(sidebarTemplate("scanning"));
},
initer: scr => {
sh_api.devices.scanning_all((err, resp, meta) => {
console.log("sh_api.devices.scanning_all", err, resp);
if(meta.status_code != 200) {
return scr.error("Server API ERROR", "");
}
renderingMainTable(scr, prepareData(resp.data), resp.data.devices.length);
initMainTableInteractiveElements(scr, sh_api);
scr.ready();
});
}
};
}
export {
scanning
}