var flask = "{{ url_for('hello', _external=True) }}"
-var updateRemote = undefined;
-var loggedIn = false;
+// State variables
+
+var updateRemote = undefined; // defined iff remote server accessible
+var loggedIn = false; // true after swiping the cards
+var timeToLogout = undefined; // defined during logout countdown
+var logoutTimer;
+var reloadTimer = undefined;
console.log("hello from flask");
//sendJSON("{\"type\":\"empty\"}");
function loadLocalStorage() {
if (localStorage) {
if (localStorage.length) {
+ var entries = [];
for (var i = 0; i < localStorage.length; i++) {
var value = localStorage.getItem(localStorage.key(i));
try {
- updateRemote(value);
+ var key = localStorage.key(i);
+ var value = localStorage.getItem(key);
+ entries.push({ key: key, value: value });
} catch (err) {
console.log("no json: " + value)
}
}
localStorage.clear();
+ entries.sort((entry1, entry2) => { return entry1.key > entry2.key;});
+ for (var i = 0; i < entries.length; i++) {
+ updateRemote(entries[i].value);
+ }
}
}
}
var flavorChosen;
-function countingTimeLogout(count_time){
- document.getElementById("logout_button").value = '\nlogout\n(' + count_time + ' s)';
- if (count_time == 0) {
- logout();
- } else {
- setTimeout(function() { countingTimeLogout(count_time - 1); }, 1000);
- }
-}
-
-function updateNextStep()
+// Central function to update UI elements. To ensure that the UI is
+// consistent, other code should only change state variables and then
+// call this function. This function updates the UI to match the state.
+function updateUI()
{
- if (loggedIn) {
- document.getElementById("nextStep").innerHTML = "Now select a beverage on the coffee machineā¦";
+ if (updateRemote === undefined) {
+ update("remote", "<center>Server offline...</center>");
+ document.getElementById("local").style.display = "block";
+ loadRemote();
} else {
- if (flavorChosen !== undefined) {
- document.getElementById("nextStep").innerHTML = "Enjoy your " + flavorChosen + "!";
- flavorChosen=undefined;
- countingTimeLogout(10); //mean 10 seconds
+ document.getElementById("local").style.display = "none";
+
+ if (loggedIn) {
+ document.getElementById("nextStep").innerHTML = "Now select a beverage on the coffee machineā¦";
+ } else {
+ document.getElementById("nextStep").innerHTML = "Enjoy your " + flavorChosen + "!";
}
+
+ if (timeToLogout !== undefined)
+ document.getElementById("logout_button").innerHTML = '<br>logout<br>(' + timeToLogout + ' s)';
}
}
+var id_user;
+
function hiddenUpdateRemote(json) {
var msg = JSON.parse(json);
"sak: " + msg.sak
);*/
login(msg.uid);
+ id_user=msg.uid;
break;
case "keys":
//update("json", "key: " + msg.key);
function loadRemote(string) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
- if (this.readyState == 4 && this.status == 200) {
- update("remote", this.responseText);
- updateRemote = hiddenUpdateRemote;
- loadLocalStorage();
- if (loggedIn) {
- document.getElementById("local").style.display = "none";
+ if (this.readyState == 4) {
+ if (this.status == 200) {
+ update("remote", this.responseText);
+ updateRemote = hiddenUpdateRemote;
+ loadLocalStorage();
+ updateUI();
+ clearTimeout(reloadTimer);
+ } else {
+ reloadTimer = setTimeout(loadRemote, 1000);
}
}
};
if (this.readyState == 4) {
if (this.status == 200) {
update(id, this.responseText);
- updateNextStep();
+ updateUI();
} else {
updateRemote = undefined;
- update("remote", "<center>Server offline...</center>");
- document.getElementById("local").style.display = "block";
+ updateUI();
+
if (localStorage) {
var now = Date.now();
var fuck = JSON.stringify({
function login(id) {
ajax("POST", "login", id, "user");
loggedIn = true;
- document.getElementById("local").style.display = "none";
+ clearTimeout(logoutTimer);
+ timeToLogout = undefined;
}
function logout() {
sendReset();
ajax("GET", "logout", "", "user");
loggedIn = false;
- document.getElementById("local").style.display = "block";
+ timeToLogout = undefined;
+}
+
+function countingTimeLogout(count_time)
+{
+ timeToLogout = count_time;
+ updateUI();
+ if (count_time == 0) {
+ logout();
+ } else {
+ logoutTimer = setTimeout(function() { countingTimeLogout(count_time - 1); }, 1000);
+ }
}
function renameUser() {
function addCoffee(flavor) {
var data = JSON.stringify({
time: new Date().toISOString(),
- flavor: flavor
+ flavor: flavor,
+ uid: id_user
});
if (loggedIn) {
ajax("POST", "coffee/add", data, "user");
flavorChosen = flavor;
loggedIn = false;
+ countingTimeLogout(10); //mean 10 seconds
}
}