From ebee9dd9736aee12320062d9ff29a82f78f23e8b Mon Sep 17 00:00:00 2001 From: Mark Williams <24956497+ndg63276@users.noreply.github.com> Date: Mon, 2 Nov 2020 21:44:22 +0000 Subject: [PATCH 1/6] Fix autorefresh behaviour --- functions.js | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/functions.js b/functions.js index 74de883..fc017a0 100644 --- a/functions.js +++ b/functions.js @@ -29,6 +29,7 @@ $( document ).ready(function() { localStorage.autoRefresh = $(this).prop("checked"); checkAutorefresh(); }); + checkAutorefresh(); }); function login(username, password, region, storecreds) { @@ -184,14 +185,14 @@ function check_login(user_info) { function readLocalStorage(){ // Not initialized - if(localStorage.autoRefresh == null){ - localStorage.autoRefresh = true; + if (localStorage.autoRefresh == null) { + localStorage.autoRefresh = "true"; localStorage.theme = "a"; } - $('#autorefresh').prop( "checked", localStorage.autoRefresh === "true").checkboxradio( "refresh" ); - if(localStorage.theme !== "a") + if (localStorage.theme !== "a") { checkTheme(); + } } function checkTheme(){ @@ -201,8 +202,9 @@ function checkTheme(){ function checkAutorefresh(){ clearInterval(autoRefreshTimer); - if($("#autorefresh").prop("checked") && user_info["logged_in"] === true) + if (localStorage.autoRefresh === "true" && user_info["logged_in"] === true) { autoRefreshTimer = setInterval(update_devices, 30_000, user_info, true); + } } function on_login(user_info) { @@ -259,9 +261,8 @@ function add_or_update_switch(device, device_no){ var type = device["dev_type"]; var currentActionDiv = $('#action_'+ device_id); - if(currentActionDiv.length === 0){ + if (currentActionDiv.length === 0) { var deviceDiv = createElement("div", "gridElem singleSwitch borderShadow ui-btn ui-btn-up-b ui-btn-hover-b"); - var nameDiv = createElement("div", "switchName"); nameDiv.innerHTML = name; var imgDiv = createElement("div", "switchImg"); @@ -269,20 +270,16 @@ function add_or_update_switch(device, device_no){ var actionDiv = createElement("div", null); actionDiv.setAttribute("id", "action_" + device_id); actionDiv.innerHTML = createActionLink(device_no, online, state, type); - deviceDiv.appendChild(imgDiv); deviceDiv.appendChild(nameDiv); deviceDiv.appendChild(actionDiv); - $('#switches')[0].appendChild(deviceDiv); - } - else{ + } else { var parentDiv = currentActionDiv.parent()[0]; currentActionDiv.remove(); var newActionDiv = createElement("div", null); newActionDiv.setAttribute("id", "action_" + device_id); newActionDiv.innerHTML = createActionLink(device_no, online, state, type); - parentDiv.appendChild(newActionDiv); } } @@ -299,16 +296,17 @@ function createActionLink(device, online, state, type){ } function createImg(icon, name, type){ - if(isNullOrEmpty(icon)) + if (isNullOrEmpty(icon)) { return "

" + type + "

"; + } return "" + name + ""; } function createElement(typeName, className){ var elem = document.createElement(typeName); - if(!isNullOrEmpty(className)) + if (!isNullOrEmpty(className)) { elem.className = className; - + } return elem; } From 2612a2eaca4da006364146ac412b1abf9cacdc40 Mon Sep 17 00:00:00 2001 From: Mark Williams <24956497+ndg63276@users.noreply.github.com> Date: Tue, 3 Nov 2020 21:59:15 +0000 Subject: [PATCH 2/6] Add refresh_token function (not yet used) --- functions.js | 71 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/functions.js b/functions.js index fc017a0..b2df669 100644 --- a/functions.js +++ b/functions.js @@ -12,12 +12,16 @@ $( document ).ready(function() { do_login(); } }; - user_info = {"access_token": getCookie("access_token")}; + user_info = { + "access_token": getCookie("access_token"), + "sl_refresh_token": getCookie("sl_refresh_token"), + "sl_expires_in": getCookie("sl_expires_in") + }; console.log(user_info); - logged_in = check_login(user_info); + logged_in = check_login(); if (logged_in["success"] === true) { user_info["devices"] = logged_in["devices"]; - on_login(user_info); + on_login(); user_info["logged_in"] = true; } else { on_logout(); @@ -33,7 +37,6 @@ $( document ).ready(function() { }); function login(username, password, region, storecreds) { - var to_return = {}; var url = baseurl + "auth.do"; var headers = { "Content-Type": "application/x-www-form-urlencoded" @@ -53,20 +56,26 @@ function login(username, password, region, storecreds) { dataType: "json", async: false, success: function (json) { - console.log(json); - if ("access_token" in json) { - to_return["access_token"] = json["access_token"]; - to_return["logged_in"] = true; - if (storecreds === true) { - setCookie("access_token", json["access_token"], json["expires_in"]/3600); - } - } + store_tokens(json, storecreds); } }); - return to_return; } -function get_device_list(user_info) { +function store_tokens(json, storecreds) { + if ("access_token" in json) { + user_info["access_token"] = json["access_token"]; + user_info["sl_refresh_token"] = json["refresh_token"]; + user_info["sl_expires_in"] = Date.now() + json["expires_in"]*1000; + user_info["logged_in"] = true; + if (storecreds === true) { + setCookie("access_token", json["access_token"], json["expires_in"]/3600); + setCookie("sl_refresh_token", json["refresh_token"], json["expires_in"]/3600); + setCookie("sl_expires_in", Date.now() + json["expires_in"]*1000, json["expires_in"]/3600); + } + } +} + +function get_device_list() { to_return = {}; var url; if (user_info["access_token"].substring(0,2) === "EU") { @@ -111,7 +120,7 @@ function get_device_list(user_info) { return to_return } -function switch_device(device, user_info, new_state) { +function switch_device(device, new_state) { to_return = {}; var url; if (user_info["access_token"].substring(0,2) === "EU") { @@ -150,6 +159,22 @@ function switch_device(device, user_info, new_state) { return to_return } +function refresh_token() { + url = baseurl + "access.do"; + params = { "grant_type": "refresh_token", "refresh_token": user_info["sl_refresh_token"] } + $.ajax({ + url: proxyurl+url, + type: "GET", + data: params, + dataType: "json", + async: false, + success: function (json) { + console.log(json); + new_info = store_tokens(json, true); + } + }); +} + function do_login() { var login_div = document.getElementById("login"); login_div.classList.add("hidden"); @@ -160,11 +185,11 @@ function do_login() { var region = document.getElementById("region").value; var storecreds = document.getElementById("storecreds").checked; setTimeout(function(){ - user_info = login(username, password, region, storecreds); + login(username, password, region, storecreds); if (user_info["logged_in"] === true) { - device_list = get_device_list(user_info); + device_list = get_device_list(); user_info["devices"] = device_list["devices"] - on_login(user_info); + on_login(); } else { on_logout(); document.getElementById("loginfailed").innerHTML = "Login failed"; @@ -172,10 +197,10 @@ function do_login() { }, 100); } -function check_login(user_info) { +function check_login() { if (user_info["access_token"] !== "") { console.log("Getting devices"); - device_list = get_device_list(user_info); + device_list = get_device_list(); return device_list; } else { console.log("No access_token"); @@ -207,7 +232,7 @@ function checkAutorefresh(){ } } -function on_login(user_info) { +function on_login() { var login_div = document.getElementById("login"); login_div.classList.add("hidden"); var switches = document.getElementById("switches"); @@ -220,7 +245,7 @@ function on_login(user_info) { function update_devices(user_info, force_update) { if (force_update === true) { - device_list = get_device_list(user_info); + device_list = get_device_list(); user_info["devices"] = device_list["devices"]; $('#switches').html(''); } @@ -238,7 +263,7 @@ function toggle(device_no) { } else { new_state = 0; } - switch_device(device, user_info, new_state); + switch_device(device, new_state); device["data"]["state"] = ! state; add_or_update_switch(device, device_no); } From c19b061911839f9abe1057d53f01c576340fa585 Mon Sep 17 00:00:00 2001 From: Mark Williams <24956497+ndg63276@users.noreply.github.com> Date: Tue, 10 Nov 2020 16:25:00 +0000 Subject: [PATCH 3/6] Add logout button --- functions.js | 4 ++++ index.html | 3 +++ 2 files changed, 7 insertions(+) diff --git a/functions.js b/functions.js index b2df669..9e129f0 100644 --- a/functions.js +++ b/functions.js @@ -339,3 +339,7 @@ function isNullOrEmpty(entry){ return entry == null || entry === ''; } +function logout() { + setCookie("access_token", "", -1); + location.reload(); +} diff --git a/index.html b/index.html index 4c520a9..5472ef6 100644 --- a/index.html +++ b/index.html @@ -76,6 +76,9 @@
+
+ +
From 7159e61f1b3969a4df44928c5d9688cc203bbdf9 Mon Sep 17 00:00:00 2001 From: Mark Williams <24956497+ndg63276@users.noreply.github.com> Date: Wed, 11 Nov 2020 21:21:24 +0000 Subject: [PATCH 4/6] Add logout button --- functions.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/functions.js b/functions.js index 9e129f0..5b3439e 100644 --- a/functions.js +++ b/functions.js @@ -341,5 +341,7 @@ function isNullOrEmpty(entry){ function logout() { setCookie("access_token", "", -1); + setCookie("sl_refresh_token", "", -1); + setCookie("sl_expires_in", "", -1); location.reload(); } From b8d9e25fa6127b972125ee9dfe02705ca2f3e322 Mon Sep 17 00:00:00 2001 From: Mark Williams Date: Mon, 23 Nov 2020 15:38:08 +0000 Subject: [PATCH 5/6] Refresh access token every 31 seconds, before discovery --- functions.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/functions.js b/functions.js index 5b3439e..e4e4961 100644 --- a/functions.js +++ b/functions.js @@ -75,7 +75,10 @@ function store_tokens(json, storecreds) { } } -function get_device_list() { +function get_device_list(refresh_access_token) { + if (refresh_access_token === true) { + refresh_token(); + } to_return = {}; var url; if (user_info["access_token"].substring(0,2) === "EU") { @@ -187,7 +190,7 @@ function do_login() { setTimeout(function(){ login(username, password, region, storecreds); if (user_info["logged_in"] === true) { - device_list = get_device_list(); + device_list = get_device_list(false); user_info["devices"] = device_list["devices"] on_login(); } else { @@ -200,7 +203,7 @@ function do_login() { function check_login() { if (user_info["access_token"] !== "") { console.log("Getting devices"); - device_list = get_device_list(); + device_list = get_device_list(false); return device_list; } else { console.log("No access_token"); @@ -228,7 +231,7 @@ function checkTheme(){ function checkAutorefresh(){ clearInterval(autoRefreshTimer); if (localStorage.autoRefresh === "true" && user_info["logged_in"] === true) { - autoRefreshTimer = setInterval(update_devices, 30_000, user_info, true); + autoRefreshTimer = setInterval(update_devices, 31_000, user_info, true); } } @@ -245,7 +248,7 @@ function on_login() { function update_devices(user_info, force_update) { if (force_update === true) { - device_list = get_device_list(); + device_list = get_device_list(true); user_info["devices"] = device_list["devices"]; $('#switches').html(''); } From 47c42aee914e5d0c79dafe1ef40364667e4bef44 Mon Sep 17 00:00:00 2001 From: Mark Williams <24956497+ndg63276@users.noreply.github.com> Date: Tue, 24 Nov 2020 21:46:44 +0000 Subject: [PATCH 6/6] Hide buttons until logged in --- functions.js | 4 +++- index.html | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/functions.js b/functions.js index e4e4961..1afefdd 100644 --- a/functions.js +++ b/functions.js @@ -207,7 +207,7 @@ function check_login() { return device_list; } else { console.log("No access_token"); - return false; + return {"success": false}; } } @@ -240,6 +240,8 @@ function on_login() { login_div.classList.add("hidden"); var switches = document.getElementById("switches"); switches.classList.remove("hidden"); + var buttons = document.getElementById("buttons"); + buttons.classList.remove("hidden"); var loader_div = document.getElementById("loader"); loader_div.classList.add("hidden"); update_devices(user_info, false); diff --git a/index.html b/index.html index 5472ef6..3dbd521 100644 --- a/index.html +++ b/index.html @@ -66,6 +66,7 @@
+