1
0
moha/utils.php
2022-09-09 16:53:49 +02:00

240 lines
5.7 KiB
PHP

<?php
////logger(DEBUG, _("Including utils.php"), __FILE__ . ":" . __LINE__);
/*function logger($level, $log, $pos = "")
{
global $logFh, $logLevel, $notificationLevel, $logLevels;
$logString = $logLevels[$level] . " " ;
if ($pos !== false)
{
$logString .= $pos;
}
$logString .= " - " . $log;
if ($level & $logLevel)
{
fwrite($logFh, date("c") . ' ' . $logString . EOL);
print ("MOHA-" . $logString . EOL);
}
}*/
function checkTopicsAvailability()
{
global$topics;
foreach($topics as $topicName => $topic)
{
if ($topic->status == 1)
{
logger(DEBUG, "Topic is :" . $topicName . " and time is " . time() . " lastSeen is " . $topic->lastSeen, __FILE__ . ":" . __LINE__ );
if ((time() - $topic->lastSeen > $topic->timeOut*60) and ($topic->notificationSent == false))
{
if (logger(ALERT, $topicName . _(" is not available"), __FILE__ . ":" . __LINE__) == false);
{
$topic->notificationSent = true;
//system("systemctl restart ")
}
}
}
}
}
function testActionneurs(array $actionneurs) //return 0 for no actionneurs and 1 if one actionneur is on
{
global $indexDevices;
$r = 0;
foreach ($actionneurs as $device)
{
logger(DEBUG, _("device: ") . var_dump($device), __FILE__ . ":" . __LINE__ );
if (array_key_exists($device[0], $indexDevices))
{
if (array_key_exists($device[1], $indexDevices[$device[0]]->properties))
{
$s = ($indexDevices[$device[0]]->properties[$device[1]]["value"] == $device[2]);
logger(DEBUG, _("testActionneurs : result is ") . bool2string($indexDevices[$device[0]]->friendlyName) . "=>" . bool2string($s), __FILE__ . ":" . __LINE__ );
$r |= $s;
}else
{
logger(ERROR, _("no property : ") . $device[1] . bool2string($r), __FILE__ . ":" . __LINE__ );
}
}else
{
logger(ERROR, _("no device : ") . $device[0] . bool2string($r), __FILE__ . ":" . __LINE__ );
}
}
logger(DEBUG, _("testActionneurs : result is ") . bool2string($r), __FILE__ . ":" . __LINE__ );
return $r;
}
function htmlGetFriendlyNames($ieeeAddress = '')
{
$url = "HTTP://localhost:1025/friendlyname";
$ch = curl_init($url);
if ($ieeeAddress !== '')
{
$url .= "&ieeeAddress=" . $ieeeAddress;
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$fn = curl_exec($ch);
curl_close($ch);
if (($start = strpos($fn, "{")) === false)
{
if (($start = strpos($fn, "[")) === false)
{
print "Erreur : fn = " . $fn . __FILE__ . ":" . __LINE__ . EOLH;
return false;
}else
{
$end = strpos($fn, "]");
}
}else
{
$end = strpos($fn, "}");
}
$length = $end - $start + 1;
$fn = substr($fn, $start, $length);
return jsonDecode($fn);
}
function bool2string($var)
{
if ($var === false)
{
return "false";
}elseif($var === true)
{
return "true";
}elseif ($var === null)
{
return "null";
}else
{
return $var;
}
}
function jsonDecode($json)
{
//print "==========>" . print_r($json, true) . EOLH;
$jsonArray = array();
$json = trim($json, " {[]}");
$tmp = explode(",", $json);
foreach($tmp as $value)
{
$tmp2 = explode (":" , $value);
$jsonArray[trim($tmp2[0], '"')] = trim($tmp2[1], '"');
}
return $jsonArray;
}
function validateDate($date, $format = 'Y-m-d')
{
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) == $date;
}
function now()
{
$now = new datetime("now");
return $now;
}
function farenheit2celsius($value)
{
return round((((float)$value - 32) / 1.8),2);
}
function mph2kmh($value)
{
return round(((float)$value * 1.60934),2);
}
function millibars($value)
{
return round(((float)$value * 33.86),2);
}
function mktopic($device)
{
return $device->topic . "/" . $device->friendlyName;
}
function getDevice($topic, $fn)
{
global $topics, $devices;
if (array_key_exists($topic, $topics))
{
$var = $devices[$topic];
$path = explode("/", $fn);
foreach($path as $tmp)
{
if (array_key_exists($tmp, $var))
{
$var = $var[$tmp];
}else
{
////logger(ERROR, sprintf(_("level %s of %s do not exists in %s"), $tmp, $fn, $topic), __FILE__ . ":" . __LINE__ );
return false;
}
}
////logger(DEBUG, sprintf(_("returning a value")), __FILE__ . ":" . __LINE__ );
return $var["device"];
}else
{
////logger(ERROR, sprintf(_("Topic %s do not exists"), $topic), __FILE__ . ":" . __LINE__ );
return false;
}
}
function getValue($ieeeAddress, $property)
{
global $indexDevices;
if (array_key_exists($property, $indexDevices[$ieeeAddress]->properties))
{
$r = $indexDevices[$ieeeAddress]->properties[$property]["value"];
//logger(DEBUG, "device: " . $indexDevices[$ieeeAddress]->friendlyName . " value: " . $r, __FILE__ . ":" . __LINE__ );
}else
{
$r = false;
//logger(ERROR, "device: " . $indexDevices[$ieeeAddress]->friendlyName . "property " . $property . "does not exists", __FILE__ . ":" . __LINE__ );
}
return $r;
}
function setValue($fn, $property, $value)
{
global $indexDevices;
$indexDevices[$ieeeAddress]->properties[$property]["value"] = $value;
}
function aliases($fn, $property)
{
global $aliases;
logger(DEBUG, "Function aliases " . $fn . "/" . $property, __FILE__ . ":" . __LINE__);
if (array_key_exists($fn . "/" . $property, $aliases))
{
logger(DEBUG, "exists " . $fn . "/" . $property, __FILE__ . ":" . __LINE__);
return "/" . $aliases[$fn . "/" . $property];
}else
{
return "/" . _($property);
}
}
//logger(DEBUG, _("signal handling"), __FILE__ . ":" . __LINE__, false);
//signal handling
function signalHandler($signal)
{
endMoha();
}
pcntl_signal(SIGTERM, 'signalHandler');// Termination ('kill' was called)
pcntl_signal(SIGHUP, 'signalHandler'); // Terminal log-out
pcntl_signal(SIGINT, 'signalHandler');
?>