262 lines
7.5 KiB
PHP
262 lines
7.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/libs/ShellyParser.php';
|
|
|
|
class Shelly_Parser_MQTT extends IPSModule
|
|
{
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
|
|
// Verbindung zum MQTT-Server herstellen
|
|
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
|
|
|
// Auf alle Topics lauschen, Filter machen wir selbst
|
|
$this->Subscribe('#');
|
|
|
|
// Debug-Property
|
|
$this->RegisterPropertyBoolean('Debug', false);
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
|
|
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
|
$this->Subscribe('#');
|
|
}
|
|
|
|
/* ---------------------------------------------------------
|
|
* MQTT SUBSCRIBE
|
|
* ---------------------------------------------------------*/
|
|
private function Subscribe(string $topic): void
|
|
{
|
|
$packet = [
|
|
'PacketType' => 8,
|
|
'QualityOfService' => 0,
|
|
'Retain' => false,
|
|
'Topic' => $topic,
|
|
'Payload' => ''
|
|
];
|
|
|
|
$this->SendDataToParent(json_encode([
|
|
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
|
|
] + $packet));
|
|
}
|
|
|
|
/* ---------------------------------------------------------
|
|
* MQTT PUBLISH
|
|
* ---------------------------------------------------------*/
|
|
private function Publish(string $topic, string $payload): void
|
|
{
|
|
$packet = [
|
|
'PacketType' => 3,
|
|
'QualityOfService' => 0,
|
|
'Retain' => false,
|
|
'Topic' => $topic,
|
|
'Payload' => $payload
|
|
];
|
|
|
|
$this->SendDataToParent(json_encode([
|
|
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
|
|
] + $packet));
|
|
|
|
if ($this->ReadPropertyBoolean('Debug')) {
|
|
IPS_LogMessage('Shelly_Parser_MQTT', 'Publish: ' . $topic . ' -> ' . $payload);
|
|
}
|
|
}
|
|
|
|
/* ---------------------------------------------------------
|
|
* RequestAction → Schalten von Outputs
|
|
* ---------------------------------------------------------*/
|
|
public function RequestAction($Ident, $Value)
|
|
{
|
|
$this->SetValue($Ident, $Value);
|
|
|
|
// Output: <deviceID>_output
|
|
if (str_ends_with($Ident, '_output')) {
|
|
|
|
$deviceID = substr($Ident, 0, -strlen('_output'));
|
|
|
|
$topic = $deviceID . '/rpc/Switch.Set';
|
|
|
|
$payload = json_encode([
|
|
'id' => 0,
|
|
'on' => (bool)$Value
|
|
]);
|
|
|
|
$this->Publish($topic, $payload);
|
|
}
|
|
}
|
|
|
|
|
|
/* ---------------------------------------------------------
|
|
* RECEIVE MQTT DATA
|
|
* ---------------------------------------------------------*/
|
|
public function ReceiveData($JSONString)
|
|
{
|
|
if ($this->ReadPropertyBoolean('Debug')) {
|
|
IPS_LogMessage('Shelly_Parser_MQTT', 'ReceiveData: ' . $JSONString);
|
|
}
|
|
|
|
$data = json_decode($JSONString, true);
|
|
if (!is_array($data)) {
|
|
return;
|
|
}
|
|
|
|
$topic = $data['Topic'] ?? '';
|
|
$payload = $data['Payload'] ?? '';
|
|
|
|
if ($topic === '') {
|
|
return;
|
|
}
|
|
|
|
$parts = explode('/', $topic);
|
|
$deviceID = $parts[0] ?? '';
|
|
|
|
if ($deviceID === '') {
|
|
return;
|
|
}
|
|
|
|
// <GeräteID>/online
|
|
if ((isset($parts[1])) && ($parts[1] === 'online')) {
|
|
$this->HandleOnline($deviceID, $payload);
|
|
return;
|
|
}
|
|
|
|
// <GeräteID>/events/rpc
|
|
if ((isset($parts[1]) && $parts[1] === 'events') &&
|
|
(isset($parts[2]) && $parts[2] === 'rpc')) {
|
|
$this->HandleRPC($deviceID, $payload);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/* ---------------------------------------------------------
|
|
* ONLINE-STATUS
|
|
* ---------------------------------------------------------*/
|
|
private function HandleOnline(string $deviceID, string $payload): void
|
|
{
|
|
$ident = $deviceID . '_online';
|
|
$name = "Online";
|
|
|
|
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
|
|
$this->SetValue($ident, $payload === 'true' || $payload === '1');
|
|
}
|
|
|
|
|
|
/* ---------------------------------------------------------
|
|
* RPC-EVENTS
|
|
* ---------------------------------------------------------*/
|
|
private function HandleRPC(string $deviceID, string $payload): void
|
|
{
|
|
$json = json_decode($payload, true);
|
|
if (!is_array($json)) {
|
|
return;
|
|
}
|
|
|
|
$src = $json['src'] ?? '';
|
|
if (!str_starts_with($src, 'shelly')) {
|
|
return; // kein Shelly-Gerät
|
|
}
|
|
|
|
// Typ extrahieren
|
|
$type = ShellyParser::ExtractType($src);
|
|
$typeIdent = $deviceID . '_type';
|
|
$this->EnsureStringVariable($deviceID, $typeIdent, 'Typ');
|
|
$this->SetValue($typeIdent, $type);
|
|
|
|
$params = $json['params'] ?? [];
|
|
if (!is_array($params)) {
|
|
return;
|
|
}
|
|
|
|
$mapped = ShellyParser::MapParams($params);
|
|
|
|
// INPUT
|
|
if (array_key_exists('input', $mapped)) {
|
|
$ident = $deviceID . '_input';
|
|
$this->EnsureBooleanVariable($deviceID, $ident, 'Input');
|
|
$this->SetValue($ident, (bool)$mapped['input']);
|
|
}
|
|
|
|
// OUTPUT
|
|
if (array_key_exists('output', $mapped)) {
|
|
$ident = $deviceID . '_output';
|
|
$varID = $this->EnsureBooleanVariable($deviceID, $ident, 'Output');
|
|
$this->EnableAction($ident);
|
|
$this->SetValue($ident, (bool)$mapped['output']);
|
|
}
|
|
|
|
// TEMPERATUR
|
|
if (array_key_exists('temperature', $mapped)) {
|
|
$ident = $deviceID . '_temperature';
|
|
$this->EnsureFloatVariable($deviceID, $ident, 'Temperatur');
|
|
$this->SetValue($ident, (float)$mapped['temperature']);
|
|
}
|
|
}
|
|
|
|
|
|
/* ---------------------------------------------------------
|
|
* Helper für Variablen
|
|
* ---------------------------------------------------------*/
|
|
private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int
|
|
{
|
|
$folderID = $this->GetDeviceFolder($deviceID);
|
|
|
|
$varID = @$this->GetIDForIdent($ident);
|
|
if ($varID === false) {
|
|
$varID = $this->RegisterVariableBoolean($ident, $name);
|
|
IPS_SetParent($varID, $folderID);
|
|
}
|
|
return $varID;
|
|
}
|
|
|
|
private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int
|
|
{
|
|
$folderID = $this->GetDeviceFolder($deviceID);
|
|
|
|
$varID = @$this->GetIDForIdent($ident);
|
|
if ($varID === false) {
|
|
$varID = $this->RegisterVariableFloat($ident, $name);
|
|
IPS_SetParent($varID, $folderID);
|
|
}
|
|
return $varID;
|
|
}
|
|
|
|
private function EnsureStringVariable(string $deviceID, string $ident, string $name): int
|
|
{
|
|
$folderID = $this->GetDeviceFolder($deviceID);
|
|
|
|
$varID = @$this->GetIDForIdent($ident);
|
|
if ($varID === false) {
|
|
$varID = $this->RegisterVariableString($ident, $name);
|
|
IPS_SetParent($varID, $folderID);
|
|
}
|
|
return $varID;
|
|
}
|
|
|
|
private function GetDeviceFolder(string $deviceID): int
|
|
{
|
|
$folderIdent = 'folder_' . $deviceID;
|
|
|
|
$folderID = @$this->GetIDForIdent($folderIdent);
|
|
if ($folderID !== false) {
|
|
return $folderID;
|
|
}
|
|
|
|
// Ordner erzeugen
|
|
$folderID = IPS_CreateCategory();
|
|
IPS_SetParent($folderID, $this->InstanceID);
|
|
IPS_SetIdent($folderID, $folderIdent);
|
|
IPS_SetName($folderID, $deviceID);
|
|
|
|
return $folderID;
|
|
}
|
|
|
|
|
|
}
|
|
?>
|