157 lines
4.7 KiB
PHP
157 lines
4.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
class VGT_Sub extends IPSModule
|
|
{
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
|
|
// Konfiguration
|
|
$this->RegisterPropertyString('DeviceID', '');
|
|
|
|
// Eigene Status Variablen (schreibbar)
|
|
$this->RegisterVariableFloat('PowerProduction', 'Power Production', '', 10);
|
|
$this->EnableAction('PowerProduction');
|
|
|
|
$this->RegisterVariableFloat('StateOfCharge', 'State of Charge', '', 11);
|
|
$this->EnableAction('StateOfCharge');
|
|
|
|
$this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 12);
|
|
$this->EnableAction('IsRunning');
|
|
|
|
$this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 13);
|
|
$this->EnableAction('IsReady');
|
|
|
|
$this->RegisterVariableFloat('MinSoc', 'Min SoC', '', 14);
|
|
$this->EnableAction('MinSoc');
|
|
|
|
$this->RegisterVariableFloat('MaxSoc', 'Max SoC', '', 15);
|
|
$this->EnableAction('MaxSoc');
|
|
|
|
// Remote-Control Werte (readonly)
|
|
$this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 30);
|
|
IPS_SetVariableCustomAction($this->GetIDForIdent('PowerSetpoint'), 0);
|
|
|
|
$this->RegisterVariableString('Strategy', 'Strategy', '', 31);
|
|
IPS_SetVariableCustomAction($this->GetIDForIdent('Strategy'), 0);
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
$this->ConnectParent('{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}');
|
|
|
|
$this->SubscribeTopics();
|
|
}
|
|
|
|
private function SubscribeTopics()
|
|
{
|
|
$device = $this->ReadPropertyString('DeviceID');
|
|
if ($device == '') {
|
|
return;
|
|
}
|
|
|
|
$topics = [
|
|
"feedback-request/$device",
|
|
"remote-control-request/$device"
|
|
];
|
|
|
|
foreach ($topics as $topic) {
|
|
$this->SendDebug("Subscribe", $topic, 0);
|
|
|
|
// Richtige MQTT Client Struktur
|
|
$this->SendDataToParent(json_encode([
|
|
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}',
|
|
'Topic' => $topic
|
|
]));
|
|
}
|
|
}
|
|
|
|
public function ReceiveData($JSONString)
|
|
{
|
|
$data = json_decode($JSONString, true);
|
|
|
|
if (!isset($data['Topic'])) {
|
|
return;
|
|
}
|
|
|
|
$topic = $data['Topic'];
|
|
$payload = $data['Payload'];
|
|
|
|
$this->SendDebug("RX Topic", $topic, 0);
|
|
$this->SendDebug("RX Payload", $payload, 0);
|
|
|
|
$device = $this->ReadPropertyString('DeviceID');
|
|
|
|
if ($topic === "feedback-request/$device") {
|
|
$this->HandleFeedbackRequest($device);
|
|
return;
|
|
}
|
|
|
|
if ($topic === "remote-control-request/$device") {
|
|
$this->HandleRemoteControlRequest($device, $payload);
|
|
return;
|
|
}
|
|
}
|
|
|
|
private function HandleFeedbackRequest($device)
|
|
{
|
|
$response = [
|
|
"power_production" => GetValue($this->GetIDForIdent('PowerProduction')),
|
|
"state_of_charge" => GetValue($this->GetIDForIdent('StateOfCharge')),
|
|
"is_running" => GetValue($this->GetIDForIdent('IsRunning')),
|
|
"is_ready" => GetValue($this->GetIDForIdent('IsReady')),
|
|
"min_soc" => GetValue($this->GetIDForIdent('MinSoc')),
|
|
"max_soc" => GetValue($this->GetIDForIdent('MaxSoc'))
|
|
];
|
|
|
|
$json = json_encode($response);
|
|
$topic = "feedback-response/$device";
|
|
|
|
$this->SendDebug("TX Feedback", "$topic => $json", 0);
|
|
|
|
// MQTT Client: Publish
|
|
$this->SendDataToParent(json_encode([
|
|
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}',
|
|
'Topic' => $topic,
|
|
'Payload' => $json
|
|
]));
|
|
}
|
|
|
|
private function HandleRemoteControlRequest($device, $payload)
|
|
{
|
|
$json = @json_decode($payload, true);
|
|
if (!is_array($json)) {
|
|
$this->SendDebug("Error", "Ungültiges JSON", 0);
|
|
return;
|
|
}
|
|
|
|
if (isset($json['power_setpoint'])) {
|
|
SetValue($this->GetIDForIdent('PowerSetpoint'), (int)$json['power_setpoint']);
|
|
}
|
|
|
|
if (isset($json['strategy'])) {
|
|
SetValueString($this->GetIDForIdent('Strategy'), (string)$json['strategy']);
|
|
}
|
|
|
|
$topic = "remote-control-response/$device";
|
|
$payloadOut = json_encode($json);
|
|
|
|
// MQTT Client: Publish
|
|
$this->SendDataToParent(json_encode([
|
|
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}',
|
|
'Topic' => $topic,
|
|
'Payload' => $payloadOut
|
|
]));
|
|
|
|
$this->SendDebug("TX RemoteControl", "$topic => $payloadOut", 0);
|
|
}
|
|
|
|
public function RequestAction($Ident, $Value)
|
|
{
|
|
SetValue($this->GetIDForIdent($Ident), $Value);
|
|
}
|
|
}
|
|
?>
|