no message
This commit is contained in:
@@ -3,11 +3,15 @@
|
||||
"name": "Int_VGT",
|
||||
"type": 3,
|
||||
"vendor": "Belevo",
|
||||
"aliases": [
|
||||
"Virtueller Gateway Test"
|
||||
],
|
||||
"prefix": "VGT",
|
||||
"parentRequirements": [
|
||||
"{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}"
|
||||
"{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}"
|
||||
],
|
||||
"childRequirements": [],
|
||||
"implemented": [],
|
||||
"aliases": []
|
||||
"implemented": [
|
||||
"{7F7632D9-FA40-4F38-8DEA-C83CD4325A32}"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -10,88 +10,151 @@ class Int_VGT extends IPSModule
|
||||
|
||||
$this->RegisterPropertyString('DeviceID', '');
|
||||
|
||||
// Speicherung empfangener Remote-Payloads
|
||||
$this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 1);
|
||||
// Variable für empfangene Remote-Control-Daten
|
||||
$this->RegisterVariableString(
|
||||
'RemoteControlPayload',
|
||||
'Remote Control Payload',
|
||||
'',
|
||||
1
|
||||
);
|
||||
|
||||
// Direkte Verbindung zum MQTT-Server herstellen
|
||||
$this->ConnectParent("{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}");
|
||||
// Mit MQTT Server (Splitter) verbinden
|
||||
// GUID: MQTT Server (Splitter) laut Doku
|
||||
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
||||
}
|
||||
|
||||
public function ApplyChanges()
|
||||
{
|
||||
parent::ApplyChanges();
|
||||
|
||||
// Sicherheitshalber erneut verbinden
|
||||
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
||||
|
||||
$device = $this->ReadPropertyString('DeviceID');
|
||||
if ($device === '') {
|
||||
$this->SendDebug('ApplyChanges', 'Kein DeviceID gesetzt – keine Subscriptions', 0);
|
||||
return;
|
||||
}
|
||||
|
||||
// Subscriptions einrichten — wie beim Shelly Modul
|
||||
// Topics:
|
||||
// 1) feedback-request/deviceId
|
||||
// 2) remote-control-response/deviceId
|
||||
|
||||
$this->Subscribe("feedback-request/$device");
|
||||
$this->Subscribe("remote-control-response/$device");
|
||||
}
|
||||
|
||||
private function Subscribe(string $topic)
|
||||
/**
|
||||
* An MQTT-Server (Splitter) subscribe senden.
|
||||
*/
|
||||
private function Subscribe(string $topic): void
|
||||
{
|
||||
$this->SendDebug("Subscribe", "Subscribing to Topic: $topic", 0);
|
||||
$this->SendDebug('Subscribe', "Topic: $topic", 0);
|
||||
|
||||
$packet = [
|
||||
'PacketType' => 8, // SUBSCRIBE
|
||||
'QualityOfService' => 0,
|
||||
'Retain' => false,
|
||||
'Topic' => $topic,
|
||||
'Payload' => '' // bei Subscribe leer
|
||||
];
|
||||
|
||||
$this->SendDataToParent(json_encode([
|
||||
"DataID" => "{04346E0A-99B3-4D6C-8F00-7E7A5C32A54B}", // MQTT_Subscribe
|
||||
"Topic" => $topic
|
||||
]));
|
||||
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' // TX zum MQTT Server
|
||||
] + $packet));
|
||||
}
|
||||
|
||||
private function Publish(string $topic, string $payload)
|
||||
/**
|
||||
* Publish an MQTT-Server (Splitter).
|
||||
*/
|
||||
private function Publish(string $topic, string $payload): void
|
||||
{
|
||||
$this->SendDebug("Publish", "Topic: $topic Payload: $payload", 0);
|
||||
$this->SendDebug('Publish', "Topic: $topic Payload: $payload", 0);
|
||||
|
||||
$packet = [
|
||||
'PacketType' => 3, // PUBLISH
|
||||
'QualityOfService' => 0,
|
||||
'Retain' => false,
|
||||
'Topic' => $topic,
|
||||
'Payload' => $payload
|
||||
];
|
||||
|
||||
$this->SendDataToParent(json_encode([
|
||||
"DataID" => "{04346E0A-99B3-4D6C-8F00-7E7A5C32A54B}", // MQTT_Publish
|
||||
"Topic" => $topic,
|
||||
"Payload" => $payload
|
||||
]));
|
||||
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' // TX zum MQTT Server
|
||||
] + $packet));
|
||||
}
|
||||
|
||||
/**
|
||||
* Wird vom MQTT-Server (Splitter) aufgerufen.
|
||||
*/
|
||||
public function ReceiveData($JSONString)
|
||||
{
|
||||
$data = json_decode($JSONString, true);
|
||||
$this->SendDebug('ReceiveData RAW', $JSONString, 0);
|
||||
|
||||
$topic = $data['Topic'] ?? '';
|
||||
$message = $data['Message'] ?? '';
|
||||
$data = json_decode($JSONString, true);
|
||||
if (!is_array($data)) {
|
||||
$this->SendDebug('ReceiveData', 'JSON decode fehlgeschlagen', 0);
|
||||
return;
|
||||
}
|
||||
|
||||
// Struktur laut Doku:
|
||||
// {
|
||||
// "DataID": "{7F7632D9-FA40-4F38-8DEA-C83CD4325A32}",
|
||||
// "PacketType": ...,
|
||||
// "QualityOfService": 0,
|
||||
// "Retain": false,
|
||||
// "Topic": "....",
|
||||
// "Payload": "...."
|
||||
// }
|
||||
$topic = $data['Topic'] ?? '';
|
||||
$payload = $data['Payload'] ?? '';
|
||||
|
||||
$device = $this->ReadPropertyString('DeviceID');
|
||||
if ($device === '') {
|
||||
$this->SendDebug('ReceiveData', 'Keine DeviceID gesetzt, ignoriere Paket', 0);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->SendDebug("Receive", "Topic: $topic Message: $message", 0);
|
||||
$this->SendDebug('ReceiveData', "Topic: $topic Payload: $payload", 0);
|
||||
|
||||
/** 1️⃣ FEEDBACK REQUEST → Starre JSON Antwort senden */
|
||||
// 1️⃣ feedback-request/deviceId → fixe JSON-Antwort
|
||||
if ($topic === "feedback-request/$device") {
|
||||
$this->SendDebug('Logic', 'Feedback-Request erkannt', 0);
|
||||
|
||||
$response = [
|
||||
"power_production" => 123,
|
||||
"is_ready" => true,
|
||||
"is_running" => true,
|
||||
"state_of_charge" => 50,
|
||||
"min_soc" => 10,
|
||||
"max_soc" => 90
|
||||
'power_production' => 123,
|
||||
'is_ready' => true,
|
||||
'is_running' => true,
|
||||
'state_of_charge' => 50,
|
||||
'min_soc' => 10,
|
||||
'max_soc' => 90
|
||||
];
|
||||
|
||||
$this->Publish("feedback-response/$device", json_encode($response));
|
||||
return;
|
||||
}
|
||||
|
||||
/** 2️⃣ Remote-Control-Response empfangen */
|
||||
// 2️⃣ remote-control-response/deviceId → speichern + Callback
|
||||
if ($topic === "remote-control-response/$device") {
|
||||
$this->SendDebug('Logic', 'Remote-Control-Response erkannt', 0);
|
||||
|
||||
$this->SetValue('RemoteControlPayload', $message);
|
||||
|
||||
$this->HandleRemoteControl($message);
|
||||
$this->SetValue('RemoteControlPayload', $payload);
|
||||
$this->HandleRemoteControl($payload);
|
||||
return;
|
||||
}
|
||||
|
||||
// Optional: andere Topics debuggen
|
||||
$this->SendDebug('ReceiveData', "Unbekanntes Topic empfangen: $topic", 0);
|
||||
}
|
||||
|
||||
protected function HandleRemoteControl(string $payload)
|
||||
/**
|
||||
* Hook für eigene Logik bei Remote-Control-Payload.
|
||||
*/
|
||||
protected function HandleRemoteControl(string $payload): void
|
||||
{
|
||||
$this->SendDebug("RemoteControl", "Payload received: $payload", 0);
|
||||
$this->SendDebug('HandleRemoteControl', "Payload: $payload", 0);
|
||||
|
||||
// → Hier kannst du später Custom Logic einbauen
|
||||
// Hier kannst du später deine eigene Logik ergänzen
|
||||
// z.B. json_decode($payload, true) und darauf reagieren.
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user