2022-01-08 11:51:50 +01:00
|
|
|
<?php
|
2022-03-13 22:33:26 +01:00
|
|
|
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
|
|
|
|
2022-03-13 22:33:26 +01:00
|
|
|
function pws2mqttCallback($topic, $message)
|
2022-01-08 11:51:50 +01:00
|
|
|
{
|
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;
|
2022-03-13 22:33:26 +01:00
|
|
|
//$indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
|
|
|
|
//$indexFriendlyNames[$topicName][$fn] = & $device["device"];
|
|
|
|
mkIndexes();
|
|
|
|
|
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"]);
|
|
|
|
|
2022-01-27 18:41:16 +01:00
|
|
|
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);
|
2022-01-27 18:41:16 +01:00
|
|
|
}elseif ($str == "h")
|
|
|
|
{
|
|
|
|
$newProperty = substr($property, 0, -3) . "kmh";
|
2022-02-23 10:23:16 +01:00
|
|
|
$payloadArray[$newProperty] = mph2kmh($value);
|
2022-01-27 18:41:16 +01:00
|
|
|
}
|
|
|
|
if ($property == "baromin")
|
|
|
|
{
|
|
|
|
$newProperty = "barominmb";
|
2022-02-23 10:23:16 +01:00
|
|
|
$payloadArray["barominmb"] = millibars($value);
|
2022-01-27 18:41:16 +01:00
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|
2022-03-13 22:33:26 +01:00
|
|
|
};
|
|
|
|
$topics["pws2mqtt"]->callback = "pws2mqttCallback";
|
|
|
|
|
|
|
|
if (!is_callable("pws2mqttGetList"))
|
|
|
|
{
|
|
|
|
function pws2mqttGetList()
|
|
|
|
{
|
|
|
|
publish("pws2mqtt", array("",""), "get" );
|
|
|
|
}
|
2022-01-08 11:51:50 +01:00
|
|
|
}
|
2022-03-13 22:33:26 +01:00
|
|
|
|
|
|
|
pws2mqttGetList();
|
2022-01-08 11:51:50 +01:00
|
|
|
?>
|