194 lines
6.0 KiB
PHP
194 lines
6.0 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
class Int_VGT extends IPSModule
|
||
{
|
||
public function Create()
|
||
{
|
||
parent::Create();
|
||
|
||
$this->RegisterPropertyString('DeviceID', '');
|
||
|
||
|
||
/** -------------------------------------------
|
||
* STATUS Variablen (schreibbar)
|
||
* ------------------------------------------*/
|
||
|
||
$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 Variablen (read-only)
|
||
* ------------------------------------------*/
|
||
|
||
$this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 20);
|
||
$this->RegisterVariableString('Strategy', 'Strategy', '', 21);
|
||
|
||
// Für Debugging
|
||
$this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 30);
|
||
|
||
|
||
/** -------------------------------------------
|
||
* MQTT SERVER verbinden
|
||
* ------------------------------------------*/
|
||
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
||
}
|
||
|
||
|
||
public function ApplyChanges()
|
||
{
|
||
parent::ApplyChanges();
|
||
|
||
// Sicherheitshalber erneut verbinden
|
||
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
||
|
||
$device = $this->ReadPropertyString('DeviceID');
|
||
if ($device === '') {
|
||
$this->SendDebug('ApplyChanges', 'Keine DeviceID gesetzt', 0);
|
||
return;
|
||
}
|
||
|
||
// Subscriptions setzen
|
||
$this->Subscribe("feedback-request/$device");
|
||
$this->Subscribe("remote-control-response/$device");
|
||
}
|
||
|
||
|
||
/** -------------------------------------------
|
||
* VARIABLE WRITE SUPPORT
|
||
* ------------------------------------------*/
|
||
public function RequestAction($Ident, $Value)
|
||
{
|
||
$this->SetValue($Ident, $Value);
|
||
}
|
||
|
||
|
||
/** -------------------------------------------
|
||
* MQTT SUBSCRIBE
|
||
* ------------------------------------------*/
|
||
private function Subscribe(string $topic): void
|
||
{
|
||
$this->SendDebug('Subscribe', $topic, 0);
|
||
|
||
$packet = [
|
||
'PacketType' => 8, // SUBSCRIBE
|
||
'QualityOfService' => 0,
|
||
'Retain' => false,
|
||
'Topic' => $topic,
|
||
'Payload' => ''
|
||
];
|
||
|
||
$this->SendDataToParent(json_encode([
|
||
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
|
||
] + $packet));
|
||
}
|
||
|
||
|
||
/** -------------------------------------------
|
||
* MQTT PUBLISH
|
||
* ------------------------------------------*/
|
||
private function Publish(string $topic, string $payload): void
|
||
{
|
||
$this->SendDebug('Publish', "Topic: $topic Payload: $payload", 0);
|
||
|
||
$packet = [
|
||
'PacketType' => 3, // PUBLISH
|
||
'QualityOfService' => 0,
|
||
'Retain' => false,
|
||
'Topic' => $topic,
|
||
'Payload' => $payload
|
||
];
|
||
|
||
$this->SendDataToParent(json_encode([
|
||
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
|
||
] + $packet));
|
||
}
|
||
|
||
|
||
/** -------------------------------------------
|
||
* RECEIVE DATA
|
||
* ------------------------------------------*/
|
||
public function ReceiveData($JSONString)
|
||
{
|
||
$data = json_decode($JSONString, true);
|
||
if (!is_array($data)) {
|
||
return;
|
||
}
|
||
|
||
$topic = $data['Topic'] ?? '';
|
||
$payload = $data['Payload'] ?? '';
|
||
|
||
$device = $this->ReadPropertyString('DeviceID');
|
||
|
||
$this->SendDebug('Receive', "Topic: $topic Payload: $payload", 0);
|
||
|
||
|
||
/** -------------------------------------------
|
||
* 1️⃣ FEEDBACK REQUEST → Statusvariablen senden
|
||
* ------------------------------------------*/
|
||
if ($topic === "feedback-request/$device") {
|
||
|
||
$response = [
|
||
"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;
|
||
}
|
||
|
||
|
||
/** -------------------------------------------
|
||
* 2️⃣ REMOTE CONTROL → write-only Variablen
|
||
* ------------------------------------------*/
|
||
if ($topic === "remote-control-response/$device") {
|
||
|
||
$this->SetValue('RemoteControlPayload', $payload);
|
||
|
||
$json = json_decode($payload, true);
|
||
if (is_array($json)) {
|
||
|
||
if (array_key_exists('power_setpoint', $json)) {
|
||
$this->SetValue('PowerSetpoint', (int)$json['power_setpoint']);
|
||
}
|
||
|
||
if (array_key_exists('strategy', $json)) {
|
||
$this->SetValue('Strategy', (string)$json['strategy']);
|
||
}
|
||
}
|
||
|
||
return;
|
||
}
|
||
}
|
||
|
||
|
||
/** -------------------------------------------
|
||
* OPTIONAL CALLBACK
|
||
* ------------------------------------------*/
|
||
protected function HandleRemoteControl(string $payload): void
|
||
{
|
||
$this->SendDebug('RemoteControl', $payload, 0);
|
||
}
|
||
}
|