//clear priority and other tasks if people reload the page or lost connection while a wizard was active
$(window.hyperion).one("ready", function (event) {
if (getStorage("wizardactive") === 'true') {
requestPriorityClear();
setStorage("wizardactive", false);
if (getStorage("kodiAddress") != null) {
kodiAddress = getStorage("kodiAddress");
sendToKodi("stop");
}
}
});
function resetWizard(reload) {
$("#wizard_modal").modal('hide');
clearInterval(wIntveralId);
requestPriorityClear();
setStorage("wizardactive", false);
$('#wizp1').toggle(true);
$('#wizp2').toggle(false);
$('#wizp3').toggle(false);
//cc
if (withKodi)
sendToKodi("stop");
step = 0;
if (!reload) location.reload();
}
//rgb byte order wizard
var wIntveralId;
var new_rgb_order;
function changeColor() {
var color = $("#wiz_canv_color").css('background-color');
if (color == 'rgb(255, 0, 0)') {
$("#wiz_canv_color").css('background-color', 'rgb(0, 255, 0)');
requestSetColor('0', '255', '0');
}
else {
$("#wiz_canv_color").css('background-color', 'rgb(255, 0, 0)');
requestSetColor('255', '0', '0');
}
}
function startWizardRGB() {
//create html
$('#wiz_header').html('' + $.i18n('wiz_rgb_title'));
$('#wizp1_body').html('
');
createTable("lidsh", "lidsb", "hue_ids_t");
$('.lidsh').append(createTableRow([$.i18n('edt_dev_spec_lights_title'),$.i18n('wiz_pos'),$.i18n('wiz_identify')], true));
$('#wizp2_footer').html('');
*/
//open modal
$("#wizard_modal").modal({
backdrop: "static",
keyboard: false,
show: true
});
//listen for continue
$('#btn_wiz_cont').off().on('click', function () {
/* For testing only
discover_wled();
var hostAddress = conf_editor.getEditor("root.specificOptions.host").getValue();
if(hostAddress != "")
{
getProperties_wled(hostAddress,"info");
identify_wled(hostAddress)
}
For testing only */
});
}
async function discover_wled() {
const res = await requestLedDeviceDiscovery('wled');
// TODO: error case unhandled
// res can be: false (timeout) or res.error (not found)
if (res && !res.error) {
const r = res.info
// Process devices returned by discovery
console.log(r);
if (r.devices.length == 0)
$('#wiz_hue_ipstate').html($.i18n('wiz_hue_failure_ip'));
else {
for (const device of r.devices) {
console.log("Device:", device);
var ip = device.hostname + ":" + device.port;
console.log("Host:", ip);
//wledIPs.push({internalipaddress : ip});
}
}
}
}
async function getProperties_wled(hostAddress, resourceFilter) {
let params = { host: hostAddress, filter: resourceFilter };
const res = await requestLedDeviceProperties('wled', params);
// TODO: error case unhandled
// res can be: false (timeout) or res.error (not found)
if (res && !res.error) {
const r = res.info
// Process properties returned
console.log(r);
}
}
function identify_wled(hostAddress) {
let params = { host: hostAddress };
const res = requestLedDeviceIdentification("wled", params);
// TODO: error case unhandled
// res can be: false (timeout) or res.error (not found)
if (res && !res.error) {
const r = res.info
console.log(r);
}
}
//****************************
// Wizard Yeelight
//****************************
var lights = null;
function startWizardYeelight(e) {
//create html
var yeelight_title = 'wiz_yeelight_title';
var yeelight_intro1 = 'wiz_yeelight_intro1';
$('#wiz_header').html('' + $.i18n(yeelight_title));
$('#wizp1_body').html('
');
createTable("lidsh", "lidsb", "yee_ids_t");
$('.lidsh').append(createTableRow([$.i18n('edt_dev_spec_lights_title'), $.i18n('wiz_pos'), $.i18n('wiz_identify')], true));
$('#wizp2_footer').html(''
+ $.i18n('general_btn_cancel') + '');
//open modal
$("#wizard_modal").modal({ backdrop: "static", keyboard: false, show: true });
//listen for continue
$('#btn_wiz_cont').off().on('click', function () {
beginWizardYeelight();
$('#wizp1').toggle(false);
$('#wizp2').toggle(true);
});
}
function beginWizardYeelight() {
lights = [];
configuredLights = conf_editor.getEditor("root.specificOptions.lights").getValue();
discover_yeelight_lights();
$('#btn_wiz_save').off().on("click", function () {
var yeelightLedConfig = [];
var finalLights = [];
//create yeelight led config
for (var key in lights) {
if ($('#yee_' + key).val() !== "disabled") {
//delete lights[key].model;
// Set Name to layout-position, if empty
if (lights[key].name === "") {
lights[key].name = $.i18n('conf_leds_layout_cl_' + $('#yee_' + key).val());
}
finalLights.push(lights[key]);
var name = lights[key].host;
if (lights[key].name !== "")
name += '_' + lights[key].name;
var idx_content = assignLightPos(key, $('#yee_' + key).val(), name);
yeelightLedConfig.push(JSON.parse(JSON.stringify(idx_content)));
}
}
//LED layout
window.serverConfig.leds = yeelightLedConfig;
//LED device config
//Start with a clean configuration
var d = {};
d.type = 'yeelight';
d.hardwareLedCount = finalLights.length;
d.colorOrder = conf_editor.getEditor("root.generalOptions.colorOrder").getValue();
d.colorModel = parseInt(conf_editor.getEditor("root.specificOptions.colorModel").getValue());
d.transEffect = parseInt(conf_editor.getEditor("root.specificOptions.transEffect").getValue());
d.transTime = parseInt(conf_editor.getEditor("root.specificOptions.transTime").getValue());
d.extraTimeDarkness = parseInt(conf_editor.getEditor("root.specificOptions.extraTimeDarkness").getValue());
d.brightnessMin = parseInt(conf_editor.getEditor("root.specificOptions.brightnessMin").getValue());
d.brightnessSwitchOffOnMinimum = JSON.parse(conf_editor.getEditor("root.specificOptions.brightnessSwitchOffOnMinimum").getValue());
d.brightnessMax = parseInt(conf_editor.getEditor("root.specificOptions.brightnessMax").getValue());
d.brightnessFactor = parseFloat(conf_editor.getEditor("root.specificOptions.brightnessFactor").getValue());
d.latchTime = parseInt(conf_editor.getEditor("root.specificOptions.latchTime").getValue());;
d.debugLevel = parseInt(conf_editor.getEditor("root.specificOptions.debugLevel").getValue());
d.lights = finalLights;
window.serverConfig.device = d;
//smoothing off
window.serverConfig.smoothing.enable = false;
requestWriteConfig(window.serverConfig, true);
resetWizard();
});
$('#btn_wiz_abort').off().on('click', resetWizard);
}
function getHostInLights(hostname) {
return lights.filter(
function (lights) {
return lights.host === hostname
}
);
}
async function discover_yeelight_lights() {
var light = {};
// Get discovered lights
const res = await requestLedDeviceDiscovery('yeelight');
// TODO: error case unhandled
// res can be: false (timeout) or res.error (not found)
if (res && !res.error) {
const r = res.info;
// Process devices returned by discovery
for (const device of r.devices) {
//console.log("Device:", device);
if (device.hostname !== "") {
if (getHostInLights(device.hostname).length === 0) {
light = {};
light.host = device.hostname;
light.port = device.port;
light.name = device.other.name;
light.model = device.other.model;
lights.push(light);
}
}
}
// Add additional items from configuration
for (var keyConfig in configuredLights) {
var [host, port] = configuredLights[keyConfig].host.split(":", 2);
//In case port has been explicitly provided, overwrite port given as part of hostname
if (configuredLights[keyConfig].port !== 0)
port = configuredLights[keyConfig].port;
if (host !== "")
if (getHostInLights(host).length === 0) {
light = {};
light.host = host;
light.port = port;
light.name = configuredLights[keyConfig].name;
light.model = "color4";
lights.push(light);
}
}
assign_yeelight_lights();
}
}
function assign_yeelight_lights() {
var models = ['color', 'color1', 'color2', 'color4', 'stripe', 'strip1'];
// If records are left for configuration
if (Object.keys(lights).length > 0) {
$('#wh_topcontainer').toggle(false);
$('#yee_ids_t,#btn_wiz_save').toggle(true);
var lightOptions = [
"top", "topleft", "topright",
"bottom", "bottomleft", "bottomright",
"left", "lefttop", "leftmiddle", "leftbottom",
"right", "righttop", "rightmiddle", "rightbottom",
"entire"
];
lightOptions.unshift("disabled");
$('.lidsb').html("");
var pos = "";
for (var lightid in lights) {
var lightHostname = lights[lightid].host;
var lightPort = lights[lightid].port;
var lightName = lights[lightid].name;
if (lightName === "")
lightName = $.i18n('edt_dev_spec_lights_itemtitle');
var options = "";
for (var opt in lightOptions) {
var val = lightOptions[opt];
var txt = (val !== 'entire' && val !== 'disabled') ? 'conf_leds_layout_cl_' : 'wiz_ids_';
options += '';
}
var enabled = 'enabled'
if (!models.includes(lights[lightid].model)) {
var enabled = 'disabled';
options = '';
}
$('.lidsb').append(createTableRow([(parseInt(lightid, 10) + 1) + '. ' + lightName + ' (' + lightHostname + ')', '', '']));
}
$('.yee_sel_watch').bind("change", function () {
var cC = 0;
for (var key in lights) {
if ($('#yee_' + key).val() !== "disabled") {
cC++;
}
}
if (cC === 0)
$('#btn_wiz_save').attr("disabled", true);
else
$('#btn_wiz_save').attr("disabled", false);
});
$('.yee_sel_watch').trigger('change');
}
else {
var noLightsTxt = '
' + $.i18n('wiz_yeelight_noLights') + '
';
$('#wizp2_body').append(noLightsTxt);
}
}
async function getProperties_yeelight(hostname, port) {
let params = { hostname: hostname, port: port };
const res = await requestLedDeviceProperties('yeelight', params);
// TODO: error case unhandled
// res can be: false (timeout) or res.error (not found)
if (res && !res.error) {
const r = res.info
// Process properties returned
console.log(r);
}
}
function identify_yeelight_device(hostname, port) {
let params = { hostname: hostname, port: port };
const res = requestLedDeviceIdentification("yeelight", params);
// TODO: error case unhandled
// res can be: false (timeout) or res.error (not found)
if (res && !res.error) {
//const r = res.info;
}
}
//****************************
// Wizard AtmoOrb
//****************************
var lights = null;
function startWizardAtmoOrb(e) {
//create html
var atmoorb_title = 'wiz_atmoorb_title';
var atmoorb_intro1 = 'wiz_atmoorb_intro1';
$('#wiz_header').html('' + $.i18n(atmoorb_title));
$('#wizp1_body').html('
');
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(''
+ $.i18n('general_btn_cancel') + '');
//open modal
$("#wizard_modal").modal({ backdrop: "static", keyboard: false, show: true });
//listen for continue
$('#btn_wiz_cont').off().on('click', function () {
beginWizardAtmoOrb();
$('#wizp1').toggle(false);
$('#wizp2').toggle(true);
});
}
function beginWizardAtmoOrb() {
lights = [];
configuredLights = [];
configruedOrbIds = conf_editor.getEditor("root.specificOptions.orbIds").getValue().trim();
if (configruedOrbIds.length !== 0) {
configuredLights = configruedOrbIds.split(",").map(Number);
}
var multiCastGroup = conf_editor.getEditor("root.specificOptions.output").getValue();
var multiCastPort = parseInt(conf_editor.getEditor("root.specificOptions.port").getValue());
discover_atmoorb_lights(multiCastGroup, multiCastPort);
$('#btn_wiz_save').off().on("click", function () {
var atmoorbLedConfig = [];
var finalLights = [];
//create atmoorb led config
for (var 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);
var name = lights[key].id;
if (lights[key].host !== "")
name += ':' + lights[key].host;
var idx_content = assignLightPos(key, $('#orb_' + key).val(), name);
atmoorbLedConfig.push(JSON.parse(JSON.stringify(idx_content)));
}
}
//LED layout
window.serverConfig.leds = atmoorbLedConfig;
//LED device config
//Start with a clean configuration
var d = {};
d.type = 'atmoorb';
d.hardwareLedCount = finalLights.length;
d.colorOrder = conf_editor.getEditor("root.generalOptions.colorOrder").getValue();
d.orbIds = finalLights.toString();
d.useOrbSmoothing = (eV("useOrbSmoothing") == true);
d.output = conf_editor.getEditor("root.specificOptions.output").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);
}
function getIdInLights(id) {
return lights.filter(
function (lights) {
return lights.id === id
}
);
}
async function discover_atmoorb_lights(multiCastGroup, multiCastPort) {
var light = {};
if (multiCastGroup === "")
multiCastGroup = "239.255.255.250";
if (multiCastPort === "")
multiCastPort = 49692;
let params = { multiCastGroup: multiCastGroup, multiCastPort: multiCastPort };
// Get discovered lights
const res = await requestLedDeviceDiscovery('atmoorb', params);
// TODO: error case unhandled
// res can be: false (timeout) or res.error (not found)
if (res && !res.error) {
const r = res.info
// Process devices returned by discovery
for (const device of r.devices) {
if (device.id !== "") {
if (getIdInLights(device.id).length === 0) {
light = {};
light.id = device.id;
light.ip = device.ip;
light.host = device.hostname;
lights.push(light);
}
}
}
// Add additional items from configuration
for (const keyConfig in configuredLights) {
if (configuredLights[keyConfig] !== "" && !isNaN(configuredLights[keyConfig])) {
if (getIdInLights(configuredLights[keyConfig]).length === 0) {
light = {};
light.id = configuredLights[keyConfig];
light.ip = "";
light.host = "";
lights.push(light);
}
}
}
lights.sort((a, b) => (a.id > b.id) ? 1 : -1);
assign_atmoorb_lights();
}
}
function assign_atmoorb_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);
var lightOptions = [
"top", "topleft", "topright",
"bottom", "bottomleft", "bottomright",
"left", "lefttop", "leftmiddle", "leftbottom",
"right", "righttop", "rightmiddle", "rightbottom",
"entire"
];
lightOptions.unshift("disabled");
$('.lidsb').html("");
var pos = "";
for (var lightid in lights) {
var orbId = lights[lightid].id;
var orbIp = lights[lightid].ip;
var orbHostname = lights[lightid].host;
if (orbHostname === "")
orbHostname = $.i18n('edt_dev_spec_lights_itemtitle');
var options = "";
for (var opt in lightOptions) {
var val = lightOptions[opt];
var txt = (val !== 'entire' && val !== 'disabled') ? 'conf_leds_layout_cl_' : 'wiz_ids_';
options += '';
}
var enabled = 'enabled'
if (orbId < 1 || orbId > 255) {
enabled = 'disabled'
options = '';
}
var lightAnnotation = "";
if (orbIp !== "") {
lightAnnotation = ': ' + orbIp + ' (' + orbHostname + ')';
}
$('.lidsb').append(createTableRow([orbId + lightAnnotation, '', '']));
}
$('.orb_sel_watch').bind("change", function () {
var cC = 0;
for (var key in lights) {
if ($('#orb_' + key).val() !== "disabled") {
cC++;
}
}
if (cC === 0)
$('#btn_wiz_save').attr("disabled", true);
else
$('#btn_wiz_save').attr("disabled", false);
});
$('.orb_sel_watch').trigger('change');
}
else {
var noLightsTxt = '
' + $.i18n('wiz_atmoorb_noLights') + '
';
$('#wizp2_body').append(noLightsTxt);
}
}
function identify_atmoorb_device(orbId) {
let params = { id: orbId };
const res = requestLedDeviceIdentification("atmoorb", params);
// TODO: error case unhandled
// res can be: false (timeout) or res.error (not found)
if (res && !res.error) {
const r = res.info
}
}
//****************************
// Wizard/Routines Nanoleaf
//****************************
async function discover_nanoleaf() {
const res = await requestLedDeviceDiscovery('nanoleaf');
// TODO: error case unhandled
// res can be: false (timeout) or res.error (not found)
if (res && !res.error) {
const r = res.info
// Process devices returned by discovery
console.log(r);
if (r.devices.length == 0)
$('#wiz_hue_ipstate').html($.i18n('wiz_hue_failure_ip'));
else {
for (const device of r.devices) {
console.log("Device:", device);
var ip = device.hostname + ":" + device.port;
console.log("Host:", ip);
//nanoleafIPs.push({internalipaddress : ip});
}
}
}
}
async function getProperties_nanoleaf(hostAddress, authToken, resourceFilter) {
let params = { host: hostAddress, token: authToken, filter: resourceFilter };
const res = await requestLedDeviceProperties('nanoleaf', params);
// TODO: error case unhandled
// res can be: false (timeout) or res.error (not found)
if (res && !res.error) {
const r = res.info
// Process properties returned
console.log(r);
}
}
function identify_nanoleaf(hostAddress, authToken) {
let params = { host: hostAddress, token: authToken };
const res = requestLedDeviceIdentification("nanoleaf", params);
// TODO: error case unhandled
// res can be: false (timeout) or res.error (not found)
if (res && !res.error) {
const r = res.info
console.log(r);
}
}
//****************************
// Wizard/Routines RS232-Devices
//****************************
async function discover_providerRs232(rs232Type) {
const res = await requestLedDeviceDiscovery(rs232Type);
// TODO: error case unhandled
// res can be: false (timeout) or res.error (not found)
if (res && !res.error) {
const r = res.info
// Process serialPorts returned by discover
console.log(r);
}
}
//****************************
// Wizard/Routines HID (USB)-Devices
//****************************
async function discover_providerHid(hidType) {
const res = await requestLedDeviceDiscovery(hidType);
console.log("discover_providerHid", res);
// TODO: error case unhandled
// res can be: false (timeout) or res.error (not found)
if (res && !res.error) {
const r = res.info
// Process HID returned by discover
console.log(r);
}
}