1
0
moha/topics_callbacks/pws2mqtt.php

44 lines
1.3 KiB
PHP
Raw Normal View History

<?php
2022-01-08 23:45:38 +01:00
$topics["pws2mqtt"] = new topic;
2022-01-08 23:45:38 +01:00
$topics["pws2mqtt"]->callback = function($topic, $message)
{
global $topics, $logFh, $devices, $included;
2022-01-17 00:18:50 +01:00
$fn = $topic[1]; // get friendlyname
2022-01-17 21:01:11 +01:00
logger(INFO, sprintf(_("Incoming notification of device %s => friendly name : %s"), $topic[0], $topic[1]));
2022-01-08 23:45:38 +01:00
$device = & $devices[$topic[0]];
2022-01-17 00:18:50 +01:00
$payloadArray = json_decode($message->payload);
2022-01-20 00:26:57 +01:00
if (!isset($device[$fn]))
2022-01-08 23:45:38 +01:00
{
logger(WARNING, $logFh, "init of " . $fn .EOL);
2022-01-17 00:18:50 +01:00
$device[$fn] = array();
$device[$fn]["device"] = new device;
$device[$fn]["device"]->type = $payloadArray->type;
$device[$fn]["device"]->ieeeAddress = $payloadArray->ieeeAddress;
2022-01-20 00:26:57 +01:00
$device[$fn]["device"]->friendlyname = $fn;
2022-01-17 21:01:11 +01:00
$indexDevices[$device[$fn]["device"]->ieeeAddress] = & $device[$fn]["device"];
2022-01-08 23:45:38 +01:00
}
2022-01-17 00:18:50 +01:00
$device = & $device[$fn];
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);
}
}
2022-01-20 00:26:57 +01:00
changeDevice($topic[0], $fn, $device["device"], $payloadArray);
}
?>