piu_ui.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Copyright (c) 2021 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. console.log("...");
  21. var _ADDRESS = "localhost";
  22. //var _ADDRESS = "127.0.0.1";
  23. //var _ADDRESS = "0.0.0.0";
  24. var _URL = "http://" + _ADDRESS;
  25. //var _PORT = 60535;
  26. var _WS_PORT = 8001;
  27. var _PORT = 8001;
  28. var _VID_FEED_PORT = 8080;
  29. var _fqADDRESS = _ADDRESS + ":" + _PORT;
  30. //var _wsADDRESS = _ADDRESS + ":" + _WS_PORT;
  31. // localhost causes ipv6 attempt first which ten falls
  32. // back to ipv4 and takes upwards of 1s+ to connect.
  33. // https://stackoverflow.com/a/15144195
  34. //
  35. // Use 127.0.0.1 instead
  36. //
  37. //var _wsADDRESS = "localhost" + ":" + _WS_PORT;
  38. //var _wsADDRESS = _ADDRESS + ":" + _WS_PORT + "/ws";
  39. var _wsADDRESS = "192.168.0.26" + ":" + _WS_PORT + "/ws";
  40. var BG_COLOR = "#f7f7f7";
  41. var TEXT_COLOR = "#444444";
  42. var PIU_UI_VERSION = "0.1.0";
  43. var PIU_UI_VERSION_DATE = "2020-04-11";
  44. var g_snd = {
  45. "reject_fare" : "",
  46. "accept_fare" : "",
  47. "rule_error" : "",
  48. "button_press" : ""
  49. };
  50. var g_sock = null;
  51. function _beep_error() {
  52. g_snd.rule_error.currentTime=0;
  53. g_snd.rule_error.play();
  54. }
  55. function _beep_reject() {
  56. g_snd.reject_fare.currentTime=0;
  57. g_snd.reject_fare.play();
  58. }
  59. function _beep_accept() {
  60. g_snd.accept_fare.currentTime=0;
  61. g_snd.accept_fare.play();
  62. }
  63. function _beep() {
  64. g_snd.button_press.currentTime=0;
  65. g_snd.button_press.play();
  66. }
  67. var g_ctx = {
  68. "current_ui" : "ui_main",
  69. "ui_stack" : [],
  70. "ui_change_timeoutid": -1,
  71. "ui_watchdog_timeoutid": -1,
  72. "status_text": "",
  73. "status_text_w": 32,
  74. "status_text_h": 11,
  75. "status_html_text":"",
  76. "piu_status" : {
  77. "last_update" : -1
  78. },
  79. // system config
  80. //
  81. "config" : {
  82. "last_update": -1,
  83. "volume": 100,
  84. "brightness": 100,
  85. "showdimclock": 0
  86. },
  87. "x": ""
  88. };
  89. function _clear_ui_watchdog() {
  90. if (g_ctx.ui_watchdog_timeoutid >= 0) {
  91. window.clearTimeout(g_ctx.ui_watchdog_timeoutid);
  92. }
  93. }
  94. function _set_ui_watchdog(failsafe_ui, _dt) {
  95. // default to 'ui_watchdog' and 5s
  96. //
  97. failsafe_ui = (( typeof failsafe_ui === "undefined") ? "ui_watchdog" : failsafe_ui);
  98. _dt = (( typeof _dt === "undefined") ? 5000 : _dt);
  99. _clear_ui_watchdog();
  100. g_ctx.ui_watchdog_timeoutid = window.setTimeout(
  101. (function(_fsui) {
  102. return function() { console.log("???", _fsui); _switch_ui(_fsui); };
  103. })(failsafe_ui),
  104. _dt);
  105. }
  106. function _switch_ui(to) {
  107. var ele;
  108. if (g_ctx.ui_change_timeoutid > 0) {
  109. window.clearTimeout(g_ctx.ui_change_timeoutid);
  110. g_ctx.ui_change_timeoutid = -1;
  111. }
  112. var from = g_ctx.current_ui;
  113. ele = document.getElementById(from);
  114. ele.style.display = "none";
  115. ele = document.getElementById(to);
  116. ele.style.display = "block";
  117. g_ctx.current_ui = to;
  118. if (to == "ui_main") {
  119. var _x = _calculate_ui_main_status_width_height();
  120. g_ctx.status_text_h = _x.n_height;
  121. g_ctx.status_text_w = _x.n_width;
  122. }
  123. if ((to == "ui_main") ||
  124. (to == "ui_login")) {
  125. _block_status_window(to);
  126. }
  127. }
  128. function _hide_ui(from) {
  129. var ele = document.getElementById(from);
  130. ele.style.display = "none";
  131. }
  132. function _hide_ui_element(from) {
  133. var ele = document.getElementById(from);
  134. ele.style.display = "none";
  135. }
  136. function _show_ui(to) {
  137. var ele = document.getElementById(to);
  138. ele.style.display = "block";
  139. g_ctx.current_ui = to;
  140. // put pane over status window to prevent highlights,
  141. // copy/pasting, etc.
  142. //
  143. if ((to == "ui_main") ||
  144. (to == "ui_login")) {
  145. _block_status_window(to);
  146. }
  147. }
  148. function _show_ui_element(to) {
  149. var ele = document.getElementById(to);
  150. ele.style.display = "block";
  151. }
  152. function _get_now_HH_MM(is12hour_format) {
  153. is12hour_format = ((typeof is12hour_format === "undefined") ? false : is12hour_format);
  154. var _dt = new Date();
  155. var _sep = ":";
  156. var ampm = "";
  157. if ( (_dt.getSeconds() % 2) == 0) {
  158. _sep = " ";
  159. }
  160. var _hr = _dt.getHours();
  161. var _min = _dt.getMinutes();
  162. if (is12hour_format) {
  163. if (_hr == 0) {
  164. _hr = 12;
  165. ampm = "AM";
  166. }
  167. else if (_hr >= 12) {
  168. if (_hr > 12) {
  169. _hr -= 12;
  170. }
  171. ampm = "PM";
  172. }
  173. }
  174. var _hr_str = _hr.toString();
  175. var _min_str = _min.toString();
  176. if (_hr < 10) { _hr_str = "0" + _hr_str; }
  177. if (_min < 10) { _min_str = "0" + _min_str; }
  178. var _time_str = _hr_str + _sep + _min_str + " " + ampm;
  179. return _time_str;
  180. }
  181. function _get_now_YYYY_MM_DD() {
  182. var _dt = new Date();
  183. var _sep = "-";
  184. var _yr = _dt.getFullYear();
  185. var _mo = _dt.getMonth()+1;
  186. var _dy = _dt.getDate();
  187. var _yr_str = _yr.toString();
  188. var _mo_str = _mo.toString();
  189. if (_mo < 10) {
  190. _mo_str = "0" + _mo_str;
  191. }
  192. var _dy_str = _dy.toString();
  193. if (_dy < 10) {
  194. _dy_str = "0" + _dy_str;
  195. }
  196. return _yr_str + _sep + _mo_str + _sep + _dy_str;
  197. }
  198. //------------
  199. // value is from [0,1]
  200. //
  201. function _process_brightness(brightness, update_system) {
  202. // 0 opacity is completely clear, so (1.0) brightness (100% brightness).
  203. //
  204. var f_brightness = parseFloat(brightness);
  205. var opacity = 1.0 - f_brightness;
  206. // fail
  207. //
  208. if ((opacity < 0.0) || (opacity > 1.0)) { return {"type":"error", "msg":"opacity out of range"}; }
  209. var ele = document.getElementById("dim_overlay");
  210. ele.style.opacity = opacity;
  211. ele = document.getElementById("ui_configuration_displaybrightness");
  212. ival = Math.floor(f_brightness*100.0);
  213. ele.innerHTML = "" + ival + "%";
  214. g_ctx.config.brightness = ival;
  215. if (update_system) { _set_system_config(); }
  216. }
  217. //------------
  218. // volume is from [0,1]
  219. //
  220. function _process_volume(vol, update_system) {
  221. var ele = document.getElementById("ui_configuration_displayvolume");
  222. ivol = Math.floor(vol*100.0);
  223. if (ivol < 0) { ivol = 0; }
  224. if (ivol > 100) { ivol = 100; }
  225. ele.innerHTML = "" + ivol + "%";
  226. g_ctx.config.volume = ivol;
  227. if (update_system) { _set_system_config(); }
  228. }
  229. var _rate_limit_beep_t = -1.0;
  230. var _rate_limit_beep_dt = 250;
  231. function _rate_limit_beep() {
  232. if (_rate_limit_beep_t<0) {
  233. _rate_limit_beep_t = Date.now();
  234. }
  235. if (Date.now() > (_rate_limit_beep_t + _rate_limit_beep_dt)) {
  236. _rate_limit_beep_t = Date.now();
  237. _beep();
  238. }
  239. }
  240. function _watchdog_tripped(inp) {
  241. console.log("watchdog UI expired!...");
  242. _reset();
  243. }
  244. function _init_dim_overlay() {
  245. var ele = document.getElementById("ui_dim_overlay");
  246. }
  247. function _update_display_time() {
  248. var ele = document.getElementById("ui_message_datetime");
  249. if (typeof ele === "undefined") { return; }
  250. var _time_str = _get_now_YYYY_MM_DD() + " " + _get_now_HH_MM(true);
  251. ele.innerHTML = _time_str ;
  252. }
  253. //-------------
  254. function _ws_process(dat) {
  255. console.log("_ws_process:", dat);
  256. var tok = dat.split(" ");
  257. if (tok.length < 1) { return; }
  258. var argv1 = ( (tok.length > 1) ? tok[1] : "");
  259. var argv2 = ( (tok.length > 2) ? tok[2] : "");
  260. var msg_payload = tok.slice(1).join(" ");
  261. if (tok[0] == "passenger_notify") {
  262. ui_setmessage("ui_message_cardinfo", msg_payload);
  263. }
  264. else if (tok[0] == "passenger_message") {
  265. ui_setmessage("ui_message_status", msg_payload);
  266. }
  267. }
  268. function _init_websocket() {
  269. var sock = new WebSocket("ws://" + _wsADDRESS);
  270. console.log("??? _init_websockets:", sock, "ws://" + _wsADDRESS);
  271. g_sock = sock;
  272. sock.onopen = function(e) { console.log("open", e); }
  273. sock.onmessage = function(e) { _ws_process(e.data); }
  274. sock.onclose = function(e) {
  275. console.log("close", e);
  276. setTimeout( function() { _init_websocket(); }, 1000);
  277. }
  278. sock.onerror = function(e) {
  279. console.log("error", e.message, e);
  280. }
  281. }
  282. //-------------
  283. function init() {
  284. _init_dim_overlay();
  285. g_snd.button_press = new Audio("assets/459992_florianreichelt_beep_short.mp3");
  286. g_snd.accept_fare = new Audio("assets/389214__joe-anderson22__microwave-beeps.mp3");
  287. g_snd.reject_fare = new Audio("assets/368780__gurie__start-sound-beep.mp3");
  288. g_snd.rule_error = new Audio("assets/363920__samsterbirdies__8-bit-error.mp3");
  289. var ele = document.getElementById('ui_main_vidfeed');
  290. ele.src = _URL + ":" + _VID_FEED_PORT;
  291. window.setInterval(_update_display_time, 200);
  292. _init_websocket();
  293. }
  294. // ui_message_status
  295. // ui_message_cardinfo
  296. // ui_message_datetime
  297. //
  298. function ui_setmessage(ui_id, msg) {
  299. var x = document.getElementById(ui_id);
  300. x.innerHTML = msg;
  301. }
  302. (function($) {
  303. $(document).ready(function() {
  304. init();
  305. });
  306. })(jQuery);