266 lines
10 KiB
PHP
266 lines
10 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
class VGT_Sub extends IPSModule
|
|
{
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
|
|
// ---------------------------------------------------------------------
|
|
// 1. MQTT Verbindungsdaten (werden an das Gateway durchgereicht)
|
|
// ---------------------------------------------------------------------
|
|
$this->RegisterPropertyString('MQTTBaseTopic', 'Test VGT_Steuerung/Komm');
|
|
$this->RegisterPropertyString('MQTTClientID', 'symcon_vgt_client');
|
|
$this->RegisterPropertyString('MQTTUser', '');
|
|
$this->RegisterPropertyString('MQTTPassword', '');
|
|
$this->RegisterPropertyBoolean('UpdateGatewayConfig', true); // Schalter: Sollen wir das Gateway überschreiben?
|
|
|
|
// ---------------------------------------------------------------------
|
|
// 2. Hardware Verknüpfungen
|
|
// ---------------------------------------------------------------------
|
|
$this->RegisterPropertyInteger('SourceSoC', 0);
|
|
$this->RegisterPropertyInteger('SourcePowerProd', 0);
|
|
$this->RegisterPropertyInteger('SourceIsReady', 0);
|
|
$this->RegisterPropertyInteger('SourceIsRunning', 0);
|
|
$this->RegisterPropertyInteger('SourceMinSoC', 0);
|
|
$this->RegisterPropertyInteger('SourceMaxSoC', 0);
|
|
$this->RegisterPropertyInteger('TargetControlVar', 0);
|
|
|
|
// ---------------------------------------------------------------------
|
|
// 3. Interne Variablen
|
|
// ---------------------------------------------------------------------
|
|
$this->RegisterVariableString('Strategy', 'Strategy', '', 10);
|
|
$this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 20);
|
|
|
|
// Visu
|
|
$this->RegisterVariableFloat('Visu_SoC', 'State of Charge', '~Intensity.100', 30);
|
|
$this->RegisterVariableFloat('Visu_Production', 'Power Production', '~Watt', 40);
|
|
$this->RegisterVariableBoolean('Visu_IsReady', 'Is Ready', '~Switch', 50);
|
|
|
|
$this->EnableAction('Strategy');
|
|
$this->EnableAction('PowerSetpoint');
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
|
|
// ---------------------------------------------------------------------
|
|
// A. Filter setzen (Automatisches Subscribe)
|
|
// ---------------------------------------------------------------------
|
|
$topic = $this->ReadPropertyString('MQTTBaseTopic');
|
|
if($topic !== "") {
|
|
$cleanTopic = preg_quote($topic, '/');
|
|
// Wir abonnieren ALLES unterhalb des BaseTopics
|
|
$this->SetReceiveDataFilter(".*\"Topic\":\"" . $cleanTopic . "/.*\".*");
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// B. Gateway (IO) Konfiguration aktualisieren
|
|
// ---------------------------------------------------------------------
|
|
if ($this->ReadPropertyBoolean('UpdateGatewayConfig')) {
|
|
$this->ConfigureGateway();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// C. Events registrieren
|
|
// ---------------------------------------------------------------------
|
|
$socID = $this->ReadPropertyInteger('SourceSoC');
|
|
if (IPS_VariableExists($socID)) {
|
|
$this->RegisterMessage($socID, VM_UPDATE);
|
|
}
|
|
$this->RegisterMessage($this->GetIDForIdent('Strategy'), VM_UPDATE);
|
|
$this->RegisterMessage($this->GetIDForIdent('PowerSetpoint'), VM_UPDATE);
|
|
}
|
|
|
|
/**
|
|
* Diese Funktion sucht die IO-Instanz (MQTT Client) und setzt User/Passwort
|
|
*/
|
|
private function ConfigureGateway()
|
|
{
|
|
// 1. Suche Parent (MQTT Splitter)
|
|
$instance = IPS_GetInstance($this->InstanceID);
|
|
$splitterID = $instance['ConnectionID'];
|
|
|
|
if ($splitterID > 0) {
|
|
// 2. Suche Grandparent (MQTT Client / IO)
|
|
$splitter = IPS_GetInstance($splitterID);
|
|
$ioID = $splitter['ConnectionID'];
|
|
|
|
if ($ioID > 0) {
|
|
// Prüfen, ob es ein MQTT Client ist (GUID prüfen oder einfach Eigenschaften setzen)
|
|
// Wir setzen die Eigenschaften, wenn sie existieren.
|
|
|
|
$user = $this->ReadPropertyString('MQTTUser');
|
|
$pass = $this->ReadPropertyString('MQTTPassword');
|
|
$client = $this->ReadPropertyString('MQTTClientID');
|
|
|
|
$hasChanged = false;
|
|
|
|
// ClientID (Achtung: Symcon MQTT Splitter nutzt ClientID meist im Splitter, nicht IO)
|
|
// Wir prüfen, wo die Property existiert.
|
|
|
|
// Versuch 1: IO Konfigurieren (User/Pass)
|
|
if (@IPS_GetProperty($ioID, 'Username') !== false) {
|
|
if (IPS_GetProperty($ioID, 'Username') != $user) { IPS_SetProperty($ioID, 'Username', $user); $hasChanged = true; }
|
|
if (IPS_GetProperty($ioID, 'Password') != $pass) { IPS_SetProperty($ioID, 'Password', $pass); $hasChanged = true; }
|
|
}
|
|
|
|
// Versuch 2: ClientID wird oft im Splitter (dem direkten Parent) konfiguriert
|
|
if (@IPS_GetProperty($splitterID, 'ClientID') !== false) {
|
|
if (IPS_GetProperty($splitterID, 'ClientID') != $client) { IPS_SetProperty($splitterID, 'ClientID', $client); $hasChanged = true; } // Splitter neu laden
|
|
if ($hasChanged) IPS_ApplyChanges($splitterID);
|
|
}
|
|
|
|
// Änderungen am IO übernehmen
|
|
if ($hasChanged) {
|
|
IPS_ApplyChanges($ioID);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// -------------------------------------------------------------------------
|
|
// MQTT Logik (unverändert)
|
|
// -------------------------------------------------------------------------
|
|
public function ReceiveData($JSONString)
|
|
{
|
|
$data = json_decode($JSONString);
|
|
$payload = utf8_decode($data->Payload);
|
|
$topic = $data->Topic;
|
|
$baseTopic = $this->ReadPropertyString('MQTTBaseTopic');
|
|
|
|
// Debug
|
|
$this->SendDebug('MQTT Received', "Topic: $topic | Payload: $payload", 0);
|
|
|
|
if ($topic === $baseTopic . '/Writebefehl') {
|
|
$this->ProcessWriteCommand($payload);
|
|
}
|
|
elseif ($topic === $baseTopic . '/Lesebefehl') {
|
|
$this->ProcessReadCommand();
|
|
}
|
|
}
|
|
|
|
public function MessageSink($TimeStamp, $SenderID, $Message, $Data)
|
|
{
|
|
if ($Message == VM_UPDATE) {
|
|
$socID = $this->ReadPropertyInteger('SourceSoC');
|
|
if ($SenderID == $socID) {
|
|
$this->SetValue('Visu_SoC', $Data[0]);
|
|
}
|
|
$this->ExecuteControlLogic();
|
|
}
|
|
}
|
|
|
|
public function RequestAction($Ident, $Value)
|
|
{
|
|
switch ($Ident) {
|
|
case 'Strategy':
|
|
case 'PowerSetpoint':
|
|
$this->SetValue($Ident, $Value);
|
|
$this->ExecuteControlLogic();
|
|
break;
|
|
}
|
|
}
|
|
|
|
private function ProcessWriteCommand($jsonInput)
|
|
{
|
|
if ($jsonInput == "") return;
|
|
$data = json_decode($jsonInput, true);
|
|
if ($data === null) return;
|
|
|
|
if (isset($data['power_setpoint'])) {
|
|
$val = $data['power_setpoint'];
|
|
if ($val > 6000) $val = 6000;
|
|
elseif ($val < -6000) $val = -6000;
|
|
$this->SetValue('PowerSetpoint', $val);
|
|
}
|
|
if (isset($data['strategy'])) {
|
|
$this->SetValue('Strategy', $data['strategy']);
|
|
}
|
|
|
|
$this->ExecuteControlLogic();
|
|
|
|
$output = [
|
|
'power_setpoint' => $this->GetValue('PowerSetpoint'),
|
|
'strategy' => $this->GetValue('Strategy')
|
|
];
|
|
// Antwort senden
|
|
$this->SendMQTT($this->ReadPropertyString('MQTTBaseTopic') . '/Writebefehl Antwort', json_encode($output));
|
|
}
|
|
|
|
private function ProcessReadCommand()
|
|
{
|
|
$data = $this->GatherHardwareData();
|
|
$this->SetValue('Visu_Production', $data['raw_prod']);
|
|
$this->SetValue('Visu_IsReady', $data['is_ready']);
|
|
|
|
$mqttData = [
|
|
'power_production' => -1 * $data['raw_prod'],
|
|
'is_ready' => $data['is_ready'],
|
|
'is_running' => $data['is_running'],
|
|
'state_of_charge' => $data['soc'],
|
|
'min_soc' => $data['min_soc'],
|
|
'max_soc' => $data['max_soc']
|
|
];
|
|
// Antwort senden
|
|
$this->SendMQTT($this->ReadPropertyString('MQTTBaseTopic') . '/Lesebefehl Antwort', json_encode($mqttData));
|
|
}
|
|
|
|
private function ExecuteControlLogic()
|
|
{
|
|
$targetID = $this->ReadPropertyInteger('TargetControlVar');
|
|
if (!IPS_VariableExists($targetID)) return;
|
|
|
|
$strategy = $this->GetValue('Strategy');
|
|
$setpoint = $this->GetValue('PowerSetpoint');
|
|
$socID = $this->ReadPropertyInteger('SourceSoC');
|
|
$soc = IPS_VariableExists($socID) ? GetValue($socID) : 0;
|
|
|
|
if ($strategy == "activate") {
|
|
RequestAction($targetID, $setpoint);
|
|
} elseif ($strategy == "stop") {
|
|
RequestAction($targetID, 0);
|
|
} else {
|
|
if ((int)$soc == 50) {
|
|
RequestAction($targetID, 0);
|
|
} elseif ($soc < 50) {
|
|
RequestAction($targetID, 2500);
|
|
} else {
|
|
RequestAction($targetID, -2500);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function GatherHardwareData()
|
|
{
|
|
$safeGet = function($propName) {
|
|
$id = $this->ReadPropertyInteger($propName);
|
|
return IPS_VariableExists($id) ? GetValue($id) : 0;
|
|
};
|
|
return [
|
|
'raw_prod' => $safeGet('SourcePowerProd'),
|
|
'is_ready' => (bool)$safeGet('SourceIsReady'),
|
|
'is_running' => (bool)$safeGet('SourceIsRunning'),
|
|
'soc' => $safeGet('SourceSoC'),
|
|
'min_soc' => $safeGet('SourceMinSoC'),
|
|
'max_soc' => $safeGet('SourceMaxSoC'),
|
|
];
|
|
}
|
|
|
|
protected function SendMQTT($Topic, $Payload)
|
|
{
|
|
// Standard DataID für MQTT Publish an Splitter
|
|
$Data['DataID'] = '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}';
|
|
$Data['PacketType'] = 3;
|
|
$Data['QualityOfService'] = 0;
|
|
$Data['Retain'] = false;
|
|
$Data['Topic'] = $Topic;
|
|
$Data['Payload'] = $Payload;
|
|
$JSON = json_encode($Data);
|
|
$this->SendDataToParent($JSON);
|
|
}
|
|
}
|
|
?>
|