1
0

debugging api and add notify command

This commit is contained in:
daniel Tartavel 2022-02-02 21:18:44 +01:00
parent 85db4e3ff4
commit 18d5b74a96
9 changed files with 100 additions and 43 deletions

View File

@ -23,7 +23,6 @@ class device
{ {
public $method; //0 = auto or 1 = manual public $method; //0 = auto or 1 = manual
public $topic; public $topic;
public $device;
public $ieeeAddress; public $ieeeAddress;
public $groupID; public $groupID;
public $friendlyName; public $friendlyName;
@ -78,37 +77,41 @@ class watch
public $topic; public $topic;
public $property; public $property;
public $PropertyValue; public $PropertyValue;
public $device;
public $function; public $function;
private $conditions = array( public $conditions = array(
"<", ">", "==", ">=", "<=" "<", ">", "==", ">=", "<="
); );
public $acceptedValues = array(
"ON", "OFF"
);
public $condition; public $condition;
public function __construct($topic, $fn, $property, $condition, $value) public function __construct($topic, $fn, $property, $condition, $value)
{ {
logger(DEBUG, _("New Notify object"), __FILE__ . ":" . __LINE__); global $conditions, $acceptedValues, $indexDevices;
if (($this->device = getDevice($topic, $fn)) === false) logger(DEBUG, _("New Notification"), __FILE__ . ":" . __LINE__);
if (($device = getDevice($topic, $fn)) !== false)
{ {
$this->topic = $topic; $this->topic = $topic;
if (array_key_exist($property, $this->device)) if (property_exists($device, $property))
{ {
$this->property = $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__ ); logger(ERROR, _("Value is not numeric"), __FILE__ . ":" . __LINE__ );
return false; return false;
} }
if (array_search($condition, $conditions)) if (array_search($condition, $this->conditions))
{ {
$this->function = '$value ' . $condition . " " . $value; $this->condition = $condition;
logger(DEBUG, _("Test function is ") . $this->function); $this->function = '$value ' . $condition . " " . $value . ";";
logger(DEBUG, _("Test function is ") . $this->function, __FILE__ . ":" . __LINE__ );
}else }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; return false;
} }
$this->device[$ieeeAddress]->$property["functions"][] = array($this,"notifyCallback"); $indexDevices[$device->ieeeAddress]->$property["functions"][] = array($this,"notifyCallback");
}else }else
{ {
logger(ERROR, _("Property do not exists"), __FILE__ . ":" . __LINE__ ); logger(ERROR, _("Property do not exists"), __FILE__ . ":" . __LINE__ );
@ -119,6 +122,7 @@ class watch
logger(ERROR, _("Device do not exists"), __FILE__ . ":" . __LINE__ ); logger(ERROR, _("Device do not exists"), __FILE__ . ":" . __LINE__ );
return false; return false;
} }
logger(DEBUG, var_export($this, true), __FILE__ . ":" . __LINE__ );
return $this; return $this;
} }
@ -126,6 +130,7 @@ class watch
{ {
if (eval($this->function)) 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 ); $msg = sprintf(_("Device '%s' have property '%s' value %s %s %s"), $device->friendlyName, $property, $value, $this->condition, $this->PropertyValue );
notify($msg); notify($msg);
} }

View File

@ -55,7 +55,7 @@ function mkDevicesDB($topic, $json, $group=false)
} }
$dbInit += 1; $dbInit += 1;
fwrite($logFh, "################################START##################################################"); fwrite($logFh, "################################START##################################################");
fwrite($logFh, print_r($devices, true)); fwrite($logFh, var_export($devices, true));
fwrite($logFh, "################################END##################################################"); fwrite($logFh, "################################END##################################################");
logger(DEBUG, _("Devices DB made"), __FILE__ . ":" . __LINE__); logger(DEBUG, _("Devices DB made"), __FILE__ . ":" . __LINE__);
@ -122,6 +122,8 @@ function changeDevice($topic, $fn, &$device, $payloadArray)
//print_r($payloadArray); //print_r($payloadArray);
if (!empty($payloadArray)) if (!empty($payloadArray))
{ {
//echo "==================== New ChangeDevice =====================" .EOL;
iterateDevice($topic, $fn, $device, $device, $payloadArray); iterateDevice($topic, $fn, $device, $device, $payloadArray);
}else }else
{ {
@ -134,8 +136,8 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
{ {
global $changed, $mohaDB, $testMode; global $changed, $mohaDB, $testMode;
$deviceType = (gettype($device) == "object"); // = true if object $deviceType = (gettype($device) == "object"); // = true if object
//echo "==================== New iterate =====================" .EOL;
//echo "devicetype = "; var_dump($deviceType); echo EOL; //echo "devicetype = "; var_dump($deviceType); echo EOL;
//print_r($payloadArray);
//echo "device =>";print_r($device);echo EOL; //echo "device =>";print_r($device);echo EOL;
//echo "PropertyTree ==============> " . $propertyTree . EOL; //echo "PropertyTree ==============> " . $propertyTree . EOL;
foreach($payloadArray as $key => $value) 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 "value =>"; print_r($value); echo EOL;
//echo "type : " . gettype($value) .EOL; //echo "type : " . gettype($value) .EOL;
$valueType = gettype($value); $valueType = gettype($value);
if ( $valueType == "object") if ($valueType == "object")
{ {
$propertyTree .= $key . "/"; $propertyTree .= $key . "/";
//echo "PropertyTree " . $propertyTree . EOL;
//echo " is Object" . EOL; //echo " is Object" . EOL;
if ($deviceType === true ) if ($deviceType === true )
{ {
//echo "deviceType = true" . EOL;
if (!property_exists($device, $key)) if (!property_exists($device, $key))
{ {
//echo "Property do not exists" . EOL;
$device->{$key} = new stdClass; $device->{$key} = new stdClass;
} }
//echo "iterating" . EOL; //echo "iterating" . EOL;
//echo "===============>";
iterateDevice($topic, $fn, $parentDevice, $device->$key, $value, $propertyTree); iterateDevice($topic, $fn, $parentDevice, $device->$key, $value, $propertyTree);
}else }else
{ {
@ -183,6 +190,7 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
}else }else
{ {
//var_dump($device);echo EOL;
//if (empty($device->$key) or $value != null) //if (empty($device->$key) or $value != null)
//{ //{
if (property_exists($device, $key)) if (property_exists($device, $key))

View File

@ -36,7 +36,7 @@ function checkEvents()
if ($exception === false) if ($exception === false)
{ {
logger(DEBUG, sprintf(_("sending command set %s => %s for %s"), $event->param ,bool2string($event->value), $event->device->friendlyName), __FILE__ . ":" . __LINE__); 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) if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false)
{ {
logger(ERROR, _("Error in adding interval to event recurrence. event: ") . $key, __FILE__ . ":" . __LINE__); logger(ERROR, _("Error in adding interval to event recurrence. event: ") . $key, __FILE__ . ":" . __LINE__);
@ -194,7 +194,7 @@ function deleteEvent($eventKey)
}else }else
{ {
//try to delete an flase event //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__);
} }
} }

View File

@ -9,10 +9,10 @@ class rdc_chambre_eclairage extends hook
public $luminance_min = 60; public $luminance_min = 60;
public $luminance_max = 3000; public $luminance_max = 3000;
protected $devicelist = array( protected $devicelist = array(
"RDC_CHAMBRE_AMBIANCE" => array("state", false), // "ON"/"OFF" RDC_CHAMBRE_AMBIANCE => array("state", false), // "ON"/"OFF"
"RDC_CHAMBRE_ECLAIRAGE" => array("state_l1", false), // "ON"/"OFF" RDC_CHAMBRE_ECLAIRAGE => array("state_l1", false), // "ON"/"OFF"
"RDC_CHAMBRE_MVMT" => array("occupancy", false), RDC_CHAMBRE_MVMT => array("occupancy", false),
"RDC_CHAMBRE_ARMOIRE_GAUCHE" => array("contact", false) RDC_CHAMBRE_ARMOIRE_GAUCHE => array("contact", false)
); );
// callback fonction. Is called with these 3 parameters // callback fonction. Is called with these 3 parameters
@ -26,7 +26,7 @@ class rdc_chambre_eclairage extends hook
if ($property == "occupancy" and $value == "ON") if ($property == "occupancy" and $value == "ON")
{ {
$this->send(RDC_CHAMBRE_AMBIANCE, "ON", "OFF", AUTO); $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); $this->send(RDC_CHAMBRE_ECLAIRAGE, "ON", "OFF", AUTO);
} }

View File

@ -29,18 +29,27 @@ $dbInit = 0; // flag to indicate that devices db is initializedl
$connected = false; // connected to MQTT server $connected = false; // connected to MQTT server
$included = false; // flag indicate scripts are loaded $included = false; // flag indicate scripts are loaded
$nSubscribed = 0; // Number of topics subsribed $nSubscribed = 0; // Number of topics subsribed
$logFile = "/var/log/moha.log"; // Path of log file
$logFh = null; // filehandle of log file $logFh = null; // filehandle of log file
$curlErr = 0; // Number of errors returned by curl $curlErr = 0; // Number of errors returned by curl
$configDir = "./config"; // default config dir (production value is /etc/moha/) $configDir = "./config"; // default config dir (production value is /etc/moha/)
$hooksInitialized = 0; // are all hooks initialized ? false/true $hooksInitialized = 0; // are all hooks initialized ? false/true
$flagHooks = false; $flagHooks = false;
$mqttServerIp = "127.0.0.1"; // IP address of mqttserver in production mode
if ($testMode) if ($testMode)
{ {
$mqttServerIp = "192.168.1.253"; // IP address of mqttserver in test mode $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); if (!init()) exit(1);
// gettext // gettext
@ -79,7 +88,11 @@ function logger($level, $log, $pos = false, $notif = true)
{ {
logger(INFO, _("Notification not sent"), __FILE__ . ":" . __LINE__, false); logger(INFO, _("Notification not sent"), __FILE__ . ":" . __LINE__, false);
} }
}else
{
logger(INFO, _("Notification not sent because of testMode"), __FILE__ . ":" . __LINE__, false);
} }
} }
function init() function init()
@ -117,12 +130,12 @@ function listHooks($dir, &$hookList)
function endMoha() function endMoha()
{ {
global $testMode, $devices, $topics, $nSubscribed ,$client, $logFh, $connected; global $testMode, $devices, $topics, $nSubscribed ,$client, $logFh, $connected, $dataPath;
$x = 0; $x = 0;
logger(WARNING, _("moha is ending"), __FILE__ . ":" . __LINE__); logger(WARNING, _("moha is ending"), __FILE__ . ":" . __LINE__);
storeDB($devices, "/usr/share/moha/moha.db"); storeDB($devices, $dataPath . "moha.db");
if($testMode) file_put_contents("moha.devices", print_r($devices, true)); if($testMode) file_put_contents($dataPath . "moha.devices", var_export($devices, true));
if ($connected) if ($connected)
{ {
$mid = $client->unsubscribe("#"); $mid = $client->unsubscribe("#");
@ -152,7 +165,7 @@ require "class/main.php";
require "class/db.php"; require "class/db.php";
require "class/hook_class.php"; require "class/hook_class.php";
require "utils.php"; require "utils.php";
require $configDir . "/properties2log.php"; require "config/properties2log.php";
require "mqtt_functions.php"; require "mqtt_functions.php";
require "events.php"; require "events.php";
require "db_functions.php"; require "db_functions.php";
@ -183,6 +196,11 @@ if (is_readable($configDir . "/" . "devices_constants.php"))
include $configDir . "/" . "devices_constants.php"; include $configDir . "/" . "devices_constants.php";
//echo "hooklist"; print_r($hooksList); echo EOL; //echo "hooklist"; print_r($hooksList); echo EOL;
logger(INFO, sprintf(_("%s/devices_constants.define found, so it has been included :-)"), $configDir), __FILE__ . ":" . __LINE__, false); 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 }else
{ {
logger(WARNING, sprintf(_("%s/devices_constants.define not found, so not included :-)"), $configDir), __FILE__ . ":" . __LINE__, false); 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); logger(INFO, _("Including ") . $hook, __FILE__ . ":" . __LINE__, false);
include $hook; 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; $included = true;
} }

View File

@ -6,7 +6,7 @@ function messageReceived($message)
global $topics, $logFh, $devices, $included; global $topics, $logFh, $devices, $included;
$topic = explode ("/", $message->topic); $topic = explode ("/", $message->topic);
$callback = $topics[$topic[0]]->callback; $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); $callback($topic, $message);
} }

View File

@ -13,7 +13,7 @@ $callback = function($topic, $message)
case "devices": case "devices":
logger(DEBUG,_("Inserting zigbee devices in DB"), __FILE__ . ":" . __LINE__); logger(DEBUG,_("Inserting zigbee devices in DB"), __FILE__ . ":" . __LINE__);
$topics[$topic[0]]->devices = json_decode($message->payload); $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); mkDevicesDB($topic[0], $topics[$topic[0]]->devices);
break; break;
case "groups": case "groups":

View File

@ -55,13 +55,15 @@ function getDevice($topic, $fn)
$var = $var[$tmp]; $var = $var[$tmp];
}else }else
{ {
logger(ERROR, sprintf(_("level %s of %s do not exists in %s"), $tmp, $fn, $topic), __FILE__ . ":" . __LINE__ );
return false; return false;
} }
} }
return $var; logger(DEBUG, sprintf(_("returning a value")), __FILE__ . ":" . __LINE__ );
return $var["device"];
}else }else
{ {
logger(ERROR, sprintf(_("Topic %s do not exists"), $topic), __FILE__ . ":" . __LINE__ );
return false; return false;
} }
} }

View File

@ -34,28 +34,46 @@ function askWebServer($read)
$argTmp = explode("&", $input[0]); $argTmp = explode("&", $input[0]);
foreach($argTmp as $tmp) foreach($argTmp as $tmp)
{ {
logger(DEBUG, $tmp, __FILE__ . ":" . __LINE__); //logger(DEBUG, $tmp, __FILE__ . ":" . __LINE__);
$array = explode("=", $tmp); $argList[strchr($tmp, "=", true)] = substr(strchr($tmp, "="), 1);
//logger(DEBUG, $argList[0] . " ==========> " . $argList[1]);
//print_r($array); //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)) if(array_key_exists("cmd", $argList))
{ {
$command = strtolower($argList["cmd"]); $command = strtolower($argList["cmd"]);
logger(DEBUG, _("command is ") . $command);
switch($command) switch($command)
{ {
case "get": case "get":
logger(DEBUG, _("GET reached"), __FILE__ . ":" . __LINE__); 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 = "<html><header><body>get passed</body></header><html>"; $response = "<html><header></header><body>GET: " . _("no parameters passed, need topic, fn and property") . "</body></html>";
}else
{
$device = getDevice($argList["topic"], $argList["fn"]);
$property = $argList["property"];
$response = "<html><header></header><body>GET: " . bool2string($device->$property["value"]) . "</body></html>";
} }
fwrite($spawn, $response); fwrite($spawn, $response);
break; break;
case "set": case "set":
logger(DEBUG, _("SET reached"), __FILE__ . ":" . __LINE__); logger(DEBUG, _("SET reached"), __FILE__ . ":" . __LINE__);
$response = "<html><header><body>set passed</body></header><html>"; 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 = "<html><header></header><body>SET: " . _("no parameters passed, need topic, fn, property and value") . "passed</body></html>";
fwrite($spawn, $response); fwrite($spawn, $response);
}else
{
$response = "<html><header></header><body>setting property " . $argList["property"] . " of " . $argList["fn"] . " to value: " . $argList["value"] . "</body></html>";
fwrite($spawn, $response);
$payload = array($argList["property"] => $argList["value"]);
publish(Z2M . "/" . $argList["fn"], $payload);
}
break; break;
case "dump": case "dump":
case "print": case "print":
@ -86,7 +104,7 @@ function askWebServer($read)
$error = error_get_last(); $error = error_get_last();
if($error !== null) if($error !== null)
{ {
$response = "<html><header><body>" . $error["message"] . " file: " . $error["file"] . " line: " . $error["line"] . "</body></header><html>"; $response = "<html><header></header><body>" . $error["message"] . " file: " . $error["file"] . " line: " . $error["line"] . "</body></html>";
} }
if ($command === "print") if ($command === "print")
{ {
@ -99,17 +117,23 @@ function askWebServer($read)
fwrite($spawn, $response); fwrite($spawn, $response);
break; break;
case "notify": 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)) 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 = "<html><header></header><body>" . _("Error: With 'notify' command, you need 4 parameters: topic, fn, property, condition, value") . "</body></html>";
fwrite($spawn, $response);
}else }else
{ {
$response = "<html><header></header><body>" . _("notify command have been set") . "</body></html>";
$monitored[] = new watch($argList["topic"], $argList["fn"], $argList["property"], $argList["condition"], $argList["value"]); $monitored[] = new watch($argList["topic"], $argList["fn"], $argList["property"], $argList["condition"], $argList["value"]);
fwrite($spawn, $response);
} }
logger(DEBUG, print_r($monitored, true));
break; break;
default: default:
logger(DEBUG, _("unknown command"), __FILE__ . ":" . __LINE__); logger(DEBUG, _("unknown command"), __FILE__ . ":" . __LINE__);
fwrite($spawn, _("unknown command")); $response = "<html><header></header><body>" . _("unknown command") . "</body></html>";
fwrite($spawn, $response);
} }
} }
} }