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

Dependents:   heating

Committer:
andrewboyson
Date:
Wed Jun 09 09:23:54 2021 +0000
Revision:
7:3035a540ef65
Parent:
1:a6b120e4031a
Changed WizSchedMinutesUtcToLocal function so that local midnight in summer is 00h00 rather than 24h00.

Who changed what in which revision?

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