Allows my home wiz lights to be timed on and off according to a schedule.

Dependents:   heating

web/web-wiz-script.inc

Committer:
andrewboyson
Date:
2021-06-09
Revision:
7:3035a540ef65
Parent:
1:a6b120e4031a

File content as of revision 7:3035a540ef65:

"//Wiz script\n"
"'use strict';\n"
"\n"
"let lights   = \"\";\n"
"let wizTrace = false;\n"
"let nowlocal = \"\";\n"
"let nowutc   = \"\";\n"
"let sunrise  = \"\";\n"
"let sunset   = \"\";\n"
"let schedule = \"\";\n"
"let macs    = new Array();\n"
"let names   = new Array();\n"
"let rooms   = new Array();\n"
"let ages    = new Array();\n"
"let signals = new Array();\n"
"let ons     = new Array();\n"
"let dimmers = new Array();\n"
"let schednames = new Array();\n"
"let schedons   = new Array();\n"
"let schedoffs  = new Array();\n"
"let actons     = new Array();\n"
"let actoffs    = new Array();\n"
"let durations  = new Array();\n"
"\n"
"function makehm(minutes)\n"
"{\n"
"    let isNegative = minutes < 0;\n"
"    if (isNegative) minutes = -minutes;\n"
"    minutes = Math.floor(minutes / 60).toString().padStart(2, '0') + 'h' + (minutes % 60).toString().padStart(2, '0');\n"
"    if (isNegative) minutes = '-' + minutes;\n"
"    return minutes;\n"
"}\n"
"\n"
"function parseGeneral(topic)\n"
"{\n"
"    let lines = topic.split('\\n');\n"
"    wizTrace = lines[0] != '0';\n"
"}\n"
"function addLight(line)\n"
"{\n"
"    let fields = line.split('\\t');\n"
"    macs   .push(fields[0].substr(-8));\n"
"    names  .push(fields[1]);\n"
"    rooms  .push(fields[2]);\n"
"    ages   .push(fields[3]);\n"
"    signals.push(fields[4]);\n"
"    ons    .push(fields[5]);\n"
"    dimmers.push(fields[6]);\n"
"}\n"
"function parseLights(topic)\n"
"{\n"
"    macs    = new Array();\n"
"    names   = new Array();\n"
"    rooms   = new Array();\n"
"    ages    = new Array();\n"
"    signals = new Array();\n"
"    ons     = new Array();\n"
"    dimmers = new Array();\n"
"    topic.split('\\n').forEach(addLight);\n"
"}\n"
"function parseDaylight(topic)\n"
"{\n"
"    let lines = topic.split('\\n');\n"
"    nowlocal = Ajax.hexToSignedInt16(lines[0]);\n"
"    nowutc   = Ajax.hexToSignedInt16(lines[1]);\n"
"    sunrise  = Ajax.hexToSignedInt16(lines[2]);\n"
"    sunset   = Ajax.hexToSignedInt16(lines[3]);\n"
"}\n"
"function addSchedule(line)\n"
"{\n"
"    let fields = line.split('\\t');\n"
"    schednames.push(fields[0]);\n"
"    schedons  .push(fields[1]);\n"
"    schedoffs .push(fields[2]);\n"
"    actons    .push(fields[3]);\n"
"    actoffs   .push(fields[4]);\n"
"    durations .push(fields[5]);\n"
"}\n"
"function parseSchedules(topic)\n"
"{\n"
"    schednames = new Array();\n"
"    schedons   = new Array();\n"
"    schedoffs  = new Array();\n"
"    actons     = new Array();\n"
"    actoffs    = new Array();\n"
"    durations  = new Array();\n"
"    topic.split('\\n').forEach(addSchedule);\n"
"}\n"
"function parse()\n"
"{\n"
"    let topics = Ajax.response.split('\\f');\n"
"    parseGeneral  (topics[0]);\n"
"    lights       = topics[1];\n"
"    parseLights   (topics[1]);\n"
"    parseDaylight (topics[2]);\n"
"    schedule     = topics[3];\n"
"    parseSchedules(topics[3]);\n"
"}\n"
"function display()\n"
"{\n"
"    let elem;\n"
"    elem = Ajax.getElementOrNull('ajax-wiz-lights'  ); if (elem) elem.textContent = lights;\n"
"    elem = Ajax.getElementOrNull('ajax-wiz-trace'   ); if (elem) elem.setAttribute('dir', wizTrace ? 'rtl' : 'ltr');\n"
"    elem = Ajax.getElementOrNull('ajax-now-local'   ); if (elem) elem.textContent = makehm(nowlocal);\n"
"    elem = Ajax.getElementOrNull('ajax-now-utc'     ); if (elem) elem.textContent = makehm(nowutc  );\n"
"    elem = Ajax.getElementOrNull('ajax-sun-rise'    ); if (elem) elem.textContent = makehm(sunrise );\n"
"    elem = Ajax.getElementOrNull('ajax-sun-set'     ); if (elem) elem.textContent = makehm(sunset  );\n"
"    elem = Ajax.getElementOrNull('ajax-wiz-schedule'); if (elem) elem.textContent = schedule;\n"
"    for (let i = 0; i < 15; i++)\n"
"    {\n"
"        elem = Ajax.getElementOrNull('ajax-mac-'    + i); if (elem) elem.textContent =   macs   [i] != null ? macs   [i] : '';\n"
"        elem = Ajax.getElementOrNull('ajax-name-'   + i); if (elem) elem.value       =   names  [i] != null ? names  [i] : '';\n"
"        elem = Ajax.getElementOrNull('ajax-room-'   + i); if (elem) elem.value       =   rooms  [i] != null ? rooms  [i] : '';\n"
"        elem = Ajax.getElementOrNull('ajax-age-'    + i); if (elem) elem.textContent =   ages   [i] != null ? ages   [i] : '';\n"
"        elem = Ajax.getElementOrNull('ajax-signal-' + i); if (elem) elem.textContent =   signals[i] != null ? signals[i] : '';\n"
"        elem = Ajax.getElementOrNull('ajax-on-'     + i); if (elem) elem.textContent =       ons[i] != null ? ons    [i] : '';\n"
"        elem = Ajax.getElementOrNull('ajax-led-'    + i); if (elem) elem.setAttribute('dir', ons[i] == '1' ? 'rtl' : 'ltr')  ;\n"
"        elem = Ajax.getElementOrNull('ajax-dimmer-' + i); if (elem) elem.textContent =   dimmers[i] != null ? dimmers[i] : '';\n"
"    }\n"
"    for (let i = 0; i < 5; i++)\n"
"    {\n"
"        elem = Ajax.getElementOrNull('sched-name-' + i); if (elem) elem.value       = schednames[i] != null ? schednames[i] : '';\n"
"        elem = Ajax.getElementOrNull('sched-on-'   + i); if (elem) elem.value       = schedons  [i] != null ? schedons  [i] : '';\n"
"        elem = Ajax.getElementOrNull('sched-off-'  + i); if (elem) elem.value       = schedoffs [i] != null ? schedoffs [i] : '';\n"
"        elem = Ajax.getElementOrNull('act-on-'     + i); if (elem) elem.textContent = actons    [i] != null ? actons    [i] : '';\n"
"        elem = Ajax.getElementOrNull('act-off-'    + i); if (elem) elem.textContent = actoffs   [i] != null ? actoffs   [i] : '';\n"
"        elem = Ajax.getElementOrNull('duration-'   + i); if (elem) elem.textContent = durations [i] != null ? durations [i] : '';\n"
"    }\n"
"}\n"
"\n"
"Ajax.server     = '/wiz-ajax';\n"
"Ajax.onResponse = function() { parse(); display(); };\n"
"Ajax.init();\n"
""