PHP_SMART_HOME_V3/app/class/AutomationManager.php

176 lines
6.4 KiB
PHP
Raw Normal View History

2019-08-23 11:39:42 +00:00
<?php
2019-09-18 07:38:24 +00:00
2019-08-23 11:39:42 +00:00
class AutomationManager{
public static $automation;
2020-04-21 17:44:30 +00:00
public static function remove($automationId) {
2019-08-23 11:39:42 +00:00
return Db::command ('DELETE FROM automation WHERE automation_id=?', array ($automationId));
}
2020-04-21 17:44:30 +00:00
public static function deactive($automationId) {
2019-08-23 11:39:42 +00:00
$automation = Db::loadOne ("SELECT * FROM automation WHERE automation_id=?" , array ($automationId));
$flipedValue = ($automation['active'] == 1 ? 0 : 1);
return Db::command ('UPDATE automation SET active = ? WHERE automation_id=?', array ($flipedValue,$automationId));
}
2020-04-21 17:44:30 +00:00
public static function restart($automationId) {
return Db::command ('UPDATE automation SET executed = 0 WHERE automation_id=?', array ($automationId));
}
2020-04-21 17:44:30 +00:00
public static function create ($name, $onDays, $doCode, $ifCode, $automationId = "") {
2020-02-21 13:25:25 +00:00
$userId = UserManager::getUserData('user_id');
2019-08-23 11:39:42 +00:00
$scene = array (
'name' => $name,
2020-02-21 13:25:25 +00:00
'owner_id' => $userId,
2019-08-23 11:39:42 +00:00
'on_days' => $onDays,
2019-08-24 11:07:07 +00:00
'if_something' => $ifCode,
'do_something' => $doCode,
2019-08-23 11:39:42 +00:00
);
try {
2019-09-19 18:05:32 +00:00
if ($automationId == "") {
Db::add ('automation', $scene);
} else {
Db::edit ('automation', $scene, 'WHERE automation_id = ?', array ($automationId));
}
2019-08-23 11:39:42 +00:00
} catch(PDOException $error) {
echo $error->getMessage();
die();
}
}
2020-04-21 17:44:30 +00:00
public static function getAll(){
2019-08-23 11:39:42 +00:00
return Db::loadAll ("SELECT * FROM automation");
}
2020-04-21 17:44:30 +00:00
public static function executeAll(){
2019-09-18 07:38:24 +00:00
global $logManager;
2019-08-23 11:39:42 +00:00
$automations = Db::loadAll ("SELECT * FROM automation");
$dayNameNow = strtolower (date('D', time()));
2019-08-24 11:07:07 +00:00
2019-08-23 11:39:42 +00:00
foreach ($automations as $automation) {
2019-08-24 11:07:07 +00:00
$onValue = json_decode($automation['if_something'], true);
2019-08-23 11:39:42 +00:00
$sceneDoJson = $automation['do_something'];
$actionDays = json_decode($automation['on_days'], true);
$value = time();
2019-08-23 12:20:58 +00:00
$run = false;
$restart = false;
2019-08-23 11:39:42 +00:00
2020-02-21 13:25:25 +00:00
if ($automation['active'] == 1 && $automation['locked'] != 1){
2019-10-08 15:08:54 +00:00
Db::edit('automation', array('locked' => 1), 'WHERE automation_id = ?', array($automation['automation_id']));
2019-08-23 11:39:42 +00:00
if (in_array($dayNameNow, $actionDays)){
2019-09-18 07:38:24 +00:00
if (in_array($onValue['type'], ['sunSet', 'sunRise', 'time','now'])) {
2019-08-24 11:07:07 +00:00
if ($onValue['type'] == 'sunSet') {
2019-09-18 07:38:24 +00:00
$value = date_sunset($value, SUNFUNCS_RET_TIMESTAMP, 50.0755381 , 14.4378005, 90);
2019-08-24 11:07:07 +00:00
} else if ($onValue['type'] == 'sunRise') {
2019-09-18 07:38:24 +00:00
$value = date_sunrise($value, SUNFUNCS_RET_TIMESTAMP, 50.0755381 , 14.4378005, 90);
2019-08-24 11:07:07 +00:00
} else if ($onValue['type'] == 'time') {
$onValue = explode(':',$onValue['value']);
2019-08-23 12:20:58 +00:00
$today = date_create('now');
$onValue = $today->setTime($onValue[0], $onValue[1]);
$value = $today->getTimestamp();
}
2019-10-09 08:59:05 +00:00
if (time() > $value && $automation['executed'] == 0){
$run = true;
2019-10-10 11:02:44 +00:00
} else if (time() < $value && $automation['executed'] == 1) { //recovery realowing of automation
2019-10-09 08:59:05 +00:00
$restart = true;
2019-08-23 11:39:42 +00:00
}
2019-10-09 08:59:05 +00:00
} else if ($onValue['type'] == 'outHome') {
2020-01-16 13:33:11 +00:00
//TODO: Add Ovner to automation
2020-01-17 07:12:40 +00:00
$userHomeStatus = UserManager::getUserData('at_home', $onValue['value']);
if ($userHomeStatus == 'false' && $automation['executed'] == 0) {
$run = true;
} else if ($userHomeStatus == 'true' && $automation['executed'] == 1) {
$restart = true;
}
} else if ($onValue['type'] == 'inHome') {
2020-01-16 13:33:11 +00:00
//TODO: Add Ovner to automation
2020-01-17 07:12:40 +00:00
$userHomeStatus = UserManager::getUserData('at_home', $onValue['value']);
if ($userHomeStatus == 'true' && $automation['executed'] == 0) {
$run = true;
} else if ($userHomeStatus == 'false' && $automation['executed'] == 1) {
$restart = true;
}
} else if ($onValue['type'] == 'noOneHome') {
$users = UserManager::getUsers();
$membersHome = 0;
foreach ($users as $key => $user) {
if ($user['at_home'] == 'true'){
$membersHome++;
}
}
if ($membersHome == 0 && $automation['executed'] == 0) {
$run = true;
} else if ($membersHome > 0 && $automation['executed'] == 1){
$restart = true;
}
} else if ($onValue['type'] == 'someOneHome') {
$users = UserManager::getUsers();
$membersHome = 0;
foreach ($users as $key => $user) {
if ($user['at_home'] == 'true'){
$membersHome++;
}
}
2019-08-26 15:32:54 +00:00
if ($membersHome == 0 && $automation['executed'] == 1) {
$restart = true;
2019-08-26 15:32:54 +00:00
} else if ($membersHome > 0 && $automation['executed'] == 0){
$run = true;
}
2020-02-21 13:01:39 +00:00
} else if ($onValue['type'] == 'atDeviceValue') {
$subDeviceId = SubDeviceManager::getSubDeviceByMaster($onValue['value']['deviceID'], $onValue['value']['type'])["subdevice_id"];
$lastValue = RecordManager::getLastRecord($subDeviceId);
if ($lastValue['value'] == $onValue['value']['value'] && $automation['executed'] == 0) {
$run = true;
} else if ($lastValue['value'] != $onValue['value']['value'] && $automation['executed'] == 1){
$restart = true;
}
2019-08-23 12:20:58 +00:00
}
//finalization
if ($run) {
2019-10-08 14:49:08 +00:00
$body = '';
2019-08-23 12:20:58 +00:00
$sceneDoArray = json_decode($sceneDoJson);
foreach ($sceneDoArray as $deviceId => $deviceState) {
RecordManager::create($deviceId, 'on/off', $deviceState);
}
2019-10-08 14:49:08 +00:00
$subscribers = NotificationManager::getSubscription();
$i = 0;
2019-10-10 11:02:44 +00:00
2020-01-16 13:33:11 +00:00
$notificationMng = new NotificationManager;
$notificationData = [
'title' => 'Automatization',
'body' => 'Automatization '.$automation['name']." was just executed",
'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
];
if ($notificationData != []) {
$subscribers = $notificationMng::getSubscription();
foreach ($subscribers as $key => $subscriber) {
$logManager->write("[NOTIFICATION/AUTOOMATION] SENDING TO" . $subscriber['id'] . " ");
$notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
}
2019-10-08 14:49:08 +00:00
}
2019-08-30 15:44:22 +00:00
$logManager->write("[AUTOMATIONS] automation id ". $automation['automation_id'] . " was executed");
2020-02-21 13:25:25 +00:00
Db::edit('automation', array('executed' => 1, 'execution_time' => date("Y-m-d H:i:s")), 'WHERE automation_id = ?', array($automation['automation_id']));
2019-08-23 12:20:58 +00:00
} else if ($restart) {
2019-08-30 15:44:22 +00:00
$logManager->write("[AUTOMATIONS] automation id ". $automation['automation_id'] . " was restarted");
2019-08-23 11:39:42 +00:00
Db::edit('automation', array('executed' => 0), 'WHERE automation_id = ?', array($automation['automation_id']));
}
2019-10-08 15:08:54 +00:00
Db::edit('automation', array('locked' => 0), 'WHERE automation_id = ?', array($automation['automation_id']));
2019-08-23 11:39:42 +00:00
}
}
}
}
2019-08-24 11:07:07 +00:00
}