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 now = Date.now();
107 var xhr = new XMLHttpRequest();
108 xhr.onreadystatechange = function() {
109 if (this.readyState == 4) {
110 if (this.status == 200) {
111 update(id, this.responseText);
114 updateRemote = undefined;
118 var fuck = JSON.stringify({
125 let queue = JSON.parse(localStorage.getItem("offlineQueue")) || [];
126 queue.push({ time: Date.now(), data: fuck });
128 localStorage.setItem("offlineQueue", JSON.stringify(queue));
137 xhr.open(method, flask + route, true);
138 xhr.withCredentials = true;
139 xhr.setRequestHeader("Content-type", "application/json");
140 if (method === "POST") {
149 ajax("POST", "login", id, "user");
151 countingTimeLogout(120);
156 ajax("GET", "logout", "", "user");
158 timeToLogout = undefined;
161 function countingTimeLogout(count_time)
163 clearTimeout(logoutTimer);
164 timeToLogout = count_time;
166 if (count_time == 0) {
169 logoutTimer = setTimeout(function() { countingTimeLogout(count_time - 1); }, 1000);
173 function renameUser() {
174 ajax("GET", "user/rename?name=" + document.getElementById("username").value, "", "user");
177 function getFlavor(letter) {
179 case "E": return "espresso";
180 case "C": return "cappuccino";
181 case "B": return "latte macchiato";
182 case "D": return "espresso lungo";
187 function addCoffee(flavor) {
188 var data = JSON.stringify({
189 time: new Date().toISOString(),
194 ajax("POST", "coffee/add", data, "user");
195 flavorChosen = flavor;
197 countingTimeLogout(10); //mean 10 seconds
201 function sendLog(json) {
202 ajax("POST", "log", json, "log");