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