Files
Symcon_Belevo_Energiemanage…/Int_VGT/module.php
2025-11-13 09:54:49 +01:00

161 lines
4.8 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
class Int_VGT extends IPSModule
{
public function Create()
{
parent::Create();
$this->RegisterPropertyString('DeviceID', '');
// Variable für empfangene Remote-Control-Daten
$this->RegisterVariableString(
'RemoteControlPayload',
'Remote Control Payload',
'',
1
);
// 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;
}
// Topics:
// 1) feedback-request/deviceId
// 2) remote-control-response/deviceId
$this->Subscribe("feedback-request/$device");
$this->Subscribe("remote-control-response/$device");
}
/**
* An MQTT-Server (Splitter) subscribe senden.
*/
private function Subscribe(string $topic): void
{
$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' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' // TX zum MQTT Server
] + $packet));
}
/**
* Publish an MQTT-Server (Splitter).
*/
private function Publish(string $topic, string $payload): void
{
$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' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' // TX zum MQTT Server
] + $packet));
}
/**
* Wird vom MQTT-Server (Splitter) aufgerufen.
*/
public function ReceiveData($JSONString)
{
$this->SendDebug('ReceiveData RAW', $JSONString, 0);
$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('ReceiveData', "Topic: $topic Payload: $payload", 0);
// 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
];
$this->Publish("feedback-response/$device", json_encode($response));
return;
}
// 2⃣ remote-control-response/deviceId → speichern + Callback
if ($topic === "remote-control-response/$device") {
$this->SendDebug('Logic', 'Remote-Control-Response erkannt', 0);
$this->SetValue('RemoteControlPayload', $payload);
$this->HandleRemoteControl($payload);
return;
}
// Optional: andere Topics debuggen
$this->SendDebug('ReceiveData', "Unbekanntes Topic empfangen: $topic", 0);
}
/**
* Hook für eigene Logik bei Remote-Control-Payload.
*/
protected function HandleRemoteControl(string $payload): void
{
$this->SendDebug('HandleRemoteControl', "Payload: $payload", 0);
// Hier kannst du später deine eigene Logik ergänzen
// z.B. json_decode($payload, true) und darauf reagieren.
}
}