1
0
moha/topics_callbacks/linky2mqtt.php

43 lines
1.5 KiB
PHP

<?php
//TODO to test and debug
if (!array_key_exists("linky2mqtt", $topics)) $topics["linky2mqtt"] = new topic;
if (!array_key_exists("linky2mqtt", $devices)) $devices["linky2mqtt"] = array();
function linky2mqttCallback($topic, $message)
{
global $topics, $logFh, $devices, $included;
$topicName = $topic[0];
$topics[$topicName]->lastSeen = time();
$friendlyName = $topic[1]; // get friendlyName
logger(INFO, sprintf(_("Incoming notification of device %s"), $topicName, $fn), __FILE__ . ":" . __LINE__);
$payloadArray = json_decode($message->payload, true);
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"];
//$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"]);
//$device = getDeviceByFriendlyname($topicName, $fn, $payloadArray, true);
changeDevice($topicName, $friendlyName, $device["device"], $payloadArray);
//print_r($device["device"]);
}
$topics["linky2mqtt"]->callback = "linky2mqttCallback";
$topics["linky2mqtt"]->timeOut = 3; // timeOut in minutes
$topics["linky2mqtt"]->lastSeen = time();
?>