112 lines
2.4 KiB
PHP
112 lines
2.4 KiB
PHP
<?php
|
|
////logger(DEBUG, _("Including utils.php"), __FILE__ . ":" . __LINE__);
|
|
|
|
function bool2string($var)
|
|
{
|
|
if ($var === false)
|
|
{
|
|
return "false";
|
|
}elseif($var === true)
|
|
{
|
|
return "true";
|
|
}elseif ($var === null)
|
|
{
|
|
return "null";
|
|
}else
|
|
{
|
|
return $var;
|
|
}
|
|
}
|
|
|
|
function validateDate($date, $format = 'Y-m-d H:i:s')
|
|
{
|
|
$d = DateTime::createFromFormat($format, $date);
|
|
return $d && $d->format($format) == $date;
|
|
}
|
|
|
|
function now()
|
|
{
|
|
$now = new datetime();
|
|
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;
|
|
}
|
|
|
|
//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');
|
|
?>
|