Allows my home wiz lights to be timed on and off according to a schedule.
web/web-wiz-script.js@0:9af80a39adcc, 2021-05-10 (annotated)
- Committer:
- andrewboyson
- Date:
- Mon May 10 08:24:22 2021 +0000
- Revision:
- 0:9af80a39adcc
- Child:
- 1:a6b120e4031a
New version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 0:9af80a39adcc | 1 | //Wiz script |
andrewboyson | 0:9af80a39adcc | 2 | 'use strict'; |
andrewboyson | 0:9af80a39adcc | 3 | |
andrewboyson | 0:9af80a39adcc | 4 | let lights = ""; |
andrewboyson | 0:9af80a39adcc | 5 | let wizTrace = false; |
andrewboyson | 0:9af80a39adcc | 6 | let nowlocal = ""; |
andrewboyson | 0:9af80a39adcc | 7 | let nowutc = ""; |
andrewboyson | 0:9af80a39adcc | 8 | let sunrise = ""; |
andrewboyson | 0:9af80a39adcc | 9 | let sunset = ""; |
andrewboyson | 0:9af80a39adcc | 10 | let schedule = ""; |
andrewboyson | 0:9af80a39adcc | 11 | let macs = new Array(); |
andrewboyson | 0:9af80a39adcc | 12 | let names = new Array(); |
andrewboyson | 0:9af80a39adcc | 13 | let rooms = new Array(); |
andrewboyson | 0:9af80a39adcc | 14 | let ages = new Array(); |
andrewboyson | 0:9af80a39adcc | 15 | let signals = new Array(); |
andrewboyson | 0:9af80a39adcc | 16 | let ons = new Array(); |
andrewboyson | 0:9af80a39adcc | 17 | let dimmers = new Array(); |
andrewboyson | 0:9af80a39adcc | 18 | let schednames = new Array(); |
andrewboyson | 0:9af80a39adcc | 19 | let schedons = new Array(); |
andrewboyson | 0:9af80a39adcc | 20 | let schedoffs = new Array(); |
andrewboyson | 0:9af80a39adcc | 21 | let actons = new Array(); |
andrewboyson | 0:9af80a39adcc | 22 | let actoffs = new Array(); |
andrewboyson | 0:9af80a39adcc | 23 | let durations = new Array(); |
andrewboyson | 0:9af80a39adcc | 24 | |
andrewboyson | 0:9af80a39adcc | 25 | function makehm(minutes) |
andrewboyson | 0:9af80a39adcc | 26 | { |
andrewboyson | 0:9af80a39adcc | 27 | let isNegative = minutes < 0; |
andrewboyson | 0:9af80a39adcc | 28 | if (isNegative) minutes = -minutes; |
andrewboyson | 0:9af80a39adcc | 29 | minutes = Math.floor(minutes / 60).toString().padStart(2, '0') + 'h' + (minutes % 60).toString().padStart(2, '0'); |
andrewboyson | 0:9af80a39adcc | 30 | if (isNegative) minutes = '-' + minutes; |
andrewboyson | 0:9af80a39adcc | 31 | return minutes; |
andrewboyson | 0:9af80a39adcc | 32 | } |
andrewboyson | 0:9af80a39adcc | 33 | |
andrewboyson | 0:9af80a39adcc | 34 | function parseGeneral(topic) |
andrewboyson | 0:9af80a39adcc | 35 | { |
andrewboyson | 0:9af80a39adcc | 36 | let lines = topic.split('\n'); |
andrewboyson | 0:9af80a39adcc | 37 | wizTrace = lines[0] != '0'; |
andrewboyson | 0:9af80a39adcc | 38 | } |
andrewboyson | 0:9af80a39adcc | 39 | function addLight(line) |
andrewboyson | 0:9af80a39adcc | 40 | { |
andrewboyson | 0:9af80a39adcc | 41 | let fields = line.split('\t'); |
andrewboyson | 0:9af80a39adcc | 42 | macs .push(fields[0]); |
andrewboyson | 0:9af80a39adcc | 43 | names .push(fields[1]); |
andrewboyson | 0:9af80a39adcc | 44 | rooms .push(fields[2]); |
andrewboyson | 0:9af80a39adcc | 45 | ages .push(fields[3]); |
andrewboyson | 0:9af80a39adcc | 46 | signals.push(fields[4]); |
andrewboyson | 0:9af80a39adcc | 47 | ons .push(fields[5]); |
andrewboyson | 0:9af80a39adcc | 48 | dimmers.push(fields[6]); |
andrewboyson | 0:9af80a39adcc | 49 | } |
andrewboyson | 0:9af80a39adcc | 50 | function parseLights(topic) |
andrewboyson | 0:9af80a39adcc | 51 | { |
andrewboyson | 0:9af80a39adcc | 52 | macs = new Array(); |
andrewboyson | 0:9af80a39adcc | 53 | names = new Array(); |
andrewboyson | 0:9af80a39adcc | 54 | rooms = new Array(); |
andrewboyson | 0:9af80a39adcc | 55 | ages = new Array(); |
andrewboyson | 0:9af80a39adcc | 56 | signals = new Array(); |
andrewboyson | 0:9af80a39adcc | 57 | ons = new Array(); |
andrewboyson | 0:9af80a39adcc | 58 | dimmers = new Array(); |
andrewboyson | 0:9af80a39adcc | 59 | topic.split('\n').forEach(addLight); |
andrewboyson | 0:9af80a39adcc | 60 | } |
andrewboyson | 0:9af80a39adcc | 61 | function parseDaylight(topic) |
andrewboyson | 0:9af80a39adcc | 62 | { |
andrewboyson | 0:9af80a39adcc | 63 | let lines = topic.split('\n'); |
andrewboyson | 0:9af80a39adcc | 64 | nowlocal = Ajax.hexToSignedInt16(lines[0]); |
andrewboyson | 0:9af80a39adcc | 65 | nowutc = Ajax.hexToSignedInt16(lines[1]); |
andrewboyson | 0:9af80a39adcc | 66 | sunrise = Ajax.hexToSignedInt16(lines[2]); |
andrewboyson | 0:9af80a39adcc | 67 | sunset = Ajax.hexToSignedInt16(lines[3]); |
andrewboyson | 0:9af80a39adcc | 68 | } |
andrewboyson | 0:9af80a39adcc | 69 | function addSchedule(line) |
andrewboyson | 0:9af80a39adcc | 70 | { |
andrewboyson | 0:9af80a39adcc | 71 | let fields = line.split('\t'); |
andrewboyson | 0:9af80a39adcc | 72 | schednames.push(fields[0]); |
andrewboyson | 0:9af80a39adcc | 73 | schedons .push(fields[1]); |
andrewboyson | 0:9af80a39adcc | 74 | schedoffs .push(fields[2]); |
andrewboyson | 0:9af80a39adcc | 75 | actons .push(fields[3]); |
andrewboyson | 0:9af80a39adcc | 76 | actoffs .push(fields[4]); |
andrewboyson | 0:9af80a39adcc | 77 | durations .push(fields[5]); |
andrewboyson | 0:9af80a39adcc | 78 | } |
andrewboyson | 0:9af80a39adcc | 79 | function parseSchedules(topic) |
andrewboyson | 0:9af80a39adcc | 80 | { |
andrewboyson | 0:9af80a39adcc | 81 | schednames = new Array(); |
andrewboyson | 0:9af80a39adcc | 82 | schedons = new Array(); |
andrewboyson | 0:9af80a39adcc | 83 | schedoffs = new Array(); |
andrewboyson | 0:9af80a39adcc | 84 | actons = new Array(); |
andrewboyson | 0:9af80a39adcc | 85 | actoffs = new Array(); |
andrewboyson | 0:9af80a39adcc | 86 | durations = new Array(); |
andrewboyson | 0:9af80a39adcc | 87 | topic.split('\n').forEach(addSchedule); |
andrewboyson | 0:9af80a39adcc | 88 | } |
andrewboyson | 0:9af80a39adcc | 89 | function parse() |
andrewboyson | 0:9af80a39adcc | 90 | { |
andrewboyson | 0:9af80a39adcc | 91 | let topics = Ajax.response.split('\f'); |
andrewboyson | 0:9af80a39adcc | 92 | parseGeneral (topics[0]); |
andrewboyson | 0:9af80a39adcc | 93 | lights = topics[1]; |
andrewboyson | 0:9af80a39adcc | 94 | parseLights (topics[1]); |
andrewboyson | 0:9af80a39adcc | 95 | parseDaylight (topics[2]); |
andrewboyson | 0:9af80a39adcc | 96 | schedule = topics[3]; |
andrewboyson | 0:9af80a39adcc | 97 | parseSchedules(topics[3]); |
andrewboyson | 0:9af80a39adcc | 98 | } |
andrewboyson | 0:9af80a39adcc | 99 | function display() |
andrewboyson | 0:9af80a39adcc | 100 | { |
andrewboyson | 0:9af80a39adcc | 101 | let elem; |
andrewboyson | 0:9af80a39adcc | 102 | elem = Ajax.getElementOrNull('ajax-wiz-lights' ); if (elem) elem.textContent = lights; |
andrewboyson | 0:9af80a39adcc | 103 | elem = Ajax.getElementOrNull('ajax-wiz-trace' ); if (elem) elem.setAttribute('dir', wizTrace ? 'rtl' : 'ltr'); |
andrewboyson | 0:9af80a39adcc | 104 | elem = Ajax.getElementOrNull('ajax-now-local' ); if (elem) elem.textContent = makehm(nowlocal); |
andrewboyson | 0:9af80a39adcc | 105 | elem = Ajax.getElementOrNull('ajax-now-utc' ); if (elem) elem.textContent = makehm(nowutc ); |
andrewboyson | 0:9af80a39adcc | 106 | elem = Ajax.getElementOrNull('ajax-sun-rise' ); if (elem) elem.textContent = makehm(sunrise ); |
andrewboyson | 0:9af80a39adcc | 107 | elem = Ajax.getElementOrNull('ajax-sun-set' ); if (elem) elem.textContent = makehm(sunset ); |
andrewboyson | 0:9af80a39adcc | 108 | elem = Ajax.getElementOrNull('ajax-wiz-schedule'); if (elem) elem.textContent = schedule; |
andrewboyson | 0:9af80a39adcc | 109 | for (let i = 0; i < 15; i++) |
andrewboyson | 0:9af80a39adcc | 110 | { |
andrewboyson | 0:9af80a39adcc | 111 | elem = Ajax.getElementOrNull('ajax-mac-' + i); if (elem) elem.textContent = macs [i] != null ? macs [i] : ''; |
andrewboyson | 0:9af80a39adcc | 112 | elem = Ajax.getElementOrNull('ajax-name-' + i); if (elem) elem.value = names [i] != null ? names [i] : ''; |
andrewboyson | 0:9af80a39adcc | 113 | elem = Ajax.getElementOrNull('ajax-room-' + i); if (elem) elem.value = rooms [i] != null ? rooms [i] : ''; |
andrewboyson | 0:9af80a39adcc | 114 | elem = Ajax.getElementOrNull('ajax-age-' + i); if (elem) elem.textContent = ages [i] != null ? ages [i] : ''; |
andrewboyson | 0:9af80a39adcc | 115 | elem = Ajax.getElementOrNull('ajax-signal-' + i); if (elem) elem.textContent = signals[i] != null ? signals[i] : ''; |
andrewboyson | 0:9af80a39adcc | 116 | elem = Ajax.getElementOrNull('ajax-on-' + i); if (elem) elem.textContent = ons [i] != null ? ons [i] : ''; |
andrewboyson | 0:9af80a39adcc | 117 | elem = Ajax.getElementOrNull('ajax-dimmer-' + i); if (elem) elem.textContent = dimmers[i] != null ? dimmers[i] : ''; |
andrewboyson | 0:9af80a39adcc | 118 | } |
andrewboyson | 0:9af80a39adcc | 119 | for (let i = 0; i < 5; i++) |
andrewboyson | 0:9af80a39adcc | 120 | { |
andrewboyson | 0:9af80a39adcc | 121 | elem = Ajax.getElementOrNull('sched-name-' + i); if (elem) elem.value = schednames[i] != null ? schednames[i] : ''; |
andrewboyson | 0:9af80a39adcc | 122 | elem = Ajax.getElementOrNull('sched-on-' + i); if (elem) elem.value = schedons [i] != null ? schedons [i] : ''; |
andrewboyson | 0:9af80a39adcc | 123 | elem = Ajax.getElementOrNull('sched-off-' + i); if (elem) elem.value = schedoffs [i] != null ? schedoffs [i] : ''; |
andrewboyson | 0:9af80a39adcc | 124 | elem = Ajax.getElementOrNull('act-on-' + i); if (elem) elem.textContent = actons [i] != null ? actons [i] : ''; |
andrewboyson | 0:9af80a39adcc | 125 | elem = Ajax.getElementOrNull('act-off-' + i); if (elem) elem.textContent = actoffs [i] != null ? actoffs [i] : ''; |
andrewboyson | 0:9af80a39adcc | 126 | elem = Ajax.getElementOrNull('duration-' + i); if (elem) elem.textContent = durations [i] != null ? durations [i] : ''; |
andrewboyson | 0:9af80a39adcc | 127 | } |
andrewboyson | 0:9af80a39adcc | 128 | } |
andrewboyson | 0:9af80a39adcc | 129 | |
andrewboyson | 0:9af80a39adcc | 130 | Ajax.server = '/wiz-ajax'; |
andrewboyson | 0:9af80a39adcc | 131 | Ajax.onResponse = function() { parse(); display(); }; |
andrewboyson | 0:9af80a39adcc | 132 | Ajax.init(); |