]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/blob - templates/main.js
Remove commented out code
[coffee/coffee-flask.git] / templates / main.js
1 var flask = "{{ url_for('hello', _external=True) }}"
2
3 // State variables
4
5 var updateRemote = undefined;   // defined iff remote server accessible
6 var timeToLogout = undefined;   // defined during logout countdown
7 var logoutTimer;
8 var reloadTimer = undefined;
9 var id_user;                    // ID of the user who is to be accounted for the next coffee
10
11 console.log("hello from flask");
12 //sendJSON("{\"type\":\"empty\"}");
13
14 function update(id, msg) {
15     document.getElementById(id).innerHTML = msg;
16 }
17
18 function loadLocalStorage() {
19     if (localStorage) {
20         if (localStorage.length) {
21             var entries = [];
22             for (var i = 0; i < localStorage.length; i++) {
23                 var value = localStorage.getItem(localStorage.key(i));
24                 try {
25                     var key = localStorage.key(i);
26                     var value = localStorage.getItem(key);
27                     entries.push({ key: key, value: value });
28                 } catch (err) {
29                     console.log("no json: " + value)
30                 }
31             }
32             localStorage.clear();
33             entries.sort((entry1, entry2) => { return entry1.key > entry2.key;});
34             for (var i = 0; i < entries.length; i++) {
35                 updateRemote(entries[i].value);
36             }
37         }
38     }
39 }
40
41 var flavorChosen;
42
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.
46 function updateUI()
47 {
48     if (updateRemote === undefined) {
49         update("remote", "<center>Server offline...</center>");
50         document.getElementById("local").style.display = "block";
51         loadRemote();
52     } else {
53         document.getElementById("local").style.display = "none";
54
55         if (id_user !== undefined) {
56             document.getElementById("nextStep").innerHTML = "Now select a beverage on the coffee machineā€¦";
57         } else {
58             document.getElementById("nextStep").innerHTML = "Enjoy your " + flavorChosen + "!";
59         }
60
61         if (timeToLogout !== undefined)
62             document.getElementById("logout_button").innerHTML = '<br>logout<br>(' + timeToLogout + ' s)';
63     }
64 }
65
66 function hiddenUpdateRemote(json) {
67     var msg = JSON.parse(json);
68
69     switch(msg.type) {
70         case "empty":
71             break;
72         case "rfid":
73             login(msg.uid);
74             break;
75         case "keys":
76             var flavor = getFlavor(msg.key);
77             if (flavor !== "") {
78                 addCoffee(flavor);
79             }
80             break;
81         case "fuck":
82             ajax(msg.method, msg.route, msg.data, msg.id);
83             break;
84     }
85     sendLog(json);
86 }
87
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;
95                 loadLocalStorage();
96                 updateUI();
97                 clearTimeout(reloadTimer);
98             } else {
99                 reloadTimer = setTimeout(loadRemote, 1000);
100             }
101         }
102     };
103     xhr.open("GET", flask, true);
104     xhr.send();
105 }
106
107 loadRemote();
108
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);
116                 updateUI();
117             } else {
118                 updateRemote = undefined;
119                 updateUI();
120
121                 if (localStorage) {
122                     var fuck = JSON.stringify({
123                         type: "fuck",
124                         method: method,
125                         route: route,
126                         data: data,
127                         id: id
128                     });
129                     localStorage.setItem(now, fuck);
130                     console.log(now + ": " + fuck);
131                 }
132             }
133         }
134     };
135     xhr.open(method, flask + route, true);
136     xhr.withCredentials = true;
137     xhr.setRequestHeader("Content-type", "application/json");
138     if (method === "POST") {
139         xhr.send(data);
140     } else {
141         xhr.send();
142     }
143 }
144
145
146 function login(id) {
147     ajax("POST", "login", id, "user");
148     id_user = id;
149     clearTimeout(logoutTimer);
150     timeToLogout = undefined;
151 }
152
153 function logout() {
154     sendReset();
155     ajax("GET", "logout", "", "user");
156     id_user = undefined;
157     timeToLogout = undefined;
158 }
159
160 function countingTimeLogout(count_time)
161 {
162     timeToLogout = count_time;
163     updateUI();
164     if (count_time == 0) {
165         logout();
166     } else {
167         logoutTimer = setTimeout(function() { countingTimeLogout(count_time - 1); }, 1000);
168     }
169 }
170
171 function renameUser() {
172     ajax("GET", "user/rename?name=" +  document.getElementById("username").value, "", "user");
173 }
174
175 function getFlavor(letter) {
176     switch (letter) {
177         case "E": return "espresso";
178         case "C": return "cappuccino";
179         case "B": return "latte macchiato";
180         case "D": return "espresso lungo";
181         default: return "";
182     }
183 }
184
185 function addCoffee(flavor) {
186     var data = JSON.stringify({
187         time: new Date().toISOString(),
188         flavor: flavor,
189         uid: id_user
190     });
191     if (id_user) {
192         ajax("POST", "coffee/add", data, "user");
193         flavorChosen = flavor;
194         id_user = undefined;
195         countingTimeLogout(10); //mean 10 seconds
196     }
197 }
198
199 function sendLog(json) {
200     ajax("POST", "log", json, "log");
201 }