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

335 lines
10 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/libs/ShellyParser.php';
class Shelly_Parser_MQTT extends IPSModule
{
public function Create()
{
parent::Create();
// MQTT-Server verbinden
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
// Auf alle Topics hören
$this->Subscribe('#');
$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));
$this->SendDebug("Publish", "$topic$payload", 0);
}
/* ---------------------------------------------------------
* REQUEST ACTION (Shelly schalten)
* ---------------------------------------------------------*/
public function RequestAction($Ident, $Value)
{
$this->SendDebug('RequestAction', "$Ident" . json_encode($Value), 0);
if (!str_contains($Ident, '_output_')) {
throw new Exception("Unknown Ident: " . $Ident);
}
// lokale Variable setzen
$varID = $this->FindVariableByIdent($Ident);
if ($varID) {
SetValue($varID, $Value);
}
// device + index extrahieren
[$deviceID, $suffix] = explode('_output_', $Ident, 2);
$index = intval($suffix);
// RPC Topic
$topic = $deviceID . '/rpc';
// RPC JSON Payload
$payload = json_encode([
'id' => 1,
'src' => 'ips',
'method' => 'Switch.Set',
'params' => [
'id' => $index,
'on' => (bool)$Value
]
]);
$this->SendDebug('MQTT SEND', "$topic : $payload", 0);
// absenden
$this->Publish($topic, $payload);
}
/* ---------------------------------------------------------
* RECEIVE MQTT DATA
* ---------------------------------------------------------*/
public function ReceiveData($JSONString)
{
$this->SendDebug('ReceiveData', $JSONString, 0);
$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;
}
// <deviceID>/online
if ((isset($parts[1])) && ($parts[1] === 'online')) {
$this->HandleOnline($deviceID, $payload);
return;
}
// <deviceID>/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
{
$value = ($payload === 'true' || $payload === '1');
$varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_online', 'Online');
SetValue($varID, $value);
}
/* ---------------------------------------------------------
* RPC-EVENTS VERARBEITEN
* ---------------------------------------------------------*/
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;
}
// Gerätetyp merken
$type = ShellyParser::ExtractType($src);
$typeID = $this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ');
SetValue($typeID, $type);
// params mappen
$params = $json['params'] ?? [];
$mapped = ShellyParser::MapParams($params);
/* -------------------------
* OUTPUTS
* -------------------------*/
foreach ($mapped['outputs'] as $index => $value) {
$ident = $deviceID . '_output_' . $index;
$name = 'Output ' . $index;
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
SetValue($varID, $value);
}
/* -------------------------
* INPUTS
* -------------------------*/
foreach ($mapped['inputs'] as $index => $value) {
$ident = $deviceID . '_input_' . $index;
$name = 'Input ' . $index;
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
SetValue($varID, $value);
}
/* -------------------------
* TEMPERATUR
* -------------------------*/
if ($mapped['temperature'] !== null) {
$tempID = $this->EnsureFloatVariable($deviceID, $deviceID . '_temperature', 'Temperatur');
SetValue($tempID, $mapped['temperature']);
}
}
/* ---------------------------------------------------------
* HELPER: BOOLEAN VARIABLE
* ---------------------------------------------------------*/
private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int
{
$folderID = $this->GetDeviceFolder($deviceID);
// bestehende Variable?
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
$obj = IPS_GetObject($cid);
if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) {
// OUTPUT → immer CustomAction setzen
if (str_contains($ident, '_output_')) {
IPS_SetVariableCustomAction($cid, $this->InstanceID);
$var = IPS_GetVariable($cid);
if ($var['VariableProfile'] === '' && $var['VariableCustomProfile'] === '') {
IPS_SetVariableCustomProfile($cid, '~Switch');
}
}
return $cid;
}
}
// neue Variable
$varID = IPS_CreateVariable(0);
IPS_SetIdent($varID, $ident);
IPS_SetName($varID, $name);
IPS_SetParent($varID, $folderID);
// OUTPUT-Profil + Action
if (str_contains($ident, '_output_')) {
IPS_SetVariableCustomAction($varID, $this->InstanceID);
IPS_SetVariableCustomProfile($varID, '~Switch');
}
return $varID;
}
/* ---------------------------------------------------------
* FLOAT VARIABLE
* ---------------------------------------------------------*/
private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int
{
$folderID = $this->GetDeviceFolder($deviceID);
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
$obj = IPS_GetObject($cid);
if ($obj['ObjectIdent'] === $ident) {
return $cid;
}
}
$id = $this->RegisterVariableFloat($ident, $name);
IPS_SetParent($id, $folderID);
return $id;
}
/* ---------------------------------------------------------
* STRING VARIABLE
* ---------------------------------------------------------*/
private function EnsureStringVariable(string $deviceID, string $ident, string $name): int
{
$folderID = $this->GetDeviceFolder($deviceID);
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
$obj = IPS_GetObject($cid);
if ($obj['ObjectIdent'] === $ident) {
return $cid;
}
}
$id = $this->RegisterVariableString($ident, $name);
IPS_SetParent($id, $folderID);
return $id;
}
/* ---------------------------------------------------------
* GERÄTE-ORDNER
* ---------------------------------------------------------*/
private function GetDeviceFolder(string $deviceID): int
{
$folderIdent = 'folder_' . $deviceID;
foreach (IPS_GetChildrenIDs($this->InstanceID) as $cid) {
$obj = IPS_GetObject($cid);
if ($obj['ObjectIdent'] === $folderIdent && $obj['ObjectType'] === OBJECTTYPE_CATEGORY) {
return $cid;
}
}
// neu anlegen
$folderID = IPS_CreateCategory();
IPS_SetParent($folderID, $this->InstanceID);
IPS_SetName($folderID, $deviceID);
IPS_SetIdent($folderID, $folderIdent);
return $folderID;
}
/* ---------------------------------------------------------
* HELPER: VARIABLE FINDEN
* ---------------------------------------------------------*/
private function FindVariableByIdent(string $Ident)
{
foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) {
foreach (IPS_GetChildrenIDs($folder) as $cid) {
if (IPS_GetObject($cid)['ObjectIdent'] === $Ident) {
return $cid;
}
}
}
return 0;
}
}
?>