diu_ui-ORG.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  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.5";
  27. var DIU_UI_VERSION_DATE = "2019-11-27 13:09:00";
  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. "current_ui" : "ui_login",
  56. "ui_change_timeoutid": -1,
  57. "bulkfare" : "",
  58. "bulkfare_count" : "",
  59. "status_text": "",
  60. "status_text_w": 32,
  61. "status_text_h": 11,
  62. "status_html_text":"",
  63. "diu_status" : {},
  64. "x": ""
  65. };
  66. function _api_fail() {
  67. console.log(">>api fail");
  68. return true;
  69. }
  70. function _api_tick() { }
  71. function _api_request(action, param, cb_ok, cb_fail, cb_neterr) {
  72. var idx = 0;
  73. var param_name = [], param_val = [];
  74. var req = new XMLHttpRequest();
  75. // Firefox gets testy about parsing assumed XML
  76. //
  77. req.overrideMimeType("text/plain");
  78. var url = "/req";
  79. var varstr = "action=" + action;
  80. for (var x in param) {
  81. param_name.push(x);
  82. param_val.push(param[x]);
  83. }
  84. for (var ii=0; ii<param_name.length; ii++) {
  85. varstr += "&" + param_name[ii] + "=" + param_val[ii];
  86. }
  87. req.onreadystatechange = function() {
  88. if (req.readyState == 4) {
  89. if (req.status == 200) {
  90. var fulltxt = req.responseText;
  91. var resp = req.responseText.split(" ");
  92. if ((resp.length>=2) && (resp[0] == "ok")) {
  93. if (typeof cb_ok !== "undefined") {
  94. cb_ok(fulltxt);
  95. }
  96. return;
  97. }
  98. if (typeof cb_fail !== "undefined") {
  99. cb_fail(fulltxt);
  100. }
  101. }
  102. else if (typeof cb_fail !== "undefined") { cb_fail(); }
  103. }
  104. };
  105. req.addEventListener("error", _api_fail);
  106. req.addEventListener("abort", _api_fail);
  107. //req.addEventListener("load", _api_tick);
  108. //req.addEventListener("progress", _api_tick);
  109. req.onerror = function() { console.log("network error"); };
  110. try {
  111. //DEBUG
  112. console.log("req, varstr:", varstr);
  113. req.open("POST", url, true);
  114. req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  115. req.send(varstr);
  116. } catch (er) {
  117. console.log(">>>>>>ERROR", er);
  118. }
  119. }
  120. function _block_status_window(ui_id) {
  121. var ele = document.getElementById(ui_id + "_status");
  122. ele.ondragstart = function() { return false; }
  123. ele.onselectstart = function() { return false; }
  124. ele.onmousedown = function() { return false; }
  125. ele.ondragstart = function() { return false; }
  126. var rect = ele.getBoundingClientRect();
  127. var block_ele = document.getElementById(ui_id + "_blockwindow");
  128. block_ele.style.width = rect.width;
  129. block_ele.style.height = rect.height;
  130. block_ele.style.bottom = rect.bottom;
  131. block_ele.style.left = rect.left;
  132. block_ele.style.top = rect.top;
  133. block_ele.style.right = rect.right;
  134. block_ele.style.x = rect.x;
  135. block_ele.style.y = rect.y;
  136. }
  137. function _switch_ui(to) {
  138. var ele;
  139. console.log(">>>to", to);
  140. if (g_ctx.ui_change_timeoutid > 0) {
  141. window.clearTimeout(g_ctx.ui_change_timeoutid);
  142. g_ctx.ui_change_timeoutid = -1;
  143. }
  144. var from = g_ctx.current_ui;
  145. ele = document.getElementById(from);
  146. ele.style.display = "none";
  147. ele = document.getElementById(to);
  148. ele.style.display = "block";
  149. g_ctx.current_ui = to;
  150. if ((to == "ui_main") ||
  151. (to == "ui_login")) {
  152. _block_status_window(to);
  153. }
  154. }
  155. function _hide_ui(from) {
  156. var ele = document.getElementById(from);
  157. ele.style.display = "none";
  158. }
  159. function _hide_ui_element(from) {
  160. var ele = document.getElementById(from);
  161. ele.style.display = "none";
  162. }
  163. function _show_ui(to) {
  164. var ele = document.getElementById(to);
  165. ele.style.display = "block";
  166. g_ctx.current_ui = to;
  167. // put pane over status window to prevent highlights,
  168. // copy/pasting, etc.
  169. //
  170. if ((to == "ui_main") ||
  171. (to == "ui_login")) {
  172. _block_status_window(to);
  173. }
  174. }
  175. function _show_ui_element(to) {
  176. var ele = document.getElementById(to);
  177. ele.style.display = "block";
  178. }
  179. //------------
  180. // volume is from [0,1]
  181. //
  182. function _process_volume(vol) {
  183. var ele = document.getElementById("ui_configuration_displayvolume");
  184. ivol = Math.floor(vol*100.0);
  185. ele.innerHTML = "" + ivol + "%";
  186. }
  187. function _slider_change(name) {
  188. if (name == "configuration.volume") {
  189. var val = document.getElementById("ui_configuration_volume").value;
  190. _process_volume(val);
  191. ivol = Math.floor(val*100.0);
  192. _api_request("volume", {"volume":ivol},
  193. function(inp) { console.log("volume api set ok:", inp); },
  194. function(inp) { console.log("volume api set fail:", inp); });
  195. }
  196. }
  197. function _volume_test() {
  198. _api_request("say", {"message":"testing 1, 2, 3"},
  199. function(inp) { console.log("volume test ok:", inp); },
  200. function(inp) { console.log("volume test fail:", inp); });
  201. }
  202. //------------
  203. function _clear_ui_driver() {
  204. var ele;
  205. var u = ["driver", "pin"];
  206. for (var i=0; i<u.length; i++) {
  207. g_ctx[u[i]] = "";
  208. ele = document.getElementById("ui_driver_" + u[i]);
  209. ele.innerHTML = "";
  210. ele.style.background = "";
  211. }
  212. ele = document.getElementById("ui_driver_pin");
  213. ele.style.display = "none";
  214. ele = document.getElementById("ui_driver_pin_text");
  215. ele.style.display = "none";
  216. }
  217. function _clear_ui_paddle() {
  218. g_ctx.paddle = "";
  219. var ele = document.getElementById("ui_paddle_paddle");
  220. ele.innerHTML = "";
  221. }
  222. function _clear_ui_bulkaccept() {
  223. var ele;
  224. g_ctx.bulkfare = "";
  225. ele = document.getElementById("ui_bulkaccept_faretype");
  226. ele.innerHTML = "";
  227. g_ctx.bulkfare_count = "";
  228. ele = document.getElementById("ui_bulkaccept_count");
  229. ele.innerHTML = "";
  230. }
  231. function _login() {
  232. _switch_ui('ui_driver');
  233. g_ctx.driver_login_state = "driver";
  234. _clear_ui_driver();
  235. _clear_ui_paddle();
  236. }
  237. //------------
  238. function _verify_driver() {
  239. var driver = g_ctx.driver;
  240. var pin = g_ctx.pin;
  241. _api_request("driverlogin", {"driver":driver, "pin":pin}, _verify_driver_ok, _verify_driver_fail);
  242. return;
  243. }
  244. function _verify_driver_ok() { _switch_ui("ui_paddle"); }
  245. function _verify_driver_fail() { _switch_ui("ui_driverincorrect"); }
  246. function _driver_incorrect(inp) {
  247. _clear_ui_driver();
  248. _switch_ui("ui_driver");
  249. }
  250. //------------
  251. function _driver_login(inp) {
  252. var uvar = "";
  253. if (g_ctx.driver_login_state == "driver") { uvar = "driver"; }
  254. else if (g_ctx.driver_login_state == "pin") { uvar = "pin"; }
  255. else {
  256. console.log("_driver_login: ERROR: unknown driver state \"" + g_ctx.driver_login_state + "\"");
  257. return;
  258. }
  259. var n = g_ctx[uvar].length;
  260. //if (inp == 'cancel') { _switch_ui('ui_driver', 'ui_login'); }
  261. if (inp == 'cancel') { _switch_ui('ui_login'); }
  262. else if (inp == 'ok') {
  263. if (uvar == "driver") {
  264. g_ctx.driver_login_state = "pin";
  265. var ele;
  266. ele = document.getElementById("ui_driver_driver");
  267. ele.style.background = "#aaaaaa";
  268. ele = document.getElementById("ui_driver_pin");
  269. ele.style.display = "block";
  270. ele = document.getElementById("ui_driver_pin_text");
  271. ele.style.display = "block";
  272. }
  273. else if (uvar == "pin") {
  274. g_ctx.driver_login_state = "driver";
  275. _verify_driver();
  276. g_ctx.ui_change_timeoutid = window.setTimeout(function() {
  277. _switch_ui("ui_driververify");
  278. }, 150);
  279. return;
  280. }
  281. }
  282. // manage adding/subtracting/clearing relevant field
  283. //
  284. else if (inp == 'bksp') { g_ctx[uvar] = g_ctx[uvar].substr(0, n-1); }
  285. else if (inp == 'clr') { g_ctx[uvar]= ""; }
  286. else { g_ctx[uvar] += inp; }
  287. // Pin should not be displayed on screen.
  288. // Use '*' to hide it.
  289. //
  290. if (uvar == "pin") {
  291. var n = g_ctx[uvar].length;
  292. var x = "";
  293. for (i=0; i<n; i++) { x += "*"; }
  294. var ele = document.getElementById('ui_driver_' + uvar);
  295. ele.innerHTML = x;
  296. }
  297. // Otherwise update the displayed (driver number)
  298. //
  299. else {
  300. var ele = document.getElementById('ui_driver_' + uvar);
  301. ele.innerHTML = g_ctx[uvar];
  302. }
  303. }
  304. //------------
  305. //------------
  306. function _verify_paddle() {
  307. var paddle = g_ctx.paddle;
  308. _api_request("paddleinput", {"paddle":paddle}, _verify_paddle_ack, _verify_paddle_fail);
  309. return;
  310. }
  311. function _verify_paddle_ack() { console.log("got ack for push paddle message\n"); }
  312. function _verify_paddle_ok() { _switch_ui("ui_main"); }
  313. function _verify_paddle_fail() { _switch_ui("ui_paddleunknown"); }
  314. function _paddle_unknown(inp) {
  315. _clear_ui_paddle();
  316. _switch_ui("ui_paddle");
  317. }
  318. //------------
  319. function _paddle_input(inp) {
  320. var n = g_ctx.paddle.length;
  321. if (inp == 'cancel') { _switch_ui('ui_login'); }
  322. else if (inp == 'ok') {
  323. _verify_paddle();
  324. g_ctx.ui_change_timeoutid = window.setTimeout(function() {
  325. _switch_ui("ui_paddleverify");
  326. }, 150);
  327. return;
  328. }
  329. else if (inp == 'bksp') { g_ctx.paddle = g_ctx.paddle.substr(0, n-1); }
  330. else if (inp == 'clr') { g_ctx.paddle = ""; }
  331. else { g_ctx.paddle += inp; }
  332. // Otherwise update the displayed (driver number)
  333. //
  334. var ele = document.getElementById('ui_paddle_paddle');
  335. ele.innerHTML = g_ctx.paddle;
  336. }
  337. //-------------
  338. //-------------
  339. //-------------
  340. function _main_prv() {
  341. console.log("prv");
  342. _api_request("prevstop");
  343. }
  344. function _main_nxt() {
  345. console.log("nxt");
  346. _api_request("nextstop");
  347. }
  348. function _main_dim() {
  349. console.log("dim...");
  350. _api_request("dim");
  351. _switch_ui('ui_dim');
  352. }
  353. function _dim_wakeup() {
  354. console.log("waking up from dim");
  355. _switch_ui("ui_main");
  356. }
  357. function _fare_adult() {
  358. console.log("fare adult");
  359. _api_request("fare", {"rule":"CASH-ADULT", "param":"", "fare":"adult","count":1});
  360. }
  361. function _fare_youth() {
  362. console.log("fare youth");
  363. _api_request("fare", {"rule":"CASH-YOUTH", "param":"", "fare":"youth","count":1});
  364. }
  365. function _fare_half() {
  366. console.log("fare half");
  367. _api_request("fare", {"rule":"CASH-HALF", "param":"", "fare":"half","count":1});
  368. }
  369. function _fare_bike() {
  370. console.log("fare bike");
  371. _api_request("fare", {"rule":"MISC-BIKE", "param":"", "fare":"bike","count":1});
  372. }
  373. function _fare_wheelchair() {
  374. console.log("fare wheelchair");
  375. _api_request("fare", {"rule":"MISC-WHEELCHAIR", "param":"", "fare":"wheelchair","count":1});
  376. }
  377. function _fare_transfer() {
  378. console.log("fare transfer");
  379. _api_request("fare", {"rule":"MISC-TRANSFER", "param":"", "fare":"transfer","count":1});
  380. }
  381. //-------------
  382. //-------------
  383. //-------------
  384. function _main_bulk() {
  385. // reset tab to Misc. (first option) by default
  386. //
  387. ele = document.getElementById("ui_bulk_misc");
  388. ele.classList.remove("highlight");
  389. ele.classList.add("highlight");
  390. ele = document.getElementById("ui_bulk_passes");
  391. ele.classList.remove("highlight");
  392. _show_ui_element("ui_bulk_misc_tab");
  393. _hide_ui_element("ui_bulk_passes_tab");
  394. _switch_ui("ui_bulk");
  395. }
  396. function _main_menu() {
  397. // reset tab to Misc. (first option) by default
  398. //
  399. ele = document.getElementById("ui_fareoverride_misc");
  400. ele.classList.remove("highlight");
  401. ele.classList.add("highlight");
  402. ele = document.getElementById("ui_fareoverride_passes");
  403. ele.classList.remove("highlight");
  404. _show_ui_element("ui_fareoverride_misc_tab");
  405. _hide_ui_element("ui_fareoverride_passes_tab");
  406. _switch_ui("ui_fareoverride");
  407. }
  408. //-------------
  409. //-------------
  410. //-------------
  411. function _bulkaccept_ok() {
  412. console.log("bulkfare:", g_ctx.bulkfare, ", count:", g_ctx.bulkfare_count);
  413. _switch_ui("ui_main");
  414. _api_request("fare",{"rule":g_ctx.bulkfare, "param":g_ctx.bulkfare_count.toString(), "fare":g_ctx.bulkfare,"count":g_ctx.bulkfare_count});
  415. }
  416. function _bulkaccept_cancel() {
  417. _switch_ui("ui_main");
  418. }
  419. function _bulkaccept_input(inp) {
  420. var n = g_ctx.bulkfare_count.length;
  421. if (inp == 'cancel') { _switch_ui('ui_main'); }
  422. else if (inp == 'ok') { _bulkaccept_ok(); return; }
  423. else if (inp == 'bksp') { g_ctx.bulkfare_count = g_ctx.bulkfare_count.substr(0, n-1); }
  424. else if (inp == 'clr') { g_ctx.bulkfare_count = ""; }
  425. else { g_ctx.bulkfare_count += inp; }
  426. // Otherwise update the displayed (driver number)
  427. //
  428. var ele = document.getElementById('ui_bulkaccept_count');
  429. ele.innerHTML = g_ctx.bulkfare_count;
  430. }
  431. //-------------
  432. //-------------
  433. //-------------
  434. // space pad
  435. // default 10
  436. //
  437. function _sp(str, align, pad) {
  438. align = ((typeof align=="undefined") ? "left" : align);
  439. pad = ((typeof pad==="undefined") ? 10 : pad);
  440. var n = 0;
  441. n = pad - str.length;
  442. if (align == "left") {
  443. for (var i=0; i<n; i++) { str += " "; }
  444. }
  445. else if (align == "center") {
  446. var rem = n%2;
  447. var n2 = Math.floor(n/2);
  448. var pfx = "", sfx = "";
  449. for (var i=0; i<n2; i++) { pfx += " "; }
  450. for (var i=0; i<(n2+rem); i++) { sfx += " "; }
  451. str = pfx + str + sfx;
  452. }
  453. else if (align == "right") {
  454. var pfx = "";
  455. for (var i=0; i<n; i++) { pfx += " "; }
  456. str = prx + str;
  457. }
  458. return str;
  459. }
  460. function _cb_status(inp) {
  461. var tok = inp.split(" ");
  462. if (tok.length < 2) { return; }
  463. if (tok[0]!="ok") { return; }
  464. valtok = tok.slice(1).join(" ").split("\n");
  465. if (valtok[0]!="msg=status") { return; }
  466. g_ctx.diu_status = {};
  467. var fields = valtok[1].split("|");
  468. for (var ii=0; ii<fields.length; ii++) {
  469. var kv = fields[ii].split("=");
  470. if (kv.length!=2) { continue; }
  471. g_ctx.diu_status[kv[0].trim()] = kv[1].trim();
  472. }
  473. var m = g_ctx.diu_status;
  474. if (g_ctx.current_ui === "ui_login") {
  475. var has_tunn = ((m["tunnel"] == '1') ? 'YES' : 'NO');
  476. var has_gprs = ((m["gprs"] == '1') ? 'YES' : 'NO');
  477. var has_gps = ((m["gps"] == '1') ? 'YES' : 'NO');
  478. var lat = ((typeof m["gpslat"] !== "undefined") ? m["gpslat"] : "");
  479. var lon = ((typeof m["gpslon"] !== "undefined") ? m["gpslon"] : "");
  480. var gps_str = ( ((lat.length>0) && (lon.length>0)) ? (" (" + lat + "/" + lon + ")") : "" );
  481. var ele = document.getElementById("ui_login_status");
  482. var html_a = [];
  483. html_a.push("Tunnel: " + has_tunn + " GPRS: " + has_gprs + " Eq#: " + m["equipno"] + " #Msg: " + m["nmsg"]);
  484. html_a.push("Last Token Read (" + m["last_token"] + ")");
  485. html_a.push("GPS: " + has_gps + gps_str + "");
  486. html_a.push(" ");
  487. html_a.push("Package Version Installed");
  488. html_a.push(_sp("ui") + _sp(DIU_UI_VERSION) + _sp(DIU_UI_VERSION_DATE) + "");
  489. html_a.push(" ");
  490. if ("IMEI" in m) { html_a.push("IMEI = " + m["IMEI"] + ""); }
  491. if ("IMSI" in m) { html_a.push("IMSI = " + m["IMSI"] + ""); }
  492. if ("ETH0" in m) { html_a.push("ETH0 = " + m["ETH0"] + ""); }
  493. html_a.push(" ");
  494. for (var ii=0; ii<html_a.length; ii++) {
  495. html_a[ii] = html_a[ii].replace(/ /g, '&nbsp;');
  496. }
  497. //span_str = "<div style='float:left; width: 100%; margin: 0 auto; display:block; text-align:left; background: #770000; color:#008800;'>";
  498. //span_str = "<div style='float:left; width: 100%; text-align:left; background: #770000; color:#008800;'>";
  499. //var div_hdr = "<div style='float:left; width: 100%; text-align:left; '>";
  500. var div_hdr = "<div class='textrow'>";
  501. var str = div_hdr + html_a.join("</div><br>" + div_hdr ) + "</div>";
  502. ele.innerHTML = str;
  503. }
  504. else if (g_ctx.current_ui === "ui_main") {
  505. var ele = document.getElementById("mainstatus");
  506. var str = "";
  507. str += "Rt " + m["route"] + " ";
  508. str += "Trip " + m["trip"] + " ";
  509. str += "Stop " + m["stop"] + " ";
  510. str += "GPS " + m["gps"] + " ";
  511. str += "Tun " + m["tunnel"] + ", ";
  512. str += m["date"];
  513. str += " " + moment().format("HH:mm");
  514. ele.innerHTML = str;
  515. }
  516. }
  517. function _cb_status_err(e) {
  518. console.log("ERROR: _cb_status_err:", e);
  519. }
  520. //-------------
  521. function _main_message_clear() {
  522. g_ctx.status_text = "";
  523. }
  524. function _main_message_replace(msg, bg_color, text_color) {
  525. var ele = document.getElementById("ui_main_status");
  526. bg_color = ((typeof bg_color === "undefined") ? BG_COLOR : bg_color);
  527. text_color = ((typeof text_color === "undefined") ? TEXT_COLOR : text_color);
  528. var lines = g_ctx.status_text.split("\n");
  529. if (lines.length > g_ctx.status_text_h) {
  530. var n = lines.length;
  531. lines = lines.slice(n - g_ctx.status_text_h);
  532. }
  533. for (var i=0; i<lines.length; i++) {
  534. lines[i] = lines[i].slice(0,g_ctx.status_text_w);
  535. }
  536. var idx = lines.length-1;
  537. if (idx<0) { idx = 0; }
  538. lines[idx] = msg;
  539. var html_lines = [];
  540. for (var i=0; i<lines.length; i++) {
  541. html_lines.push(lines[i].slice(0,g_ctx.status_text_w));
  542. if (html_lines[i].match(/ACCEPT/)) {
  543. html_lines[i] = "<div class='textrow accept'>" + html_lines[i] + "</div>";
  544. }
  545. else if (html_lines[i].match(/REJECT/)) {
  546. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  547. }
  548. else if (html_lines[i].match(/[Uu]nknown/)) {
  549. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  550. }
  551. else if (html_lines[i].match(/[Pp]assback/)) {
  552. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  553. }
  554. else if (html_lines[i].match(/Rule execution error/)) {
  555. html_lines[i] = "<div class='textrow error'>" + html_lines[i] + "</div>";
  556. }
  557. else {
  558. html_lines[i] = "<div class='textrow'>" + html_lines[i] + "</div>";
  559. }
  560. }
  561. g_ctx.status_text = lines.join("\n");
  562. g_ctx.status_text = lines.join("\n");
  563. g_ctx.status_html_text = html_lines.join("<br>");
  564. ele.innerHTML = g_ctx.status_html_text;
  565. // sounds
  566. //
  567. if (msg.match(/ACCEPT/)) {
  568. _beep_accept();
  569. }
  570. else if (msg.match(/REJECT/)) {
  571. _beep_reject();
  572. }
  573. else if (msg.match(/[Uu]nknown/)) {
  574. _beep_reject();
  575. }
  576. else if (msg.match(/[Pp]assback/)) {
  577. _beep_reject();
  578. }
  579. else if (msg.match(/Rule execution error/)) {
  580. _beep_error();
  581. }
  582. }
  583. function _main_message_add(msg, bg_color, text_color) {
  584. var ele = document.getElementById("ui_main_status");
  585. bg_color = ((typeof bg_color === "undefined") ? BG_COLOR : bg_color);
  586. text_color = ((typeof text_color === "undefined") ? TEXT_COLOR : text_color);
  587. var txt = g_ctx.status_text + "\n" + msg;
  588. var html_lines = [];
  589. var lines = txt.split("\n");
  590. if (lines.length > g_ctx.status_text_h) {
  591. var n = lines.length;
  592. lines = lines.slice(n - g_ctx.status_text_h);
  593. }
  594. for (var i=0; i<lines.length; i++) {
  595. html_lines.push(lines[i].slice(0,g_ctx.status_text_w));
  596. if (html_lines[i].match(/ACCEPT/)) {
  597. html_lines[i] = "<div class='textrow accept'>" + html_lines[i] + "</div>";
  598. }
  599. else if (html_lines[i].match(/REJECT/)) {
  600. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  601. }
  602. else if (html_lines[i].match(/[Uu]nknown/)) {
  603. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  604. }
  605. else if (html_lines[i].match(/[Pp]assback/)) {
  606. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  607. }
  608. else if (html_lines[i].match(/Rule execution error/)) {
  609. html_lines[i] = "<div class='textrow error'>" + html_lines[i] + "</div>";
  610. }
  611. else {
  612. html_lines[i] = "<div class='textrow'>" + html_lines[i] + "</div>";
  613. }
  614. }
  615. g_ctx.status_text = lines.join("\n");
  616. g_ctx.status_html_text = html_lines.join("<br>");
  617. ele.innerHTML = g_ctx.status_html_text;
  618. // sounds
  619. //
  620. if (msg.match(/ACCEPT/)) {
  621. _beep_accept();
  622. }
  623. else if (msg.match(/REJECT/)) {
  624. _beep_reject();
  625. }
  626. else if (msg.match(/[Uu]nknown/)) {
  627. _beep_reject();
  628. }
  629. else if (msg.match(/[Pp]assback/)) {
  630. _beep_reject();
  631. }
  632. else if (msg.match(/Rule execution error/)) {
  633. _beep_error();
  634. }
  635. }
  636. //-------------
  637. //-------------
  638. //-------------
  639. function _ws_process(dat) {
  640. var tok = dat.split(" ");
  641. if (tok.length < 2) { return; }
  642. if (tok[0] == "paddle") {
  643. if (tok[1] == "ok") { _verify_paddle_ok(); }
  644. else if (tok[1] == "fail") { _verify_paddle_fail(); }
  645. else { _verify_paddle_fail(); }
  646. }
  647. else if (tok[0] == "driver_notify") {
  648. if (tok.length < 5) {
  649. console.log("malformed message: ", dat);
  650. }
  651. else if (tok[1] == "replace") {
  652. _main_message_replace(tok.slice(4).join(" "));
  653. }
  654. else if (tok[1] == "ok") {
  655. _main_message_add(tok.slice(4).join(" "));
  656. }
  657. else {
  658. console.log("unknown message: ", dat);
  659. }
  660. }
  661. }
  662. function _init_websocket() {
  663. var sock = new WebSocket("ws://" + _fqADDRESS);
  664. sock.onopen = function(e) { console.log("open", e); }
  665. sock.onmessage = function(e) { _ws_process(e.data); }
  666. sock.onclose = function(e) { console.log("close", e); }
  667. sock.onerror = function(e) { console.log("error", e); }
  668. }
  669. //-------------
  670. var ui_pattern = {
  671. "state" : "idle",
  672. "lastX" : 0,
  673. "lastY" : 0,
  674. "curX" : 0,
  675. "curY" : 0
  676. };
  677. function ui_pattern_mousedown(ev) {
  678. var ele = document.getElementById("body");
  679. var rect = ele.getBoundingClientRect();
  680. var x = ev["x"];
  681. var y = ev["y"];
  682. var width = rect.width;
  683. var height = rect.height;
  684. if (ui_pattern.state == "idle") {
  685. if (y < (height/4)) {
  686. ui_pattern.state = "square_0";
  687. }
  688. else {
  689. ui_pattern.state = "idle"
  690. }
  691. }
  692. else if (ui_pattern.state == "square_0") {
  693. if (x > (3*width/4)) {
  694. ui_pattern.state = "square_1";
  695. }
  696. else {
  697. ui_pattern.state = "idle"
  698. }
  699. }
  700. else if (ui_pattern.state == "square_1") {
  701. if (y > (3*height/4)) {
  702. ui_pattern.state = "square_2";
  703. }
  704. else {
  705. ui_pattern.state = "idle"
  706. }
  707. }
  708. else if (ui_pattern.state == "square_2") {
  709. if (x < (width/4)) {
  710. ui_pattern.state = "square_3";
  711. }
  712. else {
  713. ui_pattern.state = "idle"
  714. }
  715. }
  716. else if (ui_pattern.state == "square_3") {
  717. if (y < (height/4)) {
  718. ui_pattern.state = "square_4";
  719. }
  720. else {
  721. ui_pattern.state = "idle"
  722. }
  723. }
  724. else if (ui_pattern.state == "square_4") {
  725. if (x > (3*width/4)) {
  726. ui_pattern.state = "square_5";
  727. }
  728. else {
  729. ui_pattern.state = "idle"
  730. }
  731. }
  732. else if (ui_pattern.state == "square_5") {
  733. if (y > (3*height/4)) {
  734. ui_pattern.state = "square_6";
  735. }
  736. else {
  737. ui_pattern.state = "idle"
  738. }
  739. }
  740. else if (ui_pattern.state == "square_6") {
  741. if (x < (width/4)) {
  742. _switch_ui("ui_diagnostic");
  743. }
  744. ui_pattern.state = "idle"
  745. }
  746. return false;
  747. }
  748. function ui_pattern_mouseup(ev) {
  749. return false;
  750. }
  751. //------------
  752. function _mainscreen() {
  753. _switch_ui('ui_login');
  754. g_ctx.driver_login_state = "driver";
  755. _clear_ui_driver();
  756. _clear_ui_paddle();
  757. }
  758. function _configuration() {
  759. _switch_ui('ui_configuration');
  760. g_ctx.driver_login_state = "driver";
  761. _clear_ui_driver();
  762. _clear_ui_paddle();
  763. }
  764. function _reset() {
  765. _switch_ui('ui_login');
  766. g_ctx.driver_login_state = "driver";
  767. _clear_ui_driver();
  768. _clear_ui_paddle();
  769. location.reload(true);
  770. }
  771. function _reboot() {
  772. _switch_ui('ui_login');
  773. g_ctx.driver_login_state = "driver";
  774. _clear_ui_driver();
  775. _clear_ui_paddle();
  776. }
  777. function _custom0() {
  778. _api_request("interfaceupdown", {"interface":"eth0", "state":"up"}, function(inp) { console.log("custom0 ok:", inp); }, function(inp) { console.log("custom0 error:", inp); });
  779. }
  780. function _custom1() {
  781. _api_request("interfaceupdown", {"interface":"eth0", "state":"down"}, function(inp) { console.log("custom1 ok:", inp); }, function(inp) { console.log("custom1 error:", inp); });
  782. }
  783. function _update_diagnostic_window(_msg) {
  784. var ele = document.getElementById("ui_diagnostic_view");
  785. ele.innerHTML = _msg;
  786. }
  787. function _custom2() {
  788. _api_request("interfaceinfo", {"interface":"eth0"}, _update_diagnostic_window, function(inp) { console.log("custom2 error:", inp); });
  789. }
  790. function _custom3() {
  791. _api_request("say", {"message":"speech test"}, function(inp) { console.log("custom3 ok:", inp); }, function(inp) { console.log("custom3 error:", inp); });
  792. }
  793. function _custom4() {
  794. //_switch_ui('ui_login');
  795. //g_ctx.driver_login_state = "driver";
  796. //_clear_ui_driver();
  797. //_clear_ui_paddle();
  798. }
  799. function _custom5() {
  800. //_switch_ui('ui_login');
  801. //g_ctx.driver_login_state = "driver";
  802. //_clear_ui_driver();
  803. //_clear_ui_paddle();
  804. }
  805. //------------
  806. //-------------
  807. function init() {
  808. _init_websocket();
  809. // diagnostics
  810. //
  811. document.getElementById('ui_diagnostic_mainscreen').onclick = _mainscreen;
  812. document.getElementById('ui_diagnostic_reset').onclick = _reset;
  813. document.getElementById('ui_diagnostic_reboot').onclick = _reboot;
  814. document.getElementById('ui_diagnostic_custom0').onclick = _custom0;
  815. document.getElementById('ui_diagnostic_custom1').onclick = _custom1;
  816. document.getElementById('ui_diagnostic_custom2').onclick = _custom2;
  817. document.getElementById('ui_diagnostic_custom3').onclick = _custom3;
  818. document.getElementById('ui_diagnostic_custom4').onclick = _custom4;
  819. document.getElementById('ui_diagnostic_custom5').onclick = _custom5;
  820. // configuration
  821. //
  822. document.getElementById('ui_configuration_back').onclick = _mainscreen;
  823. document.getElementById('ui_configuration_volumetest').onclick = _volume_test;
  824. // login
  825. //
  826. document.getElementById('ui_login_login').onclick = _login;
  827. document.getElementById('ui_login_configuration').onclick = _configuration;
  828. document.getElementById('ui_driverincorrect_btn').onclick = _driver_incorrect;
  829. document.getElementById('ui_paddleunknown_btn').onclick = _paddle_unknown;;
  830. // main menu buttons
  831. //
  832. document.getElementById('ui_main_prv').onclick = _main_prv;
  833. document.getElementById('ui_main_nxt').onclick = _main_nxt;
  834. document.getElementById('ui_main_bulk').onclick = _main_bulk;
  835. document.getElementById('ui_main_menu').onclick = _main_menu;
  836. document.getElementById('ui_main_dim').onclick = _main_dim;
  837. // dim
  838. //
  839. document.getElementById('ui_dim_btn').onclick = _dim_wakeup;
  840. var simple_fares = [ "adult", "youth", "half", "bike", "wheelchair", "transfer"];
  841. var simple_fares_f = [ _fare_adult, _fare_youth, _fare_half, _fare_bike, _fare_wheelchair, _fare_transfer];
  842. for (var ii=0; ii<simple_fares.length; ii++) {
  843. var fare = simple_fares[ii];
  844. document.getElementById('ui_main_' + fare).onclick = simple_fares_f[ii];
  845. }
  846. // bulk menu buttons
  847. //
  848. document.getElementById('ui_bulk_passes').onclick =
  849. function() {
  850. var ele;
  851. ele = document.getElementById("ui_bulk_misc");
  852. ele.classList.remove("highlight");
  853. ele = document.getElementById("ui_bulk_passes");
  854. ele.classList.add("highlight");
  855. _hide_ui_element("ui_bulk_misc_tab");
  856. _show_ui_element("ui_bulk_passes_tab");
  857. };
  858. document.getElementById('ui_bulk_misc').onclick =
  859. function() {
  860. var ele;
  861. ele = document.getElementById("ui_bulk_misc");
  862. ele.classList.add("highlight");
  863. ele = document.getElementById("ui_bulk_passes");
  864. ele.classList.remove("highlight");
  865. _show_ui_element("ui_bulk_misc_tab");
  866. _hide_ui_element("ui_bulk_passes_tab");
  867. };
  868. document.getElementById('ui_bulk_menu').onclick = function() { _switch_ui("ui_main"); }
  869. // Setup "Bulk" UI accept buttons to do proper screen switching and
  870. // other tasks.
  871. //
  872. var bulk_pass = ["adult", "youth", "half", "event", "transfer", "interline",
  873. "schoola", "schoolb", "schoolc", "orga", "orgb", "orgc"];
  874. var bulk_rule = ["BULK-CASH-ADULT", "BULK-CASH-YOUTH", "BULK-CASH-HALF",
  875. "BULK-MISC-EVENT", "BULK-MISC-TRANSFER", "BULK-MISC-INTERLINE",
  876. "BULK-OVR-SCHOOLA", "BULK-OVR-SCHOOLB", "BULK-OVR-SCHOOLC",
  877. "BULK-OVR-ORGA", "BULK-OVR-ORGB", "BULK-OVR-ORGC"];
  878. var bulk_pass_name = ["Adult Cash", "Youth Cash", "Half Cash", "Special Event", "Transfer", "Interline",
  879. "School A", "School B", "School C", "Org A", "Org B", "Org C"];
  880. // use site specific values if available, otherwise default to the above
  881. //
  882. if ((typeof BULK_PASS !== "undefined") && (BULK_PASS.length > 0)) {
  883. bulk_pass = BULK_PASS;
  884. }
  885. if ((typeof BULK_RULE !== "undefined") && (BULK_RULE.length > 0)) {
  886. bulk_rule= BULK_RULE;
  887. }
  888. if ((typeof BULK_PASS_NAME !== "undefined") && (BULK_PASS_NAME.length > 0)) {
  889. bulk_pass_name = BULK_PASS_NAME;
  890. }
  891. for (var i=0; i<bulk_pass.length; i++) {
  892. var p = bulk_pass[i];
  893. var u = bulk_pass_name[i];
  894. var r = bulk_rule[i];
  895. var ele = document.getElementById('ui_bulk_' + p);
  896. ele.onclick = (function(x,y) {
  897. return function() {
  898. _clear_ui_bulkaccept();
  899. g_ctx.bulkfare = x;
  900. _switch_ui("ui_bulkaccept");
  901. var ee = document.getElementById("ui_bulkaccept_faretype");
  902. ee.innerHTML = y;
  903. };
  904. })(r, u);
  905. }
  906. // bulk accept fares
  907. //
  908. document.getElementById('ui_bulkaccept_ok').onclick = _bulkaccept_ok;
  909. document.getElementById('ui_bulkaccept_cancel').onclick = _bulkaccept_cancel;
  910. // fareoverride menu buttons
  911. //
  912. document.getElementById('ui_fareoverride_passes').onclick =
  913. function() {
  914. var ele;
  915. ele = document.getElementById("ui_fareoverride_misc");
  916. ele.classList.remove("highlight");
  917. ele = document.getElementById("ui_fareoverride_passes");
  918. ele.classList.add("highlight");
  919. _hide_ui_element("ui_fareoverride_misc_tab");
  920. _show_ui_element("ui_fareoverride_passes_tab");
  921. };
  922. document.getElementById('ui_fareoverride_misc').onclick =
  923. function() {
  924. var ele;
  925. ele = document.getElementById("ui_fareoverride_misc");
  926. ele.classList.add("highlight");
  927. ele = document.getElementById("ui_fareoverride_passes");
  928. ele.classList.remove("highlight");
  929. _show_ui_element("ui_fareoverride_misc_tab");
  930. _hide_ui_element("ui_fareoverride_passes_tab");
  931. };
  932. document.getElementById('ui_fareoverride_menu').onclick = function() { _switch_ui("ui_main"); }
  933. document.getElementById('ui_fareoverride_logout').onclick =
  934. function() {
  935. _main_message_clear();
  936. _api_request("logout");
  937. _switch_ui("ui_login");
  938. }
  939. // default values for override passes
  940. //
  941. var override_pass = [ "adult", "youth", "half",
  942. "bike", "wheelchair", "transfer",
  943. "event", "interline",
  944. "schoola", "schoolb", "schoolc",
  945. "orga", "orgb", "orgc" ];
  946. var override_rule = [ "CASH-ADULT", "CASH-YOUTH", "CASH-HALF",
  947. "MISC-BIKE", "MISC-WHEELCHAIR", "MISC-TRANSFER",
  948. "MISC-EVENT", "MISC-INTERLINE",
  949. "OVR-SCHOOLA", "OVR-SCHOOLB", "OVR-SCHOOLC",
  950. "OVR-ORGA", "OVR-ORGB", "OVR-ORGC" ];
  951. // use site specific values if available, otherwise default to the above
  952. //
  953. if ((typeof OVERRIDE_PASS !== "undefined") && (OVERRIDE_PASS.length > 0)) {
  954. override_pass = OVERRIDE_PASS;
  955. }
  956. if ((typeof OVERRIDE_RULE !== "undefined") && (OVERRIDE_RULE.length > 0)) {
  957. override_rule= OVERRIDE_RULE;
  958. }
  959. for (var i=0; i<override_pass.length; i++) {
  960. var p = override_pass[i];
  961. var r = override_rule[i];
  962. var ele = document.getElementById("ui_fareoverride_" + p);
  963. ele.onclick = (function(x,y) {
  964. return function() { _api_request("fare", {"rule":x,"fare":y,"param":"","count":1}); };
  965. })(r,p);
  966. }
  967. var ui_page = [ "driver", "paddle", "bulkaccept" ];
  968. var ui_func = [ _driver_login, _paddle_input, _bulkaccept_input ];
  969. for (var idx=0; idx<ui_page.length; idx++) {
  970. var c = ui_page[idx];
  971. var f = ui_func[idx];
  972. for (var i=0; i<=9; i++) {
  973. document.getElementById('ui_' + c + '_' + i).onclick =
  974. (function(_f, x) { return function() { _f(x); } })(f, i);
  975. }
  976. document.getElementById('ui_' + c + '_' + 'bksp').onclick =
  977. (function(_f,x) { return function() { _f(x); } })(f, 'bksp');
  978. document.getElementById('ui_' + c + '_' + 'clr').onclick =
  979. (function(_f,x) { return function() { _f(x); } })(f, 'clr');
  980. document.getElementById('ui_' + c + '_' + 'ok').onclick =
  981. (function(_f,x) { return function() { _f(x); } })(f, 'ok');
  982. document.getElementById('ui_' + c + '_' + 'cancel').onclick =
  983. (function(_f,x) { return function() { _f(x); } })(f, 'cancel');
  984. }
  985. // set interval to check status every 1 second
  986. //
  987. var x = window.setInterval(function() { _api_request("status", {}, _cb_status, _cb_status_err); }, 1000);
  988. // try and reduce textarea highlighting, drag and drop/copy pasting,
  989. // and other bad behavor.
  990. //
  991. var ele;
  992. var block_ele;
  993. var rect;
  994. ele = document.getElementById("ui_login_status");
  995. ele.ondragstart = function() { return false; }
  996. ele.onselectstart = function() { return false; }
  997. ele.onmousedown = function() { return false; }
  998. ele.ondragstart = function() { return false; }
  999. rect = ele.getBoundingClientRect();
  1000. block_ele = document.getElementById("ui_login_blockwindow");
  1001. block_ele.style.width = rect.width;
  1002. block_ele.style.height = rect.height;
  1003. block_ele.style.bottom = rect.bottom;
  1004. block_ele.style.left = rect.left;
  1005. block_ele.style.top = rect.top;
  1006. block_ele.style.right = rect.right;
  1007. block_ele.style.x = rect.x;
  1008. block_ele.style.y = rect.y;
  1009. ele = document.getElementById("ui_main_status");
  1010. ele.ondragstart = function() { return false; }
  1011. ele.onselectstart = function() { return false; }
  1012. ele.onmousedown = function() { return false; }
  1013. ele.ondragstart = function() { return false; }
  1014. rect = ele.getBoundingClientRect();
  1015. block_ele = document.getElementById("ui_main_blockwindow");
  1016. block_ele.style.width = rect.width;
  1017. block_ele.style.height = rect.height;
  1018. block_ele.style.bottom = rect.bottom;
  1019. block_ele.style.left = rect.left;
  1020. block_ele.style.top = rect.top;
  1021. block_ele.style.right = rect.right;
  1022. block_ele.style.x = rect.x;
  1023. block_ele.style.y = rect.y;
  1024. ele = document.getElementById("body");
  1025. // We're using jquery so we need to gracefully add callbacks
  1026. // for mouse events.
  1027. //
  1028. ele.addEventListener('mousedown', ui_pattern_mousedown);
  1029. ele.addEventListener('mouseup', ui_pattern_mouseup);
  1030. g_snd.button_press = new Audio("assets/459992_florianreichelt_beep_short.mp3");
  1031. g_snd.accept_fare = new Audio("assets/443026__qubodup__public-domain-beep-sound.mp3");
  1032. g_snd.reject_fare = new Audio("assets/368780__gurie__start-sound-beep.mp3");
  1033. g_snd.rule_error = new Audio("assets/363920__samsterbirdies__8-bit-error.mp3");
  1034. if (typeof PopufareSiteInit !== "undefined") {
  1035. PopufareSiteInit();
  1036. }
  1037. }
  1038. (function($) {
  1039. $(document).ready(function() {
  1040. init();
  1041. });
  1042. })(jQuery);