/////////////////////
/// Parameter ///
var moistureMin = 15;
var moistureMax = 60;
var fertilityMin = 350;
var fertilityMax = 2000;
var temperatureMin = 10;
var temperatureMax = 32;
/*var lightMin = 1500;
var lightMax = 6000;*/
////////////////////////////
/// Lokale Variablen ///
var timer = {};
var timeouts = {};
var moistureArray;
var fertilityArray;
var temperatureArray;
var timerState = "javascript.0.plantTimer";
///////////////////////////////////
/// Variablen initalisieren ///
if(getState(timerState).notExist)
{
createState("plantTimer", {type: "object"});
}
setTimeout(function() {getPlants();}, 1000);
//////////////////////
/// Monitoring ///
setTimeout(function() {
//Moisture-Check
on({id: moistureArray, change: "ne"}, function (obj) {
timer = getState(timerState).val;
if(!timer)
return;
if(getState(obj.id).val < moistureMin && !timer[obj.id])
{
var raum = getObject(obj.id, 'rooms').enumNames[0].de;
var pflanzenName = getObject(obj.id.replace('.moisture', "")).common.name.replace("Flower care - ", "");
sendTo('telegram.0', 'Du solltest deinen ' + pflanzenName + ' im ' + raum + ' dringend gießen.');
timer[obj.id] = true;
setState(timerState, {val: timer});
timeouts[obj.id] = setTimeout(function() {boolTimer(obj.id)}, 14400000);
}
});
//Fertility-Check
on({id: fertilityArray, change: "ne"}, function (obj) {
timer = getState(timerState).val;
if(!timer)
return;
if(getState(obj.id).val < fertilityMin && !timer[obj.id])
{
var raum = getObject(obj.id, 'rooms').enumNames[0].de;
var pflanzenName = getObject(obj.id.replace('.fertility', "")).common.name.replace("Flower care - ", "");
sendTo('telegram.0', 'Dein ' + pflanzenName + ' im ' + raum + ' könnte etwas Dünger vertragen.');
timer[obj.id] = true;
setState(timerState, {val: timer});
timeouts[obj.id] = setTimeout(function() {boolTimer(obj.id)}, 14400000);
}
});
//Temprature-Check
on({id: temperatureArray, change: "ne"}, function (obj) {
timer = getState(timerState).val;
if(!timer)
return;
if(getState(obj.id).val < temperatureMin && !timer[obj.id])
{
var raum = getObject(obj.id, 'rooms').enumNames[0].de;
var pflanzenName = getObject(obj.id.replace('.temperature', "")).common.name.replace("Flower care - ", "");
sendTo('telegram.0', 'Deinem ' + pflanzenName + ' im ' + raum + ' ist es etwas zu kalt.');
timer[obj.id] = true;
setState(timerState, {val: timer});
timeouts[obj.id] = setTimeout(function() {boolTimer(obj.id)}, 14400000);
}
});
schedule("0 0 * * *", function()
{
getPlants();
});
}, 2000);
function boolTimer(id){
timer = getState(timerState).val;
timer[id] = false;
log("Timer: " + id + " wurde zurückgeetzt!");
setState(timerState, {val: timer});
}
function getPlants()
{
moistureArray = [];
fertilityArray = [];
temperatureArray = [];
timer = getState(timerState).val;
if(!timer)
{
timer = {};
}
var plants = getObject("enum.functions.plant").common.members;
for(let i = 0; i < plants.length; i++) {
var moisture = plants[i] + '.moisture';
var fertility = plants[i] + '.fertility';
var temperature = plants[i] + '.temperature';
moistureArray.push(moisture);
if(timer[moisture] == null)
{
timer[moisture] = false;
}
try {
if (timer[moisture] && timeouts[moisture]._destroyed)
{
timeouts[moisture] = setTimeout(function() {boolTimer(moisture)}, 14400000);
}
}
catch(err) {
if(getState(moisture).val < moistureMin)
{
timer[moisture] = false;
}
if (timer[moisture])
{
timeouts[moisture] = setTimeout(function() {boolTimer(moisture)}, 14400000);
}
}
fertilityArray.push(fertility);
if(timer[fertility] == null)
{
timer[fertility] = false;
}
try {
if (timer[fertility] && timeouts[fertility]._destroyed)
{
timeouts[fertility] = setTimeout(function() {boolTimer(fertility)}, 14400000);
}
}
catch(err) {
if(getState(fertility).val < fertilityMin)
{
timer[fertility] = false;
}
if (timer[fertility])
{
timeouts[fertility] = setTimeout(function() {boolTimer(fertility)}, 14400000);
}
}
temperatureArray.push(temperature);
if(timer[temperature] == null)
{
timer[temperature] = false;
}
try {
if (timer[temperature] && timeouts[temperature]._destroyed)
{
log(temperature);
timeouts[temperature] = setTimeout(function() {boolTimer(temperature)}, 14400000);
}
}
catch(err) {
if(getState(temperature).val < temperatureMin)
{
timer[temperature] = false;
}
if (timer[temperature])
{
log(temperature);
timeouts[temperature] = setTimeout(function() {boolTimer(temperature)}, 14400000);
}
}
}
log(timer);
setState(timerState, {val: timer});
}