no message

This commit is contained in:
2025-11-14 11:03:42 +01:00
parent 10ccfede64
commit 5f3a14cd8a

View File

@@ -10,13 +10,12 @@ class Shelly_Parser_MQTT extends IPSModule
{
parent::Create();
// Verbindung zum MQTT-Server herstellen
// MQTT-Server verbinden
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
// Auf alle Topics lauschen, Filter machen wir selbst
// Auf alle Topics hören
$this->Subscribe('#');
// Debug-Property
$this->RegisterPropertyBoolean('Debug', false);
}
@@ -63,54 +62,48 @@ class Shelly_Parser_MQTT extends IPSModule
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
] + $packet));
if ($this->ReadPropertyBoolean('Debug')) {
IPS_LogMessage('Shelly_Parser_MQTT', 'Publish: ' . $topic . ' -> ' . $payload);
}
$this->SendDebug("Publish", "$topic$payload", 0);
}
/* ---------------------------------------------------------
* RequestAction → Schalten von Outputs
* REQUEST ACTION (Shelly schalten)
* ---------------------------------------------------------*/
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_')) {
// 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;
if (!str_contains($Ident, '_output_')) {
throw new Exception("Unknown Ident: " . $Ident);
}
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)
{
if ($this->ReadPropertyBoolean('Debug')) {
IPS_LogMessage('Shelly_Parser_MQTT', 'ReceiveData: ' . $JSONString);
}
$this->SendDebug('ReceiveData', $JSONString, 0);
$data = json_decode($JSONString, true);
if (!is_array($data)) {
@@ -141,13 +132,13 @@ class Shelly_Parser_MQTT extends IPSModule
return;
}
// <GeräteID>/online
// <deviceID>/online
if ((isset($parts[1])) && ($parts[1] === 'online')) {
$this->HandleOnline($deviceID, $payload);
return;
}
// <GeräteID>/events/rpc
// <deviceID>/events/rpc
if ((isset($parts[1]) && $parts[1] === 'events') &&
(isset($parts[2]) && $parts[2] === 'rpc')) {
$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
{
@@ -167,7 +158,7 @@ class Shelly_Parser_MQTT extends IPSModule
}
/* ---------------------------------------------------------
* RPC-EVENTS
* RPC-EVENTS VERARBEITEN
* ---------------------------------------------------------*/
private function HandleRPC(string $deviceID, string $payload): void
{
@@ -176,72 +167,70 @@ class Shelly_Parser_MQTT extends IPSModule
return;
}
// Gerätetyp setzen
$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);
// Mappen
// params mappen
$params = $json['params'] ?? [];
$mapped = ShellyParser::MapParams($params);
// -----------------------------------
// DYNAMISCHE OUTPUTS
// -----------------------------------
if (isset($mapped['outputs']) && is_array($mapped['outputs'])) {
foreach ($mapped['outputs'] as $index => $value) {
$ident = $deviceID . '_output_' . $index;
$name = 'Output ' . $index;
/* -------------------------
* OUTPUTS
* -------------------------*/
foreach ($mapped['outputs'] as $index => $value) {
$ident = $deviceID . '_output_' . $index;
$name = 'Output ' . $index;
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
SetValue($varID, $value);
}
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
SetValue($varID, $value);
}
// -----------------------------------
// DYNAMISCHE INPUTS
// -----------------------------------
if (isset($mapped['inputs']) && is_array($mapped['inputs'])) {
foreach ($mapped['inputs'] as $index => $value) {
$ident = $deviceID . '_input_' . $index;
$name = 'Input ' . $index;
/* -------------------------
* INPUTS
* -------------------------*/
foreach ($mapped['inputs'] as $index => $value) {
$ident = $deviceID . '_input_' . $index;
$name = 'Input ' . $index;
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
SetValue($varID, $value);
}
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
SetValue($varID, $value);
}
// -----------------------------------
// TEMPERATUR
// -----------------------------------
if (array_key_exists('temperature', $mapped) && $mapped['temperature'] !== null) {
/* -------------------------
* TEMPERATUR
* -------------------------*/
if ($mapped['temperature'] !== null) {
$tempID = $this->EnsureFloatVariable($deviceID, $deviceID . '_temperature', 'Temperatur');
SetValue($tempID, $mapped['temperature']);
}
}
/* ---------------------------------------------------------
* Helper für Variablen
* HELPER: BOOLEAN VARIABLE
* ---------------------------------------------------------*/
private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int
{
$folderID = $this->GetDeviceFolder($deviceID);
// vorhandene Variable finden
// bestehende Variable?
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
$obj = IPS_GetObject($cid);
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_')) {
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');
}
}
@@ -250,13 +239,13 @@ class Shelly_Parser_MQTT extends IPSModule
}
}
// neue Variable erzeugen
$varID = IPS_CreateVariable(0); // Boolean
// neue Variable
$varID = IPS_CreateVariable(0);
IPS_SetIdent($varID, $ident);
IPS_SetName($varID, $name);
IPS_SetParent($varID, $folderID);
// *** geändert: für Outputs CustomAction + Profil setzen ***
// OUTPUT-Profil + Action
if (str_contains($ident, '_output_')) {
IPS_SetVariableCustomAction($varID, $this->InstanceID);
IPS_SetVariableCustomProfile($varID, '~Switch');
@@ -265,13 +254,16 @@ 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 && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) {
if ($obj['ObjectIdent'] === $ident) {
return $cid;
}
}
@@ -281,13 +273,16 @@ 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 && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) {
if ($obj['ObjectIdent'] === $ident) {
return $cid;
}
}
@@ -298,13 +293,12 @@ class Shelly_Parser_MQTT extends IPSModule
}
/* ---------------------------------------------------------
* Geräte-Ordner
* GERÄTE-ORDNER
* ---------------------------------------------------------*/
private function GetDeviceFolder(string $deviceID): int
{
$folderIdent = 'folder_' . $deviceID;
// nur unter dieser Instanz suchen
foreach (IPS_GetChildrenIDs($this->InstanceID) as $cid) {
$obj = IPS_GetObject($cid);
if ($obj['ObjectIdent'] === $folderIdent && $obj['ObjectType'] === OBJECTTYPE_CATEGORY) {
@@ -321,18 +315,9 @@ class Shelly_Parser_MQTT extends IPSModule
return $folderID;
}
private function FindDeviceFolder(string $Ident): int
{
foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) {
foreach (IPS_GetChildrenIDs($folder) as $cid) {
if (IPS_GetObject($cid)['ObjectIdent'] === $Ident) {
return $folder;
}
}
}
return 0;
}
/* ---------------------------------------------------------
* HELPER: VARIABLE FINDEN
* ---------------------------------------------------------*/
private function FindVariableByIdent(string $Ident)
{
foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) {
@@ -345,4 +330,5 @@ class Shelly_Parser_MQTT extends IPSModule
return 0;
}
}
?>