no message
This commit is contained in:
@@ -5,6 +5,10 @@
|
|||||||
"name": "DeviceID",
|
"name": "DeviceID",
|
||||||
"caption": "Device ID"
|
"caption": "Device ID"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "Label",
|
||||||
|
"caption": "Variablen zuordnen"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "SelectVariable",
|
"type": "SelectVariable",
|
||||||
"name": "VarPowerProduction",
|
"name": "VarPowerProduction",
|
||||||
|
|||||||
+126
-151
@@ -8,206 +8,181 @@ class VGT_Sub extends IPSModule
|
|||||||
{
|
{
|
||||||
parent::Create();
|
parent::Create();
|
||||||
|
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
// CONFIG PROPERTIES (for form.json)
|
||||||
|
// ---------------------------------------------------------
|
||||||
$this->RegisterPropertyString('DeviceID', '');
|
$this->RegisterPropertyString('DeviceID', '');
|
||||||
|
|
||||||
/** -------------------------
|
$this->RegisterPropertyInteger('VarPowerProduction', 0);
|
||||||
* STATUS Variablen
|
$this->RegisterPropertyInteger('VarStateOfCharge', 0);
|
||||||
* ------------------------- */
|
$this->RegisterPropertyInteger('VarIsRunning', 0);
|
||||||
$this->RegisterVariableInteger('PowerProduction', 'Power Production', '', 10);
|
|
||||||
$this->EnableAction('PowerProduction');
|
|
||||||
|
|
||||||
$this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 11);
|
$this->RegisterPropertyInteger('MinSOC', 20);
|
||||||
$this->EnableAction('IsReady');
|
$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->RegisterVariableString('Strategy', 'Strategy', '', 24);
|
||||||
$this->EnableAction('StateOfCharge');
|
$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('FeedbackRequestPayload', 'Feedback Request Payload', '', 40);
|
||||||
|
$this->RegisterVariableString('FeedbackResponsePayload', 'Feedback Response Payload', '', 41);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function ApplyChanges()
|
public function ApplyChanges()
|
||||||
{
|
{
|
||||||
parent::ApplyChanges();
|
parent::ApplyChanges();
|
||||||
$this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}'); // Dein Splitter
|
|
||||||
}
|
|
||||||
|
|
||||||
/** ----------------------------------------------------------
|
// MQTT Splitter verbinden
|
||||||
* REQUEST ACTION
|
$this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}');
|
||||||
* ----------------------------------------------------------*/
|
|
||||||
public function RequestAction($Ident, $Value)
|
|
||||||
{
|
|
||||||
$this->SetValue($Ident, $Value);
|
|
||||||
|
|
||||||
$device = $this->ReadPropertyString('DeviceID');
|
$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
|
|
||||||
* ----------------------------------------------------------*/
|
|
||||||
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
|
|
||||||
'Type' => 'SUBSCRIBE',
|
|
||||||
'Topic' => $topic
|
|
||||||
]));
|
|
||||||
|
|
||||||
$this->SendDebug('MQTT', "Subscribed: $topic", 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** ----------------------------------------------------------
|
|
||||||
* 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
|
|
||||||
'Type' => 'PUBLISH',
|
|
||||||
'Topic' => $topic,
|
|
||||||
'Payload' => $payload
|
|
||||||
]));
|
|
||||||
|
|
||||||
$this->SendDebug('MQTT', "Publish → $topic : $payload", 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** ----------------------------------------------------------
|
|
||||||
* RECEIVE DATA
|
|
||||||
* ----------------------------------------------------------*/
|
|
||||||
public function ReceiveData($JSONString)
|
|
||||||
{
|
|
||||||
$data = json_decode($JSONString, true);
|
|
||||||
|
|
||||||
if (!is_array($data)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** --- MQTT CONNECTED EVENT vom Splitter --- */
|
|
||||||
if (($data['Type'] ?? '') === 'MQTT_CONNECTED') {
|
|
||||||
$this->SendDebug("MQTT", "Splitter connected → re-subscribing", 0);
|
|
||||||
|
|
||||||
$device = $this->ReadPropertyString('DeviceID');
|
|
||||||
|
|
||||||
if ($device !== '') {
|
if ($device !== '') {
|
||||||
$this->Subscribe("feedback-request/$device");
|
$this->Subscribe("feedback-request/$device");
|
||||||
$this->Subscribe("remote-control-request/$device");
|
$this->Subscribe("remote-control-request/$device");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
// REQUESTACTION (nur Setzen)
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
public function RequestAction($Ident, $Value)
|
||||||
|
{
|
||||||
|
$this->SetValue($Ident, $Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
// MQTT SUBSCRIBE
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
private function Subscribe(string $topic): void
|
||||||
|
{
|
||||||
|
if (!$this->HasActiveParent()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** --- Normale MQTT Daten vom Splitter --- */
|
$this->SendDataToParent(json_encode([
|
||||||
if (($data['Type'] ?? '') !== 'MESSAGE') {
|
'DataID' => '{A1B5C433-4F17-462D-AD71-383F5BBE4F5A}',
|
||||||
|
'Type' => 'SUBSCRIBE',
|
||||||
|
'Topic' => $topic
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
// MQTT PUBLISH
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
private function Publish(string $topic, string $payload): void
|
||||||
|
{
|
||||||
|
if (!$this->HasActiveParent()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$topic = $data['Topic'] ?? '';
|
$this->SendDataToParent(json_encode([
|
||||||
$payload = $data['Payload'] ?? '';
|
'DataID' => '{A1B5C433-4F17-462D-AD71-383F5BBE4F5A}',
|
||||||
|
'Type' => 'PUBLISH',
|
||||||
|
'Topic' => $topic,
|
||||||
|
'Payload' => $payload
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
// RECEIVE MQTT DATA
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
public function ReceiveData($JSONString)
|
||||||
|
{
|
||||||
|
$data = json_decode($JSONString, true);
|
||||||
|
if (!is_array($data)) return;
|
||||||
|
|
||||||
|
if (($data['Type'] ?? '') !== 'MESSAGE') return;
|
||||||
|
|
||||||
|
$topic = $data['Topic'];
|
||||||
|
$payload = $data['Payload'];
|
||||||
$device = $this->ReadPropertyString('DeviceID');
|
$device = $this->ReadPropertyString('DeviceID');
|
||||||
|
|
||||||
$this->SendDebug("MQTT", "RX → $topic : $payload", 0);
|
if ($device === '') return;
|
||||||
|
|
||||||
if ($device === '') {
|
// -----------------------------------------------
|
||||||
return;
|
// FEEDBACK REQUEST
|
||||||
}
|
// -----------------------------------------------
|
||||||
|
|
||||||
/** -------------------------------------------
|
|
||||||
* FEEDBACK REQUEST
|
|
||||||
* ------------------------------------------*/
|
|
||||||
if ($topic === "feedback-request/$device") {
|
if ($topic === "feedback-request/$device") {
|
||||||
|
|
||||||
$this->SetValue('FeedbackRequestPayload', $payload);
|
$this->SetValue('FeedbackRequestPayload', $payload);
|
||||||
|
|
||||||
$json = json_decode($payload, true);
|
$reply = $this->BuildFeedbackResponse();
|
||||||
if (!is_array($json)) {
|
$json = json_encode($reply);
|
||||||
$json = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
$response = array_merge($json, [
|
$this->SetValue('FeedbackResponsePayload', $json);
|
||||||
"power_production" => $this->GetValue('PowerProduction'),
|
$this->Publish("feedback-response/$device", $json);
|
||||||
"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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** -------------------------------------------
|
// -----------------------------------------------
|
||||||
* REMOTE CONTROL REQUEST
|
// REMOTE CONTROL REQUEST
|
||||||
* ------------------------------------------*/
|
// -----------------------------------------------
|
||||||
if ($topic === "remote-control-request/$device") {
|
if ($topic === "remote-control-request/$device") {
|
||||||
|
|
||||||
$this->SetValue('RemoteControlPayload', $payload);
|
$this->SetValue('RemoteControlPayload', $payload);
|
||||||
|
|
||||||
$json = json_decode($payload, true);
|
$json = json_decode($payload, true);
|
||||||
|
if (!is_array($json)) return;
|
||||||
|
|
||||||
if (is_array($json)) {
|
// Power Setpoint Handling
|
||||||
if (isset($json['power_setpoint'])) {
|
if (isset($json['power_setpoint'])) {
|
||||||
$this->SetValue('PowerSetpoint', (int)$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'])) {
|
if (isset($json['strategy'])) {
|
||||||
$this->SetValue('Strategy', (string)$json['strategy']);
|
$this->SetValue('Strategy', $json['strategy']);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->Publish("remote-control-response/$device", $payload);
|
$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
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user