Files
Symcon_Belevo_Energiemanage…/VGT_Sub/module.php
2025-11-27 14:29:24 +01:00

190 lines
6.1 KiB
PHP

<?php
declare(strict_types=1);
class VGT_Sub extends IPSModule
{
public function Create()
{
parent::Create();
// Device-ID
$this->RegisterPropertyString('DeviceID', '');
// --- Variablen erstellen ---
$this->RegisterVariableFloat('PowerProduction', 'Power Production', '', 10);
$this->RegisterVariableFloat('StateOfCharge', 'State of Charge', '', 11);
$this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 12);
$this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 13);
$this->RegisterVariableFloat('MinSOC', 'Min SOC', '', 20);
$this->RegisterVariableFloat('MaxSOC', 'Max SOC', '', 21);
// Remote Control Variablen
$this->RegisterVariableFloat('PowerSetpoint', 'Power Setpoint', '', 30);
$this->RegisterVariableString('Strategy', 'Strategy', '', 31);
// Debug / Anzeige
$this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request Payload', '', 90);
$this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 91);
// MQTT Splitter
$this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}');
}
public function ApplyChanges()
{
parent::ApplyChanges();
$this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}');
$dev = $this->ReadPropertyString('DeviceID');
if ($dev !== '') {
$this->Subscribe("feedback-request/$dev");
$this->Subscribe("remote-control-request/$dev");
}
}
public function RequestAction($Ident, $Value)
{
// Variablen schreibbar machen
SetValue($this->GetIDForIdent($Ident), $Value);
return true;
}
/* ---------------------------------------------------------
* CONFIG FORM
* ---------------------------------------------------------*/
public function GetConfigurationForm()
{
return json_encode([
"elements" => [
["type" => "ValidationTextBox", "name" => "DeviceID", "caption" => "Device ID"]
]
]);
}
/* ---------------------------------------------------------
* MQTT Subscribe
* ---------------------------------------------------------*/
private function Subscribe(string $topic)
{
if (!$this->HasActiveParent()) return;
$this->SendDataToParent(json_encode([
"DataID" => "{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}",
"Buffer" => [
"Topic" => $topic,
"QualityOfService" => 0
]
]));
}
private function Publish(string $topic, string $payload)
{
if (!$this->HasActiveParent()) return;
$this->SendDataToParent(json_encode([
"DataID" => "{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}",
"Buffer" => [
"Command" => "PUBLISH",
"Topic" => $topic,
"Payload" => $payload,
"Retain" => false,
"QualityOfService" => 0
]
]));
}
/* ---------------------------------------------------------
* RECEIVE MQTT
* ---------------------------------------------------------*/
public function ReceiveData($JSONString)
{
$data = json_decode($JSONString, true);
if (!isset($data['Type'])) return;
$type = $data['Type'];
$topic = $data['Topic'] ?? '';
$payload = $data['Payload'] ?? '';
$device = $this->ReadPropertyString('DeviceID');
// Beim Connect Topics neu abonnieren
if ($type === "MQTT_CONNECTED") {
if ($device !== '') {
$this->Subscribe("feedback-request/$device");
$this->Subscribe("remote-control-request/$device");
}
return;
}
if ($type !== "MESSAGE") return;
/* ---------------------------------------------------------
* FEEDBACK REQUEST
* ---------------------------------------------------------*/
if ($topic === "feedback-request/$device") {
$this->SetValue('FeedbackRequestPayload', $payload);
// Variablen abrufen
$power = GetValueFloat($this->GetIDForIdent('PowerProduction'));
$soc = GetValueFloat($this->GetIDForIdent('StateOfCharge'));
$run = GetValueBoolean($this->GetIDForIdent('IsRunning'));
$min = GetValueFloat($this->GetIDForIdent('MinSOC'));
$max = GetValueFloat($this->GetIDForIdent('MaxSOC'));
$isReady = ($run && $soc > $min && $soc < $max);
SetValueBoolean($this->GetIDForIdent('IsReady'), $isReady);
// Antwort erstellen
$response = [
"power_production" => $power,
"state_of_charge" => $soc,
"is_running" => $run,
"is_ready" => $isReady,
"min_soc" => $min,
"max_soc" => $max
];
// Publish an feedbackresponse/deviceid
$this->Publish("feedbackresponse/$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;
// power_setpoint → Float-Variable
if (isset($json["power_setpoint"])) {
SetValueFloat(
$this->GetIDForIdent('PowerSetpoint'),
floatval($json["power_setpoint"])
);
}
// strategy → String-Variable
if (isset($json["strategy"])) {
SetValueString(
$this->GetIDForIdent('Strategy'),
strval($json["strategy"])
);
}
return;
}
}
}
?>