//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", "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') + '');
if (getStorage("darkMode", false) == "on")
$('#wizard_logo').attr("src", 'img/hyperion/logo_negativ.png');
//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);
}
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) {
var light = {};
light.host = device.hostname;
light.port = device.port;
if (device.txt) {
light.name = device.name;
light.model = device.txt.md;
//Yeelight does not provide correct API port with mDNS response, use default one
light.port = 55443;
}
else {
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) {
var 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() {
// Model mappings, see https://www.home-assistant.io/integrations/yeelight/
var models = ['color', 'color1', 'YLDP02YL', 'YLDP02YL', 'color2', 'YLDP06YL', 'color4', 'YLDP13YL', 'stripe', 'YLDD04YL', 'strip1', 'YLDD01YL', 'YLDD02YL'];
// 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 || window.readOnlyMode)
$('#btn_wiz_save').attr("disabled", true);
else
$('#btn_wiz_save').attr("disabled", false);
});
$('.yee_sel_watch').trigger('change');
}
else {
var noLightsTxt = '
' + $.i18n('wiz_noLights', 'Yeelights') + '
';
$('#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);
}
}
async function identify_yeelight_device(hostname, port) {
// Take care that new record cannot be save during background process
$('#btn_wiz_save').attr('disabled', true);
let params = { hostname: hostname, port: port };
await requestLedDeviceIdentification("yeelight", params);
if (!window.readOnlyMode) {
$('#btn_wiz_save').attr('disabled', false);
}
}
//****************************
// 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') + '');
if (getStorage("darkMode", false) == "on")
$('#wizard_logo').attr("src", 'img/hyperion/logo_negativ.png');
//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 = [];
var 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);
}
async function discover_atmoorb_lights(multiCastGroup, multiCastPort) {
var light = {};
var params = {};
if (multiCastGroup !== "") {
params.multiCastGroup = multiCastGroup;
}
if (multiCastPort !== 0) {
params.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) {
var 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) {
var 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 || window.readOnlyMode)
$('#btn_wiz_save').attr("disabled", true);
else
$('#btn_wiz_save').attr("disabled", false);
});
$('.orb_sel_watch').trigger('change');
}
else {
var noLightsTxt = '
' + $.i18n('wiz_noLights', 'AtmoOrbs') + '
';
$('#wizp2_body').append(noLightsTxt);
}
}
async function identify_atmoorb_device(orbId) {
// Take care that new record cannot be save during background process
$('#btn_wiz_save').attr('disabled', true);
let params = { id: orbId };
await requestLedDeviceIdentification("atmoorb", params);
if (!window.readOnlyMode) {
$('#btn_wiz_save').attr('disabled', false);
}
}