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

98 lines
2.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.
<?php
declare(strict_types=1);
class Int_VGT extends IPSModule
{
public function Create()
{
parent::Create();
$this->RegisterPropertyString('DeviceID', '');
// Speicherung empfangener Remote-Payloads
$this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 1);
// Direkte Verbindung zum MQTT-Server herstellen
$this->ConnectParent("{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}");
}
public function ApplyChanges()
{
parent::ApplyChanges();
$device = $this->ReadPropertyString('DeviceID');
if ($device === '') {
return;
}
// Subscriptions einrichten — wie beim Shelly Modul
$this->Subscribe("feedback-request/$device");
$this->Subscribe("remote-control-response/$device");
}
private function Subscribe(string $topic)
{
$this->SendDebug("Subscribe", "Subscribing to Topic: $topic", 0);
$this->SendDataToParent(json_encode([
"DataID" => "{04346E0A-99B3-4D6C-8F00-7E7A5C32A54B}", // MQTT_Subscribe
"Topic" => $topic
]));
}
private function Publish(string $topic, string $payload)
{
$this->SendDebug("Publish", "Topic: $topic Payload: $payload", 0);
$this->SendDataToParent(json_encode([
"DataID" => "{04346E0A-99B3-4D6C-8F00-7E7A5C32A54B}", // MQTT_Publish
"Topic" => $topic,
"Payload" => $payload
]));
}
public function ReceiveData($JSONString)
{
$data = json_decode($JSONString, true);
$topic = $data['Topic'] ?? '';
$message = $data['Message'] ?? '';
$device = $this->ReadPropertyString('DeviceID');
$this->SendDebug("Receive", "Topic: $topic Message: $message", 0);
/** 1⃣ FEEDBACK REQUEST → Starre JSON Antwort senden */
if ($topic === "feedback-request/$device") {
$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 empfangen */
if ($topic === "remote-control-response/$device") {
$this->SetValue('RemoteControlPayload', $message);
$this->HandleRemoteControl($message);
}
}
protected function HandleRemoteControl(string $payload)
{
$this->SendDebug("RemoteControl", "Payload received: $payload", 0);
// → Hier kannst du später Custom Logic einbauen
}
}