no message
This commit is contained in:
@@ -10,19 +10,48 @@ class Int_VGT extends IPSModule
|
||||
|
||||
$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}');
|
||||
}
|
||||
|
||||
|
||||
public function ApplyChanges()
|
||||
{
|
||||
parent::ApplyChanges();
|
||||
@@ -32,47 +61,55 @@ class Int_VGT extends IPSModule
|
||||
|
||||
$device = $this->ReadPropertyString('DeviceID');
|
||||
if ($device === '') {
|
||||
$this->SendDebug('ApplyChanges', 'Kein DeviceID gesetzt – keine Subscriptions', 0);
|
||||
$this->SendDebug('ApplyChanges', 'Keine DeviceID gesetzt', 0);
|
||||
return;
|
||||
}
|
||||
|
||||
// Topics:
|
||||
// 1) feedback-request/deviceId
|
||||
// 2) remote-control-response/deviceId
|
||||
|
||||
// Subscriptions setzen
|
||||
$this->Subscribe("feedback-request/$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
|
||||
{
|
||||
$this->SendDebug('Subscribe', "Topic: $topic", 0);
|
||||
$this->SendDebug('Subscribe', $topic, 0);
|
||||
|
||||
$packet = [
|
||||
'PacketType' => 8, // SUBSCRIBE
|
||||
'PacketType' => 8, // SUBSCRIBE
|
||||
'QualityOfService' => 0,
|
||||
'Retain' => false,
|
||||
'Topic' => $topic,
|
||||
'Payload' => '' // bei Subscribe leer
|
||||
'Payload' => ''
|
||||
];
|
||||
|
||||
$this->SendDataToParent(json_encode([
|
||||
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' // TX zum MQTT Server
|
||||
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
|
||||
] + $packet));
|
||||
}
|
||||
|
||||
/**
|
||||
* Publish an MQTT-Server (Splitter).
|
||||
*/
|
||||
|
||||
/** -------------------------------------------
|
||||
* MQTT PUBLISH
|
||||
* ------------------------------------------*/
|
||||
private function Publish(string $topic, string $payload): void
|
||||
{
|
||||
$this->SendDebug('Publish', "Topic: $topic Payload: $payload", 0);
|
||||
|
||||
$packet = [
|
||||
'PacketType' => 3, // PUBLISH
|
||||
'PacketType' => 3, // PUBLISH
|
||||
'QualityOfService' => 0,
|
||||
'Retain' => false,
|
||||
'Topic' => $topic,
|
||||
@@ -80,81 +117,77 @@ class Int_VGT extends IPSModule
|
||||
];
|
||||
|
||||
$this->SendDataToParent(json_encode([
|
||||
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' // TX zum MQTT Server
|
||||
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
|
||||
] + $packet));
|
||||
}
|
||||
|
||||
/**
|
||||
* Wird vom MQTT-Server (Splitter) aufgerufen.
|
||||
*/
|
||||
|
||||
/** -------------------------------------------
|
||||
* RECEIVE DATA
|
||||
* ------------------------------------------*/
|
||||
public function ReceiveData($JSONString)
|
||||
{
|
||||
$this->SendDebug('ReceiveData RAW', $JSONString, 0);
|
||||
|
||||
$data = json_decode($JSONString, true);
|
||||
if (!is_array($data)) {
|
||||
$this->SendDebug('ReceiveData', 'JSON decode fehlgeschlagen', 0);
|
||||
return;
|
||||
}
|
||||
|
||||
// Struktur laut Doku:
|
||||
// {
|
||||
// "DataID": "{7F7632D9-FA40-4F38-8DEA-C83CD4325A32}",
|
||||
// "PacketType": ...,
|
||||
// "QualityOfService": 0,
|
||||
// "Retain": false,
|
||||
// "Topic": "....",
|
||||
// "Payload": "...."
|
||||
// }
|
||||
$topic = $data['Topic'] ?? '';
|
||||
$payload = $data['Payload'] ?? '';
|
||||
|
||||
$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") {
|
||||
$this->SendDebug('Logic', 'Feedback-Request erkannt', 0);
|
||||
|
||||
$response = [
|
||||
'power_production' => 123,
|
||||
'is_ready' => true,
|
||||
'is_running' => true,
|
||||
'state_of_charge' => 50,
|
||||
'min_soc' => 10,
|
||||
'max_soc' => 90
|
||||
"power_production" => $this->GetValue('PowerProduction'),
|
||||
"is_ready" => $this->GetValue('IsReady'),
|
||||
"is_running" => $this->GetValue('IsRunning'),
|
||||
"state_of_charge" => $this->GetValue('StateOfCharge'),
|
||||
"min_soc" => $this->GetValue('MinSOC'),
|
||||
"max_soc" => $this->GetValue('MaxSOC')
|
||||
];
|
||||
|
||||
$this->Publish("feedback-response/$device", json_encode($response));
|
||||
return;
|
||||
}
|
||||
|
||||
// 2️⃣ remote-control-response/deviceId → speichern + Callback
|
||||
|
||||
/** -------------------------------------------
|
||||
* 2️⃣ REMOTE CONTROL → write-only Variablen
|
||||
* ------------------------------------------*/
|
||||
if ($topic === "remote-control-response/$device") {
|
||||
$this->SendDebug('Logic', 'Remote-Control-Response erkannt', 0);
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
// 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
|
||||
{
|
||||
$this->SendDebug('HandleRemoteControl', "Payload: $payload", 0);
|
||||
|
||||
// Hier kannst du später deine eigene Logik ergänzen
|
||||
// z.B. json_decode($payload, true) und darauf reagieren.
|
||||
$this->SendDebug('RemoteControl', $payload, 0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user