piu_ui.js 8.8 KB

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