home/home/static/scripts/tomatentuppen.js
2022-06-08 11:46:01 +02:00

139 lines
4.1 KiB
JavaScript

var on_switch_water = function() {
var state = true;
if(document.getElementById("water_switch").value == "ausschalten") {
state = false;
}
var json_str = JSON.stringify(
{
"id": "tomatentuppen",
"waterstate": state
}
);
patch_sample(json_str);
}
var on_switch_auto_state = function() {
var state = true;
if(document.getElementById("auto_switch").value == "ausschalten") {
state = false;
}
var json_str = JSON.stringify(
{
"id": "tomatentuppen",
"water":
{
"autostate": state
}
}
);
patch_config(json_str);
}
var on_change_config = function() {
var on_time = document.getElementById("water_on").value;
var off_time = document.getElementById("water_off").value;
var json_str = JSON.stringify(
{
"id": "tomatentuppen",
"water":
{
"times": [
{
"on_time": on_time,
"off_time": off_time
}
]
}
}
);
patch_config(json_str);
}
var http_patch_config = new XMLHttpRequest();
http_patch_config.onreadystatechange = function () {
if (http_patch_config.readyState === 4) {
var status = http_patch_config.status;
if (status === 0 || (status >= 200 && status < 400)) {
// The request has been completed successfully
parse_config(http_patch_config.responseText);
}
} else {
// request error
}
}
var patch_config = function(config) {
http_patch_config.abort();
http_patch_config.open("PATCH", "/config/tomatentuppen");
http_patch_config.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
http_patch_config.send(config);
}
var patch_http = new XMLHttpRequest();
var patch_sample = function(sample) {
patch_http.abort();
patch_http.open("PATCH", "/sample/tomatentuppen");
patch_http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
patch_http.send(sample);
}
var parse_config = function (event) {
var config = JSON.parse(event)
var output = "einschalten"
var visibility = 'hidden'
if(config.water) {
if(config.water.autostate) {
output = "ausschalten"
visibility = "visible"
document.getElementById("water_on").value = config.water.times[0].on_time;
document.getElementById("water_off").value = config.water.times[0].off_time;
}
document.getElementById("auto_switch").value = output;
document.getElementById("water_times").style.visibility = visibility
}
}
var parse_sample = function (event) {
var sample = JSON.parse(event)
if(sample.water && sample.water.id == 'tomatentuppen') {
var switch_caption = "einschalten";
if(sample.water.state) {
switch_caption = "ausschalten";
}
document.getElementById("water_switch").value = switch_caption;
}
}
var http = new XMLHttpRequest();
http.onreadystatechange = function () {
if (http.readyState === 4) {
var status = http.status;
if (status === 0 || (status >= 200 && status < 400)) {
// The request has been completed successfully
parse_sample(http.responseText);
setTimeout(function () {
http.open("GET", 'sample/tomatentuppen');
http.send();
}, 500);
}
} else {
// request error
}
}
http.open("GET", "sample/tomatentuppen");
http.send();
var http_get_config = new XMLHttpRequest();
http_get_config.onreadystatechange = function () {
if (http_get_config.readyState === 4) {
var status = http_get_config.status;
if (status === 0 || (status >= 200 && status < 400)) {
// The request has been completed successfully
parse_config(http_get_config.responseText);
}
} else {
// request error
}
}
http_get_config.open("GET", "config/tomatentuppen");
http_get_config.send();