Andrew Boyson / web

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Thu Apr 18 15:26:29 2019 +0000
Revision:
95:8c9dda8a0caf
Parent:
94:d7226b2c14b6
Child:
96:eb2eb75bad0f
Used class to encapsulate ajax part of js in base.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 94:d7226b2c14b6 1 "//Clock script\n"
andrewboyson 14:c3c43c8faf0e 2 "'use strict';\n"
andrewboyson 14:c3c43c8faf0e 3 "\n"
andrewboyson 95:8c9dda8a0caf 4 "let pseudo = new Clock();\n"
andrewboyson 95:8c9dda8a0caf 5 "let rtc = new Clock();\n"
andrewboyson 94:d7226b2c14b6 6 "\n"
andrewboyson 94:d7226b2c14b6 7 "let pseudoDisplay = false;\n"
andrewboyson 94:d7226b2c14b6 8 "let pseudoStartMs = 0;\n"
andrewboyson 94:d7226b2c14b6 9 "\n"
andrewboyson 94:d7226b2c14b6 10 "let diffMs = 0;\n"
andrewboyson 94:d7226b2c14b6 11 "let rtcIsSet = false;\n"
andrewboyson 94:d7226b2c14b6 12 "let clockIsSet = false;\n"
andrewboyson 94:d7226b2c14b6 13 "let sourceIsOk = false;\n"
andrewboyson 94:d7226b2c14b6 14 "let rateIsLocked = false;\n"
andrewboyson 94:d7226b2c14b6 15 "let timeIsLocked = false;\n"
andrewboyson 14:c3c43c8faf0e 16 "\n"
andrewboyson 94:d7226b2c14b6 17 "let ppb = 0;\n"
andrewboyson 94:d7226b2c14b6 18 "let ppbdivisor = 0;\n"
andrewboyson 94:d7226b2c14b6 19 "let ppbmaxchange = 0;\n"
andrewboyson 94:d7226b2c14b6 20 "let syncedlimitppb = 0;\n"
andrewboyson 94:d7226b2c14b6 21 "let syncedhysppb = 0;\n"
andrewboyson 94:d7226b2c14b6 22 "let slewdivisor = 0;\n"
andrewboyson 94:d7226b2c14b6 23 "let slewmax = 0;\n"
andrewboyson 94:d7226b2c14b6 24 "let syncedlimitns = 0;\n"
andrewboyson 94:d7226b2c14b6 25 "let syncedhysns = 0;\n"
andrewboyson 94:d7226b2c14b6 26 "let maxoffsetsecs = 0;\n"
andrewboyson 94:d7226b2c14b6 27 "let govTrace = false;\n"
andrewboyson 14:c3c43c8faf0e 28 "\n"
andrewboyson 94:d7226b2c14b6 29 "let ntpserver = '';\n"
andrewboyson 94:d7226b2c14b6 30 "let ntpinitial = 0;\n"
andrewboyson 94:d7226b2c14b6 31 "let ntpnormal = 0;\n"
andrewboyson 94:d7226b2c14b6 32 "let ntpretry = 0;\n"
andrewboyson 94:d7226b2c14b6 33 "let ntpoffset = 0;\n"
andrewboyson 94:d7226b2c14b6 34 "let ntpmaxdelay = 0;\n"
andrewboyson 94:d7226b2c14b6 35 "\n"
andrewboyson 94:d7226b2c14b6 36 "let scanavg = 0;\n"
andrewboyson 94:d7226b2c14b6 37 "let scanmax = 0;\n"
andrewboyson 94:d7226b2c14b6 38 "let scanmin = 0;\n"
andrewboyson 94:d7226b2c14b6 39 "\n"
andrewboyson 94:d7226b2c14b6 40 "const DISPLAY_LEAP_MS = 10000;\n"
andrewboyson 94:d7226b2c14b6 41 "\n"
andrewboyson 94:d7226b2c14b6 42 "function parseDate()\n"
andrewboyson 14:c3c43c8faf0e 43 "{\n"
andrewboyson 95:8c9dda8a0caf 44 " let iDateStart = Ajax.headers.toLowerCase().indexOf('date:');\n"
andrewboyson 95:8c9dda8a0caf 45 " let iDateEnd = Ajax.headers.indexOf('\\r', iDateStart);\n"
andrewboyson 95:8c9dda8a0caf 46 " let rtcDate = new Date(Ajax.headers.slice(iDateStart + 5, iDateEnd));\n"
andrewboyson 94:d7226b2c14b6 47 " rtc.ms = rtcDate.getTime();\n"
andrewboyson 14:c3c43c8faf0e 48 "}\n"
andrewboyson 14:c3c43c8faf0e 49 "\n"
andrewboyson 94:d7226b2c14b6 50 "function parseLinesTime(text)\n"
andrewboyson 14:c3c43c8faf0e 51 "{\n"
andrewboyson 94:d7226b2c14b6 52 " let lines = text.split('\\n');\n"
andrewboyson 94:d7226b2c14b6 53 " rtc.ms += parseInt(lines[0], 16);\n"
andrewboyson 95:8c9dda8a0caf 54 " rtc.ms -= Ajax.ms;\n"
andrewboyson 95:8c9dda8a0caf 55 " diffMs = rtc.ms + Ajax.ms - Date.now();\n"
andrewboyson 95:8c9dda8a0caf 56 " rtcIsSet = Ajax.hexToBit(lines[1], 0);\n"
andrewboyson 95:8c9dda8a0caf 57 " clockIsSet = Ajax.hexToBit(lines[1], 1);\n"
andrewboyson 95:8c9dda8a0caf 58 " sourceIsOk = Ajax.hexToBit(lines[1], 2);\n"
andrewboyson 95:8c9dda8a0caf 59 " rateIsLocked = Ajax.hexToBit(lines[1], 3);\n"
andrewboyson 95:8c9dda8a0caf 60 " timeIsLocked = Ajax.hexToBit(lines[1], 4);\n"
andrewboyson 95:8c9dda8a0caf 61 " rtc.leapEnable = Ajax.hexToBit(lines[1], 5);\n"
andrewboyson 95:8c9dda8a0caf 62 " rtc.leapForward = Ajax.hexToBit(lines[1], 6);\n"
andrewboyson 95:8c9dda8a0caf 63 " govTrace = Ajax.hexToBit(lines[1], 7);\n"
andrewboyson 95:8c9dda8a0caf 64 " rtc.months1970 = parseInt(lines[2], 16);\n"
andrewboyson 94:d7226b2c14b6 65 " rtc.leaps = parseInt(lines[3], 16);\n"
andrewboyson 14:c3c43c8faf0e 66 "}\n"
andrewboyson 94:d7226b2c14b6 67 "function parseLinesGov(text)\n"
andrewboyson 14:c3c43c8faf0e 68 "{\n"
andrewboyson 94:d7226b2c14b6 69 " let lines = text.split('\\n');\n"
andrewboyson 94:d7226b2c14b6 70 " ppb = parseInt(lines[0], 16);\n"
andrewboyson 94:d7226b2c14b6 71 " ppbdivisor = parseInt(lines[1], 16);\n"
andrewboyson 94:d7226b2c14b6 72 " ppbmaxchange = parseInt(lines[2], 16);\n"
andrewboyson 94:d7226b2c14b6 73 " syncedlimitppb = parseInt(lines[3], 16);\n"
andrewboyson 94:d7226b2c14b6 74 " syncedhysppb = parseInt(lines[4], 16);\n"
andrewboyson 94:d7226b2c14b6 75 " slewdivisor = parseInt(lines[5], 16);\n"
andrewboyson 94:d7226b2c14b6 76 " slewmax = parseInt(lines[6], 16);\n"
andrewboyson 94:d7226b2c14b6 77 " syncedlimitns = parseInt(lines[7], 16);\n"
andrewboyson 94:d7226b2c14b6 78 " syncedhysns = parseInt(lines[8], 16);\n"
andrewboyson 94:d7226b2c14b6 79 " maxoffsetsecs = parseInt(lines[9], 16);\n"
andrewboyson 14:c3c43c8faf0e 80 "}\n"
andrewboyson 94:d7226b2c14b6 81 "function parseLinesNtp(text)\n"
andrewboyson 94:d7226b2c14b6 82 "{\n"
andrewboyson 94:d7226b2c14b6 83 " let lines = text.split('\\n');\n"
andrewboyson 94:d7226b2c14b6 84 " ntpserver = lines[0];\n"
andrewboyson 94:d7226b2c14b6 85 " ntpinitial = parseInt(lines[1], 16);\n"
andrewboyson 94:d7226b2c14b6 86 " ntpnormal = parseInt(lines[2], 16);\n"
andrewboyson 94:d7226b2c14b6 87 " ntpretry = parseInt(lines[3], 16);\n"
andrewboyson 94:d7226b2c14b6 88 " ntpoffset = parseInt(lines[4], 16);\n"
andrewboyson 94:d7226b2c14b6 89 " ntpmaxdelay = parseInt(lines[5], 16);\n"
andrewboyson 94:d7226b2c14b6 90 "}\n"
andrewboyson 94:d7226b2c14b6 91 "function parseLinesScan(text)\n"
andrewboyson 14:c3c43c8faf0e 92 "{\n"
andrewboyson 94:d7226b2c14b6 93 " let lines = text.split('\\n');\n"
andrewboyson 94:d7226b2c14b6 94 " scanavg = parseInt(lines[0], 16);\n"
andrewboyson 94:d7226b2c14b6 95 " scanmax = parseInt(lines[1], 16);\n"
andrewboyson 94:d7226b2c14b6 96 " scanmin = parseInt(lines[2], 16);\n"
andrewboyson 94:d7226b2c14b6 97 "}\n"
andrewboyson 95:8c9dda8a0caf 98 "function parse()\n"
andrewboyson 94:d7226b2c14b6 99 "{\n"
andrewboyson 95:8c9dda8a0caf 100 " let topics = Ajax.response.split('\\f');\n"
andrewboyson 94:d7226b2c14b6 101 " parseDate();\n"
andrewboyson 94:d7226b2c14b6 102 " parseLinesTime(topics[0]);\n"
andrewboyson 94:d7226b2c14b6 103 " parseLinesGov (topics[1]);\n"
andrewboyson 94:d7226b2c14b6 104 " parseLinesNtp (topics[2]);\n"
andrewboyson 94:d7226b2c14b6 105 " parseLinesScan(topics[3]);\n"
andrewboyson 94:d7226b2c14b6 106 "}\n"
andrewboyson 95:8c9dda8a0caf 107 "function display()\n"
andrewboyson 94:d7226b2c14b6 108 "{\n"
andrewboyson 94:d7226b2c14b6 109 " let elem;\n"
andrewboyson 95:8c9dda8a0caf 110 " elem = Ajax.getElementOrNull('ajax-rtc-set' ); if (elem) elem.setAttribute('dir', rtcIsSet ? 'rtl' : 'ltr');\n"
andrewboyson 95:8c9dda8a0caf 111 " elem = Ajax.getElementOrNull('ajax-clock-set' ); if (elem) elem.setAttribute('dir', clockIsSet ? 'rtl' : 'ltr');\n"
andrewboyson 95:8c9dda8a0caf 112 " elem = Ajax.getElementOrNull('ajax-source-ok' ); if (elem) elem.setAttribute('dir', sourceIsOk ? 'rtl' : 'ltr');\n"
andrewboyson 95:8c9dda8a0caf 113 " elem = Ajax.getElementOrNull('ajax-rate-locked' ); if (elem) elem.setAttribute('dir', rateIsLocked ? 'rtl' : 'ltr');\n"
andrewboyson 95:8c9dda8a0caf 114 " elem = Ajax.getElementOrNull('ajax-time-locked' ); if (elem) elem.setAttribute('dir', timeIsLocked ? 'rtl' : 'ltr');\n"
andrewboyson 14:c3c43c8faf0e 115 " \n"
andrewboyson 95:8c9dda8a0caf 116 " elem = Ajax.getElementOrNull('ajax-leap-enable' ); if (elem) elem.setAttribute('dir', rtc.leapEnable ? 'rtl' : 'ltr');\n"
andrewboyson 95:8c9dda8a0caf 117 " elem = Ajax.getElementOrNull('ajax-leap-forward' ); if (elem) elem.setAttribute('dir', rtc.leapForward ? 'rtl' : 'ltr');\n"
andrewboyson 14:c3c43c8faf0e 118 " \n"
andrewboyson 95:8c9dda8a0caf 119 " elem = Ajax.getElementOrNull('ajax-leap-year' ); if (elem) elem.value = rtc.months1970 ? rtc.leapYear : '';\n"
andrewboyson 95:8c9dda8a0caf 120 " elem = Ajax.getElementOrNull('ajax-leap-month' ); if (elem) elem.value = rtc.months1970 ? rtc.leapMonth : '';\n"
andrewboyson 94:d7226b2c14b6 121 " \n"
andrewboyson 95:8c9dda8a0caf 122 " elem = Ajax.getElementOrNull('ajax-leap-count' ); if (elem) elem.value = rtc.leaps;\n"
andrewboyson 94:d7226b2c14b6 123 " \n"
andrewboyson 95:8c9dda8a0caf 124 " elem = Ajax.getElementOrNull('ajax-ppb' ); if (elem) elem.value = ppb;\n"
andrewboyson 95:8c9dda8a0caf 125 " elem = Ajax.getElementOrNull('ajax-ppb-divisor' ); if (elem) elem.value = ppbdivisor;\n"
andrewboyson 95:8c9dda8a0caf 126 " elem = Ajax.getElementOrNull('ajax-ppb-max-chg' ); if (elem) elem.value = ppbmaxchange;\n"
andrewboyson 95:8c9dda8a0caf 127 " elem = Ajax.getElementOrNull('ajax-ppb-syn-lim' ); if (elem) elem.value = syncedlimitppb;\n"
andrewboyson 95:8c9dda8a0caf 128 " elem = Ajax.getElementOrNull('ajax-ppb-syn-hys' ); if (elem) elem.value = syncedhysppb;\n"
andrewboyson 95:8c9dda8a0caf 129 " elem = Ajax.getElementOrNull('ajax-off-divisor' ); if (elem) elem.value = slewdivisor;\n"
andrewboyson 95:8c9dda8a0caf 130 " elem = Ajax.getElementOrNull('ajax-off-max' ); if (elem) elem.value = slewmax;\n"
andrewboyson 95:8c9dda8a0caf 131 " elem = Ajax.getElementOrNull('ajax-off-syn-lim' ); if (elem) elem.value = syncedlimitns / 1000000;\n"
andrewboyson 95:8c9dda8a0caf 132 " elem = Ajax.getElementOrNull('ajax-off-syn-hys' ); if (elem) elem.value = syncedhysns / 1000000;\n"
andrewboyson 95:8c9dda8a0caf 133 " elem = Ajax.getElementOrNull('ajax-off-rst-lim' ); if (elem) elem.value = maxoffsetsecs;\n"
andrewboyson 95:8c9dda8a0caf 134 " elem = Ajax.getElementOrNull('ajax-gov-trace' ); if (elem) elem.setAttribute('dir', govTrace ? 'rtl' : 'ltr');\n"
andrewboyson 94:d7226b2c14b6 135 " \n"
andrewboyson 95:8c9dda8a0caf 136 " elem = Ajax.getElementOrNull('ajax-ntp-server' ); if (elem) elem.value = ntpserver;\n"
andrewboyson 95:8c9dda8a0caf 137 " elem = Ajax.getElementOrNull('ajax-ntp-initial' ); if (elem) elem.value = ntpinitial;\n"
andrewboyson 95:8c9dda8a0caf 138 " elem = Ajax.getElementOrNull('ajax-ntp-normal' ); if (elem) elem.value = ntpnormal / 60;\n"
andrewboyson 95:8c9dda8a0caf 139 " elem = Ajax.getElementOrNull('ajax-ntp-retry' ); if (elem) elem.value = ntpretry;\n"
andrewboyson 95:8c9dda8a0caf 140 " elem = Ajax.getElementOrNull('ajax-ntp-offset' ); if (elem) elem.value = ntpoffset;\n"
andrewboyson 95:8c9dda8a0caf 141 " elem = Ajax.getElementOrNull('ajax-ntp-max-delay'); if (elem) elem.value = ntpmaxdelay;\n"
andrewboyson 94:d7226b2c14b6 142 " \n"
andrewboyson 95:8c9dda8a0caf 143 " elem = Ajax.getElementOrNull('ajax-scan-avg' ); if (elem) elem.textContent = scanavg;\n"
andrewboyson 95:8c9dda8a0caf 144 " elem = Ajax.getElementOrNull('ajax-scan-max' ); if (elem) elem.textContent = scanmax;\n"
andrewboyson 95:8c9dda8a0caf 145 " elem = Ajax.getElementOrNull('ajax-scan-min' ); if (elem) elem.textContent = scanmin;\n"
andrewboyson 94:d7226b2c14b6 146 " \n"
andrewboyson 95:8c9dda8a0caf 147 " elem = Ajax.getElementOrNull('ajax-date-diff' ); if (elem) elem.textContent = diffMs;\n"
andrewboyson 14:c3c43c8faf0e 148 "}\n"
andrewboyson 95:8c9dda8a0caf 149 "function handleTick() //This typically called every 100ms\n"
andrewboyson 14:c3c43c8faf0e 150 "{\n"
andrewboyson 94:d7226b2c14b6 151 " if (pseudoDisplay)\n"
andrewboyson 14:c3c43c8faf0e 152 " {\n"
andrewboyson 95:8c9dda8a0caf 153 " pseudo.adjustLeap (Ajax.ms);\n"
andrewboyson 95:8c9dda8a0caf 154 " pseudo.displayTime(Ajax.ms);\n"
andrewboyson 95:8c9dda8a0caf 155 " if (Ajax.ms >= pseudoStartMs + DISPLAY_LEAP_MS + 500) pseudoDisplay = false;\n"
andrewboyson 14:c3c43c8faf0e 156 " }\n"
andrewboyson 94:d7226b2c14b6 157 " else\n"
andrewboyson 94:d7226b2c14b6 158 " {\n"
andrewboyson 95:8c9dda8a0caf 159 " rtc.adjustLeap (Ajax.ms);\n"
andrewboyson 95:8c9dda8a0caf 160 " rtc.displayTime(Ajax.ms);\n"
andrewboyson 94:d7226b2c14b6 161 " }\n"
andrewboyson 14:c3c43c8faf0e 162 "}\n"
andrewboyson 14:c3c43c8faf0e 163 "\n"
andrewboyson 95:8c9dda8a0caf 164 "function displayLeap() //Called by display leap button in HTML\n"
andrewboyson 14:c3c43c8faf0e 165 "{\n"
andrewboyson 94:d7226b2c14b6 166 " pseudoDisplay = true;\n"
andrewboyson 95:8c9dda8a0caf 167 " pseudoStartMs = Ajax.ms;\n"
andrewboyson 94:d7226b2c14b6 168 " \n"
andrewboyson 94:d7226b2c14b6 169 " pseudo.leapEnable = true;\n"
andrewboyson 94:d7226b2c14b6 170 " pseudo.leapForward = rtc.leapForward;\n"
andrewboyson 94:d7226b2c14b6 171 " pseudo.leaps = rtc.leaps;\n"
andrewboyson 94:d7226b2c14b6 172 " pseudo.leapMonth = rtc.leapMonth;\n"
andrewboyson 94:d7226b2c14b6 173 " pseudo.leapYear = rtc.leapYear;\n"
andrewboyson 95:8c9dda8a0caf 174 " pseudo.ms = Date.UTC(rtc.leapYear, rtc.leapMonth - 1, 1) - DISPLAY_LEAP_MS / 2 - Ajax.ms;\n"
andrewboyson 14:c3c43c8faf0e 175 "}\n"
andrewboyson 95:8c9dda8a0caf 176 "Ajax.server = '/clock-ajax';\n"
andrewboyson 95:8c9dda8a0caf 177 "Ajax.onResponse = function() { parse(); display(); };\n"
andrewboyson 95:8c9dda8a0caf 178 "Ajax.onTick = handleTick;\n"
andrewboyson 95:8c9dda8a0caf 179 "Ajax.init();"