330 lines
9.3 KiB
PHP
330 lines
9.3 KiB
PHP
<?php
|
|
|
|
class MQTTBatterySDL extends IPSModule
|
|
{
|
|
private bool $Subscribed = false;
|
|
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
|
|
$this->RegisterPropertyString('TopicSuffix', '');
|
|
|
|
$this->RegisterPropertyInteger('ReqActionID', 0);
|
|
$this->RegisterPropertyInteger('SoCID', 0);
|
|
$this->RegisterPropertyInteger('PowerProductionID', 0);
|
|
|
|
$this->RegisterPropertyInteger('TargetSoC', 50);
|
|
$this->RegisterPropertyInteger('ChargePower', 2500);
|
|
$this->RegisterPropertyInteger('DischargePower', 2500);
|
|
$this->RegisterPropertyInteger('MaxPowerSetpoint', 10000);
|
|
|
|
$this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 10);
|
|
$this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 20);
|
|
|
|
$this->RegisterVariableFloat('MinSoC', 'Min SoC', '', 30);
|
|
$this->RegisterVariableFloat('MaxSoC', 'Max SoC', '', 40);
|
|
|
|
$this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 50);
|
|
$this->EnableAction('PowerSetpoint');
|
|
|
|
$this->RegisterVariableString('Strategy', 'Strategy', '', 60);
|
|
$this->EnableAction('Strategy');
|
|
|
|
$this->RegisterVariableString('LastReadResponse', 'Letzte Lese-Antwort', '', 70);
|
|
$this->RegisterVariableString('LastWriteResponse', 'Letzte Steuer-Antwort', '', 80);
|
|
|
|
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
|
|
$suffix = $this->ReadPropertyString('TopicSuffix');
|
|
|
|
if ($suffix == '') {
|
|
$this->SetStatus(IS_INACTIVE);
|
|
return;
|
|
}
|
|
|
|
$filter =
|
|
'.*"Topic":"(feedback-request|remote-control-request)\/'
|
|
. preg_quote($suffix, '/')
|
|
. '".*';
|
|
|
|
$this->SetReceiveDataFilter($filter);
|
|
|
|
$this->Subscribed = false;
|
|
$this->EnsureSubscribe();
|
|
|
|
$this->SetStatus(IS_ACTIVE);
|
|
}
|
|
|
|
public function RequestAction($Ident, $Value)
|
|
{
|
|
switch ($Ident) {
|
|
case 'PowerSetpoint':
|
|
$max = $this->ReadPropertyInteger('MaxPowerSetpoint');
|
|
|
|
if ($Value > $max) {
|
|
$Value = $max;
|
|
}
|
|
|
|
if ($Value < ($max * -1)) {
|
|
$Value = $max * -1;
|
|
}
|
|
|
|
SetValue($this->GetIDForIdent('PowerSetpoint'), $Value);
|
|
$this->RunBatteryControl();
|
|
break;
|
|
|
|
case 'Strategy':
|
|
SetValueString($this->GetIDForIdent('Strategy'), (string)$Value);
|
|
$this->RunBatteryControl();
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function ReceiveData($JSONString)
|
|
{
|
|
$this->EnsureSubscribe();
|
|
|
|
$data = json_decode($JSONString, true);
|
|
|
|
if (!is_array($data)) {
|
|
return;
|
|
}
|
|
|
|
$topic = $data['Topic'] ?? '';
|
|
$payload = $data['Payload'] ?? '';
|
|
|
|
if ($topic == '') {
|
|
return;
|
|
}
|
|
|
|
$suffix = $this->ReadPropertyString('TopicSuffix');
|
|
|
|
if ($suffix == '') {
|
|
return;
|
|
}
|
|
|
|
if ($topic === 'feedback-request/' . $suffix) {
|
|
$json = $this->BuildReadResponse();
|
|
|
|
$this->PublishMQTT(
|
|
'feedback-response/' . $suffix,
|
|
$json
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
if ($topic === 'remote-control-request/' . $suffix) {
|
|
$json = $this->HandleRemoteControlJSON($payload);
|
|
|
|
if ($json !== null) {
|
|
$this->PublishMQTT(
|
|
'remote-control-response/' . $suffix,
|
|
$json
|
|
);
|
|
}
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
public function RunBatteryControl()
|
|
{
|
|
$reqActionID = $this->ReadPropertyInteger('ReqActionID');
|
|
$socID = $this->ReadPropertyInteger('SoCID');
|
|
$powerProductionID = $this->ReadPropertyInteger('PowerProductionID');
|
|
|
|
if (!$this->IsValidVariable($reqActionID)) {
|
|
$this->SendDebug('RunBatteryControl', 'Keine gültige Ausgabe-Variable gewählt', 0);
|
|
return;
|
|
}
|
|
|
|
if (!$this->IsValidVariable($socID)) {
|
|
$this->SendDebug('RunBatteryControl', 'Keine gültige SoC Variable gewählt', 0);
|
|
return;
|
|
}
|
|
|
|
$strategy = GetValueString($this->GetIDForIdent('Strategy'));
|
|
$setpoint = GetValue($this->GetIDForIdent('PowerSetpoint'));
|
|
$soc = GetValue($socID);
|
|
|
|
$targetSoC = $this->ReadPropertyInteger('TargetSoC');
|
|
$chargePower = $this->ReadPropertyInteger('ChargePower');
|
|
$dischargePower = $this->ReadPropertyInteger('DischargePower');
|
|
|
|
if ($strategy == 'activate') {
|
|
RequestAction($reqActionID, $setpoint * -1);
|
|
return;
|
|
}
|
|
|
|
if ($strategy == 'stop') {
|
|
RequestAction($reqActionID, 0);
|
|
|
|
if ($this->IsValidVariable($powerProductionID)) {
|
|
SetValue($powerProductionID, 0);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
if ((int)$soc == $targetSoC) {
|
|
RequestAction($reqActionID, 0);
|
|
return;
|
|
}
|
|
|
|
if ($soc < $targetSoC) {
|
|
RequestAction($reqActionID, abs($chargePower));
|
|
return;
|
|
}
|
|
|
|
if ($soc > $targetSoC) {
|
|
RequestAction($reqActionID, abs($dischargePower) * -1);
|
|
return;
|
|
}
|
|
}
|
|
|
|
public function BuildReadResponse()
|
|
{
|
|
$socID = $this->ReadPropertyInteger('SoCID');
|
|
$powerProductionID = $this->ReadPropertyInteger('PowerProductionID');
|
|
|
|
$data = [
|
|
'power_production' => $this->IsValidVariable($powerProductionID) ? GetValue($powerProductionID) : 0,
|
|
'is_ready' => GetValue($this->GetIDForIdent('IsReady')),
|
|
'is_running' => GetValue($this->GetIDForIdent('IsRunning')),
|
|
'state_of_charge' => $this->IsValidVariable($socID) ? GetValue($socID) : 0,
|
|
'min_soc' => GetValue($this->GetIDForIdent('MinSoC')),
|
|
'max_soc' => GetValue($this->GetIDForIdent('MaxSoC'))
|
|
];
|
|
|
|
$json = json_encode($data, JSON_PRETTY_PRINT);
|
|
|
|
SetValueString($this->GetIDForIdent('LastReadResponse'), $json);
|
|
|
|
return $json;
|
|
}
|
|
|
|
public function HandleRemoteControlJSON(string $payload)
|
|
{
|
|
if ($payload == '') {
|
|
return null;
|
|
}
|
|
|
|
$data = json_decode($payload, true);
|
|
|
|
if (!is_array($data)) {
|
|
return null;
|
|
}
|
|
|
|
if (isset($data['power_setpoint'])) {
|
|
$max = $this->ReadPropertyInteger('MaxPowerSetpoint');
|
|
$val = (int)$data['power_setpoint'];
|
|
|
|
if ($val > $max) {
|
|
$val = $max;
|
|
}
|
|
|
|
if ($val < ($max * -1)) {
|
|
$val = $max * -1;
|
|
}
|
|
|
|
SetValue($this->GetIDForIdent('PowerSetpoint'), $val);
|
|
}
|
|
|
|
if (isset($data['strategy'])) {
|
|
SetValueString($this->GetIDForIdent('Strategy'), (string)$data['strategy']);
|
|
}
|
|
|
|
$this->RunBatteryControl();
|
|
|
|
$output = [
|
|
'power_setpoint' => GetValue($this->GetIDForIdent('PowerSetpoint')),
|
|
'strategy' => GetValueString($this->GetIDForIdent('Strategy'))
|
|
];
|
|
|
|
$json = json_encode($output, JSON_PRETTY_PRINT);
|
|
|
|
SetValueString($this->GetIDForIdent('LastWriteResponse'), $json);
|
|
|
|
return $json;
|
|
}
|
|
|
|
private function EnsureSubscribe()
|
|
{
|
|
if ($this->Subscribed) {
|
|
return;
|
|
}
|
|
|
|
$suffix = $this->ReadPropertyString('TopicSuffix');
|
|
|
|
if ($suffix == '') {
|
|
return;
|
|
}
|
|
|
|
$this->Subscribed = true;
|
|
|
|
foreach ([
|
|
'feedback-request/' . $suffix,
|
|
'remote-control-request/' . $suffix
|
|
] as $topic) {
|
|
$this->SafeSend([
|
|
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}',
|
|
'PacketType' => 8,
|
|
'QualityOfService' => 0,
|
|
'Retain' => false,
|
|
'Topic' => $topic,
|
|
'Payload' => ''
|
|
]);
|
|
|
|
$this->SendDebug('Subscribe', $topic, 0);
|
|
}
|
|
}
|
|
|
|
private function PublishMQTT(string $topic, string $payload)
|
|
{
|
|
$this->SafeSend([
|
|
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}',
|
|
'PacketType' => 3,
|
|
'QualityOfService' => 0,
|
|
'Retain' => false,
|
|
'Topic' => $topic,
|
|
'Payload' => $payload
|
|
]);
|
|
|
|
$this->SendDebug('PublishMQTT', $topic . ' → ' . $payload, 0);
|
|
}
|
|
|
|
private function SafeSend(array $packet)
|
|
{
|
|
$parent = @IPS_GetInstance($this->InstanceID)['ConnectionID'] ?? 0;
|
|
|
|
if ($parent === 0 || !IPS_InstanceExists($parent)) {
|
|
$this->SendDebug('SafeSend', 'Kein gültiger Parent vorhanden', 0);
|
|
return;
|
|
}
|
|
|
|
if (IPS_GetInstance($parent)['InstanceStatus'] !== 102) {
|
|
$this->SendDebug('SafeSend', 'Parent ist nicht aktiv', 0);
|
|
return;
|
|
}
|
|
|
|
@$this->SendDataToParent(json_encode($packet));
|
|
}
|
|
|
|
private function IsValidVariable(int $id)
|
|
{
|
|
return (
|
|
$id > 0
|
|
&& IPS_ObjectExists($id)
|
|
&& IPS_GetObject($id)['ObjectType'] == 2
|
|
);
|
|
}
|
|
}
|
|
|
|
?>
|