98 lines
2.8 KiB
PHP
98 lines
2.8 KiB
PHP
<?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
|
||
}
|
||
}
|