1
0

modified init and some debugging

This commit is contained in:
daniel Tartavel 2022-01-06 13:03:26 +01:00
parent 85a7fd7ed9
commit 004c6aa572
10 changed files with 178 additions and 34 deletions

View File

@ -28,7 +28,7 @@ class device
public $description; public $description;
public $functions; public $functions;
public $payload; public $payload;
public $availibility; public $availability = array();
public function set($event) public function set($event)
{ {

View File

@ -15,7 +15,11 @@ $deviceTable = array(
"0x00158d0003f0f3b4" => "RDC_SDB_DOUCHE", "0x00158d0003f0f3b4" => "RDC_SDB_DOUCHE",
"0x842e14fffe1c0cd1" => "RDC_SDB_PLAFOND_MVMT", "0x842e14fffe1c0cd1" => "RDC_SDB_PLAFOND_MVMT",
"0x00124b0022ec05dc" => "RDC_SDB_MVMT", "0x00124b0022ec05dc" => "RDC_SDB_MVMT",
"0x00158d0005c1a998" => "RDC_SDB_WC_ECLAIRAGE" "0x00158d0005c1a998" => "RDC_SDB_WC_ECLAIRAGE",
"0x00124b001f90ee7e" => "ENTREE_PORTE",
"0x00124b002226d9a2" => "GARAGE_PORTE",
"0x00124b001f90e725" => "RDC_CHAMBRE_BAIE",
"0x00124b002226e384" => "RDC_SALON_BAIE"
); );
foreach($deviceTable as $device => $name) foreach($deviceTable as $device => $name)

View File

@ -73,7 +73,7 @@ function addDevice(& $device, $fn, $jsonDevice )
// adding callback function for availability // adding callback function for availability
//print_r($hooks); //print_r($hooks);
$device["device"]->functions[] = $hooks["availability"]->getHook(); //$device["device"]->availability["functions"][] = $hooks["availability"]->getHook();
//indexing device //indexing device
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"]; $indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
@ -187,13 +187,13 @@ function iterateDevice($topic, $fn, &$device, $payloadArray)
echo " to " . $value . EOL;*/ echo " to " . $value . EOL;*/
} }
//print_r($device->functions); print_r($value); //print_r($device->functions); print_r($value);
print_r($device); //print_r($device);
if (!empty($device->$key["functions"])) if (!empty($device->$key["functions"]))
{ {
echo "executing notifications functions " . EOL; echo "executing notifications functions " . EOL;
foreach($device->$key["functions"] as $function) foreach($device->$key["functions"] as $function)
{ {
print_r($function); //print_r($function);
$function($device, $key, $value); $function($device, $key, $value);
} }
} }
@ -202,5 +202,9 @@ function iterateDevice($topic, $fn, &$device, $payloadArray)
} }
} }
function getDevicesValues()
{
}
?> ?>

View File

@ -4,13 +4,33 @@ class availability
{ {
// by default all devices are listening for availability // by default all devices are listening for availability
function __construct()
{
global $devices;
$this->iterate($devices);
}
private function iterate(& $device)
{
foreach ($device as $key => $value)
{
if (gettype($value) == "array")
{
$this->iterate($value);
}elseif (is_a($value, "device"))
{
$value->availability["functions"][] = array($this,"callback");
}
}
}
// callback fonction. Is called with these 3 parameters // callback fonction. Is called with these 3 parameters
// $device -> calling device // $device -> calling device
// $param -> parameter passed by mqtt // $property -> parameter passed by mqtt
// $value -> value of the parameter // $value -> value of the parameter
public function callBack($device, $param, $value) public function callBack($device, $property, $value)
{ {
switch($param) switch($property)
{ {
case "availability": // theorically can't be other, but .... case "availability": // theorically can't be other, but ....
if ($device->availability != $value) if ($device->availability != $value)
@ -28,14 +48,15 @@ class availability
} }
break; break;
} }
echo sprintf(_("notification received from MQTT -> device %s is %s"), $device->friendlyName , $value). EOL; //echo sprintf(_("notification received from MQTT -> device %s is %s"), $device->friendlyName , $value). EOL;
//echo $param . "=> " . $value . EOL; //echo $property . "=> " . $value . EOL;
} }
/*
public function getHook() public function getHook()
{ {
return array($this,"callback"); return array($this,"callback");
} }*/
} }
$hooks["availability"] = new availability; $hooks["availability"] = new availability();
?> ?>

View File

@ -0,0 +1,44 @@
<?php
class alerte_intrusion
{
public $hookName = "alerte_intrusion";
public $active = true;
private $devicelist = array(ENTREE_PORTE => "contact", GARAGE_PORTE => "contact", RDC_CHAMBRE_BAIE => "contact", RDC_SALON_BAIE => "contact");
public $delay = 3; // amount of time in $timeunit
public $delayManual = 10; // amount of time in $timeunit for manual mode
public $timeUnit = "minute"; // unit of time for delay, second, minute, day, week, month, year
function __construct()
{
global $indexDevices;
// assigne the function to the sensors devices
if ($this->active === true)
{
foreach ($this->devicelist as $ieeeAddress => $param)
{
$indexDevices[$ieeeAddress]->$param["functions"][] = array($this,"callback");
}
}
}
// callback fonction. Is called with these 4 parameters
public function callBack(&$device, $param, $value)
{
global $devices, $indexDevices;
switch($param)
{
case "contact":
if ($value == false)
{
logger(ALERT, sprintf(_("%s est ouverte alors que personne n'est présent"), $device->friendlyName));
}
break;
}
}
}
$hooks["alerte_intrusion"] = new alerte_intrusion();
?>

View File

@ -14,7 +14,7 @@ class rdc_salon_eclairage
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_min = 60;
public $luminance_max = 3000; public $luminance_max = 3000;
@ -60,12 +60,19 @@ class rdc_salon_eclairage
private function send($state) private function send($state)
{ {
global $devices, $indexDevices; global $devices, $indexDevices;
$msg = array("state" => $state);
$device = & $indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU]; $device = & $indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU];
logger(INFO, sprintf(_("publishing message: %s to %s"), $msg, $device->friendlyName)); if ($device->state_l1["value"] != $state)
{
$msg = array("state" => $state);
logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $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);
}else
{
logger(INFO, sprintf(_("not publishing message: %s to %s, already set"), json_encode($msg), $device->friendlyName));
}
} }
} }

View File

@ -3,17 +3,22 @@
class rdc_sdb_eclairage class rdc_sdb_eclairage
{ {
public $hookName = "rdc_sdb_eclairage"; public $hookName = "rdc_sdb_eclairage";
public $active = false; public $active = true;
// list of devices we are listening to // list of devices we are listening to
// 0x00158d0003f0f3b4 douche mvmnt // 0x00158d0003f0f3b4 douche mvmnt
// 0x842e14fffe1c0cd1 plafond mvmnt // 0x842e14fffe1c0cd1 plafond mvmnt
// 0x00124b0022ec05dc mvmnt // 0x00124b0022ec05dc mvmnt
// 0x00158d0005c1a998 module commutateur => state_l1 // 0x00158d0005c1a998 module commutateur => state_l1
private $devicelist = array("0x00158d0003f0f3b4", "0x842e14fffe1c0cd1", "0x00124b0022ec05dc"); private $devicelist = array(
"0x00158d0003f0f3b4" => "occupancy",
"0x842e14fffe1c0cd1" => "occupancy",
"0x00124b0022ec05dc" => "occupancy",
RDC_SDB_WC_ECLAIRAGE => "state_l1"
);
public $delay = 3; // amount of time in $timeunit public $delay = 3; // amount of time in $timeunit
public $delayManual = 15; // amount of time in $timeunit for manual mode public $delayManual = 10; // amount of time in $timeunit for manual mode
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
function __construct() function __construct()
@ -23,18 +28,19 @@ class rdc_sdb_eclairage
// assigne the function to the sensors devices // assigne the function to the sensors devices
if ($this->active === true) if ($this->active === true)
{ {
foreach ($this->devicelist as $ieeeAddress => $param) foreach ($this->devicelist as $ieeeAddress => $property)
{ {
$indexDevices[$ieeeAddress]->$param["functions"][] = array($this,"callback"); $indexDevices[$ieeeAddress]->$property["functions"][] = array($this,"callback");
print_r($indexDevices[$ieeeAddress]);
} }
} }
} }
// callback fonction. Is called with these 4 parameters // callback fonction. Is called with these 4 parameters
public function callBack(&$device, $param, $value) public function callBack(&$device, $property, $value)
{ {
global $devices, $indexDevices; global $devices, $indexDevices;
switch($param) switch($property)
{ {
case "occupancy": case "occupancy":
if ($value == ON) if ($value == ON)
@ -47,7 +53,6 @@ class rdc_sdb_eclairage
case "state_l1": case "state_l1":
if ($value == ON) if ($value == ON)
{ {
$this->send("ON");
setDelay($device, $this->delayManual, $this->timeUnit, "state_l1", "OFF", true); setDelay($device, $this->delayManual, $this->timeUnit, "state_l1", "OFF", true);
$device->method = MANUAL; $device->method = MANUAL;
}elseif ($value = OFF) }elseif ($value = OFF)
@ -56,7 +61,7 @@ class rdc_sdb_eclairage
} }
break; break;
} }
logger (INFO, _("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $param, $value); logger (INFO, _("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, $value);
} }
private function send($state) private function send($state)
@ -67,7 +72,6 @@ class rdc_sdb_eclairage
logger(INFO, sprintf(_("publishing message: %s to %s"), $msg, $device->friendlyName)); logger(INFO, sprintf(_("publishing message: %s to %s"), $msg, $device->friendlyName));
$device->payload = $msg; $device->payload = $msg;
$device->set(null); $device->set(null);
} }
} }

View File

@ -0,0 +1,50 @@
<?php
class rdc_wc_eclairage
{
public $hookName = "rdc_wc_eclairage";
public $active = true;
private $devicelist = array(RDC_SDB_WC_ECLAIRAGE => "state_l2");
public $delay = 3; // amount of time in $timeunit
public $delayManual = 10; // amount of time in $timeunit for manual mode
public $timeUnit = "minute"; // unit of time for delay, second, minute, day, week, month, year
function __construct()
{
global $indexDevices;
// assigne the function to the sensors devices
if ($this->active === true)
{
foreach ($this->devicelist as $ieeeAddress => $property)
{
$indexDevices[$ieeeAddress]->$property["functions"][] = array($this,"callback");
}
}
}
// callback fonction. Is called with these 4 parameters
public function callBack(&$device, $property, $value)
{
global $devices, $indexDevices;
switch($property)
{
case "state_l2":
if ($value == ON)
{
setDelay($device, $this->delayManual, $this->timeUnit, "state_l2", "OFF", true);
$device->method = MANUAL;
}elseif ($value = OFF)
{
deleteEvent(searchEvent($device, "state_l2", "OFF"));
}
break;
}
logger (INFO, _("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, $value);
}
}
$hooks["rdc_wc_eclairage"] = new rdc_wc_eclairage();
?>

View File

@ -53,7 +53,8 @@ define( "INFO", $client::LOG_INFO); // => 1
define( "NOTICE", $client::LOG_NOTICE); // => 2 define( "NOTICE", $client::LOG_NOTICE); // => 2
define( "WARNING", $client::LOG_WARNING); // => 4 define( "WARNING", $client::LOG_WARNING); // => 4
define( "ERROR", $client::LOG_ERR); // => 8 define( "ERROR", $client::LOG_ERR); // => 8
define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR); define( "ALERT", 32);
define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT);
$logLevel = ALL; $logLevel = ALL;
$notificationLevel = WARNING | ERROR; $notificationLevel = WARNING | ERROR;
@ -61,7 +62,7 @@ require "utils.php";
require "mqtt_functions.php"; require "mqtt_functions.php";
require "events.php"; require "events.php";
require "db_functions.php"; require "db_functions.php";
require "class/availability.php"; //require "class/availability.php";
//include predefined file witch define constants for devices //include predefined file witch define constants for devices
if (is_readable($configDir . "/" . "devices_constants.php")) if (is_readable($configDir . "/" . "devices_constants.php"))
@ -94,7 +95,7 @@ $client->connect("192.168.1.253", 1883, 5);
foreach($topics as $name => $topic) foreach($topics as $name => $topic)
{ {
//echo $name; //echo $name;
$topic->mid = $client->subscribe($name . "/#", 2); $topic->mid = $client->subscribe($name . "/bridge/#", 2);
$mids[$topic->mid] = $name; $mids[$topic->mid] = $name;
$topic->status = false; $topic->status = false;
} }
@ -104,6 +105,7 @@ while (true)
$client->loop(); $client->loop();
if ($dbInit == 2 and ! $included) if ($dbInit == 2 and ! $included)
{ {
getDevicesValues();
loadHooks("./hooks", $hooksList); loadHooks("./hooks", $hooksList);
if (!empty($hooksList)) if (!empty($hooksList))
{ {
@ -118,6 +120,13 @@ while (true)
if ($oneshot === false) // execute once initialization finished if ($oneshot === false) // execute once initialization finished
{ {
$oneshot = true; $oneshot = true;
foreach($topics as $name => $topic)
{
//echo $name;
$topic->mid = $client->subscribe($name . "/#", 2);
$mids[$topic->mid] = $name;
$topic->status = false;
}
} }
checkEvents(); checkEvents();
} }

View File

@ -2,7 +2,7 @@
function message($message) function message($message)
{ {
global $topics, $logFh, $devices; global $topics, $logFh, $devices, $included;
$topic = explode ("/", $message->topic); $topic = explode ("/", $message->topic);
if ($topic[1] == "bridge") if ($topic[1] == "bridge")
{ {
@ -35,9 +35,8 @@ function message($message)
default: default:
break; break;
}; };
}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" and $included)
{ {
$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;
@ -119,6 +118,7 @@ function connectResponse($r, $message)
function subscribeResponse($mid, $qosCount) function subscribeResponse($mid, $qosCount)
{ {
global $topics, $mids, $nSubscribed; global $topics, $mids, $nSubscribed;
print_r($mids);
$key = $mids[$mid]; $key = $mids[$mid];
echo _("Subscribed to ") . $key . EOL; echo _("Subscribed to ") . $key . EOL;
$topics[$key]->status = true; $topics[$key]->status = true;
@ -157,6 +157,7 @@ function publishResponse($mid)
{ {
//echo "unsetting mids" . EOL; //echo "unsetting mids" . EOL;
unset ($mids[$mid]); unset ($mids[$mid]);
//print_r($mids);
}else }else
{ {
//echo "setting mids" . EOL; //echo "setting mids" . EOL;