DTux
/
dtux__moha
Archived
1
0
Fork 0

debugged notification of availability of devices

This commit is contained in:
Daniel Tartavel 2022-08-12 10:41:55 +02:00
parent 247d1fd17d
commit df94a6becf
18 changed files with 176 additions and 70 deletions

View File

@ -153,11 +153,14 @@ function apiServer($read)
apiDashboard($spawn, $argList[0]); apiDashboard($spawn, $argList[0]);
}else }else
{ {
logger(DEBUG, $command . _(" so default action"), __FILE__ . ":" . __LINE__); logger(DEBUG, $command . _(" does not exists ... so default action"), __FILE__ . ":" . __LINE__);
if (file_exists("php://temp/". $command)) if (file_exists("php://temp/". $command))
{ {
logger(DEBUG, $command . _(" is a file"), __FILE__ . ":" . __LINE__); logger(DEBUG, $command . _(" is a file"), __FILE__ . ":" . __LINE__);
htmlSend($spawn, file_get_contents("php://temp/". $command), 'Content-Type: image/png'); htmlSend($spawn, file_get_contents("php://temp/". $command), 'Content-Type: image/png');
}else
{
htmlSend($spawn, "command does not exists");
} }
} }
} }

View File

@ -342,7 +342,7 @@ function apiSet($argList)
$device = $indexFriendlyNames[$argList["fn"]]; $device = $indexFriendlyNames[$argList["fn"]];
$device->payload = array($argList["property"] => $argList["value"]); $device->payload = array($argList["property"] => $argList["value"]);
$device->set($argList["property"], IDLE); $device->set($argList["property"], IDLE);
publish($argList["topic"] . "/" . $argList["fn"], $payload, "set"); publish($argList["topic"] . "/" . $argList["fn"], $device->payload, "set");
//removeEvent($indexFriendlyNames($argList["fn"]), $argList["property"], "OFF"); //removeEvent($indexFriendlyNames($argList["fn"]), $argList["property"], "OFF");
} }
return $response; return $response;
@ -482,41 +482,48 @@ function PropertiesDashboard($argList)
logger(INFO, _("propertiesDashboard function"), __FILE__ . ":" . __LINE__); logger(INFO, _("propertiesDashboard function"), __FILE__ . ":" . __LINE__);
$response = insertJavascript(); $response = insertJavascript();
$property = $argList["property"]; $property = $argList["property"];
foreach ($indexProperties[$property] as $device) print_r($indexProperties[$property]);
foreach($indexProperties as $propertyName => $index)
{ {
$propertyObject = $device->properties[$property]; if (str_contains($propertyName, $property ))
$value = $propertyObject["value"];
logger(DEBUG, $device->friendlyName . " => " . bool2string(_($value)), __FILE__ . ":" . __LINE__);
$response .= $device->friendlyName . aliases($device->friendlyName, $property) . ' => ';
if (array_key_exists("access", $propertyObject))
{ {
logger(DEBUG, _("Access = ") . $propertyObject["access"], __FILE__ . ":" . __LINE__); foreach($index as $device)
if(($propertyObject["access"] & 2))
{ {
logger(DEBUG, _("Write Access OK ") . ($propertyObject["access"] & 2), __FILE__ . ":" . __LINE__); $propertyObject = $device->properties[$propertyName];
$response .= "     " . displayChoice($device, $property, $value); $value = $propertyObject["value"];
logger(DEBUG, $device->friendlyName . " => " . bool2string(_($value)), __FILE__ . ":" . __LINE__);
$response .= $device->friendlyName . aliases($device->friendlyName, $propertyName) . ' => ';
if (array_key_exists("access", $propertyObject))
{
logger(DEBUG, _("Access = ") . $propertyObject["access"], __FILE__ . ":" . __LINE__);
if(($propertyObject["access"] & 2))
{
logger(DEBUG, _("Write Access OK ") . ($propertyObject["access"] & 2), __FILE__ . ":" . __LINE__);
$response .= "     " . displayChoice($device, $propertyName, $value);
}
if(($propertyObject["access"] & 4))
{
logger(DEBUG, _("can get value") . ($propertyObject["access"] & 4), __FILE__ . ":" . __LINE__);
$response .= '<input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $propertyObject["name"] . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "','" . $device->friendlyName . "','" . $propertyObject["name"] . "')\">";
}else
{
$response .= $value;
}
$response .= EOLH;
}else
{
$response .= bool2string(_($value));
if(array_key_exists("unit", $propertyObject))
{
$response .= $propertyObject["unit"];
logger(DEBUG, _("unit = ") . $unit, __FILE__ . ":" . __LINE__);
}
$response .= EOLH;
}
} }
if(($propertyObject["access"] & 4))
{
logger(DEBUG, _("can get value") . ($propertyObject["access"] & 4), __FILE__ . ":" . __LINE__);
$response .= '<input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $propertyObject["name"] . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "','" . $device->friendlyName . "','" . $propertyObject["name"] . "')\">";
}else
{
$response .= $value;
}
$response .= EOLH;
}else
{
$response .= bool2string(_($value));
if(array_key_exists("unit", $propertyObject))
{
$response .= $propertyObject["unit"];
logger(DEBUG, _("unit = ") . $unit, __FILE__ . ":" . __LINE__);
}
$response .= EOLH;
} }
} }
return "json=" . $response; return $response;
} }
function apiHookActive($argList, $status) function apiHookActive($argList, $status)

View File

@ -10,19 +10,31 @@ class hook
// list of devices we are listening to // list of devices we are listening to
function __construct() function __construct()
{ {
logger(INFO, _("Initializing hook: ") . $this->hookName); if ($this->active === true)
//$this->installHooks();
foreach ($this->devicelist as $ieeeAddress => $property)
{ {
$this->propertyInitialized[$ieeeAddress][$property] = false; logger(INFO, _("Initializing hook: ") . $this->hookName);
} //$this->installHooks();
if (method_exists($this, "init")) if (method_exists($this, "init"))
{
$this->init();
}
if (method_exists($this, "iterate"))
{
$this->iterate();
}
foreach ($this->devicelist as $ieeeAddress => $property)
{
$this->propertyInitialized[$ieeeAddress][$property] = false;
}
}else
{ {
$this->init(); logger(INFO, _("Not initializing inactive hook: ") . $this->hookName);
} }
} }
function installHooks(&$indexDevices) function installHooksFunction(&$indexDevices)
{ {
global $devices, $devicesRequest; global $devices, $devicesRequest;
static $requestflag = 0; static $requestflag = 0;

5
config/logger.php Normal file
View File

@ -0,0 +1,5 @@
<?php
?>

View File

@ -29,5 +29,4 @@ foreach ($logLevels as $key => $string)
$logLevels[6] = "NOTICE & WARNING"; $logLevels[6] = "NOTICE & WARNING";
define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT); define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT);
define("NOTIF_DEFAULT_DEST", "daniel");
?> ?>

View File

@ -31,18 +31,18 @@ class notificationFreemobile
// $result contains the output string // $result contains the output string
if ($this->curlErr <= 5) if ($this->curlErr <= 5)
{ {
logger(DEBUG, _("Curl sending message"), false, __FILE__ . ":" . __LINE__); logger(DEBUG, _("Curl sending message"), __FILE__ . ":" . __LINE__);
echo $this->url . urlencode(trim($message)) . EOL; echo $this->url . urlencode(trim($message)) . EOL;
curl_exec($ch); curl_exec($ch);
if (curl_errno($ch) != 0) if (curl_errno($ch) != 0)
{ {
$this->curlErr += 1; $this->curlErr += 1;
$this->lastTry = time(); $this->lastTry = time();
logger(ERROR, sprintf( _("Curl return error %d: %s when sending notification"), curl_errno($ch), curl_error($ch)), false, __FILE__ . ":" . __LINE__); logger(ERROR, sprintf( _("Curl return error %d: %s when sending notification"), curl_errno($ch), curl_error($ch)), __FILE__ . ":" . __LINE__);
$error = true; $error = true;
}else }else
{ {
logger(DEBUG, sprintf(_("Curl return: %s when sending notification"), curl_error($ch)), false, __FILE__ . ":" . __LINE__); logger(DEBUG, sprintf(_("Curl return: %s when sending notification"), curl_error($ch)), __FILE__ . ":" . __LINE__);
$this->curlErr = 0; $this->curlErr = 0;
} }
}else }else
@ -61,5 +61,9 @@ class notificationFreemobile
} }
} }
// constant definition
define("NOTIF_DEFAULT_DEST", "daniel");
//init class
$notificationMethods["freemobile"] = new notificationFreemobile(); $notificationMethods["freemobile"] = new notificationFreemobile();
?> ?>

View File

@ -1,31 +1,40 @@
<?php <?php
class availability class availability extends hook
{ {
public $hookname = "availability"; public $hookname = "availability";
public $initialized = true;
public $active = true; public $active = true;
// by default all devices are listening for availability // by default all devices are listening for availability
protected $devicelist = array();
function __construct() function init()
{ {
global $devices; global $indexDevices;
$this->iterate(); $this->iterate();
} }
private function iterate() protected function iterate()
{ {
global $indexDevices; global $indexDevices;
foreach ($indexDevices as &$value) foreach ($indexDevices as $ieeeAddress => $value)
{ {
$value->properties["availability"]["functions"][] = array($this,"callback"); $deviceList[] = array( $ieeeAdress => "availability");
} }
$this->installHooksFunction($indexDevices);
} }
function installHooks(&$indexDevices) function installHooks(&$indexDevices)
{ {
return true; static $indexDevicesSize;
$tmp = count($indexDevices);
if ($tmp != $indexDevicesSize)
{
$this->iterate();
$indexDevicesSize = $tmp;
}
return false;
} }
// callback fonction. Is called with these 3 parameters // callback fonction. Is called with these 3 parameters
// $device -> calling device // $device -> calling device
// $property -> parameter passed by mqtt // $property -> parameter passed by mqtt
@ -35,18 +44,19 @@ class availability
switch($property) switch($property)
{ {
case "availability": // theorically can't be other, but .... case "availability": // theorically can't be other, but ....
if ($device->availability != $value) if ($device->properties["availability"]["value]"] != $value)
{ {
//echo "==========>>>>>> Availability $value" . EOL; //echo "==========>>>>>> Availability $value" . EOL;
//if (!empty($device->availability)) //if (!empty($device->availability))
//{ //{[ Torrent911.com]
$log = ALERT; $log = ALERT;
//}else //}else
//{ //{
// $log = INFO; // $log = INFO;
//} //}
//$device->availability = $value; //$device->availability = $value;
logger($log, sprintf(_("Device: %s/%s is %s"), $device->topic, $device->friendlyName, bool2string($value)), __FILE__ . ":" . __LINE__);
logger($log, sprintf(_("Device: %s/%s is %s"), $device->topic, $device->friendlyName, bool2string($value)), __FILE__ . ":" . __LINE__, $device);
} }
break; break;
} }

View File

@ -49,6 +49,11 @@ class radiateurs extends hook
// ETAGE_CHAMBRE_RADIATEUR => 0 // ETAGE_CHAMBRE_RADIATEUR => 0
); );
function installHooks(&$indexDevices)
{
return $this->installHooksFunction($indexDevices);
}
// callback fonction. Is called with these 3 parameters // callback fonction. Is called with these 3 parameters
public function callBack(&$device, $property, $value) public function callBack(&$device, $property, $value)
{ {

View File

@ -16,6 +16,11 @@ class rdc_chambre_eclairage extends hook
RDC_CHAMBRE_AMBIANCE_INTER => "action" RDC_CHAMBRE_AMBIANCE_INTER => "action"
); );
function installHooks(&$indexDevices)
{
return $this->installHooksFunction($indexDevices);
}
// callback fonction. Is called with these 3 parameters // callback fonction. Is called with these 3 parameters
// $device -> calling device // $device -> calling device
// $property -> property of the device (given by mqtt) // $property -> property of the device (given by mqtt)

View File

@ -14,6 +14,11 @@ class alerte_intrusion extends hook
GARAGE_PORTAIL => "contact" GARAGE_PORTAIL => "contact"
); );
function installHooks(&$indexDevices)
{
return $this->installHooksFunction($indexDevices);
}
// callback fonction. Is called with these 4 parameters // callback fonction. Is called with these 4 parameters
public function callBack($device, $property, $value) public function callBack($device, $property, $value)
{ {

View File

@ -20,6 +20,11 @@ class rdc_salon_eclairage extends hook
public $luminance_min = 50; public $luminance_min = 50;
public $luminance_max = 100; public $luminance_max = 100;
function installHooks(&$indexDevices)
{
return $this->installHooksFunction($indexDevices);
}
// callback fonction. Is called with these 4 parameters // callback fonction. Is called with these 4 parameters
public function callBack($device, $param, $value) public function callBack($device, $param, $value)
{ {

View File

@ -27,6 +27,12 @@ class rdc_sdb_eclairage extends hook
array(RDC_SDB_PLAFOND_MVMT, "occupancy", 1), array(RDC_SDB_PLAFOND_MVMT, "occupancy", 1),
array(RDC_SDB_MVMT, "occupancy", 1) array(RDC_SDB_MVMT, "occupancy", 1)
); );
function installHooks(&$indexDevices)
{
return $this->installHooksFunction($indexDevices);
}
// callback fonction. Is called with these 3 parameters // callback fonction. Is called with these 3 parameters
public function callBack(&$device, $property, $value) public function callBack(&$device, $property, $value)
{ {

View File

@ -21,6 +21,11 @@ class rdc_store extends hook
METEO => "windgustkmh" METEO => "windgustkmh"
); );
function installHooks(&$indexDevices)
{
return $this->installHooksFunction($indexDevices);
}
// callback fonction. Is called with these 3 parameters // callback fonction. Is called with these 3 parameters
// $device -> calling device // $device -> calling device
// $property -> property of the device (given by mqtt) // $property -> property of the device (given by mqtt)

View File

@ -21,6 +21,11 @@ class rdc_store extends hook
METEO => "windgustkmh" METEO => "windgustkmh"
); );
function installHooks(&$indexDevices)
{
$this->installHooksFunction($indexDevices);
}
// callback fonction. Is called with these 3 parameters // callback fonction. Is called with these 3 parameters
// $device -> calling device // $device -> calling device
// $property -> property of the device (given by mqtt) // $property -> property of the device (given by mqtt)
@ -153,7 +158,7 @@ class rdc_store extends hook
} }
}elseif ($exterieurLuxMoyen < 6000 and $salon_lux < 200 and $soleil < 100) }elseif ($exterieurLuxMoyen < 6000 and $salon_lux < 200 and $soleil < 100)
{ {
logger(DEBUG, "exterieurLuxMoyen < 11000 and salon_lux < 1000", __FILE__ . ":" . __LINE__); logger(DEBUG, "exterieurLuxMoyen < 6000 and salon_lux < 200 and soleil < 100", __FILE__ . ":" . __LINE__);
$this->close("Luminosité faible"); $this->close("Luminosité faible");
} }
@ -237,7 +242,7 @@ class rdc_store extends hook
} }
} }
private function send($level) private function send($level, $method)
{ {
global $indexDevices; global $indexDevices;
$deviceObject = $indexDevices[RDC_STORE]; $deviceObject = $indexDevices[RDC_STORE];

View File

@ -4,26 +4,33 @@ class rdc_temperature_int_ext extends hook
{ {
public $hookName = "rdc_wc_eclairage"; public $hookName = "rdc_wc_eclairage";
public $active = true; //enable/disable hook (true => enabled) public $active = true; //enable/disable hook (true => enabled)
public $wantedTemp = 24;
protected $devicelist = array( protected $devicelist = array(
METEO=> "tempc", METEO=> "tempc",
); );
function installHooks(&$indexDevices)
{
return $this->installHooksFunction($indexDevices);
}
// callback fonction. Is called with these 4 parameters // callback fonction. Is called with these 4 parameters
public function callBack(&$device, $property, $value) public function callBack(&$device, $property, $value)
{ {
global $indexDevices; global $indexDevices;
static $status = 0; static $status = -1;
if ($device->properties["indoortempc"]["value"] > $value and $status === 1) $indoorTemp = $device->properties["indoortempc"]["value"];
if ( (($value > $this->wantedTemp and $indoorTemp < $this->wantedTemp) or ( $value < $this->wantedTemp and $indoorTemp >= $this->wantedTemp)) and $status === 1)
{ {
$status = 0; $status = 0;
logger(ALERT, _("Indoor temperature is superior to outdoor one")); logger(ALERT, _("Open doors to climate"), null ,$device);
}elseif ($device->properties["indoortempc"]["value"] < $value and $status === 0) }elseif ( (($value > $this->wantedTemp and $indoorTemp >= $this->wantedTemp) or ( $value < $this->wantedTemp and $indoorTemp <= $this->wantedTemp)) and $status === 0 )
{ {
$status = 1; $status = 1;
logger(ALERT, _("Indoor temperature is inferior to outdoor one")); logger(ALERT, _("Close doors to climate"), null, $device);
} }
logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, bool2string($value)), __FILE__ . ":" . __LINE__); logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, bool2string($value)), __FILE__ . ":" . __LINE__, $device);
} }
} }

View File

@ -14,6 +14,11 @@ class rdc_wc_eclairage extends hook
public $delayManual = 10; // amount of time in $timeunit for manual mode 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 public $timeUnit = "minute"; // unit of time for delay, second, minute, day, week, month, year
function installHooks(&$indexDevices)
{
return $this->installHooksFunction($indexDevices);
}
// callback fonction. Is called with these 4 parameters // callback fonction. Is called with these 4 parameters
public function callBack(&$device, $property, $value) public function callBack(&$device, $property, $value)
{ {

View File

@ -14,6 +14,11 @@ class rdc_wc_eclairage extends hook
public $delayManual = 10; // amount of time in $timeunit for manual mode 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 public $timeUnit = "minute"; // unit of time for delay, second, minute, day, week, month, year
function installHooks(&$indexDevices)
{
$this->installHooksFunction($indexDevices);
}
// callback fonction. Is called with these 4 parameters // callback fonction. Is called with these 4 parameters
public function callBack(&$device, $property, $value) public function callBack(&$device, $property, $value)
{ {

View File

@ -50,7 +50,7 @@ if ($testMode)
$httpServerIp = "192.168.1.253"; $httpServerIp = "192.168.1.253";
}else }else
{ {
$logLevel = DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT; $logLevel = INFO | NOTICE | WARNING | ERROR | ALERT;
$mqttServerIp = "127.0.0.1"; // IP address of mqttserver in production mode $mqttServerIp = "127.0.0.1"; // IP address of mqttserver in production mode
$dataPath = "/usr/share/moha/"; $dataPath = "/usr/share/moha/";
$logFile = "/var/log/moha.log"; // Path of log file $logFile = "/var/log/moha.log"; // Path of log file
@ -64,18 +64,25 @@ if (!init()) exit(1);
bindtextdomain("moha", "./locale"); bindtextdomain("moha", "./locale");
textdomain("moha"); textdomain("moha");
function notify($message, $destinataire=NOTIF_DEFAULT_DEST) function notify($message, $device)
{ {
global $notificationMethods; global $notificationMethods;
$result = false; $result = false;
//TODO destinataire selon fichier de config
$destinataire = array("daniel");
foreach($notificationMethods as $value) foreach($notificationMethods as $value)
{ {
$result = $result | $value->send($message, $destinataire); foreach($destinataire as $dest)
{
$result = $result | $value->send($message, $dest);
}
} }
return $result; return $result;
} }
function logger($level, $log, $pos = "", $notif = true) function logger($level, $log, $pos = "", $device=null, $notif = true)
{ {
global $logFh, $logLevel, $notificationLevel, $logLevels; global $logFh, $logLevel, $notificationLevel, $logLevels;
$logString = $logLevels[$level] . " " ; $logString = $logLevels[$level] . " " ;
@ -92,7 +99,7 @@ function logger($level, $log, $pos = "", $notif = true)
$test = $level & $notificationLevel; $test = $level & $notificationLevel;
if (($test != 0) and ($notif === true)) if (($test != 0) and ($notif === true))
{ {
if(notify("Moha\n" . $logString) === false) if(notify("Moha\n" . $logString, $device) === false)
{ {
logger(INFO, _("Notification not sent"), __FILE__ . ":" . __LINE__, false); logger(INFO, _("Notification not sent"), __FILE__ . ":" . __LINE__, false);
return true; return true;
@ -331,10 +338,16 @@ while (true)
$i = 1; $i = 1;
foreach($hooks as $hookName => &$hook) foreach($hooks as $hookName => &$hook)
{ {
if ($hook->initialized === false); // and $hook->active === true) if ($hook->active === false)
{ {
logger(WARNING, _("Initializing Hook not completely initialized :") . $hookName, __FILE__ . ":" . __LINE__); $i = 1;
$i &= $hook->installHooks($indexDevices); }else
{
if ($hook->initialized === false); // and $hook->active === true)
{
logger(WARNING, _("Initializing Hook not completely initialized :") . $hookName, __FILE__ . ":" . __LINE__);
$i &= $hook->installHooks($indexDevices);
}
} }
} }
$hooksInitialized = $i; $hooksInitialized = $i;