gardencontrol/greenhouse/static/scripts/index.js

67 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-05-05 10:30:56 +00:00
var on_switch_heat = function() {
var state = true;
if(document.getElementById("heat_switch").value == "ausschalten") {
state = false;
}
var json_str = JSON.stringify({"id": "1", "heat": state});
patch_sample(json_str);
}
var on_switch_water = function() {
var state = true;
if(document.getElementById("water_switch").value == "ausschalten") {
state = false;
}
var json_str = JSON.stringify({"id": "1", "water": state});
patch_sample(json_str);
}
var patch_http = new XMLHttpRequest();
var patch_sample = function(sample) {
patch_http.abort();
patch_http.open("PATCH", "/sample");
patch_http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
patch_http.send(sample);
}
var get_sample = function (event) {
var sample = JSON.parse(event)
output = "einschalten"
out_state = "aus"
if(sample.water) {
output = "ausschalten"
out_state = "an"
}
document.getElementById("temperature_value").innerHTML = sample.temperature + " °C";
document.getElementById("water_switch").value = output;
document.getElementById("water_state").innerHTML = out_state;
if(sample.heat) {
output = "ausschalten"
out_state = "an"
} else {
output = "einschalten"
out_state = "aus"
}
document.getElementById("heat_switch").value = output;
document.getElementById("heat_state").innerHTML = out_state;
}
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
get_sample(http.responseText);
setTimeout(function () {
http.open("GET", 'sample');
http.send();
}, 500);
}
} else {
// request error
}
}
http.open("GET", "sample");
http.send();