1
0
moha/topics_callbacks/pws2mqtt.php

60 lines
1.7 KiB
PHP
Raw Normal View History

<?php
2022-01-08 23:45:38 +01:00
$topics["pws2mqtt"] = new topic;
2022-02-23 10:23:16 +01:00
$devices["pws2mqtt"] = array();
2022-01-08 23:45:38 +01:00
$topics["pws2mqtt"]->callback = function($topic, $message)
{
2022-02-23 10:23:16 +01:00
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"];
2022-02-23 10:23:16 +01:00
}else
2022-01-08 23:45:38 +01:00
{
2022-02-23 10:23:16 +01:00
$device = &$device[$fn];
2022-01-08 23:45:38 +01:00
}
2022-02-23 10:23:16 +01:00
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";
2022-02-23 10:23:16 +01:00
$payloadArray[$newProperty] = farenheit2celsius($value);
}elseif ($str == "h")
{
$newProperty = substr($property, 0, -3) . "kmh";
2022-02-23 10:23:16 +01:00
$payloadArray[$newProperty] = mph2kmh($value);
}
if ($property == "baromin")
{
$newProperty = "barominmb";
2022-02-23 10:23:16 +01:00
$payloadArray["barominmb"] = millibars($value);
}
}
2022-02-23 10:23:16 +01:00
//$device = getDeviceByFriendlyname($topicName, $fn, $payloadArray, true);
2022-03-04 22:30:16 +01:00
if (!empty($payloadArray))
{
changeDevice($topicName, $fn, $device["device"], $payloadArray);
}
}
?>