Automation Rewrite 1%

This commit is contained in:
JonatanRek 2019-08-23 14:20:58 +02:00
parent b4e854d8dd
commit 11eae4932c

View File

@ -41,39 +41,51 @@ class AutomationManager{
$sceneDoJson = $automation['do_something']; $sceneDoJson = $automation['do_something'];
$actionDays = json_decode($automation['on_days'], true); $actionDays = json_decode($automation['on_days'], true);
$value = time(); $value = time();
$run = false;
$restart = false;
if ($automation['active'] != 0){ if ($automation['active'] != 0){
if (in_array($dayNameNow, $actionDays)){ if (in_array($dayNameNow, $actionDays)){
switch ($onValue) { if (in_array($onValue, ['sunSet','time','now'])) {
case 'sunSet': if ($onValue == 'sunSet') {
$value = date_sunset($value, SUNFUNCS_RET_TIMESTAMP, 50.0755381 , 14.4378005, 90, $dateTimeOffset); $value = date_sunset($value, SUNFUNCS_RET_TIMESTAMP, 50.0755381 , 14.4378005, 90, $dateTimeOffset);
break; } else if ($onValue == 'sunRise') {
case 'sunRise':
$value = date_sunrise($value, SUNFUNCS_RET_TIMESTAMP, 50.0755381 , 14.4378005, 90, $dateTimeOffset); $value = date_sunrise($value, SUNFUNCS_RET_TIMESTAMP, 50.0755381 , 14.4378005, 90, $dateTimeOffset);
break; } else if ($onValue == 'time') {
case 'time':
$onValue = explode(':',$onValue); $onValue = explode(':',$onValue);
$today = date_create('now'); $today = date_create('now');
$onValue = $today->setTime($onValue[0], $onValue[1]); $onValue = $today->setTime($onValue[0], $onValue[1]);
$value = $today->getTimestamp(); $value = $today->getTimestamp();
break;
default:
break;
} }
if (time() > $value){ if (time() > $value){
if ($automation['executed'] == 0){ if ($automation['executed'] == 0){
$run = true;
} else if (time() < $value && $automation['executed'] = 1) { //recovery realowing of automation
$restart = true;
}
}
}
if ($onValue == 'outHome') {
}
if ($onValue == 'inHome') {
}
//finalization
if ($run) {
$sceneDoArray = json_decode($sceneDoJson); $sceneDoArray = json_decode($sceneDoJson);
foreach ($sceneDoArray as $deviceId => $deviceState) { foreach ($sceneDoArray as $deviceId => $deviceState) {
RecordManager::create($deviceId, 'on/off', $deviceState); RecordManager::create($deviceId, 'on/off', $deviceState);
} }
Db::edit('automation', array('executed' => 1), 'WHERE automation_id = ?', array($automation['automation_id'])); Db::edit('automation', array('executed' => 1), 'WHERE automation_id = ?', array($automation['automation_id']));
} } else if ($restart) {
} elseif (time() < $value && $automation['executed'] = 1) { //recovery realowing of automation
Db::edit('automation', array('executed' => 0), 'WHERE automation_id = ?', array($automation['automation_id'])); Db::edit('automation', array('executed' => 0), 'WHERE automation_id = ?', array($automation['automation_id']));
} }
} }
} }
} }
} }
}