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);
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";
44 loadRemote(); // Try to contact the server periodically
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) {
63 var msg = JSON.parse(json);
72 var flavor = getFlavor(msg.key);
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 reloadTimer = setTimeout(loadRemote, 1000);
99 xhr.open("GET", flask, true);
105 function ajax(method, route, data, id) {
106 var xhr = new XMLHttpRequest();
107 xhr.onreadystatechange = function() {
108 if (this.readyState == 4) {
109 if (this.status == 200) {
110 update(id, this.responseText);
113 updateRemote = undefined;
117 var ajax_failure = JSON.stringify({
118 type: "ajax_failure",
124 let queue = JSON.parse(localStorage.getItem("offlineQueue")) || [];
125 queue.push({ time: Date.now(), data: ajax_failure });
127 localStorage.setItem("offlineQueue", JSON.stringify(queue));
136 xhr.open(method, flask + route, true);
137 xhr.withCredentials = true;
138 xhr.setRequestHeader("Content-type", "application/json");
139 if (method === "POST") {
148 ajax("POST", "login", id, "user");
150 countingTimeLogout(120);
155 ajax("GET", "logout", "", "user");
157 timeToLogout = undefined;
160 function countingTimeLogout(count_time)
162 clearTimeout(logoutTimer);
163 timeToLogout = count_time;
165 if (count_time == 0) {
168 logoutTimer = setTimeout(function() { countingTimeLogout(count_time - 1); }, 1000);
172 function renameUser() {
173 ajax("GET", "user/rename?name=" + document.getElementById("username").value, "", "user");
176 function getFlavor(letter) {
178 case "E": return "espresso";
179 case "C": return "cappuccino";
180 case "B": return "latte macchiato";
181 case "D": return "espresso lungo";
186 function addCoffee(flavor) {
187 var data = JSON.stringify({
188 time: new Date().toISOString(),
193 ajax("POST", "coffee/add", data, "user");
194 flavorChosen = flavor;
196 countingTimeLogout(10); //mean 10 seconds
200 function sendLog(json) {
201 ajax("POST", "log", json, "log");