no message

This commit is contained in:
2025-11-25 07:48:33 +01:00
parent f064840987
commit 7983a921f5
2 changed files with 110 additions and 131 deletions

View File

@@ -5,6 +5,10 @@
"name": "DeviceID",
"caption": "Device ID"
},
{
"type": "Label",
"caption": "Variablen zuordnen"
},
{
"type": "SelectVariable",
"name": "VarPowerProduction",

View File

@@ -8,206 +8,181 @@ class VGT_Sub extends IPSModule
{
parent::Create();
// ---------------------------------------------------------
// CONFIG PROPERTIES (for form.json)
// ---------------------------------------------------------
$this->RegisterPropertyString('DeviceID', '');
/** -------------------------
* STATUS Variablen
* ------------------------- */
$this->RegisterVariableInteger('PowerProduction', 'Power Production', '', 10);
$this->EnableAction('PowerProduction');
$this->RegisterPropertyInteger('VarPowerProduction', 0);
$this->RegisterPropertyInteger('VarStateOfCharge', 0);
$this->RegisterPropertyInteger('VarIsRunning', 0);
$this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 11);
$this->EnableAction('IsReady');
$this->RegisterPropertyInteger('MinSOC', 20);
$this->RegisterPropertyInteger('MaxSOC', 80);
$this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 12);
$this->EnableAction('IsRunning');
// ---------------------------------------------------------
// INTERNAL VARIABLES
// ---------------------------------------------------------
$this->RegisterVariableFloat('PowerSetpoint', 'Power Setpoint', '', 20);
$this->RegisterVariableFloat('PowerSetpointAbs', 'Power Setpoint (ABS)', '', 21);
$this->RegisterVariableFloat('PowerSetpointPositive', 'Power Setpoint Positive', '', 22);
$this->RegisterVariableFloat('PowerSetpointNegative', 'Power Setpoint Negative', '', 23);
$this->RegisterVariableInteger('StateOfCharge', 'State of Charge', '', 13);
$this->EnableAction('StateOfCharge');
$this->RegisterVariableString('Strategy', 'Strategy', '', 24);
$this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 25);
$this->RegisterVariableInteger('MinSOC', 'Min SOC', '', 14);
$this->EnableAction('MinSOC');
$this->RegisterVariableInteger('MaxSOC', 'Max SOC', '', 15);
$this->EnableAction('MaxSOC');
/** -------------------------
* REMOTE CONTROL
* ------------------------- */
$this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 20);
$this->RegisterVariableString('Strategy', 'Strategy', '', 21);
$this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 30);
/** -------------------------
* FEEDBACK REQUEST
* ------------------------- */
$this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request Payload', '', 40);
$this->RegisterVariableString('FeedbackResponsePayload', 'Feedback Response Payload', '', 41);
}
public function ApplyChanges()
{
parent::ApplyChanges();
$this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}'); // Dein Splitter
// MQTT Splitter verbinden
$this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}');
$device = $this->ReadPropertyString('DeviceID');
if ($device !== '') {
$this->Subscribe("feedback-request/$device");
$this->Subscribe("remote-control-request/$device");
}
}
/** ----------------------------------------------------------
* REQUEST ACTION
* ----------------------------------------------------------*/
// ---------------------------------------------------------
// REQUESTACTION (nur Setzen)
// ---------------------------------------------------------
public function RequestAction($Ident, $Value)
{
$this->SetValue($Ident, $Value);
$device = $this->ReadPropertyString('DeviceID');
if ($device === '') {
return;
}
switch ($Ident) {
case 'PowerProduction':
case 'IsReady':
case 'IsRunning':
case 'StateOfCharge':
case 'MinSOC':
case 'MaxSOC':
$payload = json_encode([
'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("status/$device", $payload);
break;
}
}
/** ----------------------------------------------------------
* SUBSCRIBE
* ----------------------------------------------------------*/
// ---------------------------------------------------------
// MQTT SUBSCRIBE
// ---------------------------------------------------------
private function Subscribe(string $topic): void
{
if (!$this->HasActiveParent()) {
$this->SendDebug("MQTT", "Parent offline subscribe skipped: $topic", 0);
return;
}
$this->SendDataToParent(json_encode([
'DataID' => '{A1B5C433-4F17-462D-AD71-383F5BBE4F5A}', // DataID deines Splitters → SUBSCRIBE
'DataID' => '{A1B5C433-4F17-462D-AD71-383F5BBE4F5A}',
'Type' => 'SUBSCRIBE',
'Topic' => $topic
]));
$this->SendDebug('MQTT', "Subscribed: $topic", 0);
}
/** ----------------------------------------------------------
* PUBLISH
* ----------------------------------------------------------*/
// ---------------------------------------------------------
// MQTT PUBLISH
// ---------------------------------------------------------
private function Publish(string $topic, string $payload): void
{
if (!$this->HasActiveParent()) {
$this->SendDebug("MQTT", "Parent offline publish skipped: $topic", 0);
return;
}
$this->SendDataToParent(json_encode([
'DataID' => '{A1B5C433-4F17-462D-AD71-383F5BBE4F5A}', // DataID deines Splitters → PUBLISH
'DataID' => '{A1B5C433-4F17-462D-AD71-383F5BBE4F5A}',
'Type' => 'PUBLISH',
'Topic' => $topic,
'Payload' => $payload
]));
$this->SendDebug('MQTT', "Publish → $topic : $payload", 0);
}
/** ----------------------------------------------------------
* RECEIVE DATA
* ----------------------------------------------------------*/
// ---------------------------------------------------------
// RECEIVE MQTT DATA
// ---------------------------------------------------------
public function ReceiveData($JSONString)
{
$data = json_decode($JSONString, true);
if (!is_array($data)) return;
if (!is_array($data)) {
return;
}
if (($data['Type'] ?? '') !== 'MESSAGE') return;
/** --- MQTT CONNECTED EVENT vom Splitter --- */
if (($data['Type'] ?? '') === 'MQTT_CONNECTED') {
$this->SendDebug("MQTT", "Splitter connected → re-subscribing", 0);
$topic = $data['Topic'];
$payload = $data['Payload'];
$device = $this->ReadPropertyString('DeviceID');
$device = $this->ReadPropertyString('DeviceID');
if ($device === '') return;
if ($device !== '') {
$this->Subscribe("feedback-request/$device");
$this->Subscribe("remote-control-request/$device");
}
return;
}
/** --- Normale MQTT Daten vom Splitter --- */
if (($data['Type'] ?? '') !== 'MESSAGE') {
return;
}
$topic = $data['Topic'] ?? '';
$payload = $data['Payload'] ?? '';
$device = $this->ReadPropertyString('DeviceID');
$this->SendDebug("MQTT", "RX → $topic : $payload", 0);
if ($device === '') {
return;
}
/** -------------------------------------------
* FEEDBACK REQUEST
* ------------------------------------------*/
// -----------------------------------------------
// FEEDBACK REQUEST
// -----------------------------------------------
if ($topic === "feedback-request/$device") {
$this->SetValue('FeedbackRequestPayload', $payload);
$json = json_decode($payload, true);
if (!is_array($json)) {
$json = [];
}
$reply = $this->BuildFeedbackResponse();
$json = json_encode($reply);
$response = array_merge($json, [
"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));
$this->SetValue('FeedbackResponsePayload', $json);
$this->Publish("feedback-response/$device", $json);
return;
}
/** -------------------------------------------
* REMOTE CONTROL REQUEST
* ------------------------------------------*/
// -----------------------------------------------
// REMOTE CONTROL REQUEST
// -----------------------------------------------
if ($topic === "remote-control-request/$device") {
$this->SetValue('RemoteControlPayload', $payload);
$json = json_decode($payload, true);
if (!is_array($json)) return;
if (is_array($json)) {
if (isset($json['power_setpoint'])) {
$this->SetValue('PowerSetpoint', (int)$json['power_setpoint']);
}
if (isset($json['strategy'])) {
$this->SetValue('Strategy', (string)$json['strategy']);
// Power Setpoint Handling
if (isset($json['power_setpoint'])) {
$ps = floatval($json['power_setpoint']);
$this->SetValue('PowerSetpoint', $ps);
$this->SetValue('PowerSetpointAbs', abs($ps));
if ($ps > 0) {
$this->SetValue('PowerSetpointPositive', $ps);
$this->SetValue('PowerSetpointNegative', 0);
} elseif ($ps < 0) {
$this->SetValue('PowerSetpointNegative', $ps);
$this->SetValue('PowerSetpointPositive', 0);
} else {
$this->SetValue('PowerSetpointPositive', 0);
$this->SetValue('PowerSetpointNegative', 0);
}
}
// Strategy Handling
if (isset($json['strategy'])) {
$this->SetValue('Strategy', $json['strategy']);
}
$this->Publish("remote-control-response/$device", $payload);
}
}
// ---------------------------------------------------------
// FEEDBACK RESPONSE BUILDER
// ---------------------------------------------------------
private function BuildFeedbackResponse(): array
{
$idProd = $this->ReadPropertyInteger('VarPowerProduction');
$idSOC = $this->ReadPropertyInteger('VarStateOfCharge');
$idRun = $this->ReadPropertyInteger('VarIsRunning');
$min = $this->ReadPropertyInteger('MinSOC');
$max = $this->ReadPropertyInteger('MaxSOC');
$soc = ($idSOC > 0) ? GetValue($idSOC) : 0;
$isRunning = ($idRun > 0) ? GetValue($idRun) : false;
$isReady = ($isRunning && ($soc >= $min) && ($soc <= $max));
return [
'power_production' => ($idProd > 0) ? GetValue($idProd) : 0,
'is_running' => $isRunning,
'state_of_charge' => $soc,
'min_soc' => $min,
'max_soc' => $max,
'is_ready' => $isReady
];
}
}