1
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]);
}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))
{
logger(DEBUG, $command . _(" is a file"), __FILE__ . ":" . __LINE__);
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->payload = array($argList["property"] => $argList["value"]);
$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");
}
return $response;
@ -482,19 +482,24 @@ function PropertiesDashboard($argList)
logger(INFO, _("propertiesDashboard function"), __FILE__ . ":" . __LINE__);
$response = insertJavascript();
$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 ))
{
foreach($index as $device)
{
$propertyObject = $device->properties[$propertyName];
$value = $propertyObject["value"];
logger(DEBUG, $device->friendlyName . " => " . bool2string(_($value)), __FILE__ . ":" . __LINE__);
$response .= $device->friendlyName . aliases($device->friendlyName, $property) . ' => ';
$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, $property, $value);
$response .= "     " . displayChoice($device, $propertyName, $value);
}
if(($propertyObject["access"] & 4))
{
@ -516,7 +521,9 @@ function PropertiesDashboard($argList)
$response .= EOLH;
}
}
return "json=" . $response;
}
}
return $response;
}
function apiHookActive($argList, $status)

View File

@ -9,20 +9,32 @@ class hook
// list of devices we are listening to
function __construct()
{
if ($this->active === true)
{
logger(INFO, _("Initializing hook: ") . $this->hookName);
//$this->installHooks();
foreach ($this->devicelist as $ieeeAddress => $property)
{
$this->propertyInitialized[$ieeeAddress][$property] = false;
}
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
{
logger(INFO, _("Not initializing inactive hook: ") . $this->hookName);
}
function installHooks(&$indexDevices)
}
function installHooksFunction(&$indexDevices)
{
global $devices, $devicesRequest;
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";
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
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;
curl_exec($ch);
if (curl_errno($ch) != 0)
{
$this->curlErr += 1;
$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;
}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;
}
}else
@ -61,5 +61,9 @@ class notificationFreemobile
}
}
// constant definition
define("NOTIF_DEFAULT_DEST", "daniel");
//init class
$notificationMethods["freemobile"] = new notificationFreemobile();
?>

View File

@ -1,31 +1,40 @@
<?php
class availability
class availability extends hook
{
public $hookname = "availability";
public $initialized = true;
public $active = true;
// by default all devices are listening for availability
protected $devicelist = array();
function __construct()
function init()
{
global $devices;
global $indexDevices;
$this->iterate();
}
private function iterate()
protected function iterate()
{
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)
{
return true;
static $indexDevicesSize;
$tmp = count($indexDevices);
if ($tmp != $indexDevicesSize)
{
$this->iterate();
$indexDevicesSize = $tmp;
}
return false;
}
// callback fonction. Is called with these 3 parameters
// $device -> calling device
// $property -> parameter passed by mqtt
@ -35,18 +44,19 @@ class availability
switch($property)
{
case "availability": // theorically can't be other, but ....
if ($device->availability != $value)
if ($device->properties["availability"]["value]"] != $value)
{
//echo "==========>>>>>> Availability $value" . EOL;
//if (!empty($device->availability))
//{
//{[ Torrent911.com]
$log = ALERT;
//}else
//{
// $log = INFO;
//}
//$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;
}

View File

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

View File

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

View File

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

View File

@ -20,6 +20,11 @@ class rdc_salon_eclairage extends hook
public $luminance_min = 50;
public $luminance_max = 100;
function installHooks(&$indexDevices)
{
return $this->installHooksFunction($indexDevices);
}
// callback fonction. Is called with these 4 parameters
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_MVMT, "occupancy", 1)
);
function installHooks(&$indexDevices)
{
return $this->installHooksFunction($indexDevices);
}
// callback fonction. Is called with these 3 parameters
public function callBack(&$device, $property, $value)
{

View File

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

View File

@ -21,6 +21,11 @@ class rdc_store extends hook
METEO => "windgustkmh"
);
function installHooks(&$indexDevices)
{
$this->installHooksFunction($indexDevices);
}
// callback fonction. Is called with these 3 parameters
// $device -> calling device
// $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)
{
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");
}
@ -237,7 +242,7 @@ class rdc_store extends hook
}
}
private function send($level)
private function send($level, $method)
{
global $indexDevices;
$deviceObject = $indexDevices[RDC_STORE];

View File

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

View File

@ -50,7 +50,7 @@ if ($testMode)
$httpServerIp = "192.168.1.253";
}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
$dataPath = "/usr/share/moha/";
$logFile = "/var/log/moha.log"; // Path of log file
@ -64,18 +64,25 @@ if (!init()) exit(1);
bindtextdomain("moha", "./locale");
textdomain("moha");
function notify($message, $destinataire=NOTIF_DEFAULT_DEST)
function notify($message, $device)
{
global $notificationMethods;
$result = false;
//TODO destinataire selon fichier de config
$destinataire = array("daniel");
foreach($notificationMethods as $value)
{
$result = $result | $value->send($message, $destinataire);
foreach($destinataire as $dest)
{
$result = $result | $value->send($message, $dest);
}
}
return $result;
}
function logger($level, $log, $pos = "", $notif = true)
function logger($level, $log, $pos = "", $device=null, $notif = true)
{
global $logFh, $logLevel, $notificationLevel, $logLevels;
$logString = $logLevels[$level] . " " ;
@ -92,7 +99,7 @@ function logger($level, $log, $pos = "", $notif = true)
$test = $level & $notificationLevel;
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);
return true;
@ -330,6 +337,11 @@ while (true)
{
$i = 1;
foreach($hooks as $hookName => &$hook)
{
if ($hook->active === false)
{
$i = 1;
}else
{
if ($hook->initialized === false); // and $hook->active === true)
{
@ -337,6 +349,7 @@ while (true)
$i &= $hook->installHooks($indexDevices);
}
}
}
$hooksInitialized = $i;
}elseif($flagHooks === false)
{