Files
Symcon_Belevo_Energiemanage…/Shelly_Parser_MQTT/module.php
2025-11-14 08:47:13 +01:00

248 lines
7.4 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)
{
// Wert lokal setzen
$this->SetValue($Ident, $Value);
// Wir erwarten Ident im Format: "<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 = $deviceID . ' Online';
$this->EnsureBooleanVariable($ident, $name);
$value = ($payload === 'true' || $payload === '1');
$this->SetValue($ident, $value);
if ($this->ReadPropertyBoolean('Debug')) {
IPS_LogMessage('Shelly_Parser_MQTT', sprintf(
'Online-Status %s -> %s',
$deviceID,
$value ? 'true' : 'false'
));
}
}
/* ---------------------------------------------------------
* RPC-EVENTS
* ---------------------------------------------------------*/
private function HandleRPC(string $deviceID, string $payload): void
{
$json = json_decode($payload, true);
if (!is_array($json)) {
return;
}
$src = $json['src'] ?? '';
if (!is_string($src) || !str_starts_with($src, 'shelly')) {
// kein Shelly-Gerät → ignorieren
return;
}
// Typ/Modell aus src extrahieren
$type = ShellyParser::ExtractType($src);
// Typ-Variable
$typeIdent = $deviceID . '_type';
$this->EnsureStringVariable($typeIdent, $deviceID . ' Typ');
$this->SetValue($typeIdent, $type);
// params parsen
$params = $json['params'] ?? [];
if (!is_array($params)) {
return;
}
$mapped = ShellyParser::MapParams($params);
// input
if (array_key_exists('input', $mapped)) {
$ident = $deviceID . '_input';
$this->EnsureBooleanVariable($ident, $deviceID . ' Input');
$this->SetValue($ident, (bool)$mapped['input']);
}
// output
if (array_key_exists('output', $mapped)) {
$ident = $deviceID . '_output';
$this->EnsureBooleanVariable($ident, $deviceID . ' Output');
$this->EnableAction($ident);
$this->SetValue($ident, (bool)$mapped['output']);
}
// temperature
if (array_key_exists('temperature', $mapped)) {
$ident = $deviceID . '_temperature';
$this->EnsureFloatVariable($ident, $deviceID . ' Temperatur');
$this->SetValue($ident, (float)$mapped['temperature']);
}
if ($this->ReadPropertyBoolean('Debug')) {
IPS_LogMessage('Shelly_Parser_MQTT', sprintf(
'RPC von %s (Typ %s) → %s',
$deviceID,
$type,
json_encode($mapped)
));
}
}
/* ---------------------------------------------------------
* Helper für Variablen
* ---------------------------------------------------------*/
private function EnsureBooleanVariable(string $ident, string $name): void
{
$id = @$this->GetIDForIdent($ident);
if ($id === false) {
$this->RegisterVariableBoolean($ident, $name);
}
}
private function EnsureFloatVariable(string $ident, string $name): void
{
$id = @$this->GetIDForIdent($ident);
if ($id === false) {
$this->RegisterVariableFloat($ident, $name);
}
}
private function EnsureStringVariable(string $ident, string $name): void
{
$id = @$this->GetIDForIdent($ident);
if ($id === false) {
$this->RegisterVariableString($ident, $name);
}
}
}