189 lines
6.6 KiB
PHP
189 lines
6.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
class VGT_Sub extends IPSModule
|
|
{
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
|
|
// ---------------------------------------------------------
|
|
// CONFIG PROPERTIES (for form.json)
|
|
// ---------------------------------------------------------
|
|
$this->RegisterPropertyString('DeviceID', '');
|
|
|
|
$this->RegisterPropertyInteger('VarPowerProduction', 0);
|
|
$this->RegisterPropertyInteger('VarStateOfCharge', 0);
|
|
$this->RegisterPropertyInteger('VarIsRunning', 0);
|
|
|
|
$this->RegisterPropertyInteger('MinSOC', 20);
|
|
$this->RegisterPropertyInteger('MaxSOC', 80);
|
|
|
|
// ---------------------------------------------------------
|
|
// INTERNAL VARIABLES
|
|
// ---------------------------------------------------------
|
|
$this->RegisterVariableFloat('PowerSetpoint', 'Power Setpoint', '', 20);
|
|
$this->RegisterVariableFloat('PowerSetpointAbs', 'Power Setpoint (ABS)', '', 21);
|
|
$this->RegisterVariableFloat('PowerSetpointPositive', 'Power Setpoint Positive', '', 22);
|
|
$this->RegisterVariableFloat('PowerSetpointNegative', 'Power Setpoint Negative', '', 23);
|
|
|
|
$this->RegisterVariableString('Strategy', 'Strategy', '', 24);
|
|
$this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 25);
|
|
|
|
$this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request Payload', '', 40);
|
|
$this->RegisterVariableString('FeedbackResponsePayload', 'Feedback Response Payload', '', 41);
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
|
|
// MQTT Splitter verbinden
|
|
$this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}');
|
|
|
|
$device = $this->ReadPropertyString('DeviceID');
|
|
if ($device !== '') {
|
|
$this->Subscribe("feedback-request/$device");
|
|
$this->Subscribe("remote-control-request/$device");
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------
|
|
// REQUESTACTION (nur Setzen)
|
|
// ---------------------------------------------------------
|
|
public function RequestAction($Ident, $Value)
|
|
{
|
|
$this->SetValue($Ident, $Value);
|
|
}
|
|
|
|
// ---------------------------------------------------------
|
|
// MQTT SUBSCRIBE
|
|
// ---------------------------------------------------------
|
|
private function Subscribe(string $topic): void
|
|
{
|
|
if (!$this->HasActiveParent()) {
|
|
return;
|
|
}
|
|
|
|
$this->SendDataToParent(json_encode([
|
|
'DataID' => '{A1B5C433-4F17-462D-AD71-383F5BBE4F5A}',
|
|
'Type' => 'SUBSCRIBE',
|
|
'Topic' => $topic
|
|
]));
|
|
}
|
|
|
|
// ---------------------------------------------------------
|
|
// MQTT PUBLISH
|
|
// ---------------------------------------------------------
|
|
private function Publish(string $topic, string $payload): void
|
|
{
|
|
if (!$this->HasActiveParent()) {
|
|
return;
|
|
}
|
|
|
|
$this->SendDataToParent(json_encode([
|
|
'DataID' => '{A1B5C433-4F17-462D-AD71-383F5BBE4F5A}',
|
|
'Type' => 'PUBLISH',
|
|
'Topic' => $topic,
|
|
'Payload' => $payload
|
|
]));
|
|
}
|
|
|
|
// ---------------------------------------------------------
|
|
// RECEIVE MQTT DATA
|
|
// ---------------------------------------------------------
|
|
public function ReceiveData($JSONString)
|
|
{
|
|
$data = json_decode($JSONString, true);
|
|
if (!is_array($data)) return;
|
|
|
|
if (($data['Type'] ?? '') !== 'MESSAGE') return;
|
|
|
|
$topic = $data['Topic'];
|
|
$payload = $data['Payload'];
|
|
$device = $this->ReadPropertyString('DeviceID');
|
|
|
|
if ($device === '') return;
|
|
|
|
// -----------------------------------------------
|
|
// FEEDBACK REQUEST
|
|
// -----------------------------------------------
|
|
if ($topic === "feedback-request/$device") {
|
|
|
|
$this->SetValue('FeedbackRequestPayload', $payload);
|
|
|
|
$reply = $this->BuildFeedbackResponse();
|
|
$json = json_encode($reply);
|
|
|
|
$this->SetValue('FeedbackResponsePayload', $json);
|
|
$this->Publish("feedback-response/$device", $json);
|
|
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 Handling
|
|
if (isset($json['power_setpoint'])) {
|
|
$ps = floatval($json['power_setpoint']);
|
|
$this->SetValue('PowerSetpoint', $ps);
|
|
|
|
$this->SetValue('PowerSetpointAbs', abs($ps));
|
|
|
|
if ($ps > 0) {
|
|
$this->SetValue('PowerSetpointPositive', $ps);
|
|
$this->SetValue('PowerSetpointNegative', 0);
|
|
} elseif ($ps < 0) {
|
|
$this->SetValue('PowerSetpointNegative', $ps);
|
|
$this->SetValue('PowerSetpointPositive', 0);
|
|
} else {
|
|
$this->SetValue('PowerSetpointPositive', 0);
|
|
$this->SetValue('PowerSetpointNegative', 0);
|
|
}
|
|
}
|
|
|
|
// Strategy Handling
|
|
if (isset($json['strategy'])) {
|
|
$this->SetValue('Strategy', $json['strategy']);
|
|
}
|
|
|
|
$this->Publish("remote-control-response/$device", $payload);
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------
|
|
// FEEDBACK RESPONSE BUILDER
|
|
// ---------------------------------------------------------
|
|
private function BuildFeedbackResponse(): array
|
|
{
|
|
$idProd = $this->ReadPropertyInteger('VarPowerProduction');
|
|
$idSOC = $this->ReadPropertyInteger('VarStateOfCharge');
|
|
$idRun = $this->ReadPropertyInteger('VarIsRunning');
|
|
|
|
$min = $this->ReadPropertyInteger('MinSOC');
|
|
$max = $this->ReadPropertyInteger('MaxSOC');
|
|
|
|
$soc = ($idSOC > 0) ? GetValue($idSOC) : 0;
|
|
$isRunning = ($idRun > 0) ? GetValue($idRun) : false;
|
|
|
|
$isReady = ($isRunning && ($soc >= $min) && ($soc <= $max));
|
|
|
|
return [
|
|
'power_production' => ($idProd > 0) ? GetValue($idProd) : 0,
|
|
'is_running' => $isRunning,
|
|
'state_of_charge' => $soc,
|
|
'min_soc' => $min,
|
|
'max_soc' => $max,
|
|
'is_ready' => $isReady
|
|
];
|
|
}
|
|
}
|