no message

This commit is contained in:
2025-11-14 11:08:45 +01:00
parent 5f3a14cd8a
commit 3474cbafc6

View File

@@ -16,6 +16,7 @@ class Shelly_Parser_MQTT extends IPSModule
// Auf alle Topics hören
$this->Subscribe('#');
// Debug Switch
$this->RegisterPropertyBoolean('Debug', false);
}
@@ -27,6 +28,22 @@ class Shelly_Parser_MQTT extends IPSModule
$this->Subscribe('#');
}
/* ---------------------------------------------------------
* DEBUG WRAPPER
* ---------------------------------------------------------*/
private function Log($title, $msg)
{
if (!$this->ReadPropertyBoolean('Debug')) {
return;
}
// IPS Log
IPS_LogMessage("ShellyMQTT - $title", $msg);
// Instance Debug
$this->SendDebug($title, $msg, 0);
}
/* ---------------------------------------------------------
* MQTT SUBSCRIBE
* ---------------------------------------------------------*/
@@ -43,6 +60,8 @@ class Shelly_Parser_MQTT extends IPSModule
$this->SendDataToParent(json_encode([
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
] + $packet));
$this->Log("Subscribe", "Topic: $topic");
}
/* ---------------------------------------------------------
@@ -62,7 +81,7 @@ class Shelly_Parser_MQTT extends IPSModule
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
] + $packet));
$this->SendDebug("Publish", "$topic$payload", 0);
$this->Log("Publish", "$topic$payload");
}
/* ---------------------------------------------------------
@@ -70,7 +89,7 @@ class Shelly_Parser_MQTT extends IPSModule
* ---------------------------------------------------------*/
public function RequestAction($Ident, $Value)
{
$this->SendDebug('RequestAction', "$Ident" . json_encode($Value), 0);
$this->Log("RequestAction", "$Ident" . json_encode($Value));
if (!str_contains($Ident, '_output_')) {
throw new Exception("Unknown Ident: " . $Ident);
@@ -100,18 +119,18 @@ class Shelly_Parser_MQTT extends IPSModule
]
]);
$this->SendDebug('MQTT SEND', "$topic : $payload", 0);
$this->Log("MQTT SEND", "$topic : $payload");
// absenden
$this->Publish($topic, $payload);
}
/* ---------------------------------------------------------
* RECEIVE MQTT DATA
* RECEIVE MQTT
* ---------------------------------------------------------*/
public function ReceiveData($JSONString)
{
$this->SendDebug('ReceiveData', $JSONString, 0);
$this->Log("ReceiveData Raw", $JSONString);
$data = json_decode($JSONString, true);
if (!is_array($data)) {
@@ -125,6 +144,8 @@ class Shelly_Parser_MQTT extends IPSModule
return;
}
$this->Log("ReceiveTopic", "$topic$payload");
$parts = explode('/', $topic);
$deviceID = $parts[0] ?? '';
@@ -153,15 +174,19 @@ class Shelly_Parser_MQTT extends IPSModule
{
$value = ($payload === 'true' || $payload === '1');
$this->Log("DeviceOnline", "$deviceID = " . json_encode($value));
$varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_online', 'Online');
SetValue($varID, $value);
}
/* ---------------------------------------------------------
* RPC-EVENTS VERARBEITEN
* RPC-EVENTS
* ---------------------------------------------------------*/
private function HandleRPC(string $deviceID, string $payload): void
{
$this->Log("HandleRPC", "$deviceID : $payload");
$json = json_decode($payload, true);
if (!is_array($json)) {
return;
@@ -186,7 +211,9 @@ class Shelly_Parser_MQTT extends IPSModule
* -------------------------*/
foreach ($mapped['outputs'] as $index => $value) {
$ident = $deviceID . '_output_' . $index;
$name = 'Output ' . $index;
$name = "Output $index";
$this->Log("RPC Output", "$ident = " . json_encode($value));
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
SetValue($varID, $value);
@@ -197,7 +224,9 @@ class Shelly_Parser_MQTT extends IPSModule
* -------------------------*/
foreach ($mapped['inputs'] as $index => $value) {
$ident = $deviceID . '_input_' . $index;
$name = 'Input ' . $index;
$name = "Input $index";
$this->Log("RPC Input", "$ident = " . json_encode($value));
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
SetValue($varID, $value);
@@ -207,13 +236,15 @@ class Shelly_Parser_MQTT extends IPSModule
* TEMPERATUR
* -------------------------*/
if ($mapped['temperature'] !== null) {
$this->Log("RPC Temperature", json_encode($mapped['temperature']));
$tempID = $this->EnsureFloatVariable($deviceID, $deviceID . '_temperature', 'Temperatur');
SetValue($tempID, $mapped['temperature']);
}
}
/* ---------------------------------------------------------
* HELPER: BOOLEAN VARIABLE
* VARIABLE-HELPER
* ---------------------------------------------------------*/
private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int
{
@@ -225,7 +256,7 @@ class Shelly_Parser_MQTT extends IPSModule
if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) {
// OUTPUT → immer CustomAction setzen
// OUTPUT → immer Action setzen
if (str_contains($ident, '_output_')) {
IPS_SetVariableCustomAction($cid, $this->InstanceID);
@@ -254,16 +285,12 @@ class Shelly_Parser_MQTT extends IPSModule
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) {
if (IPS_GetObject($cid)['ObjectIdent'] === $ident) {
return $cid;
}
}
@@ -273,16 +300,12 @@ class Shelly_Parser_MQTT extends IPSModule
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) {
if (IPS_GetObject($cid)['ObjectIdent'] === $ident) {
return $cid;
}
}
@@ -293,20 +316,18 @@ class Shelly_Parser_MQTT extends IPSModule
}
/* ---------------------------------------------------------
* GERÄTE-ORDNER
* DEVICE FOLDER
* ---------------------------------------------------------*/
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) {
if (IPS_GetObject($cid)['ObjectIdent'] === $folderIdent) {
return $cid;
}
}
// neu anlegen
$folderID = IPS_CreateCategory();
IPS_SetParent($folderID, $this->InstanceID);
IPS_SetName($folderID, $deviceID);
@@ -316,7 +337,7 @@ class Shelly_Parser_MQTT extends IPSModule
}
/* ---------------------------------------------------------
* HELPER: VARIABLE FINDEN
* VARIABLE FINDER
* ---------------------------------------------------------*/
private function FindVariableByIdent(string $Ident)
{