214 lines
7.1 KiB
PHP
214 lines
7.1 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
class VGT_Sub extends IPSModule
|
||
{
|
||
public function Create()
|
||
{
|
||
parent::Create();
|
||
|
||
$this->RegisterPropertyString('DeviceID', '');
|
||
|
||
/** -------------------------
|
||
* STATUS Variablen
|
||
* ------------------------- */
|
||
$this->RegisterVariableInteger('PowerProduction', 'Power Production', '', 10);
|
||
$this->EnableAction('PowerProduction');
|
||
|
||
$this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 11);
|
||
$this->EnableAction('IsReady');
|
||
|
||
$this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 12);
|
||
$this->EnableAction('IsRunning');
|
||
|
||
$this->RegisterVariableInteger('StateOfCharge', 'State of Charge', '', 13);
|
||
$this->EnableAction('StateOfCharge');
|
||
|
||
$this->RegisterVariableInteger('MinSOC', 'Min SOC', '', 14);
|
||
$this->EnableAction('MinSOC');
|
||
|
||
$this->RegisterVariableInteger('MaxSOC', 'Max SOC', '', 15);
|
||
$this->EnableAction('MaxSOC');
|
||
|
||
/** -------------------------
|
||
* REMOTE CONTROL
|
||
* ------------------------- */
|
||
$this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 20);
|
||
$this->RegisterVariableString('Strategy', 'Strategy', '', 21);
|
||
|
||
$this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 30);
|
||
|
||
/** -------------------------
|
||
* FEEDBACK REQUEST
|
||
* ------------------------- */
|
||
$this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request Payload', '', 40);
|
||
}
|
||
|
||
public function ApplyChanges()
|
||
{
|
||
parent::ApplyChanges();
|
||
$this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}'); // Dein Splitter
|
||
}
|
||
|
||
/** ----------------------------------------------------------
|
||
* REQUEST ACTION
|
||
* ----------------------------------------------------------*/
|
||
public function RequestAction($Ident, $Value)
|
||
{
|
||
$this->SetValue($Ident, $Value);
|
||
|
||
$device = $this->ReadPropertyString('DeviceID');
|
||
if ($device === '') {
|
||
return;
|
||
}
|
||
|
||
switch ($Ident) {
|
||
case 'PowerProduction':
|
||
case 'IsReady':
|
||
case 'IsRunning':
|
||
case 'StateOfCharge':
|
||
case 'MinSOC':
|
||
case 'MaxSOC':
|
||
|
||
$payload = json_encode([
|
||
'power_production' => $this->GetValue('PowerProduction'),
|
||
'is_ready' => $this->GetValue('IsReady'),
|
||
'is_running' => $this->GetValue('IsRunning'),
|
||
'state_of_charge' => $this->GetValue('StateOfCharge'),
|
||
'min_soc' => $this->GetValue('MinSOC'),
|
||
'max_soc' => $this->GetValue('MaxSOC')
|
||
]);
|
||
|
||
$this->Publish("status/$device", $payload);
|
||
break;
|
||
}
|
||
}
|
||
|
||
/** ----------------------------------------------------------
|
||
* SUBSCRIBE
|
||
* ----------------------------------------------------------*/
|
||
private function Subscribe(string $topic): void
|
||
{
|
||
if (!$this->HasActiveParent()) {
|
||
$this->SendDebug("MQTT", "Parent offline – subscribe skipped: $topic", 0);
|
||
return;
|
||
}
|
||
|
||
$this->SendDataToParent(json_encode([
|
||
'DataID' => '{A1B5C433-4F17-462D-AD71-383F5BBE4F5A}', // DataID deines Splitters → SUBSCRIBE
|
||
'Type' => 'SUBSCRIBE',
|
||
'Topic' => $topic
|
||
]));
|
||
|
||
$this->SendDebug('MQTT', "Subscribed: $topic", 0);
|
||
}
|
||
|
||
/** ----------------------------------------------------------
|
||
* PUBLISH
|
||
* ----------------------------------------------------------*/
|
||
private function Publish(string $topic, string $payload): void
|
||
{
|
||
if (!$this->HasActiveParent()) {
|
||
$this->SendDebug("MQTT", "Parent offline – publish skipped: $topic", 0);
|
||
return;
|
||
}
|
||
|
||
$this->SendDataToParent(json_encode([
|
||
'DataID' => '{A1B5C433-4F17-462D-AD71-383F5BBE4F5A}', // DataID deines Splitters → PUBLISH
|
||
'Type' => 'PUBLISH',
|
||
'Topic' => $topic,
|
||
'Payload' => $payload
|
||
]));
|
||
|
||
$this->SendDebug('MQTT', "Publish → $topic : $payload", 0);
|
||
}
|
||
|
||
/** ----------------------------------------------------------
|
||
* RECEIVE DATA
|
||
* ----------------------------------------------------------*/
|
||
public function ReceiveData($JSONString)
|
||
{
|
||
$data = json_decode($JSONString, true);
|
||
|
||
if (!is_array($data)) {
|
||
return;
|
||
}
|
||
|
||
/** --- MQTT CONNECTED EVENT vom Splitter --- */
|
||
if (($data['Type'] ?? '') === 'MQTT_CONNECTED') {
|
||
$this->SendDebug("MQTT", "Splitter connected → re-subscribing", 0);
|
||
|
||
$device = $this->ReadPropertyString('DeviceID');
|
||
|
||
if ($device !== '') {
|
||
$this->Subscribe("feedback-request/$device");
|
||
$this->Subscribe("remote-control-request/$device");
|
||
}
|
||
return;
|
||
}
|
||
|
||
/** --- Normale MQTT Daten vom Splitter --- */
|
||
if (($data['Type'] ?? '') !== 'MESSAGE') {
|
||
return;
|
||
}
|
||
|
||
$topic = $data['Topic'] ?? '';
|
||
$payload = $data['Payload'] ?? '';
|
||
|
||
$device = $this->ReadPropertyString('DeviceID');
|
||
|
||
$this->SendDebug("MQTT", "RX → $topic : $payload", 0);
|
||
|
||
if ($device === '') {
|
||
return;
|
||
}
|
||
|
||
/** -------------------------------------------
|
||
* FEEDBACK REQUEST
|
||
* ------------------------------------------*/
|
||
if ($topic === "feedback-request/$device") {
|
||
|
||
$this->SetValue('FeedbackRequestPayload', $payload);
|
||
|
||
$json = json_decode($payload, true);
|
||
if (!is_array($json)) {
|
||
$json = [];
|
||
}
|
||
|
||
$response = array_merge($json, [
|
||
"power_production" => $this->GetValue('PowerProduction'),
|
||
"is_ready" => $this->GetValue('IsReady'),
|
||
"is_running" => $this->GetValue('IsRunning'),
|
||
"state_of_charge" => $this->GetValue('StateOfCharge'),
|
||
"min_soc" => $this->GetValue('MinSOC'),
|
||
"max_soc" => $this->GetValue('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)) {
|
||
if (isset($json['power_setpoint'])) {
|
||
$this->SetValue('PowerSetpoint', (int)$json['power_setpoint']);
|
||
}
|
||
if (isset($json['strategy'])) {
|
||
$this->SetValue('Strategy', (string)$json['strategy']);
|
||
}
|
||
}
|
||
|
||
$this->Publish("remote-control-response/$device", $payload);
|
||
}
|
||
}
|
||
}
|