ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}'); // Auf alle Topics lauschen, Filter machen wir selbst $this->Subscribe('#'); // Debug-Property $this->RegisterPropertyBoolean('Debug', false); } public function ApplyChanges() { parent::ApplyChanges(); $this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}'); $this->Subscribe('#'); } /* --------------------------------------------------------- * MQTT SUBSCRIBE * ---------------------------------------------------------*/ private function Subscribe(string $topic): void { $packet = [ 'PacketType' => 8, 'QualityOfService' => 0, 'Retain' => false, 'Topic' => $topic, 'Payload' => '' ]; $this->SendDataToParent(json_encode([ 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' ] + $packet)); } /* --------------------------------------------------------- * MQTT PUBLISH * ---------------------------------------------------------*/ private function Publish(string $topic, string $payload): void { $packet = [ 'PacketType' => 3, 'QualityOfService' => 0, 'Retain' => false, 'Topic' => $topic, 'Payload' => $payload ]; $this->SendDataToParent(json_encode([ 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' ] + $packet)); if ($this->ReadPropertyBoolean('Debug')) { IPS_LogMessage('Shelly_Parser_MQTT', 'Publish: ' . $topic . ' -> ' . $payload); } } /* --------------------------------------------------------- * RequestAction → Schalten von Outputs * ---------------------------------------------------------*/ public function RequestAction($Ident, $Value) { $this->SetValue($Ident, $Value); // Output: _output if (str_ends_with($Ident, '_output')) { $deviceID = substr($Ident, 0, -strlen('_output')); $topic = $deviceID . '/rpc/Switch.Set'; $payload = json_encode([ 'id' => 0, 'on' => (bool)$Value ]); $this->Publish($topic, $payload); } } /* --------------------------------------------------------- * RECEIVE MQTT DATA * ---------------------------------------------------------*/ public function ReceiveData($JSONString) { if ($this->ReadPropertyBoolean('Debug')) { IPS_LogMessage('Shelly_Parser_MQTT', 'ReceiveData: ' . $JSONString); } $data = json_decode($JSONString, true); if (!is_array($data)) { return; } $topic = $data['Topic'] ?? ''; $payload = $data['Payload'] ?? ''; if ($topic === '') { return; } $parts = explode('/', $topic); $deviceID = $parts[0] ?? ''; if ($deviceID === '') { return; } // /online if ((isset($parts[1])) && ($parts[1] === 'online')) { $this->HandleOnline($deviceID, $payload); return; } // /events/rpc if ((isset($parts[1]) && $parts[1] === 'events') && (isset($parts[2]) && $parts[2] === 'rpc')) { $this->HandleRPC($deviceID, $payload); return; } } /* --------------------------------------------------------- * ONLINE-STATUS * ---------------------------------------------------------*/ private function HandleOnline(string $deviceID, string $payload): void { $value = ($payload === 'true' || $payload === '1'); $this->SetValue( $this->EnsureBooleanVariable($deviceID, $deviceID . '_online', 'Online'), $value ); } /* --------------------------------------------------------- * RPC-EVENTS * ---------------------------------------------------------*/ private function HandleRPC(string $deviceID, string $payload): void { $json = json_decode($payload, true); if (!is_array($json)) { return; } $src = $json['src'] ?? ''; if (!is_string($src) || !str_starts_with($src, 'shelly')) { return; } // Modell Typ extrahieren $type = ShellyParser::ExtractType($src); // Typ Variable $this->SetValue( $this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ'), $type ); // PARAMETER MAPPEN $params = $json['params'] ?? []; $mapped = ShellyParser::MapParams($params); // INPUT if (isset($mapped['input'])) { $this->SetValue( $this->EnsureBooleanVariable($deviceID, $deviceID . '_input', 'Input'), (bool)$mapped['input'] ); } // OUTPUT if (isset($mapped['output'])) { $varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_output', 'Output'); $this->EnableAction($deviceID . '_output'); $this->SetValue($varID, (bool)$mapped['output']); } // TEMPERATURE if (isset($mapped['temperature'])) { $this->SetValue( $this->EnsureFloatVariable($deviceID, $deviceID . '_temperature', 'Temperatur'), (float)$mapped['temperature'] ); } } /* --------------------------------------------------------- * Helper für Variablen * ---------------------------------------------------------*/ private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int { $folderID = $this->GetDeviceFolder($deviceID); // Immer im Ordner suchen foreach (IPS_GetChildrenIDs($folderID) as $cid) { if (IPS_GetObject($cid)['ObjectIdent'] === $ident) { return $cid; } } // Neue Variable anlegen $varID = $this->RegisterVariableBoolean($ident, $name); IPS_SetParent($varID, $folderID); return $varID; } private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int { $folderID = $this->GetDeviceFolder($deviceID); foreach (IPS_GetChildrenIDs($folderID) as $cid) { if (IPS_GetObject($cid)['ObjectIdent'] === $ident) { return $cid; } } $varID = $this->RegisterVariableFloat($ident, $name); IPS_SetParent($varID, $folderID); return $varID; } private function EnsureStringVariable(string $deviceID, string $ident, string $name): int { $folderID = $this->GetDeviceFolder($deviceID); foreach (IPS_GetChildrenIDs($folderID) as $cid) { if (IPS_GetObject($cid)['ObjectIdent'] === $ident) { return $cid; } } $varID = $this->RegisterVariableString($ident, $name); IPS_SetParent($varID, $folderID); return $varID; } private function GetDeviceFolder(string $deviceID): int { $folderIdent = 'folder_' . $deviceID; $folderID = @IPS_GetObjectIDByIdent($folderIdent); if ($folderID !== false) { return $folderID; } $folderID = IPS_CreateCategory(); IPS_SetParent($folderID, $this->InstanceID); IPS_SetName($folderID, $deviceID); IPS_SetIdent($folderID, $folderIdent); return $folderID; } } ?>