149 lines
5.3 KiB
PHP
149 lines
5.3 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
class VGT_Sub extends IPSModule
|
||
{
|
||
public function Create()
|
||
{
|
||
parent::Create();
|
||
|
||
$this->RegisterPropertyString("DeviceID", "");
|
||
|
||
// --- Variablen für Feedback ---
|
||
$this->RegisterVariableFloat("PowerProduction", "Power Production", "", 10);
|
||
$this->RegisterVariableBoolean("IsReady", "Is Ready", "", 11);
|
||
$this->RegisterVariableBoolean("IsRunning", "Is Running", "", 12);
|
||
$this->RegisterVariableFloat("StateOfCharge", "State of Charge", "", 13);
|
||
$this->RegisterVariableFloat("MinSOC", "Min SOC", "", 14);
|
||
$this->RegisterVariableFloat("MaxSOC", "Max SOC", "", 15);
|
||
|
||
// --- Variablen für Remote Control ---
|
||
$this->RegisterVariableFloat("PowerSetpoint", "Power Setpoint", "", 20);
|
||
$this->RegisterVariableString("Strategy", "Strategy", "", 21);
|
||
|
||
// Debug-Variablen
|
||
$this->RegisterVariableString("FeedbackRequestPayload", "Feedback Request", "", 90);
|
||
$this->RegisterVariableString("RemoteControlPayload", "Remote Control Request", "", 91);
|
||
|
||
// MQTT Splitter (NUR dieser ist korrekt)
|
||
$this->ConnectParent("{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}");
|
||
}
|
||
|
||
public function ApplyChanges()
|
||
{
|
||
parent::ApplyChanges();
|
||
$this->ConnectParent("{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}");
|
||
}
|
||
|
||
public function RequestAction($Ident, $Value)
|
||
{
|
||
SetValue($this->GetIDForIdent($Ident), $Value);
|
||
}
|
||
|
||
|
||
/* =====================================================================================
|
||
* MQTT Funktionen
|
||
* ===================================================================================== */
|
||
|
||
private function Subscribe(string $topic)
|
||
{
|
||
$this->SendDataToParent(json_encode([
|
||
"DataID" => "{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}",
|
||
"Buffer" => [
|
||
"Topic" => $topic,
|
||
"QualityOfService" => 0
|
||
]
|
||
]));
|
||
}
|
||
|
||
private function Publish(string $topic, string $payload)
|
||
{
|
||
$this->SendDataToParent(json_encode([
|
||
"DataID" => "{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}",
|
||
"Buffer" => [
|
||
"Command" => "PUBLISH",
|
||
"Topic" => $topic,
|
||
"Payload" => $payload,
|
||
"Retain" => false,
|
||
"QualityOfService" => 0
|
||
]
|
||
]));
|
||
}
|
||
|
||
|
||
/* =====================================================================================
|
||
* ReceiveData – erkennt NUR das tatsächliche Splitter-Format
|
||
* ===================================================================================== */
|
||
public function ReceiveData($JSONString)
|
||
{
|
||
$data = json_decode($JSONString, true);
|
||
if (!isset($data["Buffer"])) return;
|
||
|
||
$buf = $data["Buffer"];
|
||
|
||
/* CONNECTED? → Dann subscriben */
|
||
if (isset($buf["Message"]) && $buf["Message"] === "MQTT_CONNECTED") {
|
||
|
||
$dev = $this->ReadPropertyString("DeviceID");
|
||
if ($dev) {
|
||
$this->Subscribe("feedback-request/$dev");
|
||
$this->Subscribe("remote-control-request/$dev");
|
||
}
|
||
return;
|
||
}
|
||
|
||
/* Normale MQTT Message */
|
||
if (!isset($buf["Topic"]) || !isset($buf["Payload"])) return;
|
||
|
||
$topic = $buf["Topic"];
|
||
$payload = $buf["Payload"];
|
||
|
||
$device = $this->ReadPropertyString("DeviceID");
|
||
|
||
/* =====================================================================================
|
||
* FEEDBACK REQUEST
|
||
* ===================================================================================== */
|
||
if ($topic === "feedback-request/$device") {
|
||
|
||
$this->SetValue("FeedbackRequestPayload", $payload);
|
||
|
||
$response = [
|
||
"power_production" => GetValueFloat($this->GetIDForIdent("PowerProduction")),
|
||
"is_ready" => GetValueBoolean($this->GetIDForIdent("IsReady")),
|
||
"is_running" => GetValueBoolean($this->GetIDForIdent("IsRunning")),
|
||
"state_of_charge" => GetValueFloat($this->GetIDForIdent("StateOfCharge")),
|
||
"min_soc" => GetValueFloat($this->GetIDForIdent("MinSOC")),
|
||
"max_soc" => GetValueFloat($this->GetIDForIdent("MaxSOC")),
|
||
];
|
||
|
||
$this->Publish("feedback-response/$device", json_encode($response));
|
||
return;
|
||
}
|
||
|
||
/* =====================================================================================
|
||
* REMOTE CONTROL REQUEST
|
||
* ===================================================================================== */
|
||
if ($topic === "remote-control-request/$device") {
|
||
|
||
$this->SetValue("RemoteControlPayload", $payload);
|
||
|
||
$json = json_decode($payload, true);
|
||
if (!is_array($json)) return;
|
||
|
||
if (isset($json["power_setpoint"])) {
|
||
SetValueFloat($this->GetIDForIdent("PowerSetpoint"), floatval($json["power_setpoint"]));
|
||
}
|
||
|
||
if (isset($json["strategy"])) {
|
||
SetValueString($this->GetIDForIdent("Strategy"), strval($json["strategy"]));
|
||
}
|
||
|
||
$this->Publish("remote-control-response/$device", json_encode($json));
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
|
||
?>
|