From c8772aeda062150fe3a4b64d8e11787bf12021c6 Mon Sep 17 00:00:00 2001 From: daniel Tartavel Date: Sun, 13 Mar 2022 22:33:26 +0100 Subject: [PATCH] a lot of bugs corrected\nrdc_wc_eclairage --- class/db.php | 2 +- class/hook_class.php | 34 +++-- class/main.php | 2 +- config/dashboard_conf.php | 2 + config/devices_constants.php | 18 ++- config/propertiesbytype.php | 18 +++ db_functions.php | 94 +++++++++---- events.php | 2 +- hooks/scripts/availability.php | 2 +- .../scripts/etage_plan_travail_eclairage.php | 3 +- hooks/scripts/radiateurs.php | 128 ++++++++++++++++++ hooks/scripts/rdc_chambre_eclairage.php | 13 +- hooks/scripts/rdc_portes_ouvertes.php | 16 ++- hooks/scripts/rdc_salon_eclairage.php | 27 ++-- hooks/scripts/rdc_sdb_eclairage.php | 12 +- hooks/scripts/{store.php => rdc_store.php} | 75 +++++++--- hooks/scripts/rdc_wc_eclairage.php | 28 +++- moha.php | 93 ++++++++----- mqtt_functions.php | 23 ++-- topics_callbacks/linky2mqtt.php | 13 +- topics_callbacks/pws2mqtt.php | 24 +++- topics_callbacks/zigbee2mqtt.php | 7 +- webserver/cmd_functions.php | 75 ++++++---- webserver/webserver.php | 14 +- 24 files changed, 534 insertions(+), 191 deletions(-) create mode 100644 config/propertiesbytype.php create mode 100644 hooks/scripts/radiateurs.php rename hooks/scripts/{store.php => rdc_store.php} (51%) diff --git a/class/db.php b/class/db.php index d74fcb2..a584af2 100644 --- a/class/db.php +++ b/class/db.php @@ -81,7 +81,7 @@ class db extends mysqli function moyenne($deviceObject, $property, $time) { - $query = "SELECT AVG(value) WHERE device='" . $deviceObject->ieeeAddress . "' AND property='" . $property . "' AND TIMEDIFF(NOW(), 'date') < " . $time; + $query = "SELECT AVG(value) FROM logs WHERE device='" . $deviceObject->ieeeAddress . "' AND property='" . $property . "' AND TIMEDIFF(NOW(), 'date') < " . $time; if(!$this->result = $this->query($query)) { logger(ERROR, _("mysql query errror: ") . $this->error, __FILE__ . ":" . __LINE__); diff --git a/class/hook_class.php b/class/hook_class.php index 8fa6760..49bed85 100644 --- a/class/hook_class.php +++ b/class/hook_class.php @@ -5,40 +5,45 @@ class hook public $active = true; public $initialized = false; protected $devicelist; + protected $propertyInitialized; // list of devices we are listening to function __construct() { logger(INFO, _("Initializing hook: ") . $this->hookName); - $this->installHooks(); + //$this->installHooks(); + foreach ($this->devicelist as $ieeeAddress => $property) + { + $this->propertyInitialized[$ieeeAddress][$property] = false; + } if (method_exists($this, "init")) { $this->init(); } } - function installHooks() + function installHooks(&$indexDevices) { - global $indexDevices; + global $devices; $result = true; // assigne the function to the sensors devices if ($this->active === true) { - foreach ($this->devicelist as $ieeeAddress => $property2change) + foreach ($this->devicelist as $ieeeAddress => $property) { logger(DEBUG, _("Device: ") . $ieeeAddress, __FILE__ . ":" . __LINE__); - if ($property2change[1] === false) + if ($this->propertyInitialized[$ieeeAddress][$property] === false) { logger(DEBUG, _("Trying to store callback"), __FILE__ . ":" . __LINE__); - if (isset($indexDevices[$ieeeAddress])) + if (isset($indexDevices[$ieeeAddress]->properties[$property]["functions"])) { - $property = $property2change[0]; - $indexDevices[$ieeeAddress]->properties[$property]["functions"][] = array($this,"callback"); - $property2change[1] = true; - logger(DEBUG, sprintf(_("Property '%s' is initialized with callback"), $property2change[0]), __FILE__ . ":" . __LINE__); + $indexDevices[$ieeeAddress]->properties[$property]["functions"][$this->hookName] = array($this,"callback"); + $this->propertyInitialized[$ieeeAddress][$property] = true; + logger(DEBUG, sprintf(_("Property '%s' of %s is initialized with callback"), $property, $indexDevices[$ieeeAddress]->friendlyName), __FILE__ . ":" . __LINE__); + }else { - logger(WARNING, sprintf(_("Hook %s can not initialize Property '%s' of device %s"), $this->hookName, $property2change[0], $ieeeAddress), __FILE__ . ":" . __LINE__); + logger(WARNING, sprintf(_("Hook %s can not initialize Property '%s' of device %s"), $this->hookName, $property, $ieeeAddress), __FILE__ . ":" . __LINE__); $result = false; } }else @@ -53,12 +58,13 @@ class hook logger(INFO, $this->hookName . _(" initialized"), __FILE__ . ":" . __LINE__); //var_dump($this); } - }else + }/*else { $this->initialized = true; logger(INFO, $this->hookName . _("hook is disabled"), __FILE__ . ":" . __LINE__); - } - + }*/ + //print(var_export($indexDevices[$ieeeAddress],true)); + //storeDB($devices,"debug.devices"); return $result; } } diff --git a/class/main.php b/class/main.php index 8a880e0..7c14d3c 100644 --- a/class/main.php +++ b/class/main.php @@ -127,7 +127,7 @@ class watch return $this; } - public function notifyCallback(&$device, $property, $value) + public function notifyCallback($device, $property, $value) { if (eval($this->function)) { diff --git a/config/dashboard_conf.php b/config/dashboard_conf.php index 4d11f4f..72146b5 100644 --- a/config/dashboard_conf.php +++ b/config/dashboard_conf.php @@ -5,6 +5,8 @@ array(RDC_SDB_WC_ECLAIRAGE, "state_l2"), array(RDC_CHAMBRE_AMBIANCE, "state"), array(RDC_CHAMBRE_ECLAIRAGE, "state_l1"), + array(RDC_SDB_TEMP_HUM, "temperature"), + array(RDC_SDB_RADIATEUR, "current_heating_setpoint"), array(METEO, "tempc"), array(METEO, "humidity"), array(METEO, "winddir"), diff --git a/config/devices_constants.php b/config/devices_constants.php index f3cc147..9531045 100644 --- a/config/devices_constants.php +++ b/config/devices_constants.php @@ -10,19 +10,35 @@ $deviceTable = array( "0x00124b0022ebac5c" => "RDC_SALON_MVMT2", "0x588e81fffe2cf695" => "RDC_SALON_MVMT", "0x00124b001f900753" => "RDC_ENTREE_PORTE", + "0x00158d0001923c21" => "RDC_CHAMBRE_RADIATEUR", "0x04cf8cdf3c78aff0" => "RDC_SALON_LUMINOSITE", "0x588e81fffe343e8f" => "RDC_SALON_ECLAIRAGE_PANNEAU", - "0x00158d0003f0f3b4" => "RDC_SDB_DOUCHE_MVMT", + "0x804b50fffef31656" => "RDC_SALON_RADIATEUR", + "0xbc33acfffe458c65" => "RDC_SDB_RADIATEUR", + "0xa4c138b5a98db785" => "RDC_SDB_DOUCHE_MVMT", "0x842e14fffe1c0cd1" => "RDC_SDB_PLAFOND_MVMT", "0x00124b0022ec05dc" => "RDC_SDB_MVMT", "0x00158d0005c1a998" => "RDC_SDB_WC_ECLAIRAGE", + "0xbc33acfffe458c65" => "RDC_SDB_RADIATEUR", + "0x00158d000533c45e" => "RDC_SDB_TEMP_HUM", + "0xa4c1384a2b586da9" => "RDC_WC_MVMT", "0x00124b001f90ee7e" => "ENTREE_PORTE", "0x00124b002226d9a2" => "GARAGE_PORTE", "0x00124b001f90e725" => "RDC_CHAMBRE_BAIE", "0x00124b002226e384" => "RDC_SALON_BAIE", + "0xbc33acfffe4583f7" => "ETAGE_SALON_RADIATEUR", +// "" => "ETAGE_CHAMBRE_RADIATEUR", +// "" => "ETAGE_SDB_RADIATEUR", + "0x5c0272fffec4de9e" => "ETAGE_BUREAU_RADIATEUR", + "0x00158d0001a0028c" => "ETAGE_BUREAU_FENETRE", + "0x00158d0001de080d" => "ETAGE_CHAMBRE_FENETRE", "0x842e14fffe8c53a5" => "ETAGE_CUISINE_PLAN_TRAVAIL_INTER", "0x680ae2fffe403f22" => "ETAGE_CUISINE_PLAN_TRAVAIL_ECLAIRAGE", "0xbc33acfffe65621b" => "ETAGE_CUISINE_PLAN_TRAVAIL_SPOT", + "0x00158d0001ddefe1" => "ETAGE_ENTREE_PORTE", + "0x00158d00044fd579" => "ETAGE_SALON_FENETRE", + "0x00158d0004621e1f" => "ETAGE_CUISINE_FENETRE", + "0x00158d0001ddefe1" => "ETAGE_PORTE_FENETRE", "0x086bd7fffe5aeab6" => "RDC_CHAMBRE_AMBIANCE", "0xbc33acfffe6561e4" => "RDC_CHAMBRE_ECLAIRAGE", "0x00124b00234484ad" => "RDC_CHAMBRE_MVMT", diff --git a/config/propertiesbytype.php b/config/propertiesbytype.php new file mode 100644 index 0000000..1d0dedc --- /dev/null +++ b/config/propertiesbytype.php @@ -0,0 +1,18 @@ + array( + "state", + "brightness", + "state_l1", + "state_l2" + ) + "climate" => array( + "local_temperature", + "value", + "current_heating_setpoint", + ) + ); + + +?> diff --git a/db_functions.php b/db_functions.php index 8a524ce..f7d2453 100644 --- a/db_functions.php +++ b/db_functions.php @@ -5,18 +5,29 @@ logger(DEBUG,_("Including db_functions.php"), __FILE__ . ":" . __LINE__); //to save or not to save the DB, that is the question ... function storeDB($db, $filepath) { - $data = serialize($db); - return file_put_contents($filepath, $data); - } + logger(DEBUG,_("Storing database ") . $filepath, __FILE__ . ":" . __LINE__); + $data = serialize($db); + if (file_put_contents($filepath, $data) === false) + { + logger(DEBUG,_("Failed storing database ") . $filepath , __FILE__ . ":" . __LINE__); + return false; + }else + { + return true; + } +} -function loadDB(& $db, $filepath) +function loadDB($filepath) { + logger(DEBUG,_("Loading database ") . $filepath, __FILE__ . ":" . __LINE__); $data = file_get_contents($filepath); if ($data === false) { + logger(DEBUG,_("Failed loading database ") . $filepath , __FILE__ . ":" . __LINE__); return false; } $db = unserialize($data); + return $db; } function mkDevicesDB($topic, $json, $group=false) @@ -52,14 +63,15 @@ function mkDevicesDB($topic, $json, $group=false) { //print_r($device); $device["device"]->groupID = $jsonDevice->id; - $indexDevices[$device["device"]->groupID] = & $device["device"]; - $indexFriendlyNames[$topic][$fn] = & $device["device"]; + //$indexDevices[$device["device"]->groupID] = & $device["device"]; + //$indexFriendlyNames[$topic][$fn] = & $device["device"]; }else { addDevice($topic, $device["device"], $fn, $jsonDevice); } } $dbInit += 1; + mkIndexes(); fwrite($logFh, "################################START##################################################"); fwrite($logFh, var_export($devices, true)); fwrite($logFh, "################################END##################################################"); @@ -84,8 +96,8 @@ function addDevice($topic, &$device, $fn, $jsonDevice ) searchPropertyValue($fn, $device, $jsonDevice, $listProperties); //indexing device - $indexDevices[$device->ieeeAddress] = & $device; - $indexFriendlyNames[$topic][$fn] = & $device; + //$indexDevices[$device->ieeeAddress] = & $device; + //$indexFriendlyNames[$topic][$fn] = & $device; } function searchPropertyKey($fn, &$device, $inputObject, $listPropertiesKeys) @@ -102,8 +114,8 @@ function searchPropertyKey($fn, &$device, $inputObject, $listPropertiesKeys) if (!array_key_exists($string, $device->properties)) { $device->properties[$string]["value"] = null; - } $device->properties[$string]["functions"] = array(); + } foreach($inputObject as $key2 => $value2) { if ($key2 != "property") @@ -162,7 +174,7 @@ function changeDevice($topic, $fn, &$device, $payloadArray) } } -function iterateDevice($topic, $fn, $parentDevice, &$properties, $payloadArray, $propertyTree="") +function iterateDevice($topic, $fn, &$parentDevice, &$properties, $payloadArray, $propertyTree="") { global $changed, $mohaDB, $testMode; //if (is_a($device, "device")) // = true if object @@ -181,13 +193,16 @@ function iterateDevice($topic, $fn, $parentDevice, &$properties, $payloadArray, if ($valueType == "array") { - logger(DEBUG,_("valueType == array"), __FILE__ . ":" . __LINE__); + //logger(DEBUG,_("valueType == array"), __FILE__ . ":" . __LINE__); $propertyTree .= $key . "/"; - $properties[$key] = array(); + if(!array_key_exists($key, $properties)) + { + $properties[$key] = array(); + } iterateDevice($topic, $fn, $parentDevice, $properties[$key], $value, $propertyTree); }else { - logger(DEBUG,_("valueType is ") . $valueType, __FILE__ . ":" . __LINE__); + //logger(DEBUG,_("valueType is ") . $valueType, __FILE__ . ":" . __LINE__); //var_dump($properties);echo EOL; if (!array_key_exists($key, $properties)) { @@ -205,7 +220,7 @@ function getDevicesValues($topic) { global $indexDevices, $topics; logger(DEBUG, _("getDevicesValues function" ), __FILE__ . ":" . __LINE__ ); - foreach($indexDevices as $device) + foreach($indexDevices as &$device) { if ($device->topic == $topic) { @@ -220,9 +235,10 @@ function getDevicesValues($topic) { if (array_key_exists("access", $value)) { - if ($value["access"] & 5) + if ($value["access"] & 4) { $device->payload[$property] = ""; + break; } } } @@ -256,23 +272,45 @@ function changeValue(&$property, $value, &$parentDevice, $propertyTree, $key) } } -function mkIndexes($devices) +function mkIndexes() { - global $indexDevices, $indexFriendlyNames; + global $devices, $indexDevices, $indexFriendlyNames; + $device = null; logger(INFO, _("function mkIndexes"), __FILE__ . ":" . __LINE__); - foreach ($devices as $key => $object) + foreach ($devices as $key => &$object) { - //print "======####" . $key . EOL; - if (is_a($object, "device")) - { - //echo "==========>" . $object->friendlyName . EOL; - $indexDevices[$object->ieeeAddress] = &$object; - $indexFriendlyNames[$object->friendlyName] = &$object; - }else - { - mkIndexes($object); - } + print "======####" . $key . EOL; + $r = iterate2device($object); } } +function iterate2device(&$object) +{ + global $indexDevices, $indexFriendlyNames, $indexTypes; + foreach ($object as $key => &$device) + { + if (is_a($device, "device")) + { + + $object = &$device; + //print("=============" . $device->friendlyName); + if (!array_key_exists($object->ieeeAddress, $indexDevices)) + { + //print("============>"); + $indexDevices[$object->ieeeAddress] = &$object; + $indexFriendlyNames[$object->topic][$object->friendlyName] = &$object; + if (property_exists($object, "type")) + { + $indexTypes[$object->type][] = &$object; + } + } + return true; + }else + { + iterate2device($device); + } + } + return false; +} + ?> diff --git a/events.php b/events.php index 9f988e5..3381b01 100644 --- a/events.php +++ b/events.php @@ -10,7 +10,7 @@ function checkEvents() $oldLevel = $logLevel; $logLevel = DEBUG; $exception = false; - foreach ($events as $key => $event) + foreach ($events as $key => &$event) { $now = now(); if (!empty($event->startDatetime)) diff --git a/hooks/scripts/availability.php b/hooks/scripts/availability.php index f8adb6b..0ab8db0 100644 --- a/hooks/scripts/availability.php +++ b/hooks/scripts/availability.php @@ -15,7 +15,7 @@ class availability private function iterate() { global $indexDevices; - foreach ($indexDevices as $value) + foreach ($indexDevices as &$value) { $value->properties["availability"]["functions"][] = array($this,"callback"); } diff --git a/hooks/scripts/etage_plan_travail_eclairage.php b/hooks/scripts/etage_plan_travail_eclairage.php index d76fc7a..bca8353 100644 --- a/hooks/scripts/etage_plan_travail_eclairage.php +++ b/hooks/scripts/etage_plan_travail_eclairage.php @@ -6,7 +6,7 @@ class etage_plan_travail_eclairage extends hook public $active = false; //enable/disable hook (true => enabled) public $delay = 3; // amount of time in $timeunit public $timeUnit = "hour"; // unit of time for delay, second, minute, hour, day, week, month, year - protected $devicelist = array(ETAGE_CUISINE_PLAN_TRAVAIL_ECLAIRAGE => array("state", false)); + protected $devicelist = array(ETAGE_CUISINE_PLAN_TRAVAIL_ECLAIRAGE => "state"); // callback fonction. Is called with these 3 parameters // $device -> calling device @@ -14,6 +14,7 @@ class etage_plan_travail_eclairage extends hook // $value -> value of the property public function callBack($device, $property, $value) { + logger(DEBUG, sprintf(_("property=%s, value=%s"), $property, $value), __FILE__ . ":" . __LINE__); // here your code if($value == "ON") // ON { diff --git a/hooks/scripts/radiateurs.php b/hooks/scripts/radiateurs.php new file mode 100644 index 0000000..41c6617 --- /dev/null +++ b/hooks/scripts/radiateurs.php @@ -0,0 +1,128 @@ + property to watch + // devicelist[$ieeAddress][1] => initialized = true + protected $devicelist = array( + RDC_CHAMBRE_BAIE => "contact", + RDC_SALON_BAIE => "contact", + RDC_ENTREE_PORTE => "contact", + ETAGE_ENTREE_PORTE => "contact", + ETAGE_CUISINE_FENETRE => "contact", + ETAGE_SALON_FENETRE => "contact", + ETAGE_SALON_PORTE_FENETRE => "contact", + ETAGE_CHAMBRE_FENETRE => "contact", + ETAGE_BUREAU_FENETRE => "contact" + ); + + PROTECTED $hvac = array( + RDC_CHAMBRE_BAIE => RDC_CHAMBRE_RADIATEUR, + RDC_SALON_BAIE => RDC_SALON_RADIATEUR, + RDC_ENTREE_PORTE => RDC_SALON_RADIATEUR, + RDC_GARAGE_PORTE => RDC_SDB_RADIATEUR, + ETAGE_ENTREE_PORTE => ETAGE_SALON_RADIATEUR, + ETAGE_CUISINE_FENETRE => ETAGE_SALON_RADIATEUR, + ETAGE_SALON_FENETRE => ETAGE_SALON_RADIATEUR, + ETAGE_SALON_PORTE_FENETRE => ETAGE_SALON_RADIATEUR, +// ETAGE_CHAMBRE_FENETRE => ETAGE_CHAMBRE_RADIATEUR, + ETAGE_BUREAU_FENETRE => ETAGE_BUREAU_RADIATEUR, +// ETAGE_SDB_FENETRE => ETAGE_SDB_RADIATEUR + ); + + protected $prevTemp = array( + RDC_CHAMBRE_RADIATEUR => 0, + RDC_SALON_RADIATEUR => 0, + RDC_SDB_RADIATEUR => 0, + ETAGE_SALON_RADIATEUR => 0, + ETAGE_BUREAU_RADIATEUR => 0 +// ETAGE_SDB_RADIATEUR => 0, +// ETAGE_CHAMBRE_RADIATEUR => 0 + ); + + // callback fonction. Is called with these 3 parameters + public function callBack(&$device, $property, $value) + { + global $indexDevices; + $now = date("MM"); + if ($now <= 5 or $now >= 11) + { + if ($value === false) + { + send($indexDevices[$hvac[$device->ieeeAddress]], true); + /* switch ($device->$ieeeAdress) + { + send($indexDevices[$hvac[$device->ieeeAddress]], false); + case RDC_CHAMBRE_BAIE: + break; + case RDC_SALON_BAIE: + case RDC_ENTREE_PORTE: + break; + case RDC_GARAGE_PORTE: + break; + case ETAGE_CUISINE_FENETRE: + case ETAGE_SALON_FENETRE: + case ETAGE_SALON_PORTE_FENETRE: + case ETAGE_ENTREE_PORTE: + + break; + case ETAGE_CHAMBRE_FENETRE: + + break; + case ETAGE_BUREAU_FENETRE: + + break; + }*/ + }else + { + send($indexDevices[$hvac[$device->ieeeAddress]], false); + } + } + } + + function send($device, $state) + { + global $indexDevices; + if ($state === false) + { + $t = getValue($device, "current_heating_setpoint"); + if ($t != $minTemp) + { + $prevTemp[$device] = getValue($device, "current_heating_setpoint"); + } + }else + { + $r = array_keys($hvac, $device->ieeeAddress); + foreach($r as $value) + { + if (getValue($indexDevices[$value], "contact") === true) + { + $flag = true; + break; + } + } + if ($flag === false) + { + $msg = array("current_heating_setpoint" => (!empty($prevTemp[$device])?$prevTemp[$device]:19)); + logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $deviceTarget->friendlyName), __FILE__ . ":" . __LINE__); + $deviceTarget->payload = $msg; + $deviceTarget->set(); + $deviceTarget->method = AUTO; + } + } + } +} +$hooks["radiateurs"] = new radiateurs(); + +?> diff --git a/hooks/scripts/rdc_chambre_eclairage.php b/hooks/scripts/rdc_chambre_eclairage.php index ec4cdd9..22ba373 100644 --- a/hooks/scripts/rdc_chambre_eclairage.php +++ b/hooks/scripts/rdc_chambre_eclairage.php @@ -9,10 +9,10 @@ class rdc_chambre_eclairage extends hook public $luminance_min = 60; public $luminance_max = 3000; protected $devicelist = array( - RDC_CHAMBRE_AMBIANCE => array("state", false), // "ON"/"OFF" - RDC_CHAMBRE_ECLAIRAGE => array("state_l1", false), // "ON"/"OFF" - RDC_CHAMBRE_MVMT => array("occupancy", false), - RDC_CHAMBRE_ARMOIRE_GAUCHE => array("contact", false) + RDC_CHAMBRE_AMBIANCE => "state", // "ON"/"OFF" + RDC_CHAMBRE_ECLAIRAGE => "state_l1", // "ON"/"OFF" + RDC_CHAMBRE_MVMT => "occupancy", + RDC_CHAMBRE_ARMOIRE_GAUCHE => "contact" ); // callback fonction. Is called with these 3 parameters @@ -25,9 +25,10 @@ class rdc_chambre_eclairage extends hook $lux = $indexDevices[RDC_CHAMBRE_LUMINOSITE]->properties["illuminance_lux"]; $targetAmbiance = $indexDevices[RDC_CHAMBRE_AMBIANCE]; $targetEclairage = $indexDevices[RDC_CHAMBRE_ECLAIRAGE]; - + logger(DEBUG, sprintf(_("property=%s, value=%s"), $property, $value), __FILE__ . ":" . __LINE__); if ($property == "occupancy" and $value == ON) { + logger(DEBUG, _("CASE: occupancy"), __FILE__ . ":" . __LINE__); $this->send($targetAmbiance, "state", "ON", "OFF", AUTO); }elseif ($property == "contact") { @@ -42,7 +43,7 @@ class rdc_chambre_eclairage 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__); } - private function send($deviceObject, $property, $state, $delayState = false, $method = MANUAL) + private function send(&$deviceObject, $property, $state, $delayState = false, $method = MANUAL) { global $indexDevices; $msg = array($property => $state); diff --git a/hooks/scripts/rdc_portes_ouvertes.php b/hooks/scripts/rdc_portes_ouvertes.php index 783952e..4a390c1 100644 --- a/hooks/scripts/rdc_portes_ouvertes.php +++ b/hooks/scripts/rdc_portes_ouvertes.php @@ -5,26 +5,28 @@ class alerte_intrusion extends hook public $active = true; protected $devicelist = array( - ENTREE_PORTE => array("contact", false), - GARAGE_PORTE => array("contact", false), - RDC_CHAMBRE_BAIE => array("contact", false), - RDC_SALON_BAIE => array("contact", false)); + 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 // callback fonction. Is called with these 4 parameters - public function callBack($device, $param, $value) + public function callBack($device, $property, $value) { - switch($param) + + switch($property) { case "contact": if ($value == false) { logger(ALERT, sprintf(_("%s est ouverte alors que personne n'est présent"), $device->friendlyName), __FILE__ . ":" . __LINE__); } - break; + break; } } } diff --git a/hooks/scripts/rdc_salon_eclairage.php b/hooks/scripts/rdc_salon_eclairage.php index 7daca51..65449cd 100644 --- a/hooks/scripts/rdc_salon_eclairage.php +++ b/hooks/scripts/rdc_salon_eclairage.php @@ -6,10 +6,10 @@ class rdc_salon_eclairage extends hook public $active = true; // list of devices we are listening to protected $devicelist = array( - RDC_SALON_MVMT => array("occupancy", false), - RDC_SALON_MVMT2 => array("occupancy", false), - RDC_ENTREE_PORTE => array("contact", false), - RDC_SALON_LUMINOSITE => array("illuminance_lux", false) + RDC_SALON_MVMT => "occupancy", + RDC_SALON_MVMT2 => "occupancy", + RDC_ENTREE_PORTE => "contact", + RDC_SALON_LUMINOSITE => "illuminance_lux" ); public $delay = 3; // amount of time in $timeunit @@ -18,7 +18,7 @@ class rdc_salon_eclairage extends hook public $luminance_max = 100; // callback fonction. Is called with these 4 parameters - public function callBack(&$device, $param, $value) + public function callBack($device, $param, $value) { global $indexDevices; logger(INFO, _("hook : rdc_salon_eclairage"), __FILE__ . ":" . __LINE__); @@ -33,12 +33,17 @@ class rdc_salon_eclairage extends hook case "occupancy": logger(INFO, _("CASE: Occupancy => ") . bool2string($value), __FILE__ . ":" . __LINE__); //print_r(getValue(RDC_SALON_LUMINOSITE, "illuminance_lux")); - if ($value == ON and getValue(RDC_SALON_LUMINOSITE, "illuminance_lux") <= $this->luminance_min) + if ($value == ON) { - logger(INFO, _("setting to ON"), __FILE__ . ":" . __LINE__); - $this->send($deviceTarget, "ON", null, AUTO); - removeEvent($deviceTarget, "state", "OFF"); - }elseif ($value == OFF) + $illuminance = getValue(RDC_SALON_LUMINOSITE, "illuminance_lux"); + logger(INFO, _("illuminance value : ") . $illuminance, __FILE__ . ":" . __LINE__); + if ($illuminance <= $this->luminance_min) + { + logger(INFO, _("setting to ON"), __FILE__ . ":" . __LINE__); + $this->send($deviceTarget, "ON", null, AUTO); + removeEvent($deviceTarget, "state", "OFF"); + } + }else { logger(INFO, _("Value is OFF"), __FILE__ . ":" . __LINE__); if (getValue(RDC_SALON_MVMT, "occupancy") == OFF and (getValue(RDC_SALON_MVMT2, "occupancy") == OFF)) @@ -74,7 +79,7 @@ class rdc_salon_eclairage extends hook { setDelay($deviceTarget, $this->delay, $this->timeUnit, "state", "OFF", true); } - }elseif ($value <= $this->luminance_min and (getValue(RDC_SALON_MVMT, "occupancy") == ON OR getValue(RDC_SALON_MVMT2,"occupancy") == ON)) + }elseif ($value <= $this->luminance_min and (getValue(RDC_SALON_MVMT, "occupancy") == ON || getValue(RDC_SALON_MVMT2,"occupancy") == ON)) { logger(INFO, _("illuminance < min and movement detected"), __FILE__ . ":" . __LINE__); $this->send($deviceTarget, "ON", null, AUTO); diff --git a/hooks/scripts/rdc_sdb_eclairage.php b/hooks/scripts/rdc_sdb_eclairage.php index 10528a6..8667f1c 100644 --- a/hooks/scripts/rdc_sdb_eclairage.php +++ b/hooks/scripts/rdc_sdb_eclairage.php @@ -17,10 +17,10 @@ class rdc_sdb_eclairage extends hook // devicelist[$ieeAddress][0] => property to watch // devicelist[$ieeAddress][1] => initialized = true protected $devicelist = array( - //RDC_SDB_DOUCHE_MVMT => array("occupancy", false), - RDC_SDB_PLAFOND_MVMT => array("occupancy", false), - RDC_SDB_MVMT => array("occupancy", false), - RDC_SDB_WC_ECLAIRAGE => array("state_l1", false) + RDC_SDB_DOUCHE_MVMT => "occupancy", + RDC_SDB_PLAFOND_MVMT => "occupancy", + RDC_SDB_MVMT => "occupancy", + RDC_SDB_WC_ECLAIRAGE => "state_l1" ); // callback fonction. Is called with these 3 parameters @@ -29,10 +29,11 @@ class rdc_sdb_eclairage extends hook global $indexDevices; //var_dump($value); $deviceTarget = $indexDevices[RDC_SDB_WC_ECLAIRAGE]; - + logger(DEBUG, sprintf(_("property=%s, value=%s"), $property, $value), __FILE__ . ":" . __LINE__); switch($property) { case "occupancy": + logger(DEBUG, _("CASE: occupancy"), __FILE__ . ":" . __LINE__); if ($value == ON) { if (getValue($deviceTarget->ieeeAddress, "state_l1") != "ON") @@ -44,6 +45,7 @@ class rdc_sdb_eclairage extends hook } break; case "state_l1": + logger(DEBUG, _("CASE: state_l1"), __FILE__ . ":" . __LINE__); if ($value == "ON") { setDelay($deviceTarget, $this->delay, $this->timeUnit, "state_l1", "OFF", true); diff --git a/hooks/scripts/store.php b/hooks/scripts/rdc_store.php similarity index 51% rename from hooks/scripts/store.php rename to hooks/scripts/rdc_store.php index ca4d658..3317984 100644 --- a/hooks/scripts/store.php +++ b/hooks/scripts/rdc_store.php @@ -7,16 +7,16 @@ class rdc_store extends hook public $timer = 0; public $luminance_min = 60; public $luminance_max = 3000; - public $storeDownTime = 20; - public $storeUpTime = 25; + public $storeDownTime = 38; + public $storeUpTime = 41; protected $storeLevel; - + protected $propertyInitialized =array(); protected $devicelist = array( - RDC_EXTERIEUR_LUMINOSITE => array("illuminance_lux", false), // "ON"/"OFF" - METEO => array("rainin", false), + RDC_EXTERIEUR_LUMINOSITE => "illuminance_lux", // "ON"/"OFF" + METEO => "rainin", //METEO => array("solarradiation", false), - METEO => array("windspeedkmh", false), - METEO => array("windgustkmh", false) + METEO => "windspeedkmh", + METEO => "windgustkmh" ); // callback fonction. Is called with these 3 parameters @@ -27,22 +27,50 @@ class rdc_store extends hook { global $mohaDB, $devices, $indexDevices; + logger(DEBUG, "Callback : RDC_STORE", __FILE__ . ":" . __LINE__); + $rain = 0; + $exterieur_lux = 0; + $rafale = 0; + $soleil = 0; $storeDevice = $indexDevices[RDC_STORE]; - $storeLevel = $storeDevice->properties["position"]; - $exterieur_lux = $mohaDB->moyenne($indexDevices[RDC_EXTERIEUR_LUMINOSITE], "illuminance_lux", 15); - $rain = $indexDevices[METEO]->properties["rainin"]; - $rafale = $mohaDB->moyenne($indexDevices[METEO], "windgustkmh", 12); - $soleil = $mohaDB->moyenne($indexDevices[METEO], "solarradiation", 12); + if (array_key_exists("position", $storeDevice->properties)) + { + $storeLevel = $storeDevice->properties["position"]; + }else + { + return true; + } + if (array_key_exists("illuminance_lux", $indexDevices[RDC_EXTERIEUR_LUMINOSITE]->properties)) + { + $exterieur_lux = $mohaDB->moyenne($indexDevices[RDC_EXTERIEUR_LUMINOSITE], "illuminance_lux", 15); + } + if (array_key_exists("rainin", $indexDevices[METEO]->properties)) + { + $rain = $indexDevices[METEO]->properties["rainin"]; + } + if (array_key_exists("windgustkmh", $indexDevices[METEO]->properties)) + { + $rafale = $mohaDB->moyenne($indexDevices[METEO], "windgustkmh", 12); + } + if (array_key_exists("solarradiation", $indexDevices[METEO]->properties)) + { + $soleil = $mohaDB->moyenne($indexDevices[METEO], "solarradiation", 12); + } + logger(DEBUG, sprintf(_("property=%s, value=%s"), $property, $value), __FILE__ . ":" . __LINE__); if ($rafale >= 6 or $rain != 0) { $maxLevel = 0; }else { - $maxLevel = 100 / $rafale; + if ($rafale != 0) + { + $maxLevel = 100 / $rafale; + } } switch ($property) { case "illuminance_lux": + logger(DEBUG, "CASE: illuminance_lux:" . $value, __FILE__ . ":" . __LINE__); if ($exterieur_lux > 3000) { if ($rain == 0 and $maxLevel != 0) @@ -62,26 +90,29 @@ class rdc_store extends hook } break; case "rainin": + logger(DEBUG, "CASE: rainin:" . $value, __FILE__ . ":" . __LINE__); $this->close("Pluie"); break; case "windgustkmh"; + logger(DEBUG, "CASE: windgustkmh:" . $value, __FILE__ . ":" . __LINE__); case "windspeedkmh": + logger(DEBUG, "CASE: windspeedkmh:" . $value, __FILE__ . ":" . __LINE__); if($value > 50) { $this->close("Trop de vent"); - }elseif ($value > 40 and $level > 20) + }elseif ($value > 40 and $this->storeLevel > 20) { $this->maxLevel = 20; $this->send(20); - }elseif ($value > 30 and $level > 40) + }elseif ($value > 30 and $this->storeLevel > 40) { $this->maxLevel = 40; $this->send(40); - }elseif ($value >20 and $level > 60) + }elseif ($value >20 and $this->storeLevel > 60) { $this->maxLevel = 60; $this->send(60); - }elseif ($value >10 and $level > 80) + }elseif ($value >10 and $this->storeLevel > 80) { $this->maxLevel = 80; $this->send(80); @@ -91,6 +122,8 @@ class rdc_store extends hook } $this->close("Fortes rafales de vent"); break; + default: + } } @@ -98,7 +131,8 @@ class rdc_store extends hook { if ($this->storeLevel < $level) { - $this->send($level); + logger(DEBUG, "Open store :" . $level, __FILE__ . ":" . __LINE__); + $this->send(100 - $level); } } @@ -106,7 +140,9 @@ class rdc_store extends hook { if ($this->storeLevel != 0) { + logger(DEBUG, "Close store :" . $level, __FILE__ . ":" . __LINE__); print ( "store fermé " . $reason); + $this->send(100); } } @@ -117,8 +153,7 @@ class rdc_store extends hook $msg = array("position" => $level); logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $deviceObject->friendlyName), __FILE__ . ":" . __LINE__); $deviceObject->payload = $msg; - $deviceObject->set(); - $deviceObject->method = AUTO; + $deviceObject->set(AUTO); } } diff --git a/hooks/scripts/rdc_wc_eclairage.php b/hooks/scripts/rdc_wc_eclairage.php index f0cedbc..bc467a5 100644 --- a/hooks/scripts/rdc_wc_eclairage.php +++ b/hooks/scripts/rdc_wc_eclairage.php @@ -4,20 +4,24 @@ class rdc_wc_eclairage extends hook { public $hookName = "rdc_wc_eclairage"; - protected $devicelist = array(RDC_SDB_WC_ECLAIRAGE => array("state_l2", false)); + protected $devicelist = array( + RDC_SDB_WC_ECLAIRAGE => "state_l2", + RDC_WC_MVMT => "occupancy" + ); public $delay = 3; // amount of time in $timeunit public $delayManual = 8; // amount of time in $timeunit for manual mode public $timeUnit = "minute"; // unit of time for delay, second, minute, day, week, month, year // callback fonction. Is called with these 4 parameters - public function callBack($device, $property, $value) + public function callBack(&$device, $property, $value) { global $indexDevices; //var_dump($value); switch($property) { case "state_l2": + logger(DEBUG, _("CASE: state_l2"), __FILE__ . ":" . __LINE__); if ($value == "ON") { setDelay($device, $this->delayManual, $this->timeUnit, "state_l2", "OFF", true); @@ -27,9 +31,29 @@ class rdc_wc_eclairage extends hook deleteEvent(searchEvent($device, "state_l2", "OFF")); } break; + case "occupancy": + logger(DEBUG, _("CASE: occupancy"), __FILE__ . ":" . __LINE__); + if ($value == true) + { + $this->send("ON"); + } } logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, bool2string($value)), __FILE__ . ":" . __LINE__); } + private function send($state) + { + global $indexDevices; + $deviceObject = $indexDevices[RDC_SDB_WC_ECLAIRAGE]; + $msg = array("state_l2" => $state); + logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $deviceObject->friendlyName), __FILE__ . ":" . __LINE__); + $deviceObject->payload = $msg; + $deviceObject->set(null); + if ($deviceObject->method != MANUAL) + { + $deviceObject->method = AUTO; + } + setDelay($deviceObject, $this->delay, $this->timeUnit, "state_l2", "OFF", true); + } } $hooks["rdc_wc_eclairage"] = new rdc_wc_eclairage(); diff --git a/moha.php b/moha.php index e4b0997..0ca1fc1 100644 --- a/moha.php +++ b/moha.php @@ -20,6 +20,7 @@ $mids = array(); // list of message IDs $devices = array(); // array of device objetcs $indexDevices = array(); // index of devices by ieee_address $indexFriendlyNames = array(); // index of devices by freindly name +$indexTypes = array(); // index of devices by type $hooksList = array(); // list of hooks to be included $hooks = array(); // array of hooks $notificationMethods = array(); // array of notification methods objects @@ -75,7 +76,7 @@ function notify($message) function logger($level, $log, $pos = "", $notif = true) { global $logFh, $logLevel, $notificationLevel, $logLevels; - $logString = date("c") . ' ' . $logLevels[$level] . " " ; + $logString = $logLevels[$level] . " " ; if ($pos !== false) { $logString .= $pos; @@ -83,8 +84,8 @@ function logger($level, $log, $pos = "", $notif = true) $logString .= " - " . $log; if ($level & $logLevel) { - fwrite($logFh, $logString . EOL); - print ($logString . EOL); + fwrite($logFh, date("c") . ' ' . $logString . EOL); + print ("MOHA-" . $logString . EOL); } $test = $level & $notificationLevel; if (($test != 0) and ($notif === true)) @@ -135,9 +136,13 @@ function endMoha() $x = 0; logger(WARNING, _("moha is ending"), __FILE__ . ":" . __LINE__); - if (storeDB($devices, $dataPath . "moha.db") === false) + if (storeDB($devices, $dataPath . "devices.db") === false) { - logger(ERROR, _("Can not store db" ), __FILE__ . ":" . __LINE__); + logger(ERROR, _("Can not store devices db" ), __FILE__ . ":" . __LINE__); + } + if (storeDB($topics, $dataPath . "topics.db") === false) + { + logger(ERROR, _("Can not store topics db" ), __FILE__ . ":" . __LINE__); } if($testMode) file_put_contents($dataPath . "moha.devices", var_export($devices, true)); if ($connected) @@ -173,11 +178,6 @@ if (is_readable($configDir . "/devices_constants.php")) include $configDir . "/devices_constants.php"; //echo "hooklist"; print_r($hooksList); echo EOL; logger(INFO, sprintf(_("%s/devices_constants.define found, so it has been included :-)"), $configDir), __FILE__ . ":" . __LINE__, false); -}elseif(is_readable("config/devices_constants.php")) -{ - include "config/devices_constants.php"; - //echo "hooklist"; print_r($hooksList); echo EOL; - logger(INFO, sprintf(_("%s/devices_constants.define found, so it has been included :-)"), $configDir), __FILE__ . ":" . __LINE__, false); }else { logger(WARNING, sprintf(_("%s/devices_constants.define not found, so not included :-)"), $configDir), __FILE__ . ":" . __LINE__, false); @@ -194,17 +194,6 @@ require "events.php"; require "db_functions.php"; require "webserver/webserver.php"; -logger(DEBUG, _("Loading stored devices datas from ") . $dataPath . "moha.db", __FILE__ . ":" . __LINE__); -if (loadDB($devices, $dataPath . "moha.db") === false) -{ - logger(ERROR, _("Can not load device db"), __FILE__ . ":" . __LINE__); -}else -{ - mkIndexes($devices); -} - - - // topics definition listHooks("./topics_callbacks", $hooksList); if (!empty($hooksList)) @@ -216,9 +205,42 @@ if (!empty($hooksList)) } } - // making the list of hooks to include listHooks("./hooks", $hooksList); +if (!empty($hooksList)) // some hooks to include if hooklist is not empty +{ + foreach ($hooksList as $hookFile) // loop to include hooks in hookslist + { + logger(INFO, _("Including ") . $hookFile, __FILE__ . ":" . __LINE__, false); + include_once $hookFile; + } + if ($logLevel & DEBUG) + { + file_put_contents($dataPath . "moha.devices", var_export($devices, true)); // debugging : save device list + } + $included = true; +} + +logger(DEBUG, _("Loading stored topics datas from ") . $dataPath . "topics.db", __FILE__ . ":" . __LINE__); +if (file_exists($dataPath . "topics.db")) +{ + if (($topics = loadDB($dataPath . "topics.db")) === false) + { + logger(ERROR, _("Can not load topics db"), __FILE__ . ":" . __LINE__); + } +} + +logger(DEBUG, _("Loading stored devices datas from ") . $dataPath . "devices.db", __FILE__ . ":" . __LINE__); +if (file_exists($dataPath . "devices.db")) +{ + if (($devices = loadDB($dataPath . "devices.db")) === false) + { + logger(ERROR, _("Can not load devices db"), __FILE__ . ":" . __LINE__); + }else + { + mkIndexes(); + } +} // Program start logger(DEBUG, _("Program start"), __FILE__ . ":" . __LINE__, false); @@ -247,7 +269,7 @@ $client->onPublish('publishResponse'); $client->connect($mqttServerIp, 1883, 5); logger(INFO, _("Subscribing to bridge"), __FILE__ . ":" . __LINE__, false); -foreach($topics as $name => $topic) +foreach($topics as $name => &$topic) { $topic->mid = $client->subscribe($name . "/#", 2); $mids[$topic->mid] = $name; @@ -261,23 +283,24 @@ $oneshot = false; while (true) { $client->loop(); // mqtt server loop() - if (! $included) // hooks not already included + /*if (! $included) // hooks not already included { - logger(DEBUG, _("Making hooks list"), __FILE__ . ":" . __LINE__, false); - + logger(DEBUG, _("Loading hooks list"), __FILE__ . ":" . __LINE__, false); if (!empty($hooksList)) // some hooks to include if hooklist is not empty { foreach ($hooksList as $hookFile) // loop to include hooks in hookslist { logger(INFO, _("Including ") . $hookFile, __FILE__ . ":" . __LINE__, false); - include $hookFile; + include_once $hookFile; + } + if ($logLevel & DEBUG) + { + file_put_contents($dataPath . "moha.devices", var_export($devices, true)); // debugging : save device list } - file_put_contents($dataPath . "moha.devices", var_export($devices, true)); // debugging : save device list $included = true; } - }else - { + {*/ if ($oneshot === false) // execute while the first loop :WARNING hooks can to be not initialized { @@ -285,15 +308,16 @@ while (true) $oneshot = true; } + $nn = 0; if($hooksInitialized == 0) // all hooks are not initialized { $i = 1; - foreach($hooks as $hookName => $hook) + foreach($hooks as $hookName => &$hook) { - if ($hook->initialized === false) + if ($hook->initialized === false and $hook->active === true) { logger(WARNING, _("Initializing Hook not completely initialized :") . $hookName, __FILE__ . ":" . __LINE__); - $i &= $hook->installHooks(); + $i &= $hook->installHooks($indexDevices); } } $hooksInitialized = $i; @@ -301,7 +325,6 @@ while (true) { logger(DEBUG, _("All hooks initialized"), __FILE__ . ":" . __LINE__); $flagHooks = true; - }else // executed when hooks initialization finished but database init not finished { //logger(DEBUG, _("looping"), __FILE__ . ":" . __LINE__); @@ -309,7 +332,7 @@ while (true) } checkEvents(); askWebServer($read); - } + //} } endMoha(); diff --git a/mqtt_functions.php b/mqtt_functions.php index 1861fb0..65ba5cf 100644 --- a/mqtt_functions.php +++ b/mqtt_functions.php @@ -13,23 +13,30 @@ function messageReceived($message) // payload is an array : // $key is property and $value is value of the property -function publish($topic, $payload, $commande="set") //, $eventKey) +function publish($topic, $payloadArray, $commande="set") //, $eventKey) { global $client, $mids, $logFh, $testMode; $string = $topic . "/" . $commande; if ($testMode === false) { - $mid = $client->publish($string, json_encode($payload) , 2); - if (isset($mids[$mid])) + if ($client !== null) { - //echo "unsetting mids" .EOL; - unset ($mids[$mid]); + $mid = $client->publish($string, json_encode($payloadArray) , 2); + if (isset($mids[$mid])) + { + //echo "unsetting mids" .EOL; + unset ($mids[$mid]); + }else + { + //echo "setting mids" .EOL; + $mids[$mid] = true; + } + logger(LOG_INFO, $logFh, "Publishing " . $string . " with payload => " . json_encode($payloadArray), __FILE__ . ":" . __LINE__); }else { - //echo "setting mids" .EOL; - $mids[$mid] = true; + logger(ERROR, $logFh, "MQTT client is null not publishing " . $string . " with payload => " . json_encode($payloadArray), __FILE__ . ":" . __LINE__); } - logger(LOG_INFO, $logFh, "Publishing " . $string . " with payload => " . json_encode($payload), __FILE__ . ":" . __LINE__); + }else { logger(INFO, _("Test mode on: no publishing "), __FILE__ . ":" . __LINE__); diff --git a/topics_callbacks/linky2mqtt.php b/topics_callbacks/linky2mqtt.php index 10940ee..df803ef 100644 --- a/topics_callbacks/linky2mqtt.php +++ b/topics_callbacks/linky2mqtt.php @@ -1,10 +1,11 @@ callback = function($topic, $message) +function linky2mqttCallback($topic, $message) { global $topics, $logFh, $devices, $included; $topicName = $topic[0]; @@ -20,8 +21,9 @@ $topics["linky2mqtt"]->callback = function($topic, $message) $device["device"]->friendlyName = $payloadArray["friendly_name"]; $device["device"]->ieeeAddress = $payloadArray["ieeeAddress"]; $device["device"]->type = $payloadArray["type"]; - $indexDevices[$device["device"]->ieeeAddress] = & $device["device"]; - $indexFriendlyNames[$topicName][$fn] = & $device["device"]; + //$indexDevices[$device["device"]->ieeeAddress] = & $device["device"]; + //$indexFriendlyNames[$topicName][$fn] = & $device["device"]; + mkIndexes(); }else { $device = &$device[$fn]; @@ -34,4 +36,5 @@ $topics["linky2mqtt"]->callback = function($topic, $message) changeDevice($topicName, $friendlyName, $device["device"], $payloadArray); //print_r($device["device"]); } +$topics["linky2mqtt"]->callback = "linky2mqttCallback"; ?> diff --git a/topics_callbacks/pws2mqtt.php b/topics_callbacks/pws2mqtt.php index daffe8d..bac835d 100644 --- a/topics_callbacks/pws2mqtt.php +++ b/topics_callbacks/pws2mqtt.php @@ -1,9 +1,8 @@ callback = function($topic, $message) +function pws2mqttCallback($topic, $message) { global $logFh, $devices, $included; $topicName = $topic[0]; @@ -21,8 +20,10 @@ $topics["pws2mqtt"]->callback = function($topic, $message) $device["device"]->ieeeAddress = $payloadArray["ieeeAddress"]; $device["device"]->type = $payloadArray["type"]; $device["device"]->topic = $topicName; - $indexDevices[$device["device"]->ieeeAddress] = & $device["device"]; - $indexFriendlyNames[$topicName][$fn] = & $device["device"]; + //$indexDevices[$device["device"]->ieeeAddress] = & $device["device"]; + //$indexFriendlyNames[$topicName][$fn] = & $device["device"]; + mkIndexes(); + }else { $device = &$device[$fn]; @@ -54,5 +55,16 @@ $topics["pws2mqtt"]->callback = function($topic, $message) { changeDevice($topicName, $fn, $device["device"], $payloadArray); } +}; +$topics["pws2mqtt"]->callback = "pws2mqttCallback"; + +if (!is_callable("pws2mqttGetList")) +{ + function pws2mqttGetList() + { + publish("pws2mqtt", array("",""), "get" ); + } } + +pws2mqttGetList(); ?> diff --git a/topics_callbacks/zigbee2mqtt.php b/topics_callbacks/zigbee2mqtt.php index eb4de77..647d113 100644 --- a/topics_callbacks/zigbee2mqtt.php +++ b/topics_callbacks/zigbee2mqtt.php @@ -1,9 +1,9 @@ callback = function($topic, $message) +function zigbee2mqttCallback($topic, $message) { global $topics, $devices, $included, $logFh, $indexFriendlyNames; @@ -85,5 +85,6 @@ $topics["zigbee2mqtt"]->callback = function($topic, $message) } }; +$topics["zigbee2mqtt"]->callback = "zigbee2mqttCallback"; ?> diff --git a/webserver/cmd_functions.php b/webserver/cmd_functions.php index bf93e0a..c7064df 100644 --- a/webserver/cmd_functions.php +++ b/webserver/cmd_functions.php @@ -1,7 +1,7 @@ properties)) { - $propertyObject = $device->properties[$property]; $value = $propertyObject["value"]; - if ($value === null) - { - $value = "null"; - } logger(DEBUG, $device->friendlyName . " => " . bool2string(_($value))); $response .= $device->friendlyName . aliases($device->friendlyName, $property) . " => " . bool2string(_($value)) . EOL; if (array_key_exists("access", $propertyObject)) @@ -33,17 +28,15 @@ function webDashboard($socket, $n="Général", $page="/") if(($propertyObject["access"] & 2)) { logger(DEBUG, _("Write Access OK ") . ($propertyObject["access"] & 2), __FILE__ . ":" . __LINE__); - $response .= "     " . displayChoice($device, $property) . EOL; + $response .= "     " . displayChoice($device, $property); } if(($propertyObject["access"] & 4)) { logger(DEBUG, _("can get value") . ($propertyObject["access"] & 4), __FILE__ . ":" . __LINE__); - $response .= '     friendlyName . "/" . $propertyObject["name"] . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "','" . $device->friendlyName . "','" . $propertyObject["name"] . "')\">
" . EOL; + $response .= '     friendlyName . "/" . $propertyObject["name"] . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "','" . $device->friendlyName . "','" . $propertyObject["name"] . "')\">"; } - }else - { - $response .= EOLH; } + $response .= EOLH; }else { logger(ERROR, _("Property do not exist"), __FILE__ . ":" . __LINE__); @@ -167,25 +160,7 @@ function webBrowse($socket, $argList, $page="/browse") $response .= insertJavascript(); foreach($device->properties as $key => $value) { - $response .= $key . "     "; - if (array_key_exists("access", $value)) - { - if(($value["access"] & 2)) - { - logger(DEBUG, _("Write Access OK ") . ($value["access"] & 2), __FILE__ . ":" . __LINE__); - $response .= displayChoice($device, $key); - } - if(($value["access"] & 4)) - { - logger(DEBUG, _("can get value") . ($value["access"] & 4), __FILE__ . ":" . __LINE__); - $response .= '     friendlyName . "/" . $key . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "', '" . $device->friendlyName . "', '" . $key . "')\">"; - } - } - $response .= "
\n" . $tab . "[
\n"; - //echo memory_get_usage(); - //$response = value($key, $value, $response); - iterateProperty($device, $key, $value, $response, $tab); - $response .= $tab . "]
\n"; + $response = displayProperty($device, $key, $value, $response); } /*foreach($device->properties as $key => $value) { @@ -228,6 +203,30 @@ function webBrowse($socket, $argList, $page="/browse") htmlSend($socket, $response); } +function displayProperty($device, $key, $value, $response) +{ + $response .= $key . "     "; + if (array_key_exists("access", $value)) + { + if(($value["access"] & 2)) + { + logger(DEBUG, _("Write Access OK ") . ($value["access"] & 2), __FILE__ . ":" . __LINE__); + $response .= displayChoice($device, $key); + } + if(($value["access"] & 4)) + { + logger(DEBUG, _("can get value") . ($value["access"] & 4), __FILE__ . ":" . __LINE__); + $response .= '     friendlyName . "/" . $key . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "', '" . $device->friendlyName . "', '" . $key . "')\">"; + } + } + $response .= "
\n" . $tab . "[
\n"; + //echo memory_get_usage(); + //$response = value($key, $value, $response); + iterateProperty($device, $key, $value, $response, $tab); + $response .= $tab . "]
\n"; + return $response; +} + function iterateProperty($device, $property, $value, &$response, $tab="") { $tab .= "        "; @@ -367,5 +366,21 @@ function webNotify($argList) $response = _("notify command have been set"); $monitored[] = new watch($argList["topic"], $argList["fn"], $argList["property"], $argList["condition"], $argList["value"]); } + return $response; } +/* +function webDisplayByType($argList) +{ + global $indexTypes, $config; + require_once $config . "porpertiesbytype.php"; + require_once "webserver/javascript.php"; + + $response .= insertJavascript(); + foreach($indexTypes[$argList["type"]] as $device) + { + foreach($) + displayProperty($device, $device->, $value, $response) + } +} +*/ ?> diff --git a/webserver/webserver.php b/webserver/webserver.php index 2b1a131..e52c7d2 100644 --- a/webserver/webserver.php +++ b/webserver/webserver.php @@ -28,7 +28,6 @@ function htmlSend($socket, $text, $meta="") "Connection: close" . EOLR . "Content-Type: text/html; charset=UTF-8" . EOLR . EOLR; $response = $httpHeader . '' . EOL . '' . EOL . '' . EOL . $meta . EOL . '' . EOL . 'Moha' . EOL . '' . EOL . $text . ""; - stream_socket_sendto($socket, $response); } @@ -62,11 +61,13 @@ function askWebServer($read) $argList["cmd"] = trim($tmp); }else { - $argList[trim(strchr($tmp, "=", true))] = trim(substr(strchr($tmp, "="), 1)); + $key = trim(strchr($tmp, "=", true)); + $argList[$key] = trim(substr(strchr($tmp, "="), 1)); + if (count($argTmp) == 1) $argList["cmd"] = $key; } } - logger(DEBUG, print_r($argList, true), __FILE__ . ":" . __LINE__); + logger(DEBUG, print_r($argList, true), __FILE__ . ":" . __LINE__); if(array_key_exists("cmd", $argList)) { $command = strtolower($argList["cmd"]); @@ -74,11 +75,11 @@ function askWebServer($read) switch($command) { case "dashboard": - webDashboard($spawn, $argList["dashboard"], $argList["page"]); + webDashboard($spawn, $argList["dashboard"]); break; case "browse": logger(DEBUG, _("Browsing"), __FILE__ . ":" . __LINE__); - webBrowse($spawn, $argList, $argList["page"]); + webBrowse($spawn, $argList); //return true; break; case "get": @@ -99,6 +100,9 @@ function askWebServer($read) htmlSend($spawn, webNotify($argList)); logger(DEBUG, print_r($monitored, true), __FILE__ . ":" . __LINE__); break; + case "type": + logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__); + htmlSend($spawn, webDisplayByType($argList)); default: if (is_numeric(array_key_first($argList))) {