101 lines
3.3 KiB
PHP
101 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
class VGT_Sub extends IPSModule
|
|
{
|
|
// Die GUID des IP-Symcon MQTT Clients (Splitter)
|
|
private const PARENT_GUID = '{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}';
|
|
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
// Verbindet sich automatisch mit einem MQTT Client
|
|
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
|
|
|
// Topic Definitionen
|
|
$requestTopic = 'feedback-request/deviceOne';
|
|
|
|
// 1. Filter setzen: Wir wollen nur Nachrichten für unser Request-Topic sehen.
|
|
// Das "Topic" im Filter bezieht sich auf das JSON-Paket vom MQTT Client.
|
|
$this->SetReceiveDataFilter('.*"Topic":"' . preg_quote($requestTopic, '/') . '".*');
|
|
|
|
// 2. Beim MQTT Broker abonnieren (Subscribe senden)
|
|
// Wir prüfen erst, ob wir einen Parent haben, um Fehler zu vermeiden
|
|
if ($this->HasActiveParent()) {
|
|
$this->Subscribe($requestTopic);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Wird aufgerufen, wenn der Parent (MQTT Client) Daten empfängt
|
|
*/
|
|
public function ReceiveData($JSONString)
|
|
{
|
|
$data = json_decode($JSONString, true);
|
|
|
|
$this->SendDebug('ReceiveData', $JSONString, 0);
|
|
|
|
if (!isset($data['Topic']) || !isset($data['Payload'])) {
|
|
return;
|
|
}
|
|
|
|
// Sicherheitscheck: Ist es wirklich das gewünschte Topic?
|
|
if ($data['Topic'] === 'feedback-request/deviceOne') {
|
|
|
|
$this->SendDebug('VGT_Logic', 'Request received. Sending "Hello"...', 0);
|
|
|
|
// Antwort senden
|
|
$this->Publish('feedback-response', 'Hello');
|
|
}
|
|
}
|
|
|
|
/* ---------------------------------------------------------
|
|
* MQTT HILFSFUNKTIONEN
|
|
* ---------------------------------------------------------*/
|
|
|
|
private function Subscribe(string $topic)
|
|
{
|
|
$payload = [
|
|
'DataID' => self::PARENT_GUID,
|
|
'PacketType' => 3, // Achtung: Symcon intern ist Subscribe oft mapped,
|
|
// aber für den MQTT Client ist es Type 3 (Publish) oder spezial.
|
|
// Standard Symcon MQTT Client nutzt jedoch oft Type 8 für Subscribe.
|
|
// Hier nutzen wir den Standard Weg für JSON DataID:
|
|
];
|
|
|
|
// Sende Subscribe Paket (Type 8 = Subscribe beim Client Splitter)
|
|
$this->SendDataToParent(json_encode([
|
|
'DataID' => self::PARENT_GUID,
|
|
'PacketType' => 8,
|
|
'QualityOfService' => 0,
|
|
'Retain' => false,
|
|
'Topic' => $topic,
|
|
'Payload' => ''
|
|
]));
|
|
|
|
$this->SendDebug('MQTT', "Subscribed to $topic", 0);
|
|
}
|
|
|
|
private function Publish(string $topic, string $payload)
|
|
{
|
|
// Sende Publish Paket (Type 3 = Publish)
|
|
$this->SendDataToParent(json_encode([
|
|
'DataID' => self::PARENT_GUID,
|
|
'PacketType' => 3,
|
|
'QualityOfService' => 0,
|
|
'Retain' => false,
|
|
'Topic' => $topic,
|
|
'Payload' => $payload
|
|
]));
|
|
|
|
$this->SendDebug('MQTT', "Published to $topic: $payload", 0);
|
|
}
|
|
}
|
|
?>
|