//****************************
// Wizard AtmoOrb
//****************************
import { ledDeviceWizardUtils as utils } from './LedDevice_utils.js';
const atmoorbWizard = (() => {
const lights = [];
let configuredLights = [];
function getIdInLights(id) {
return lights.filter(
function (lights) {
return lights.id === id
}
);
}
function begin() {
const configruedOrbIds = conf_editor.getEditor("root.specificOptions.orbIds").getValue().trim();
if (configruedOrbIds.length !== 0) {
configuredLights = configruedOrbIds.split(",").map(Number);
}
const multiCastGroup = conf_editor.getEditor("root.specificOptions.host").getValue();
const multiCastPort = parseInt(conf_editor.getEditor("root.specificOptions.port").getValue());
discover(multiCastGroup, multiCastPort);
$('#btn_wiz_save').off().on("click", function () {
let ledConfig = [];
let finalLights = [];
//create atmoorb led config
for (let key in lights) {
if ($('#orb_' + key).val() !== "disabled") {
// Set Name to layout-position, if empty
if (lights[key].name === "") {
lights[key].name = $.i18n('conf_leds_layout_cl_' + $('#orb_' + key).val());
}
finalLights.push(lights[key].id);
let name = lights[key].id;
if (lights[key].host !== "")
name += ':' + lights[key].host;
const idx_content = utils.assignLightPos($('#orb_' + key).val(), name);
ledConfig.push(JSON.parse(JSON.stringify(idx_content)));
}
}
//LED layout
window.serverConfig.leds = ledConfig;
//LED device config
//Start with a clean configuration
let d = {};
d.type = 'atmoorb';
d.hardwareLedCount = finalLights.length;
d.colorOrder = conf_editor.getEditor("root.generalOptions.colorOrder").getValue();
d.orbIds = finalLights.toString();
d.useOrbSmoothing = utils.eV("useOrbSmoothing");
d.host = conf_editor.getEditor("root.specificOptions.host").getValue();
d.port = parseInt(conf_editor.getEditor("root.specificOptions.port").getValue());
d.latchTime = parseInt(conf_editor.getEditor("root.specificOptions.latchTime").getValue());;
window.serverConfig.device = d;
requestWriteConfig(window.serverConfig, true);
resetWizard();
});
$('#btn_wiz_abort').off().on('click', resetWizard);
}
async function discover(multiCastGroup, multiCastPort) {
let params = {};
if (multiCastGroup !== "") {
params.multiCastGroup = multiCastGroup;
}
if (multiCastPort !== 0) {
params.multiCastPort = multiCastPort;
}
// Get discovered lights
const res = await requestLedDeviceDiscovery('atmoorb', params);
if (res && !res.error) {
const r = res.info;
// Process devices returned by discovery
processDiscoveredDevices(r.devices);
// Add additional items from configuration
for (const configuredLight of configuredLights) {
processConfiguredLight(configuredLight);
}
sortLightsById();
assign_lights();
}
}
function processDiscoveredDevices(devices) {
for (const device of devices) {
if (device.id !== "" && getIdInLights(device.id).length === 0) {
const light = {
id: device.id,
ip: device.ip,
host: device.hostname
};
lights.push(light);
}
}
}
function processConfiguredLight(configuredLight) {
if (configuredLight !== "" && !isNaN(configuredLight)) {
if (getIdInLights(configuredLight).length === 0) {
const light = {
id: configuredLight,
ip: "",
host: ""
};
lights.push(light);
}
}
}
function attachIdentifyButtonEvent() {
// Use event delegation to handle clicks on buttons with class "btn-identify"
$('#wizp2_body').on('click', '.btn-identify', function () {
const orbId = $(this).data('orb-id');
identify(orbId);
});
}
function sortLightsById() {
lights.sort((a, b) => (a.id > b.id) ? 1 : -1);
}
function assign_lights() {
// If records are left for configuration
if (Object.keys(lights).length > 0) {
$('#wh_topcontainer').toggle(false);
$('#orb_ids_t, #btn_wiz_save').toggle(true);
const lightOptions = utils.getLayoutPositions();
lightOptions.unshift("disabled");
$('.lidsb').html("");
let pos = "";
for (const lightid in lights) {
const orbId = lights[lightid].id;
const orbIp = lights[lightid].ip;
let orbHostname = lights[lightid].host;
if (orbHostname === "")
orbHostname = $.i18n('edt_dev_spec_lights_itemtitle');
let options = "";
for (const opt in lightOptions) {
const val = lightOptions[opt];
options += '';
}
let enabled = 'enabled';
if (orbId < 1 || orbId > 255) {
enabled = 'disabled';
options = '';
}
let lightAnnotation = "";
if (orbIp !== "") {
lightAnnotation = ': ' + orbIp + '
(' + orbHostname + ')';
}
$('.lidsb').append(createTableRow([orbId + lightAnnotation, '', '']));
}
attachIdentifyButtonEvent();
$('.orb_sel_watch').on("change", function () {
let cC = 0;
for (const key in lights) {
if ($('#orb_' + key).val() !== "disabled") {
cC++;
}
}
if (cC === 0 || window.readOnlyMode)
$('#btn_wiz_save').prop("disabled", true);
else
$('#btn_wiz_save').prop("disabled", false);
});
$('.orb_sel_watch').trigger('change');
}
else {
const noLightsTxt = '
' + $.i18n('wiz_noLights', 'AtmoOrbs') + '
'; $('#wizp2_body').append(noLightsTxt); } } async function identify(orbId) { const disabled = $('#btn_wiz_save').is(':disabled'); // Take care that new record cannot be save during background process $('#btn_wiz_save').prop('disabled', true); const params = { id: orbId }; await requestLedDeviceIdentification("atmoorb", params); if (!window.readOnlyMode) { $('#btn_wiz_save').prop('disabled', disabled); } } return { start: function (e) { //create html const atmoorb_title = 'wiz_atmoorb_title'; const atmoorb_intro1 = 'wiz_atmoorb_intro1'; $('#wiz_header').html('' + $.i18n(atmoorb_title)); $('#wizp1_body').html('' + $.i18n(atmoorb_intro1) + '
'); $('#wizp1_footer').html(''); $('#wizp2_body').html(''); $('#wh_topcontainer').append(''); $('#wizp2_body').append(' '); createTable("lidsh", "lidsb", "orb_ids_t"); $('.lidsh').append(createTableRow([$.i18n('edt_dev_spec_lights_title'), $.i18n('wiz_pos'), $.i18n('wiz_identify')], true)); $('#wizp2_footer').html('