diu_ui-ORG.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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.7";
  27. var DIU_UI_VERSION_DATE = "2019-12-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. "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 = pfx + 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. // some site specific fiddling to display package information
  488. //
  489. html_a.push(_sp("Package") + _sp("Version", "left", 18) + _sp("Installed", "right", 12));
  490. html_a.push(_sp("ui") + _sp(DIU_UI_VERSION, "left", 18) + _sp(DIU_UI_VERSION_DATE, "right", 12) + "");
  491. for (var _ii=0; _ii<3; _ii++) {
  492. var key = "pkg" + _ii;
  493. if (key in g_ctx.diu_status) {
  494. var aval = g_ctx.diu_status[key].split(" ");
  495. html_a.push(_sp(aval[0]) + _sp(aval[1], "left", 18) + _sp(aval[2], "right", 12));
  496. }
  497. }
  498. html_a.push(" ");
  499. if ("IMEI" in m) { html_a.push("IMEI = " + m["IMEI"] + ""); }
  500. if ("IMSI" in m) { html_a.push("IMSI = " + m["IMSI"] + ""); }
  501. if ("ETH0" in m) { html_a.push("ETH0 = " + m["ETH0"] + ""); }
  502. html_a.push(" ");
  503. for (var ii=0; ii<html_a.length; ii++) {
  504. html_a[ii] = html_a[ii].replace(/ /g, '&nbsp;');
  505. }
  506. var div_hdr = "<div class='textrow'>";
  507. var str = div_hdr + html_a.join("</div><br>" + div_hdr ) + "</div>";
  508. ele.innerHTML = str;
  509. }
  510. else if (g_ctx.current_ui === "ui_main") {
  511. var ele = document.getElementById("mainstatus");
  512. var str = "";
  513. str += "Rt " + m["route"] + " ";
  514. str += "Trip " + m["trip"] + " ";
  515. str += "Stop " + m["stop"] + " ";
  516. str += "GPS " + m["gps"] + " ";
  517. str += "Tun " + m["tunnel"] + ", ";
  518. str += m["date"];
  519. str += " " + moment().format("HH:mm");
  520. ele.innerHTML = str;
  521. }
  522. }
  523. function _cb_status_err(e) {
  524. console.log("ERROR: _cb_status_err:", e);
  525. }
  526. //-------------
  527. function _main_message_clear() {
  528. g_ctx.status_text = "";
  529. }
  530. function _main_message_replace(msg, bg_color, text_color) {
  531. var ele = document.getElementById("ui_main_status");
  532. bg_color = ((typeof bg_color === "undefined") ? BG_COLOR : bg_color);
  533. text_color = ((typeof text_color === "undefined") ? TEXT_COLOR : text_color);
  534. var lines = g_ctx.status_text.split("\n");
  535. if (lines.length > g_ctx.status_text_h) {
  536. var n = lines.length;
  537. lines = lines.slice(n - g_ctx.status_text_h);
  538. }
  539. for (var i=0; i<lines.length; i++) {
  540. lines[i] = lines[i].slice(0,g_ctx.status_text_w);
  541. }
  542. var idx = lines.length-1;
  543. if (idx<0) { idx = 0; }
  544. lines[idx] = msg;
  545. var html_lines = [];
  546. for (var i=0; i<lines.length; i++) {
  547. html_lines.push(lines[i].slice(0,g_ctx.status_text_w));
  548. if (html_lines[i].match(/ACCEPT/)) {
  549. html_lines[i] = "<div class='textrow accept'>" + html_lines[i] + "</div>";
  550. }
  551. else if (html_lines[i].match(/REJECT/)) {
  552. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  553. }
  554. else if (html_lines[i].match(/[Uu]nknown/)) {
  555. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  556. }
  557. else if (html_lines[i].match(/[Pp]assback/)) {
  558. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  559. }
  560. else if (html_lines[i].match(/Rule execution error/)) {
  561. html_lines[i] = "<div class='textrow error'>" + html_lines[i] + "</div>";
  562. }
  563. else {
  564. html_lines[i] = "<div class='textrow'>" + html_lines[i] + "</div>";
  565. }
  566. }
  567. g_ctx.status_text = lines.join("\n");
  568. g_ctx.status_text = lines.join("\n");
  569. g_ctx.status_html_text = html_lines.join("<br>");
  570. ele.innerHTML = g_ctx.status_html_text;
  571. // sounds
  572. //
  573. if (msg.match(/ACCEPT/)) {
  574. _beep_accept();
  575. }
  576. else if (msg.match(/REJECT/)) {
  577. _beep_reject();
  578. }
  579. else if (msg.match(/[Uu]nknown/)) {
  580. _beep_reject();
  581. }
  582. else if (msg.match(/[Pp]assback/)) {
  583. _beep_reject();
  584. }
  585. else if (msg.match(/Rule execution error/)) {
  586. _beep_error();
  587. }
  588. }
  589. function _main_message_add(msg, bg_color, text_color) {
  590. var ele = document.getElementById("ui_main_status");
  591. bg_color = ((typeof bg_color === "undefined") ? BG_COLOR : bg_color);
  592. text_color = ((typeof text_color === "undefined") ? TEXT_COLOR : text_color);
  593. var txt = g_ctx.status_text + "\n" + msg;
  594. var html_lines = [];
  595. var lines = txt.split("\n");
  596. if (lines.length > g_ctx.status_text_h) {
  597. var n = lines.length;
  598. lines = lines.slice(n - g_ctx.status_text_h);
  599. }
  600. for (var i=0; i<lines.length; i++) {
  601. html_lines.push(lines[i].slice(0,g_ctx.status_text_w));
  602. if (html_lines[i].match(/ACCEPT/)) {
  603. html_lines[i] = "<div class='textrow accept'>" + html_lines[i] + "</div>";
  604. }
  605. else if (html_lines[i].match(/REJECT/)) {
  606. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  607. }
  608. else if (html_lines[i].match(/[Uu]nknown/)) {
  609. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  610. }
  611. else if (html_lines[i].match(/[Pp]assback/)) {
  612. html_lines[i] = "<div class='textrow reject'>" + html_lines[i] + "</div>";
  613. }
  614. else if (html_lines[i].match(/Rule execution error/)) {
  615. html_lines[i] = "<div class='textrow error'>" + html_lines[i] + "</div>";
  616. }
  617. else {
  618. html_lines[i] = "<div class='textrow'>" + html_lines[i] + "</div>";
  619. }
  620. }
  621. g_ctx.status_text = lines.join("\n");
  622. g_ctx.status_html_text = html_lines.join("<br>");
  623. ele.innerHTML = g_ctx.status_html_text;
  624. // sounds
  625. //
  626. if (msg.match(/ACCEPT/)) {
  627. _beep_accept();
  628. }
  629. else if (msg.match(/REJECT/)) {
  630. _beep_reject();
  631. }
  632. else if (msg.match(/[Uu]nknown/)) {
  633. _beep_reject();
  634. }
  635. else if (msg.match(/[Pp]assback/)) {
  636. _beep_reject();
  637. }
  638. else if (msg.match(/Rule execution error/)) {
  639. _beep_error();
  640. }
  641. }
  642. //-------------
  643. //-------------
  644. //-------------
  645. function _ws_process(dat) {
  646. var tok = dat.split(" ");
  647. if (tok.length < 2) { return; }
  648. if (tok[0] == "paddle") {
  649. if (tok[1] == "ok") { _verify_paddle_ok(); }
  650. else if (tok[1] == "fail") { _verify_paddle_fail(); }
  651. else { _verify_paddle_fail(); }
  652. }
  653. else if (tok[0] == "driver_notify") {
  654. if (tok.length < 5) {
  655. console.log("malformed message: ", dat);
  656. }
  657. else if (tok[1] == "replace") {
  658. _main_message_replace(tok.slice(4).join(" "));
  659. }
  660. else if (tok[1] == "ok") {
  661. _main_message_add(tok.slice(4).join(" "));
  662. }
  663. else {
  664. console.log("unknown message: ", dat);
  665. }
  666. }
  667. }
  668. function _init_websocket() {
  669. var sock = new WebSocket("ws://" + _fqADDRESS);
  670. sock.onopen = function(e) { console.log("open", e); }
  671. sock.onmessage = function(e) { _ws_process(e.data); }
  672. sock.onclose = function(e) { console.log("close", e); }
  673. sock.onerror = function(e) { console.log("error", e); }
  674. }
  675. //-------------
  676. var ui_pattern = {
  677. "state" : "idle",
  678. "lastX" : 0,
  679. "lastY" : 0,
  680. "curX" : 0,
  681. "curY" : 0
  682. };
  683. function ui_pattern_mousedown(ev) {
  684. var ele = document.getElementById("body");
  685. var rect = ele.getBoundingClientRect();
  686. var x = ev["x"];
  687. var y = ev["y"];
  688. var width = rect.width;
  689. var height = rect.height;
  690. if (ui_pattern.state == "idle") {
  691. if (y < (height/4)) {
  692. ui_pattern.state = "square_0";
  693. }
  694. else {
  695. ui_pattern.state = "idle"
  696. }
  697. }
  698. else if (ui_pattern.state == "square_0") {
  699. if (x > (3*width/4)) {
  700. ui_pattern.state = "square_1";
  701. }
  702. else {
  703. ui_pattern.state = "idle"
  704. }
  705. }
  706. else if (ui_pattern.state == "square_1") {
  707. if (y > (3*height/4)) {
  708. ui_pattern.state = "square_2";
  709. }
  710. else {
  711. ui_pattern.state = "idle"
  712. }
  713. }
  714. else if (ui_pattern.state == "square_2") {
  715. if (x < (width/4)) {
  716. ui_pattern.state = "square_3";
  717. }
  718. else {
  719. ui_pattern.state = "idle"
  720. }
  721. }
  722. else if (ui_pattern.state == "square_3") {
  723. if (y < (height/4)) {
  724. ui_pattern.state = "square_4";
  725. }
  726. else {
  727. ui_pattern.state = "idle"
  728. }
  729. }
  730. else if (ui_pattern.state == "square_4") {
  731. if (x > (3*width/4)) {
  732. ui_pattern.state = "square_5";
  733. }
  734. else {
  735. ui_pattern.state = "idle"
  736. }
  737. }
  738. else if (ui_pattern.state == "square_5") {
  739. if (y > (3*height/4)) {
  740. ui_pattern.state = "square_6";
  741. }
  742. else {
  743. ui_pattern.state = "idle"
  744. }
  745. }
  746. else if (ui_pattern.state == "square_6") {
  747. if (x < (width/4)) {
  748. _switch_ui("ui_diagnostic");
  749. }
  750. ui_pattern.state = "idle"
  751. }
  752. return false;
  753. }
  754. function ui_pattern_mouseup(ev) {
  755. return false;
  756. }
  757. //------------
  758. function _mainscreen() {
  759. _switch_ui('ui_login');
  760. g_ctx.driver_login_state = "driver";
  761. _clear_ui_driver();
  762. _clear_ui_paddle();
  763. }
  764. function _configuration() {
  765. _switch_ui('ui_configuration');
  766. g_ctx.driver_login_state = "driver";
  767. _clear_ui_driver();
  768. _clear_ui_paddle();
  769. }
  770. function _reset() {
  771. _switch_ui('ui_login');
  772. g_ctx.driver_login_state = "driver";
  773. _clear_ui_driver();
  774. _clear_ui_paddle();
  775. location.reload(true);
  776. }
  777. function _reboot() {
  778. _switch_ui('ui_login');
  779. g_ctx.driver_login_state = "driver";
  780. _clear_ui_driver();
  781. _clear_ui_paddle();
  782. }
  783. function _custom0() {
  784. _api_request("interfaceupdown", {"interface":"eth0", "state":"up"}, function(inp) { console.log("custom0 ok:", inp); }, function(inp) { console.log("custom0 error:", inp); });
  785. }
  786. function _custom1() {
  787. _api_request("interfaceupdown", {"interface":"eth0", "state":"down"}, function(inp) { console.log("custom1 ok:", inp); }, function(inp) { console.log("custom1 error:", inp); });
  788. }
  789. function _update_diagnostic_window(_msg) {
  790. var ele = document.getElementById("ui_diagnostic_view");
  791. ele.innerHTML = _msg;
  792. }
  793. function _custom2() {
  794. _api_request("interfaceinfo", {"interface":"eth0"}, _update_diagnostic_window, function(inp) { console.log("custom2 error:", inp); });
  795. }
  796. function _custom3() {
  797. _api_request("say", {"message":"speech test"}, function(inp) { console.log("custom3 ok:", inp); }, function(inp) { console.log("custom3 error:", inp); });
  798. }
  799. function _custom4() {
  800. //_switch_ui('ui_login');
  801. //g_ctx.driver_login_state = "driver";
  802. //_clear_ui_driver();
  803. //_clear_ui_paddle();
  804. }
  805. function _custom5() {
  806. //_switch_ui('ui_login');
  807. //g_ctx.driver_login_state = "driver";
  808. //_clear_ui_driver();
  809. //_clear_ui_paddle();
  810. }
  811. //------------
  812. //-------------
  813. function init() {
  814. _init_websocket();
  815. // diagnostics
  816. //
  817. document.getElementById('ui_diagnostic_mainscreen').onclick = _mainscreen;
  818. document.getElementById('ui_diagnostic_reset').onclick = _reset;
  819. document.getElementById('ui_diagnostic_reboot').onclick = _reboot;
  820. document.getElementById('ui_diagnostic_custom0').onclick = _custom0;
  821. document.getElementById('ui_diagnostic_custom1').onclick = _custom1;
  822. document.getElementById('ui_diagnostic_custom2').onclick = _custom2;
  823. document.getElementById('ui_diagnostic_custom3').onclick = _custom3;
  824. document.getElementById('ui_diagnostic_custom4').onclick = _custom4;
  825. document.getElementById('ui_diagnostic_custom5').onclick = _custom5;
  826. // configuration
  827. //
  828. document.getElementById('ui_configuration_back').onclick = _mainscreen;
  829. document.getElementById('ui_configuration_volumetest').onclick = _volume_test;
  830. // login
  831. //
  832. document.getElementById('ui_login_login').onclick = _login;
  833. document.getElementById('ui_login_configuration').onclick = _configuration;
  834. document.getElementById('ui_driverincorrect_btn').onclick = _driver_incorrect;
  835. document.getElementById('ui_paddleunknown_btn').onclick = _paddle_unknown;;
  836. // main menu buttons
  837. //
  838. document.getElementById('ui_main_prv').onclick = _main_prv;
  839. document.getElementById('ui_main_nxt').onclick = _main_nxt;
  840. document.getElementById('ui_main_bulk').onclick = _main_bulk;
  841. document.getElementById('ui_main_menu').onclick = _main_menu;
  842. document.getElementById('ui_main_dim').onclick = _main_dim;
  843. // dim
  844. //
  845. document.getElementById('ui_dim_btn').onclick = _dim_wakeup;
  846. var simple_fares = [ "adult", "youth", "half", "bike", "wheelchair", "transfer"];
  847. var simple_fares_f = [ _fare_adult, _fare_youth, _fare_half, _fare_bike, _fare_wheelchair, _fare_transfer];
  848. for (var ii=0; ii<simple_fares.length; ii++) {
  849. var fare = simple_fares[ii];
  850. document.getElementById('ui_main_' + fare).onclick = simple_fares_f[ii];
  851. }
  852. // bulk menu buttons
  853. //
  854. document.getElementById('ui_bulk_passes').onclick =
  855. function() {
  856. var ele;
  857. ele = document.getElementById("ui_bulk_misc");
  858. ele.classList.remove("highlight");
  859. ele = document.getElementById("ui_bulk_passes");
  860. ele.classList.add("highlight");
  861. _hide_ui_element("ui_bulk_misc_tab");
  862. _show_ui_element("ui_bulk_passes_tab");
  863. };
  864. document.getElementById('ui_bulk_misc').onclick =
  865. function() {
  866. var ele;
  867. ele = document.getElementById("ui_bulk_misc");
  868. ele.classList.add("highlight");
  869. ele = document.getElementById("ui_bulk_passes");
  870. ele.classList.remove("highlight");
  871. _show_ui_element("ui_bulk_misc_tab");
  872. _hide_ui_element("ui_bulk_passes_tab");
  873. };
  874. document.getElementById('ui_bulk_menu').onclick = function() { _switch_ui("ui_main"); }
  875. // Setup "Bulk" UI accept buttons to do proper screen switching and
  876. // other tasks.
  877. //
  878. var bulk_pass = ["adult", "youth", "half", "event", "transfer", "interline",
  879. "schoola", "schoolb", "schoolc", "orga", "orgb", "orgc"];
  880. var bulk_rule = ["BULK-CASH-ADULT", "BULK-CASH-YOUTH", "BULK-CASH-HALF",
  881. "BULK-MISC-EVENT", "BULK-MISC-TRANSFER", "BULK-MISC-INTERLINE",
  882. "BULK-OVR-SCHOOLA", "BULK-OVR-SCHOOLB", "BULK-OVR-SCHOOLC",
  883. "BULK-OVR-ORGA", "BULK-OVR-ORGB", "BULK-OVR-ORGC"];
  884. var bulk_pass_name = ["Adult Cash", "Youth Cash", "Half Cash", "Special Event", "Transfer", "Interline",
  885. "School A", "School B", "School C", "Org A", "Org B", "Org C"];
  886. // use site specific values if available, otherwise default to the above
  887. //
  888. if ((typeof BULK_PASS !== "undefined") && (BULK_PASS.length > 0)) {
  889. bulk_pass = BULK_PASS;
  890. }
  891. if ((typeof BULK_RULE !== "undefined") && (BULK_RULE.length > 0)) {
  892. bulk_rule= BULK_RULE;
  893. }
  894. if ((typeof BULK_PASS_NAME !== "undefined") && (BULK_PASS_NAME.length > 0)) {
  895. bulk_pass_name = BULK_PASS_NAME;
  896. }
  897. for (var i=0; i<bulk_pass.length; i++) {
  898. var p = bulk_pass[i];
  899. var u = bulk_pass_name[i];
  900. var r = bulk_rule[i];
  901. var ele = document.getElementById('ui_bulk_' + p);
  902. ele.onclick = (function(x,y) {
  903. return function() {
  904. _clear_ui_bulkaccept();
  905. g_ctx.bulkfare = x;
  906. _switch_ui("ui_bulkaccept");
  907. var ee = document.getElementById("ui_bulkaccept_faretype");
  908. ee.innerHTML = y;
  909. };
  910. })(r, u);
  911. }
  912. // bulk accept fares
  913. //
  914. document.getElementById('ui_bulkaccept_ok').onclick = _bulkaccept_ok;
  915. document.getElementById('ui_bulkaccept_cancel').onclick = _bulkaccept_cancel;
  916. // fareoverride menu buttons
  917. //
  918. document.getElementById('ui_fareoverride_passes').onclick =
  919. function() {
  920. var ele;
  921. ele = document.getElementById("ui_fareoverride_misc");
  922. ele.classList.remove("highlight");
  923. ele = document.getElementById("ui_fareoverride_passes");
  924. ele.classList.add("highlight");
  925. _hide_ui_element("ui_fareoverride_misc_tab");
  926. _show_ui_element("ui_fareoverride_passes_tab");
  927. };
  928. document.getElementById('ui_fareoverride_misc').onclick =
  929. function() {
  930. var ele;
  931. ele = document.getElementById("ui_fareoverride_misc");
  932. ele.classList.add("highlight");
  933. ele = document.getElementById("ui_fareoverride_passes");
  934. ele.classList.remove("highlight");
  935. _show_ui_element("ui_fareoverride_misc_tab");
  936. _hide_ui_element("ui_fareoverride_passes_tab");
  937. };
  938. document.getElementById('ui_fareoverride_menu').onclick = function() { _switch_ui("ui_main"); }
  939. document.getElementById('ui_fareoverride_logout').onclick =
  940. function() {
  941. _main_message_clear();
  942. _api_request("logout");
  943. _switch_ui("ui_login");
  944. }
  945. // default values for override passes
  946. //
  947. var override_pass = [ "adult", "youth", "half",
  948. "bike", "wheelchair", "transfer",
  949. "event", "interline",
  950. "schoola", "schoolb", "schoolc",
  951. "orga", "orgb", "orgc" ];
  952. var override_rule = [ "CASH-ADULT", "CASH-YOUTH", "CASH-HALF",
  953. "MISC-BIKE", "MISC-WHEELCHAIR", "MISC-TRANSFER",
  954. "MISC-EVENT", "MISC-INTERLINE",
  955. "OVR-SCHOOLA", "OVR-SCHOOLB", "OVR-SCHOOLC",
  956. "OVR-ORGA", "OVR-ORGB", "OVR-ORGC" ];
  957. // use site specific values if available, otherwise default to the above
  958. //
  959. if ((typeof OVERRIDE_PASS !== "undefined") && (OVERRIDE_PASS.length > 0)) {
  960. override_pass = OVERRIDE_PASS;
  961. }
  962. if ((typeof OVERRIDE_RULE !== "undefined") && (OVERRIDE_RULE.length > 0)) {
  963. override_rule= OVERRIDE_RULE;
  964. }
  965. for (var i=0; i<override_pass.length; i++) {
  966. var p = override_pass[i];
  967. var r = override_rule[i];
  968. var ele = document.getElementById("ui_fareoverride_" + p);
  969. ele.onclick = (function(x,y) {
  970. return function() { _api_request("fare", {"rule":x,"fare":y,"param":"","count":1}); };
  971. })(r,p);
  972. }
  973. var ui_page = [ "driver", "paddle", "bulkaccept" ];
  974. var ui_func = [ _driver_login, _paddle_input, _bulkaccept_input ];
  975. for (var idx=0; idx<ui_page.length; idx++) {
  976. var c = ui_page[idx];
  977. var f = ui_func[idx];
  978. for (var i=0; i<=9; i++) {
  979. document.getElementById('ui_' + c + '_' + i).onclick =
  980. (function(_f, x) { return function() { _f(x); } })(f, i);
  981. }
  982. document.getElementById('ui_' + c + '_' + 'bksp').onclick =
  983. (function(_f,x) { return function() { _f(x); } })(f, 'bksp');
  984. document.getElementById('ui_' + c + '_' + 'clr').onclick =
  985. (function(_f,x) { return function() { _f(x); } })(f, 'clr');
  986. document.getElementById('ui_' + c + '_' + 'ok').onclick =
  987. (function(_f,x) { return function() { _f(x); } })(f, 'ok');
  988. document.getElementById('ui_' + c + '_' + 'cancel').onclick =
  989. (function(_f,x) { return function() { _f(x); } })(f, 'cancel');
  990. }
  991. // set interval to check status every 1 second
  992. //
  993. var x = window.setInterval(function() { _api_request("status", {}, _cb_status, _cb_status_err); }, 1000);
  994. // try and reduce textarea highlighting, drag and drop/copy pasting,
  995. // and other bad behavor.
  996. //
  997. var ele;
  998. var block_ele;
  999. var rect;
  1000. ele = document.getElementById("ui_login_status");
  1001. ele.ondragstart = function() { return false; }
  1002. ele.onselectstart = function() { return false; }
  1003. ele.onmousedown = function() { return false; }
  1004. ele.ondragstart = function() { return false; }
  1005. rect = ele.getBoundingClientRect();
  1006. block_ele = document.getElementById("ui_login_blockwindow");
  1007. block_ele.style.width = rect.width;
  1008. block_ele.style.height = rect.height;
  1009. block_ele.style.bottom = rect.bottom;
  1010. block_ele.style.left = rect.left;
  1011. block_ele.style.top = rect.top;
  1012. block_ele.style.right = rect.right;
  1013. block_ele.style.x = rect.x;
  1014. block_ele.style.y = rect.y;
  1015. ele = document.getElementById("ui_main_status");
  1016. ele.ondragstart = function() { return false; }
  1017. ele.onselectstart = function() { return false; }
  1018. ele.onmousedown = function() { return false; }
  1019. ele.ondragstart = function() { return false; }
  1020. rect = ele.getBoundingClientRect();
  1021. block_ele = document.getElementById("ui_main_blockwindow");
  1022. block_ele.style.width = rect.width;
  1023. block_ele.style.height = rect.height;
  1024. block_ele.style.bottom = rect.bottom;
  1025. block_ele.style.left = rect.left;
  1026. block_ele.style.top = rect.top;
  1027. block_ele.style.right = rect.right;
  1028. block_ele.style.x = rect.x;
  1029. block_ele.style.y = rect.y;
  1030. ele = document.getElementById("body");
  1031. // We're using jquery so we need to gracefully add callbacks
  1032. // for mouse events.
  1033. //
  1034. ele.addEventListener('mousedown', ui_pattern_mousedown);
  1035. ele.addEventListener('mouseup', ui_pattern_mouseup);
  1036. g_snd.button_press = new Audio("assets/459992_florianreichelt_beep_short.mp3");
  1037. g_snd.accept_fare = new Audio("assets/443026__qubodup__public-domain-beep-sound.mp3");
  1038. g_snd.reject_fare = new Audio("assets/368780__gurie__start-sound-beep.mp3");
  1039. g_snd.rule_error = new Audio("assets/363920__samsterbirdies__8-bit-error.mp3");
  1040. if (typeof PopufareSiteInit !== "undefined") {
  1041. PopufareSiteInit();
  1042. }
  1043. }
  1044. (function($) {
  1045. $(document).ready(function() {
  1046. init();
  1047. });
  1048. })(jQuery);