no message

This commit is contained in:
2025-11-24 11:25:55 +01:00
parent ffa5cd8112
commit dde54768b1

View File

@@ -12,7 +12,7 @@ class Shelly_Parser_MQTT extends IPSModule
{ {
parent::Create(); parent::Create();
// MQTT-Server // MQTT Parent
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}'); $this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
} }
@@ -22,12 +22,14 @@ class Shelly_Parser_MQTT extends IPSModule
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}'); $this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
// Subscribe wird erst beim ersten Paket ausgeführt // Eines für ALLE Outputs
$this->EnsureActionScript();
$this->Subscribed = false; $this->Subscribed = false;
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
* DEBUG SHORTCUT * DEBUG
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
private function Log($title, $msg) private function Log($title, $msg)
{ {
@@ -36,32 +38,24 @@ class Shelly_Parser_MQTT extends IPSModule
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
* Sicher Senden (Parent OK?) * SAFE SEND (Parent prüfen)
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
private function SafeSend(array $packet) private function SafeSend(array $packet)
{ {
$parent = @IPS_GetInstance($this->InstanceID)['ConnectionID'] ?? 0; $parent = @IPS_GetInstance($this->InstanceID)['ConnectionID'] ?? 0;
if ($parent === 0 || !IPS_InstanceExists($parent)) { if ($parent === 0 || !IPS_InstanceExists($parent)) return;
return; if (IPS_GetInstance($parent)['InstanceStatus'] !== 102) return;
}
if (IPS_GetInstance($parent)['InstanceStatus'] !== 102) {
return;
}
@$this->SendDataToParent(json_encode($packet)); @$this->SendDataToParent(json_encode($packet));
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
* Subscribe once * SUBSCRIBE
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
private function EnsureSubscribe() private function EnsureSubscribe()
{ {
if ($this->Subscribed) { if ($this->Subscribed) return;
return;
}
$this->Subscribed = true; $this->Subscribed = true;
$this->SafeSend([ $this->SafeSend([
@@ -73,11 +67,11 @@ class Shelly_Parser_MQTT extends IPSModule
'Payload' => '' 'Payload' => ''
]); ]);
$this->Log("Subscribe", "Subscribed to #"); $this->Log("Subscribe", "#");
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
* MQTT Publish * PUBLISH
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
private function Publish(string $topic, string $payload) private function Publish(string $topic, string $payload)
{ {
@@ -94,23 +88,20 @@ class Shelly_Parser_MQTT extends IPSModule
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
* REQUEST ACTION (Schalten Shelly Output) * REQUEST ACTION
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
public function RequestAction($Ident, $Value) public function RequestAction($Ident, $Value)
{ {
$this->Log("RequestAction", "$Ident" . json_encode($Value)); $this->Log("RequestAction", "$Ident" . json_encode($Value));
if (!str_contains($Ident, '_output_')) { if (!str_contains($Ident, '_output_'))
throw new Exception("Unknown Ident: $Ident"); throw new Exception("Unknown Ident: $Ident");
}
// Lokale Variable setzen // Variable setzen
$varID = $this->FindVariableByIdent($Ident); $varID = $this->FindVariableByIdent($Ident);
if ($varID > 0) { if ($varID) SetValue($varID, $Value);
SetValue($varID, $Value);
}
// deviceID / Index extrahieren // deviceID / index extrahieren
[$deviceID, $suffix] = explode('_output_', $Ident); [$deviceID, $suffix] = explode('_output_', $Ident);
$index = intval($suffix); $index = intval($suffix);
@@ -135,8 +126,8 @@ class Shelly_Parser_MQTT extends IPSModule
public function ReceiveData($JSONString) public function ReceiveData($JSONString)
{ {
$this->EnsureSubscribe(); $this->EnsureSubscribe();
$data = json_decode($JSONString, true);
$data = json_decode($JSONString, true);
if (!is_array($data)) return; if (!is_array($data)) return;
$topic = $data['Topic'] ?? ''; $topic = $data['Topic'] ?? '';
@@ -151,13 +142,13 @@ class Shelly_Parser_MQTT extends IPSModule
if ($deviceID === '') return; if ($deviceID === '') return;
// Online // ONLINE
if (($parts[1] ?? '') === 'online') { if (($parts[1] ?? '') === 'online') {
$this->HandleOnline($deviceID, $payload); $this->HandleOnline($deviceID, $payload);
return; return;
} }
// Events RPC // RPC
if (($parts[1] ?? '') === 'events' && ($parts[2] ?? '') === 'rpc') { if (($parts[1] ?? '') === 'events' && ($parts[2] ?? '') === 'rpc') {
$this->HandleRPC($deviceID, $payload); $this->HandleRPC($deviceID, $payload);
return; return;
@@ -175,7 +166,7 @@ class Shelly_Parser_MQTT extends IPSModule
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
* RPC Handler * RPC / EVENTS
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
private function HandleRPC(string $deviceID, string $payload) private function HandleRPC(string $deviceID, string $payload)
{ {
@@ -185,27 +176,23 @@ class Shelly_Parser_MQTT extends IPSModule
$src = $json['src'] ?? ''; $src = $json['src'] ?? '';
if (!str_starts_with($src, 'shelly')) return; if (!str_starts_with($src, 'shelly')) return;
// Typ
$type = ShellyParser::ExtractType($src); $type = ShellyParser::ExtractType($src);
$tid = $this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ'); $tid = $this->EnsureStringVariable($deviceID, "{$deviceID}_type", "Typ");
SetValue($tid, $type); SetValue($tid, $type);
$params = $json['params'] ?? []; $params = $json['params'] ?? [];
$mapped = ShellyParser::MapParams($params); $mapped = ShellyParser::MapParams($params);
// Outputs
foreach ($mapped['outputs'] as $i => $val) { foreach ($mapped['outputs'] as $i => $val) {
$vid = $this->EnsureBooleanVariable($deviceID, "{$deviceID}_output_$i", "Output $i"); $vid = $this->EnsureBooleanVariable($deviceID, "{$deviceID}_output_$i", "Output $i");
SetValue($vid, $val); SetValue($vid, $val);
} }
// Inputs
foreach ($mapped['inputs'] as $i => $val) { foreach ($mapped['inputs'] as $i => $val) {
$vid = $this->EnsureBooleanVariable($deviceID, "{$deviceID}_input_$i", "Input $i"); $vid = $this->EnsureBooleanVariable($deviceID, "{$deviceID}_input_$i", "Input $i");
SetValue($vid, $val); SetValue($vid, $val);
} }
// Temperatur
if ($mapped['temperature'] !== null) { if ($mapped['temperature'] !== null) {
$f = $this->EnsureFloatVariable($deviceID, "{$deviceID}_temperature", "Temperatur"); $f = $this->EnsureFloatVariable($deviceID, "{$deviceID}_temperature", "Temperatur");
SetValue($f, $mapped['temperature']); SetValue($f, $mapped['temperature']);
@@ -213,7 +200,41 @@ class Shelly_Parser_MQTT extends IPSModule
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
* VARIABLEN MIT AUTOMATISCHER REPARATUR * ACTION SCRIPT (einmalig für ALLE Outputs)
* ---------------------------------------------------------*/
private function EnsureActionScript()
{
$ident = "action_handler";
$sid = @IPS_GetObjectIDByIdent($ident, $this->InstanceID);
if ($sid === false) {
$sid = IPS_CreateScript(0);
IPS_SetParent($sid, $this->InstanceID);
IPS_SetIdent($sid, $ident);
IPS_SetName($sid, "Shelly Action Handler");
$code = <<<'EOF'
<?php
$moduleID = IPS_GetParent($_IPS['SELF']);
$varID = $_IPS['VARIABLE'];
$value = $_IPS['VALUE'];
$ident = IPS_GetObject($varID)['ObjectIdent'];
RequestAction($moduleID, $ident, $value);
?>
EOF;
IPS_SetScriptContent($sid, $code);
}
return $sid;
}
/* ---------------------------------------------------------
* VARIABLEN
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int
{ {
@@ -223,7 +244,7 @@ class Shelly_Parser_MQTT extends IPSModule
foreach (IPS_GetChildrenIDs($folder) as $cid) { foreach (IPS_GetChildrenIDs($folder) as $cid) {
$obj = IPS_GetObject($cid); $obj = IPS_GetObject($cid);
// Auto-Repair für alte Versionen ohne Ident // AutoFix alter Versionen ohne Ident
if ($obj['ObjectName'] === $name && $obj['ObjectIdent'] === '') { if ($obj['ObjectName'] === $name && $obj['ObjectIdent'] === '') {
IPS_SetIdent($cid, $ident); IPS_SetIdent($cid, $ident);
$vid = $cid; $vid = $cid;
@@ -243,36 +264,12 @@ class Shelly_Parser_MQTT extends IPSModule
IPS_SetParent($vid, $folder); IPS_SetParent($vid, $folder);
} }
// OUTPUT: Switch-Profil + ActionScript // OUTPUT → EIN gemeinsames Action Script
if (str_contains($ident, '_output_')) { if (str_contains($ident, '_output_')) {
IPS_SetVariableCustomProfile($vid, '~Switch'); IPS_SetVariableCustomProfile($vid, '~Switch');
IPS_SetVariableCustomAction($vid, $this->EnsureActionScript());
// Action Script suchen / erstellen
$scriptIdent = $ident . "_action";
$scriptID = @IPS_GetObjectIDByIdent($scriptIdent, $folder);
if ($scriptID === false) {
$scriptID = IPS_CreateScript(0); // 0 = PHP Script
IPS_SetParent($scriptID, $folder);
IPS_SetIdent($scriptID, $scriptIdent);
IPS_SetName($scriptID, $name . " Action");
$code = <<<EOF
<?php
\$module = IPS_GetParent(\$_IPS['SELF']);
RequestAction(\$module, \$_IPS['VARIABLE'], \$_IPS['VALUE']);
?>
EOF;
IPS_SetScriptContent($scriptID, $code);
} }
// Variable zeigt auf dieses Action-Script
IPS_SetVariableCustomAction($vid, $scriptID);
}
return $vid; return $vid;
} }
@@ -281,10 +278,9 @@ class Shelly_Parser_MQTT extends IPSModule
$folder = $this->GetDeviceFolder($deviceID); $folder = $this->GetDeviceFolder($deviceID);
foreach (IPS_GetChildrenIDs($folder) as $cid) { foreach (IPS_GetChildrenIDs($folder) as $cid) {
if (IPS_GetObject($cid)['ObjectIdent'] === $ident) { if (IPS_GetObject($cid)['ObjectIdent'] === $ident)
return $cid; return $cid;
} }
}
$vid = $this->RegisterVariableFloat($ident, $name); $vid = $this->RegisterVariableFloat($ident, $name);
IPS_SetParent($vid, $folder); IPS_SetParent($vid, $folder);
@@ -296,25 +292,26 @@ class Shelly_Parser_MQTT extends IPSModule
$folder = $this->GetDeviceFolder($deviceID); $folder = $this->GetDeviceFolder($deviceID);
foreach (IPS_GetChildrenIDs($folder) as $cid) { foreach (IPS_GetChildrenIDs($folder) as $cid) {
if (IPS_GetObject($cid)['ObjectIdent'] === $ident) { if (IPS_GetObject($cid)['ObjectIdent'] === $ident)
return $cid; return $cid;
} }
}
$vid = $this->RegisterVariableString($ident, $name); $vid = $this->RegisterVariableString($ident, $name);
IPS_SetParent($vid, $folder); IPS_SetParent($vid, $folder);
return $vid; return $vid;
} }
/* ---------------------------------------------------------
* DEVICE FOLDER
* ---------------------------------------------------------*/
private function GetDeviceFolder(string $deviceID): int private function GetDeviceFolder(string $deviceID): int
{ {
$ident = 'folder_' . $deviceID; $ident = "folder_" . $deviceID;
foreach (IPS_GetChildrenIDs($this->InstanceID) as $cid) { foreach (IPS_GetChildrenIDs($this->InstanceID) as $cid) {
if (IPS_GetObject($cid)['ObjectIdent'] === $ident) { if (IPS_GetObject($cid)['ObjectIdent'] === $ident)
return $cid; return $cid;
} }
}
$fid = IPS_CreateCategory(); $fid = IPS_CreateCategory();
IPS_SetName($fid, $deviceID); IPS_SetName($fid, $deviceID);
@@ -328,11 +325,10 @@ class Shelly_Parser_MQTT extends IPSModule
{ {
foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) { foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) {
foreach (IPS_GetChildrenIDs($folder) as $cid) { foreach (IPS_GetChildrenIDs($folder) as $cid) {
if (IPS_GetObject($cid)['ObjectIdent'] === $Ident) { if (IPS_GetObject($cid)['ObjectIdent'] === $Ident)
return $cid; return $cid;
} }
} }
}
return 0; return 0;
} }
} }