diff --git a/db_functions.php b/db_functions.php index 9f73cd3..823c343 100644 --- a/db_functions.php +++ b/db_functions.php @@ -15,7 +15,7 @@ function deviceTree(string $topic, array $fnTree, array &$device) function mkDevicesDB($topic, $json, $group=false) { - global $devices, $listProperties, $indexDevices, $dbInit, $logFh; + global $devices, $listProperties, $listPropertiesKeys, $indexDevices, $dbInit, $logFh; if (!isset($devices[$topic])) $devices[$topic]= array(); foreach ($json as $jsonDevice) { @@ -49,22 +49,40 @@ function mkDevicesDB($topic, $json, $group=false) if ($jsonDevice->definition != null) { $device["device"]->description = $jsonDevice->definition->description; + searchPropertyKey($fn, $device["device"], $jsonDevice->definition->exposes, $listPropertiesKeys); } - //searchProperty($fn, $device["device"], $jsonDevice, $listProperties); + searchPropertyValue($fn, $device["device"], $jsonDevice, $listProperties); $indexDevices[$device["device"]->ieeeAddress] = & $device["device"]; //print_r($device); } } - $dbInit = true; + $dbInit += 1; fwrite($logFh, "################################START##################################################"); fwrite($logFh, print_r($devices, true)); fwrite($logFh, "################################END##################################################"); - echo "Devices DB made" .EOL; + echo "Devices DB made" . EOL; //print_r($devices); } -function searchProperty($fn, &$device, $object, $listProperties) +function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys) +{ + foreach($listPropertiesKeys as $property) + { + foreach($object as $key => $value) + { + + if ( isset($value->property)) + { + //echo "property ===> " . $value->property . EOL; + $device->{$value->property} = null; + } + } + //print_r($device); + } +} + +function searchPropertyValue($fn, &$device, $object, $listProperties) { $objectArray = (array)$object; foreach($listProperties as $key => $value) @@ -93,16 +111,16 @@ function changeDevice($topic, $fn, &$device, $json) //print_r($json); $fnTree = explode("/", $fn); - print_r($json); + //print_r($json); if ( ($jsonDevice = json_decode($json)) === null ) { - echo "json ========>>>>>>> " . print_r($json,true) . EOL; + //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); + //print_r($device); }else { iterateDevice($topic, $fn, $device, $jsonDevice); diff --git a/events.php b/events.php index 51e034c..5e98ead 100644 --- a/events.php +++ b/events.php @@ -8,8 +8,8 @@ function checkEvents() { if (!empty($event->dateTimeEvent) and $event->dateTimeEvent <= now()) { - //echo "---->sending command" . EOL; - publish($devices[$event->topic][$indexDevices[$event->ieeeAddress]], array($event->param => $event->value), "set", $key); + echo "---->sending command" . EOL; + publish($event->device, array($event->param => $event->value), "set", $key); //$event->published = now(); //echo "#################################\nUnsetting event $key \n###########################" . EOL; unset($events[$key]); @@ -62,10 +62,13 @@ function setDelay(&$device, $delay, $unit="sec", $param, $value, $replace=false) $events[$key]->topic = $device->topic; $events[$key]->param = $param; $events[$key]->value = $value; + $events[$key]->device = & $device; echo "new event"; } -function searchEvent($device, $param , $value) + + +function searchEvent($device, $param , $value): int { global $events; echo "searching event" . EOL; diff --git a/hooks/notifiers/notificationfreemobile.php b/hooks/notifiers/notificationfreemobile.php new file mode 100644 index 0000000..d676a3d --- /dev/null +++ b/hooks/notifiers/notificationfreemobile.php @@ -0,0 +1,2 @@ +devicelist as $ieeeAddress) + { + $indexDevices[$ieeeAddress]->functions[] = array($this,"callback"); + //print_r($indexDevices[$ieeeAddress]); + } + + } + + public function callBack($topic, $fn, $param, $value) + { + global $devices, $indexDevices; + + switch($param) + { + case "occupancy": + if ($value == 1) $this->send(); + break; + case "contact": + if ($value == false) $this->send(); + break; + } + echo _("notification received from MQTT") . EOL; + //echo $param . "=> " . $value . EOL; + } + + private function send() + { + global $devices, $indexDevices; + if ($indexDevices["0x04cf8cdf3c78aff0"]->illuminance_lux <= 76) + { + $msg = array("state" => "ON"); + $device = & $indexDevices["0x588e81fffe343e8f"]; + $device->payload = $msg; + $device->set(null); + setDelay($device, $this->delay, $this->timeUnit, "state", "OFF", true); + } + } +} + +$panneau_salon = new 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 +?> diff --git a/moha.php b/moha.php index 12d02a9..a1308ac 100644 --- a/moha.php +++ b/moha.php @@ -14,7 +14,8 @@ $logLevel = LOG_DEBUG; declare(ticks = 1); -$listProperties = array( "powerSource" => "batterie" ); +$listProperties = array("powerSource" => "batterie"); +$listPropertiesKeys = array("property"); class Message { @@ -64,6 +65,7 @@ class event public $topic; public $param; public $value; + public $device; public $published; public $dateTimeEvent; public $recurrenceDay; @@ -178,17 +180,20 @@ foreach($topics as $name => $topic) while (true) { $client->loop(); - if ($dbInit and ! $included) + if ($dbInit == 2 and ! $included) { - $hooks = loadHooks("./scripts"); - if (!empty($hooks)) + $hooksList = array(); + loadHooks("./hooks", $hooksList); + //print_r($hooksList); + if (!empty($hooksList)) { - foreach ($hooks as $hook) + foreach ($hooksList as $hook) { + echo "Including $hook" . EOL; include $hook; } } - }elseif($dbInit and $included) + }elseif($dbInit == 2 and $included) { checkEvents(); if (empty($flag)) @@ -213,32 +218,31 @@ function init() return true; } -function loadHooks($dir) +function loadHooks($dir, &$hookList) { global $included; - $hookList = array(); $files = scandir($dir); //print_r($files); foreach ($files as $file) { - //echo " $file" . EOL; - if ($file != "." and $file != ".." and strpos($file, "~", -2) === false) + //echo "=====> $file" . EOL; + + if ($file != "." and $file != "..") { - //echo "not . or .." . EOL; - if (is_dir($file)) + //echo "not . or .." . EOL;echo strpos($file, ".php", -4) . EOL; + if (is_dir($dir . "/" . $file)) { - //echo "directory" . EOL; - loadHooks($dir . '/' . $file); - }else + //echo "directory : " . $dir . '/' . $file . EOL; + loadHooks($dir . '/' . $file, $hookList); + }elseif (strpos($file, ".php", -4) !== false) { - //echo "file" . EOL; + //echo "file : " . $dir . "/" . $file . EOL; $hookList[] = $dir . "/" . $file; } } } //print_r($hookList); $included = true; - return $hookList; }