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 loadLocalStorage() {
20 if (localStorage.length) {
22 for (var i = 0; i < localStorage.length; i++) {
23 var value = localStorage.getItem(localStorage.key(i));
25 var key = localStorage.key(i);
26 var value = localStorage.getItem(key);
27 entries.push({ key: key, value: value });
29 console.log("no json: " + value)
33 entries.sort((entry1, entry2) => { return entry1.key > entry2.key;});
34 for (var i = 0; i < entries.length; i++) {
35 updateRemote(entries[i].value);
43 // Central function to update UI elements. To ensure that the UI is
44 // consistent, other code should only change state variables and then
45 // call this function. This function updates the UI to match the state.
48 if (updateRemote === undefined) {
49 update("remote", "<center>Server offline...</center>");
50 document.getElementById("local").style.display = "block";
53 document.getElementById("local").style.display = "none";
55 if (id_user !== undefined) {
56 document.getElementById("nextStep").innerHTML = "Now select a beverage on the coffee machineā¦";
58 document.getElementById("nextStep").innerHTML = "Enjoy your " + flavorChosen + "!";
61 if (timeToLogout !== undefined)
62 document.getElementById("logout_button").innerHTML = '<br>logout<br>(' + timeToLogout + ' s)';
66 function hiddenUpdateRemote(json) {
67 var msg = JSON.parse(json);
76 var flavor = getFlavor(msg.key);
82 ajax(msg.method, msg.route, msg.data, msg.id);
88 function loadRemote(string) {
89 var xhr = new XMLHttpRequest();
90 xhr.onreadystatechange = function() {
91 if (this.readyState == 4) {
92 if (this.status == 200) {
93 update("remote", this.responseText);
94 updateRemote = hiddenUpdateRemote;
97 clearTimeout(reloadTimer);
99 reloadTimer = setTimeout(loadRemote, 1000);
103 xhr.open("GET", flask, true);
109 function ajax(method, route, data, id) {
110 var now = Date.now();
111 var xhr = new XMLHttpRequest();
112 xhr.onreadystatechange = function() {
113 if (this.readyState == 4) {
114 if (this.status == 200) {
115 update(id, this.responseText);
118 updateRemote = undefined;
122 var fuck = JSON.stringify({
129 localStorage.setItem(now, fuck);
130 console.log(now + ": " + fuck);
135 xhr.open(method, flask + route, true);
136 xhr.withCredentials = true;
137 xhr.setRequestHeader("Content-type", "application/json");
138 if (method === "POST") {
147 ajax("POST", "login", id, "user");
149 clearTimeout(logoutTimer);
150 timeToLogout = undefined;
155 ajax("GET", "logout", "", "user");
157 timeToLogout = undefined;
160 function countingTimeLogout(count_time)
162 timeToLogout = count_time;
164 if (count_time == 0) {
167 logoutTimer = setTimeout(function() { countingTimeLogout(count_time - 1); }, 1000);
171 function renameUser() {
172 ajax("GET", "user/rename?name=" + document.getElementById("username").value, "", "user");
175 function getFlavor(letter) {
177 case "E": return "espresso";
178 case "C": return "cappuccino";
179 case "B": return "latte macchiato";
180 case "D": return "espresso lungo";
185 function addCoffee(flavor) {
186 var data = JSON.stringify({
187 time: new Date().toISOString(),
192 ajax("POST", "coffee/add", data, "user");
193 flavorChosen = flavor;
195 countingTimeLogout(10); //mean 10 seconds
199 function sendLog(json) {
200 ajax("POST", "log", json, "log");