44 lines
1.3 KiB
PHP
44 lines
1.3 KiB
PHP
<?php
|
|
$topics["pws2mqtt"] = new topic;
|
|
|
|
$topics["pws2mqtt"]->callback = function($topic, $message)
|
|
{
|
|
global $topics, $logFh, $devices, $included;
|
|
$fn = $topic[1]; // get friendlyname
|
|
logger(INFO, sprintf(_("Incoming notification of device %s => friendly name : %s"), $topic[0], $topic[1]));
|
|
$device = & $devices[$topic[0]];
|
|
$payloadArray = json_decode($message->payload);
|
|
|
|
if (!isset($device[$fn]))
|
|
{
|
|
logger(WARNING, $logFh, "init of " . $fn .EOL);
|
|
$device[$fn] = array();
|
|
$device[$fn]["device"] = new device;
|
|
$device[$fn]["device"]->type = $payloadArray->type;
|
|
$device[$fn]["device"]->ieeeAddress = $payloadArray->ieeeAddress;
|
|
$device[$fn]["device"]->friendlyname = $fn;
|
|
$indexDevices[$device[$fn]["device"]->ieeeAddress] = & $device[$fn]["device"];
|
|
}
|
|
$device = & $device[$fn];
|
|
foreach ($payloadArray as $property => $value)
|
|
{
|
|
$str = substr($property, -1);
|
|
if ($str == "f")
|
|
{
|
|
$newProperty = rtrim($property, "f") . "c";
|
|
$payloadArray->{$newProperty} = farenheit2celsius($value);
|
|
}elseif ($str == "h")
|
|
{
|
|
$newProperty = substr($property, 0, -3) . "kmh";
|
|
$payloadArray->{$newProperty} = mph2kmh($value);
|
|
}
|
|
if ($property == "baromin")
|
|
{
|
|
$newProperty = "barominmb";
|
|
$payloadArray->{"barominmb"} = millibars($value);
|
|
}
|
|
}
|
|
changeDevice($topic[0], $fn, $device["device"], $payloadArray);
|
|
}
|
|
?>
|