no message
This commit is contained in:
@@ -10,20 +10,99 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
{
|
{
|
||||||
parent::Create();
|
parent::Create();
|
||||||
|
|
||||||
// MQTT verbinden
|
// MQTT verbinden (nur Verknüpfung, noch kein Subscribe!)
|
||||||
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
||||||
|
|
||||||
// Alles abonnieren
|
|
||||||
$this->Subscribe('#');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function ApplyChanges()
|
public function ApplyChanges()
|
||||||
{
|
{
|
||||||
parent::ApplyChanges();
|
parent::ApplyChanges();
|
||||||
|
|
||||||
|
// Parent verbinden
|
||||||
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
||||||
|
|
||||||
|
// Auf Statusänderungen der eigenen Instanz hören
|
||||||
|
$this->RegisterMessage($this->InstanceID, IM_CHANGESTATUS);
|
||||||
|
|
||||||
|
// Auf Statusänderungen des Parents hören, falls vorhanden
|
||||||
|
$inst = IPS_GetInstance($this->InstanceID);
|
||||||
|
$parentID = $inst['ConnectionID'] ?? 0;
|
||||||
|
if ($parentID > 0 && IPS_InstanceExists($parentID)) {
|
||||||
|
$this->RegisterMessage($parentID, IM_CHANGESTATUS);
|
||||||
|
}
|
||||||
|
|
||||||
|
// WICHTIG:
|
||||||
|
// KEIN Subscribe() hier aufrufen!
|
||||||
|
// Subscribe passiert erst, wenn Instanz/Parent wirklich aktiv ist (MessageSink).
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------
|
||||||
|
* MESSAGE SINK – REAKTION AUF STATUSÄNDERUNGEN
|
||||||
|
* ---------------------------------------------------------*/
|
||||||
|
public function MessageSink($TimeStamp, $SenderID, $Message, $Data)
|
||||||
|
{
|
||||||
|
$this->SendDebug('MessageSink', 'Sender=' . $SenderID . ' Message=' . $Message . ' Data=' . print_r($Data, true), 0);
|
||||||
|
|
||||||
|
if ($Message === IM_CHANGESTATUS) {
|
||||||
|
|
||||||
|
// Eigene Instanz-Statusänderung
|
||||||
|
if ($SenderID === $this->InstanceID) {
|
||||||
|
$newStatus = $Data[0] ?? 0;
|
||||||
|
$this->Log('STATUS', 'Instance status changed to ' . $newStatus);
|
||||||
|
if ($newStatus === IS_ACTIVE) {
|
||||||
|
// Jetzt ist das Modul aktiv → Subscribe starten
|
||||||
|
$this->Log('STATUS', 'Instance is ACTIVE → performing MQTT subscribe');
|
||||||
$this->Subscribe('#');
|
$this->Subscribe('#');
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parent-Statusänderung
|
||||||
|
$inst = IPS_GetInstance($this->InstanceID);
|
||||||
|
$parentID = $inst['ConnectionID'] ?? 0;
|
||||||
|
|
||||||
|
if (($parentID > 0) && ($SenderID === $parentID)) {
|
||||||
|
$newStatus = $Data[0] ?? 0;
|
||||||
|
$this->Log('STATUS', 'Parent status changed to ' . $newStatus);
|
||||||
|
|
||||||
|
if ($newStatus === IS_ACTIVE) {
|
||||||
|
// Parent wieder aktiv → neu subscriben
|
||||||
|
$this->Log('STATUS', 'Parent is ACTIVE → performing MQTT subscribe');
|
||||||
|
$this->Subscribe('#');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------
|
||||||
|
* HILFSFUNKTIONEN FÜR PARENT-CHECK & SICHERES SENDEN
|
||||||
|
* ---------------------------------------------------------*/
|
||||||
|
private function HasActiveParent(): bool
|
||||||
|
{
|
||||||
|
if (!IPS_InstanceExists($this->InstanceID)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$inst = IPS_GetInstance($this->InstanceID);
|
||||||
|
$parentID = $inst['ConnectionID'] ?? 0;
|
||||||
|
if ($parentID <= 0 || !IPS_InstanceExists($parentID)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$parentStatus = IPS_GetInstance($parentID)['InstanceStatus'];
|
||||||
|
return ($parentStatus === IS_ACTIVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function SafeSendToParent(array $packet): void
|
||||||
|
{
|
||||||
|
if (!$this->HasActiveParent()) {
|
||||||
|
$this->Log('MQTT', 'Cannot send to parent – parent not active or not available');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fehler unterdrücken, falls doch mal etwas schiefgeht
|
||||||
|
@$this->SendDataToParent(json_encode($packet));
|
||||||
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------
|
/* ---------------------------------------------------------
|
||||||
* DEBUG
|
* DEBUG
|
||||||
@@ -47,11 +126,11 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
'Payload' => ''
|
'Payload' => ''
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->SendDataToParent(json_encode([
|
$this->SafeSendToParent([
|
||||||
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
|
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
|
||||||
] + $packet));
|
] + $packet);
|
||||||
|
|
||||||
$this->Log("Subscribe", $topic);
|
$this->Log('Subscribe', $topic);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------
|
/* ---------------------------------------------------------
|
||||||
@@ -67,11 +146,11 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
'Payload' => $payload
|
'Payload' => $payload
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->SendDataToParent(json_encode([
|
$this->SafeSendToParent([
|
||||||
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
|
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
|
||||||
] + $packet));
|
] + $packet);
|
||||||
|
|
||||||
$this->Log("Publish", "$topic → $payload");
|
$this->Log('Publish', "$topic → $payload");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------
|
/* ---------------------------------------------------------
|
||||||
@@ -109,7 +188,7 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->Log("MQTT SEND", "$topic : $payload");
|
$this->Log('MQTT SEND', "$topic : $payload");
|
||||||
|
|
||||||
// Senden
|
// Senden
|
||||||
$this->Publish($topic, $payload);
|
$this->Publish($topic, $payload);
|
||||||
@@ -123,19 +202,25 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
$this->Log('ReceiveData', $JSONString);
|
$this->Log('ReceiveData', $JSONString);
|
||||||
|
|
||||||
$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'] ?? '';
|
||||||
$payload = $data['Payload'] ?? '';
|
$payload = $data['Payload'] ?? '';
|
||||||
|
|
||||||
if ($topic === '') return;
|
if ($topic === '') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$this->Log('ReceiveTopic', "$topic → $payload");
|
$this->Log('ReceiveTopic', "$topic → $payload");
|
||||||
|
|
||||||
$parts = explode('/', $topic);
|
$parts = explode('/', $topic);
|
||||||
$deviceID = $parts[0] ?? '';
|
$deviceID = $parts[0] ?? '';
|
||||||
|
|
||||||
if ($deviceID === '') return;
|
if ($deviceID === '') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Online-Status
|
// Online-Status
|
||||||
if (($parts[1] ?? '') === 'online') {
|
if (($parts[1] ?? '') === 'online') {
|
||||||
@@ -158,7 +243,7 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
{
|
{
|
||||||
$value = ($payload === 'true' || $payload === '1');
|
$value = ($payload === 'true' || $payload === '1');
|
||||||
|
|
||||||
$this->Log("Online", "$deviceID = $value");
|
$this->Log('Online', "$deviceID = " . ($value ? 'true' : 'false'));
|
||||||
|
|
||||||
$varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_online', 'Online');
|
$varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_online', 'Online');
|
||||||
SetValue($varID, $value);
|
SetValue($varID, $value);
|
||||||
@@ -169,13 +254,17 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
* ---------------------------------------------------------*/
|
* ---------------------------------------------------------*/
|
||||||
private function HandleRPC(string $deviceID, string $payload): void
|
private function HandleRPC(string $deviceID, string $payload): void
|
||||||
{
|
{
|
||||||
$this->Log("RPC", "$deviceID : $payload");
|
$this->Log('RPC', "$deviceID : $payload");
|
||||||
|
|
||||||
$json = json_decode($payload, true);
|
$json = json_decode($payload, true);
|
||||||
if (!is_array($json)) return;
|
if (!is_array($json)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$src = $json['src'] ?? '';
|
$src = $json['src'] ?? '';
|
||||||
if (!str_starts_with($src, 'shelly')) return;
|
if (!str_starts_with($src, 'shelly')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Typ
|
// Typ
|
||||||
$type = ShellyParser::ExtractType($src);
|
$type = ShellyParser::ExtractType($src);
|
||||||
@@ -188,15 +277,15 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
|
|
||||||
// Outputs
|
// Outputs
|
||||||
foreach ($mapped['outputs'] as $index => $value) {
|
foreach ($mapped['outputs'] as $index => $value) {
|
||||||
$ident = $deviceID . "_output_" . $index;
|
$ident = $deviceID . '_output_' . $index;
|
||||||
$varID = $this->EnsureBooleanVariable($deviceID, $ident, "Output $index");
|
$varID = $this->EnsureBooleanVariable($deviceID, $ident, 'Output ' . $index);
|
||||||
SetValue($varID, $value);
|
SetValue($varID, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inputs
|
// Inputs
|
||||||
foreach ($mapped['inputs'] as $index => $value) {
|
foreach ($mapped['inputs'] as $index => $value) {
|
||||||
$ident = $deviceID . "_input_" . $index;
|
$ident = $deviceID . '_input_' . $index;
|
||||||
$varID = $this->EnsureBooleanVariable($deviceID, $ident, "Input $index");
|
$varID = $this->EnsureBooleanVariable($deviceID, $ident, 'Input ' . $index);
|
||||||
SetValue($varID, $value);
|
SetValue($varID, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,7 +301,7 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
* ---------------------------------------------------------*/
|
* ---------------------------------------------------------*/
|
||||||
private function EnsureActionScript(): int
|
private function EnsureActionScript(): int
|
||||||
{
|
{
|
||||||
$scriptName = "~ShellyMQTT_Action_" . $this->InstanceID;
|
$scriptName = '~ShellyMQTT_Action_' . $this->InstanceID;
|
||||||
|
|
||||||
foreach (IPS_GetChildrenIDs($this->InstanceID) as $cid) {
|
foreach (IPS_GetChildrenIDs($this->InstanceID) as $cid) {
|
||||||
$obj = IPS_GetObject($cid);
|
$obj = IPS_GetObject($cid);
|
||||||
@@ -257,6 +346,7 @@ IPS_RequestAction($instance, $ident, $value);
|
|||||||
|
|
||||||
if ($o['ObjectIdent'] === $ident) {
|
if ($o['ObjectIdent'] === $ident) {
|
||||||
|
|
||||||
|
// Outputs schaltbar machen
|
||||||
if (str_contains($ident, '_output_')) {
|
if (str_contains($ident, '_output_')) {
|
||||||
$actionScript = $this->EnsureActionScript();
|
$actionScript = $this->EnsureActionScript();
|
||||||
IPS_SetVariableCustomAction($cid, $actionScript);
|
IPS_SetVariableCustomAction($cid, $actionScript);
|
||||||
@@ -291,9 +381,10 @@ IPS_RequestAction($instance, $ident, $value);
|
|||||||
$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)
|
if (IPS_GetObject($cid)['ObjectIdent'] === $ident) {
|
||||||
return $cid;
|
return $cid;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$id = $this->RegisterVariableFloat($ident, $name);
|
$id = $this->RegisterVariableFloat($ident, $name);
|
||||||
IPS_SetParent($id, $folderID);
|
IPS_SetParent($id, $folderID);
|
||||||
@@ -305,9 +396,10 @@ IPS_RequestAction($instance, $ident, $value);
|
|||||||
$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)
|
if (IPS_GetObject($cid)['ObjectIdent'] === $ident) {
|
||||||
return $cid;
|
return $cid;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$id = $this->RegisterVariableString($ident, $name);
|
$id = $this->RegisterVariableString($ident, $name);
|
||||||
IPS_SetParent($id, $folderID);
|
IPS_SetParent($id, $folderID);
|
||||||
@@ -322,9 +414,10 @@ IPS_RequestAction($instance, $ident, $value);
|
|||||||
$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)
|
if (IPS_GetObject($cid)['ObjectIdent'] === $folderIdent) {
|
||||||
return $cid;
|
return $cid;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$folderID = IPS_CreateCategory();
|
$folderID = IPS_CreateCategory();
|
||||||
IPS_SetParent($folderID, $this->InstanceID);
|
IPS_SetParent($folderID, $this->InstanceID);
|
||||||
|
|||||||
Reference in New Issue
Block a user