Home Refactor/Optimalization #2

This commit is contained in:
JonatanRek 2019-10-10 15:13:43 +02:00
parent c6c3b5831a
commit b63be94d27
2 changed files with 47 additions and 2 deletions

View File

@ -107,4 +107,36 @@ class Utilities
{
return $count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) );
}
function checkOperator($value1, $operator, $value2) {
switch ($operator) {
case '<': // Less than
return $value1 < $value2;
case '<=': // Less than or equal to
return $value1 <= $value2;
case '>': // Greater than
return $value1 > $value2;
case '>=': // Greater than or equal to
return $value1 >= $value2;
case '==': // Equal
return $value1 == $value2;
case '===': // Identical
return $value1 === $value2;
case '!==': // Not Identical
return $value1 !== $value2;
case '!=': // Not equal
case '<>': // Not equal
return $value1 != $value2;
case '||': // Or
case 'or': // Or
return $value1 || $value2;
case '&&': // And
case 'and': // And
return $value1 && $value2;
case 'xor': // Or
return $value1 xor $value2;
default:
return FALSE;
} // end switch
}
}

View File

@ -60,21 +60,33 @@ class Home extends Template
case 'on/off':
$replacementTrue = 'On';
$replacementFalse = 'Off';
$operator = '==';
$breakValue = 1;
break;
case 'door':
$replacementTrue = 'Closed';
$replacementFalse = 'Open';
$operator = '==';
$breakValue = 1;
break;
case 'light':
$replacementTrue = 'Light';
$replacementFalse = 'Dark';
$operator = '==';
$breakValue = 1;
if ($lastValue != 1 || $lastValue != 0) { //Digital Light Senzor
$operator = '<=';
$breakValue = 810;
}
break;
case 'water':
$replacementTrue = 'Wet';
$replacementFalse = 'Dry';
$operator = '==';
$breakValue = 1;
break;
default:
@ -86,14 +98,15 @@ class Home extends Template
if ($replacementTrue != '' && $replacementFalse != '') {
//parsing last values
$parsedValue = $replacementFalse;
if ($lastValue == 1) {
if (Utilities::checkOperator($lastValue, $operator, $breakValue)) {
$parsedValue = $replacementTrue;
}
//parsing last events values
foreach ($events as $key => $value) {
$events[$key]['value'] = $replacementFalse;
if ($value == 1) {
if (Utilities::checkOperator($lastValue, $operator, $breakValue)) {
$events[$key]['value'] = $replacementTrue;
}
}