1
0

added distants notifications and devices availability warning

This commit is contained in:
daniel Tartavel 2022-01-02 13:47:41 +01:00
parent 02dfa615fa
commit 2e6b2fe5cb
6 changed files with 99 additions and 17 deletions

31
class/availibility.php Normal file
View File

@ -0,0 +1,31 @@
<?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

@ -33,7 +33,7 @@ function checkEvents()
publish($event->device, array($event->param => $event->value), "set", $key);
if (($event->dateTimeEvent->add($event->recurrenceInterval)) === false)
{
logger(LOG_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(LOG_ERROR, __("Error in event recurrence. event: ") . $key);
logger(ERROR, __("Error in event recurrence. event: ") . $key);
}
}
@ -124,11 +124,11 @@ function setDelay(&$device, $delay, $unit="second", $param, $value, $replace=fal
}
if (empty($s))
{
logger(LOG_ERROR, __("setDelay error: unit is empty"));
logger(ERROR, __("setDelay error: unit is empty"));
}
if (($datetime->add(new DateInterval('PT'. $delay . $s))) === false)
{
logger(LOG_ERROR, __("setDelay error: datetime->add"));
logger(ERROR, __("setDelay error: datetime->add"));
}
print_r($datetime);
if ($replace)
@ -169,7 +169,7 @@ function deleteEvent($eventKey)
{
global $events;
unset ($events[$eventKey]);
logger(LOG_INFO, __("delete event ") . $eventKey);
logger(INFO, __("delete event ") . $eventKey);
}

View File

@ -1,2 +1,38 @@
<?php
$notificationMethods["freemobile"] = new notificationMethod("https://smsapi.free-mobile.fr/sendmsg?user=32886706&pass=JTGUY6l5OG73zX&msg=");
class notificationFreemobile
{
private $url = "https://smsapi.free-mobile.fr/sendmsg?user=32886706&pass=JTGUY6l5OG73zX&msg=";
private $name = "freemobile";
function __construct()
{
global $notificationMethods;
$notificationMethods[$name]->active = true;
$notificationMethods[$name]->level = ALL;
}
function send($message)
{
if ($this->active == true)
{
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $this->url . $message);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
logger(INFO, curl_exec($ch));
// close curl resource to free up system resources
curl_close($ch);
return true;
}
return false;
}
}
$notificationMethods["freemobile"] = new notificationMethod();
?>

View File

@ -53,6 +53,7 @@ define( "WARNING", $client->LOG_WARNING);
define( "ERROR", $client->LOG_ERR);
define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR);
$logLevel = DEBUG;
$notificationLevel = WARNING | ERROR;
// defining callback functions
$client->onConnect('connectResponse');

View File

@ -162,15 +162,4 @@ function publishResponse($mid)
$mids[$mid] = true;
}
}
function logger($level, $log)
{
global $logFh, $logLevel;
if ( $level >= $logLevel)
{
fwrite($logFh, "$level : $log" . EOL);
print ("$level : $log" . EOL);
}
}
?>

View File

@ -10,5 +10,30 @@ function signalHandler($signal)
endMoha();
}
function notify($message)
{
foreach($notificationMethods as $value)
{
$result |= $value->send($message);
}
return $result;
}
function logger($level, $log)
{
global $logFh, $logLevel;
if ( $level >= $logLevel)
{
fwrite($logFh, "$level : $log" . EOL);
print ("$level : $log" . EOL);
}
if ($level >= $notificationLevel)
{
if(notify($message) == true)
{
logger(INFO, __("Notification not sent"));
}
}
}
?>