1
0
moha/topics_callbacks/pws2mqtt.php

71 lines
2.0 KiB
PHP

<?php
if (!array_key_exists("pws2mqtt", $topics)) $topics["pws2mqtt"] = new topic;
if (!array_key_exists("pws2mqtt", $devices)) $devices["pws2mqtt"] = array();
function pws2mqttCallback($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"];
mkIndexes();
}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);
}
};
$topics["pws2mqtt"]->callback = "pws2mqttCallback";
if (!is_callable("pws2mqttGetList"))
{
function pws2mqttGetList()
{
publish("pws2mqtt", array("",""), "get" );
}
}
pws2mqttGetList();
?>