]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/blobdiff - templates/main.js
Rename the class of the main div from "user" to "content"
[coffee/coffee-flask.git] / templates / main.js
index 034fcfa96f28bc8262158d8f5d4d9cae3857f543..9cc11f5351a4ff891aceb0bf4c3341c426d22fe4 100644 (file)
@@ -2,11 +2,13 @@ var flask = "{{ url_for('hello', _external=True) }}"
 
 // State variables
 
-var updateRemote = undefined;   // defined iff remote server accessible
-var timeToLogout = undefined;   // defined during logout countdown
+var updateRemote = undefined;           // defined iff remote server accessible
+var timeToLogout = undefined;           // defined during logout countdown
 var logoutTimer;
 var reloadTimer = undefined;
-var id_user;                    // ID of the user who is to be accounted for the next coffee
+var id_user;                            // ID of the user who is to be accounted for the next coffee
+var identifier_registration = false;    // true if identifier is supposed to be registered for user
+var eventMsg = undefined;               // Feedback message about the last event performed by the user
 
 console.log("hello from flask");
 //sendJSON("{\"type\":\"empty\"}");
@@ -20,7 +22,7 @@ function replayOfflineQueue() {
         let queue = JSON.parse(localStorage.getItem("offlineQueue")) || [];
         if (Array.isArray(queue)) {
             queue.forEach(function (entry) {
-                updateRemote(entry.data);
+                updateRemote(entry.data, new Date(entry.time));
             });
             localStorage.removeItem("offlineQueue");
         }
@@ -41,13 +43,17 @@ function updateUI()
         document.getElementById("remote").style.display = offline ? "none" : "block";
 
         if (offline) {
-            loadRemote();       // Try to contact the server periodically
+            showOfflineQueue();
             return;
         }
 
+        if (eventMsg !== undefined) {
+            update("eventMsg", eventMsg);
+            eventMsg = undefined;
+        }
         if (id_user !== undefined) {
             document.getElementById("nextStep").innerHTML = "Now select a beverage on the coffee machineā€¦";
-        } else {
+        } else if (flavorChosen !== undefined) {
             document.getElementById("nextStep").innerHTML = "Enjoy your " + flavorChosen + "!";
         }
 
@@ -55,29 +61,38 @@ function updateUI()
             document.getElementById("logout_button").innerHTML = '<br>logout<br>(' + timeToLogout + ' s)';
     }
     catch (err) {
-        console.log("Error: ", err);
+        console.log("updateUI error: ", err);
     }
 }
 
-function hiddenUpdateRemote(json) {
+function hiddenUpdateRemote(json, time = new Date()) {
     var msg = JSON.parse(json);
 
     switch(msg.type) {
         case "empty":
             break;
         case "rfid":
-            login(msg.uid);
+            if (identifier_registration) {
+                ajax("POST", "user/identifier/add", JSON.stringify({ id: msg.uid }), "content");
+
+                addIdentifier_finish();
+            } else {
+                login(msg.uid);
+            }
             break;
         case "keys":
-            var flavor = getFlavor(msg.key);
-            if (flavor !== "") {
-                addCoffee(flavor);
+            if (!identifier_registration) {
+                var flavor = getFlavor(msg.key);
+                if (flavor !== "") {
+                    addCoffee(flavor, time);
+                }
             }
             break;
-        case "fuck":
+        case "ajax_failure":
             ajax(msg.method, msg.route, msg.data, msg.id);
             break;
     }
+
     sendLog(json);
 }
 
@@ -92,6 +107,10 @@ function loadRemote(string) {
                 updateUI();
                 clearTimeout(reloadTimer);
             } else {
+                // Cancel current timer for the case when loadRemote()
+                // was called multiple times (e.g. multiple ajax()
+                // calls failed simultaneously).
+                clearTimeout(reloadTimer);
                 reloadTimer = setTimeout(loadRemote, 1000);
             }
         }
@@ -103,7 +122,6 @@ function loadRemote(string) {
 loadRemote();
 
 function ajax(method, route, data, id) {
-    var now = Date.now();
     var xhr = new XMLHttpRequest();
     xhr.onreadystatechange = function() {
         if (this.readyState == 4) {
@@ -113,17 +131,18 @@ function ajax(method, route, data, id) {
             } else {
                 updateRemote = undefined;
                 updateUI();
+                loadRemote(); // Try to contact the server periodically
 
                 if (localStorage) {
-                    var fuck = JSON.stringify({
-                        type: "fuck",
+                    var ajax_failure = JSON.stringify({
+                        type: "ajax_failure",
                         method: method,
                         route: route,
                         data: data,
                         id: id
                     });
                     let queue = JSON.parse(localStorage.getItem("offlineQueue")) || [];
-                    queue.push({ time: Date.now(), data: fuck });
+                    queue.push({ time: Date.now(), data: ajax_failure });
                     try {
                         localStorage.setItem("offlineQueue", JSON.stringify(queue));
                     }
@@ -146,21 +165,23 @@ function ajax(method, route, data, id) {
 
 
 function login(id) {
-    ajax("POST", "login", id, "user");
+    ajax("POST", "login", id, "content");
     id_user = id;
-    clearTimeout(logoutTimer);
-    timeToLogout = undefined;
+    countingTimeLogout(120);
 }
 
 function logout() {
     sendReset();
-    ajax("GET", "logout", "", "user");
+    ajax("GET", "logout", "", "content");
     id_user = undefined;
     timeToLogout = undefined;
+    identifier_registration = false;
+    window.scrollTo(0, 0); // Scroll up
 }
 
 function countingTimeLogout(count_time)
 {
+    clearTimeout(logoutTimer);
     timeToLogout = count_time;
     updateUI();
     if (count_time == 0) {
@@ -171,7 +192,7 @@ function countingTimeLogout(count_time)
 }
 
 function renameUser() {
-    ajax("GET", "user/rename?name=" +  document.getElementById("username").value, "", "user");
+    ajax("GET", "user/rename?name=" +  document.getElementById("username").value, "", "content");
 }
 
 function getFlavor(letter) {
@@ -184,20 +205,66 @@ function getFlavor(letter) {
     }
 }
 
-function addCoffee(flavor) {
+function addCoffee(flavor, time = new Date()) {
     var data = JSON.stringify({
-        time: new Date().toISOString(),
-        flavor: flavor,
-        uid: id_user
+        time: time.toISOString(),
+        flavor: flavor
     });
     if (id_user) {
-        ajax("POST", "coffee/add", data, "user");
+        ajax("POST", "coffee/add", data, "content");
         flavorChosen = flavor;
         id_user = undefined;
         countingTimeLogout(10); //mean 10 seconds
     }
 }
 
+
+function addEvent(event_name, action_msg, time = new Date()) {
+    var data = JSON.stringify({
+        time: time.toISOString(),
+        event_name: event_name,
+        uid: id_user
+    });
+    if (id_user) {
+        eventMsg = "You have " + action_msg + ". Thanks!"
+        ajax("POST", "event", data, "content");
+        window.scrollTo(0, 0); // Scroll up
+    }
+}
+
+
+function addIdentifier_start() {
+    identifier_registration = true;
+    document.getElementById("addIdentifier").disabled = true;
+    document.getElementById("labelIdentifier").style.visibility = "visible";
+    document.getElementById("abortIdentifier").disabled = false;
+}
+
+function addIdentifier_finish() {
+    identifier_registration = false;
+    document.getElementById("addIdentifier").disabled = false;
+    document.getElementById("labelIdentifier").style.visibility = "hidden";
+    document.getElementById("abortIdentifier").disabled = true;
+}
+
+function disableIdentifier(id) {
+    ajax("POST", "user/identifier/disable", JSON.stringify({ id: id }), "content");
+}
+
+function renameIdentifier(i) {
+    var data = JSON.stringify({
+        id: document.getElementById("identifier_" + i).innerText,
+        name: document.getElementById("identifier_name_" + i).value
+    });
+
+    ajax("POST", "user/identifier/rename", data, "content");
+}
+
 function sendLog(json) {
     ajax("POST", "log", json, "log");
 }
+
+function tellCoffeebot(what)
+{
+    ajax("POST", "tellCoffeebot", JSON.stringify({text: what}), "log");
+}