1
0

hooks are now classes and devices scan exposes to add properties

This commit is contained in:
daniel Tartavel 2021-12-31 23:09:58 +01:00
parent 57390d099b
commit f2f8e27c89
5 changed files with 111 additions and 28 deletions

View File

@ -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,13 +49,14 @@ 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##################################################");
@ -64,7 +65,24 @@ function mkDevicesDB($topic, $json, $group=false)
//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);

View File

@ -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;

View File

@ -0,0 +1,2 @@
<?php
$notificationMethods["freemobile"] = new notificationMethod("https://smsapi.free-mobile.fr/sendmsg?user=32886706&pass=JTGUY6l5OG73zX&msg=");

View File

@ -0,0 +1,56 @@
<?php
class panneau_salon
{
private $devicelist = array("0x00124b0022ebac5c", "0x588e81fffe2cf695", "0x00124b001f900753", "0x04cf8cdf3c78aff0");
public $delay = 3;
public $timeUnit = "min";
function __construct()
{
global $indexDevices;
foreach ($this->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
?>

View File

@ -15,6 +15,7 @@ $logLevel = LOG_DEBUG;
declare(ticks = 1);
$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;
}