1
0

making compatibility with mqtt's friendly names

This commit is contained in:
daniel Tartavel 2021-12-31 14:34:20 +01:00
parent 5162731a16
commit 57390d099b
5 changed files with 151 additions and 61 deletions

View File

@ -8,37 +8,63 @@ function storeDB($db, $filepath)
fclose($fp); fclose($fp);
} }
function mkDevicesDB($topic, $json) function deviceTree(string $topic, array $fnTree, array &$device)
{
}
function mkDevicesDB($topic, $json, $group=false)
{ {
global $devices, $listProperties, $indexDevices, $dbInit, $logFh; global $devices, $listProperties, $indexDevices, $dbInit, $logFh;
foreach ($json as $device) if (!isset($devices[$topic])) $devices[$topic]= array();
foreach ($json as $jsonDevice)
{ {
//print_r($device); //print_r($device);
$fn = $device->friendly_name; $fn = $jsonDevice->friendly_name;
$devices[$topic][$fn] = new device; $fnTree = explode("/", $fn);
$devices[$topic][$fn]->topic = $topic; $device = & $devices[$topic];
$devices[$topic][$fn]->device = $device; foreach($fnTree as $fnPart)
$devices[$topic][$fn]->type = $device->type;
$devices[$topic][$fn]->ieeeAddress = $device->ieee_address;
$devices[$topic][$fn]->friendlyName = $device->friendly_name;
if ( !empty($devices[$topic][$fn]->powerSource ) )
{ {
$devices[$topic][$fn]->powerSource = $device->power_source; $device[$fnPart] = array();
$device = & $device[$fnPart];
} }
if ($device->definition != null)
$device["device"] = new device;
$device["device"]->topic = $topic;
//$device["device"]->device = $jsonDevice;
$device["device"]->friendlyName = $jsonDevice->friendly_name;
if ($group)
{ {
$devices[$topic][$fn]->description = $device->definition->description; //print_r($device);
$device["device"]->groupID = $jsonDevice->id;
$indexDevices[$device["device"]->groupID] = $jsonDevice->friendly_name;
}else
{
$device["device"]->type = $jsonDevice->type;
$device["device"]->ieeeAddress = $jsonDevice->ieee_address;
if ( !empty($jsonDevice->power_source ) )
{
$device["device"]->powerSource = $jsonDevice->power_source;
}
if ($jsonDevice->definition != null)
{
$device["device"]->description = $jsonDevice->definition->description;
}
//searchProperty($fn, $device["device"], $jsonDevice, $listProperties);
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
//print_r($device);
} }
searchProperty($fn, $devices[$topic][$fn], $device, $listProperties);
$indexDevices[$device->ieee_address] = $device->friendly_name;
} }
$dbInit = true; $dbInit = true;
fwrite($logFh, "################################START##################################################");
fwrite($logFh, print_r($devices, true)); fwrite($logFh, print_r($devices, true));
fwrite($logFh, "################################END##################################################");
echo "Devices DB made" .EOL; echo "Devices DB made" .EOL;
//print_r($devices); //print_r($devices);
} }
function searchProperty($fn, $device, $object, $listProperties) function searchProperty($fn, &$device, $object, $listProperties)
{ {
$objectArray = (array)$object; $objectArray = (array)$object;
foreach($listProperties as $key => $value) foreach($listProperties as $key => $value)
@ -52,17 +78,43 @@ function searchProperty($fn, $device, $object, $listProperties)
} }
} }
function changeDevice($topic, $fn, $device) function changeDevice($topic, $fn, &$device, $json)
{ {
//print_r($device); //print_r($device);
iterateDevice($topic, $fn, $device, $device->json); /*foreach($fnTree as $fn)
{
//print_r($device);
if (!isset($device[$fn]))
{
}
$device = & $device[$fn];
}*/
//print_r($json);
$fnTree = explode("/", $fn);
print_r($json);
if ( ($jsonDevice = json_decode($json)) === null )
{
echo "json ========>>>>>>> " . print_r($json,true) . EOL;
if (!isset($device->value))
{
$device->{"value"} = $json;
$device->type = true; //set this as parameter and not device
}
print_r($device);
}else
{
iterateDevice($topic, $fn, $device, $jsonDevice);
}
} }
function iterateDevice($topic, $fn, $device, $object) function iterateDevice($topic, $fn, &$device, $json)
{ {
global $changed; global $changed;
//echo "device =>";print_r($device);echo EOL; //echo "device =>";print_r($device);echo EOL;
foreach($object as $key => $value) foreach($json as $key => $value)
{ {
$oldValue = 0; $oldValue = 0;
//echo "key =>"; print_r($key); echo EOL; //echo "key =>"; print_r($key); echo EOL;
@ -95,14 +147,14 @@ function iterateDevice($topic, $fn, $device, $object)
$changed[$fn]["value"] = $value; $changed[$fn]["value"] = $value;
//echo "oldvalue => " . print_r($oldValue, true) . EOL; //echo "oldvalue => " . print_r($oldValue, true) . EOL;
if (empty($oldValue)) /*if (empty($oldValue))
{ {
echo "Initializing " . $key; echo "Initializing " . $key;
}else }else
{ {
echo "changed " . $key . " value " . $oldValue;; echo "changed " . $key . " value " . $oldValue;;
} }
echo " to " . $value . EOL; echo " to " . $value . EOL;*/
} }
//print_r($device->functions); //print_r($device->functions);
if (!empty($device->functions)) if (!empty($device->functions))

View File

@ -3,15 +3,16 @@
function checkEvents() function checkEvents()
{ {
global $events, $indexDevices, $devices; global $events, $indexDevices, $devices;
echo "===========> checking events" . EOL; //echo "===========> checking events" . EOL;
foreach ($events as $key => $event) foreach ($events as $key => $event)
{ {
if ($event->dateTimeEvent <= now()) if (!empty($event->dateTimeEvent) and $event->dateTimeEvent <= now())
{ {
echo "---->sending command" . EOL; //echo "---->sending command" . EOL;
publish($devices[$event->topic][$indexDevices[$event->ieeeAddress]], array($event->param => $event->value), "set", $key); publish($devices[$event->topic][$indexDevices[$event->ieeeAddress]], array($event->param => $event->value), "set", $key);
//$event->published = now(); //$event->published = now();
unset($key); //echo "#################################\nUnsetting event $key \n###########################" . EOL;
unset($events[$key]);
} }
} }
//print_r($events); //print_r($events);
@ -69,15 +70,15 @@ function searchEvent($device, $param , $value)
global $events; global $events;
echo "searching event" . EOL; echo "searching event" . EOL;
//$keys = array_keys($events, $device->ieeeAddress); //$keys = array_keys($events, $device->ieeeAddress);
echo "ieee_address =>" . $device->ieeeAddress . EOL; //echo "ieee_address =>" . $device->ieeeAddress . EOL;
print_r($events); //print_r($events);
foreach($events as $key => $event) foreach($events as $key => $event)
{ {
//echo "Event : $event => $value" . EOL; //echo "Event : $event => $value" . EOL;
echo "===>";print_r($event); echo EOL; //echo "===>";print_r($event); echo EOL;
if($event->topic == $device->topic and $event->param == $param and $event->value == $value and $event->ieeeAddress == $device->ieeeAddress) if($event->topic == $device->topic and $event->param == $param and $event->value == $value and $event->ieeeAddress == $device->ieeeAddress)
{ {
echo "==============================\nfound " . $key . "\n=================================" . EOL; //echo "==============================\nfound " . $key . "\n=================================" . EOL;
return $key; return $key;
} }
} }
@ -87,7 +88,7 @@ function deleteEvent($eventKey)
{ {
global $events; global $events;
unset ($events[$eventKey]); unset ($events[$eventKey]);
echo "delete event " . $eventKey . EOL; //echo "delete event " . $eventKey . EOL;
} }

View File

@ -35,12 +35,13 @@ class topic {
class device class device
{ {
public $method; //auto or manual
public $topic; public $topic;
public $device; public $device;
public $json;
public $ieeeAddress; public $ieeeAddress;
public $groupID;
public $friendlyName; public $friendlyName;
public $type; public $type; // if true then not a device but parameter of device(eg. topic/FRIENDLYNAME/PARAMETER
public $powerSource; public $powerSource;
public $description; public $description;
public $functions; public $functions;
@ -50,6 +51,11 @@ class device
{ {
publish($this, $this->payload, "set", $event); publish($this, $this->payload, "set", $event);
} }
public function get()
{
publish($this, $this->payload, "get", $event);
}
} }
class event class event
@ -136,15 +142,14 @@ $topics["zigbee2mqtt"] = new topic;
bindtextdomain("moha", "./locale"); bindtextdomain("moha", "./locale");
textdomain("moha"); textdomain("moha");
//signal handling if (!init()) exit(1);
pcntl_signal(SIGTERM, 'signalHandler');// Termination ('kill' was called)
pcntl_signal(SIGHUP, 'signalHandler'); // Terminal log-out
pcntl_signal(SIGINT, 'signalHandler');
require "mqtt_functions.php";
require "utils.php";
require "events.php";
require "db_functions.php";
$client = new Mosquitto\Client(); $client = new Mosquitto\Client();
if (!init()) exit(1);
$client->onConnect('connectResponse'); $client->onConnect('connectResponse');
$client->onDisconnect('disconnectResponse'); $client->onDisconnect('disconnectResponse');
$client->onSubscribe('subscribeResponse'); $client->onSubscribe('subscribeResponse');
@ -152,8 +157,16 @@ $client->onMessage('message');
$client->onLog('logger'); $client->onLog('logger');
$client->onPublish('publishResponse'); $client->onPublish('publishResponse');
$include 'mqtt_functions.php';
//signal handling
pcntl_signal(SIGTERM, 'signalHandler');// Termination ('kill' was called)
pcntl_signal(SIGHUP, 'signalHandler'); // Terminal log-out
pcntl_signal(SIGINT, 'signalHandler');
// Program start
$client->connect("192.168.1.253", 1883, 5);
foreach($topics as $name => $topic) foreach($topics as $name => $topic)
{ {
//echo $name; //echo $name;
@ -178,6 +191,10 @@ while (true)
}elseif($dbInit and $included) }elseif($dbInit and $included)
{ {
checkEvents(); checkEvents();
if (empty($flag))
{
$flag = true;
}
} }
} }
@ -187,7 +204,7 @@ function init()
{ {
global $logFh, $client; global $logFh, $client;
date_default_timezone_set('Europe/Paris'); date_default_timezone_set('Europe/Paris');
$client->connect("192.168.1.253", 1883, 5);
if (! $logFh = fopen("moha.log", "w") ) if (! $logFh = fopen("moha.log", "w") )
{ {
echo _("error opening log file"); echo _("error opening log file");
@ -211,7 +228,7 @@ function loadHooks($dir)
if (is_dir($file)) if (is_dir($file))
{ {
//echo "directory" . EOL; //echo "directory" . EOL;
listHooks($dir . '/' . $file); loadHooks($dir . '/' . $file);
}else }else
{ {
//echo "file" . EOL; //echo "file" . EOL;
@ -234,14 +251,15 @@ function endMoha()
{ {
if ($object->status) if ($object->status)
{ {
$client->unsubscribe($topic); $mid = $client->unsubscribe($topic);
$mids[$mid] = $topic;
} }
} }
while ($nSubscribed != 0) while ($nSubscribed != 0)
{ {
//echo $nSubscribed;0x00124b0022ebac5c //echo $nSubscribed;0x00124b0022ebac5c
if ( $x++ > 30) exit (0); if ( $x++ > 60) exit (0);
$client->loop(); $client->loop();
} }
$client->disconnect(); $client->disconnect();

View File

@ -13,12 +13,12 @@ function message($message)
break; break;
case "devices": case "devices":
$topics[$topic[0]]->devices = json_decode($message->payload); $topics[$topic[0]]->devices = json_decode($message->payload);
mkDevicesDB($topic[0], $topics[$topic[0]]->devices);
fwrite($logFh, print_r($topics[$topic[0]]->devices, true)); fwrite($logFh, print_r($topics[$topic[0]]->devices, true));
mkDevicesDB($topic[0], $topics[$topic[0]]->devices);
break; break;
case "groups": case "groups":
$topics[$topic[0]]->groups = json_decode($message->payload); $topics[$topic[0]]->groups = json_decode($message->payload);
mkDevicesDB($topic[0], $topics[$topic[0]]->groups); mkDevicesDB($topic[0], $topics[$topic[0]]->groups, true);
break; break;
case "extensions": case "extensions":
$topics[$topic[0]]->extensions = json_decode($message->payload); $topics[$topic[0]]->extensions = json_decode($message->payload);
@ -37,11 +37,23 @@ function message($message)
}; };
}elseif (($topic[array_key_last($topic)]) != "get" and ($topic[array_key_last($topic)]) != "set") }elseif (($topic[array_key_last($topic)]) != "get" and ($topic[array_key_last($topic)]) != "set")
{ {
$topic = explode ("/", $message->topic, 2); $topic = explode ("/", $message->topic, 2); // get topic name
$fnTree = explode ("/" , $topic[1]); // get friendlyname
echo $topic[0] . " => " . $topic[1] . EOL; echo $topic[0] . " => " . $topic[1] . EOL;
$devices[$topic[0]][$topic[1]]->json = json_decode($message->payload); //$devices[$topic[0]][$fnTree[0]]->json = json_decode($message->payload);
changeDevice($topic[0], $topic[1], $devices[$topic[0]][$topic[1]]); $device = & $devices[$topic[0]];
foreach($fnTree as $fn)
{
//print_r($device) ;
if (!isset($device[$fn]))
{
logger(LOG_INFO, $logFh, "init of " . $fn .EOL);
$device[$fn] = array();
$device[$fn]["device"] = new device;
}
$device = & $device[$fn];
}
changeDevice($topic[0], $topic[1], $device["device"], $message->payload);
//fwrite($logFh, print_r($msg, true)); //fwrite($logFh, print_r($msg, true));
} }
} }
@ -52,17 +64,19 @@ function message($message)
function publish($device, $payload, $commande="set", $eventKey) function publish($device, $payload, $commande="set", $eventKey)
{ {
global $client, $mids, $logFh; global $client, $mids, $logFh;
print_r($payload); //print_r($payload);
$string = $device->topic . "/" . $device->friendlyName . "/" . $commande; $string = $device->topic . "/" . $device->friendlyName . "/" . $commande;
$mid = $client->publish($string, json_encode($payload) , 2); $mid = $client->publish($string, json_encode($payload) , 2);
if ($mids[$mid]) if (isset($mids[$mid]))
{ {
//echo "unsetting mids" .EOL;
unset ($mids[$mid]); unset ($mids[$mid]);
}else }else
{ {
//echo "setting mids" .EOL;
$mids[$mid] = true; $mids[$mid] = true;
} }
echo $string . " =>>>>>> " . json_encode($payload) . EOL; //echo $string . " =>>>>>> " . json_encode($payload) . EOL;
logger(LOG_INFO, $logFh, "Publishing " . $string . " with payload => " . json_encode($payload)); logger(LOG_INFO, $logFh, "Publishing " . $string . " with payload => " . json_encode($payload));
} }
@ -127,9 +141,11 @@ function publishResponse($mid)
logger(LOG_INFO, "Event with mid = " . $mid . " published by MQTT broker"); logger(LOG_INFO, "Event with mid = " . $mid . " published by MQTT broker");
if (isset($mids[$mid])) if (isset($mids[$mid]))
{ {
//echo "unsetting mids" . EOL;
unset ($mids[$mid]); unset ($mids[$mid]);
}else }else
{ {
//echo "setting mids" . EOL;
$mids[$mid] = true; $mids[$mid] = true;
} }
} }

View File

@ -5,8 +5,8 @@
$rdcPanneau = function($topic, $fn, $param, $value) $rdcPanneau = function($topic, $fn, $param, $value)
{ {
global $devices, $indexDevices; global $devices, $indexDevices;
$delay = 20; $delay = 3;
$timeUnit = "sec"; $timeUnit = "min";
switch($param) switch($param)
{ {
case "occupancy": case "occupancy":
@ -14,16 +14,19 @@
{ {
$msg = array("state" => "ON"); $msg = array("state" => "ON");
$device = $devices[Z2M][$indexDevices["0x588e81fffe343e8f"]]; $device = $devices[Z2M][$indexDevices["0x588e81fffe343e8f"]];
$device->set($msg); $device->payload = $msg;
setDelay($device, $delay, $timeUnit, $param, "OFF", true); $device->set(null);
setDelay($device, $delay, $timeUnit, "state", "OFF", true);
} }
break; break;
} }
echo _("notification received from MQTT") . EOL; echo _("notification received from MQTT") . EOL;
echo $param . "=> " . $value . EOL; //echo $param . "=> " . $value . EOL;
}; };
//assignation of the function to the devices //assignation of the function to the sensors devices
$devices[Z2M][$indexDevices["0x00124b0022ebac5c"]]->functions[] = $rdcPanneau; $indexDevices["0x00124b0022ebac5c"]->functions[] = $rdcPanneau;
$devices[Z2M][$indexDevices["0x588e81fffe2cf695"]]->functions[] = $rdcPanneau; $indexDevices["0x588e81fffe2cf695"]->functions[] = $rdcPanneau;
$indexDevices["0x04cf8cdf3c78aff0"]->functions[] = $rdcPanneau;
?> ?>