1
0

some debbuging

This commit is contained in:
daniel Tartavel 2022-01-02 18:14:13 +01:00
parent 2e6b2fe5cb
commit d79463fbd4
11 changed files with 147 additions and 242 deletions

41
class/availability.php Normal file
View File

@ -0,0 +1,41 @@
<?php
class availability
{
// by default all devices are listening for availability
// callback fonction. Is called with these 3 parameters
// $device -> calling device
// $param -> parameter passed by mqtt
// $value -> value of the parameter
public function callBack($device, $param, $value)
{
switch($param)
{
case "availability": // theorically can't be other, but ....
if ($device->availability != $value)
{
//echo "==========>>>>>> Availability $value" . EOL;
if (!empty($device->availability))
{
$log = WARNING;
}else
{
$log = INFO;
}
$device->availability = $value
logger($log, sprintf(_("Device: %s/%s is %s"), $topic, $fn , $value));
}
break;
}
echo _("notification received from MQTT") . EOL;
//echo $param . "=> " . $value . EOL;
}
public function getHook()
{
return array($this,"callback");
}
}
$hooks["availability"] = new availability;
?>

View File

@ -1,31 +0,0 @@
<?php
<?php
class availability
{
// by default all devices are listening for availability
// callback fonction. Is called with these 4 parameters
public function callBack($topic, $fn, $param, $value)
{
global $devices, $indexDevices, $notificationMethods;
switch($param)
{
case "availability":
if ($value != "online")
{
//your code here
logger("Device: " . $topic . "/" . $fn . "is offline");
}
break;
}
echo _("notification received from MQTT") . EOL;
//echo $param . "=> " . $value . EOL;
}
}
$hooks["availability"] = new availability;
?>
?>

View File

@ -68,41 +68,4 @@ class interval
public $endDate;
}
class notificationMethod
{
public $url;
public $msg;
public $active;
public $level;
function __construct($url)
{
$this->url = $url;
}
public function send($text=null)
{
if (empty($text)) $text= $this->msg;
if (!empty($msg))
{
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: fr\r\n"
)
);
$context = stream_context_create($opts);
/* Envoi une requête HTTP vers $url
avec les en-têtes additionnels ci-dessus */
$fp = fopen($url . "/" . $msg, 'r', false, $context);
$response = stream_get_contents($fp, -1, 0);
fclose($fp);
}else
{
// TODO log_error("notificationMethod : $msg is null");
}
}
}
?>

View File

@ -1,106 +0,0 @@
class Message
{
public $id;
public $state = false;
public $msg;
}
class topic {
public $mid;
public $status;
public $info;
public $devices;
public $groups;
public $extensions;
public $config;
}
class device
{
public $method; //auto or manual
public $topic;
public $device;
public $ieeeAddress;
public $groupID;
public $friendlyName;
public $powerSource;
public $description;
public $functions;
public $payload;
public $availibility;
public function set($event)
{
publish($this, $this->payload, "set", $event);
}
public function get()
{
publish($this, $this->payload, "get", $event);
}
}
class ranges
{
public $start; //datetime
public $end; //datetime
}
class event
{
public $ieeeAddress;
public $topic;
public $param;
public $value;
public $device;
public $published;
public $dateTimeEvent; // datetime : next occurence for recurrent event
public $startDatetime;
public $stopDatetime;
public $recurrenceInterval; // interval : for recurrent event
public $exceptionInterval; // array of object ranges
}
class interval
{
public $startDate;
public $endDate;
}
class notificationMethod
{
public $url;
public $msg;
public $active;
public $level;
function __construct($url)
{
$this->url = $url;
}
public function send($text=null)
{
if (empty($text)) $text= $this->msg;
if (!empty($msg))
{
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: fr\r\n"
)
);
$context = stream_context_create($opts);
/* Envoi une requête HTTP vers $url
avec les en-têtes additionnels ci-dessus */
$fp = fopen($url . "/" . $msg, 'r', false, $context);
$response = stream_get_contents($fp, -1, 0);
fclose($fp);
}else
{
// TODO log_error("notificationMethod : $msg is null");
}
}
}

View File

@ -10,8 +10,11 @@ function storeDB($db, $filepath)
function mkDevicesDB($topic, $json, $group=false)
{
global $devices, $listProperties, $listPropertiesKeys, $indexDevices, $dbInit, $logFh;
if (!isset($devices[$topic])) $devices[$topic]= array();
global $devices, $listProperties, $listPropertiesKeys, $indexDevices, $dbInit, $logFh, $hooks;
if (!isset($devices[$topic]))
{
$devices[$topic]= array();
}
foreach ($json as $jsonDevice)
{
//print_r($device);
@ -19,12 +22,17 @@ function mkDevicesDB($topic, $json, $group=false)
$fnTree = explode("/", $fn);
$device = & $devices[$topic];
foreach($fnTree as $fnPart)
{
if (!isset($device[$fnPart]))
{
$device[$fnPart] = array();
}
$device = & $device[$fnPart];
}
if (!isset($device["device"]))
{
$device["device"] = new device;
}
$device["device"]->topic = $topic;
//$device["device"]->device = $jsonDevice;
$device["device"]->friendlyName = $jsonDevice->friendly_name;
@ -35,6 +43,21 @@ function mkDevicesDB($topic, $json, $group=false)
$indexDevices[$device["device"]->groupID] = $jsonDevice->friendly_name;
}else
{
addDevice($device, $fn, $jsonDevice);
}
}
$dbInit += 1;
fwrite($logFh, "################################START##################################################");
fwrite($logFh, print_r($devices, true));
fwrite($logFh, "################################END##################################################");
echo "Devices DB made" . EOL;
//print_r($devices);
}
function addDevice(& $device, $fn, $jsonDevice )
{
global $listProperties, $listPropertiesKeys, $hooks, $indexDevices;
$device["device"]->type = $jsonDevice->type;
$device["device"]->ieeeAddress = $jsonDevice->ieee_address;
if ( !empty($jsonDevice->power_source ) )
@ -47,17 +70,15 @@ function mkDevicesDB($topic, $json, $group=false)
searchPropertyKey($fn, $device["device"], $jsonDevice->definition->exposes, $listPropertiesKeys);
}
searchPropertyValue($fn, $device["device"], $jsonDevice, $listProperties);
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
//print_r($device);
}
}
$dbInit += 1;
fwrite($logFh, "################################START##################################################");
fwrite($logFh, print_r($devices, true));
fwrite($logFh, "################################END##################################################");
echo "Devices DB made" . EOL;
//print_r($devices);
// adding callback function for availability
//print_r($hooks);
$device["device"]->functions[] = $hooks["availability"]->getHook();
//indexing device
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
//print_r($device);
}
function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys)
@ -101,7 +122,7 @@ function changeDevice($topic, $fn, &$device, $payloadArray)
function iterateDevice($topic, $fn, &$device, $payloadArray)
{
global $changed;
print_r($payloadArray);
//print_r($payloadArray);
//echo "device =>";print_r($device);echo EOL;
foreach($payloadArray as $key => $value)
{
@ -128,8 +149,11 @@ function iterateDevice($topic, $fn, &$device, $payloadArray)
{
if (empty($device->$key) or $value != null)
{
if (isset($device->$key)) $oldValue = $device->$key;
if ($oldValue != $value)
if (isset($device->$key))
{
$oldValue = $device->$key;
}if ($oldValue != $value)
{
$device->{$key} = $value;
$changed[$fn]["key"] = $key;
@ -145,14 +169,15 @@ function iterateDevice($topic, $fn, &$device, $payloadArray)
}
echo " to " . $value . EOL;*/
}
//print_r($device->functions);
//print_r($device->functions); print_r($value);
if (!empty($device->functions))
{
echo "executing notifications functions " . EOL;
foreach($device->functions as $function)
{
//print_r($function);
$function($topic, $fn, $key, $value);
$function($device, $key, $value);
}
}
}

View File

@ -33,7 +33,7 @@ function checkEvents()
publish($event->device, array($event->param => $event->value), "set", $key);
if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false)
{
logger(ERROR, __("Error in event recurrence. event: ") . $key);
logger(ERROR, _("Error in event recurrence. event: ") . $key);
}
}
}
@ -88,7 +88,7 @@ function setRecurrentEvent(&$device, $param, $value, $startDatetime, $stopDateti
$event->dateTimeEvent = $event->startDatetime;
if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false)
{
logger(ERROR, __("Error in event recurrence. event: ") . $key);
logger(ERROR, _("Error in event recurrence. event: ") . $key);
}
}
@ -124,13 +124,13 @@ function setDelay(&$device, $delay, $unit="second", $param, $value, $replace=fal
}
if (empty($s))
{
logger(ERROR, __("setDelay error: unit is empty"));
logger(ERROR, _("setDelay error: unit is empty"));
}
if (($datetime->add(new DateInterval('PT'. $delay . $s))) === false)
{
logger(ERROR, __("setDelay error: datetime->add"));
logger(ERROR, _("setDelay error: datetime->add"));
}
print_r($datetime);
//print_r($datetime);
if ($replace)
{
$eventKey = searchEvent($device, $param, $value);
@ -169,7 +169,7 @@ function deleteEvent($eventKey)
{
global $events;
unset ($events[$eventKey]);
logger(INFO, __("delete event ") . $eventKey);
logger(INFO, _("delete event ") . $eventKey);
}

View File

@ -3,12 +3,12 @@ class notificationFreemobile
{
private $url = "https://smsapi.free-mobile.fr/sendmsg?user=32886706&pass=JTGUY6l5OG73zX&msg=";
private $name = "freemobile";
public $active = true;
public $level;
function __construct()
{
global $notificationMethods;
$notificationMethods[$name]->active = true;
$notificationMethods[$name]->level = ALL;
$this->level = WARNING | ERROR;
}
function send($message)
@ -23,7 +23,13 @@ class notificationFreemobile
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
logger(INFO, curl_exec($ch));
if (($result = curl_exec($ch)) === false)
{
logger(ERROR, _(sprintf( "Curl return error: %s when sending notification", curl_error($ch))));
}else
{
logger(INFO, _("Curl return: ") . $result . _(" when sending notification"));
}
// close curl resource to free up system resources
curl_close($ch);
@ -33,6 +39,6 @@ class notificationFreemobile
}
}
$notificationMethods["freemobile"] = new notificationMethod();
$notificationMethods["freemobile"] = new notificationFreemobile();
?>

View File

@ -21,37 +21,40 @@ class rdc_panneau_salon
}
// callback fonction. Is called with these 4 parameters
public function callBack($topic, $fn, $param, $value)
public function callBack(&$device, $param, $value)
{
global $devices, $indexDevices;
switch($param)
{
case "occupancy":
if ($value == 1) $this->send();
if ($value == 1 and $indexDevices["0x04cf8cdf3c78aff0"]->illuminance_lux <= $this->luminance_min)
{
$this->send("ON");
}
break;
case "contact":
if ($value == false) $this->send();
if ($value == false and $indexDevices["0x04cf8cdf3c78aff0"]->illuminance_lux <= $this->luminance_min)
{
$this->send("ON");
}
break;
case "illuminance_lux":
if ($value >= $this->luminance_max) $this->send();
if ($value >= $this->luminance_max) $this->send("OFF");
}
echo _("notification received from MQTT") . EOL;
//echo $param . "=> " . $value . EOL;
}
private function send()
private function send($state)
{
global $devices, $indexDevices;
if ($indexDevices["0x04cf8cdf3c78aff0"]->illuminance_lux <= $this->luminance_min)
{
$msg = array("state" => "ON");
$msg = array("state" => $state);
$device = & $indexDevices["0x588e81fffe343e8f"];
logger(INFO, __("publishing ") . $msg . __(" message: ") . $device->friendlyName);
logger(INFO, _("publishing ") . $msg . _(" message: ") . $device->friendlyName);
$device->payload = $msg;
$device->set(null);
setDelay($device, $this->delay, $this->timeUnit, "state", "OFF", true);
}
}
}
$hooks["rdc/panneau/salon"] = new rdc_panneau_salon();

View File

@ -4,15 +4,13 @@
define( "EOL", "\n");
define("Z2M", "zigbee2mqtt");
declare(ticks = 1);
$listProperties = array("powerSource" => "batterie");
$listPropertiesKeys = array("property");
include "class/main.php";
require "class/main.php";
//global variables
$topics = array(); // list of topics
@ -36,25 +34,28 @@ $topics["zigbee2mqtt"] = new topic;
bindtextdomain("moha", "./locale");
textdomain("moha");
if (!init()) exit(1);
require "mqtt_functions.php";
require "utils.php";
require "events.php";
require "db_functions.php";
if (!init()) exit(1);
$client = new Mosquitto\Client();
// log levels
define( "DEBUG", $client->LOG_DEBUG);
define( "INFO", $client->LOG_INFO);
define( "NOTICE", $client->LOG_NOTICE);
define( "WARNING", $client->LOG_WARNING);
define( "ERROR", $client->LOG_ERR);
define( "DEBUG", $client::LOG_DEBUG);
define( "INFO", $client::LOG_INFO);
define( "NOTICE", $client::LOG_NOTICE);
define( "WARNING", $client::LOG_WARNING);
define( "ERROR", $client::LOG_ERR);
define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR);
$logLevel = DEBUG;
$notificationLevel = WARNING | ERROR;
require "utils.php";
require "mqtt_functions.php";
require "events.php";
require "db_functions.php";
require "class/availability.php";
// defining callback functions
$client->onConnect('connectResponse');
$client->onDisconnect('disconnectResponse');

View File

@ -46,7 +46,7 @@ function message($message)
{
unset ($fnTree[array_key_last($fnTree)]);
$payloadArray = array("availability" => $message->payload);
print_r($payloadArray);
//print_r($payloadArray);
}else
{
$payloadArray = json_decode($message->payload);

View File

@ -12,6 +12,8 @@ function signalHandler($signal)
function notify($message)
{
global $notificationMethods;
$result = false;
foreach($notificationMethods as $value)
{
$result |= $value->send($message);
@ -21,7 +23,8 @@ function notify($message)
function logger($level, $log)
{
global $logFh, $logLevel;
global $logFh, $logLevel, $notificationLevel;
echo "$level"
if ( $level >= $logLevel)
{
fwrite($logFh, "$level : $log" . EOL);
@ -29,9 +32,9 @@ function logger($level, $log)
}
if ($level >= $notificationLevel)
{
if(notify($message) == true)
if(notify(" Moha\n" . $log) == false)
{
logger(INFO, __("Notification not sent"));
logger(INFO, _("Notification not sent"));
}
}
}