Files
Symcon_Belevo_Energiemanage…/VGT_Sub/module.php
2025-11-28 07:07:03 +01:00

172 lines
5.3 KiB
PHP

<?php
declare(strict_types=1);
class VGT_Sub extends IPSModule
{
public function Create()
{
parent::Create();
// Device-ID
$this->RegisterPropertyString("DeviceID", "");
// 6 editable values (response payload)
$this->RegisterVariableFloat("PowerProduction", "Power Production", "", 10);
$this->EnableAction("PowerProduction");
$this->RegisterVariableFloat("StateOfCharge", "State of Charge", "", 20);
$this->EnableAction("StateOfCharge");
$this->RegisterVariableBoolean("IsRunning", "Is Running", "", 30);
$this->EnableAction("IsRunning");
$this->RegisterVariableBoolean("IsReady", "Is Ready", "", 40);
$this->EnableAction("IsReady");
$this->RegisterVariableInteger("MinSOC", "Min SOC", "", 50);
$this->EnableAction("MinSOC");
$this->RegisterVariableInteger("MaxSOC", "Max SOC", "", 60);
$this->EnableAction("MaxSOC");
// Remote control readonly values
$this->RegisterVariableFloat("RC_PowerSetpoint", "RC Power Setpoint", "", 70);
$this->RegisterVariableString("RC_Strategy", "RC Strategy", "", 80);
// Attach to MQTT Client Splitter
$this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}');
}
public function ApplyChanges()
{
parent::ApplyChanges();
// Reconnect parent
$this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}');
$device = $this->ReadPropertyString("DeviceID");
// Required incoming topics
$topics = [
"feedback-request/" . $device,
"remote-control-request/" . $device
];
// Subscribe each topic using the CORRECT DataID
foreach ($topics as $t) {
$this->SendDataToParent(json_encode([
"DataID" => "{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}", // <- correct for subscribe
"Topic" => $t,
"Retain" => false
]));
}
}
public function RequestAction($Ident, $Value)
{
SetValue($this->GetIDForIdent($Ident), $Value);
}
public function ReceiveData($JSONString)
{
$data = json_decode($JSONString);
// This format is correct for your MQTT client splitter:
// { "Topic": "...", "Payload": "..." }
if (!isset($data->Topic) || !isset($data->Payload)) {
return;
}
$topic = $data->Topic;
$payload = $data->Payload;
$device = $this->ReadPropertyString("DeviceID");
if ($topic === "feedback-request/" . $device) {
$this->HandleFeedbackRequest();
}
if ($topic === "remote-control-request/" . $device) {
$this->HandleRemoteControlRequest($payload);
}
}
// ----------------------------------------------------------------------------
// FEEDBACK REQUEST → SEND STATE JSON
// ----------------------------------------------------------------------------
private function HandleFeedbackRequest()
{
$response = [
"power_production" => GetValue($this->GetIDForIdent("PowerProduction")),
"state_of_charge" => GetValue($this->GetIDForIdent("StateOfCharge")),
"is_running" => GetValue($this->GetIDForIdent("IsRunning")),
"is_ready" => GetValue($this->GetIDForIdent("IsReady")),
"min_soc" => GetValue($this->GetIDForIdent("MinSOC")),
"max_soc" => GetValue($this->GetIDForIdent("MaxSOC"))
];
$topic = "feedback-response/" . $this->ReadPropertyString("DeviceID");
$this->SendToMQTT($topic, json_encode($response));
}
// ----------------------------------------------------------------------------
// REMOTE CONTROL REQUEST → WRITE VALUES + RESPOND
// ----------------------------------------------------------------------------
private function HandleRemoteControlRequest(string $payload)
{
$data = json_decode($payload, true);
if (!is_array($data)) {
return;
}
if (isset($data["power_setpoint"])) {
SetValue($this->GetIDForIdent("RC_PowerSetpoint"), $data["power_setpoint"]);
}
if (isset($data["strategy"])) {
SetValue($this->GetIDForIdent("RC_Strategy"), $data["strategy"]);
}
// Respond back
$topic = "remote-control-response/" . $this->ReadPropertyString("DeviceID");
$this->SendToMQTT($topic, json_encode($data));
}
// ----------------------------------------------------------------------------
// MQTT PUBLISH (correct format for your splitter!)
// ----------------------------------------------------------------------------
private function SendToMQTT(string $topic, string $payload)
{
$this->SendDataToParent(json_encode([
"DataID" => "{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}", // CORRECT!
"Topic" => $topic,
"Payload" => $payload,
"Retain" => false
]));
}
public function GetConfigurationForm()
{
return json_encode([
"elements" => [
[
"type" => "ValidationTextBox",
"name" => "DeviceID",
"caption" => "Device ID"
]
]
]);
}
}
?>