no message
This commit is contained in:
+7
-3
@@ -3,11 +3,15 @@
|
|||||||
"name": "Int_VGT",
|
"name": "Int_VGT",
|
||||||
"type": 3,
|
"type": 3,
|
||||||
"vendor": "Belevo",
|
"vendor": "Belevo",
|
||||||
|
"aliases": [
|
||||||
|
"Virtueller Gateway Test"
|
||||||
|
],
|
||||||
"prefix": "VGT",
|
"prefix": "VGT",
|
||||||
"parentRequirements": [
|
"parentRequirements": [
|
||||||
"{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}"
|
"{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}"
|
||||||
],
|
],
|
||||||
"childRequirements": [],
|
"childRequirements": [],
|
||||||
"implemented": [],
|
"implemented": [
|
||||||
"aliases": []
|
"{7F7632D9-FA40-4F38-8DEA-C83CD4325A32}"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
+97
-34
@@ -10,88 +10,151 @@ class Int_VGT extends IPSModule
|
|||||||
|
|
||||||
$this->RegisterPropertyString('DeviceID', '');
|
$this->RegisterPropertyString('DeviceID', '');
|
||||||
|
|
||||||
// Speicherung empfangener Remote-Payloads
|
// Variable für empfangene Remote-Control-Daten
|
||||||
$this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 1);
|
$this->RegisterVariableString(
|
||||||
|
'RemoteControlPayload',
|
||||||
|
'Remote Control Payload',
|
||||||
|
'',
|
||||||
|
1
|
||||||
|
);
|
||||||
|
|
||||||
// Direkte Verbindung zum MQTT-Server herstellen
|
// Mit MQTT Server (Splitter) verbinden
|
||||||
$this->ConnectParent("{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}");
|
// GUID: MQTT Server (Splitter) laut Doku
|
||||||
|
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function ApplyChanges()
|
public function ApplyChanges()
|
||||||
{
|
{
|
||||||
parent::ApplyChanges();
|
parent::ApplyChanges();
|
||||||
|
|
||||||
|
// Sicherheitshalber erneut verbinden
|
||||||
|
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
||||||
|
|
||||||
$device = $this->ReadPropertyString('DeviceID');
|
$device = $this->ReadPropertyString('DeviceID');
|
||||||
if ($device === '') {
|
if ($device === '') {
|
||||||
|
$this->SendDebug('ApplyChanges', 'Kein DeviceID gesetzt – keine Subscriptions', 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subscriptions einrichten — wie beim Shelly Modul
|
// Topics:
|
||||||
|
// 1) feedback-request/deviceId
|
||||||
|
// 2) remote-control-response/deviceId
|
||||||
|
|
||||||
$this->Subscribe("feedback-request/$device");
|
$this->Subscribe("feedback-request/$device");
|
||||||
$this->Subscribe("remote-control-response/$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([
|
$this->SendDataToParent(json_encode([
|
||||||
"DataID" => "{04346E0A-99B3-4D6C-8F00-7E7A5C32A54B}", // MQTT_Subscribe
|
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' // TX zum MQTT Server
|
||||||
"Topic" => $topic
|
] + $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([
|
$this->SendDataToParent(json_encode([
|
||||||
"DataID" => "{04346E0A-99B3-4D6C-8F00-7E7A5C32A54B}", // MQTT_Publish
|
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' // TX zum MQTT Server
|
||||||
"Topic" => $topic,
|
] + $packet));
|
||||||
"Payload" => $payload
|
|
||||||
]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wird vom MQTT-Server (Splitter) aufgerufen.
|
||||||
|
*/
|
||||||
public function ReceiveData($JSONString)
|
public function ReceiveData($JSONString)
|
||||||
{
|
{
|
||||||
$data = json_decode($JSONString, true);
|
$this->SendDebug('ReceiveData RAW', $JSONString, 0);
|
||||||
|
|
||||||
$topic = $data['Topic'] ?? '';
|
$data = json_decode($JSONString, true);
|
||||||
$message = $data['Message'] ?? '';
|
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');
|
$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") {
|
if ($topic === "feedback-request/$device") {
|
||||||
|
$this->SendDebug('Logic', 'Feedback-Request erkannt', 0);
|
||||||
|
|
||||||
$response = [
|
$response = [
|
||||||
"power_production" => 123,
|
'power_production' => 123,
|
||||||
"is_ready" => true,
|
'is_ready' => true,
|
||||||
"is_running" => true,
|
'is_running' => true,
|
||||||
"state_of_charge" => 50,
|
'state_of_charge' => 50,
|
||||||
"min_soc" => 10,
|
'min_soc' => 10,
|
||||||
"max_soc" => 90
|
'max_soc' => 90
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->Publish("feedback-response/$device", json_encode($response));
|
$this->Publish("feedback-response/$device", json_encode($response));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 2️⃣ Remote-Control-Response empfangen */
|
// 2️⃣ remote-control-response/deviceId → speichern + Callback
|
||||||
if ($topic === "remote-control-response/$device") {
|
if ($topic === "remote-control-response/$device") {
|
||||||
|
$this->SendDebug('Logic', 'Remote-Control-Response erkannt', 0);
|
||||||
|
|
||||||
$this->SetValue('RemoteControlPayload', $message);
|
$this->SetValue('RemoteControlPayload', $payload);
|
||||||
|
$this->HandleRemoteControl($payload);
|
||||||
$this->HandleRemoteControl($message);
|
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