diu_ui-ORG.js 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810
  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 = parseInt(m["brightness"]); }
  867. if ("volume" in m) { g_ctx.config.volume = parseInt(m["volume"]); }
  868. if ("showdimclock" in m) { g_ctx.config.showdimclock = parseInt(m["showdimclock"]); }
  869. var ele;
  870. ele = $("#ui_configuration_volume");
  871. if (ele) { ele.val(g_ctx.config.volume/100.0); }
  872. ele = $("#ui_configuration_brightness");
  873. if (ele) { ele.val(g_ctx.config.brightness/100.0); }
  874. ele = document.getElementById("ui_configuration_showclockondim");
  875. if (ele) { ele.innerHTML = ((g_ctx.config.showdimclock == 1) ? "X" : "" ); }
  876. _process_volume(g_ctx.config.volume/100.0);
  877. _process_brightness(g_ctx.config.brightness/100.0);
  878. _process_showdimclock(g_ctx.config.showdimclock);
  879. }
  880. //---
  881. function _set_system_config_ok(dat) { }
  882. function _set_system_config_fail(dat) { }
  883. function _set_system_config() {
  884. var dat = {
  885. "function" : "setconfig",
  886. "parameter" : "volume " + g_ctx.config.volume + "," +
  887. "brightness " + g_ctx.config.brightness + "," +
  888. "showdimclock " + g_ctx.config.showdimclock
  889. };
  890. console.log(">>", dat);
  891. _api_request("custom", dat, _set_system_config_ok, _set_system_config_fail);
  892. }
  893. //-------------
  894. //-------------
  895. //-------------
  896. function _ws_process(dat) {
  897. console.log("_ws_process:", dat);
  898. var tok = dat.split(" ");
  899. if (tok.length < 2) { return; }
  900. if (tok[0] == "paddle") {
  901. if (tok[1] == "ok") { _verify_paddle_ok(); }
  902. else if (tok[1] == "fail") { _verify_paddle_fail(); }
  903. else { _verify_paddle_fail(); }
  904. }
  905. else if (tok[0] == "driver_notify") {
  906. if (tok.length < 5) {
  907. console.log("malformed message: ", dat);
  908. }
  909. else if (tok[1] == "replace") {
  910. _main_message_replace(tok.slice(4).join(" "));
  911. }
  912. else if (tok[1] == "ok") {
  913. _main_message_add(tok.slice(4).join(" "));
  914. }
  915. else {
  916. console.log("unknown message: ", dat);
  917. }
  918. }
  919. }
  920. function _init_websocket() {
  921. var sock = new WebSocket("ws://" + _fqADDRESS);
  922. sock.onopen = function(e) { console.log("open", e); }
  923. sock.onmessage = function(e) { _ws_process(e.data); }
  924. sock.onclose = function(e) {
  925. console.log("close", e);
  926. setTimeout( function() { _init_websocket(); }, 1000);
  927. }
  928. sock.onerror = function(e) {
  929. console.log("error", e);
  930. }
  931. }
  932. //-------------
  933. var ui_pattern = {
  934. "state" : "idle",
  935. "lastX" : 0,
  936. "lastY" : 0,
  937. "curX" : 0,
  938. "curY" : 0
  939. };
  940. function ui_pattern_mousedown(ev) {
  941. var ele = document.getElementById("body");
  942. var rect = ele.getBoundingClientRect();
  943. var x = ev["x"];
  944. var y = ev["y"];
  945. var width = rect.width;
  946. var height = rect.height;
  947. if (ui_pattern.state == "idle") {
  948. if (y < (height/4)) {
  949. ui_pattern.state = "square_0";
  950. }
  951. else {
  952. ui_pattern.state = "idle"
  953. }
  954. }
  955. else if (ui_pattern.state == "square_0") {
  956. if (x > (3*width/4)) {
  957. ui_pattern.state = "square_1";
  958. }
  959. else {
  960. ui_pattern.state = "idle"
  961. }
  962. }
  963. else if (ui_pattern.state == "square_1") {
  964. if (y > (3*height/4)) {
  965. ui_pattern.state = "square_2";
  966. }
  967. else {
  968. ui_pattern.state = "idle"
  969. }
  970. }
  971. else if (ui_pattern.state == "square_2") {
  972. if (x < (width/4)) {
  973. ui_pattern.state = "square_3";
  974. }
  975. else {
  976. ui_pattern.state = "idle"
  977. }
  978. }
  979. else if (ui_pattern.state == "square_3") {
  980. if (y < (height/4)) {
  981. ui_pattern.state = "square_4";
  982. }
  983. else {
  984. ui_pattern.state = "idle"
  985. }
  986. }
  987. else if (ui_pattern.state == "square_4") {
  988. if (x > (3*width/4)) {
  989. ui_pattern.state = "square_5";
  990. }
  991. else {
  992. ui_pattern.state = "idle"
  993. }
  994. }
  995. else if (ui_pattern.state == "square_5") {
  996. if (y > (3*height/4)) {
  997. ui_pattern.state = "square_6";
  998. }
  999. else {
  1000. ui_pattern.state = "idle"
  1001. }
  1002. }
  1003. else if (ui_pattern.state == "square_6") {
  1004. if (x < (width/4)) {
  1005. _switch_ui("ui_diagnostic");
  1006. }
  1007. ui_pattern.state = "idle"
  1008. }
  1009. return false;
  1010. }
  1011. function ui_pattern_mouseup(ev) {
  1012. return false;
  1013. }
  1014. //------------
  1015. function _mainscreen() {
  1016. _switch_ui('ui_login');
  1017. g_ctx.driver_login_state = "driver";
  1018. _clear_ui_driver();
  1019. _clear_ui_paddle();
  1020. }
  1021. function _configuration() {
  1022. _switch_ui('ui_configuration');
  1023. g_ctx.driver_login_state = "driver";
  1024. _clear_ui_driver();
  1025. _clear_ui_paddle();
  1026. }
  1027. function _configuration_back() {
  1028. var ui_prev = "ui_login";
  1029. if (g_ctx.ui_stack.length > 0) {
  1030. ui_prev = g_ctx.ui_stack.pop();
  1031. }
  1032. _switch_ui(ui_prev);
  1033. if (ui_prev == "ui_login") {
  1034. g_ctx.driver_login_state = "driver";
  1035. _clear_ui_driver();
  1036. _clear_ui_paddle();
  1037. }
  1038. }
  1039. function _eqnum() {
  1040. _switch_ui('ui_eqnum');
  1041. g_ctx.driver_login_state = "driver";
  1042. if (g_ctx.cur_eqnum.length > 0) {
  1043. var ele = document.getElementById("ui_eqnum_eqnum");
  1044. ele.innerHTML = g_ctx.cur_eqnum;
  1045. g_ctx.eqnum = g_ctx.cur_eqnum;
  1046. }
  1047. _clear_ui_driver();
  1048. _clear_ui_paddle();
  1049. }
  1050. function _reset() {
  1051. _switch_ui('ui_login');
  1052. g_ctx.driver_login_state = "driver";
  1053. _clear_ui_driver();
  1054. _clear_ui_paddle();
  1055. location.reload(true);
  1056. }
  1057. function _reboot() {
  1058. _api_request("custom", {"function":"reboot"});
  1059. _switch_ui('ui_login');
  1060. g_ctx.driver_login_state = "driver";
  1061. _clear_ui_driver();
  1062. _clear_ui_paddle();
  1063. }
  1064. function _custom0() {
  1065. _api_request("interfaceupdown", {"interface":"eth0", "state":"up"});
  1066. }
  1067. function _custom1() {
  1068. _api_request("interfaceupdown", {"interface":"eth0", "state":"down"});
  1069. }
  1070. function _update_diagnostic_window(_msg) {
  1071. var ele = document.getElementById("ui_diagnostic_view");
  1072. ele.innerHTML = _msg;
  1073. }
  1074. function _custom2() {
  1075. _api_request("interfaceinfo", {"interface":"eth0"}, _update_diagnostic_window);
  1076. }
  1077. function _custom3() {
  1078. _api_request("say", {"message":"speech test"});
  1079. }
  1080. function _custom4() {
  1081. }
  1082. function _custom5() {
  1083. }
  1084. //------------
  1085. function _set_eqnum() {
  1086. if (g_ctx.eqnum.length == 0) { return; }
  1087. _api_request("eqnum", {"eqnum":g_ctx.eqnum}, function(inp) { console.log("eqnum ok:", inp); }, function(inp) { console.log("eqnum error:", inp); });
  1088. }
  1089. function _eqnum_input(inp) {
  1090. var n = g_ctx.eqnum.length;
  1091. if (inp == 'cancel') { _switch_ui('ui_login'); }
  1092. else if (inp == 'ok') {
  1093. _set_eqnum();
  1094. _switch_ui('ui_login');
  1095. return;
  1096. }
  1097. else if (inp == 'bksp') { g_ctx.eqnum = g_ctx.eqnum.substr(0, n-1); }
  1098. else if (inp == 'clr') { g_ctx.eqnum = ""; }
  1099. else { g_ctx.eqnum += inp; }
  1100. // Otherwise update the displayed equipment number
  1101. //
  1102. var ele = document.getElementById('ui_eqnum_eqnum');
  1103. ele.innerHTML = g_ctx.eqnum;
  1104. }
  1105. //-------------
  1106. function _text_width_height() {
  1107. var _t = document.getElementById("ui_test");
  1108. console.log(">>>", _t.clientHeight, _t.clientWidth);
  1109. }
  1110. function _calculate_ui_main_status_width_height() {
  1111. var _t = document.getElementById("ui_test");
  1112. var ch_w = _t.clientWidth;
  1113. var ch_h = _t.clientHeight;
  1114. var _w = document.getElementById("ui_main_status");
  1115. var win_w = _w.clientWidth;
  1116. var win_h = _w.clientHeight;
  1117. var n_w = Math.floor(win_w / ch_w);
  1118. var n_h = Math.floor(win_h / ch_h);
  1119. return {"n_width": n_w, "n_height":n_h};
  1120. }
  1121. function _init_dim_overlay() {
  1122. var ele = document.getElementById("ui_dim_overlay");
  1123. }
  1124. function init() {
  1125. _init_websocket();
  1126. _init_dim_overlay();
  1127. window.setInterval(_update_display_time, 200);
  1128. window.setInterval(_update_login_time, 200);
  1129. window.setInterval(_update_login_date, 200);
  1130. window.setInterval(_update_dimclock_datetime, 200);
  1131. // watchdog
  1132. //
  1133. document.getElementById('ui_watchdog_btn').onclick = _watchdog_tripped;
  1134. // diagnostics
  1135. //
  1136. document.getElementById('ui_diagnostic_mainscreen').onclick = _mainscreen;
  1137. document.getElementById('ui_diagnostic_reset').onclick = _reset;
  1138. document.getElementById('ui_diagnostic_reboot').onclick = _reboot;
  1139. document.getElementById('ui_diagnostic_custom0').onclick = _custom0;
  1140. document.getElementById('ui_diagnostic_custom1').onclick = _custom1;
  1141. document.getElementById('ui_diagnostic_custom2').onclick = _custom2;
  1142. document.getElementById('ui_diagnostic_custom3').onclick = _custom3;
  1143. document.getElementById('ui_diagnostic_custom4').onclick = _custom4;
  1144. document.getElementById('ui_diagnostic_custom5').onclick = _custom5;
  1145. // configuration
  1146. //
  1147. //document.getElementById('ui_configuration_back').onclick = _mainscreen;
  1148. document.getElementById('ui_configuration_back').onclick = _configuration_back;
  1149. //document.getElementById('ui_configuration_volumetest').onclick = _volume_test;
  1150. document.getElementById('ui_configuration_eqnum').onclick = _eqnum;
  1151. // equipment number change
  1152. //
  1153. document.getElementById('ui_eqnum_cancel').onclick = _mainscreen;
  1154. document.getElementById('ui_eqnum_ok').onclick = function() { console.log("eqnum change"); }
  1155. // login
  1156. //
  1157. document.getElementById('ui_login_login').onclick = _login;
  1158. document.getElementById('ui_login_configuration').onclick = _configuration;
  1159. document.getElementById('ui_driverincorrect_btn').onclick = _driver_incorrect;
  1160. document.getElementById('ui_paddleunknown_btn').onclick = _paddle_unknown;
  1161. // main menu buttons
  1162. //
  1163. document.getElementById('ui_main_prv').onclick = _main_prv;
  1164. document.getElementById('ui_main_nxt').onclick = _main_nxt;
  1165. document.getElementById('ui_main_bulk').onclick = _main_bulk;
  1166. document.getElementById('ui_main_menu').onclick = _main_menu;
  1167. document.getElementById('ui_main_dim').onclick = _main_dim;
  1168. // dim
  1169. //
  1170. document.getElementById('ui_dim_btn').onclick = _dim_wakeup;
  1171. document.getElementById('ui_dimclock_btn').onclick = _dim_soft_wakeup;
  1172. var simple_fares = [ "adult", "youth", "half", "bike", "wheelchair", "transfer"];
  1173. var simple_fares_f = [ _fare_adult, _fare_youth, _fare_half, _fare_bike, _fare_wheelchair, _fare_transfer];
  1174. for (var ii=0; ii<simple_fares.length; ii++) {
  1175. var fare = simple_fares[ii];
  1176. document.getElementById('ui_main_' + fare).onclick = simple_fares_f[ii];
  1177. }
  1178. // bulk menu buttons
  1179. //
  1180. document.getElementById('ui_bulk_passes').onclick =
  1181. function() {
  1182. var ele;
  1183. ele = document.getElementById("ui_bulk_misc");
  1184. ele.classList.remove("highlight");
  1185. ele = document.getElementById("ui_bulk_passes");
  1186. ele.classList.add("highlight");
  1187. _hide_ui_element("ui_bulk_misc_tab");
  1188. _show_ui_element("ui_bulk_passes_tab");
  1189. };
  1190. document.getElementById('ui_bulk_misc').onclick =
  1191. function() {
  1192. var ele;
  1193. ele = document.getElementById("ui_bulk_misc");
  1194. ele.classList.add("highlight");
  1195. ele = document.getElementById("ui_bulk_passes");
  1196. ele.classList.remove("highlight");
  1197. _show_ui_element("ui_bulk_misc_tab");
  1198. _hide_ui_element("ui_bulk_passes_tab");
  1199. };
  1200. document.getElementById('ui_bulk_menu').onclick = function() { _switch_ui("ui_main"); }
  1201. // Setup "Bulk" UI accept buttons to do proper screen switching and
  1202. // other tasks.
  1203. //
  1204. var bulk_pass = ["adult", "youth", "half", "event", "transfer", "interline",
  1205. "schoola", "schoolb", "schoolc", "orga", "orgb", "orgc"];
  1206. var bulk_rule = ["BULK-CASH-ADULT", "BULK-CASH-YOUTH", "BULK-CASH-HALF",
  1207. "BULK-MISC-EVENT", "BULK-MISC-TRANSFER", "BULK-MISC-INTERLINE",
  1208. "BULK-OVR-SCHOOLA", "BULK-OVR-SCHOOLB", "BULK-OVR-SCHOOLC",
  1209. "BULK-OVR-ORGA", "BULK-OVR-ORGB", "BULK-OVR-ORGC"];
  1210. var bulk_pass_name = ["Adult Cash", "Youth Cash", "Half Cash", "Special Event", "Transfer", "Interline",
  1211. "School A", "School B", "School C", "Org A", "Org B", "Org C"];
  1212. // use site specific values if available, otherwise default to the above
  1213. //
  1214. if ((typeof BULK_PASS !== "undefined") && (BULK_PASS.length > 0)) {
  1215. bulk_pass = BULK_PASS;
  1216. }
  1217. if ((typeof BULK_RULE !== "undefined") && (BULK_RULE.length > 0)) {
  1218. bulk_rule= BULK_RULE;
  1219. }
  1220. if ((typeof BULK_PASS_NAME !== "undefined") && (BULK_PASS_NAME.length > 0)) {
  1221. bulk_pass_name = BULK_PASS_NAME;
  1222. }
  1223. for (var i=0; i<bulk_pass.length; i++) {
  1224. var p = bulk_pass[i];
  1225. var u = bulk_pass_name[i];
  1226. var r = bulk_rule[i];
  1227. var ele = document.getElementById('ui_bulk_' + p);
  1228. ele.onclick = (function(x,y) {
  1229. return function() {
  1230. _clear_ui_bulkaccept();
  1231. g_ctx.bulkfare = x;
  1232. _switch_ui("ui_bulkaccept");
  1233. var ee = document.getElementById("ui_bulkaccept_faretype");
  1234. ee.innerHTML = y;
  1235. };
  1236. })(r, u);
  1237. }
  1238. // bulk accept fares
  1239. //
  1240. document.getElementById('ui_bulkaccept_ok').onclick = _bulkaccept_ok;
  1241. document.getElementById('ui_bulkaccept_cancel').onclick = _bulkaccept_cancel;
  1242. // fareoverride menu buttons
  1243. //
  1244. document.getElementById('ui_fareoverride_misc').onclick =
  1245. function() {
  1246. var ele;
  1247. ele = document.getElementById("ui_fareoverride_misc");
  1248. ele.classList.add("highlight");
  1249. ele = document.getElementById("ui_fareoverride_passes");
  1250. ele.classList.remove("highlight");
  1251. ele = document.getElementById("ui_fareoverride_options");
  1252. ele.classList.remove("highlight");
  1253. _show_ui_element("ui_fareoverride_misc_tab");
  1254. _hide_ui_element("ui_fareoverride_passes_tab");
  1255. _hide_ui_element("ui_fareoverride_options_tab");
  1256. };
  1257. document.getElementById('ui_fareoverride_passes').onclick =
  1258. function() {
  1259. var ele;
  1260. ele = document.getElementById("ui_fareoverride_misc");
  1261. ele.classList.remove("highlight");
  1262. ele = document.getElementById("ui_fareoverride_passes");
  1263. ele.classList.add("highlight");
  1264. ele = document.getElementById("ui_fareoverride_options");
  1265. ele.classList.remove("highlight");
  1266. _hide_ui_element("ui_fareoverride_misc_tab");
  1267. _show_ui_element("ui_fareoverride_passes_tab");
  1268. _hide_ui_element("ui_fareoverride_options_tab");
  1269. };
  1270. document.getElementById('ui_fareoverride_options').onclick =
  1271. function() {
  1272. var ele;
  1273. ele = document.getElementById("ui_fareoverride_misc");
  1274. ele.classList.remove("highlight");
  1275. ele = document.getElementById("ui_fareoverride_passes");
  1276. ele.classList.remove("highlight");
  1277. ele = document.getElementById("ui_fareoverride_options");
  1278. ele.classList.add("highlight");
  1279. _hide_ui_element("ui_fareoverride_misc_tab");
  1280. _hide_ui_element("ui_fareoverride_passes_tab");
  1281. _show_ui_element("ui_fareoverride_options_tab");
  1282. };
  1283. document.getElementById('ui_fareoverride_menu').onclick = function() { _switch_ui("ui_main"); };
  1284. document.getElementById('ui_fareoverride_config').onclick =
  1285. function() {
  1286. g_ctx.ui_stack.push("ui_fareoverride");
  1287. _switch_ui("ui_configuration");
  1288. };
  1289. document.getElementById('ui_fareoverride_logout').onclick =
  1290. function() {
  1291. _main_message_clear();
  1292. _api_request("logout");
  1293. _switch_ui("ui_login");
  1294. };
  1295. // default values for override passes
  1296. //
  1297. var override_pass = [ "adult", "youth", "half",
  1298. "bike", "wheelchair", "transfer",
  1299. "event", "interline",
  1300. "schoola", "schoolb", "schoolc",
  1301. "orga", "orgb", "orgc" ];
  1302. var override_rule = [ "CASH-ADULT", "CASH-YOUTH", "CASH-HALF",
  1303. "MISC-BIKE", "MISC-WHEELCHAIR", "MISC-TRANSFER",
  1304. "MISC-EVENT", "MISC-INTERLINE",
  1305. "OVR-SCHOOLA", "OVR-SCHOOLB", "OVR-SCHOOLC",
  1306. "OVR-ORGA", "OVR-ORGB", "OVR-ORGC" ];
  1307. // use site specific values if available, otherwise default to the above
  1308. //
  1309. if ((typeof OVERRIDE_PASS !== "undefined") && (OVERRIDE_PASS.length > 0)) {
  1310. override_pass = OVERRIDE_PASS;
  1311. }
  1312. if ((typeof OVERRIDE_RULE !== "undefined") && (OVERRIDE_RULE.length > 0)) {
  1313. override_rule= OVERRIDE_RULE;
  1314. }
  1315. for (var i=0; i<override_pass.length; i++) {
  1316. var p = override_pass[i];
  1317. var r = override_rule[i];
  1318. var ele = document.getElementById("ui_fareoverride_" + p);
  1319. ele.onclick = (function(x,y) {
  1320. return function() { _api_request("fare", {"rule":x,"fare":y,"param":"","count":1}); };
  1321. })(r,p);
  1322. }
  1323. var ui_page = [ "driver", "paddle", "bulkaccept", "eqnum" ];
  1324. var ui_func = [ _driver_login, _paddle_input, _bulkaccept_input, _eqnum_input ];
  1325. for (var idx=0; idx<ui_page.length; idx++) {
  1326. var c = ui_page[idx];
  1327. var f = ui_func[idx];
  1328. for (var i=0; i<=9; i++) {
  1329. document.getElementById('ui_' + c + '_' + i).onclick =
  1330. (function(_f, x) { return function() { _f(x); } })(f, i);
  1331. }
  1332. document.getElementById('ui_' + c + '_' + 'bksp').onclick =
  1333. (function(_f,x) { return function() { _f(x); } })(f, 'bksp');
  1334. document.getElementById('ui_' + c + '_' + 'clr').onclick =
  1335. (function(_f,x) { return function() { _f(x); } })(f, 'clr');
  1336. document.getElementById('ui_' + c + '_' + 'ok').onclick =
  1337. (function(_f,x) { return function() { _f(x); } })(f, 'ok');
  1338. document.getElementById('ui_' + c + '_' + 'cancel').onclick =
  1339. (function(_f,x) { return function() { _f(x); } })(f, 'cancel');
  1340. }
  1341. // set interval to check status every 1 second
  1342. //
  1343. var x = window.setInterval(function() { _api_request("status", {}, _cb_status, _cb_status_err); }, 1000);
  1344. // try and reduce textarea highlighting, drag and drop/copy pasting,
  1345. // and other bad behavor.
  1346. //
  1347. var ele;
  1348. var block_ele;
  1349. var rect;
  1350. ele = document.getElementById("ui_login_status");
  1351. ele.ondragstart = function() { return false; }
  1352. ele.onselectstart = function() { return false; }
  1353. ele.onmousedown = function() { return false; }
  1354. ele.ondragstart = function() { return false; }
  1355. rect = ele.getBoundingClientRect();
  1356. block_ele = document.getElementById("ui_login_blockwindow");
  1357. block_ele.style.width = rect.width;
  1358. block_ele.style.height = rect.height;
  1359. block_ele.style.bottom = rect.bottom;
  1360. block_ele.style.left = rect.left;
  1361. block_ele.style.top = rect.top;
  1362. block_ele.style.right = rect.right;
  1363. block_ele.style.x = rect.x;
  1364. block_ele.style.y = rect.y;
  1365. ele = document.getElementById("ui_main_status");
  1366. ele.ondragstart = function() { return false; }
  1367. ele.onselectstart = function() { return false; }
  1368. ele.onmousedown = function() { return false; }
  1369. ele.ondragstart = function() { return false; }
  1370. rect = ele.getBoundingClientRect();
  1371. block_ele = document.getElementById("ui_main_blockwindow");
  1372. block_ele.style.width = rect.width;
  1373. block_ele.style.height = rect.height;
  1374. block_ele.style.bottom = rect.bottom;
  1375. block_ele.style.left = rect.left;
  1376. block_ele.style.top = rect.top;
  1377. block_ele.style.right = rect.right;
  1378. block_ele.style.x = rect.x;
  1379. block_ele.style.y = rect.y;
  1380. ele = document.getElementById("body");
  1381. // We're using jquery so we need to gracefully add callbacks
  1382. // for mouse events.
  1383. //
  1384. ele.addEventListener('mousedown', ui_pattern_mousedown);
  1385. ele.addEventListener('mouseup', ui_pattern_mouseup);
  1386. g_snd.button_press = new Audio("assets/459992_florianreichelt_beep_short.mp3");
  1387. //g_snd.accept_fare = new Audio("assets/443026__qubodup__public-domain-beep-sound.mp3");
  1388. g_snd.accept_fare = new Audio("assets/389214__joe-anderson22__microwave-beeps.mp3");
  1389. g_snd.reject_fare = new Audio("assets/368780__gurie__start-sound-beep.mp3");
  1390. g_snd.rule_error = new Audio("assets/363920__samsterbirdies__8-bit-error.mp3");
  1391. if (typeof PopufareSiteInit !== "undefined") {
  1392. PopufareSiteInit();
  1393. }
  1394. }
  1395. (function($) {
  1396. $(document).ready(function() {
  1397. init();
  1398. });
  1399. })(jQuery);