/*
* Copyright (c) 2019 Clementine Computing LLC.
*
* This file is part of PopuFare.
*
* PopuFare is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PopuFare is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with PopuFare. If not, see .
*
*/
var _ADDRESS = "localhost";
var _URL = "http://" + _ADDRESS;
var _PORT = 60535;
var _fqADDRESS = _ADDRESS + ":" + _PORT;
var BG_COLOR = "#f7f7f7";
var TEXT_COLOR = "#444444";
var DIU_UI_VERSION = "0.1.0";
var DIU_UI_VERSION_DATE = "2019-09-04 15:24:11";
var g_snd = {
"button_press" : ""
};
function _beep() {
g_snd.button_press.currentTime=0;
g_snd.button_press.play();
}
var g_ctx = {
"driver_login_state" : "driver",
"driver" : "",
"pin" : "",
"paddle" : "",
"current_ui" : "ui_login",
"ui_change_timeoutid": -1,
"bulkfare" : "",
"bulkfare_count" : "",
"status_text": "",
"status_text_w": 0,
"status_text_h": 0,
"diu_status" : {},
"x": ""
};
function _api_fail() {
console.log(">>api fail");
return true;
}
function _api_tick() { }
function _api_request(action, param, cb_ok, cb_fail, cb_neterr) {
var idx = 0;
var param_name = [], param_val = [];
var req = new XMLHttpRequest();
//var url = _URL + ":" + _PORT + "/req";
var url = "/req";
var varstr = "action=" + action;
for (var x in param) {
param_name.push(x);
param_val.push(param[x]);
}
for (var ii=0; ii=2) && (resp[0] == "ok")) {
if (typeof cb_ok !== "undefined") {
cb_ok(fulltxt);
}
return;
}
if (typeof cb_fail !== "undefined") {
cb_fail(fulltxt);
}
}
else if (typeof cb_fail !== "undefined") { cb_fail(); }
}
};
req.addEventListener("error", _api_fail);
req.addEventListener("abort", _api_fail);
//req.addEventListener("load", _api_tick);
//req.addEventListener("progress", _api_tick);
req.onerror = function() { console.log("network error"); };
try {
//DEBUG
console.log("req, varstr:", varstr);
req.open("POST", url, true);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.send(varstr);
} catch (er) {
console.log(">>>>>>ERROR", er);
}
}
function _block_status_window(ui_id) {
var ele = document.getElementById(ui_id + "_status");
ele.ondragstart = function() { return false; }
ele.onselectstart = function() { return false; }
ele.onmousedown = function() { return false; }
ele.ondragstart = function() { return false; }
var rect = ele.getBoundingClientRect();
var block_ele = document.getElementById(ui_id + "_blockwindow");
block_ele.style.width = rect.width;
block_ele.style.height = rect.height;
block_ele.style.bottom = rect.bottom;
block_ele.style.left = rect.left;
block_ele.style.top = rect.top;
block_ele.style.right = rect.right;
block_ele.style.x = rect.x;
block_ele.style.y = rect.y;
}
function _switch_ui(to) {
var ele;
console.log(">>>to", to);
if (g_ctx.ui_change_timeoutid > 0) {
window.clearTimeout(g_ctx.ui_change_timeoutid);
g_ctx.ui_change_timeoutid = -1;
}
var from = g_ctx.current_ui;
ele = document.getElementById(from);
ele.style.display = "none";
ele = document.getElementById(to);
ele.style.display = "block";
g_ctx.current_ui = to;
if ((to == "ui_main") ||
(to == "ui_login")) {
_block_status_window(to);
}
}
function _hide_ui(from) {
var ele = document.getElementById(from);
ele.style.display = "none";
}
function _hide_ui_element(from) {
var ele = document.getElementById(from);
ele.style.display = "none";
}
function _show_ui(to) {
var ele = document.getElementById(to);
ele.style.display = "block";
g_ctx.current_ui = to;
// put pane over status window to prevent highlights,
// copy/pasting, etc.
//
if ((to == "ui_main") ||
(to == "ui_login")) {
_block_status_window(to);
}
}
function _show_ui_element(to) {
var ele = document.getElementById(to);
ele.style.display = "block";
}
//------------
function _clear_ui_driver() {
var ele;
var u = ["driver", "pin"];
for (var i=0; i g_ctx.status_text_h) {
var n = lines.length;
lines = lines.slice(n - g_ctx.status_text_h);
}
for (var i=0; i g_ctx.status_text_h) {
var n = lines.length;
lines = lines.slice(n - g_ctx.status_text_h);
}
for (var i=0; i (3*width/4)) {
ui_pattern.state = "square_1";
}
else {
ui_pattern.state = "idle"
}
}
else if (ui_pattern.state == "square_1") {
if (y > (3*height/4)) {
ui_pattern.state = "square_2";
}
else {
ui_pattern.state = "idle"
}
}
else if (ui_pattern.state == "square_2") {
if (x < (width/4)) {
_switch_ui("ui_diagnostic");
}
ui_pattern.state = "idle"
}
return false;
if (ui_pattern.state == "idle") {
if ((x < (width/4)) && (y < (height/4))) {
ui_pattern.state = "cross_0_start";
}
else {
ui_pattern.state = "idle"
}
}
else if (ui_pattern.state == "cross_0_end") {
if ((x > (3*width/4)) && (y < (height/4))) {
ui_pattern.state = "cross_1_start";
}
else {
ui_pattern.state = "idle"
}
}
else {
ui_pattern.state = "idle";
}
return false;
}
function ui_pattern_mouseup(ev) {
return false;
var ele = document.getElementById("body");
var rect = ele.getBoundingClientRect();
var x = ev["x"];
var y = ev["y"];
var width = rect.width;
var height = rect.height;
if (ui_pattern.state == "cross_0_start") {
if ((x > (3*width/4)) && (y > (3*height/4))) {
ui_pattern.state = "cross_0_end";
}
else {
ui_pattern.state = "idle"
}
}
else if (ui_pattern.state == "cross_1_start") {
if ((x < (width/4)) && (y > (3*height/4))) {
_switch_ui("ui_diagnostic");
}
ui_pattern.state = "idle";
}
else {
ui_pattern.state = "idle";
}
return false;
}
//------------
function _mainscreen() {
_switch_ui('ui_login');
g_ctx.driver_login_state = "driver";
_clear_ui_driver();
_clear_ui_paddle();
}
function _reset() {
_switch_ui('ui_login');
g_ctx.driver_login_state = "driver";
_clear_ui_driver();
_clear_ui_paddle();
location.reload(true);
}
function _reboot() {
_switch_ui('ui_login');
g_ctx.driver_login_state = "driver";
_clear_ui_driver();
_clear_ui_paddle();
}
function _custom0() {
_switch_ui('ui_login');
g_ctx.driver_login_state = "driver";
_clear_ui_driver();
_clear_ui_paddle();
}
function _custom1() {
_switch_ui('ui_login');
g_ctx.driver_login_state = "driver";
_clear_ui_driver();
_clear_ui_paddle();
}
function _custom2() {
_switch_ui('ui_login');
g_ctx.driver_login_state = "driver";
_clear_ui_driver();
_clear_ui_paddle();
}
//------------
//-------------
function init() {
_init_websocket();
// diagnostics
//
document.getElementById('ui_diagnostic_mainscreen').onclick = _mainscreen;
document.getElementById('ui_diagnostic_reset').onclick = _reset;
document.getElementById('ui_diagnostic_reboot').onclick = _reboot;
document.getElementById('ui_diagnostic_custom0').onclick = _custom0;
document.getElementById('ui_diagnostic_custom1').onclick = _custom1;
document.getElementById('ui_diagnostic_custom2').onclick = _custom2;
// login
//
document.getElementById('ui_login_login').onclick = _login;
document.getElementById('ui_driverincorrect_btn').onclick = _driver_incorrect;
document.getElementById('ui_paddleunknown_btn').onclick = _paddle_unknown;;
// main menu buttons
//
document.getElementById('ui_main_prv').onclick = _main_prv;
document.getElementById('ui_main_nxt').onclick = _main_nxt;
document.getElementById('ui_main_bulk').onclick = _main_bulk;
document.getElementById('ui_main_menu').onclick = _main_menu;
var simple_fares = [ "adult", "youth", "half", "bike", "wheelchair", "transfer"];
var simple_fares_f = [ _fare_adult, _fare_youth, _fare_half, _fare_bike, _fare_wheelchair, _fare_transfer];
for (var ii=0; ii