1 var flask = "{{ url_for('hello', _external=True) }}"
5 var updateRemote = undefined; // defined iff remote server accessible
6 var timeToLogout = undefined; // defined during logout countdown
8 var reloadTimer = undefined;
9 var id_user; // ID of the user who is to be accounted for the next coffee
11 console.log("hello from flask");
12 //sendJSON("{\"type\":\"empty\"}");
14 function update(id, msg) {
15 document.getElementById(id).innerHTML = msg;
18 function replayOfflineQueue() {
20 let queue = JSON.parse(localStorage.getItem("offlineQueue")) || [];
21 if (Array.isArray(queue)) {
22 queue.forEach(function (entry) {
23 updateRemote(entry.data, new Date(entry.time));
25 localStorage.removeItem("offlineQueue");
32 // Central function to update UI elements. To ensure that the UI is
33 // consistent, other code should only change state variables and then
34 // call this function. This function updates the UI to match the state.
38 let offline = updateRemote === undefined;
40 document.getElementById("local").style.display = !offline ? "none" : "block";
41 document.getElementById("remote").style.display = offline ? "none" : "block";
48 if (id_user !== undefined) {
49 document.getElementById("nextStep").innerHTML = "Now select a beverage on the coffee machineā¦";
51 document.getElementById("nextStep").innerHTML = "Enjoy your " + flavorChosen + "!";
54 if (timeToLogout !== undefined)
55 document.getElementById("logout_button").innerHTML = '<br>logout<br>(' + timeToLogout + ' s)';
58 console.log("Error: ", err);
62 function hiddenUpdateRemote(json, time = new Date()) {
63 var msg = JSON.parse(json);
72 var flavor = getFlavor(msg.key);
74 addCoffee(flavor, time);
78 ajax(msg.method, msg.route, msg.data, msg.id);
84 function loadRemote(string) {
85 var xhr = new XMLHttpRequest();
86 xhr.onreadystatechange = function() {
87 if (this.readyState == 4) {
88 if (this.status == 200) {
89 update("remote", this.responseText);
90 updateRemote = hiddenUpdateRemote;
93 clearTimeout(reloadTimer);
95 // Cancel current timer for the case when loadRemote()
96 // was called multiple times (e.g. multiple ajax()
97 // calls failed simultaneously).
98 clearTimeout(reloadTimer);
99 reloadTimer = setTimeout(loadRemote, 1000);
103 xhr.open("GET", flask, true);
109 function ajax(method, route, data, id) {
110 var xhr = new XMLHttpRequest();
111 xhr.onreadystatechange = function() {
112 if (this.readyState == 4) {
113 if (this.status == 200) {
114 update(id, this.responseText);
117 updateRemote = undefined;
119 loadRemote(); // Try to contact the server periodically
122 var ajax_failure = JSON.stringify({
123 type: "ajax_failure",
129 let queue = JSON.parse(localStorage.getItem("offlineQueue")) || [];
130 queue.push({ time: Date.now(), data: ajax_failure });
132 localStorage.setItem("offlineQueue", JSON.stringify(queue));
141 xhr.open(method, flask + route, true);
142 xhr.withCredentials = true;
143 xhr.setRequestHeader("Content-type", "application/json");
144 if (method === "POST") {
153 ajax("POST", "login", id, "user");
155 countingTimeLogout(120);
160 ajax("GET", "logout", "", "user");
162 timeToLogout = undefined;
165 function countingTimeLogout(count_time)
167 clearTimeout(logoutTimer);
168 timeToLogout = count_time;
170 if (count_time == 0) {
173 logoutTimer = setTimeout(function() { countingTimeLogout(count_time - 1); }, 1000);
177 function renameUser() {
178 ajax("GET", "user/rename?name=" + document.getElementById("username").value, "", "user");
181 function getFlavor(letter) {
183 case "E": return "espresso";
184 case "C": return "cappuccino";
185 case "B": return "latte macchiato";
186 case "D": return "espresso lungo";
191 function addCoffee(flavor, time = new Date()) {
192 var data = JSON.stringify({
193 time: time.toISOString(),
198 ajax("POST", "coffee/add", data, "user");
199 flavorChosen = flavor;
201 countingTimeLogout(10); //mean 10 seconds
205 function sendLog(json) {
206 ajax("POST", "log", json, "log");