no message
This commit is contained in:
@@ -12,7 +12,7 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
{
|
||||
parent::Create();
|
||||
|
||||
// MQTT-Server
|
||||
// MQTT Parent
|
||||
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
||||
}
|
||||
|
||||
@@ -22,12 +22,14 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------
|
||||
* DEBUG SHORTCUT
|
||||
* DEBUG
|
||||
* ---------------------------------------------------------*/
|
||||
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)
|
||||
{
|
||||
$parent = @IPS_GetInstance($this->InstanceID)['ConnectionID'] ?? 0;
|
||||
|
||||
if ($parent === 0 || !IPS_InstanceExists($parent)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (IPS_GetInstance($parent)['InstanceStatus'] !== 102) {
|
||||
return;
|
||||
}
|
||||
if ($parent === 0 || !IPS_InstanceExists($parent)) return;
|
||||
if (IPS_GetInstance($parent)['InstanceStatus'] !== 102) return;
|
||||
|
||||
@$this->SendDataToParent(json_encode($packet));
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------
|
||||
* Subscribe once
|
||||
* SUBSCRIBE
|
||||
* ---------------------------------------------------------*/
|
||||
private function EnsureSubscribe()
|
||||
{
|
||||
if ($this->Subscribed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->Subscribed) return;
|
||||
$this->Subscribed = true;
|
||||
|
||||
$this->SafeSend([
|
||||
@@ -73,11 +67,11 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
'Payload' => ''
|
||||
]);
|
||||
|
||||
$this->Log("Subscribe", "Subscribed to #");
|
||||
$this->Log("Subscribe", "#");
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------
|
||||
* MQTT Publish
|
||||
* PUBLISH
|
||||
* ---------------------------------------------------------*/
|
||||
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)
|
||||
{
|
||||
$this->Log("RequestAction", "$Ident → " . json_encode($Value));
|
||||
|
||||
if (!str_contains($Ident, '_output_')) {
|
||||
if (!str_contains($Ident, '_output_'))
|
||||
throw new Exception("Unknown Ident: $Ident");
|
||||
}
|
||||
|
||||
// Lokale Variable setzen
|
||||
// Variable setzen
|
||||
$varID = $this->FindVariableByIdent($Ident);
|
||||
if ($varID > 0) {
|
||||
SetValue($varID, $Value);
|
||||
}
|
||||
if ($varID) SetValue($varID, $Value);
|
||||
|
||||
// deviceID / Index extrahieren
|
||||
// deviceID / index extrahieren
|
||||
[$deviceID, $suffix] = explode('_output_', $Ident);
|
||||
$index = intval($suffix);
|
||||
|
||||
@@ -135,8 +126,8 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
public function ReceiveData($JSONString)
|
||||
{
|
||||
$this->EnsureSubscribe();
|
||||
$data = json_decode($JSONString, true);
|
||||
|
||||
$data = json_decode($JSONString, true);
|
||||
if (!is_array($data)) return;
|
||||
|
||||
$topic = $data['Topic'] ?? '';
|
||||
@@ -151,13 +142,13 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
|
||||
if ($deviceID === '') return;
|
||||
|
||||
// Online
|
||||
// ONLINE
|
||||
if (($parts[1] ?? '') === 'online') {
|
||||
$this->HandleOnline($deviceID, $payload);
|
||||
return;
|
||||
}
|
||||
|
||||
// Events RPC
|
||||
// RPC
|
||||
if (($parts[1] ?? '') === 'events' && ($parts[2] ?? '') === 'rpc') {
|
||||
$this->HandleRPC($deviceID, $payload);
|
||||
return;
|
||||
@@ -175,7 +166,7 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------
|
||||
* RPC Handler
|
||||
* RPC / EVENTS
|
||||
* ---------------------------------------------------------*/
|
||||
private function HandleRPC(string $deviceID, string $payload)
|
||||
{
|
||||
@@ -185,27 +176,23 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
$src = $json['src'] ?? '';
|
||||
if (!str_starts_with($src, 'shelly')) return;
|
||||
|
||||
// Typ
|
||||
$type = ShellyParser::ExtractType($src);
|
||||
$tid = $this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ');
|
||||
$tid = $this->EnsureStringVariable($deviceID, "{$deviceID}_type", "Typ");
|
||||
SetValue($tid, $type);
|
||||
|
||||
$params = $json['params'] ?? [];
|
||||
$mapped = ShellyParser::MapParams($params);
|
||||
|
||||
// Outputs
|
||||
foreach ($mapped['outputs'] as $i => $val) {
|
||||
$vid = $this->EnsureBooleanVariable($deviceID, "{$deviceID}_output_$i", "Output $i");
|
||||
SetValue($vid, $val);
|
||||
}
|
||||
|
||||
// Inputs
|
||||
foreach ($mapped['inputs'] as $i => $val) {
|
||||
$vid = $this->EnsureBooleanVariable($deviceID, "{$deviceID}_input_$i", "Input $i");
|
||||
SetValue($vid, $val);
|
||||
}
|
||||
|
||||
// Temperatur
|
||||
if ($mapped['temperature'] !== null) {
|
||||
$f = $this->EnsureFloatVariable($deviceID, "{$deviceID}_temperature", "Temperatur");
|
||||
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
|
||||
{
|
||||
@@ -223,7 +244,7 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
foreach (IPS_GetChildrenIDs($folder) as $cid) {
|
||||
$obj = IPS_GetObject($cid);
|
||||
|
||||
// Auto-Repair für alte Versionen ohne Ident
|
||||
// AutoFix alter Versionen ohne Ident
|
||||
if ($obj['ObjectName'] === $name && $obj['ObjectIdent'] === '') {
|
||||
IPS_SetIdent($cid, $ident);
|
||||
$vid = $cid;
|
||||
@@ -243,36 +264,12 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
IPS_SetParent($vid, $folder);
|
||||
}
|
||||
|
||||
// OUTPUT: Switch-Profil + ActionScript
|
||||
// OUTPUT → EIN gemeinsames Action Script
|
||||
if (str_contains($ident, '_output_')) {
|
||||
|
||||
IPS_SetVariableCustomProfile($vid, '~Switch');
|
||||
|
||||
// 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);
|
||||
IPS_SetVariableCustomAction($vid, $this->EnsureActionScript());
|
||||
}
|
||||
|
||||
|
||||
return $vid;
|
||||
}
|
||||
|
||||
@@ -281,9 +278,8 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
$folder = $this->GetDeviceFolder($deviceID);
|
||||
|
||||
foreach (IPS_GetChildrenIDs($folder) as $cid) {
|
||||
if (IPS_GetObject($cid)['ObjectIdent'] === $ident) {
|
||||
if (IPS_GetObject($cid)['ObjectIdent'] === $ident)
|
||||
return $cid;
|
||||
}
|
||||
}
|
||||
|
||||
$vid = $this->RegisterVariableFloat($ident, $name);
|
||||
@@ -296,9 +292,8 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
$folder = $this->GetDeviceFolder($deviceID);
|
||||
|
||||
foreach (IPS_GetChildrenIDs($folder) as $cid) {
|
||||
if (IPS_GetObject($cid)['ObjectIdent'] === $ident) {
|
||||
if (IPS_GetObject($cid)['ObjectIdent'] === $ident)
|
||||
return $cid;
|
||||
}
|
||||
}
|
||||
|
||||
$vid = $this->RegisterVariableString($ident, $name);
|
||||
@@ -306,14 +301,16 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
return $vid;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------
|
||||
* DEVICE FOLDER
|
||||
* ---------------------------------------------------------*/
|
||||
private function GetDeviceFolder(string $deviceID): int
|
||||
{
|
||||
$ident = 'folder_' . $deviceID;
|
||||
$ident = "folder_" . $deviceID;
|
||||
|
||||
foreach (IPS_GetChildrenIDs($this->InstanceID) as $cid) {
|
||||
if (IPS_GetObject($cid)['ObjectIdent'] === $ident) {
|
||||
if (IPS_GetObject($cid)['ObjectIdent'] === $ident)
|
||||
return $cid;
|
||||
}
|
||||
}
|
||||
|
||||
$fid = IPS_CreateCategory();
|
||||
@@ -328,9 +325,8 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
{
|
||||
foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) {
|
||||
foreach (IPS_GetChildrenIDs($folder) as $cid) {
|
||||
if (IPS_GetObject($cid)['ObjectIdent'] === $Ident) {
|
||||
if (IPS_GetObject($cid)['ObjectIdent'] === $Ident)
|
||||
return $cid;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user