no message

This commit is contained in:
dh
2025-11-14 11:03:42 +01:00
parent 10ccfede64
commit 5f3a14cd8a
+85 -99
View File
@@ -10,13 +10,12 @@ class Shelly_Parser_MQTT extends IPSModule
{ {
parent::Create(); parent::Create();
// Verbindung zum MQTT-Server herstellen // MQTT-Server verbinden
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}'); $this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
// Auf alle Topics lauschen, Filter machen wir selbst // Auf alle Topics hören
$this->Subscribe('#'); $this->Subscribe('#');
// Debug-Property
$this->RegisterPropertyBoolean('Debug', false); $this->RegisterPropertyBoolean('Debug', false);
} }
@@ -63,54 +62,48 @@ class Shelly_Parser_MQTT extends IPSModule
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
] + $packet)); ] + $packet));
if ($this->ReadPropertyBoolean('Debug')) { $this->SendDebug("Publish", "$topic$payload", 0);
IPS_LogMessage('Shelly_Parser_MQTT', 'Publish: ' . $topic . ' -> ' . $payload);
}
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
* RequestAction → Schalten von Outputs * REQUEST ACTION (Shelly schalten)
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
public function RequestAction($Ident, $Value) public function RequestAction($Ident, $Value)
{ {
IPS_LogMessage('ShellyDebug', "RequestAction($Ident" . var_export($Value, true) . ')'); $this->SendDebug('RequestAction', "$Ident" . json_encode($Value), 0);
// Prüfen ob es eine Output-Variable ist if (!str_contains($Ident, '_output_')) {
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);
// Shelly-RPC Topic
$topic = $deviceID . '/rpc';
// Shelly-RPC-kompatibles JSON
$payloadArr = [
'id' => 1,
'src' => 'ips',
'method' => 'Switch.Set',
'params' => [
'id' => $index,
'on' => (bool) $Value
]
];
$payload = json_encode($payloadArr);
IPS_LogMessage('ShellyDebug', "SEND → $topic : $payload");
// absenden
$this->Publish($topic, $payload);
return;
} }
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);
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
@@ -118,9 +111,7 @@ class Shelly_Parser_MQTT extends IPSModule
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
public function ReceiveData($JSONString) public function ReceiveData($JSONString)
{ {
if ($this->ReadPropertyBoolean('Debug')) { $this->SendDebug('ReceiveData', $JSONString, 0);
IPS_LogMessage('Shelly_Parser_MQTT', 'ReceiveData: ' . $JSONString);
}
$data = json_decode($JSONString, true); $data = json_decode($JSONString, true);
if (!is_array($data)) { if (!is_array($data)) {
@@ -141,13 +132,13 @@ class Shelly_Parser_MQTT extends IPSModule
return; return;
} }
// <GeräteID>/online // <deviceID>/online
if ((isset($parts[1])) && ($parts[1] === 'online')) { if ((isset($parts[1])) && ($parts[1] === 'online')) {
$this->HandleOnline($deviceID, $payload); $this->HandleOnline($deviceID, $payload);
return; return;
} }
// <GeräteID>/events/rpc // <deviceID>/events/rpc
if ((isset($parts[1]) && $parts[1] === 'events') && if ((isset($parts[1]) && $parts[1] === 'events') &&
(isset($parts[2]) && $parts[2] === 'rpc')) { (isset($parts[2]) && $parts[2] === 'rpc')) {
$this->HandleRPC($deviceID, $payload); $this->HandleRPC($deviceID, $payload);
@@ -156,7 +147,7 @@ class Shelly_Parser_MQTT extends IPSModule
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
* ONLINE-STATUS * ONLINE STATUS
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
private function HandleOnline(string $deviceID, string $payload): void private function HandleOnline(string $deviceID, string $payload): void
{ {
@@ -167,7 +158,7 @@ class Shelly_Parser_MQTT extends IPSModule
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
* RPC-EVENTS * RPC-EVENTS VERARBEITEN
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
private function HandleRPC(string $deviceID, string $payload): void private function HandleRPC(string $deviceID, string $payload): void
{ {
@@ -176,72 +167,70 @@ class Shelly_Parser_MQTT extends IPSModule
return; return;
} }
// Gerätetyp setzen
$src = $json['src'] ?? ''; $src = $json['src'] ?? '';
if (!str_starts_with($src, 'shelly')) { if (!str_starts_with($src, 'shelly')) {
return; return;
} }
// Gerätetyp merken
$type = ShellyParser::ExtractType($src); $type = ShellyParser::ExtractType($src);
$typeID = $this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ'); $typeID = $this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ');
SetValue($typeID, $type); SetValue($typeID, $type);
// Mappen // params mappen
$params = $json['params'] ?? []; $params = $json['params'] ?? [];
$mapped = ShellyParser::MapParams($params); $mapped = ShellyParser::MapParams($params);
// ----------------------------------- /* -------------------------
// DYNAMISCHE OUTPUTS * OUTPUTS
// ----------------------------------- * -------------------------*/
if (isset($mapped['outputs']) && is_array($mapped['outputs'])) { 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;
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name); $varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
SetValue($varID, $value); SetValue($varID, $value);
}
} }
// ----------------------------------- /* -------------------------
// DYNAMISCHE INPUTS * INPUTS
// ----------------------------------- * -------------------------*/
if (isset($mapped['inputs']) && is_array($mapped['inputs'])) { 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;
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name); $varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
SetValue($varID, $value); SetValue($varID, $value);
}
} }
// ----------------------------------- /* -------------------------
// TEMPERATUR * TEMPERATUR
// ----------------------------------- * -------------------------*/
if (array_key_exists('temperature', $mapped) && $mapped['temperature'] !== null) { if ($mapped['temperature'] !== null) {
$tempID = $this->EnsureFloatVariable($deviceID, $deviceID . '_temperature', 'Temperatur'); $tempID = $this->EnsureFloatVariable($deviceID, $deviceID . '_temperature', 'Temperatur');
SetValue($tempID, $mapped['temperature']); SetValue($tempID, $mapped['temperature']);
} }
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
* Helper für Variablen * HELPER: BOOLEAN VARIABLE
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int
{ {
$folderID = $this->GetDeviceFolder($deviceID); $folderID = $this->GetDeviceFolder($deviceID);
// vorhandene Variable finden // bestehende Variable?
foreach (IPS_GetChildrenIDs($folderID) as $cid) { foreach (IPS_GetChildrenIDs($folderID) as $cid) {
$obj = IPS_GetObject($cid); $obj = IPS_GetObject($cid);
if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) { if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) {
// *** geändert: für Outputs immer CustomAction + Profil 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);
$varInfo = IPS_GetVariable($cid);
if ($varInfo['VariableProfile'] === '' && $varInfo['VariableCustomProfile'] === '') { $var = IPS_GetVariable($cid);
if ($var['VariableProfile'] === '' && $var['VariableCustomProfile'] === '') {
IPS_SetVariableCustomProfile($cid, '~Switch'); IPS_SetVariableCustomProfile($cid, '~Switch');
} }
} }
@@ -250,13 +239,13 @@ class Shelly_Parser_MQTT extends IPSModule
} }
} }
// neue Variable erzeugen // neue Variable
$varID = IPS_CreateVariable(0); // Boolean $varID = IPS_CreateVariable(0);
IPS_SetIdent($varID, $ident); IPS_SetIdent($varID, $ident);
IPS_SetName($varID, $name); IPS_SetName($varID, $name);
IPS_SetParent($varID, $folderID); IPS_SetParent($varID, $folderID);
// *** geändert: für Outputs CustomAction + Profil setzen *** // OUTPUT-Profil + Action
if (str_contains($ident, '_output_')) { if (str_contains($ident, '_output_')) {
IPS_SetVariableCustomAction($varID, $this->InstanceID); IPS_SetVariableCustomAction($varID, $this->InstanceID);
IPS_SetVariableCustomProfile($varID, '~Switch'); IPS_SetVariableCustomProfile($varID, '~Switch');
@@ -265,13 +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) {
$obj = IPS_GetObject($cid); $obj = IPS_GetObject($cid);
if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) { if ($obj['ObjectIdent'] === $ident) {
return $cid; return $cid;
} }
} }
@@ -281,13 +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) {
$obj = IPS_GetObject($cid); $obj = IPS_GetObject($cid);
if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) { if ($obj['ObjectIdent'] === $ident) {
return $cid; return $cid;
} }
} }
@@ -298,13 +293,12 @@ class Shelly_Parser_MQTT extends IPSModule
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
* Geräte-Ordner * GERÄTE-ORDNER
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
private function GetDeviceFolder(string $deviceID): int private function GetDeviceFolder(string $deviceID): int
{ {
$folderIdent = 'folder_' . $deviceID; $folderIdent = 'folder_' . $deviceID;
// nur unter dieser Instanz suchen
foreach (IPS_GetChildrenIDs($this->InstanceID) as $cid) { foreach (IPS_GetChildrenIDs($this->InstanceID) as $cid) {
$obj = IPS_GetObject($cid); $obj = IPS_GetObject($cid);
if ($obj['ObjectIdent'] === $folderIdent && $obj['ObjectType'] === OBJECTTYPE_CATEGORY) { if ($obj['ObjectIdent'] === $folderIdent && $obj['ObjectType'] === OBJECTTYPE_CATEGORY) {
@@ -321,18 +315,9 @@ class Shelly_Parser_MQTT extends IPSModule
return $folderID; return $folderID;
} }
private function FindDeviceFolder(string $Ident): int /* ---------------------------------------------------------
{ * HELPER: VARIABLE FINDEN
foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) { * ---------------------------------------------------------*/
foreach (IPS_GetChildrenIDs($folder) as $cid) {
if (IPS_GetObject($cid)['ObjectIdent'] === $Ident) {
return $folder;
}
}
}
return 0;
}
private function FindVariableByIdent(string $Ident) private function FindVariableByIdent(string $Ident)
{ {
foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) { foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) {
@@ -345,4 +330,5 @@ class Shelly_Parser_MQTT extends IPSModule
return 0; return 0;
} }
} }
?> ?>