1
0

finished rdc_temperature_int_ext.php\ncontinued events.php (recurrence)

This commit is contained in:
Daniel Tartavel 2022-08-28 00:06:24 +02:00
parent f42df97c5c
commit b5c30b1ae4
6 changed files with 209 additions and 90 deletions

View File

@ -39,16 +39,22 @@ class db extends mysqli
{ {
global $mohaDB, $properties2log, $testMode; global $mohaDB, $properties2log, $testMode;
$precision = 0; $precision = 0;
$oldValue = $device->properties[$property]["lastValueLogged"];
//echo "############## logProperty ################\nproperty => " . $propertyTree .EOL; //echo "############## logProperty ################\nproperty => " . $propertyTree .EOL;
//echo "logging in database"; //echo "logging in database";
//var_dump($device); //var_dump($device);
$ieeeAddress = $device->ieeeAddress; $ieeeAddress = $device->ieeeAddress;
//print_r($ieeeAddress); //print_r($ieeeAddress);
$query = "INSERT INTO logs (device, property, value) VALUES('" . $this->protect($ieeeAddress) . "', '" . $this->protect($property) . "', '" . $this->protect(bool2string($value)) . "')";
//echo $query . EOL; //echo $query . EOL;
if (is_numeric($value) and !empty($properties2log[$property])) if (is_numeric($value) and !empty($properties2log[$property]))
{ {
if (!array_key_exists("lastValueLogged", $device->properties[$property]))
{
$oldvalue = -1;
}else
{
$oldValue = $device->properties[$property]["lastValueLogged"];
}
// calculate a min/max value for storing data // calculate a min/max value for storing data
$r = $properties2log[$property]; $r = $properties2log[$property];
if (is_callable($r)) if (is_callable($r))
@ -59,10 +65,9 @@ class db extends mysqli
$minMax = $r; $minMax = $r;
} }
logger(DEBUG, _("minMax = " . $minMax), __FILE__ . ":" . __LINE__); logger(DEBUG, _("minMax = " . $minMax), __FILE__ . ":" . __LINE__);
var_dump($value) . EOL; //var_dump($value) . EOL;
var_dump($minMax) . EOL; //var_dump($minMax) . EOL;
var_dump($oldValue) . EOL; //var_dump($oldValue) . EOL;
if ( !is_numeric($oldValue)) $oldValue = 0;
//echo "minMax = " .$minMax . EOL; //echo "minMax = " .$minMax . EOL;
//echo "oldValue = " . $oldValue . EOL; //echo "oldValue = " . $oldValue . EOL;
//echo "Value = " . $value . EOL; //echo "Value = " . $value . EOL;
@ -77,6 +82,7 @@ class db extends mysqli
logger(INFO, _("Test mode on: not storing in DB "), __FILE__ . ":" . __LINE__); logger(INFO, _("Test mode on: not storing in DB "), __FILE__ . ":" . __LINE__);
}else }else
{ {
$query = "INSERT INTO logs (device, property, value) VALUES('" . $this->protect($ieeeAddress) . "', '" . $this->protect($property) . "', '" . $this->protect(bool2string($value)) . "')";
if(!$this->result = $this->query($query)) if(!$this->result = $this->query($query))
{ {
logger(ERROR, _("mysql query errror: ") . $this->error, __FILE__ . ":" . __LINE__); logger(ERROR, _("mysql query errror: ") . $this->error, __FILE__ . ":" . __LINE__);

View File

@ -70,13 +70,13 @@ class event
public $function; public $function;
public $device; public $device;
public $published; public $published;
public $dateTimeEvent; // datetime : next occurence for recurrent event public $dateTimeEvent; // recurrent event: datetime
public $startDatetime; public $startDatetime;
public $stopDatetime; public $stopDatetime;
public $recurrenceInterval; // interval : for recurrent event public $recurrence; // recurrent event: interval or date to add
public $exceptionInterval; // array of object ranges public $exceptionInterval; // array of object ranges
public $method; // cf: constants.php (IDLE, AUTO, MANUAL) public $method; // cf: constants.php (IDLE, AUTO, MANUAL)
public $isInterval; // 0 => recurence by date 1 => by interval public $isInterval = false; // 0 => recurence by date 1 => by interval
} }
class watch class watch

View File

@ -16,66 +16,155 @@ function checkEvents()
{ {
$events = array(); $events = array();
}*/ }*/
$now = now();
foreach ($events as $key => &$event) foreach ($events as $key => &$event)
{ {
$now = now();
if (!empty($event->startDatetime)) if($event->dateTimeEvent < $now)
{ {
logger(DEBUG, _("StartDatetime is set"), __FILE__ . ":" . __LINE__); if (is_callable($event->function))
if($event->dateTimeEvent < $now)
{ {
logger(DEBUG, _("Event must be executed"), __FILE__ . ":" . __LINE__); logger(DEBUG, _("executing function") . $event->function[1], __FILE__ . ":" . __LINE__);
$event->function($event);
if (!empty($event->exceptionInterval) and $event->isInterval == true) }elseif (is_object($event->device))
{ {
logger(DEBUG, _("Testing exceptions"), __FILE__ . ":" . __LINE__); logger(DEBUG, sprintf(_("sending command set %s => %s for %s"), $event->param ,bool2string($event->value), $event->device->friendlyName), __FILE__ . ":" . __LINE__);
foreach($event->exceptionInterval as $key => $value) publish(mktopic($event->device), array($event->param => $event->value), "set");
{ if ($event->method !== null) $event->device->properties[$event->param]["method"] = $event->method;
if($now > $value->start and $now < $value->end) }else
{ {
$exception = true; logger(ERROR, _("Event is malformed") . $key, __FILE__ . ":" . __LINE__);
}elseif($now > $value->stop)
{
unset($event->exceptionInterval[$key]);
}
}
}
if ($exception === false)
{
if (is_callable($event->function))
{
logger(DEBUG, _("executing function") . $event->function[1], __FILE__ . ":" . __LINE__);
$event->function();
$event->published = $now;
}else
{
logger(DEBUG, sprintf(_("sending command set %s => %s for %s"), $event->param ,bool2string($event->value), $event->device->friendlyName), __FILE__ . ":" . __LINE__);
publish(mktopic($event->device), array($event->param => $event->value), "set");
$event->published = $now;
}
if ($event->method !== null) $event->device->properties[$event->param]["method"] = $event->method;
if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false)
{
logger(ERROR, _("Error in adding interval to event recurrence. event: ") . $key, __FILE__ . ":" . __LINE__);
}
}
} }
}elseif (!empty($event->dateTimeEvent) and $event->dateTimeEvent < now())
$event->published = $now;
}
if (!empty($event->recurrence))
{ {
logger(DEBUG, sprintf(_("Sending command set %s => %s to %s"),$event->param, bool2string($event->value), $event->device->friendlyName), __FILE__ . ":" . __LINE__); nextOccurence($event, $key, $now);
$mid = publish(mktopic($event->device), array($event->param => $event->value), "set"); //, $key);null }else
$event->published = $now; {
if ($event->method !== null) unset($events[$key]);
{
$event->device->properties[$event->param]["method"] = $event->method;
}
//echo "#################################\nUnsetting event $key \n###########################" . EOL;
unset($events[$key]);
} }
} }
//print_r($events); }
//$logLevel = $oldLevel;
function nextIntervalDate(&$event)
{
if (($event->dateTimeEvent = $event->dateTimeEvent->add($event->recurrence)) === false)
{
logger(ERROR, _("Error in adding interval to event recurrence. event: ") . $eventKey, __FILE__ . ":" . __LINE__);
return false;
}
}
function nextDate(&$event)
{
$isDate = false;
$date = new DateTime;
$year = $event->dateTimeEvent->format("Y");
$month = $event->dateTimeEvent->format("M");
$day = $event->dateTimeEvent->format("D");
$hour = $event->dateTimeEvent->format("H");
$minutes = $event->dateTimeEvent->format("I");
$seconds = $event->dateTimeEvent->format("S");
$temp = "";
for ($x = 0; $x < strlen($event->recurrence); $x++)
{
switch ($event->recurrence[$x])
{
case "P":
$isDate = true;
break;
case "T":
$isDate = false;
break;
case "Y":
$year = $temp;
$temp = 0;
break;
case "M":
if ($isdate)
{
$month = $temp;
}else
{
$minutes = $temp;
}
$temp = 0;
break;
case "W":
$temp = 0;
break;
case "H":
$hour = $temp;
$temp = 0;
case "S":
$seconds = $temp;
$temp = 0;
default:
$temp = $event->recurrence[$x] . $temp;
break;
}
}
if ($month != 0)
{
$year += 1;
}elseif ($day != 0)
{
$month += 1;
}elseif ($hour != 0)
{
$day = +1;
}elseif ($minutes != 0)
{
$hour = +1;
}
$event->dateTimeEvent->setDate(intval($year), intval($month), intval($day));
$event->dateTimeEvent->setTime(intval($hour), intval($minutes), intval($seconds));
}
function exceptionsTest(&$event, $now)
{
$exception = false;
logger(DEBUG, _("Testing exceptions"), __FILE__ . ":" . __LINE__);
if (!empty($event->exceptionInterval))
{
foreach($event->exceptionInterval as $key => $value)
{
if($now > $value->start and $now < $value->end)
{
$exception = true;
}elseif($now > $value->stop)
{
unset($event->exceptionInterval[$key]);
}
}
}
return $exception;
}
function nextOccurence(&$event, $eventKey, $now)
{
$n = 0;
logger(DEBUG, _("Calculating next occurence"), __FILE__ . ":" . __LINE__);
do
{
if ($event->isInterval === true)
{
nextIntervalDate($event);
}else
{
nextDate($event);
}
if ($n++ > 1000)
{
logger(ERROR, _("infinite loop"), __FILE__ . ":" . __LINE__);
return 1;
}
}while (exceptionsTest($event, $now) === true); // loop until the date is not comprise in exceptions dates
} }
// datetime format : "yyyy-mm-dd hh:mm:ss" // datetime format : "yyyy-mm-dd hh:mm:ss"
@ -109,7 +198,7 @@ function setRecurrentEvent($function, $ieeeAddress, string $property, $value, bo
if (!empty($days)) $string .= $days . "D"; if (!empty($days)) $string .= $days . "D";
$string .= "T"; $string .= "T";
if (!empty($hours)) $string .= $hours . "H"; if (!empty($hours)) $string .= $hours . "H";
if (!empty($months)) $string .= $months . "M"; if (!empty($minutes)) $string .= $minutes . "M";
if (!empty($seconds)) $string .= $seconds . "S"; if (!empty($seconds)) $string .= $seconds . "S";
logger(DEBUG, _("reccurrent event string : ") . $string, __FILE__ . ":" . __LINE__); logger(DEBUG, _("reccurrent event string : ") . $string, __FILE__ . ":" . __LINE__);
@ -118,10 +207,17 @@ function setRecurrentEvent($function, $ieeeAddress, string $property, $value, bo
// pb in recurrent event in case of date and not interval // pb in recurrent event in case of date and not interval
if ($isInterval === true) if ($isInterval === true)
{ {
$event->recurrenceInterval = new DateInterval($string); if (($event->dateTimeEvent->add($event->recurrence)) === false)
{
logger(ERROR, _("Error in event recurrence. event: ") . $key, __FILE__ . ":" . __LINE__);
return true;
}
$event->recurrence = new DateInterval($string);
$event->isInterval = true;
}else }else
{ {
$event->recurrenceInterval = 0; $event->recurrence = $string;
$event->isInterval = false;
} }
if (!empty($startDatetime)) if (!empty($startDatetime))
@ -131,12 +227,13 @@ function setRecurrentEvent($function, $ieeeAddress, string $property, $value, bo
{ {
$event->startDatetime = now(); $event->startDatetime = now();
} }
$event->dateTimeEvent = $event->startDatetime;
if (!empty($stopDatetime)) if (!empty($stopDatetime))
{ {
$event->stopDatetime = new datetime($stopDatetime); $event->stopDatetime = new datetime($stopDatetime);
} }
$event->dateTimeEvent = $event->startDatetime;
if (!empty($ieeeAddress)) if (!empty($ieeeAddress))
{ {
$event->ieee_address = $ieeeAddress; $event->ieee_address = $ieeeAddress;
@ -146,16 +243,9 @@ function setRecurrentEvent($function, $ieeeAddress, string $property, $value, bo
$event->value = $value; $event->value = $value;
} }
if ($event->recurrenceInterval != 0)
{
if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false)
{
logger(ERROR, _("Error in event recurrence. event: ") . $key, __FILE__ . ":" . __LINE__);
return 1;
}
}
if ($method !== null) $event->method = $method; if ($method !== null) $event->method = $method;
return 0; $events[] = $event;
return false;
} }
function setDelay(device &$deviceObject, float $delay, string $unit, string $property, $value, $function, bool $replace=false, int $method = null) function setDelay(device &$deviceObject, float $delay, string $unit, string $property, $value, $function, bool $replace=false, int $method = null)

View File

@ -11,7 +11,10 @@ class alerte_intrusion extends hook
GARAGE_PORTE => "contact", GARAGE_PORTE => "contact",
RDC_CHAMBRE_BAIE => "contact", RDC_CHAMBRE_BAIE => "contact",
RDC_SALON_BAIE => "contact", RDC_SALON_BAIE => "contact",
GARAGE_PORTAIL => "contact" GARAGE_PORTAIL => "contact",
ETAGE_SALON_FENETRE => "contact",
ETAGE_CUISINE_FENETRE => "contact",
ETAGE_PORTE_FENETRE => "contact"
); );
function installHooks(&$indexDevices) function installHooks(&$indexDevices)
@ -40,7 +43,7 @@ class alerte_intrusion extends hook
} }
} }
public function testPortes($send=true) :array public function testPortes($send=true, $opened=true) :array
{ {
global $indexDevices; global $indexDevices;
$portes = array(); $portes = array();
@ -49,10 +52,20 @@ class alerte_intrusion extends hook
$msg = ""; $msg = "";
foreach ($this->devicelist as $device => $property) foreach ($this->devicelist as $device => $property)
{ {
if($indexDevices[$device]->properties[$property] == false) if (!$opened)
{ {
$portes[] = $indexDevices[$device]->friendlyName; if($indexDevices[$device]->properties[$property] == false)
$msg .= $indexDevices[$device]->friendlyName . "\n"; {
$portes[] = $indexDevices[$device]->friendlyName;
$msg .= $indexDevices[$device]->friendlyName . "\n";
}
}else
{
if($indexDevices[$device]->properties[$property] == true)
{
$portes[] = $indexDevices[$device]->friendlyName;
$msg .= $indexDevices[$device]->friendlyName . "\n";
}
} }
} }
if ($send) if ($send)
@ -66,6 +79,6 @@ class alerte_intrusion extends hook
$hooks["alerte_intrusion"] = new alerte_intrusion(); $hooks["alerte_intrusion"] = new alerte_intrusion();
logger(DEBUG, _("Initializing event"), __FILE__ . ":" . __LINE__); logger(DEBUG, _("Initializing event"), __FILE__ . ":" . __LINE__);
$function = array($hooks["alerte_intrusion"], "testPortes"); $function = array($hooks["alerte_intrusion"], "testPortes");
setRecurrentEvent($function, 0, 0, 0, 0, 0, 0, 21, 0, 0, 1, 0, 0, 0); setRecurrentEvent($function, 0, 0, 0, false, 0, 0, 21, 0, 0, 0, 0, 0, 0);
?> ?>

View File

@ -23,15 +23,12 @@ class rdc_temperature_int_ext extends hook
global $indexDevices, $hooks; global $indexDevices, $hooks;
static $time; static $time;
$portes = array(); $portes = array();
$state = -1; $status = -1;
$msg = ""; $msg = "";
if (empty($time)) $time = now();
$indoorTemp = $device->properties["indoortempc"]["value"]; $indoorTemp = $device->properties["indoortempc"]["value"];
$portes = $hooks["alerte_intrusion"]->testPortes(false);
foreach($portes as $value)
{
$msg .= $value . "\n";
}
if ( $value > $indoorTemp) if ( $value > $indoorTemp)
{ {
@ -57,13 +54,26 @@ class rdc_temperature_int_ext extends hook
} }
logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, bool2string($value)), __FILE__ . ":" . __LINE__, $device); logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, bool2string($value)), __FILE__ . ":" . __LINE__, $device);
} }
$portes = $hooks["alerte_intrusion"]->testPortes(false, true);
if ($status == 1) if ($status == 1)
{ {
//if ($time - DateTime(); if (empty($portes) and ($time->diff(now())->format("i")) > 5)
//logger(ALERT, _("Open doors to climate"), null ,$device); {
logger(ALERT, _("Open doors to climate"), null, $device);
$time = now();
}
}else }else
{ {
//logger(ALERT, _("Close doors to climate\n") . $msg, null, $device); //$portes = $hooks["alerte_intrusion"]->testPortes(false, true);
if (!empty($portes) and ($time.diff(now()).format("i") > 5))
{
$time = now();
foreach($portes as $porte)
{
$msg .= $porte . "\n";
logger(ALERT, _("Close doors to climate\n") . $msg, null, $device);
}
}
} }
} }
} }

View File

@ -50,7 +50,7 @@ if ($testMode)
$httpServerIp = "192.168.1.253"; $httpServerIp = "192.168.1.253";
}else }else
{ {
$logLevel = INFO | NOTICE | WARNING | ERROR | ALERT; $logLevel = DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT;
$mqttServerIp = "127.0.0.1"; // IP address of mqttserver in production mode $mqttServerIp = "127.0.0.1"; // IP address of mqttserver in production mode
$dataPath = "/usr/share/moha/"; $dataPath = "/usr/share/moha/";
$logFile = "/var/log/moha.log"; // Path of log file $logFile = "/var/log/moha.log"; // Path of log file