Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
clock/http-clock-script.inc@94:d7226b2c14b6, 2019-04-16 (annotated)
- Committer:
- andrewboyson
- Date:
- Tue Apr 16 19:18:27 2019 +0000
- Revision:
- 94:d7226b2c14b6
- Parent:
- 77:4689596a2f3f
- Child:
- 95:8c9dda8a0caf
Used a class for the utc time to handle both the server 'real' time and a pseudo 'next leap' time. Also split the includes into building blocks so that many of the js scripts could be made up from them.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| andrewboyson | 94:d7226b2c14b6 | 1 | "//Clock script\n" |
| andrewboyson | 14:c3c43c8faf0e | 2 | "'use strict';\n" |
| andrewboyson | 14:c3c43c8faf0e | 3 | "\n" |
| andrewboyson | 94:d7226b2c14b6 | 4 | "let pseudo = new Utc();\n" |
| andrewboyson | 94:d7226b2c14b6 | 5 | "let rtc = new Utc();\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 | 94:d7226b2c14b6 | 16 | "let leapmonths1970 = 0;\n" |
| andrewboyson | 14:c3c43c8faf0e | 17 | "\n" |
| andrewboyson | 94:d7226b2c14b6 | 18 | "let ppb = 0;\n" |
| andrewboyson | 94:d7226b2c14b6 | 19 | "let ppbdivisor = 0;\n" |
| andrewboyson | 94:d7226b2c14b6 | 20 | "let ppbmaxchange = 0;\n" |
| andrewboyson | 94:d7226b2c14b6 | 21 | "let syncedlimitppb = 0;\n" |
| andrewboyson | 94:d7226b2c14b6 | 22 | "let syncedhysppb = 0;\n" |
| andrewboyson | 94:d7226b2c14b6 | 23 | "let slewdivisor = 0;\n" |
| andrewboyson | 94:d7226b2c14b6 | 24 | "let slewmax = 0;\n" |
| andrewboyson | 94:d7226b2c14b6 | 25 | "let syncedlimitns = 0;\n" |
| andrewboyson | 94:d7226b2c14b6 | 26 | "let syncedhysns = 0;\n" |
| andrewboyson | 94:d7226b2c14b6 | 27 | "let maxoffsetsecs = 0;\n" |
| andrewboyson | 94:d7226b2c14b6 | 28 | "let govTrace = false;\n" |
| andrewboyson | 14:c3c43c8faf0e | 29 | "\n" |
| andrewboyson | 94:d7226b2c14b6 | 30 | "let ntpserver = '';\n" |
| andrewboyson | 94:d7226b2c14b6 | 31 | "let ntpinitial = 0;\n" |
| andrewboyson | 94:d7226b2c14b6 | 32 | "let ntpnormal = 0;\n" |
| andrewboyson | 94:d7226b2c14b6 | 33 | "let ntpretry = 0;\n" |
| andrewboyson | 94:d7226b2c14b6 | 34 | "let ntpoffset = 0;\n" |
| andrewboyson | 94:d7226b2c14b6 | 35 | "let ntpmaxdelay = 0;\n" |
| andrewboyson | 94:d7226b2c14b6 | 36 | "\n" |
| andrewboyson | 94:d7226b2c14b6 | 37 | "let scanavg = 0;\n" |
| andrewboyson | 94:d7226b2c14b6 | 38 | "let scanmax = 0;\n" |
| andrewboyson | 94:d7226b2c14b6 | 39 | "let scanmin = 0;\n" |
| andrewboyson | 94:d7226b2c14b6 | 40 | "\n" |
| andrewboyson | 94:d7226b2c14b6 | 41 | "const DISPLAY_LEAP_MS = 10000;\n" |
| andrewboyson | 94:d7226b2c14b6 | 42 | "const SERVER = '/clock-ajax';\n" |
| andrewboyson | 94:d7226b2c14b6 | 43 | "\n" |
| andrewboyson | 94:d7226b2c14b6 | 44 | "function parseDate()\n" |
| andrewboyson | 14:c3c43c8faf0e | 45 | "{\n" |
| andrewboyson | 94:d7226b2c14b6 | 46 | " let iDateStart = BaseHeaders.toLowerCase().indexOf('date:');\n" |
| andrewboyson | 94:d7226b2c14b6 | 47 | " let iDateEnd = BaseHeaders.indexOf('\\r', iDateStart);\n" |
| andrewboyson | 94:d7226b2c14b6 | 48 | " let rtcDate = new Date(BaseHeaders.slice(iDateStart + 5, iDateEnd));\n" |
| andrewboyson | 94:d7226b2c14b6 | 49 | " rtc.ms = rtcDate.getTime();\n" |
| andrewboyson | 14:c3c43c8faf0e | 50 | "}\n" |
| andrewboyson | 14:c3c43c8faf0e | 51 | "\n" |
| andrewboyson | 94:d7226b2c14b6 | 52 | "function parseLinesTime(text)\n" |
| andrewboyson | 14:c3c43c8faf0e | 53 | "{\n" |
| andrewboyson | 94:d7226b2c14b6 | 54 | " let lines = text.split('\\n');\n" |
| andrewboyson | 94:d7226b2c14b6 | 55 | " rtc.ms += parseInt(lines[0], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 56 | " rtc.ms -= BaseMs;\n" |
| andrewboyson | 94:d7226b2c14b6 | 57 | " diffMs = rtc.ms + BaseMs - Date.now();\n" |
| andrewboyson | 94:d7226b2c14b6 | 58 | " rtcIsSet = hexToBit(lines[1], 0);\n" |
| andrewboyson | 94:d7226b2c14b6 | 59 | " clockIsSet = hexToBit(lines[1], 1);\n" |
| andrewboyson | 94:d7226b2c14b6 | 60 | " sourceIsOk = hexToBit(lines[1], 2);\n" |
| andrewboyson | 94:d7226b2c14b6 | 61 | " rateIsLocked = hexToBit(lines[1], 3);\n" |
| andrewboyson | 94:d7226b2c14b6 | 62 | " timeIsLocked = hexToBit(lines[1], 4);\n" |
| andrewboyson | 94:d7226b2c14b6 | 63 | " rtc.leapEnable = hexToBit(lines[1], 5);\n" |
| andrewboyson | 94:d7226b2c14b6 | 64 | " rtc.leapForward = hexToBit(lines[1], 6);\n" |
| andrewboyson | 94:d7226b2c14b6 | 65 | " govTrace = hexToBit(lines[1], 7);\n" |
| andrewboyson | 94:d7226b2c14b6 | 66 | " leapmonths1970 = parseInt(lines[2], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 67 | " rtc.leapMonth = leapmonths1970 % 12;\n" |
| andrewboyson | 94:d7226b2c14b6 | 68 | " rtc.leapYear = (leapmonths1970 - rtc.leapMonth) / 12;\n" |
| andrewboyson | 94:d7226b2c14b6 | 69 | " rtc.leapMonth += 1;\n" |
| andrewboyson | 94:d7226b2c14b6 | 70 | " rtc.leapYear += 1970;\n" |
| andrewboyson | 94:d7226b2c14b6 | 71 | " rtc.leaps = parseInt(lines[3], 16);\n" |
| andrewboyson | 14:c3c43c8faf0e | 72 | "}\n" |
| andrewboyson | 94:d7226b2c14b6 | 73 | "function parseLinesGov(text)\n" |
| andrewboyson | 14:c3c43c8faf0e | 74 | "{\n" |
| andrewboyson | 94:d7226b2c14b6 | 75 | " let lines = text.split('\\n');\n" |
| andrewboyson | 94:d7226b2c14b6 | 76 | " ppb = parseInt(lines[0], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 77 | " ppbdivisor = parseInt(lines[1], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 78 | " ppbmaxchange = parseInt(lines[2], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 79 | " syncedlimitppb = parseInt(lines[3], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 80 | " syncedhysppb = parseInt(lines[4], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 81 | " slewdivisor = parseInt(lines[5], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 82 | " slewmax = parseInt(lines[6], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 83 | " syncedlimitns = parseInt(lines[7], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 84 | " syncedhysns = parseInt(lines[8], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 85 | " maxoffsetsecs = parseInt(lines[9], 16);\n" |
| andrewboyson | 14:c3c43c8faf0e | 86 | "}\n" |
| andrewboyson | 94:d7226b2c14b6 | 87 | "function parseLinesNtp(text)\n" |
| andrewboyson | 94:d7226b2c14b6 | 88 | "{\n" |
| andrewboyson | 94:d7226b2c14b6 | 89 | " let lines = text.split('\\n');\n" |
| andrewboyson | 94:d7226b2c14b6 | 90 | " ntpserver = lines[0];\n" |
| andrewboyson | 94:d7226b2c14b6 | 91 | " ntpinitial = parseInt(lines[1], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 92 | " ntpnormal = parseInt(lines[2], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 93 | " ntpretry = parseInt(lines[3], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 94 | " ntpoffset = parseInt(lines[4], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 95 | " ntpmaxdelay = parseInt(lines[5], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 96 | "}\n" |
| andrewboyson | 94:d7226b2c14b6 | 97 | "function parseLinesScan(text)\n" |
| andrewboyson | 14:c3c43c8faf0e | 98 | "{\n" |
| andrewboyson | 94:d7226b2c14b6 | 99 | " let lines = text.split('\\n');\n" |
| andrewboyson | 94:d7226b2c14b6 | 100 | " scanavg = parseInt(lines[0], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 101 | " scanmax = parseInt(lines[1], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 102 | " scanmin = parseInt(lines[2], 16);\n" |
| andrewboyson | 94:d7226b2c14b6 | 103 | "}\n" |
| andrewboyson | 94:d7226b2c14b6 | 104 | "function DerivedParse()\n" |
| andrewboyson | 94:d7226b2c14b6 | 105 | "{\n" |
| andrewboyson | 94:d7226b2c14b6 | 106 | " let topics = BaseResponse.split('\\f');\n" |
| andrewboyson | 94:d7226b2c14b6 | 107 | " parseDate();\n" |
| andrewboyson | 94:d7226b2c14b6 | 108 | " parseLinesTime(topics[0]);\n" |
| andrewboyson | 94:d7226b2c14b6 | 109 | " parseLinesGov (topics[1]);\n" |
| andrewboyson | 94:d7226b2c14b6 | 110 | " parseLinesNtp (topics[2]);\n" |
| andrewboyson | 94:d7226b2c14b6 | 111 | " parseLinesScan(topics[3]);\n" |
| andrewboyson | 94:d7226b2c14b6 | 112 | "}\n" |
| andrewboyson | 94:d7226b2c14b6 | 113 | "function DerivedDisplay()\n" |
| andrewboyson | 94:d7226b2c14b6 | 114 | "{\n" |
| andrewboyson | 94:d7226b2c14b6 | 115 | " let elem;\n" |
| andrewboyson | 94:d7226b2c14b6 | 116 | " elem = getElementOrNull('ajax-rtc-set' ); if (elem) elem.setAttribute('dir', rtcIsSet ? 'rtl' : 'ltr');\n" |
| andrewboyson | 94:d7226b2c14b6 | 117 | " elem = getElementOrNull('ajax-clock-set' ); if (elem) elem.setAttribute('dir', clockIsSet ? 'rtl' : 'ltr');\n" |
| andrewboyson | 94:d7226b2c14b6 | 118 | " elem = getElementOrNull('ajax-source-ok' ); if (elem) elem.setAttribute('dir', sourceIsOk ? 'rtl' : 'ltr');\n" |
| andrewboyson | 94:d7226b2c14b6 | 119 | " elem = getElementOrNull('ajax-rate-locked' ); if (elem) elem.setAttribute('dir', rateIsLocked ? 'rtl' : 'ltr');\n" |
| andrewboyson | 94:d7226b2c14b6 | 120 | " elem = getElementOrNull('ajax-time-locked' ); if (elem) elem.setAttribute('dir', timeIsLocked ? 'rtl' : 'ltr');\n" |
| andrewboyson | 14:c3c43c8faf0e | 121 | " \n" |
| andrewboyson | 94:d7226b2c14b6 | 122 | " elem = getElementOrNull('ajax-leap-enable' ); if (elem) elem.setAttribute('dir', rtc.leapEnable ? 'rtl' : 'ltr');\n" |
| andrewboyson | 94:d7226b2c14b6 | 123 | " elem = getElementOrNull('ajax-leap-forward' ); if (elem) elem.setAttribute('dir', rtc.leapForward ? 'rtl' : 'ltr');\n" |
| andrewboyson | 14:c3c43c8faf0e | 124 | " \n" |
| andrewboyson | 94:d7226b2c14b6 | 125 | " elem = getElementOrNull('ajax-leap-year' ); if (elem) elem.value = leapmonths1970 ? rtc.leapYear : '';\n" |
| andrewboyson | 94:d7226b2c14b6 | 126 | " elem = getElementOrNull('ajax-leap-month' ); if (elem) elem.value = leapmonths1970 ? rtc.leapMonth : '';\n" |
| andrewboyson | 94:d7226b2c14b6 | 127 | " \n" |
| andrewboyson | 94:d7226b2c14b6 | 128 | " elem = getElementOrNull('ajax-leap-count' ); if (elem) elem.value = rtc.leaps;\n" |
| andrewboyson | 94:d7226b2c14b6 | 129 | " \n" |
| andrewboyson | 94:d7226b2c14b6 | 130 | " elem = getElementOrNull('ajax-ppb' ); if (elem) elem.value = ppb;\n" |
| andrewboyson | 94:d7226b2c14b6 | 131 | " elem = getElementOrNull('ajax-ppb-divisor' ); if (elem) elem.value = ppbdivisor;\n" |
| andrewboyson | 94:d7226b2c14b6 | 132 | " elem = getElementOrNull('ajax-ppb-max-chg' ); if (elem) elem.value = ppbmaxchange;\n" |
| andrewboyson | 94:d7226b2c14b6 | 133 | " elem = getElementOrNull('ajax-ppb-syn-lim' ); if (elem) elem.value = syncedlimitppb;\n" |
| andrewboyson | 94:d7226b2c14b6 | 134 | " elem = getElementOrNull('ajax-ppb-syn-hys' ); if (elem) elem.value = syncedhysppb;\n" |
| andrewboyson | 94:d7226b2c14b6 | 135 | " elem = getElementOrNull('ajax-off-divisor' ); if (elem) elem.value = slewdivisor;\n" |
| andrewboyson | 94:d7226b2c14b6 | 136 | " elem = getElementOrNull('ajax-off-max' ); if (elem) elem.value = slewmax;\n" |
| andrewboyson | 94:d7226b2c14b6 | 137 | " elem = getElementOrNull('ajax-off-syn-lim' ); if (elem) elem.value = syncedlimitns / 1000000;\n" |
| andrewboyson | 94:d7226b2c14b6 | 138 | " elem = getElementOrNull('ajax-off-syn-hys' ); if (elem) elem.value = syncedhysns / 1000000;\n" |
| andrewboyson | 94:d7226b2c14b6 | 139 | " elem = getElementOrNull('ajax-off-rst-lim' ); if (elem) elem.value = maxoffsetsecs;\n" |
| andrewboyson | 94:d7226b2c14b6 | 140 | " elem = getElementOrNull('ajax-gov-trace' ); if (elem) elem.setAttribute('dir', govTrace ? 'rtl' : 'ltr');\n" |
| andrewboyson | 94:d7226b2c14b6 | 141 | " \n" |
| andrewboyson | 94:d7226b2c14b6 | 142 | " elem = getElementOrNull('ajax-ntp-server' ); if (elem) elem.value = ntpserver;\n" |
| andrewboyson | 94:d7226b2c14b6 | 143 | " elem = getElementOrNull('ajax-ntp-initial' ); if (elem) elem.value = ntpinitial;\n" |
| andrewboyson | 94:d7226b2c14b6 | 144 | " elem = getElementOrNull('ajax-ntp-normal' ); if (elem) elem.value = ntpnormal / 60;\n" |
| andrewboyson | 94:d7226b2c14b6 | 145 | " elem = getElementOrNull('ajax-ntp-retry' ); if (elem) elem.value = ntpretry;\n" |
| andrewboyson | 94:d7226b2c14b6 | 146 | " elem = getElementOrNull('ajax-ntp-offset' ); if (elem) elem.value = ntpoffset;\n" |
| andrewboyson | 94:d7226b2c14b6 | 147 | " elem = getElementOrNull('ajax-ntp-max-delay'); if (elem) elem.value = ntpmaxdelay;\n" |
| andrewboyson | 94:d7226b2c14b6 | 148 | " \n" |
| andrewboyson | 94:d7226b2c14b6 | 149 | " elem = getElementOrNull('ajax-scan-avg' ); if (elem) elem.textContent = scanavg;\n" |
| andrewboyson | 94:d7226b2c14b6 | 150 | " elem = getElementOrNull('ajax-scan-max' ); if (elem) elem.textContent = scanmax;\n" |
| andrewboyson | 94:d7226b2c14b6 | 151 | " elem = getElementOrNull('ajax-scan-min' ); if (elem) elem.textContent = scanmin;\n" |
| andrewboyson | 94:d7226b2c14b6 | 152 | " \n" |
| andrewboyson | 94:d7226b2c14b6 | 153 | " elem = getElementOrNull('ajax-date-diff' ); if (elem) elem.textContent = diffMs;\n" |
| andrewboyson | 14:c3c43c8faf0e | 154 | "}\n" |
| andrewboyson | 14:c3c43c8faf0e | 155 | "\n" |
| andrewboyson | 94:d7226b2c14b6 | 156 | "function DerivedTick() //This typically called every 100ms\n" |
| andrewboyson | 14:c3c43c8faf0e | 157 | "{\n" |
| andrewboyson | 94:d7226b2c14b6 | 158 | " if (pseudoDisplay)\n" |
| andrewboyson | 14:c3c43c8faf0e | 159 | " {\n" |
| andrewboyson | 94:d7226b2c14b6 | 160 | " pseudo.adjustLeap (BaseMs);\n" |
| andrewboyson | 94:d7226b2c14b6 | 161 | " pseudo.displayTime(BaseMs);\n" |
| andrewboyson | 94:d7226b2c14b6 | 162 | " if (BaseMs >= pseudoStartMs + DISPLAY_LEAP_MS + 500) pseudoDisplay = false;\n" |
| andrewboyson | 14:c3c43c8faf0e | 163 | " }\n" |
| andrewboyson | 94:d7226b2c14b6 | 164 | " else\n" |
| andrewboyson | 94:d7226b2c14b6 | 165 | " {\n" |
| andrewboyson | 94:d7226b2c14b6 | 166 | " rtc.adjustLeap (BaseMs);\n" |
| andrewboyson | 94:d7226b2c14b6 | 167 | " rtc.displayTime(BaseMs);\n" |
| andrewboyson | 94:d7226b2c14b6 | 168 | " }\n" |
| andrewboyson | 14:c3c43c8faf0e | 169 | "}\n" |
| andrewboyson | 14:c3c43c8faf0e | 170 | "\n" |
| andrewboyson | 14:c3c43c8faf0e | 171 | "function DisplayLeap() //Called by display leap button in HTML\n" |
| andrewboyson | 14:c3c43c8faf0e | 172 | "{\n" |
| andrewboyson | 94:d7226b2c14b6 | 173 | " pseudoDisplay = true;\n" |
| andrewboyson | 94:d7226b2c14b6 | 174 | " pseudoStartMs = BaseMs;\n" |
| andrewboyson | 94:d7226b2c14b6 | 175 | " \n" |
| andrewboyson | 94:d7226b2c14b6 | 176 | " pseudo.leapEnable = true;\n" |
| andrewboyson | 94:d7226b2c14b6 | 177 | " pseudo.leapForward = rtc.leapForward;\n" |
| andrewboyson | 94:d7226b2c14b6 | 178 | " pseudo.leaps = rtc.leaps;\n" |
| andrewboyson | 94:d7226b2c14b6 | 179 | " pseudo.leapMonth = rtc.leapMonth;\n" |
| andrewboyson | 94:d7226b2c14b6 | 180 | " pseudo.leapYear = rtc.leapYear;\n" |
| andrewboyson | 94:d7226b2c14b6 | 181 | " pseudo.ms = Date.UTC(rtc.leapYear, rtc.leapMonth - 1, 1) - DISPLAY_LEAP_MS / 2 - BaseMs;\n" |
| andrewboyson | 14:c3c43c8faf0e | 182 | "}\n" |
| andrewboyson | 14:c3c43c8faf0e | 183 | "" |