From d7def17e256adf327aa2a358966deae8f7863b24 Mon Sep 17 00:00:00 2001
From: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
Date: Mon, 17 Jan 2022 12:32:10 +1300
Subject: [PATCH] Add number setting to web_server (#1804)
---
Makefile | 4 ++--
_static/webserver-v1.js | 9 +++++++++
_static/webserver-v1.min.js | 2 +-
web-api/index.rst | 22 ++++++++++++++++++++++
4 files changed, 34 insertions(+), 3 deletions(-)
diff --git a/Makefile b/Makefile
index ddd1468da..3c9fcae97 100644
--- a/Makefile
+++ b/Makefile
@@ -10,8 +10,8 @@ html-strict:
sphinx-build -M html . _build -W -j auto -n $(O)
minify:
- minify -o _static/webserver-v1.min.js _static/webserver-v1.js
- minify -o _static/webserver-v1.min.css _static/webserver-v1.css
+ minify _static/webserver-v1.js > _static/webserver-v1.min.js
+ minify _static/webserver-v1.css > _static/webserver-v1.min.css
cleanhtml:
rm -rf "_build/html/*"
diff --git a/_static/webserver-v1.js b/_static/webserver-v1.js
index 36898d887..1f66a04a9 100644
--- a/_static/webserver-v1.js
+++ b/_static/webserver-v1.js
@@ -83,6 +83,15 @@ for (; row = states.rows[i]; i++) {
});
})(row.id);
}
+ if (row.classList.contains("number")) {
+ (function(id) {
+ row.children[2].children[0].addEventListener('change', function () {
+ const xhr = new XMLHttpRequest();
+ xhr.open("POST", '/number/' + id.substr(7) + '/set?value=' + encodeURIComponent(this.value), true);
+ xhr.send();
+ });
+ })(row.id);
+ }
if (row.classList.contains("button")) {
(function(id) {
row.children[2].children[0].addEventListener('click', function () {
diff --git a/_static/webserver-v1.min.js b/_static/webserver-v1.min.js
index f2dddc114..1b5110716 100644
--- a/_static/webserver-v1.min.js
+++ b/_static/webserver-v1.min.js
@@ -1 +1 @@
-const source=new EventSource("/events");source.addEventListener("log",(function(t){const n=document.getElementById("log");let e="";t.data.startsWith("[1;31m")?e="e":t.data.startsWith("[0;33m")?e="w":t.data.startsWith("[0;32m")?e="i":t.data.startsWith("[0;35m")?e="c":t.data.startsWith("[0;36m")?e="d":t.data.startsWith("[0;37m")?e="v":n.innerHTML+=t.data+"\n",n.innerHTML+=''+t.data.substr(7,t.data.length-10)+"\n"})),source.addEventListener("state",(function(t){const n=JSON.parse(t.data);document.getElementById(n.id).children[1].innerText=n.state}));const states=document.getElementById("states");let row,i=0;for(;row=states.rows[i];i++)row.children[2].children.length&&(row.classList.contains("switch")&&function(t){row.children[2].children[0].addEventListener("click",(function(){const n=new XMLHttpRequest;n.open("POST","/switch/"+t.substr(7)+"/toggle",!0),n.send()}))}(row.id),row.classList.contains("fan")&&function(t){row.children[2].children[0].addEventListener("click",(function(){const n=new XMLHttpRequest;n.open("POST","/fan/"+t.substr(4)+"/toggle",!0),n.send()}))}(row.id),row.classList.contains("light")&&function(t){row.children[2].children[0].addEventListener("click",(function(){const n=new XMLHttpRequest;n.open("POST","/light/"+t.substr(6)+"/toggle",!0),n.send()}))}(row.id),row.classList.contains("cover")&&function(t){row.children[2].children[0].addEventListener("click",(function(){const n=new XMLHttpRequest;n.open("POST","/cover/"+t.substr(6)+"/open",!0),n.send()})),row.children[2].children[1].addEventListener("click",(function(){const n=new XMLHttpRequest;n.open("POST","/cover/"+t.substr(6)+"/close",!0),n.send()}))}(row.id),row.classList.contains("select")&&function(t){row.children[2].children[0].addEventListener("change",(function(){const n=new XMLHttpRequest;n.open("POST","/select/"+t.substr(7)+"/set?option="+encodeURIComponent(this.value),!0),n.send()}))}(row.id),row.classList.contains("button")&&function(t){row.children[2].children[0].addEventListener("click",(function(){const n=new XMLHttpRequest;n.open("POST","/button/"+t.substr(7)+"/press",!0),n.send()}))}(row.id));
+const source=new EventSource("/events");source.addEventListener("log",(function(t){const n=document.getElementById("log");let e="";t.data.startsWith("[1;31m")?e="e":t.data.startsWith("[0;33m")?e="w":t.data.startsWith("[0;32m")?e="i":t.data.startsWith("[0;35m")?e="c":t.data.startsWith("[0;36m")?e="d":t.data.startsWith("[0;37m")?e="v":n.innerHTML+=t.data+"\n",n.innerHTML+=''+t.data.substr(7,t.data.length-10)+"\n"})),source.addEventListener("state",(function(t){const n=JSON.parse(t.data);document.getElementById(n.id).children[1].innerText=n.state}));const states=document.getElementById("states");let row,i=0;for(;row=states.rows[i];i++)row.children[2].children.length&&(row.classList.contains("switch")&&function(t){row.children[2].children[0].addEventListener("click",(function(){const n=new XMLHttpRequest;n.open("POST","/switch/"+t.substr(7)+"/toggle",!0),n.send()}))}(row.id),row.classList.contains("fan")&&function(t){row.children[2].children[0].addEventListener("click",(function(){const n=new XMLHttpRequest;n.open("POST","/fan/"+t.substr(4)+"/toggle",!0),n.send()}))}(row.id),row.classList.contains("light")&&function(t){row.children[2].children[0].addEventListener("click",(function(){const n=new XMLHttpRequest;n.open("POST","/light/"+t.substr(6)+"/toggle",!0),n.send()}))}(row.id),row.classList.contains("cover")&&function(t){row.children[2].children[0].addEventListener("click",(function(){const n=new XMLHttpRequest;n.open("POST","/cover/"+t.substr(6)+"/open",!0),n.send()})),row.children[2].children[1].addEventListener("click",(function(){const n=new XMLHttpRequest;n.open("POST","/cover/"+t.substr(6)+"/close",!0),n.send()}))}(row.id),row.classList.contains("select")&&function(t){row.children[2].children[0].addEventListener("change",(function(){const n=new XMLHttpRequest;n.open("POST","/select/"+t.substr(7)+"/set?option="+encodeURIComponent(this.value),!0),n.send()}))}(row.id),row.classList.contains("number")&&function(t){row.children[2].children[0].addEventListener("change",(function(){const n=new XMLHttpRequest;n.open("POST","/number/"+t.substr(7)+"/set?value="+encodeURIComponent(this.value),!0),n.send()}))}(row.id),row.classList.contains("button")&&function(t){row.children[2].children[0].addEventListener("click",(function(){const n=new XMLHttpRequest;n.open("POST","/button/"+t.substr(7)+"/press",!0),n.send()}))}(row.id));
diff --git a/web-api/index.rst b/web-api/index.rst
index 1fb23fc1b..4ef75a961 100644
--- a/web-api/index.rst
+++ b/web-api/index.rst
@@ -275,3 +275,25 @@ Button
******
A button can be ``pressed`` from the REST API by sending a POST request to ``button/do_something/press``.
+
+Number
+******
+
+Numbers can be set to a value within their minimum and maximum range and will return their current value. For example sending
+a GET request to ``/number/desired_delay`` could yield this payload:
+
+.. code-block:: json
+
+ {
+ "id": "number-desired_delay",
+ "state": "20.0000",
+ "value": 20
+ }
+
+POST requests on the other hand allow setting the number, the available
+method is ``set``. The following parameter can be used:
+
+- **value**: The value you want to set the number to. The value must be within the
+ minimum and maximum range of the number otherwise it will be ignored.
+
+For example POST ``/number/desired_delay/set?value=24`` will set the number to 24.