2021-12-30 16:18:32 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
function message($message)
|
|
|
|
{
|
2022-01-06 13:03:26 +01:00
|
|
|
global $topics, $logFh, $devices, $included;
|
2021-12-30 16:18:32 +01:00
|
|
|
$topic = explode ("/", $message->topic);
|
|
|
|
if ($topic[1] == "bridge")
|
|
|
|
{
|
|
|
|
switch ($topic[2])
|
|
|
|
{
|
|
|
|
case "info":
|
|
|
|
$topics[$topic[0]]->info = json_decode($message->payload);
|
|
|
|
break;
|
|
|
|
case "devices":
|
|
|
|
$topics[$topic[0]]->devices = json_decode($message->payload);
|
|
|
|
fwrite($logFh, print_r($topics[$topic[0]]->devices, true));
|
2021-12-31 14:34:20 +01:00
|
|
|
mkDevicesDB($topic[0], $topics[$topic[0]]->devices);
|
2021-12-30 16:18:32 +01:00
|
|
|
break;
|
|
|
|
case "groups":
|
|
|
|
$topics[$topic[0]]->groups = json_decode($message->payload);
|
2021-12-31 14:34:20 +01:00
|
|
|
mkDevicesDB($topic[0], $topics[$topic[0]]->groups, true);
|
2021-12-30 16:18:32 +01:00
|
|
|
break;
|
|
|
|
case "extensions":
|
|
|
|
$topics[$topic[0]]->extensions = json_decode($message->payload);
|
|
|
|
break;
|
|
|
|
case "config":
|
|
|
|
$topics[$topic[0]]->config = json_decode($message->payload);
|
|
|
|
break;
|
|
|
|
case "logging":
|
|
|
|
//TODO
|
|
|
|
break;
|
|
|
|
case "state":
|
|
|
|
$topics[$topic[0]]->state = $message->payload;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
};
|
2022-01-06 13:03:26 +01:00
|
|
|
}elseif (($topic[array_key_last($topic)]) != "get" and ($topic[array_key_last($topic)]) != "set" and $included)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2021-12-31 14:34:20 +01:00
|
|
|
$topic = explode ("/", $message->topic, 2); // get topic name
|
2022-01-02 12:14:30 +01:00
|
|
|
$fnTree = explode ("/" , $topic[1]); // get friendlyname
|
2021-12-30 16:18:32 +01:00
|
|
|
echo $topic[0] . " => " . $topic[1] . EOL;
|
2021-12-31 14:34:20 +01:00
|
|
|
//$devices[$topic[0]][$fnTree[0]]->json = json_decode($message->payload);
|
2022-01-02 12:14:30 +01:00
|
|
|
if ($fnTree[array_key_last($fnTree)] == "availability")
|
|
|
|
{
|
|
|
|
unset ($fnTree[array_key_last($fnTree)]);
|
|
|
|
$payloadArray = array("availability" => $message->payload);
|
2022-01-02 18:14:13 +01:00
|
|
|
//print_r($payloadArray);
|
2022-01-02 12:14:30 +01:00
|
|
|
}else
|
|
|
|
{
|
|
|
|
$payloadArray = json_decode($message->payload);
|
|
|
|
}
|
2021-12-31 14:34:20 +01:00
|
|
|
$device = & $devices[$topic[0]];
|
|
|
|
foreach($fnTree as $fn)
|
|
|
|
{
|
|
|
|
//print_r($device) ;
|
2022-01-03 21:11:52 +01:00
|
|
|
if (!isset($device[$fn])) //must not exists, but ...
|
2021-12-31 14:34:20 +01:00
|
|
|
{
|
2022-01-03 21:11:52 +01:00
|
|
|
logger(LOG_WARNING, $logFh, "init of " . $fn .EOL);
|
2021-12-31 14:34:20 +01:00
|
|
|
$device[$fn] = array();
|
|
|
|
$device[$fn]["device"] = new device;
|
2022-01-03 21:11:52 +01:00
|
|
|
//addDevice($device[$fn], $fn, );
|
2021-12-31 14:34:20 +01:00
|
|
|
}
|
|
|
|
$device = & $device[$fn];
|
|
|
|
}
|
2022-01-02 12:14:30 +01:00
|
|
|
changeDevice($topic[0], $topic[1], $device["device"], $payloadArray);
|
2021-12-30 16:18:32 +01:00
|
|
|
//fwrite($logFh, print_r($msg, true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// payload is an array :
|
2022-01-03 21:11:52 +01:00
|
|
|
// $key is parameter => $value is value of the parameter
|
2021-12-30 16:18:32 +01:00
|
|
|
|
2022-01-03 21:11:52 +01:00
|
|
|
function publish($topic, $payload, $commande="set", $eventKey)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
|
|
|
global $client, $mids, $logFh;
|
2021-12-31 14:34:20 +01:00
|
|
|
//print_r($payload);
|
2022-01-03 21:11:52 +01:00
|
|
|
$string = $topic . "/" . $commande;
|
2021-12-30 16:18:32 +01:00
|
|
|
$mid = $client->publish($string, json_encode($payload) , 2);
|
2021-12-31 14:34:20 +01:00
|
|
|
if (isset($mids[$mid]))
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2021-12-31 14:34:20 +01:00
|
|
|
//echo "unsetting mids" .EOL;
|
2021-12-30 16:18:32 +01:00
|
|
|
unset ($mids[$mid]);
|
|
|
|
}else
|
|
|
|
{
|
2021-12-31 14:34:20 +01:00
|
|
|
//echo "setting mids" .EOL;
|
2021-12-30 16:18:32 +01:00
|
|
|
$mids[$mid] = true;
|
|
|
|
}
|
2021-12-31 14:34:20 +01:00
|
|
|
//echo $string . " =>>>>>> " . json_encode($payload) . EOL;
|
2021-12-30 16:18:32 +01:00
|
|
|
logger(LOG_INFO, $logFh, "Publishing " . $string . " with payload => " . json_encode($payload));
|
|
|
|
}
|
|
|
|
|
|
|
|
function connectResponse($r, $message)
|
|
|
|
{
|
|
|
|
global $connected;
|
2022-01-03 21:11:52 +01:00
|
|
|
echo sprintf(_("I got code %d and message : '%s'"), $r, $message) . EOL;
|
2021-12-30 16:18:32 +01:00
|
|
|
switch ($r)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
echo _("Successfull connection") . EOL;
|
2022-01-01 07:35:50 +01:00
|
|
|
$connected = true;
|
2021-12-30 16:18:32 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
echo _("Connection refused : unacceptable protocol version") . EOL;
|
2022-01-01 07:35:50 +01:00
|
|
|
$connected = true;
|
2021-12-30 16:18:32 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
echo _("Connection refused : identifier rejected") . EOL;
|
2022-01-01 07:35:50 +01:00
|
|
|
$connected = true;
|
2021-12-30 16:18:32 +01:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
echo _("Connection refused (broker unavailable )") . EOL;
|
2022-01-01 07:35:50 +01:00
|
|
|
$connected = true;
|
2021-12-30 16:18:32 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function subscribeResponse($mid, $qosCount)
|
|
|
|
{
|
|
|
|
global $topics, $mids, $nSubscribed;
|
2022-01-06 13:03:26 +01:00
|
|
|
print_r($mids);
|
2021-12-30 16:18:32 +01:00
|
|
|
$key = $mids[$mid];
|
|
|
|
echo _("Subscribed to ") . $key . EOL;
|
|
|
|
$topics[$key]->status = true;
|
|
|
|
$nSubscribed += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function unsubscribeResponse($mid)
|
|
|
|
{
|
2022-01-01 07:35:50 +01:00
|
|
|
global $client; //$topics, $mids, $nSubscribed;
|
|
|
|
//$key = $mids[$mid];
|
|
|
|
echo _("Unsubscribed"); // from ") . $topics[$key]->name . EOL;
|
|
|
|
//$topics[$key]->status = false;
|
|
|
|
//$nSubscribed -= 1;
|
|
|
|
$client->disconnect();
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function disconnectResponse($r)
|
|
|
|
{
|
2022-01-01 07:35:50 +01:00
|
|
|
global $connected;
|
|
|
|
if ($r != 0)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
|
|
|
echo _('Badly ');
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
echo _('Cleanly ');
|
|
|
|
}
|
|
|
|
echo _("disconnected from server") . EOL;
|
2022-01-01 07:35:50 +01:00
|
|
|
$connected = false;
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function publishResponse($mid)
|
|
|
|
{
|
|
|
|
global $mids, $events;
|
|
|
|
logger(LOG_INFO, "Event with mid = " . $mid . " published by MQTT broker");
|
|
|
|
if (isset($mids[$mid]))
|
|
|
|
{
|
2021-12-31 14:34:20 +01:00
|
|
|
//echo "unsetting mids" . EOL;
|
2021-12-30 16:18:32 +01:00
|
|
|
unset ($mids[$mid]);
|
2022-01-06 13:03:26 +01:00
|
|
|
//print_r($mids);
|
2021-12-30 16:18:32 +01:00
|
|
|
}else
|
|
|
|
{
|
2021-12-31 14:34:20 +01:00
|
|
|
//echo "setting mids" . EOL;
|
2021-12-30 16:18:32 +01:00
|
|
|
$mids[$mid] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|