piu_ui.js 8.5 KB

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