Andrew Boyson / web

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Sat Mar 23 12:26:49 2019 +0000
Revision:
77:4689596a2f3f
Parent:
46:1822fdbe6c0c
Child:
94:d7226b2c14b6
Changed from using float to using flex for creating left and right aligned lines.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 14:c3c43c8faf0e 1 "'use strict';\n"
andrewboyson 14:c3c43c8faf0e 2 "\n"
andrewboyson 14:c3c43c8faf0e 3 "var response = '';\n"
andrewboyson 14:c3c43c8faf0e 4 "var headers = '';\n"
andrewboyson 77:4689596a2f3f 5 "var msRtc = 0; //nibbles 0 to 3: 16 bits\n"
andrewboyson 14:c3c43c8faf0e 6 "var msCountAtRtcSet = 0;\n"
andrewboyson 14:c3c43c8faf0e 7 "var msDiff = 0;\n"
andrewboyson 77:4689596a2f3f 8 "var rtcIsSet = false; //nibble 4 : bit 0\n"
andrewboyson 77:4689596a2f3f 9 "var clockIsSet = false; //nibble 4 : bit 1\n"
andrewboyson 77:4689596a2f3f 10 "var sourceIsOk = false; //nibble 4 : bit 2\n"
andrewboyson 77:4689596a2f3f 11 "var rateIsLocked = false; //nibble 4 : bit 3\n"
andrewboyson 77:4689596a2f3f 12 "var timeIsLocked = false; //nibble 5 : bit 0\n"
andrewboyson 77:4689596a2f3f 13 "var leapEnable = false; //nibble 5 : bit 1\n"
andrewboyson 77:4689596a2f3f 14 "var leapForward = false; //nibble 5 : bit 2\n"
andrewboyson 77:4689596a2f3f 15 "var leapmonths1970 = 0; //nibbles 6 to 8: 12 bits\n"
andrewboyson 14:c3c43c8faf0e 16 "var leapmonth = 0;\n"
andrewboyson 14:c3c43c8faf0e 17 "var leapyear = 0;\n"
andrewboyson 77:4689596a2f3f 18 "var leaps = 0; //nibbles 9 to 12: 16 bits\n"
andrewboyson 77:4689596a2f3f 19 "var ppb = 0; //nibbles 13 to 20: 32 bits\n"
andrewboyson 77:4689596a2f3f 20 "var scanavg = 0; //nibbles 21 to 28: 32 bits\n"
andrewboyson 77:4689596a2f3f 21 "var scanmax = 0; //nibbles 29 to 36: 32 bits\n"
andrewboyson 77:4689596a2f3f 22 "var scanmin = 0; //nibbles 37 to 44: 32 bits\n"
andrewboyson 14:c3c43c8faf0e 23 "var msCount = 0;\n"
andrewboyson 14:c3c43c8faf0e 24 "\n"
andrewboyson 14:c3c43c8faf0e 25 "const TICK_MS = 100;\n"
andrewboyson 14:c3c43c8faf0e 26 "const AJAX_REFRESH_MS = 10000;\n"
andrewboyson 14:c3c43c8faf0e 27 "const AJAX_QUIET_MS = 3000;\n"
andrewboyson 14:c3c43c8faf0e 28 "\n"
andrewboyson 14:c3c43c8faf0e 29 "function hexToBit(iChar, iBit)\n"
andrewboyson 14:c3c43c8faf0e 30 "{\n"
andrewboyson 14:c3c43c8faf0e 31 " var value = parseInt(response.charAt(iChar), 16);\n"
andrewboyson 14:c3c43c8faf0e 32 " value >>= iBit;\n"
andrewboyson 14:c3c43c8faf0e 33 " return value & 1;\n"
andrewboyson 14:c3c43c8faf0e 34 "}\n"
andrewboyson 14:c3c43c8faf0e 35 "function parseAjax()\n"
andrewboyson 14:c3c43c8faf0e 36 "{\n"
andrewboyson 40:1dc51ae32787 37 " var iDateStart = headers.toLowerCase().indexOf('date:');\n"
andrewboyson 14:c3c43c8faf0e 38 " var iDateEnd = headers.indexOf('\\r', iDateStart);\n"
andrewboyson 14:c3c43c8faf0e 39 " var rtcDate = new Date(headers.slice(iDateStart + 5, iDateEnd));\n"
andrewboyson 14:c3c43c8faf0e 40 " msRtc = parseInt(response.substr(0, 4), 16);\n"
andrewboyson 14:c3c43c8faf0e 41 " msRtc += rtcDate.getTime();\n"
andrewboyson 14:c3c43c8faf0e 42 " msDiff = msRtc - Date.now();\n"
andrewboyson 14:c3c43c8faf0e 43 " msCountAtRtcSet = msCount;\n"
andrewboyson 22:f45bbb468439 44 " rtcIsSet = hexToBit(4, 0);\n"
andrewboyson 22:f45bbb468439 45 " clockIsSet = hexToBit(4, 1);\n"
andrewboyson 15:2cea2bbd5046 46 " sourceIsOk = hexToBit(4, 2);\n"
andrewboyson 15:2cea2bbd5046 47 " rateIsLocked = hexToBit(4, 3);\n"
andrewboyson 15:2cea2bbd5046 48 " timeIsLocked = hexToBit(5, 0);\n"
andrewboyson 15:2cea2bbd5046 49 " leapEnable = hexToBit(5, 1);\n"
andrewboyson 24:0ed9f1faea96 50 " leapForward = hexToBit(5, 2);\n"
andrewboyson 14:c3c43c8faf0e 51 " leapmonths1970 = parseInt(response.substr(6, 3), 16);\n"
andrewboyson 14:c3c43c8faf0e 52 " leapmonth = leapmonths1970 % 12;\n"
andrewboyson 14:c3c43c8faf0e 53 " leapyear = (leapmonths1970 - leapmonth) / 12;\n"
andrewboyson 14:c3c43c8faf0e 54 " leapmonth += 1;\n"
andrewboyson 14:c3c43c8faf0e 55 " leapyear += 1970;\n"
andrewboyson 77:4689596a2f3f 56 " leaps = parseInt(response.substr( 9, 4), 16);\n"
andrewboyson 77:4689596a2f3f 57 " ppb = parseInt(response.substr(13, 8), 16);\n"
andrewboyson 77:4689596a2f3f 58 " scanavg = parseInt(response.substr(21, 8), 16);\n"
andrewboyson 77:4689596a2f3f 59 " scanmax = parseInt(response.substr(29, 8), 16);\n"
andrewboyson 77:4689596a2f3f 60 " scanmin = parseInt(response.substr(37, 8), 16);\n"
andrewboyson 14:c3c43c8faf0e 61 "}\n"
andrewboyson 14:c3c43c8faf0e 62 "function displayGeneral()\n"
andrewboyson 14:c3c43c8faf0e 63 "{\n"
andrewboyson 14:c3c43c8faf0e 64 " var elem;\n"
andrewboyson 15:2cea2bbd5046 65 " elem = document.getElementById('ajax-rtc-set' ); if (elem) elem.setAttribute('dir', rtcIsSet ? 'rtl' : 'ltr');\n"
andrewboyson 15:2cea2bbd5046 66 " elem = document.getElementById('ajax-clock-set' ); if (elem) elem.setAttribute('dir', clockIsSet ? 'rtl' : 'ltr');\n"
andrewboyson 15:2cea2bbd5046 67 " elem = document.getElementById('ajax-source-ok' ); if (elem) elem.setAttribute('dir', sourceIsOk ? 'rtl' : 'ltr');\n"
andrewboyson 14:c3c43c8faf0e 68 " elem = document.getElementById('ajax-rate-locked' ); if (elem) elem.setAttribute('dir', rateIsLocked ? 'rtl' : 'ltr');\n"
andrewboyson 14:c3c43c8faf0e 69 " elem = document.getElementById('ajax-time-locked' ); if (elem) elem.setAttribute('dir', timeIsLocked ? 'rtl' : 'ltr');\n"
andrewboyson 14:c3c43c8faf0e 70 "\n"
andrewboyson 14:c3c43c8faf0e 71 " elem = document.getElementById('ajax-leap-enable' ); if (elem) elem.setAttribute('dir', leapEnable ? 'rtl' : 'ltr');\n"
andrewboyson 24:0ed9f1faea96 72 " elem = document.getElementById('ajax-leap-forward' ); if (elem) elem.setAttribute('dir', leapForward ? 'rtl' : 'ltr');\n"
andrewboyson 14:c3c43c8faf0e 73 "\n"
andrewboyson 14:c3c43c8faf0e 74 " elem = document.getElementById('ajax-leap-year' ); if (elem) elem.value = leapmonths1970 ? leapyear : '';\n"
andrewboyson 14:c3c43c8faf0e 75 " elem = document.getElementById('ajax-leap-month' ); if (elem) elem.value = leapmonths1970 ? leapmonth : '';\n"
andrewboyson 14:c3c43c8faf0e 76 "\n"
andrewboyson 14:c3c43c8faf0e 77 " elem = document.getElementById('ajax-leap-count' ); if (elem) elem.value = leaps;\n"
andrewboyson 14:c3c43c8faf0e 78 "\n"
andrewboyson 77:4689596a2f3f 79 " elem = document.getElementById('ajax-ppb' ); if (elem) elem.value = ppb;\n"
andrewboyson 77:4689596a2f3f 80 "\n"
andrewboyson 77:4689596a2f3f 81 " elem = document.getElementById('ajax-scan-avg' ); if (elem) elem.textContent = scanavg;\n"
andrewboyson 77:4689596a2f3f 82 " elem = document.getElementById('ajax-scan-max' ); if (elem) elem.textContent = scanmax;\n"
andrewboyson 77:4689596a2f3f 83 " elem = document.getElementById('ajax-scan-min' ); if (elem) elem.textContent = scanmin;\n"
andrewboyson 77:4689596a2f3f 84 "\n"
andrewboyson 77:4689596a2f3f 85 " elem = document.getElementById('ajax-response' ); if (elem) elem.textContent = response;\n"
andrewboyson 77:4689596a2f3f 86 " elem = document.getElementById('ajax-headers' ); if (elem) elem.textContent = headers;\n"
andrewboyson 14:c3c43c8faf0e 87 " \n"
andrewboyson 77:4689596a2f3f 88 " elem = document.getElementById('ajax-date-diff' ); if (elem) elem.textContent = msDiff;\n"
andrewboyson 14:c3c43c8faf0e 89 "}\n"
andrewboyson 14:c3c43c8faf0e 90 "\n"
andrewboyson 14:c3c43c8faf0e 91 "function formatNumbers00(i)\n"
andrewboyson 14:c3c43c8faf0e 92 "{\n"
andrewboyson 14:c3c43c8faf0e 93 " if (i<10) i='0' + i;\n"
andrewboyson 14:c3c43c8faf0e 94 " return i;\n"
andrewboyson 14:c3c43c8faf0e 95 "}\n"
andrewboyson 14:c3c43c8faf0e 96 "function formatDayOfWeek(wday)\n"
andrewboyson 14:c3c43c8faf0e 97 "{\n"
andrewboyson 14:c3c43c8faf0e 98 " switch(wday)\n"
andrewboyson 14:c3c43c8faf0e 99 " {\n"
andrewboyson 14:c3c43c8faf0e 100 " case 0: return 'Sun';\n"
andrewboyson 14:c3c43c8faf0e 101 " case 1: return 'Mon';\n"
andrewboyson 14:c3c43c8faf0e 102 " case 2: return 'Tue';\n"
andrewboyson 14:c3c43c8faf0e 103 " case 3: return 'Wed';\n"
andrewboyson 14:c3c43c8faf0e 104 " case 4: return 'Thu';\n"
andrewboyson 14:c3c43c8faf0e 105 " case 5: return 'Fri';\n"
andrewboyson 14:c3c43c8faf0e 106 " case 6: return 'Sat';\n"
andrewboyson 14:c3c43c8faf0e 107 " default: return '---';\n"
andrewboyson 14:c3c43c8faf0e 108 " }\n"
andrewboyson 14:c3c43c8faf0e 109 "}\n"
andrewboyson 14:c3c43c8faf0e 110 "function adjustLeap()\n"
andrewboyson 14:c3c43c8faf0e 111 "{\n"
andrewboyson 14:c3c43c8faf0e 112 " if (msRtc == 0) return; //Don't attempt to adjust an invalid time\n"
andrewboyson 14:c3c43c8faf0e 113 " \n"
andrewboyson 14:c3c43c8faf0e 114 " if (!leapEnable) return; // Adjustment disabled\n"
andrewboyson 14:c3c43c8faf0e 115 " \n"
andrewboyson 14:c3c43c8faf0e 116 " //Get the calander date and time from the ms\n"
andrewboyson 23:f340b48d0538 117 " var now = msCount - msCountAtRtcSet + msRtc;\n"
andrewboyson 25:467494781985 118 " var leapStart = Date.UTC(leapyear, leapmonth - 1, 1, 0, 0, leapForward ? 0: -1);\n"
andrewboyson 21:048b7dbdf0aa 119 " \n"
andrewboyson 23:f340b48d0538 120 " if (now < leapStart) return; //Do nothing until reached the leap start\n"
andrewboyson 14:c3c43c8faf0e 121 " \n"
andrewboyson 25:467494781985 122 " if (leapForward) { msRtc -= 1000; leaps += 1; } //repeat 59\n"
andrewboyson 25:467494781985 123 " else { msRtc += 1000; leaps -= 1; } //skip 59\n"
andrewboyson 21:048b7dbdf0aa 124 " \n"
andrewboyson 21:048b7dbdf0aa 125 " leapEnable = false;\n"
andrewboyson 14:c3c43c8faf0e 126 "}\n"
andrewboyson 14:c3c43c8faf0e 127 "function displayTime()\n"
andrewboyson 14:c3c43c8faf0e 128 "{\n"
andrewboyson 14:c3c43c8faf0e 129 " if (msRtc == 0) return; //Don't attempt to display an invalid time\n"
andrewboyson 14:c3c43c8faf0e 130 " \n"
andrewboyson 14:c3c43c8faf0e 131 " //Get the calander date and time from the ms\n"
andrewboyson 14:c3c43c8faf0e 132 " var now = new Date(msCount - msCountAtRtcSet + msRtc);\n"
andrewboyson 14:c3c43c8faf0e 133 " var y = now.getUTCFullYear();\n"
andrewboyson 14:c3c43c8faf0e 134 " var n = now.getUTCMonth () + 1;\n"
andrewboyson 14:c3c43c8faf0e 135 " var d = now.getUTCDate ();\n"
andrewboyson 14:c3c43c8faf0e 136 " var w = now.getUTCDay (); // 0 == Sunday\n"
andrewboyson 14:c3c43c8faf0e 137 " var h = now.getUTCHours ();\n"
andrewboyson 14:c3c43c8faf0e 138 " var m = now.getUTCMinutes ();\n"
andrewboyson 14:c3c43c8faf0e 139 " var s = now.getUTCSeconds ();\n"
andrewboyson 14:c3c43c8faf0e 140 " \n"
andrewboyson 14:c3c43c8faf0e 141 " //Format time\n"
andrewboyson 14:c3c43c8faf0e 142 " n = formatNumbers00(n);\n"
andrewboyson 14:c3c43c8faf0e 143 " d = formatNumbers00(d);\n"
andrewboyson 14:c3c43c8faf0e 144 " h = formatNumbers00(h);\n"
andrewboyson 14:c3c43c8faf0e 145 " m = formatNumbers00(m);\n"
andrewboyson 14:c3c43c8faf0e 146 " s = formatNumbers00(s);\n"
andrewboyson 14:c3c43c8faf0e 147 " w = formatDayOfWeek(w);\n"
andrewboyson 14:c3c43c8faf0e 148 " \n"
andrewboyson 14:c3c43c8faf0e 149 " //Display time\n"
andrewboyson 14:c3c43c8faf0e 150 " var elem;\n"
andrewboyson 14:c3c43c8faf0e 151 " \n"
andrewboyson 77:4689596a2f3f 152 " elem = document.getElementById('ajax-date-utc');\n"
andrewboyson 77:4689596a2f3f 153 " if (elem) elem.textContent = y + '-' + n + '-' + d + ' ' + w + ' ' + h + ':' + m + ':' + s + ' TAI-UTC=' + leaps;\n"
andrewboyson 14:c3c43c8faf0e 154 "\n"
andrewboyson 77:4689596a2f3f 155 " elem = document.getElementById('ajax-date-pc');\n"
andrewboyson 14:c3c43c8faf0e 156 " var options = \n"
andrewboyson 14:c3c43c8faf0e 157 " {\n"
andrewboyson 14:c3c43c8faf0e 158 " year: 'numeric',\n"
andrewboyson 14:c3c43c8faf0e 159 " month: 'short',\n"
andrewboyson 14:c3c43c8faf0e 160 " day: '2-digit',\n"
andrewboyson 14:c3c43c8faf0e 161 " weekday: 'short',\n"
andrewboyson 14:c3c43c8faf0e 162 " hour: '2-digit',\n"
andrewboyson 14:c3c43c8faf0e 163 " minute: '2-digit',\n"
andrewboyson 14:c3c43c8faf0e 164 " second: '2-digit',\n"
andrewboyson 14:c3c43c8faf0e 165 " timeZoneName: 'short'\n"
andrewboyson 14:c3c43c8faf0e 166 " };\n"
andrewboyson 77:4689596a2f3f 167 " if (elem) elem.textContent = now.toLocaleString(undefined, options);\n"
andrewboyson 14:c3c43c8faf0e 168 "}\n"
andrewboyson 14:c3c43c8faf0e 169 "\n"
andrewboyson 14:c3c43c8faf0e 170 "var ajax;\n"
andrewboyson 14:c3c43c8faf0e 171 "var msCountAtAjaxSend = 0;\n"
andrewboyson 14:c3c43c8faf0e 172 "function AjaxRequest(request) //Used by this script and from HTML page\n"
andrewboyson 14:c3c43c8faf0e 173 "{\n"
andrewboyson 14:c3c43c8faf0e 174 " ajax=new XMLHttpRequest();\n"
andrewboyson 14:c3c43c8faf0e 175 " ajax.onreadystatechange=handleAjaxResponse;\n"
andrewboyson 46:1822fdbe6c0c 176 " if (request) ajax.open('GET', '/clock-ajax' + '?' + request, true);\n"
andrewboyson 46:1822fdbe6c0c 177 " else ajax.open('GET', '/clock-ajax' , true);\n"
andrewboyson 14:c3c43c8faf0e 178 " ajax.send();\n"
andrewboyson 14:c3c43c8faf0e 179 "}\n"
andrewboyson 14:c3c43c8faf0e 180 "function requestAjax() //Used in this script\n"
andrewboyson 14:c3c43c8faf0e 181 "{\n"
andrewboyson 14:c3c43c8faf0e 182 " \n"
andrewboyson 14:c3c43c8faf0e 183 " AjaxRequest('');\n"
andrewboyson 14:c3c43c8faf0e 184 " msCountAtAjaxSend = msCount;\n"
andrewboyson 14:c3c43c8faf0e 185 "}\n"
andrewboyson 14:c3c43c8faf0e 186 "\n"
andrewboyson 14:c3c43c8faf0e 187 "function counter()\n"
andrewboyson 14:c3c43c8faf0e 188 "{\n"
andrewboyson 14:c3c43c8faf0e 189 " msCount += TICK_MS; //Don't use Date.now() as we don't know when its clock will be updated around a leap second\n"
andrewboyson 14:c3c43c8faf0e 190 " adjustLeap();\n"
andrewboyson 14:c3c43c8faf0e 191 " displayTime();\n"
andrewboyson 14:c3c43c8faf0e 192 " if (msCount >= msCountAtRtcSet + AJAX_REFRESH_MS && //Wait until time to refresh\n"
andrewboyson 14:c3c43c8faf0e 193 " msCount >= msCountAtAjaxSend + AJAX_QUIET_MS) //Don't repeat during quiet period\n"
andrewboyson 14:c3c43c8faf0e 194 " {\n"
andrewboyson 14:c3c43c8faf0e 195 " requestAjax(); //RequestAjax will set msRtc and reset msCount\n"
andrewboyson 14:c3c43c8faf0e 196 " }\n"
andrewboyson 14:c3c43c8faf0e 197 "}\n"
andrewboyson 14:c3c43c8faf0e 198 "\n"
andrewboyson 14:c3c43c8faf0e 199 "function handleAjaxResponse()\n"
andrewboyson 14:c3c43c8faf0e 200 "{\n"
andrewboyson 14:c3c43c8faf0e 201 " if (ajax.readyState==4 && ajax.status==200)\n"
andrewboyson 14:c3c43c8faf0e 202 " {\n"
andrewboyson 14:c3c43c8faf0e 203 " response = ajax.responseText;\n"
andrewboyson 14:c3c43c8faf0e 204 " headers = ajax.getAllResponseHeaders();\n"
andrewboyson 14:c3c43c8faf0e 205 " parseAjax();\n"
andrewboyson 14:c3c43c8faf0e 206 " displayGeneral();\n"
andrewboyson 14:c3c43c8faf0e 207 " }\n"
andrewboyson 14:c3c43c8faf0e 208 "}\n"
andrewboyson 14:c3c43c8faf0e 209 "\n"
andrewboyson 14:c3c43c8faf0e 210 "function DisplayLeap() //Called by display leap button in HTML\n"
andrewboyson 14:c3c43c8faf0e 211 "{\n"
andrewboyson 14:c3c43c8faf0e 212 " leapEnable = true;\n"
andrewboyson 14:c3c43c8faf0e 213 " msRtc = Date.UTC(leapyear, leapmonth - 1, 1) - AJAX_REFRESH_MS / 2; //displays the refresh period around the latest leap second\n"
andrewboyson 14:c3c43c8faf0e 214 " msCountAtRtcSet = msCount;\n"
andrewboyson 14:c3c43c8faf0e 215 "}\n"
andrewboyson 14:c3c43c8faf0e 216 "\n"
andrewboyson 14:c3c43c8faf0e 217 "function init()\n"
andrewboyson 14:c3c43c8faf0e 218 "{\n"
andrewboyson 14:c3c43c8faf0e 219 " setInterval(counter, TICK_MS);\n"
andrewboyson 14:c3c43c8faf0e 220 " requestAjax();\n"
andrewboyson 14:c3c43c8faf0e 221 "}\n"
andrewboyson 14:c3c43c8faf0e 222 "if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init ); // Loading hasn't finished yet\n"
andrewboyson 14:c3c43c8faf0e 223 "else init(); //`DOMContentLoaded` has already fired\n"
andrewboyson 14:c3c43c8faf0e 224 ""