diff --git a/class/main.php b/class/main.php index 6274980..9a9e4dc 100644 --- a/class/main.php +++ b/class/main.php @@ -23,7 +23,6 @@ class device { public $method; //0 = auto or 1 = manual public $topic; - public $device; public $ieeeAddress; public $groupID; public $friendlyName; @@ -78,37 +77,41 @@ class watch public $topic; public $property; public $PropertyValue; - public $device; public $function; - private $conditions = array( + public $conditions = array( "<", ">", "==", ">=", "<=" ); + public $acceptedValues = array( + "ON", "OFF" + ); public $condition; public function __construct($topic, $fn, $property, $condition, $value) { - logger(DEBUG, _("New Notify object"), __FILE__ . ":" . __LINE__); - if (($this->device = getDevice($topic, $fn)) === false) + global $conditions, $acceptedValues, $indexDevices; + logger(DEBUG, _("New Notification"), __FILE__ . ":" . __LINE__); + if (($device = getDevice($topic, $fn)) !== false) { $this->topic = $topic; - if (array_key_exist($property, $this->device)) + if (property_exists($device, $property)) { $this->property = $property; - if (!is_numeric($value)) + if (!is_numeric($value) and array_search($value, $this->acceptedValues) === false) { logger(ERROR, _("Value is not numeric"), __FILE__ . ":" . __LINE__ ); return false; } - if (array_search($condition, $conditions)) + if (array_search($condition, $this->conditions)) { - $this->function = '$value ' . $condition . " " . $value; - logger(DEBUG, _("Test function is ") . $this->function); + $this->condition = $condition; + $this->function = '$value ' . $condition . " " . $value . ";"; + logger(DEBUG, _("Test function is ") . $this->function, __FILE__ . ":" . __LINE__ ); }else { - logger(ERROR, _("Condition is not one of the permitted"), __FILE__ . ":" . __LINE__ ); + logger(ERROR, sprintf(_("Condition %s is not one of the permitted : "), $condition) . print_r($conditions, true), __FILE__ . ":" . __LINE__ ); return false; } - $this->device[$ieeeAddress]->$property["functions"][] = array($this,"notifyCallback"); + $indexDevices[$device->ieeeAddress]->$property["functions"][] = array($this,"notifyCallback"); }else { logger(ERROR, _("Property do not exists"), __FILE__ . ":" . __LINE__ ); @@ -119,6 +122,7 @@ class watch logger(ERROR, _("Device do not exists"), __FILE__ . ":" . __LINE__ ); return false; } + logger(DEBUG, var_export($this, true), __FILE__ . ":" . __LINE__ ); return $this; } @@ -126,6 +130,7 @@ class watch { if (eval($this->function)) { + logger(DEBUG, _("notifyCallback")); $msg = sprintf(_("Device '%s' have property '%s' value %s %s %s"), $device->friendlyName, $property, $value, $this->condition, $this->PropertyValue ); notify($msg); } diff --git a/db_functions.php b/db_functions.php index d24904c..6e4719a 100644 --- a/db_functions.php +++ b/db_functions.php @@ -55,7 +55,7 @@ function mkDevicesDB($topic, $json, $group=false) } $dbInit += 1; fwrite($logFh, "################################START##################################################"); - fwrite($logFh, print_r($devices, true)); + fwrite($logFh, var_export($devices, true)); fwrite($logFh, "################################END##################################################"); logger(DEBUG, _("Devices DB made"), __FILE__ . ":" . __LINE__); @@ -122,6 +122,8 @@ function changeDevice($topic, $fn, &$device, $payloadArray) //print_r($payloadArray); if (!empty($payloadArray)) { + //echo "==================== New ChangeDevice =====================" .EOL; + iterateDevice($topic, $fn, $device, $device, $payloadArray); }else { @@ -134,8 +136,8 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro { global $changed, $mohaDB, $testMode; $deviceType = (gettype($device) == "object"); // = true if object + //echo "==================== New iterate =====================" .EOL; //echo "devicetype = "; var_dump($deviceType); echo EOL; - //print_r($payloadArray); //echo "device =>";print_r($device);echo EOL; //echo "PropertyTree ==============> " . $propertyTree . EOL; foreach($payloadArray as $key => $value) @@ -145,17 +147,22 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro //echo "value =>"; print_r($value); echo EOL; //echo "type : " . gettype($value) .EOL; $valueType = gettype($value); - if ( $valueType == "object") + if ($valueType == "object") { $propertyTree .= $key . "/"; + //echo "PropertyTree " . $propertyTree . EOL; //echo " is Object" . EOL; if ($deviceType === true ) { + //echo "deviceType = true" . EOL; if (!property_exists($device, $key)) { + //echo "Property do not exists" . EOL; $device->{$key} = new stdClass; } //echo "iterating" . EOL; + //echo "===============>"; + iterateDevice($topic, $fn, $parentDevice, $device->$key, $value, $propertyTree); }else { @@ -183,6 +190,7 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro }else { + //var_dump($device);echo EOL; //if (empty($device->$key) or $value != null) //{ if (property_exists($device, $key)) diff --git a/events.php b/events.php index f087af9..f59be59 100644 --- a/events.php +++ b/events.php @@ -36,7 +36,7 @@ function checkEvents() if ($exception === false) { logger(DEBUG, sprintf(_("sending command set %s => %s for %s"), $event->param ,bool2string($event->value), $event->device->friendlyName), __FILE__ . ":" . __LINE__); - publish(mktopic($event->device), array($event->param => $event->value), "set", $key); + publish(mktopic($event->device), array($event->param => $event->value), "set"); if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false) { logger(ERROR, _("Error in adding interval to event recurrence. event: ") . $key, __FILE__ . ":" . __LINE__); @@ -194,7 +194,7 @@ function deleteEvent($eventKey) }else { //try to delete an flase event - logger(WARNING, _("Try to delete event with key = ") . var_dump($eventKey), __FILE__ . ":" . __LINE__); + logger(WARNING, _("Try to delete event with key = ") . var_export($eventKey, true), __FILE__ . ":" . __LINE__); } } diff --git a/hooks/scripts/rdc_chambre_eclairage.php b/hooks/scripts/rdc_chambre_eclairage.php index 4013579..09f93aa 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 => 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) ); // callback fonction. Is called with these 3 parameters @@ -26,7 +26,7 @@ class rdc_chambre_eclairage extends hook if ($property == "occupancy" and $value == "ON") { $this->send(RDC_CHAMBRE_AMBIANCE, "ON", "OFF", AUTO); - }elseif ($property == "contact" and $value = true and getValue(RDC_CHAMBRE_ECLAIRAGE) == "OFF") + }elseif ($property == "contact" and $value = true and getValue(RDC_CHAMBRE_ECLAIRAGE, "state_l1") == "OFF") { $this->send(RDC_CHAMBRE_ECLAIRAGE, "ON", "OFF", AUTO); } diff --git a/moha.php b/moha.php index 66d30cd..e3d35f4 100644 --- a/moha.php +++ b/moha.php @@ -29,18 +29,27 @@ $dbInit = 0; // flag to indicate that devices db is initializedl $connected = false; // connected to MQTT server $included = false; // flag indicate scripts are loaded $nSubscribed = 0; // Number of topics subsribed -$logFile = "/var/log/moha.log"; // Path of log file $logFh = null; // filehandle of log file $curlErr = 0; // Number of errors returned by curl $configDir = "./config"; // default config dir (production value is /etc/moha/) $hooksInitialized = 0; // are all hooks initialized ? false/true $flagHooks = false; -$mqttServerIp = "127.0.0.1"; // IP address of mqttserver in production mode if ($testMode) { $mqttServerIp = "192.168.1.253"; // IP address of mqttserver in test mode + $dataPath = "."; + $logFile = "./moha.log"; // Path of log file + $configDir = "./config"; // default config dir (production value is /etc/moha/) +}else +{ + $mqttServerIp = "127.0.0.1"; // IP address of mqttserver in production mode + $dataPath = "/usr/share/moha/"; + $logFile = "/var/log/moha.log"; // Path of log file + $configDir = "/etc/config"; // default config dir (production value is /etc/moha/) + } + if (!init()) exit(1); // gettext @@ -79,7 +88,11 @@ function logger($level, $log, $pos = false, $notif = true) { logger(INFO, _("Notification not sent"), __FILE__ . ":" . __LINE__, false); } + }else + { + logger(INFO, _("Notification not sent because of testMode"), __FILE__ . ":" . __LINE__, false); } + } function init() @@ -117,12 +130,12 @@ function listHooks($dir, &$hookList) function endMoha() { - global $testMode, $devices, $topics, $nSubscribed ,$client, $logFh, $connected; + global $testMode, $devices, $topics, $nSubscribed ,$client, $logFh, $connected, $dataPath; $x = 0; logger(WARNING, _("moha is ending"), __FILE__ . ":" . __LINE__); - storeDB($devices, "/usr/share/moha/moha.db"); - if($testMode) file_put_contents("moha.devices", print_r($devices, true)); + storeDB($devices, $dataPath . "moha.db"); + if($testMode) file_put_contents($dataPath . "moha.devices", var_export($devices, true)); if ($connected) { $mid = $client->unsubscribe("#"); @@ -152,7 +165,7 @@ require "class/main.php"; require "class/db.php"; require "class/hook_class.php"; require "utils.php"; -require $configDir . "/properties2log.php"; +require "config/properties2log.php"; require "mqtt_functions.php"; require "events.php"; require "db_functions.php"; @@ -183,6 +196,11 @@ 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); @@ -242,7 +260,7 @@ while (true) logger(INFO, _("Including ") . $hook, __FILE__ . ":" . __LINE__, false); include $hook; } - file_put_contents("/usr/share/moha/moha.devices", print_r($devices, true)); // debugging : save device list + file_put_contents($dataPath . "moha.devices", var_export($devices, true)); // debugging : save device list $included = true; } diff --git a/mqtt_functions.php b/mqtt_functions.php index bb5c3bf..4d2d8d0 100644 --- a/mqtt_functions.php +++ b/mqtt_functions.php @@ -6,7 +6,7 @@ function messageReceived($message) global $topics, $logFh, $devices, $included; $topic = explode ("/", $message->topic); $callback = $topics[$topic[0]]->callback; - logger(DEBUG, "topic => " . print_r($topic, true), __FILE__ . ":" . __LINE__); + //logger(DEBUG, "topic => " . var_export($topic, true), __FILE__ . ":" . __LINE__); $callback($topic, $message); } diff --git a/topics_callbacks/zigbee2mqtt.php b/topics_callbacks/zigbee2mqtt.php index efdf635..c707ea9 100644 --- a/topics_callbacks/zigbee2mqtt.php +++ b/topics_callbacks/zigbee2mqtt.php @@ -13,7 +13,7 @@ $callback = function($topic, $message) case "devices": logger(DEBUG,_("Inserting zigbee devices in DB"), __FILE__ . ":" . __LINE__); $topics[$topic[0]]->devices = json_decode($message->payload); - fwrite($logFh, print_r($topics[$topic[0]]->devices, true)); + fwrite($logFh, var_export($topics[$topic[0]]->devices, true)); mkDevicesDB($topic[0], $topics[$topic[0]]->devices); break; case "groups": diff --git a/utils.php b/utils.php index febe2a5..00e8d55 100644 --- a/utils.php +++ b/utils.php @@ -55,13 +55,15 @@ function getDevice($topic, $fn) $var = $var[$tmp]; }else { + logger(ERROR, sprintf(_("level %s of %s do not exists in %s"), $tmp, $fn, $topic), __FILE__ . ":" . __LINE__ ); return false; } - } - return $var; + logger(DEBUG, sprintf(_("returning a value")), __FILE__ . ":" . __LINE__ ); + return $var["device"]; }else { + logger(ERROR, sprintf(_("Topic %s do not exists"), $topic), __FILE__ . ":" . __LINE__ ); return false; } } diff --git a/webserver.php b/webserver.php index 4a3102d..30a874c 100644 --- a/webserver.php +++ b/webserver.php @@ -34,28 +34,46 @@ function askWebServer($read) $argTmp = explode("&", $input[0]); foreach($argTmp as $tmp) { - logger(DEBUG, $tmp, __FILE__ . ":" . __LINE__); - $array = explode("=", $tmp); + //logger(DEBUG, $tmp, __FILE__ . ":" . __LINE__); + $argList[strchr($tmp, "=", true)] = substr(strchr($tmp, "="), 1); + //logger(DEBUG, $argList[0] . " ==========> " . $argList[1]); //print_r($array); - if (isset($array[1])) $argList[$array[0]] = $array[1]; + //if (isset($array[1])) $argList[$array[0]] = $array[1]; } + logger(DEBUG, print_r($argList, true)); if(array_key_exists("cmd", $argList)) { $command = strtolower($argList["cmd"]); + logger(DEBUG, _("command is ") . $command); switch($command) { case "get": logger(DEBUG, _("GET reached"), __FILE__ . ":" . __LINE__); - if(empty($argList["device"])) + if(!array_key_exists("topic", $argList) or !array_key_exists("fn", $argList) or !array_key_exists("property", $argList)) { - $response = "
get passed
"; + $response = "
GET: " . _("no parameters passed, need topic, fn and property") . ""; + }else + { + $device = getDevice($argList["topic"], $argList["fn"]); + $property = $argList["property"]; + $response = "
GET: " . bool2string($device->$property["value"]) . ""; } fwrite($spawn, $response); break; case "set": logger(DEBUG, _("SET reached"), __FILE__ . ":" . __LINE__); - $response = "
set passed
"; - fwrite($spawn, $response); + if(!array_key_exists("topic", $argList) or !array_key_exists("fn", $argList) or !array_key_exists("property", $argList) or !array_key_exists("value", $argList)) + { + $response = "
SET: " . _("no parameters passed, need topic, fn, property and value") . "passed"; + fwrite($spawn, $response); + }else + { + $response = "
setting property " . $argList["property"] . " of " . $argList["fn"] . " to value: " . $argList["value"] . ""; + fwrite($spawn, $response); + $payload = array($argList["property"] => $argList["value"]); + publish(Z2M . "/" . $argList["fn"], $payload); + } + break; case "dump": case "print": @@ -86,7 +104,7 @@ function askWebServer($read) $error = error_get_last(); if($error !== null) { - $response = "
" . $error["message"] . " file: " . $error["file"] . " line: " . $error["line"] . "
"; + $response = "
" . $error["message"] . " file: " . $error["file"] . " line: " . $error["line"] . ""; } if ($command === "print") { @@ -99,17 +117,23 @@ function askWebServer($read) fwrite($spawn, $response); break; case "notify": + logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__); if (!array_key_exists("topic", $argList) or !array_key_exists("fn", $argList) or !array_key_exists("property", $argList) or !array_key_exists("condition", $argList) or !array_key_exists("value", $argList)) { - fwrite($spawn, _("Error: With 'notify' command, you need 4 parameters: topic, fn, property, condition, value")); + $response = "
" . _("Error: With 'notify' command, you need 4 parameters: topic, fn, property, condition, value") . ""; + fwrite($spawn, $response); }else { + $response = "
" . _("notify command have been set") . ""; $monitored[] = new watch($argList["topic"], $argList["fn"], $argList["property"], $argList["condition"], $argList["value"]); + fwrite($spawn, $response); } + logger(DEBUG, print_r($monitored, true)); break; default: logger(DEBUG, _("unknown command"), __FILE__ . ":" . __LINE__); - fwrite($spawn, _("unknown command")); + $response = "
" . _("unknown command") . ""; + fwrite($spawn, $response); } } }