diu_ui-ORG.js 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799
  1. /*
  2. * Copyright (c) 2019 Clementine Computing LLC.
  3. *
  4. * This file is part of PopuFare.
  5. *
  6. * PopuFare is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * PopuFare is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. */
  20. var _ADDRESS = "localhost";
  21. var _URL = "http://" + _ADDRESS;
  22. var _PORT = 60535;
  23. var _fqADDRESS = _ADDRESS + ":" + _PORT;
  24. var BG_COLOR = "#f7f7f7";
  25. var TEXT_COLOR = "#444444";
  26. var DIU_UI_VERSION = "0.1.10";
  27. var DIU_UI_VERSION_DATE = "2020-02-10";
  28. var g_snd = {
  29. "reject_fare" : "",
  30. "accept_fare" : "",
  31. "rule_error" : "",
  32. "button_press" : ""
  33. };
  34. function _beep_error() {
  35. g_snd.rule_error.currentTime=0;
  36. g_snd.rule_error.play();
  37. }
  38. function _beep_reject() {
  39. g_snd.reject_fare.currentTime=0;
  40. g_snd.reject_fare.play();
  41. }
  42. function _beep_accept() {
  43. g_snd.accept_fare.currentTime=0;
  44. g_snd.accept_fare.play();
  45. }
  46. function _beep() {
  47. g_snd.button_press.currentTime=0;
  48. g_snd.button_press.play();
  49. }
  50. var g_ctx = {
  51. "driver_login_state" : "driver",
  52. "driver" : "",
  53. "pin" : "",
  54. "paddle" : "",
  55. "eqnum" : "",
  56. "cur_eqnum" : "",
  57. "current_ui" : "ui_login",
  58. "ui_stack" : [],
  59. "ui_change_timeoutid": -1,
  60. "ui_watchdog_timeoutid": -1,
  61. "bulkfare" : "",
  62. "bulkfare_count" : "",
  63. "status_text": "",
  64. "status_text_w": 32,
  65. "status_text_h": 11,
  66. "status_html_text":"",
  67. "diu_status" : {
  68. "last_update" : -1
  69. },
  70. // system config
  71. //
  72. "config" : {
  73. "last_update": -1,
  74. "volume": 100,
  75. "brightness": 100,
  76. "showdimclock": 0
  77. },
  78. "x": ""
  79. };
  80. function _api_fail() {
  81. console.log(">>api fail");
  82. return true;
  83. }
  84. function _api_tick() { }
  85. function _api_request(action, param, cb_ok, cb_fail, cb_neterr) {
  86. var idx = 0;
  87. var param_name = [], param_val = [];
  88. var req = new XMLHttpRequest();
  89. // Firefox gets testy about parsing assumed XML
  90. //
  91. req.overrideMimeType("text/plain");
  92. var url = "/req";
  93. var varstr = "action=" + action;
  94. for (var x in param) {
  95. param_name.push(x);
  96. param_val.push(param[x]);
  97. }
  98. for (var ii=0; ii<param_name.length; ii++) {
  99. varstr += "&" + param_name[ii] + "=" + param_val[ii];
  100. }
  101. req.onreadystatechange = function() {
  102. if (req.readyState == 4) {
  103. if (req.status == 200) {
  104. var fulltxt = req.responseText;
  105. var resp = req.responseText.split(" ");
  106. if ((resp.length>=2) && (resp[0] == "ok")) {
  107. if (typeof cb_ok !== "undefined") {
  108. cb_ok(fulltxt);
  109. }
  110. return;
  111. }
  112. if (typeof cb_fail !== "undefined") {
  113. cb_fail(fulltxt);
  114. }
  115. }
  116. else if (typeof cb_fail !== "undefined") { cb_fail(); }
  117. }
  118. };
  119. req.addEventListener("error", _api_fail);
  120. req.addEventListener("abort", _api_fail);
  121. req.onerror = function() { console.log("network error"); };
  122. try {
  123. req.open("POST", url, true);
  124. req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  125. req.send(varstr);
  126. } catch (er) {
  127. console.log(">>>>>>ERROR", er);
  128. }
  129. }
  130. function _block_status_window(ui_id) {
  131. var ele = document.getElementById(ui_id + "_status");
  132. ele.ondragstart = function() { return false; }
  133. ele.onselectstart = function() { return false; }
  134. ele.onmousedown = function() { return false; }
  135. ele.ondragstart = function() { return false; }
  136. var rect = ele.getBoundingClientRect();
  137. var block_ele = document.getElementById(ui_id + "_blockwindow");
  138. block_ele.style.width = rect.width;
  139. block_ele.style.height = rect.height;
  140. block_ele.style.bottom = rect.bottom;
  141. block_ele.style.left = rect.left;
  142. block_ele.style.top = rect.top;
  143. block_ele.style.right = rect.right;
  144. block_ele.style.x = rect.x;
  145. block_ele.style.y = rect.y;
  146. }
  147. function _clear_ui_watchdog() {
  148. if (g_ctx.ui_watchdog_timeoutid >= 0) {
  149. window.clearTimeout(g_ctx.ui_watchdog_timeoutid);
  150. }
  151. }
  152. function _set_ui_watchdog(failsafe_ui, _dt) {
  153. // default to 'ui_watchdog' and 5s
  154. //
  155. failsafe_ui = (( typeof failsafe_ui === "undefined") ? "ui_watchdog" : failsafe_ui);
  156. _dt = (( typeof _dt === "undefined") ? 5000 : _dt);
  157. _clear_ui_watchdog();
  158. g_ctx.ui_watchdog_timeoutid = window.setTimeout(
  159. (function(_fsui) {
  160. return function() { console.log("???", _fsui); _switch_ui(_fsui); };
  161. })(failsafe_ui),
  162. _dt);
  163. }
  164. function _switch_ui(to) {
  165. var ele;
  166. if (g_ctx.ui_change_timeoutid > 0) {
  167. window.clearTimeout(g_ctx.ui_change_timeoutid);
  168. g_ctx.ui_change_timeoutid = -1;
  169. }
  170. var from = g_ctx.current_ui;
  171. ele = document.getElementById(from);
  172. ele.style.display = "none";
  173. ele = document.getElementById(to);
  174. ele.style.display = "block";
  175. g_ctx.current_ui = to;
  176. if (to == "ui_main") {
  177. var _x = _calculate_ui_main_status_width_height();
  178. g_ctx.status_text_h = _x.n_height;
  179. g_ctx.status_text_w = _x.n_width;
  180. }
  181. if ((to == "ui_main") ||
  182. (to == "ui_login")) {
  183. _block_status_window(to);
  184. }
  185. }
  186. function _hide_ui(from) {
  187. var ele = document.getElementById(from);
  188. ele.style.display = "none";
  189. }
  190. function _hide_ui_element(from) {
  191. var ele = document.getElementById(from);
  192. ele.style.display = "none";
  193. }
  194. function _show_ui(to) {
  195. var ele = document.getElementById(to);
  196. ele.style.display = "block";
  197. g_ctx.current_ui = to;
  198. // put pane over status window to prevent highlights,
  199. // copy/pasting, etc.
  200. //
  201. if ((to == "ui_main") ||
  202. (to == "ui_login")) {
  203. _block_status_window(to);
  204. }
  205. }
  206. function _show_ui_element(to) {
  207. var ele = document.getElementById(to);
  208. ele.style.display = "block";
  209. }
  210. //------------
  211. function _get_now_HH_MM() {
  212. var _dt = new Date();
  213. var _sep = ":";
  214. if ( (_dt.getSeconds() % 2) == 0) {
  215. _sep = " ";
  216. }
  217. var _hr = _dt.getHours();
  218. var _min = _dt.getMinutes();
  219. var _hr_str = _hr.toString();
  220. var _min_str = _min.toString();
  221. if (_hr < 10) { _hr_str = "0" + _hr_str; }
  222. if (_min < 10) { _min_str = "0" + _min_str; }
  223. var _time_str = _hr_str + _sep + _min_str;
  224. return _time_str;
  225. }
  226. function _get_now_YYYY_MM_DD() {
  227. var _dt = new Date();
  228. var _sep = "-";
  229. var _yr = _dt.getFullYear();
  230. var _mo = _dt.getMonth()+1;
  231. var _dy = _dt.getDate();
  232. var _yr_str = _yr.toString();
  233. var _mo_str = _mo.toString();
  234. if (_mo < 10) {
  235. _mo_str = "0" + _mo_str;
  236. }
  237. var _dy_str = _dy.toString();
  238. if (_dy < 10) {
  239. _dy_str = "0" + _dy_str;
  240. }
  241. return _yr_str + _sep + _mo_str + _sep + _dy_str;
  242. }
  243. function _update_display_time() {
  244. var ele = document.getElementById("ui_main_time");
  245. if (typeof ele === "undefined") { return; }
  246. var _time_str = _get_now_HH_MM();
  247. ele.innerHTML = "<span style='white-space:nowrap;'>" + _time_str + "</span>";
  248. }
  249. //------------
  250. function _update_login_time() {
  251. var ele = document.getElementById("ui_login_time");
  252. if (typeof ele === "undefined") { return; }
  253. var _time_str = _get_now_HH_MM();
  254. ele.innerHTML = "<span style='white-space:nowrap;'>" + _time_str + "</span>";
  255. }
  256. function _update_login_date() {
  257. var ele = document.getElementById("ui_login_date");
  258. if (typeof ele === "undefined") { return; }
  259. var _date_str = _get_now_YYYY_MM_DD();
  260. ele.innerHTML = "<span style='white-space:nowrap;'>" + _date_str + "</span>";
  261. }
  262. //------------
  263. function _update_dimclock_datetime() {
  264. var ele = document.getElementById("ui_dimclock_clock");
  265. if (typeof ele === "undefined") { return; }
  266. var _time_str = _get_now_HH_MM();
  267. ele.innerHTML = "<span style='white-space:nowrap;'>" + _time_str + "</span>";
  268. var ele = document.getElementById("ui_dimclock_date");
  269. if (typeof ele === "undefined") { return; }
  270. var _date_str = _get_now_YYYY_MM_DD();
  271. ele.innerHTML = "<span style='white-space:nowrap;'>" + _date_str + "</span>";
  272. }
  273. //------------
  274. // value is from [0,1]
  275. //
  276. function _process_brightness(brightness, update_system) {
  277. // 0 opacity is completely clear, so (1.0) brightness (100% brightness).
  278. //
  279. var f_brightness = parseFloat(brightness);
  280. var opacity = 1.0 - f_brightness;
  281. // fail
  282. //
  283. if ((opacity < 0.0) || (opacity > 1.0)) { return {"type":"error", "msg":"opacity out of range"}; }
  284. var ele = document.getElementById("dim_overlay");
  285. ele.style.opacity = opacity;
  286. ele = document.getElementById("ui_configuration_displaybrightness");
  287. ival = Math.floor(f_brightness*100.0);
  288. ele.innerHTML = "" + ival + "%";
  289. g_ctx.config.brightness = ival;
  290. if (update_system) { _set_system_config(); }
  291. }
  292. //------------
  293. // volume is from [0,1]
  294. //
  295. function _process_volume(vol, update_system) {
  296. var ele = document.getElementById("ui_configuration_displayvolume");
  297. ivol = Math.floor(vol*100.0);
  298. if (ivol < 0) { ivol = 0; }
  299. if (ivol > 100) { ivol = 100; }
  300. ele.innerHTML = "" + ivol + "%";
  301. g_ctx.config.volume = ivol;
  302. if (update_system) { _set_system_config(); }
  303. }
  304. var _rate_limit_beep_t = -1.0;
  305. var _rate_limit_beep_dt = 250;
  306. function _rate_limit_beep() {
  307. if (_rate_limit_beep_t<0) {
  308. _rate_limit_beep_t = Date.now();
  309. }
  310. if (Date.now() > (_rate_limit_beep_t + _rate_limit_beep_dt)) {
  311. _rate_limit_beep_t = Date.now();
  312. _beep();
  313. }
  314. }
  315. function _slider_change(name) {
  316. if (name == "configuration.volume") {
  317. var val = document.getElementById("ui_configuration_volume").value;
  318. _process_volume(val, true);
  319. ivol = Math.floor(val*100.0);
  320. _api_request("custom", {"function":"volume", "parameter":ivol});
  321. _rate_limit_beep();
  322. }
  323. else if (name == "configuration.brightness") {
  324. var val = document.getElementById("ui_configuration_brightness").value;
  325. _process_brightness(val, true);
  326. }
  327. }
  328. function _volume_test() {
  329. _api_request("custom", {"function":"say", "parameter":"testing 1, 2, 3"});
  330. }
  331. function _btn_show_clock_on_dim_toggle() {
  332. if (g_ctx.config.showdimclock == 0) {
  333. g_ctx.config.showdimclock = 1;
  334. }
  335. else {
  336. g_ctx.config.showdimclock = 0;
  337. }
  338. _process_showdimclock(g_ctx.config.showdimclock, true);
  339. _beep();
  340. }
  341. function _process_showdimclock(val, update_system) {
  342. var ele = document.getElementById("ui_configuration_showclockondim");
  343. if (val == 1) { ele.innerHTML = 'X'; }
  344. else { ele.innerHTML = ''; }
  345. if (update_system) {
  346. _set_system_config();
  347. }
  348. }
  349. //------------
  350. function _clear_ui_driver() {
  351. var ele;
  352. var u = ["driver", "pin"];
  353. for (var i=0; i<u.length; i++) {
  354. g_ctx[u[i]] = "";
  355. ele = document.getElementById("ui_driver_" + u[i]);
  356. ele.innerHTML = "";
  357. ele.style.background = "";
  358. }
  359. ele = document.getElementById("ui_driver_pin");
  360. ele.style.display = "none";
  361. ele = document.getElementById("ui_driver_pin_text");
  362. ele.style.display = "none";
  363. }
  364. function _clear_ui_paddle() {
  365. g_ctx.paddle = "";
  366. var ele = document.getElementById("ui_paddle_paddle");
  367. ele.innerHTML = "";
  368. }
  369. function _clear_ui_bulkaccept() {
  370. var ele;
  371. g_ctx.bulkfare = "";
  372. ele = document.getElementById("ui_bulkaccept_faretype");
  373. ele.innerHTML = "";
  374. g_ctx.bulkfare_count = "";
  375. ele = document.getElementById("ui_bulkaccept_count");
  376. ele.innerHTML = "";
  377. }
  378. function _login() {
  379. _switch_ui('ui_driver');
  380. g_ctx.driver_login_state = "driver";
  381. _clear_ui_driver();
  382. _clear_ui_paddle();
  383. }
  384. //------------
  385. function _verify_driver() {
  386. var driver = g_ctx.driver;
  387. var pin = g_ctx.pin;
  388. // have a fail safe for the verification
  389. //
  390. _set_ui_watchdog();
  391. _api_request("driverlogin", {"driver":driver, "pin":pin}, _verify_driver_ok, _verify_driver_fail);
  392. return;
  393. }
  394. function _verify_driver_ok(dat) {
  395. console.log("driver ok:", dat);
  396. _clear_ui_watchdog();
  397. var tok = dat.split(" ");
  398. // If we have an admin login...
  399. //
  400. if ( (tok.length >= 2) &&
  401. ((tok[1] == "admin") ||
  402. (tok[1] == "AdminMode")) ) {
  403. _switch_ui("ui_diagnostic");
  404. return;
  405. }
  406. _switch_ui("ui_paddle");
  407. }
  408. function _verify_driver_fail() {
  409. console.log("driver fail");
  410. _clear_ui_watchdog();
  411. _switch_ui("ui_driverincorrect");
  412. }
  413. function _driver_incorrect(inp) {
  414. console.log("driver incorrect");
  415. _clear_ui_watchdog();
  416. _clear_ui_driver();
  417. _switch_ui("ui_driver");
  418. }
  419. //------------
  420. function _driver_login(inp) {
  421. var uvar = "";
  422. if (g_ctx.driver_login_state == "driver") { uvar = "driver"; }
  423. else if (g_ctx.driver_login_state == "pin") { uvar = "pin"; }
  424. else {
  425. console.log("_driver_login: ERROR: unknown driver state \"" + g_ctx.driver_login_state + "\"");
  426. return;
  427. }
  428. var n = g_ctx[uvar].length;
  429. if (inp == 'cancel') { _switch_ui('ui_login'); }
  430. else if (inp == 'ok') {
  431. if (uvar == "driver") {
  432. g_ctx.driver_login_state = "pin";
  433. var ele;
  434. ele = document.getElementById("ui_driver_driver");
  435. ele.style.background = "#aaaaaa";
  436. // Show 'pin' box now that the 'driver' box has been filled
  437. //
  438. ele = document.getElementById("ui_driver_pin");
  439. ele.style.display = "block";
  440. ele = document.getElementById("ui_driver_pin_text");
  441. ele.style.display = "block";
  442. }
  443. else if (uvar == "pin") {
  444. g_ctx.driver_login_state = "driver";
  445. _verify_driver();
  446. g_ctx.ui_change_timeoutid = window.setTimeout(function() {
  447. _switch_ui("ui_driververify");
  448. }, 150);
  449. return;
  450. }
  451. }
  452. // manage adding/subtracting/clearing relevant field
  453. //
  454. else if (inp == 'bksp') { g_ctx[uvar] = g_ctx[uvar].substr(0, n-1); }
  455. else if (inp == 'clr') { g_ctx[uvar]= ""; }
  456. else { g_ctx[uvar] += inp; }
  457. // Pin should not be displayed on screen.
  458. // Use '*' to hide it.
  459. //
  460. if (uvar == "pin") {
  461. var n = g_ctx[uvar].length;
  462. var x = "";
  463. for (i=0; i<n; i++) { x += "*"; }
  464. var ele = document.getElementById('ui_driver_' + uvar);
  465. ele.innerHTML = x;
  466. }
  467. // Otherwise update the displayed (driver number)
  468. //
  469. else {
  470. var ele = document.getElementById('ui_driver_' + uvar);
  471. ele.innerHTML = g_ctx[uvar];
  472. }
  473. }
  474. //------------
  475. //------------
  476. function _verify_paddle() {
  477. var paddle = g_ctx.paddle;
  478. // have a fail safe for the verification
  479. //
  480. _set_ui_watchdog();
  481. _api_request("paddleinput", {"paddle":paddle}, _verify_paddle_ack, _verify_paddle_fail);
  482. return;
  483. }
  484. function _verify_paddle_ack() {
  485. console.log("got ack for push paddle message\n");
  486. }
  487. function _verify_paddle_ok() {
  488. console.log("verify paddle ok");
  489. _clear_ui_watchdog();
  490. _switch_ui("ui_main");
  491. }
  492. function _verify_paddle_fail() {
  493. console.log("verify paddle fail");
  494. _clear_ui_watchdog();
  495. _switch_ui("ui_paddleunknown");
  496. }
  497. function _paddle_unknown(inp) {
  498. console.log("paddle unknown...");
  499. _clear_ui_watchdog();
  500. _clear_ui_paddle();
  501. _switch_ui("ui_paddle");
  502. }
  503. function _watchdog_tripped(inp) {
  504. console.log("watchdog UI expired!...");
  505. _reset();
  506. }
  507. //------------
  508. function _paddle_input(inp) {
  509. var n = g_ctx.paddle.length;
  510. if (inp == 'cancel') { _switch_ui('ui_login'); }
  511. else if (inp == 'ok') {
  512. _verify_paddle();
  513. g_ctx.ui_change_timeoutid = window.setTimeout(function() {
  514. _switch_ui("ui_paddleverify");
  515. }, 150);
  516. return;
  517. }
  518. else if (inp == 'bksp') { g_ctx.paddle = g_ctx.paddle.substr(0, n-1); }
  519. else if (inp == 'clr') { g_ctx.paddle = ""; }
  520. else { g_ctx.paddle += inp; }
  521. // Otherwise update the displayed (driver number)
  522. //
  523. var ele = document.getElementById('ui_paddle_paddle');
  524. ele.innerHTML = g_ctx.paddle;
  525. }
  526. //-------------
  527. //-------------
  528. //-------------
  529. function _main_prv() {
  530. console.log("prv");
  531. _api_request("prevstop");
  532. }
  533. function _main_nxt() {
  534. console.log("nxt");
  535. _api_request("nextstop");
  536. }
  537. function _main_dim() {
  538. console.log("dim...");
  539. if (g_ctx.config.showdimclock) {
  540. _switch_ui("ui_dimclock");
  541. }
  542. else {
  543. _api_request("custom", {"function":"dim"});
  544. _switch_ui('ui_dim');
  545. }
  546. }
  547. function _dim_wakeup() {
  548. console.log("waking up from dim");
  549. _api_request("custom", {"function": "undim" });
  550. _switch_ui("ui_main");
  551. }
  552. function _dim_soft_wakeup() {
  553. console.log("waking up from soft dim");
  554. _switch_ui("ui_main");
  555. // just in case
  556. //
  557. _api_request("custom", {"function": "undim" });
  558. }
  559. function _fare_adult() {
  560. console.log("fare adult");
  561. _api_request("fare", {"rule":"CASH-ADULT", "param":"", "fare":"adult","count":1});
  562. }
  563. function _fare_youth() {
  564. console.log("fare youth");
  565. _api_request("fare", {"rule":"CASH-YOUTH", "param":"", "fare":"youth","count":1});
  566. }
  567. function _fare_half() {
  568. console.log("fare half");
  569. _api_request("fare", {"rule":"CASH-HALF", "param":"", "fare":"half","count":1});
  570. }
  571. function _fare_bike() {
  572. console.log("fare bike");
  573. _api_request("fare", {"rule":"MISC-BIKE", "param":"", "fare":"bike","count":1});
  574. }
  575. function _fare_wheelchair() {
  576. console.log("fare wheelchair");
  577. _api_request("fare", {"rule":"MISC-WHEELCHAIR", "param":"", "fare":"wheelchair","count":1});
  578. }
  579. function _fare_transfer() {
  580. console.log("fare transfer");
  581. _api_request("fare", {"rule":"MISC-TRANSFER", "param":"", "fare":"transfer","count":1});
  582. }
  583. //-------------
  584. //-------------
  585. //-------------
  586. function _main_bulk() {
  587. // reset tab to Misc. (first option) by default
  588. //
  589. ele = document.getElementById("ui_bulk_misc");
  590. ele.classList.remove("highlight");
  591. ele.classList.add("highlight");
  592. ele = document.getElementById("ui_bulk_passes");
  593. ele.classList.remove("highlight");
  594. _show_ui_element("ui_bulk_misc_tab");
  595. _hide_ui_element("ui_bulk_passes_tab");
  596. _switch_ui("ui_bulk");
  597. }
  598. function _main_menu() {
  599. // reset tab to Misc. (first option) by default
  600. //
  601. ele = document.getElementById("ui_fareoverride_misc");
  602. ele.classList.remove("highlight");
  603. ele.classList.add("highlight");
  604. ele = document.getElementById("ui_fareoverride_passes");
  605. ele.classList.remove("highlight");
  606. ele = document.getElementById("ui_fareoverride_options");
  607. ele.classList.remove("highlight");
  608. _show_ui_element("ui_fareoverride_misc_tab");
  609. _hide_ui_element("ui_fareoverride_passes_tab");
  610. _hide_ui_element("ui_fareoverride_options_tab");
  611. _switch_ui("ui_fareoverride");
  612. }
  613. //-------------
  614. //-------------
  615. //-------------
  616. function _bulkaccept_ok() {
  617. console.log("bulkfare:", g_ctx.bulkfare, ", count:", g_ctx.bulkfare_count);
  618. _switch_ui("ui_main");
  619. _api_request("fare",{"rule":g_ctx.bulkfare, "param":g_ctx.bulkfare_count.toString(), "fare":g_ctx.bulkfare,"count":g_ctx.bulkfare_count});
  620. }
  621. function _bulkaccept_cancel() {
  622. _switch_ui("ui_main");
  623. }
  624. function _bulkaccept_input(inp) {
  625. var n = g_ctx.bulkfare_count.length;
  626. if (inp == 'cancel') { _switch_ui('ui_main'); }
  627. else if (inp == 'ok') { _bulkaccept_ok(); return; }
  628. else if (inp == 'bksp') { g_ctx.bulkfare_count = g_ctx.bulkfare_count.substr(0, n-1); }
  629. else if (inp == 'clr') { g_ctx.bulkfare_count = ""; }
  630. else { g_ctx.bulkfare_count += inp; }
  631. // Otherwise update the displayed (driver number)
  632. //
  633. var ele = document.getElementById('ui_bulkaccept_count');
  634. ele.innerHTML = g_ctx.bulkfare_count;
  635. }
  636. //-------------
  637. //-------------
  638. //-------------
  639. // space pad
  640. // default 10
  641. //
  642. function _sp(str, align, pad) {
  643. align = ((typeof align=="undefined") ? "left" : align);
  644. pad = ((typeof pad==="undefined") ? 10 : pad);
  645. var n = 0;
  646. n = pad - str.length;
  647. if (align == "left") {
  648. for (var i=0; i<n; i++) { str += " "; }
  649. }
  650. else if (align == "center") {
  651. var rem = n%2;
  652. var n2 = Math.floor(n/2);
  653. var pfx = "", sfx = "";
  654. for (var i=0; i<n2; i++) { pfx += " "; }
  655. for (var i=0; i<(n2+rem); i++) { sfx += " "; }
  656. str = pfx + str + sfx;
  657. }
  658. else if (align == "right") {
  659. var pfx = "";
  660. for (var i=0; i<n; i++) { pfx += " "; }
  661. str = pfx + str;
  662. }
  663. return str;
  664. }
  665. function _cb_status(inp) {
  666. var tok = inp.split(" ");
  667. if (tok.length < 2) { return; }
  668. if (tok[0]!="ok") { return; }
  669. valtok = tok.slice(1).join(" ").split("\n");
  670. if (valtok[0]!="msg=status") { return; }
  671. g_ctx.diu_status = {};
  672. g_ctx.diu_status.last_update = Date.now();
  673. var fields = valtok[1].split("|");
  674. for (var ii=0; ii<fields.length; ii++) {
  675. var kv = fields[ii].split("=");
  676. if (kv.length!=2) { continue; }
  677. g_ctx.diu_status[kv[0].trim()] = kv[1].trim();
  678. }
  679. if ("equipno" in g_ctx.diu_status) {
  680. g_ctx.cur_eqnum = g_ctx.diu_status["equipno"];
  681. }
  682. if (g_ctx.config.last_update < 0) {
  683. _get_system_config();
  684. }
  685. var m = g_ctx.diu_status;
  686. if (g_ctx.current_ui === "ui_login") {
  687. var has_tunn = ((m["tunnel"] == '1') ? 'YES' : 'NO');
  688. var has_gprs = ((m["gprs"] == '1') ? 'YES' : 'NO');
  689. var has_gps = ((m["gps"] == '1') ? 'YES' : 'NO');
  690. var lat = ((typeof m["gpslat"] !== "undefined") ? m["gpslat"] : "");
  691. var lon = ((typeof m["gpslon"] !== "undefined") ? m["gpslon"] : "");
  692. var gps_str = ( ((lat.length>0) && (lon.length>0)) ? (" (" + lat + "/" + lon + ")") : "" );
  693. var ele = document.getElementById("ui_login_status");
  694. var html_a = [];
  695. html_a.push("Tunnel: " + has_tunn + " GPRS: " + has_gprs + " Eq#: " + m["equipno"] + " #Msg: " + m["nmsg"]);
  696. html_a.push("Last Token Read (" + m["last_token"] + ")");
  697. html_a.push("GPS: " + has_gps + gps_str + "");
  698. html_a.push(" ");
  699. // some site specific fiddling to display package information
  700. //
  701. html_a.push(_sp("Package") + _sp("Version", "left", 18) + _sp("Installed", "right", 12));
  702. html_a.push(_sp("ui") + _sp(DIU_UI_VERSION, "left", 18) + _sp(DIU_UI_VERSION_DATE, "right", 12) + "");
  703. for (var _ii=0; _ii<3; _ii++) {
  704. var key = "pkg" + _ii;
  705. if (key in g_ctx.diu_status) {
  706. var aval = g_ctx.diu_status[key].split(" ");
  707. html_a.push(_sp(aval[0]) + _sp(aval[1], "left", 18) + _sp(aval[2], "right", 12));
  708. }
  709. }
  710. html_a.push(" ");
  711. if ("IMEI" in m) { html_a.push("IMEI = " + m["IMEI"] + ""); }
  712. if ("IMSI" in m) { html_a.push("IMSI = " + m["IMSI"] + ""); }
  713. if ("ETH0" in m) { html_a.push("ETH0 = " + m["ETH0"] + ""); }
  714. html_a.push(" ");
  715. for (var ii=0; ii<html_a.length; ii++) {
  716. html_a[ii] = html_a[ii].replace(/ /g, '&nbsp;');
  717. }
  718. var div_hdr = "<div class='textrow'>";
  719. var str = div_hdr + html_a.join("</div><br>" + div_hdr ) + "</div>";
  720. ele.innerHTML = str;
  721. }
  722. else if (g_ctx.current_ui === "ui_main") {
  723. var ele = document.getElementById("mainstatus");
  724. var str = "";
  725. str += "Rt " + m["route"] + " ";
  726. str += "Trip " + m["trip"] + " ";
  727. str += "Stop " + m["stop"] + " ";
  728. str += "GPS " + m["gps"] + " ";
  729. str += "Tun " + m["tunnel"] + ", ";
  730. str += m["date"];
  731. str += " " + moment().format("HH:mm");
  732. ele.innerHTML = str;
  733. }
  734. }
  735. function _cb_status_err(e) {
  736. console.log("ERROR: _cb_status_err:", e);
  737. }
  738. //-------------
  739. function _main_message_clear() {
  740. g_ctx.status_text = "";
  741. }
  742. function _main_message_replace(msg, bg_color, text_color) {
  743. var ele = document.getElementById("ui_main_status");
  744. bg_color = ((typeof bg_color === "undefined") ? BG_COLOR : bg_color);
  745. text_color = ((typeof text_color === "undefined") ? TEXT_COLOR : text_color);
  746. var lines = g_ctx.status_text.split("\n");
  747. if (lines.length > g_ctx.status_text_h) {
  748. var n = lines.length;
  749. lines = lines.slice(n - g_ctx.status_text_h);
  750. }
  751. for (var i=0; i<lines.length; i++) {
  752. lines[i] = lines[i].slice(0,g_ctx.status_text_w-1);
  753. }
  754. var idx = lines.length-1;
  755. if (idx<0) { idx = 0; }
  756. lines[idx] = msg;
  757. var html_lines = [];
  758. for (var i=0; i<lines.length; i++) {
  759. html_lines.push(lines[i].slice(0,g_ctx.status_text_w-1));
  760. if (html_lines[i].match(/ACCEPT/)) {
  761. html_lines[i] = "<div class='textrow accept'>" + html_lines[i] + "</div>";
  762. }
  763. else if (html_lines[i].match(/REJECT/)) {
  764. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  765. }
  766. else if (html_lines[i].match(/[Uu]nknown/)) {
  767. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  768. }
  769. else if (html_lines[i].match(/[Pp]assback/)) {
  770. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  771. }
  772. else if (html_lines[i].match(/Rule execution error/)) {
  773. html_lines[i] = "<div class='textrow error'>" + html_lines[i] + "</div>";
  774. }
  775. else {
  776. html_lines[i] = "<div class='textrow'>" + html_lines[i] + "</div>";
  777. }
  778. }
  779. g_ctx.status_text = lines.join("\n");
  780. g_ctx.status_html_text = html_lines.join("<br>");
  781. ele.innerHTML = g_ctx.status_html_text;
  782. // sounds
  783. //
  784. if (msg.match(/ACCEPT/)) {
  785. _beep_accept();
  786. }
  787. else if (msg.match(/REJECT/)) {
  788. _beep_reject();
  789. }
  790. else if (msg.match(/[Uu]nknown/)) {
  791. _beep_reject();
  792. }
  793. else if (msg.match(/[Pp]assback/)) {
  794. _beep_reject();
  795. }
  796. else if (msg.match(/Rule execution error/)) {
  797. _beep_error();
  798. }
  799. }
  800. function _main_message_add(msg, bg_color, text_color) {
  801. var ele = document.getElementById("ui_main_status");
  802. bg_color = ((typeof bg_color === "undefined") ? BG_COLOR : bg_color);
  803. text_color = ((typeof text_color === "undefined") ? TEXT_COLOR : text_color);
  804. var txt = g_ctx.status_text + "\n" + msg;
  805. var html_lines = [];
  806. var lines = txt.split("\n");
  807. if (lines.length > g_ctx.status_text_h) {
  808. var n = lines.length;
  809. lines = lines.slice(n - g_ctx.status_text_h);
  810. }
  811. for (var i=0; i<lines.length; i++) {
  812. lines[i] = lines[i].slice(0,g_ctx.status_text_w-1);
  813. }
  814. for (var i=0; i<lines.length; i++) {
  815. html_lines.push(lines[i].slice(0,g_ctx.status_text_w-1));
  816. if (html_lines[i].match(/ACCEPT/)) {
  817. html_lines[i] = "<div class='textrow accept'>" + html_lines[i] + "</div>";
  818. }
  819. else if (html_lines[i].match(/REJECT/)) {
  820. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  821. }
  822. else if (html_lines[i].match(/[Uu]nknown/)) {
  823. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  824. }
  825. else if (html_lines[i].match(/[Pp]assback/)) {
  826. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  827. }
  828. else if (html_lines[i].match(/Rule execution error/)) {
  829. html_lines[i] = "<div class='textrow error'>" + html_lines[i] + "</div>";
  830. }
  831. else {
  832. html_lines[i] = "<div class='textrow'>" + html_lines[i] + "</div>";
  833. }
  834. }
  835. g_ctx.status_text = lines.join("\n");
  836. g_ctx.status_html_text = html_lines.join("<br>");
  837. ele.innerHTML = g_ctx.status_html_text;
  838. // sounds
  839. //
  840. if (msg.match(/ACCEPT/)) {
  841. _beep_accept();
  842. }
  843. else if (msg.match(/REJECT/)) {
  844. _beep_reject();
  845. }
  846. else if (msg.match(/[Uu]nknown/)) {
  847. _beep_reject();
  848. }
  849. else if (msg.match(/[Pp]assback/)) {
  850. _beep_reject();
  851. }
  852. else if (msg.match(/Rule execution error/)) {
  853. _beep_error();
  854. }
  855. }
  856. //-------------
  857. // The status message has the config lines, if present,
  858. // so parse them into the 'config' sub-object.
  859. // This should be called after we feel confident the status
  860. // has been updated with the most current config.
  861. //
  862. function _get_system_config() {
  863. var m = g_ctx.diu_status;
  864. if (typeof m === 'undefined') { return; }
  865. g_ctx.config.last_update = Date.now();
  866. if ("brightness" in m) { g_ctx.config.brightness = m["brightness"]; }
  867. if ("volume" in m) { g_ctx.config.volume = m["volume"]; }
  868. if ("showdimclock" in m) { g_ctx.config.showdimclock = m["showdimclock"]; }
  869. _process_volume(g_ctx.config.volume/100.0);
  870. _process_brightness(g_ctx.config.brightness/100.0);
  871. _process_showdimclock(g_ctx.config.showdimclock);
  872. }
  873. //---
  874. function _set_system_config_ok(dat) { }
  875. function _set_system_config_fail(dat) { }
  876. function _set_system_config() {
  877. var dat = {
  878. "function" : "setconfig",
  879. "parameter" : "volume " + g_ctx.config.volume + "," +
  880. "brightness " + g_ctx.config.brightness + "," +
  881. "showdimclock " + g_ctx.config.showdimclock
  882. };
  883. console.log(">>", dat);
  884. _api_request("custom", dat, _set_system_config_ok, _set_system_config_fail);
  885. }
  886. //-------------
  887. //-------------
  888. //-------------
  889. function _ws_process(dat) {
  890. console.log("_ws_process:", dat);
  891. var tok = dat.split(" ");
  892. if (tok.length < 2) { return; }
  893. if (tok[0] == "paddle") {
  894. if (tok[1] == "ok") { _verify_paddle_ok(); }
  895. else if (tok[1] == "fail") { _verify_paddle_fail(); }
  896. else { _verify_paddle_fail(); }
  897. }
  898. else if (tok[0] == "driver_notify") {
  899. if (tok.length < 5) {
  900. console.log("malformed message: ", dat);
  901. }
  902. else if (tok[1] == "replace") {
  903. _main_message_replace(tok.slice(4).join(" "));
  904. }
  905. else if (tok[1] == "ok") {
  906. _main_message_add(tok.slice(4).join(" "));
  907. }
  908. else {
  909. console.log("unknown message: ", dat);
  910. }
  911. }
  912. }
  913. function _init_websocket() {
  914. var sock = new WebSocket("ws://" + _fqADDRESS);
  915. sock.onopen = function(e) { console.log("open", e); }
  916. sock.onmessage = function(e) { _ws_process(e.data); }
  917. sock.onclose = function(e) {
  918. console.log("close", e);
  919. setTimeout( function() { _init_websocket(); }, 1000);
  920. }
  921. sock.onerror = function(e) {
  922. console.log("error", e);
  923. }
  924. }
  925. //-------------
  926. var ui_pattern = {
  927. "state" : "idle",
  928. "lastX" : 0,
  929. "lastY" : 0,
  930. "curX" : 0,
  931. "curY" : 0
  932. };
  933. function ui_pattern_mousedown(ev) {
  934. var ele = document.getElementById("body");
  935. var rect = ele.getBoundingClientRect();
  936. var x = ev["x"];
  937. var y = ev["y"];
  938. var width = rect.width;
  939. var height = rect.height;
  940. if (ui_pattern.state == "idle") {
  941. if (y < (height/4)) {
  942. ui_pattern.state = "square_0";
  943. }
  944. else {
  945. ui_pattern.state = "idle"
  946. }
  947. }
  948. else if (ui_pattern.state == "square_0") {
  949. if (x > (3*width/4)) {
  950. ui_pattern.state = "square_1";
  951. }
  952. else {
  953. ui_pattern.state = "idle"
  954. }
  955. }
  956. else if (ui_pattern.state == "square_1") {
  957. if (y > (3*height/4)) {
  958. ui_pattern.state = "square_2";
  959. }
  960. else {
  961. ui_pattern.state = "idle"
  962. }
  963. }
  964. else if (ui_pattern.state == "square_2") {
  965. if (x < (width/4)) {
  966. ui_pattern.state = "square_3";
  967. }
  968. else {
  969. ui_pattern.state = "idle"
  970. }
  971. }
  972. else if (ui_pattern.state == "square_3") {
  973. if (y < (height/4)) {
  974. ui_pattern.state = "square_4";
  975. }
  976. else {
  977. ui_pattern.state = "idle"
  978. }
  979. }
  980. else if (ui_pattern.state == "square_4") {
  981. if (x > (3*width/4)) {
  982. ui_pattern.state = "square_5";
  983. }
  984. else {
  985. ui_pattern.state = "idle"
  986. }
  987. }
  988. else if (ui_pattern.state == "square_5") {
  989. if (y > (3*height/4)) {
  990. ui_pattern.state = "square_6";
  991. }
  992. else {
  993. ui_pattern.state = "idle"
  994. }
  995. }
  996. else if (ui_pattern.state == "square_6") {
  997. if (x < (width/4)) {
  998. _switch_ui("ui_diagnostic");
  999. }
  1000. ui_pattern.state = "idle"
  1001. }
  1002. return false;
  1003. }
  1004. function ui_pattern_mouseup(ev) {
  1005. return false;
  1006. }
  1007. //------------
  1008. function _mainscreen() {
  1009. _switch_ui('ui_login');
  1010. g_ctx.driver_login_state = "driver";
  1011. _clear_ui_driver();
  1012. _clear_ui_paddle();
  1013. }
  1014. function _configuration() {
  1015. _switch_ui('ui_configuration');
  1016. g_ctx.driver_login_state = "driver";
  1017. _clear_ui_driver();
  1018. _clear_ui_paddle();
  1019. }
  1020. function _configuration_back() {
  1021. var ui_prev = "ui_login";
  1022. if (g_ctx.ui_stack.length > 0) {
  1023. ui_prev = g_ctx.ui_stack.pop();
  1024. }
  1025. _switch_ui(ui_prev);
  1026. if (ui_prev == "ui_login") {
  1027. g_ctx.driver_login_state = "driver";
  1028. _clear_ui_driver();
  1029. _clear_ui_paddle();
  1030. }
  1031. }
  1032. function _eqnum() {
  1033. _switch_ui('ui_eqnum');
  1034. g_ctx.driver_login_state = "driver";
  1035. if (g_ctx.cur_eqnum.length > 0) {
  1036. var ele = document.getElementById("ui_eqnum_eqnum");
  1037. ele.innerHTML = g_ctx.cur_eqnum;
  1038. g_ctx.eqnum = g_ctx.cur_eqnum;
  1039. }
  1040. _clear_ui_driver();
  1041. _clear_ui_paddle();
  1042. }
  1043. function _reset() {
  1044. _switch_ui('ui_login');
  1045. g_ctx.driver_login_state = "driver";
  1046. _clear_ui_driver();
  1047. _clear_ui_paddle();
  1048. location.reload(true);
  1049. }
  1050. function _reboot() {
  1051. _api_request("custom", {"function":"reboot"});
  1052. _switch_ui('ui_login');
  1053. g_ctx.driver_login_state = "driver";
  1054. _clear_ui_driver();
  1055. _clear_ui_paddle();
  1056. }
  1057. function _custom0() {
  1058. _api_request("interfaceupdown", {"interface":"eth0", "state":"up"});
  1059. }
  1060. function _custom1() {
  1061. _api_request("interfaceupdown", {"interface":"eth0", "state":"down"});
  1062. }
  1063. function _update_diagnostic_window(_msg) {
  1064. var ele = document.getElementById("ui_diagnostic_view");
  1065. ele.innerHTML = _msg;
  1066. }
  1067. function _custom2() {
  1068. _api_request("interfaceinfo", {"interface":"eth0"}, _update_diagnostic_window);
  1069. }
  1070. function _custom3() {
  1071. _api_request("say", {"message":"speech test"});
  1072. }
  1073. function _custom4() {
  1074. }
  1075. function _custom5() {
  1076. }
  1077. //------------
  1078. function _set_eqnum() {
  1079. if (g_ctx.eqnum.length == 0) { return; }
  1080. _api_request("eqnum", {"eqnum":g_ctx.eqnum}, function(inp) { console.log("eqnum ok:", inp); }, function(inp) { console.log("eqnum error:", inp); });
  1081. }
  1082. function _eqnum_input(inp) {
  1083. var n = g_ctx.eqnum.length;
  1084. if (inp == 'cancel') { _switch_ui('ui_login'); }
  1085. else if (inp == 'ok') {
  1086. _set_eqnum();
  1087. _switch_ui('ui_login');
  1088. return;
  1089. }
  1090. else if (inp == 'bksp') { g_ctx.eqnum = g_ctx.eqnum.substr(0, n-1); }
  1091. else if (inp == 'clr') { g_ctx.eqnum = ""; }
  1092. else { g_ctx.eqnum += inp; }
  1093. // Otherwise update the displayed equipment number
  1094. //
  1095. var ele = document.getElementById('ui_eqnum_eqnum');
  1096. ele.innerHTML = g_ctx.eqnum;
  1097. }
  1098. //-------------
  1099. function _text_width_height() {
  1100. var _t = document.getElementById("ui_test");
  1101. console.log(">>>", _t.clientHeight, _t.clientWidth);
  1102. }
  1103. function _calculate_ui_main_status_width_height() {
  1104. var _t = document.getElementById("ui_test");
  1105. var ch_w = _t.clientWidth;
  1106. var ch_h = _t.clientHeight;
  1107. var _w = document.getElementById("ui_main_status");
  1108. var win_w = _w.clientWidth;
  1109. var win_h = _w.clientHeight;
  1110. var n_w = Math.floor(win_w / ch_w);
  1111. var n_h = Math.floor(win_h / ch_h);
  1112. return {"n_width": n_w, "n_height":n_h};
  1113. }
  1114. function _init_dim_overlay() {
  1115. var ele = document.getElementById("ui_dim_overlay");
  1116. }
  1117. function init() {
  1118. _init_websocket();
  1119. _init_dim_overlay();
  1120. window.setInterval(_update_display_time, 200);
  1121. window.setInterval(_update_login_time, 200);
  1122. window.setInterval(_update_login_date, 200);
  1123. window.setInterval(_update_dimclock_datetime, 200);
  1124. // watchdog
  1125. //
  1126. document.getElementById('ui_watchdog_btn').onclick = _watchdog_tripped;
  1127. // diagnostics
  1128. //
  1129. document.getElementById('ui_diagnostic_mainscreen').onclick = _mainscreen;
  1130. document.getElementById('ui_diagnostic_reset').onclick = _reset;
  1131. document.getElementById('ui_diagnostic_reboot').onclick = _reboot;
  1132. document.getElementById('ui_diagnostic_custom0').onclick = _custom0;
  1133. document.getElementById('ui_diagnostic_custom1').onclick = _custom1;
  1134. document.getElementById('ui_diagnostic_custom2').onclick = _custom2;
  1135. document.getElementById('ui_diagnostic_custom3').onclick = _custom3;
  1136. document.getElementById('ui_diagnostic_custom4').onclick = _custom4;
  1137. document.getElementById('ui_diagnostic_custom5').onclick = _custom5;
  1138. // configuration
  1139. //
  1140. //document.getElementById('ui_configuration_back').onclick = _mainscreen;
  1141. document.getElementById('ui_configuration_back').onclick = _configuration_back;
  1142. //document.getElementById('ui_configuration_volumetest').onclick = _volume_test;
  1143. document.getElementById('ui_configuration_eqnum').onclick = _eqnum;
  1144. // equipment number change
  1145. //
  1146. document.getElementById('ui_eqnum_cancel').onclick = _mainscreen;
  1147. document.getElementById('ui_eqnum_ok').onclick = function() { console.log("eqnum change"); }
  1148. // login
  1149. //
  1150. document.getElementById('ui_login_login').onclick = _login;
  1151. document.getElementById('ui_login_configuration').onclick = _configuration;
  1152. document.getElementById('ui_driverincorrect_btn').onclick = _driver_incorrect;
  1153. document.getElementById('ui_paddleunknown_btn').onclick = _paddle_unknown;
  1154. // main menu buttons
  1155. //
  1156. document.getElementById('ui_main_prv').onclick = _main_prv;
  1157. document.getElementById('ui_main_nxt').onclick = _main_nxt;
  1158. document.getElementById('ui_main_bulk').onclick = _main_bulk;
  1159. document.getElementById('ui_main_menu').onclick = _main_menu;
  1160. document.getElementById('ui_main_dim').onclick = _main_dim;
  1161. // dim
  1162. //
  1163. document.getElementById('ui_dim_btn').onclick = _dim_wakeup;
  1164. document.getElementById('ui_dimclock_btn').onclick = _dim_soft_wakeup;
  1165. var simple_fares = [ "adult", "youth", "half", "bike", "wheelchair", "transfer"];
  1166. var simple_fares_f = [ _fare_adult, _fare_youth, _fare_half, _fare_bike, _fare_wheelchair, _fare_transfer];
  1167. for (var ii=0; ii<simple_fares.length; ii++) {
  1168. var fare = simple_fares[ii];
  1169. document.getElementById('ui_main_' + fare).onclick = simple_fares_f[ii];
  1170. }
  1171. // bulk menu buttons
  1172. //
  1173. document.getElementById('ui_bulk_passes').onclick =
  1174. function() {
  1175. var ele;
  1176. ele = document.getElementById("ui_bulk_misc");
  1177. ele.classList.remove("highlight");
  1178. ele = document.getElementById("ui_bulk_passes");
  1179. ele.classList.add("highlight");
  1180. _hide_ui_element("ui_bulk_misc_tab");
  1181. _show_ui_element("ui_bulk_passes_tab");
  1182. };
  1183. document.getElementById('ui_bulk_misc').onclick =
  1184. function() {
  1185. var ele;
  1186. ele = document.getElementById("ui_bulk_misc");
  1187. ele.classList.add("highlight");
  1188. ele = document.getElementById("ui_bulk_passes");
  1189. ele.classList.remove("highlight");
  1190. _show_ui_element("ui_bulk_misc_tab");
  1191. _hide_ui_element("ui_bulk_passes_tab");
  1192. };
  1193. document.getElementById('ui_bulk_menu').onclick = function() { _switch_ui("ui_main"); }
  1194. // Setup "Bulk" UI accept buttons to do proper screen switching and
  1195. // other tasks.
  1196. //
  1197. var bulk_pass = ["adult", "youth", "half", "event", "transfer", "interline",
  1198. "schoola", "schoolb", "schoolc", "orga", "orgb", "orgc"];
  1199. var bulk_rule = ["BULK-CASH-ADULT", "BULK-CASH-YOUTH", "BULK-CASH-HALF",
  1200. "BULK-MISC-EVENT", "BULK-MISC-TRANSFER", "BULK-MISC-INTERLINE",
  1201. "BULK-OVR-SCHOOLA", "BULK-OVR-SCHOOLB", "BULK-OVR-SCHOOLC",
  1202. "BULK-OVR-ORGA", "BULK-OVR-ORGB", "BULK-OVR-ORGC"];
  1203. var bulk_pass_name = ["Adult Cash", "Youth Cash", "Half Cash", "Special Event", "Transfer", "Interline",
  1204. "School A", "School B", "School C", "Org A", "Org B", "Org C"];
  1205. // use site specific values if available, otherwise default to the above
  1206. //
  1207. if ((typeof BULK_PASS !== "undefined") && (BULK_PASS.length > 0)) {
  1208. bulk_pass = BULK_PASS;
  1209. }
  1210. if ((typeof BULK_RULE !== "undefined") && (BULK_RULE.length > 0)) {
  1211. bulk_rule= BULK_RULE;
  1212. }
  1213. if ((typeof BULK_PASS_NAME !== "undefined") && (BULK_PASS_NAME.length > 0)) {
  1214. bulk_pass_name = BULK_PASS_NAME;
  1215. }
  1216. for (var i=0; i<bulk_pass.length; i++) {
  1217. var p = bulk_pass[i];
  1218. var u = bulk_pass_name[i];
  1219. var r = bulk_rule[i];
  1220. var ele = document.getElementById('ui_bulk_' + p);
  1221. ele.onclick = (function(x,y) {
  1222. return function() {
  1223. _clear_ui_bulkaccept();
  1224. g_ctx.bulkfare = x;
  1225. _switch_ui("ui_bulkaccept");
  1226. var ee = document.getElementById("ui_bulkaccept_faretype");
  1227. ee.innerHTML = y;
  1228. };
  1229. })(r, u);
  1230. }
  1231. // bulk accept fares
  1232. //
  1233. document.getElementById('ui_bulkaccept_ok').onclick = _bulkaccept_ok;
  1234. document.getElementById('ui_bulkaccept_cancel').onclick = _bulkaccept_cancel;
  1235. // fareoverride menu buttons
  1236. //
  1237. document.getElementById('ui_fareoverride_misc').onclick =
  1238. function() {
  1239. var ele;
  1240. ele = document.getElementById("ui_fareoverride_misc");
  1241. ele.classList.add("highlight");
  1242. ele = document.getElementById("ui_fareoverride_passes");
  1243. ele.classList.remove("highlight");
  1244. ele = document.getElementById("ui_fareoverride_options");
  1245. ele.classList.remove("highlight");
  1246. _show_ui_element("ui_fareoverride_misc_tab");
  1247. _hide_ui_element("ui_fareoverride_passes_tab");
  1248. _hide_ui_element("ui_fareoverride_options_tab");
  1249. };
  1250. document.getElementById('ui_fareoverride_passes').onclick =
  1251. function() {
  1252. var ele;
  1253. ele = document.getElementById("ui_fareoverride_misc");
  1254. ele.classList.remove("highlight");
  1255. ele = document.getElementById("ui_fareoverride_passes");
  1256. ele.classList.add("highlight");
  1257. ele = document.getElementById("ui_fareoverride_options");
  1258. ele.classList.remove("highlight");
  1259. _hide_ui_element("ui_fareoverride_misc_tab");
  1260. _show_ui_element("ui_fareoverride_passes_tab");
  1261. _hide_ui_element("ui_fareoverride_options_tab");
  1262. };
  1263. document.getElementById('ui_fareoverride_options').onclick =
  1264. function() {
  1265. var ele;
  1266. ele = document.getElementById("ui_fareoverride_misc");
  1267. ele.classList.remove("highlight");
  1268. ele = document.getElementById("ui_fareoverride_passes");
  1269. ele.classList.remove("highlight");
  1270. ele = document.getElementById("ui_fareoverride_options");
  1271. ele.classList.add("highlight");
  1272. _hide_ui_element("ui_fareoverride_misc_tab");
  1273. _hide_ui_element("ui_fareoverride_passes_tab");
  1274. _show_ui_element("ui_fareoverride_options_tab");
  1275. };
  1276. document.getElementById('ui_fareoverride_menu').onclick = function() { _switch_ui("ui_main"); };
  1277. document.getElementById('ui_fareoverride_config').onclick =
  1278. function() {
  1279. g_ctx.ui_stack.push("ui_fareoverride");
  1280. _switch_ui("ui_configuration");
  1281. };
  1282. document.getElementById('ui_fareoverride_logout').onclick =
  1283. function() {
  1284. _main_message_clear();
  1285. _api_request("logout");
  1286. _switch_ui("ui_login");
  1287. };
  1288. // default values for override passes
  1289. //
  1290. var override_pass = [ "adult", "youth", "half",
  1291. "bike", "wheelchair", "transfer",
  1292. "event", "interline",
  1293. "schoola", "schoolb", "schoolc",
  1294. "orga", "orgb", "orgc" ];
  1295. var override_rule = [ "CASH-ADULT", "CASH-YOUTH", "CASH-HALF",
  1296. "MISC-BIKE", "MISC-WHEELCHAIR", "MISC-TRANSFER",
  1297. "MISC-EVENT", "MISC-INTERLINE",
  1298. "OVR-SCHOOLA", "OVR-SCHOOLB", "OVR-SCHOOLC",
  1299. "OVR-ORGA", "OVR-ORGB", "OVR-ORGC" ];
  1300. // use site specific values if available, otherwise default to the above
  1301. //
  1302. if ((typeof OVERRIDE_PASS !== "undefined") && (OVERRIDE_PASS.length > 0)) {
  1303. override_pass = OVERRIDE_PASS;
  1304. }
  1305. if ((typeof OVERRIDE_RULE !== "undefined") && (OVERRIDE_RULE.length > 0)) {
  1306. override_rule= OVERRIDE_RULE;
  1307. }
  1308. for (var i=0; i<override_pass.length; i++) {
  1309. var p = override_pass[i];
  1310. var r = override_rule[i];
  1311. var ele = document.getElementById("ui_fareoverride_" + p);
  1312. ele.onclick = (function(x,y) {
  1313. return function() { _api_request("fare", {"rule":x,"fare":y,"param":"","count":1}); };
  1314. })(r,p);
  1315. }
  1316. var ui_page = [ "driver", "paddle", "bulkaccept", "eqnum" ];
  1317. var ui_func = [ _driver_login, _paddle_input, _bulkaccept_input, _eqnum_input ];
  1318. for (var idx=0; idx<ui_page.length; idx++) {
  1319. var c = ui_page[idx];
  1320. var f = ui_func[idx];
  1321. for (var i=0; i<=9; i++) {
  1322. document.getElementById('ui_' + c + '_' + i).onclick =
  1323. (function(_f, x) { return function() { _f(x); } })(f, i);
  1324. }
  1325. document.getElementById('ui_' + c + '_' + 'bksp').onclick =
  1326. (function(_f,x) { return function() { _f(x); } })(f, 'bksp');
  1327. document.getElementById('ui_' + c + '_' + 'clr').onclick =
  1328. (function(_f,x) { return function() { _f(x); } })(f, 'clr');
  1329. document.getElementById('ui_' + c + '_' + 'ok').onclick =
  1330. (function(_f,x) { return function() { _f(x); } })(f, 'ok');
  1331. document.getElementById('ui_' + c + '_' + 'cancel').onclick =
  1332. (function(_f,x) { return function() { _f(x); } })(f, 'cancel');
  1333. }
  1334. // set interval to check status every 1 second
  1335. //
  1336. var x = window.setInterval(function() { _api_request("status", {}, _cb_status, _cb_status_err); }, 1000);
  1337. // try and reduce textarea highlighting, drag and drop/copy pasting,
  1338. // and other bad behavor.
  1339. //
  1340. var ele;
  1341. var block_ele;
  1342. var rect;
  1343. ele = document.getElementById("ui_login_status");
  1344. ele.ondragstart = function() { return false; }
  1345. ele.onselectstart = function() { return false; }
  1346. ele.onmousedown = function() { return false; }
  1347. ele.ondragstart = function() { return false; }
  1348. rect = ele.getBoundingClientRect();
  1349. block_ele = document.getElementById("ui_login_blockwindow");
  1350. block_ele.style.width = rect.width;
  1351. block_ele.style.height = rect.height;
  1352. block_ele.style.bottom = rect.bottom;
  1353. block_ele.style.left = rect.left;
  1354. block_ele.style.top = rect.top;
  1355. block_ele.style.right = rect.right;
  1356. block_ele.style.x = rect.x;
  1357. block_ele.style.y = rect.y;
  1358. ele = document.getElementById("ui_main_status");
  1359. ele.ondragstart = function() { return false; }
  1360. ele.onselectstart = function() { return false; }
  1361. ele.onmousedown = function() { return false; }
  1362. ele.ondragstart = function() { return false; }
  1363. rect = ele.getBoundingClientRect();
  1364. block_ele = document.getElementById("ui_main_blockwindow");
  1365. block_ele.style.width = rect.width;
  1366. block_ele.style.height = rect.height;
  1367. block_ele.style.bottom = rect.bottom;
  1368. block_ele.style.left = rect.left;
  1369. block_ele.style.top = rect.top;
  1370. block_ele.style.right = rect.right;
  1371. block_ele.style.x = rect.x;
  1372. block_ele.style.y = rect.y;
  1373. ele = document.getElementById("body");
  1374. // We're using jquery so we need to gracefully add callbacks
  1375. // for mouse events.
  1376. //
  1377. ele.addEventListener('mousedown', ui_pattern_mousedown);
  1378. ele.addEventListener('mouseup', ui_pattern_mouseup);
  1379. g_snd.button_press = new Audio("assets/459992_florianreichelt_beep_short.mp3");
  1380. //g_snd.accept_fare = new Audio("assets/443026__qubodup__public-domain-beep-sound.mp3");
  1381. g_snd.accept_fare = new Audio("assets/389214__joe-anderson22__microwave-beeps.mp3");
  1382. g_snd.reject_fare = new Audio("assets/368780__gurie__start-sound-beep.mp3");
  1383. g_snd.rule_error = new Audio("assets/363920__samsterbirdies__8-bit-error.mp3");
  1384. if (typeof PopufareSiteInit !== "undefined") {
  1385. PopufareSiteInit();
  1386. }
  1387. }
  1388. (function($) {
  1389. $(document).ready(function() {
  1390. init();
  1391. });
  1392. })(jQuery);