no message

This commit is contained in:
2025-11-14 11:11:25 +01:00
parent 3474cbafc6
commit 37403f9c1f

View File

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