2021-12-30 16:18:32 +01:00
|
|
|
<?php
|
2022-04-23 02:00:52 +02:00
|
|
|
////logger(DEBUG, _("Including utils.php"), __FILE__ . ":" . __LINE__);
|
2021-12-30 16:18:32 +01:00
|
|
|
|
2022-05-05 21:28:26 +02:00
|
|
|
/*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 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);
|
|
|
|
}
|
|
|
|
|
2022-01-19 00:22:34 +01:00
|
|
|
function bool2string($var)
|
|
|
|
{
|
|
|
|
if ($var === false)
|
|
|
|
{
|
|
|
|
return "false";
|
|
|
|
}elseif($var === true)
|
|
|
|
{
|
|
|
|
return "true";
|
2022-02-23 10:23:16 +01:00
|
|
|
}elseif ($var === null)
|
|
|
|
{
|
|
|
|
return "null";
|
2022-01-19 00:22:34 +01:00
|
|
|
}else
|
|
|
|
{
|
|
|
|
return $var;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 21:28:26 +02:00
|
|
|
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')
|
2022-03-28 00:40:34 +02:00
|
|
|
{
|
|
|
|
$d = DateTime::createFromFormat($format, $date);
|
|
|
|
return $d && $d->format($format) == $date;
|
|
|
|
}
|
|
|
|
|
2021-12-30 16:18:32 +01:00
|
|
|
function now()
|
|
|
|
{
|
2022-01-20 00:26:57 +01:00
|
|
|
$now = new datetime();
|
|
|
|
return $now;
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
2022-01-17 00:18:50 +01:00
|
|
|
function farenheit2celsius($value)
|
2022-01-02 13:47:41 +01:00
|
|
|
{
|
2022-01-27 18:41:16 +01:00
|
|
|
return round((((float)$value - 32) / 1.8),2);
|
|
|
|
}
|
|
|
|
|
|
|
|
function mph2kmh($value)
|
|
|
|
{
|
|
|
|
return round(((float)$value * 1.60934),2);
|
2022-01-17 00:18:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function millibars($value)
|
|
|
|
{
|
2022-01-27 18:41:16 +01:00
|
|
|
return round(((float)$value * 33.86),2);
|
2022-01-02 13:47:41 +01:00
|
|
|
}
|
|
|
|
|
2022-01-08 11:51:50 +01:00
|
|
|
function mktopic($device)
|
2022-01-02 13:47:41 +01:00
|
|
|
{
|
2022-01-08 11:51:50 +01:00
|
|
|
return $device->topic . "/" . $device->friendlyName;
|
2022-01-02 13:47:41 +01:00
|
|
|
}
|
2021-12-30 16:18:32 +01:00
|
|
|
|
2022-01-30 00:21:50 +01:00
|
|
|
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
|
|
|
|
{
|
2022-04-23 02:00:52 +02:00
|
|
|
////logger(ERROR, sprintf(_("level %s of %s do not exists in %s"), $tmp, $fn, $topic), __FILE__ . ":" . __LINE__ );
|
2022-01-30 00:21:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-04-23 02:00:52 +02:00
|
|
|
////logger(DEBUG, sprintf(_("returning a value")), __FILE__ . ":" . __LINE__ );
|
2022-02-02 21:18:44 +01:00
|
|
|
return $var["device"];
|
2022-01-30 00:21:50 +01:00
|
|
|
}else
|
|
|
|
{
|
2022-04-23 02:00:52 +02:00
|
|
|
////logger(ERROR, sprintf(_("Topic %s do not exists"), $topic), __FILE__ . ":" . __LINE__ );
|
2022-01-30 00:21:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-29 19:58:01 +01:00
|
|
|
function getValue($ieeeAddress, $property)
|
2022-01-28 23:05:58 +01:00
|
|
|
{
|
|
|
|
global $indexDevices;
|
2022-04-07 01:44:17 +02:00
|
|
|
if (array_key_exists($property, $indexDevices[$ieeeAddress]->properties))
|
|
|
|
{
|
|
|
|
$r = $indexDevices[$ieeeAddress]->properties[$property]["value"];
|
2022-04-23 02:00:52 +02:00
|
|
|
//logger(DEBUG, "device: " . $indexDevices[$ieeeAddress]->friendlyName . " value: " . $r, __FILE__ . ":" . __LINE__ );
|
2022-04-07 01:44:17 +02:00
|
|
|
}else
|
|
|
|
{
|
|
|
|
$r = false;
|
2022-04-23 02:00:52 +02:00
|
|
|
//logger(ERROR, "device: " . $indexDevices[$ieeeAddress]->friendlyName . "property " . $property . "does not exists", __FILE__ . ":" . __LINE__ );
|
2022-04-07 01:44:17 +02:00
|
|
|
}
|
2022-03-28 00:40:34 +02:00
|
|
|
return $r;
|
2022-01-28 23:05:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function setValue($fn, $property, $value)
|
|
|
|
{
|
|
|
|
global $indexDevices;
|
2022-02-23 10:23:16 +01:00
|
|
|
$indexDevices[$ieeeAddress]->properties[$property]["value"] = $value;
|
2022-01-28 23:05:58 +01:00
|
|
|
}
|
|
|
|
|
2022-04-23 02:00:52 +02:00
|
|
|
//logger(DEBUG, _("signal handling"), __FILE__ . ":" . __LINE__, false);
|
2022-01-08 11:51:50 +01:00
|
|
|
//signal handling
|
|
|
|
function signalHandler($signal)
|
2022-01-03 21:11:52 +01:00
|
|
|
{
|
2022-01-08 11:51:50 +01:00
|
|
|
endMoha();
|
2022-01-03 21:11:52 +01:00
|
|
|
}
|
2022-01-08 11:51:50 +01:00
|
|
|
pcntl_signal(SIGTERM, 'signalHandler');// Termination ('kill' was called)
|
|
|
|
pcntl_signal(SIGHUP, 'signalHandler'); // Terminal log-out
|
|
|
|
pcntl_signal(SIGINT, 'signalHandler');
|
2021-12-30 16:18:32 +01:00
|
|
|
?>
|