no message

This commit is contained in:
2026-05-26 13:05:51 +02:00
parent b47ab188ec
commit 716620cf09
3 changed files with 155 additions and 269 deletions
-5
View File
@@ -5,11 +5,6 @@
"name": "TopicSuffix", "name": "TopicSuffix",
"caption": "Topic Suffix / x" "caption": "Topic Suffix / x"
}, },
{
"type": "SelectVariable",
"name": "FeedbackResponseID",
"caption": "Feedback Response Variable"
},
{ {
"type": "SelectVariable", "type": "SelectVariable",
"name": "ReqActionID", "name": "ReqActionID",
-1
View File
@@ -7,7 +7,6 @@
"MQTT Battery SDL" "MQTT Battery SDL"
], ],
"parentRequirements": [ "parentRequirements": [
"{018EF6B5-AB94-40C6-AA53-46943E824ACF}",
"{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}" "{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}"
], ],
"childRequirements": [], "childRequirements": [],
+125 -233
View File
@@ -2,11 +2,12 @@
class MQTTBatterySDL extends IPSModule class MQTTBatterySDL extends IPSModule
{ {
private bool $Subscribed = false;
public function Create() public function Create()
{ {
parent::Create(); parent::Create();
// Eigenschaften
$this->RegisterPropertyString('TopicSuffix', ''); $this->RegisterPropertyString('TopicSuffix', '');
$this->RegisterPropertyInteger('ReqActionID', 0); $this->RegisterPropertyInteger('ReqActionID', 0);
@@ -18,9 +19,6 @@ class MQTTBatterySDL extends IPSModule
$this->RegisterPropertyInteger('DischargePower', 2500); $this->RegisterPropertyInteger('DischargePower', 2500);
$this->RegisterPropertyInteger('MaxPowerSetpoint', 10000); $this->RegisterPropertyInteger('MaxPowerSetpoint', 10000);
$this->RegisterPropertyInteger('FeedbackResponseID', 0);
// Variablen
$this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 10); $this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 10);
$this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 20); $this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 20);
@@ -36,12 +34,11 @@ class MQTTBatterySDL extends IPSModule
$this->RegisterVariableString('LastReadResponse', 'Letzte Lese-Antwort', '', 70); $this->RegisterVariableString('LastReadResponse', 'Letzte Lese-Antwort', '', 70);
$this->RegisterVariableString('LastWriteResponse', 'Letzte Steuer-Antwort', '', 80); $this->RegisterVariableString('LastWriteResponse', 'Letzte Steuer-Antwort', '', 80);
$this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}'); $this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
} }
public function ApplyChanges() public function ApplyChanges()
{ {
parent::ApplyChanges(); parent::ApplyChanges();
$suffix = $this->ReadPropertyString('TopicSuffix'); $suffix = $this->ReadPropertyString('TopicSuffix');
@@ -58,15 +55,16 @@ public function ApplyChanges()
$this->SetReceiveDataFilter($filter); $this->SetReceiveDataFilter($filter);
$this->Subscribed = false;
$this->EnsureSubscribe();
$this->SetStatus(IS_ACTIVE); $this->SetStatus(IS_ACTIVE);
} }
public function RequestAction($Ident, $Value) public function RequestAction($Ident, $Value)
{ {
switch ($Ident) { switch ($Ident) {
case 'PowerSetpoint': case 'PowerSetpoint':
$max = $this->ReadPropertyInteger('MaxPowerSetpoint'); $max = $this->ReadPropertyInteger('MaxPowerSetpoint');
if ($Value > $max) { if ($Value > $max) {
@@ -78,21 +76,61 @@ public function ApplyChanges()
} }
SetValue($this->GetIDForIdent('PowerSetpoint'), $Value); SetValue($this->GetIDForIdent('PowerSetpoint'), $Value);
$this->RunBatteryControl(); $this->RunBatteryControl();
break; break;
case 'Strategy': case 'Strategy':
SetValueString($this->GetIDForIdent('Strategy'), (string)$Value);
$this->RunBatteryControl();
break;
}
}
SetValueString( public function ReceiveData($JSONString)
$this->GetIDForIdent('Strategy'), {
(string)$Value $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
); );
$this->RunBatteryControl(); return;
}
break; if ($topic === 'remote-control-request/' . $suffix) {
$json = $this->HandleRemoteControlJSON($payload);
if ($json !== null) {
$this->PublishMQTT(
'remote-control-response/' . $suffix,
$json
);
}
return;
} }
} }
@@ -103,59 +141,29 @@ public function ApplyChanges()
$powerProductionID = $this->ReadPropertyInteger('PowerProductionID'); $powerProductionID = $this->ReadPropertyInteger('PowerProductionID');
if (!$this->IsValidVariable($reqActionID)) { if (!$this->IsValidVariable($reqActionID)) {
$this->SendDebug( $this->SendDebug('RunBatteryControl', 'Keine gültige Ausgabe-Variable gewählt', 0);
'RunBatteryControl',
'Keine gültige Ausgabe-Variable gewählt',
0
);
return; return;
} }
if (!$this->IsValidVariable($socID)) { if (!$this->IsValidVariable($socID)) {
$this->SendDebug( $this->SendDebug('RunBatteryControl', 'Keine gültige SoC Variable gewählt', 0);
'RunBatteryControl',
'Keine gültige SoC Variable gewählt',
0
);
return; return;
} }
$strategy = GetValueString( $strategy = GetValueString($this->GetIDForIdent('Strategy'));
$this->GetIDForIdent('Strategy') $setpoint = GetValue($this->GetIDForIdent('PowerSetpoint'));
);
$setpoint = GetValue(
$this->GetIDForIdent('PowerSetpoint')
);
$soc = GetValue($socID); $soc = GetValue($socID);
$targetSoC = $this->ReadPropertyInteger('TargetSoC'); $targetSoC = $this->ReadPropertyInteger('TargetSoC');
$chargePower = $this->ReadPropertyInteger('ChargePower'); $chargePower = $this->ReadPropertyInteger('ChargePower');
$dischargePower = $this->ReadPropertyInteger('DischargePower'); $dischargePower = $this->ReadPropertyInteger('DischargePower');
// -------------------------------------------------
// Strategy activate
// -------------------------------------------------
if ($strategy == 'activate') { if ($strategy == 'activate') {
RequestAction($reqActionID, $setpoint * -1);
RequestAction(
$reqActionID,
$setpoint * -1
);
return; return;
} }
// -------------------------------------------------
// Strategy stop
// -------------------------------------------------
if ($strategy == 'stop') { if ($strategy == 'stop') {
RequestAction($reqActionID, 0); RequestAction($reqActionID, 0);
if ($this->IsValidVariable($powerProductionID)) { if ($this->IsValidVariable($powerProductionID)) {
@@ -165,42 +173,18 @@ public function ApplyChanges()
return; return;
} }
// -------------------------------------------------
// Ziel erreicht
// -------------------------------------------------
if ((int)$soc == $targetSoC) { if ((int)$soc == $targetSoC) {
RequestAction($reqActionID, 0); RequestAction($reqActionID, 0);
return; return;
} }
// -------------------------------------------------
// Laden
// -------------------------------------------------
if ($soc < $targetSoC) { if ($soc < $targetSoC) {
RequestAction($reqActionID, abs($chargePower));
RequestAction(
$reqActionID,
abs($chargePower)
);
return; return;
} }
// -------------------------------------------------
// Entladen
// -------------------------------------------------
if ($soc > $targetSoC) { if ($soc > $targetSoC) {
RequestAction($reqActionID, abs($dischargePower) * -1);
RequestAction(
$reqActionID,
abs($dischargePower) * -1
);
return; return;
} }
} }
@@ -208,51 +192,20 @@ public function ApplyChanges()
public function BuildReadResponse() public function BuildReadResponse()
{ {
$socID = $this->ReadPropertyInteger('SoCID'); $socID = $this->ReadPropertyInteger('SoCID');
$powerProductionID = $this->ReadPropertyInteger('PowerProductionID');
$powerProductionID = $this->ReadPropertyInteger(
'PowerProductionID'
);
$data = [ $data = [
'power_production' => $this->IsValidVariable($powerProductionID) ? GetValue($powerProductionID) : 0,
'power_production' => $this->IsValidVariable( 'is_ready' => GetValue($this->GetIDForIdent('IsReady')),
$powerProductionID 'is_running' => GetValue($this->GetIDForIdent('IsRunning')),
) 'state_of_charge' => $this->IsValidVariable($socID) ? GetValue($socID) : 0,
? GetValue($powerProductionID) 'min_soc' => GetValue($this->GetIDForIdent('MinSoC')),
: 0, 'max_soc' => GetValue($this->GetIDForIdent('MaxSoC'))
'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( $json = json_encode($data, JSON_PRETTY_PRINT);
$data,
JSON_PRETTY_PRINT
);
SetValueString( SetValueString($this->GetIDForIdent('LastReadResponse'), $json);
$this->GetIDForIdent('LastReadResponse'),
$json
);
return $json; return $json;
} }
@@ -260,25 +213,17 @@ public function ApplyChanges()
public function HandleRemoteControlJSON(string $payload) public function HandleRemoteControlJSON(string $payload)
{ {
if ($payload == '') { if ($payload == '') {
return; return null;
} }
$data = json_decode($payload, true); $data = json_decode($payload, true);
if (!is_array($data)) { if (!is_array($data)) {
return; return null;
} }
// -------------------------------------------------
// Power Setpoint
// -------------------------------------------------
if (isset($data['power_setpoint'])) { if (isset($data['power_setpoint'])) {
$max = $this->ReadPropertyInteger('MaxPowerSetpoint');
$max = $this->ReadPropertyInteger(
'MaxPowerSetpoint'
);
$val = (int)$data['power_setpoint']; $val = (int)$data['power_setpoint'];
if ($val > $max) { if ($val > $max) {
@@ -289,142 +234,88 @@ public function ApplyChanges()
$val = $max * -1; $val = $max * -1;
} }
SetValue( SetValue($this->GetIDForIdent('PowerSetpoint'), $val);
$this->GetIDForIdent('PowerSetpoint'),
$val
);
} }
// -------------------------------------------------
// Strategy
// -------------------------------------------------
if (isset($data['strategy'])) { if (isset($data['strategy'])) {
SetValueString($this->GetIDForIdent('Strategy'), (string)$data['strategy']);
SetValueString(
$this->GetIDForIdent('Strategy'),
(string)$data['strategy']
);
} }
// -------------------------------------------------
// Batterie-Regelung ausführen
// -------------------------------------------------
$this->RunBatteryControl(); $this->RunBatteryControl();
// -------------------------------------------------
// Antwort bauen
// -------------------------------------------------
$output = [ $output = [
'power_setpoint' => GetValue($this->GetIDForIdent('PowerSetpoint')),
'power_setpoint' => GetValue( 'strategy' => GetValueString($this->GetIDForIdent('Strategy'))
$this->GetIDForIdent('PowerSetpoint')
),
'strategy' => GetValueString(
$this->GetIDForIdent('Strategy')
)
]; ];
$json = json_encode( $json = json_encode($output, JSON_PRETTY_PRINT);
$output,
JSON_PRETTY_PRINT
);
SetValueString( SetValueString($this->GetIDForIdent('LastWriteResponse'), $json);
$this->GetIDForIdent('LastWriteResponse'),
$json
);
return $json; return $json;
} }
public function ReceiveData($JSONString) private function EnsureSubscribe()
{ {
$data = json_decode($JSONString, true); if ($this->Subscribed) {
if (!is_array($data)) {
return; return;
} }
if (($data['PacketType'] ?? 0) != 3) {
return;
}
if (!isset($data['Topic'])) {
return;
}
$topic = $data['Topic'];
if (strpos($topic, 'feedback-response/') === 0) {
return;
}
if (strpos($topic, 'remote-control-response/') === 0) {
return;
}
$this->SendDebug('ReceiveData', $JSONString, 0);
$payload = $data['Payload'] ?? '';
$suffix = $this->ReadPropertyString('TopicSuffix'); $suffix = $this->ReadPropertyString('TopicSuffix');
if ($topic === 'feedback-request/' . $suffix) { if ($suffix == '') {
$json = $this->BuildReadResponse();
$this->SendFeedbackResponse($json);
return; return;
} }
if ($topic === 'remote-control-request/' . $suffix) { $this->Subscribed = true;
$json = $this->HandleRemoteControlJSON($payload);
if ($json !== null) { foreach ([
$this->QueuePublish( 'feedback-request/' . $suffix,
'remote-control-response/' . $suffix, 'remote-control-request/' . $suffix
$json ] as $topic) {
); $this->SafeSend([
}
return;
}
}
private function PublishMQTT(string $topic, string $payload)
{
$this->SendDebug(
'PublishMQTT',
$topic . ' => ' . $payload,
0
);
$this->SendDataToParent(json_encode([
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}', 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}',
'PacketType' => 3, 'PacketType' => 8,
'Payload' => $payload,
'QualityOfService' => 0, 'QualityOfService' => 0,
'Retain' => false, 'Retain' => false,
'Topic' => $topic 'Topic' => $topic,
])); 'Payload' => ''
} ]);
$this->SendDebug('Subscribe', $topic, 0);
}
}
private function SendFeedbackResponse(string $payload) private function PublishMQTT(string $topic, string $payload)
{ {
$id = $this->ReadPropertyInteger('FeedbackResponseID'); $this->SafeSend([
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}',
'PacketType' => 3,
'QualityOfService' => 0,
'Retain' => false,
'Topic' => $topic,
'Payload' => $payload
]);
if (!$this->IsValidVariable($id)) { $this->SendDebug('PublishMQTT', $topic . ' → ' . $payload, 0);
$this->SendDebug('SendFeedbackResponse', 'Keine gültige FeedbackResponseID gewählt', 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; return;
} }
RequestAction($id, $payload); 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) private function IsValidVariable(int $id)
{ {
@@ -435,4 +326,5 @@ private function SendFeedbackResponse(string $payload)
); );
} }
} }
?> ?>