1
0

debugging hooks\ncleaning code

This commit is contained in:
daniel Tartavel 2022-01-17 21:01:11 +01:00
parent 4fb5504cdc
commit 11a2253804
7 changed files with 61 additions and 91 deletions

View File

@ -42,7 +42,7 @@ class hook
logger(DEBUG, _("Callback already installed"));
}
}
echo "result => "; var_dump($result);
//echo "result => "; var_dump($result);
if ($result === true)
{
$this->initialized = true;

View File

@ -80,7 +80,6 @@ function addDevice(& $device, $fn, $jsonDevice )
//indexing device
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
//print_r($device);
}
function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys)
@ -96,7 +95,6 @@ function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys)
if ( isset($value->property))
{
$string = $value->property;
//echo "property ===> " . $value->property . EOL;
$device->{$string}["value"] = null;
$device->$string["functions"] = array();
}
@ -134,7 +132,7 @@ function changeDevice($topic, $fn, &$device, $payloadArray)
function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $propertyTree="")
{
global $changed, $mohaDB;
global $changed, $mohaDB, $testMode;
$deviceType = (gettype($device) == "object"); // = true if object
//echo "devicetype = "; var_dump($deviceType); echo EOL;
//print_r($payloadArray);
@ -185,8 +183,6 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
}else
{
//echo "db_functions".EOL;
//($device);
if (empty($device->$key) or $value != null)
{
if (property_exists($device, $key))
@ -209,25 +205,19 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
$changed[$fn]["key"] = $key;
$changed[$fn]["value"] = $value;
logger(INFO, sprintf(_("Device %s property %s, value changed to %s"), $fn, $propertyTree . $key, $value));
//print_r($device);
//$mohaDB->logProperty($parentDevice, $propertyTree . $key, $value, $oldValue); TODO re-activate
//echo "oldvalue => " . print_r($oldValue, true) . EOL;
/*if (empty($oldValue))
if ($testMode === false)
{
echo "Initializing " . $key;
$mohaDB->logProperty($parentDevice, $propertyTree . $key, $value, $oldValue);
}else
{
echo "changed " . $key . " value " . $oldValue;;
logger(INFO, _("Test mode on: not storing in DB "));
}
echo " to " . $value . EOL;*/
}
if (!empty($device->$key["functions"]))
{
logger(DEBUG,_("executing notifications functions"));
foreach($device->$key["functions"] as $function)
{
//print_r($function);
$function($device, $key, $value);
}
}

View File

@ -2,8 +2,8 @@
class rdc_salon_eclairage extends hook
{
public $hookName = "rdc_salon_eclairage";
// list of devices we are listening to
// list of devices we are listening to
protected $devicelist = array(
RDC_SALON_MVMT => array("occupancy", false),
RDC_SALON_MVMT2 => array("occupancy", false),
@ -21,9 +21,11 @@ class rdc_salon_eclairage extends hook
public function callBack(&$device, $param, $value)
{
global $devices, $indexDevices;
logger(INFO, _("hook : rdc_salon_eclairage"));
switch($param)
{
case "occupancy":
//print_r($indexDevices[RDC_SALON_LUMINOSITE]);
if ($value == 1 and $indexDevices[RDC_SALON_LUMINOSITE]->illuminance_lux["value"] <= $this->luminance_min)
{
$this->send("ON");
@ -36,7 +38,10 @@ class rdc_salon_eclairage extends hook
}
break;
case "illuminance_lux":
if ($value >= $this->luminance_max) $this->send("OFF");
if ($value >= $this->luminance_max)
{
$this->send("OFF");
}
break;
}
logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $param, $value));

View File

@ -1,5 +1,7 @@
<?php
$title = "moha";
$testMode = false;
cli_set_process_title($title);
file_put_contents("/proc/".getmypid()."/comm",$title);
@ -10,7 +12,14 @@ define("ON", 1);
define("OFF", 0);
define("AUTO", 0);
define("MANUAL", 1);
// log levels
define( "DEBUG", 16); // => 16
define( "INFO", 1); // => 1
define( "NOTICE", 2); // => 2
define( "WARNING", 4); // => 4
define( "ERROR", 8); // => 8
define( "ALERT", 32);
define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT);
declare(ticks = 1);
@ -18,6 +27,8 @@ $listProperties = array("powerSource" => "batterie");
$listPropertiesKeys = array("property");
//global variables
$logLevel = INFO | NOTICE | WARNING | ERROR | ALERT; //ALL;
$notificationLevel = WARNING | ERROR;
$topics = array(); // list of topics
$mids = array(); // list of message IDs
$devices = array(); // array of device objetcs
@ -36,9 +47,8 @@ $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
require $configDir . "/properties2log.php";
logger(DEBUG, _("lauching init function"), false);
if (!init()) exit(1);
// gettext
@ -77,29 +87,21 @@ function logger($level, $log, $notif = true)
}
}
logger(DEBUG, _('assigning variable : $client'), false);
$client = new Mosquitto\Client();
// log levels
//define( "DEBUG", 16); // => 16
define( "INFO", $client::LOG_INFO); // => 1
define( "NOTICE", $client::LOG_NOTICE); // => 2
define( "WARNING", $client::LOG_WARNING); // => 4
define( "ERROR", $client::LOG_ERR); // => 8
define( "ALERT", 32);
define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT);
$logLevel = ALL;
$notificationLevel = WARNING | ERROR;
logger(DEBUG, _("requiring php modules"), false);
require "class/main.php";
require "class/db.php";
require "class/hook_class.php";
require "utils.php";
require $configDir . "/properties2log.php";
require "mqtt_functions.php";
require "events.php";
require "db_functions.php";
logger(DEBUG, _('assigning variable $client to mosquitto class "client"'), false);
$client = new Mosquitto\Client();
logger(DEBUG, _("Loading stored devices datas"));
loadDB($devices, "moha.db");
@ -130,8 +132,8 @@ if (is_readable($configDir . "/" . "devices_constants.php"))
// making the list of hooks to include
listHooks("./hooks", $hooksList);
logger(DEBUG, _("Program start"), false);
// Program start
logger(DEBUG, _("Program start"), false);
$client = new Mosquitto\Client();
// defining callback functions
@ -143,51 +145,47 @@ $client->onMessage('messageReceived');
$client->onLog('logger');
$client->onPublish('publishResponse');
// connectong to mqtt server
$client->connect("192.168.1.253", 1883, 5);
/*while(!$connected)
{
sleep (1);
}*/
logger(DEBUG, _("Subsribing to bridge"), false);
logger(INFO, _("Subscribing to bridge"), false);
foreach($topics as $name => $topic)
{
//echo $name;
$topic->mid = $client->subscribe($name . "/#", 2);
$mids[$topic->mid] = $name;
$topic->status = false;
}
logger(DEBUG, _("Starting loop"), false);
// starting main loop
logger(INFO, _("Starting loop"), false);
$oneshot = false;
while (true)
{
$client->loop();
if (! $included)
$client->loop(); // mqtt server loop()
if (! $included) // hooks not already included
{
logger(DEBUG, _("Making hooks list"), false);
getDevicesValues();
if (!empty($hooksList))
getDevicesValues(); // TODO get the values of devices
if (!empty($hooksList)) // some hooks to include if hooklist is not empty
{
foreach ($hooksList as $hook)
foreach ($hooksList as $hook) // loop to include hooks in hookslist
{
logger(INFO, _("Including ") . $hook, false);
include $hook;
}
file_put_contents("moha.devices", print_r($devices, true));
file_put_contents("moha.devices", print_r($devices, true)); // debugging : save device list
$included = true;
}
}elseif($included)
}else
{
if ($oneshot === false) // execute once initialization finished
if ($oneshot === false) // execute once initialization finished :WARNING hooks can to be not initialized
{
logger(DEBUG, _("Oneshot part of loop"), false);
$oneshot = true;
}
checkEvents();
if($hooksInitialized == 0)
if($hooksInitialized == 0) // all hooks are not initialized
{
$i = 1;
foreach($hooks as $hookName => $hook)
@ -196,10 +194,6 @@ while (true)
{
logger(WARNING, _("Hook not completely initialized :") . $hookName);
$i &= $hook->installHooks();
echo "hook->initialized";var_dump($hook->initialized);echo EOL;
// (int)$hook->initialized;
var_dump($hook);
echo "i =======> " . $i;var_dump($i); echo EOL;
}
}
$hooksInitialized = $i;
@ -227,21 +221,15 @@ function init()
function listHooks($dir, &$hookList)
{
$files = scandir($dir);
//print_r($files);
foreach ($files as $file)
{
//echo "=====> $file" . EOL;
if ($file != "." and $file != "..")
{
//echo "not . or .." . EOL;echo strpos($file, ".php", -4) . EOL;
if (is_dir($dir . "/" . $file))
{
//echo "directory : " . $dir . '/' . $file . EOL;
listHooks($dir . '/' . $file, $hookList);
}elseif (strpos($file, ".php", -4) !== false)
{
//echo "file : " . $dir . "/" . $file . EOL;
if (substr($file, -4) == ".php")
{
$hookList[] = $dir . "/" . $file;
@ -249,7 +237,6 @@ function listHooks($dir, &$hookList)
}
}
}
//print_r($hookList);
}
function endMoha()
@ -262,12 +249,10 @@ function endMoha()
{
$mid = $client->unsubscribe("#");
$client->disconnect();
//echo $nSubscribed;0x00124b0022ebac5c
while ($connected)
{
if ( $x++ <= 60)
{
fclose($logFh);
exit (1);
}
@ -275,7 +260,6 @@ function endMoha()
}
}
fclose($logFh);
exit(0);
}

View File

@ -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));
logger(DEBUG, "topic => " . print_r($topic, true));
$callback($topic, $message);
}
@ -15,10 +15,11 @@ function messageReceived($message)
function publish($topic, $payload, $commande="set", $eventKey)
{
global $client, $mids, $logFh;
//print_r($payload);
global $client, $mids, $logFh, $testMode;
$string = $topic . "/" . $commande;
//$mid = $client->publish($string, json_encode($payload) , 2); //TODO activer
if ($testMode === false)
{
$mid = $client->publish($string, json_encode($payload) , 2);
if (isset($mids[$mid]))
{
//echo "unsetting mids" .EOL;
@ -28,8 +29,11 @@ function publish($topic, $payload, $commande="set", $eventKey)
//echo "setting mids" .EOL;
$mids[$mid] = true;
}
//echo $string . " =>>>>>> " . json_encode($payload) . EOL;
logger(LOG_INFO, $logFh, "Publishing " . $string . " with payload => " . json_encode($payload));
}else
{
logger(INFO, _("Test mode on: no publishing "));
}
}
function connectResponse($r, $message)

View File

@ -1,20 +1,15 @@
<?php
//TODO to test and debug
$topics["linky2mqtt"] = new topic;
/*$devices["linky2mqtt"]["linky"]["device"] = new device;
$device = &$devices["linky2mqtt"]["linky"]["device"];
*/
$topics["linky2mqtt"]->callback = function($topic, $message)
{
global $topics, $logFh, $devices, $included;
$topicName = $topic[0];
$friendlyName = $topic[1]; // get friendlyName
logger(INFO, sprintf(_("Icoming notification of device %s"), $topic[0], $topic[1]));
logger(INFO, sprintf(_("Incoming notification of device %s"), $topic[0], $topic[1]));
$device = & $devices[$topic[0]];
$payloadArray = json_decode($message->payload);
//print_r($payloadArray);
//print_r($device) ;
if (!isset($device[$fn])) //must not exists, but ...
{
logger(LOG_WARNING, $logFh, "init of " . $fn .EOL);
@ -22,11 +17,9 @@ $topics["linky2mqtt"]->callback = function($topic, $message)
$device[$fn]["device"] = new device;
$device[$fn]["device"]->type = "mesure";
$device[$fn]["device"]->ieeeAddress = $payloadArray["ADSC"];
//addDevice($device[$fn], $fn, );
$indexDevices[$device[$fn]["device"]->ieeeAddress] = & $device[$fn]["device"];
}
$device = & $device[$fn];
//print_r($device);
changeDevice($topicName, $friendlyName, $device["device"], $payloadArray);
print_r($device["device"]);
}

View File

@ -1,17 +1,13 @@
<?php
$topics["pws2mqtt"] = new topic;
/*$devices["pws2mqtt"]["WH2650A"]["device"] = new device;
$device = &$devices["pws2mqtt"]["WH2650A"]["device"];*/
$topics["pws2mqtt"]->callback = function($topic, $message)
{
global $topics, $logFh, $devices, $included;
$fn = $topic[1]; // get friendlyname
logger(INFO, sprintf(_("Icoming notification of device %s => friendly name : %s"), $topic[0], $topic[1]));
logger(INFO, sprintf(_("Incoming notification of device %s => friendly name : %s"), $topic[0], $topic[1]));
$device = & $devices[$topic[0]];
$payloadArray = json_decode($message->payload);
//print_r($payloadArray);
//print_r($device) ;
if (!isset($device[$fn])) //must not exists, but ...
{
logger(LOG_WARNING, $logFh, "init of " . $fn .EOL);
@ -19,11 +15,9 @@ $topics["pws2mqtt"]->callback = function($topic, $message)
$device[$fn]["device"] = new device;
$device[$fn]["device"]->type = $payloadArray->type;
$device[$fn]["device"]->ieeeAddress = $payloadArray->ieeeAddress;
//addDevice($device[$fn], $fn, );
$indexDevices[$device[$fn]["device"]->ieeeAddress] = & $device[$fn]["device"];
}
//print_r($device);
$device = & $device[$fn];
changeDevice($topic[0], $topic[1], $device["device"], $payloadArray);
//print_r($device["device"]);
}
?>