60 lines
1.7 KiB
PHP
60 lines
1.7 KiB
PHP
<?php
|
|
$topics["pws2mqtt"] = new topic;
|
|
$devices["pws2mqtt"] = array();
|
|
|
|
|
|
$topics["pws2mqtt"]->callback = function($topic, $message)
|
|
{
|
|
global $logFh, $devices, $included;
|
|
$topicName = $topic[0];
|
|
$fn = $topic[1];
|
|
logger(INFO, sprintf(_("Incoming notification of device %s => friendly name : %s"), $topicName, $fn), __FILE__ . ":" . __LINE__);
|
|
$device = & $devices[$topicName];
|
|
$payloadArray = json_decode($message->payload, true);
|
|
// friendlyname
|
|
if (!array_key_exists($fn, $device))
|
|
{
|
|
echo "initializing $fn";
|
|
$device[$fn] = array("device" => new device);
|
|
$device = &$device[$fn];
|
|
$device["device"]->friendlyName = $payloadArray["friendly_name"];
|
|
$device["device"]->ieeeAddress = $payloadArray["ieeeAddress"];
|
|
$device["device"]->type = $payloadArray["type"];
|
|
$device["device"]->topic = $topicName;
|
|
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
|
|
$indexFriendlyNames[$topicName][$fn] = & $device["device"];
|
|
|
|
}else
|
|
{
|
|
$device = &$device[$fn];
|
|
}
|
|
unset($payloadArray["friendly_name"]);
|
|
unset($payloadArray["ieeeAddress"]);
|
|
unset($payloadArray["type"]);
|
|
|
|
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);
|
|
}
|
|
}
|
|
//$device = getDeviceByFriendlyname($topicName, $fn, $payloadArray, true);
|
|
if (!empty($payloadArray))
|
|
{
|
|
changeDevice($topicName, $fn, $device["device"], $payloadArray);
|
|
}
|
|
}
|
|
?>
|