1
0
moha/topics_callbacks/pws2mqtt.php

77 lines
2.2 KiB
PHP
Raw Normal View History

<?php
if (!array_key_exists("pws2mqtt", $topics)) $topics["pws2mqtt"] = new topic;
if (!array_key_exists("pws2mqtt", $devices)) $devices["pws2mqtt"] = array();
2022-02-23 10:23:16 +01:00
function pws2mqttCallback($topic, $message)
{
global $logFh, $devices, $included, $topics;
2022-09-05 13:47:41 +02:00
2022-02-23 10:23:16 +01:00
$topicName = $topic[0];
2022-09-05 13:47:41 +02:00
$topics[$topicName]->lastSeen = time();
2022-02-23 10:23:16 +01:00
$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
2022-04-23 02:00:52 +02:00
if ( $fn != "get")
2022-02-23 10:23:16 +01:00
{
2022-04-23 02:00:52 +02:00
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();
2022-02-23 10:23:16 +01:00
2022-04-23 02:00:52 +02:00
}else
{
2022-04-23 02:00:52 +02:00
$device = &$device[$fn];
}
unset($payloadArray["friendly_name"]);
unset($payloadArray["ieeeAddress"]);
unset($payloadArray["type"]);
foreach ($payloadArray as $property => $value)
{
2022-04-23 02:00:52 +02:00
$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);
}
}
2022-04-23 02:00:52 +02:00
//$device = getDeviceByFriendlyname($topicName, $fn, $payloadArray, true);
if (!empty($payloadArray))
{
2022-04-23 02:00:52 +02:00
changeDevice($topicName, $fn, $device["device"], $payloadArray);
}
}
};
$topics["pws2mqtt"]->callback = "pws2mqttCallback";
$topics["pws2mqtt"]->timeOut = 3; //timeOut in minutes
$topics["pws2mqtt"]->lastSeen = time();
if (!is_callable("pws2mqttGetList"))
{
function pws2mqttGetList()
{
publish("pws2mqtt", array("",""), "get" );
}
}
?>