Ich hab mal eine kleine Methode implementiert, die das realisiert.
Einfach als Globale JS Funktion einfügen:
var historyValuesOBJ ;
function compareHistoryValue(currentValueFLT, operatorSTR, compareValueFLT, durationINT, identifierSTR) {
console.log(currentValueFLT + " " + operatorSTR + " " + compareValueFLT + " For " + durationINT + "s");
//Current timestamp
var currentTimeTST = Math.round(new Date().getTime() / 1000);
//check if the global object exists and create it if not
if (typeof historyValuesOBJ == "undefined") {
historyValuesOBJ = {};
historyValuesOBJ[identifierSTR] = {};
historyValuesOBJ[identifierSTR].lastCallINT = currentTimeTST;
historyValuesOBJ[identifierSTR].countINT = 0;
}
//Calculate the delata since last Call
var deltaTimeINT = currentTimeTST - historyValuesOBJ[identifierSTR].lastCallINT;
//Add the dealte to the count value
historyValuesOBJ[identifierSTR].countINT = historyValuesOBJ[identifierSTR].countINT + deltaTimeINT;
//set the last call to current timestamp
historyValuesOBJ[identifierSTR].lastCallINT = currentTimeTST;
console.log("Time since Last Call: " + historyValuesOBJ[identifierSTR].countINT );
if (operatorSTR == '<' ){
//if comparison fails, reset counter
if (currentValueFLT >= compareValueFLT) {
historyValuesOBJ[identifierSTR].countINT = 0;
return false;
}
//if comparison is true
else if (currentValueFLT < compareValueFLT) {
//return true if value is larger
if (historyValuesOBJ[identifierSTR].countINT >= durationINT){
return true;
}
return false;
}
}
else if (operatorSTR == '>' ){
//if comparison fails, reset counter
if (currentValueFLT <= compareValueFLT) {
historyValuesOBJ[identifierSTR].countINT = 0;
return false;
}
//if comparison is true
else if (currentValueFLT > compareValueFLT) {
//return true if value is larger
if (historyValuesOBJ[identifierSTR].countINT >= durationINT){
return true;
}
return false;
}
}
}
Im Blockly baut man sich dann noch einen Wrapper (JS-Funktion mit Ergebnis):
222b82a7-e309-49c1-9590-fb509f99bcc8-grafik.png
Und kann das dann so benutzen:
18108933-ea4e-445f-9ee4-16a251937167-grafik.png
Das obige Script gibt true zurück, wenn der Wert für mindestens 45 Sekunden unter 500 geblieben ist.
ToDo:
Ich habe nur größer und kleine implementiert, es fehlen noch Gleich sowie Größer-Gleich und Kleine-Gleich an Operatoren
Auch vergleiche mit true/false fehlen noch
Leider muss man noch einen Identifier angeben, damit das Script die Werte intern korrekt speichern kann.
Wenn jemand eine Idee hat, wie man das Generisch gestalten kann, immer her damit.