173 lines
5.7 KiB
PHP
173 lines
5.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
class VGT_Sub extends IPSModule
|
|
{
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
|
|
// Device
|
|
$this->RegisterPropertyString('DeviceID', '');
|
|
|
|
// --- Variablen anlegen ---
|
|
$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);
|
|
|
|
$this->RegisterVariableFloat('PowerSetpoint', 'Power Setpoint', '', 30);
|
|
$this->RegisterVariableString('Strategy', 'Strategy', '', 31);
|
|
|
|
$this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request', '', 80);
|
|
$this->RegisterVariableString('RemoteControlPayload', 'Remote Control Request', '', 81);
|
|
|
|
// MQTT Splitter
|
|
$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);
|
|
}
|
|
|
|
public function GetConfigurationForm()
|
|
{
|
|
return json_encode([
|
|
"elements" => [
|
|
[ "type" => "ValidationTextBox", "name" => "DeviceID", "caption" => "Device ID" ]
|
|
]
|
|
]);
|
|
}
|
|
|
|
/* =========================================================
|
|
* MQTT Subscribe (kompatibel zu MQTT-Splitter)
|
|
* ========================================================= */
|
|
private function Subscribe(string $topic)
|
|
{
|
|
if (!$this->HasActiveParent()) return;
|
|
|
|
$this->SendDataToParent(json_encode([
|
|
"DataID" => "{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}",
|
|
"Buffer" => [
|
|
"Topic" => $topic,
|
|
"QualityOfService" => 0
|
|
]
|
|
]));
|
|
}
|
|
|
|
/* =========================================================
|
|
* MQTT Publish (kompatibel zu MQTT-Splitter)
|
|
* ========================================================= */
|
|
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
|
|
]
|
|
]));
|
|
}
|
|
|
|
/* =========================================================
|
|
* MQTT RECEIVE
|
|
* ========================================================= */
|
|
public function ReceiveData($JSONString)
|
|
{
|
|
$data = json_decode($JSONString, true);
|
|
if (!isset($data['Buffer'])) return;
|
|
|
|
$buf = $data['Buffer'];
|
|
$device = $this->ReadPropertyString('DeviceID');
|
|
|
|
// Wenn der MQTT-Splitter ready ist → abonnieren
|
|
if (isset($buf['Message']) && $buf['Message'] === "MQTT_CONNECTED") {
|
|
if ($device !== '') {
|
|
$this->Subscribe("feedback-request/$device");
|
|
$this->Subscribe("remote-control-request/$device");
|
|
}
|
|
return;
|
|
}
|
|
|
|
// echte MQTT Nachrichten
|
|
if (!isset($buf['Topic']) || !isset($buf['Payload'])) return;
|
|
|
|
$topic = $buf['Topic'];
|
|
$payload = $buf['Payload'];
|
|
|
|
/* ---------------------------------------------------------
|
|
* FEEDBACK REQUEST
|
|
* --------------------------------------------------------- */
|
|
if ($topic === "feedback-request/$device") {
|
|
|
|
$this->SetValue('FeedbackRequestPayload', $payload);
|
|
|
|
$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);
|
|
|
|
$response = [
|
|
"power_production" => $power,
|
|
"state_of_charge" => $soc,
|
|
"is_running" => $run,
|
|
"is_ready" => $isReady,
|
|
"min_soc" => $min,
|
|
"max_soc" => $max
|
|
];
|
|
|
|
$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;
|
|
|
|
if (isset($json["power_setpoint"])) {
|
|
SetValueFloat(
|
|
$this->GetIDForIdent('PowerSetpoint'),
|
|
floatval($json["power_setpoint"])
|
|
);
|
|
}
|
|
|
|
if (isset($json["strategy"])) {
|
|
SetValueString(
|
|
$this->GetIDForIdent('Strategy'),
|
|
strval($json["strategy"])
|
|
);
|
|
}
|
|
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|