no message

This commit is contained in:
2025-11-13 10:02:52 +01:00
parent ef99fa7b1e
commit 116e171636

View File

@@ -10,19 +10,48 @@ class Int_VGT extends IPSModule
$this->RegisterPropertyString('DeviceID', ''); $this->RegisterPropertyString('DeviceID', '');
// Variable für empfangene Remote-Control-Daten
$this->RegisterVariableString(
'RemoteControlPayload',
'Remote Control Payload',
'',
1
);
// Mit MQTT Server (Splitter) verbinden /** -------------------------------------------
// GUID: MQTT Server (Splitter) laut Doku * STATUS Variablen (schreibbar)
* ------------------------------------------*/
$this->RegisterVariableInteger('PowerProduction', 'Power Production', '', 10);
$this->EnableAction('PowerProduction');
$this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 11);
$this->EnableAction('IsReady');
$this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 12);
$this->EnableAction('IsRunning');
$this->RegisterVariableInteger('StateOfCharge', 'State of Charge', '', 13);
$this->EnableAction('StateOfCharge');
$this->RegisterVariableInteger('MinSOC', 'Min SOC', '', 14);
$this->EnableAction('MinSOC');
$this->RegisterVariableInteger('MaxSOC', 'Max SOC', '', 15);
$this->EnableAction('MaxSOC');
/** -------------------------------------------
* REMOTE CONTROL Variablen (read-only)
* ------------------------------------------*/
$this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 20);
$this->RegisterVariableString('Strategy', 'Strategy', '', 21);
// Für Debugging
$this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 30);
/** -------------------------------------------
* MQTT SERVER verbinden
* ------------------------------------------*/
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}'); $this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
} }
public function ApplyChanges() public function ApplyChanges()
{ {
parent::ApplyChanges(); parent::ApplyChanges();
@@ -32,41 +61,49 @@ class Int_VGT extends IPSModule
$device = $this->ReadPropertyString('DeviceID'); $device = $this->ReadPropertyString('DeviceID');
if ($device === '') { if ($device === '') {
$this->SendDebug('ApplyChanges', 'Kein DeviceID gesetzt keine Subscriptions', 0); $this->SendDebug('ApplyChanges', 'Keine DeviceID gesetzt', 0);
return; return;
} }
// Topics: // Subscriptions setzen
// 1) feedback-request/deviceId
// 2) remote-control-response/deviceId
$this->Subscribe("feedback-request/$device"); $this->Subscribe("feedback-request/$device");
$this->Subscribe("remote-control-response/$device"); $this->Subscribe("remote-control-response/$device");
} }
/**
* An MQTT-Server (Splitter) subscribe senden. /** -------------------------------------------
*/ * VARIABLE WRITE SUPPORT
* ------------------------------------------*/
public function RequestAction($Ident, $Value)
{
$this->SetValue($Ident, $Value);
}
/** -------------------------------------------
* MQTT SUBSCRIBE
* ------------------------------------------*/
private function Subscribe(string $topic): void private function Subscribe(string $topic): void
{ {
$this->SendDebug('Subscribe', "Topic: $topic", 0); $this->SendDebug('Subscribe', $topic, 0);
$packet = [ $packet = [
'PacketType' => 8, // SUBSCRIBE 'PacketType' => 8, // SUBSCRIBE
'QualityOfService' => 0, 'QualityOfService' => 0,
'Retain' => false, 'Retain' => false,
'Topic' => $topic, 'Topic' => $topic,
'Payload' => '' // bei Subscribe leer 'Payload' => ''
]; ];
$this->SendDataToParent(json_encode([ $this->SendDataToParent(json_encode([
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' // TX zum MQTT Server 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
] + $packet)); ] + $packet));
} }
/**
* Publish an MQTT-Server (Splitter). /** -------------------------------------------
*/ * MQTT PUBLISH
* ------------------------------------------*/
private function Publish(string $topic, string $payload): void private function Publish(string $topic, string $payload): void
{ {
$this->SendDebug('Publish', "Topic: $topic Payload: $payload", 0); $this->SendDebug('Publish', "Topic: $topic Payload: $payload", 0);
@@ -80,81 +117,77 @@ class Int_VGT extends IPSModule
]; ];
$this->SendDataToParent(json_encode([ $this->SendDataToParent(json_encode([
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' // TX zum MQTT Server 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
] + $packet)); ] + $packet));
} }
/**
* Wird vom MQTT-Server (Splitter) aufgerufen. /** -------------------------------------------
*/ * RECEIVE DATA
* ------------------------------------------*/
public function ReceiveData($JSONString) public function ReceiveData($JSONString)
{ {
$this->SendDebug('ReceiveData RAW', $JSONString, 0);
$data = json_decode($JSONString, true); $data = json_decode($JSONString, true);
if (!is_array($data)) { if (!is_array($data)) {
$this->SendDebug('ReceiveData', 'JSON decode fehlgeschlagen', 0);
return; return;
} }
// Struktur laut Doku:
// {
// "DataID": "{7F7632D9-FA40-4F38-8DEA-C83CD4325A32}",
// "PacketType": ...,
// "QualityOfService": 0,
// "Retain": false,
// "Topic": "....",
// "Payload": "...."
// }
$topic = $data['Topic'] ?? ''; $topic = $data['Topic'] ?? '';
$payload = $data['Payload'] ?? ''; $payload = $data['Payload'] ?? '';
$device = $this->ReadPropertyString('DeviceID'); $device = $this->ReadPropertyString('DeviceID');
if ($device === '') {
$this->SendDebug('ReceiveData', 'Keine DeviceID gesetzt, ignoriere Paket', 0);
return;
}
$this->SendDebug('ReceiveData', "Topic: $topic Payload: $payload", 0); $this->SendDebug('Receive', "Topic: $topic Payload: $payload", 0);
// 1⃣ feedback-request/deviceId → fixe JSON-Antwort
/** -------------------------------------------
* 1⃣ FEEDBACK REQUEST → Statusvariablen senden
* ------------------------------------------*/
if ($topic === "feedback-request/$device") { if ($topic === "feedback-request/$device") {
$this->SendDebug('Logic', 'Feedback-Request erkannt', 0);
$response = [ $response = [
'power_production' => 123, "power_production" => $this->GetValue('PowerProduction'),
'is_ready' => true, "is_ready" => $this->GetValue('IsReady'),
'is_running' => true, "is_running" => $this->GetValue('IsRunning'),
'state_of_charge' => 50, "state_of_charge" => $this->GetValue('StateOfCharge'),
'min_soc' => 10, "min_soc" => $this->GetValue('MinSOC'),
'max_soc' => 90 "max_soc" => $this->GetValue('MaxSOC')
]; ];
$this->Publish("feedback-response/$device", json_encode($response)); $this->Publish("feedback-response/$device", json_encode($response));
return; return;
} }
// 2⃣ remote-control-response/deviceId → speichern + Callback
/** -------------------------------------------
* 2⃣ REMOTE CONTROL → write-only Variablen
* ------------------------------------------*/
if ($topic === "remote-control-response/$device") { if ($topic === "remote-control-response/$device") {
$this->SendDebug('Logic', 'Remote-Control-Response erkannt', 0);
$this->SetValue('RemoteControlPayload', $payload); $this->SetValue('RemoteControlPayload', $payload);
$this->HandleRemoteControl($payload);
$json = json_decode($payload, true);
if (is_array($json)) {
if (array_key_exists('power_setpoint', $json)) {
$this->SetValue('PowerSetpoint', (int)$json['power_setpoint']);
}
if (array_key_exists('strategy', $json)) {
$this->SetValue('Strategy', (string)$json['strategy']);
}
}
return; return;
} }
// Optional: andere Topics debuggen
$this->SendDebug('ReceiveData', "Unbekanntes Topic empfangen: $topic", 0);
} }
/**
* Hook für eigene Logik bei Remote-Control-Payload. /** -------------------------------------------
*/ * OPTIONAL CALLBACK
* ------------------------------------------*/
protected function HandleRemoteControl(string $payload): void protected function HandleRemoteControl(string $payload): void
{ {
$this->SendDebug('HandleRemoteControl', "Payload: $payload", 0); $this->SendDebug('RemoteControl', $payload, 0);
// Hier kannst du später deine eigene Logik ergänzen
// z.B. json_decode($payload, true) und darauf reagieren.
} }
} }