207 lines
7.1 KiB
PHP
207 lines
7.1 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
class VGT_Sub extends IPSModule
|
||
{
|
||
public function Create()
|
||
{
|
||
parent::Create();
|
||
|
||
// Device
|
||
$this->RegisterPropertyString('DeviceID', '');
|
||
|
||
// Variable-Auswahl
|
||
$this->RegisterPropertyInteger('VarPowerProduction', 0);
|
||
$this->RegisterPropertyInteger('VarStateOfCharge', 0);
|
||
$this->RegisterPropertyInteger('VarIsRunning', 0);
|
||
$this->RegisterPropertyInteger('VarPositiveSetpointTarget', 0);
|
||
$this->RegisterPropertyInteger('VarNegativeSetpointTarget', 0);
|
||
|
||
// Numerische SOC-Grenzen
|
||
$this->RegisterPropertyFloat('MinSOC', 20.0);
|
||
$this->RegisterPropertyFloat('MaxSOC', 90.0);
|
||
|
||
// Speicherung des letzten PowerSetpoints
|
||
$this->RegisterPropertyFloat('LastPowerSetpoint', 0.0);
|
||
|
||
// Anzeigevariable für Feedback Request
|
||
$this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request', '', 10);
|
||
|
||
// Anzeigevariable für Remote Control Request
|
||
$this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 20);
|
||
|
||
$this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}');
|
||
}
|
||
|
||
public function ApplyChanges()
|
||
{
|
||
parent::ApplyChanges();
|
||
$this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}');
|
||
|
||
$dev = $this->ReadPropertyString('DeviceID');
|
||
if ($dev != '') {
|
||
$this->Subscribe("feedback-request/$dev");
|
||
$this->Subscribe("remote-control-request/$dev");
|
||
}
|
||
}
|
||
|
||
public function RequestAction($Ident, $Value)
|
||
{
|
||
// Einfach akzeptieren und nichts machen
|
||
return true;
|
||
}
|
||
|
||
public function GetConfigurationForm()
|
||
{
|
||
return json_encode([
|
||
"elements" => [
|
||
|
||
["type" => "ValidationTextBox", "name" => "DeviceID", "caption" => "Device ID"],
|
||
|
||
["type" => "NumberSpinner", "name" => "MinSOC", "caption" => "Min SOC (%)"],
|
||
["type" => "NumberSpinner", "name" => "MaxSOC", "caption" => "Max SOC (%)"],
|
||
|
||
["type" => "SelectVariable", "name" => "VarPowerProduction", "caption" => "Variable – Power Production"],
|
||
["type" => "SelectVariable", "name" => "VarStateOfCharge", "caption" => "Variable – State of Charge"],
|
||
["type" => "SelectVariable", "name" => "VarIsRunning", "caption" => "Variable – Is Running"],
|
||
|
||
["type" => "SelectVariable", "name" => "VarPositiveSetpointTarget", "caption" => "Variable – Positive Power Setpoint"],
|
||
["type" => "SelectVariable", "name" => "VarNegativeSetpointTarget", "caption" => "Variable – Negative Power Setpoint"]
|
||
]
|
||
]);
|
||
}
|
||
|
||
/* -------------------------------------------------------------------------
|
||
* MQTT SUBSCRIBE
|
||
* ----------------------------------------------------------------------*/
|
||
private function Subscribe(string $topic)
|
||
{
|
||
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)
|
||
{
|
||
if (!$this->HasActiveParent()) {
|
||
return;
|
||
}
|
||
|
||
$this->SendDataToParent(json_encode([
|
||
"DataID" => "{A1B5C433-4F17-462D-AD71-383F5BBE4F5A}",
|
||
"Type" => "PUBLISH",
|
||
"Topic" => $topic,
|
||
"Payload" => $payload
|
||
]));
|
||
}
|
||
|
||
/* -------------------------------------------------------------------------
|
||
* RECEIVE MQTT
|
||
* ----------------------------------------------------------------------*/
|
||
public function ReceiveData($JSONString)
|
||
{
|
||
$data = json_decode($JSONString, true);
|
||
if (!isset($data['Type'])) {
|
||
return;
|
||
}
|
||
|
||
$type = $data['Type'];
|
||
|
||
if ($type === "MQTT_CONNECTED") {
|
||
$dev = $this->ReadPropertyString('DeviceID');
|
||
if ($dev != '') {
|
||
$this->Subscribe("feedback-request/$dev");
|
||
$this->Subscribe("remote-control-request/$dev");
|
||
}
|
||
return;
|
||
}
|
||
|
||
if ($type !== "MESSAGE") {
|
||
return;
|
||
}
|
||
|
||
$topic = $data['Topic'];
|
||
$payload = $data['Payload'];
|
||
$device = $this->ReadPropertyString('DeviceID');
|
||
|
||
/* ---------------------------------------------------------------------
|
||
* FEEDBACK REQUEST
|
||
* ------------------------------------------------------------------*/
|
||
if ($topic === "feedback-request/$device") {
|
||
|
||
$this->SetValue('FeedbackRequestPayload', $payload);
|
||
|
||
$varProd = $this->ReadPropertyInteger('VarPowerProduction');
|
||
$varSOC = $this->ReadPropertyInteger('VarStateOfCharge');
|
||
$varRun = $this->ReadPropertyInteger('VarIsRunning');
|
||
|
||
$power = ($varProd ? GetValue($varProd) : 0.0);
|
||
$soc = ($varSOC ? GetValue($varSOC) : 0.0);
|
||
$run = ($varRun ? GetValue($varRun) : false);
|
||
|
||
$min = $this->ReadPropertyFloat('MinSOC');
|
||
$max = $this->ReadPropertyFloat('MaxSOC');
|
||
|
||
$isReady = ($run && $soc > $min && $soc < $max);
|
||
|
||
$response = [
|
||
"power_production" => $power,
|
||
"state_of_charge" => $soc,
|
||
"is_running" => $run,
|
||
"is_ready" => $isReady,
|
||
"min_soc" => $min,
|
||
"max_soc" => $max
|
||
];
|
||
|
||
$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)) {
|
||
return;
|
||
}
|
||
|
||
if (isset($json["power_setpoint"])) {
|
||
$power = floatval($json["power_setpoint"]);
|
||
IPS_SetProperty($this->InstanceID, 'LastPowerSetpoint', $power);
|
||
IPS_ApplyChanges($this->InstanceID);
|
||
|
||
// Positive oder negativ?
|
||
if ($power > 0) {
|
||
$varPos = $this->ReadPropertyInteger('VarPositiveSetpointTarget');
|
||
if ($varPos) {
|
||
SetValue($varPos, $power);
|
||
}
|
||
} else {
|
||
$varNeg = $this->ReadPropertyInteger('VarNegativeSetpointTarget');
|
||
if ($varNeg) {
|
||
SetValue($varNeg, $power);
|
||
}
|
||
}
|
||
}
|
||
|
||
$this->Publish("remote-control-response/$device", $payload);
|
||
}
|
||
}
|
||
}
|
||
|
||
?>
|