1
0

availibility is now property of device object

This commit is contained in:
daniel Tartavel 2022-01-02 12:14:30 +01:00
parent 260d220326
commit 91c3a3a5d4
5 changed files with 51 additions and 67 deletions

View File

@ -91,43 +91,19 @@ function searchPropertyValue($fn, &$device, $object, $listProperties)
} }
} }
function changeDevice($topic, $fn, &$device, $json) function changeDevice($topic, $fn, &$device, $payloadArray)
{ {
//print_r($device); //$fnTree = explode("/", $fn);
/*foreach($fnTree as $fn) //print_r($payloadArray);
{ iterateDevice($topic, $fn, $device, $payloadArray);
//print_r($device);
if (!isset($device[$fn]))
{
}
$device = & $device[$fn];
}*/
//print_r($json);
$fnTree = explode("/", $fn);
//print_r($json);
if ( ($jsonDevice = json_decode($json)) === null )
{
//echo "json ========>>>>>>> " . print_r($json,true) . EOL;
if (!isset($device->value))
{
$device->{"value"} = $json;
$device->type = true; //set this as parameter and not device
}
//print_r($device);
}else
{
iterateDevice($topic, $fn, $device, $jsonDevice);
}
} }
function iterateDevice($topic, $fn, &$device, $json) function iterateDevice($topic, $fn, &$device, $payloadArray)
{ {
global $changed; global $changed;
print_r($payloadArray);
//echo "device =>";print_r($device);echo EOL; //echo "device =>";print_r($device);echo EOL;
foreach($json as $key => $value) foreach($payloadArray as $key => $value)
{ {
$oldValue = 0; $oldValue = 0;
//echo "key =>"; print_r($key); echo EOL; //echo "key =>"; print_r($key); echo EOL;

View File

@ -6,18 +6,14 @@
function checkEvents() function checkEvents()
{ {
global $events, $indexDevices, $devices; global $events, $indexDevices, $devices;
//echo "===========> checking events" . EOL;
$exception = false; $exception = false;
foreach ($events as $key => $event) foreach ($events as $key => $event)
{ {
if (!empty($event->startDatetime)) if (!empty($event->startDatetime))
{ {
if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false) if($event->dateTimeEvent <= now())
{ {
logger(LOG_ERROR, "Error in event recurrence. event: " . $key);
}elseif($event->dateTimeEvent <= now())
{
if (!empty($event->exceptionInterval)) if (!empty($event->exceptionInterval))
{ {
foreach($event->exceptionInterval as $key => $value) foreach($event->exceptionInterval as $key => $value)
@ -35,13 +31,17 @@ function checkEvents()
{ {
echo "---->sending command set " . $event->param . "=>" . $event->value . " to " . $event->device->friendlyName . EOL; echo "---->sending command set " . $event->param . "=>" . $event->value . " to " . $event->device->friendlyName . EOL;
publish($event->device, array($event->param => $event->value), "set", $key); publish($event->device, array($event->param => $event->value), "set", $key);
if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false)
{
logger(LOG_ERROR, __("Error in event recurrence. event: ") . $key);
}
} }
} }
}elseif (!empty($event->dateTimeEvent) and $event->dateTimeEvent <= now()) }elseif (!empty($event->dateTimeEvent) and $event->dateTimeEvent <= now())
{ {
echo "---->sending command set " . $event->param . "=>" . $event->value . " to " . $event->device->friendlyName . EOL; echo "---->sending command set " . $event->param . "=>" . $event->value . " to " . $event->device->friendlyName . EOL;
publish($event->device, array($event->param => $event->value), "set", $key); $mid = publish($event->device, array($event->param => $event->value), "set", $key);
//$event->published = now(); $event->published = now();
//echo "#################################\nUnsetting event $key \n###########################" . EOL; //echo "#################################\nUnsetting event $key \n###########################" . EOL;
unset($events[$key]); unset($events[$key]);
} }
@ -77,15 +77,20 @@ function setRecurrentEvent(&$device, $param, $value, $startDatetime, $stopDateti
$event = new event; $event = new event;
$event->recurrenceInterval = new DateInterval($string); $event->recurrenceInterval = new DateInterval($string);
$event->startDatetime = new datetime($startDatetime); $event->startDatetime = new datetime($startDatetime);
$event->stopDatetime = new datetime($stopDatetime); $event->stopDatetime = new datetime($stopDatetime);
$event->ieee_address = $device->ieeeAddress; $event->ieee_address = $device->ieeeAddress;
$event->topic = $device->topic; $event->topic = $device->topic;
$event->device = & $device; $event->device = & $device;
$event->param = $param; $event->param = $param;
$event->value = $value; $event->value = $value;
$event->dateTimeEvent = $event->startDatetime; $event->dateTimeEvent = $event->startDatetime;
if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false)
{
logger(LOG_ERROR, __("Error in event recurrence. event: ") . $key);
}
} }
function setDelay(&$device, $delay, $unit="second", $param, $value, $replace=false) function setDelay(&$device, $delay, $unit="second", $param, $value, $replace=false)
@ -143,15 +148,10 @@ function setDelay(&$device, $delay, $unit="second", $param, $value, $replace=fal
echo "new event"; echo "new event";
} }
function searchEvent($device, $param , $value) function searchEvent($device, $param , $value)
{ {
global $events; global $events;
echo "searching event" . EOL; echo "searching event" . EOL;
//$keys = array_keys($events, $device->ieeeAddress);
//echo "ieee_address =>" . $device->ieeeAddress . EOL;
//print_r($events);
foreach($events as $key => $event) foreach($events as $key => $event)
{ {
//echo "Event : $event => $value" . EOL; //echo "Event : $event => $value" . EOL;
@ -169,7 +169,7 @@ function deleteEvent($eventKey)
{ {
global $events; global $events;
unset ($events[$eventKey]); unset ($events[$eventKey]);
//echo "delete event " . $eventKey . EOL; logger(LOG_INFO, __("delete event ") . $eventKey);
} }

View File

@ -5,14 +5,18 @@ class rdc_panneau_salon
private $devicelist = array("0x00124b0022ebac5c", "0x588e81fffe2cf695", "0x00124b001f900753", "0x04cf8cdf3c78aff0"); private $devicelist = array("0x00124b0022ebac5c", "0x588e81fffe2cf695", "0x00124b001f900753", "0x04cf8cdf3c78aff0");
public $delay = 3; // amount of time in $timeunit public $delay = 3; // amount of time in $timeunit
public $timeUnit = "minute"; // unit of time for delay, second, minute, day, week, month, year public $timeUnit = "minute"; // unit of time for delay, second, minute, day, week, month, year
public $luminance_min = 80;
public $luminance_max = 3000;
function __construct() function __construct()
{ {
global $indexDevices; global $indexDevices;
// assigne the function to the sensors devices
foreach ($this->devicelist as $ieeeAddress) foreach ($this->devicelist as $ieeeAddress)
{ {
$indexDevices[$ieeeAddress]->functions[] = array($this,"callback"); $indexDevices[$ieeeAddress]->functions[] = array($this,"callback");
//print_r($indexDevices[$ieeeAddress]);
} }
} }
@ -28,6 +32,8 @@ class rdc_panneau_salon
case "contact": case "contact":
if ($value == false) $this->send(); if ($value == false) $this->send();
break; break;
case "illuminance_lux":
if ($value >= $this->luminance_max) $this->send();
} }
echo _("notification received from MQTT") . EOL; echo _("notification received from MQTT") . EOL;
//echo $param . "=> " . $value . EOL; //echo $param . "=> " . $value . EOL;
@ -36,10 +42,11 @@ class rdc_panneau_salon
private function send() private function send()
{ {
global $devices, $indexDevices; global $devices, $indexDevices;
if ($indexDevices["0x04cf8cdf3c78aff0"]->illuminance_lux <= 76) if ($indexDevices["0x04cf8cdf3c78aff0"]->illuminance_lux <= $this->luminance_min)
{ {
$msg = array("state" => "ON"); $msg = array("state" => "ON");
$device = & $indexDevices["0x588e81fffe343e8f"]; $device = & $indexDevices["0x588e81fffe343e8f"];
logger(LOG_INFO, __("publishing ") . $msg . __(" message: ") . $device->friendlyName);
$device->payload = $msg; $device->payload = $msg;
$device->set(null); $device->set(null);
setDelay($device, $this->delay, $this->timeUnit, "state", "OFF", true); setDelay($device, $this->delay, $this->timeUnit, "state", "OFF", true);
@ -48,9 +55,4 @@ class rdc_panneau_salon
} }
$hooks["rdc/panneau/salon"] = new rdc_panneau_salon(); $hooks["rdc/panneau/salon"] = new rdc_panneau_salon();
//assignation of the function to the sensors devices
//$indexDevices["0x00124b0022ebac5c"]->functions[] = array($panneau_salon,"getCallback"); // rdc-salon-mvmt2
//$indexDevices["0x588e81fffe2cf695"]->functions[] = array($panneau_salon,"getCallback"); // rdc-salon-mvmt
//$indexDevices["0x00124b001f900753"]->functions[] = array($panneau_salon,"getCallback"); // rdc-porte-entree
//$indexDevices["0x04cf8cdf3c78aff0"]->functions[] = array($panneau_salon,"getCallback"); // rdc-salon-luminosite
?> ?>

View File

@ -42,11 +42,11 @@ class device
public $ieeeAddress; public $ieeeAddress;
public $groupID; public $groupID;
public $friendlyName; public $friendlyName;
public $type; // if true then not a device but parameter of device(eg. topic/FRIENDLYNAME/PARAMETER
public $powerSource; public $powerSource;
public $description; public $description;
public $functions; public $functions;
public $payload; public $payload;
public $availibility;
public function set($event) public function set($event)
{ {
@ -198,10 +198,6 @@ while (true)
}elseif($dbInit == 2 and $included) }elseif($dbInit == 2 and $included)
{ {
checkEvents(); checkEvents();
if (empty($flag))
{
$flag = true;
}
} }
} }

View File

@ -37,10 +37,20 @@ function message($message)
}; };
}elseif (($topic[array_key_last($topic)]) != "get" and ($topic[array_key_last($topic)]) != "set") }elseif (($topic[array_key_last($topic)]) != "get" and ($topic[array_key_last($topic)]) != "set")
{ {
$topic = explode ("/", $message->topic, 2); // get topic name $topic = explode ("/", $message->topic, 2); // get topic name
$fnTree = explode ("/" , $topic[1]); // get friendlyname $fnTree = explode ("/" , $topic[1]); // get friendlyname
echo $topic[0] . " => " . $topic[1] . EOL; echo $topic[0] . " => " . $topic[1] . EOL;
//$devices[$topic[0]][$fnTree[0]]->json = json_decode($message->payload); //$devices[$topic[0]][$fnTree[0]]->json = json_decode($message->payload);
if ($fnTree[array_key_last($fnTree)] == "availability")
{
unset ($fnTree[array_key_last($fnTree)]);
$payloadArray = array("availability" => $message->payload);
print_r($payloadArray);
}else
{
$payloadArray = json_decode($message->payload);
}
$device = & $devices[$topic[0]]; $device = & $devices[$topic[0]];
foreach($fnTree as $fn) foreach($fnTree as $fn)
{ {
@ -53,7 +63,7 @@ function message($message)
} }
$device = & $device[$fn]; $device = & $device[$fn];
} }
changeDevice($topic[0], $topic[1], $device["device"], $message->payload); changeDevice($topic[0], $topic[1], $device["device"], $payloadArray);
//fwrite($logFh, print_r($msg, true)); //fwrite($logFh, print_r($msg, true));
} }
} }