From 35a02e8fa798f05389d97e392e3864ee9f93f5bc Mon Sep 17 00:00:00 2001 From: l_samenv Date: Wed, 4 Sep 2024 12:56:35 +0200 Subject: [PATCH] Post getvars + fixed colors.py valid hex --- client/jsFiles/SEAWebClientCommunication.js | 32 +++++++++++++++++++-- client/jsFiles/SEAWebClientGraphics.js | 20 +++++++++++-- colors.py | 2 +- seaweb.py | 4 +-- 4 files changed, 50 insertions(+), 8 deletions(-) diff --git a/client/jsFiles/SEAWebClientCommunication.js b/client/jsFiles/SEAWebClientCommunication.js index 14b8a98..f092f43 100644 --- a/client/jsFiles/SEAWebClientCommunication.js +++ b/client/jsFiles/SEAWebClientCommunication.js @@ -270,6 +270,34 @@ function reqJSON(s, url, successHandler, errorHandler) { xhr.send(); } +function reqJSONPOST(s, url, parameters, successHandler, errorHandler) { + var xhr = typeof XMLHttpRequest != 'undefined' ? new XMLHttpRequest() + : new ActiveXObject('Microsoft.XMLHTTP'); + if (debugCommunication) { + console.log("%cto server (reqJSON): " + url, + "color:white;background:lightgreen"); + } + xhr.open('post', url, true); + xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); + xhr.onreadystatechange = function() { + // console.log(xhr) + var status; + var data; + // https://xhr.spec.whatwg.org/#dom-xmlhttprequest-readystate + if (xhr.readyState == 4) { // `DONE` + status = xhr.status; + if (status == 200) { + data = JSON.parse(xhr.responseText); + successHandler && successHandler(s, data); + } else { + errorHandler && errorHandler(status); + } + } + }; + xhr.send(parameters); +} + + function successHandler(s, message) { // Handles incoming XMLHttp-messages depending on type of message. // s: slide number or -1 for replacing slide in all slider instances @@ -350,8 +378,8 @@ function successHandler(s, message) { begin = timeRange[0] - timeRange[1]; select.value = begin; // Server-request for variable-list.*/ - reqJSON(0, "http://" + hostPort + "/getvars?time=" + timeRange + "&userconfiguration=" + encodeURIComponent(JSON.stringify(getFormattedUserConfigurationFromLocalStorage())) + "&id=" - + clientID, successHandler, errorHandler); + reqJSONPOST(0, "http://" + hostPort + "/getvars", "time=" + timeRange + "&userconfiguration=" + JSON.stringify(getFormattedUserConfigurationFromLocalStorage()) + "&id=" + + clientID, successHandler, errorHandler); break; // Response to a "getvars"-server-request. case "var_list": diff --git a/client/jsFiles/SEAWebClientGraphics.js b/client/jsFiles/SEAWebClientGraphics.js index 0c1c388..c26384b 100644 --- a/client/jsFiles/SEAWebClientGraphics.js +++ b/client/jsFiles/SEAWebClientGraphics.js @@ -96,6 +96,20 @@ function AJAX(addr){ }); } + this.postForm = function(args){ + xhr.open("POST", addr, true); + xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); + xhr.send(args); + return new Promise(function(resolve, reject){ + xhr.addEventListener('load', function(){ + if(this.status == 200){ + if (debugCommunication) console.log('A RES', JSON.parse(this.responseText)); + resolve(JSON.parse(this.responseText)); + } + }); + }); + } + this.get = function(responseType = "text"){ xhr.open("GET", addr, true); xhr.responseType = responseType; @@ -831,7 +845,7 @@ let graphs = (function (){ msRightTimestampGetVars = dateTimestampMs + timeValueMs; msRightTimestampGetGraph = dateTimestampMs + 24*60*60*1000; - AJAX("http://" + hostPort + "/getvars?time=" + msLeftTimestampGetVars/1000 + "," + msRightTimestampGetVars/1000 + "&userconfiguration=" + encodeURIComponent(JSON.stringify(getFormattedUserConfigurationFromLocalStorage())) + "&id="+ clientID).getJSON().then(function(data){ + AJAX("http://" + hostPort + "/getvars").postForm("time=" + msLeftTimestampGetVars/1000 + "," + msRightTimestampGetVars/1000 + "&userconfiguration=" + JSON.stringify(getFormattedUserConfigurationFromLocalStorage()) + "&id="+ clientID).then(function(data){ blocks = data.blocks; document.getElementById("device").innerHTML = data.device maxTime = msRightTimestampGetGraph; @@ -901,7 +915,7 @@ let graphs = (function (){ let msLeftTimestamp = msRightTimestamp - 30*60*1000; cursorLine(null); - AJAX("http://" + hostPort + "/getvars?time=" + msLeftTimestamp/1000 + "," + msRightTimestamp/1000 + "&userconfiguration=" + encodeURIComponent(JSON.stringify(getFormattedUserConfigurationFromLocalStorage())) + "&id="+ clientID).getJSON().then(function(data){ + AJAX("http://" + hostPort + "/getvars").postForm("time=" + msLeftTimestamp/1000 + "," + msRightTimestamp/1000 + "&userconfiguration=" + JSON.stringify(getFormattedUserConfigurationFromLocalStorage()) + "&id="+ clientID).then(function(data){ currentMaxTime = msRightTimestamp + 60000; currentMinTime = msLeftTimestamp; @@ -1212,7 +1226,7 @@ let graphs = (function (){ function applySettingsCallback(userConfiguration){ cursorLine(null); - AJAX("http://" + hostPort + "/getvars?time=" + currentMinTime/1000 + "," + currentMaxTime/1000 + "&userconfiguration=" + encodeURIComponent(JSON.stringify(userConfiguration)) + "&id="+ clientID).getJSON().then(function(data){ + AJAX("http://" + hostPort + "/getvars").postForm("time=" + currentMinTime/1000 + "," + currentMaxTime/1000 + "&userconfiguration=" + JSON.stringify(userConfiguration) + "&id="+ clientID).then(function(data){ blocks = data.blocks; document.getElementById("device").innerHTML = data.device maxTime = currentMaxTime; diff --git a/colors.py b/colors.py index fdd479d..1147f26 100644 --- a/colors.py +++ b/colors.py @@ -104,7 +104,7 @@ class ColorMap(object): if len(code) != 7: return None try: - int(code[1:]) # we have a valid hex color code + int(code[1:], 16) # we have a valid hex color code return code except ValueError: return None diff --git a/seaweb.py b/seaweb.py index b66a129..0cf8e78 100755 --- a/seaweb.py +++ b/seaweb.py @@ -155,9 +155,9 @@ def export(): @app.route('/graph') @app.route('/updategraph') @app.route('/gettime') -@app.route('/getvars') +@app.route('/getvars', methods=["GET", "POST"]) def reply(): - args = flask.request.args + args = flask.request.values kwargs = dict((k, args.get(k)) for k in args) path = flask.request.path logging.info('GET %s %s', path, repr(kwargs))