127 lines
3.8 KiB
PHP
127 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
class VGT_Sub extends IPSModule
|
|
{
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
|
|
$this->RegisterPropertyString("DeviceID", "");
|
|
|
|
$this->RegisterVariableFloat("PowerProduction", "Power Production", "", 10);
|
|
$this->EnableAction("PowerProduction");
|
|
|
|
$this->RegisterVariableFloat("StateOfCharge", "State of Charge", "", 20);
|
|
$this->EnableAction("StateOfCharge");
|
|
|
|
$this->RegisterVariableBoolean("IsRunning", "Is Running", "", 30);
|
|
$this->EnableAction("IsRunning");
|
|
|
|
$this->RegisterVariableBoolean("IsReady", "Is Ready", "", 40);
|
|
$this->EnableAction("IsReady");
|
|
|
|
$this->RegisterVariableInteger("MinSOC", "Min SOC", "", 50);
|
|
$this->EnableAction("MinSOC");
|
|
|
|
$this->RegisterVariableInteger("MaxSOC", "Max SOC", "", 60);
|
|
$this->EnableAction("MaxSOC");
|
|
|
|
$this->RegisterVariableFloat("RC_PowerSetpoint", "RC Power Setpoint", "", 70);
|
|
$this->RegisterVariableString("RC_Strategy", "RC Strategy", "", 80);
|
|
|
|
$this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}');
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
|
|
// Child darf NICHT subscriben!
|
|
$this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}');
|
|
}
|
|
|
|
public function RequestAction($Ident, $Value)
|
|
{
|
|
SetValue($this->GetIDForIdent($Ident), $Value);
|
|
}
|
|
|
|
public function ReceiveData($JSONString)
|
|
{
|
|
$data = json_decode($JSONString);
|
|
|
|
if (!isset($data->Topic) || !isset($data->Payload)) {
|
|
return;
|
|
}
|
|
|
|
$topic = $data->Topic;
|
|
$payload = $data->Payload;
|
|
|
|
$device = $this->ReadPropertyString("DeviceID");
|
|
|
|
if ($topic === "feedback-request/" . $device) {
|
|
$this->HandleFeedbackRequest();
|
|
}
|
|
|
|
if ($topic === "remote-control-request/" . $device) {
|
|
$this->HandleRemoteControlRequest($payload);
|
|
}
|
|
}
|
|
|
|
private function HandleFeedbackRequest()
|
|
{
|
|
$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"))
|
|
];
|
|
|
|
$this->SendToMQTT("feedback-response/" . $this->ReadPropertyString("DeviceID"), json_encode($response));
|
|
}
|
|
|
|
private function HandleRemoteControlRequest(string $payload)
|
|
{
|
|
$data = json_decode($payload, true);
|
|
if (!is_array($data)) {
|
|
return;
|
|
}
|
|
|
|
if (isset($data["power_setpoint"])) {
|
|
SetValue($this->GetIDForIdent("RC_PowerSetpoint"), $data["power_setpoint"]);
|
|
}
|
|
|
|
if (isset($data["strategy"])) {
|
|
SetValue($this->GetIDForIdent("RC_Strategy"), $data["strategy"]);
|
|
}
|
|
|
|
$this->SendToMQTT("remote-control-response/" . $this->ReadPropertyString("DeviceID"), json_encode($data));
|
|
}
|
|
|
|
private function SendToMQTT(string $topic, string $payload)
|
|
{
|
|
$this->SendDataToParent(json_encode([
|
|
"DataID" => "{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}",
|
|
"PacketType" => 3,
|
|
"QualityOfService" => 0,
|
|
"Retain" => false,
|
|
"Topic" => $topic,
|
|
"Payload" => $payload
|
|
]));
|
|
}
|
|
|
|
public function GetConfigurationForm()
|
|
{
|
|
return json_encode([
|
|
"elements" => [
|
|
[ "type" => "ValidationTextBox", "name" => "DeviceID", "caption" => "Device ID" ]
|
|
]
|
|
]);
|
|
}
|
|
}
|
|
|
|
?>
|