Files
Symcon_Belevo_Energiemanage…/VGT_Sub/module.php
2025-11-28 08:52:04 +01:00

158 lines
4.9 KiB
PHP

<?php
declare(strict_types=1);
class VGT_Sub extends IPSModule
{
public function Create()
{
parent::Create();
$this->RegisterPropertyString('DeviceID', '');
// Status Variablen (bearbeitbar)
$this->RegisterVariableFloat('PowerProduction', 'Power Production', '', 10);
$this->EnableAction('PowerProduction');
$this->RegisterVariableFloat('StateOfCharge', 'State of Charge', '', 11);
$this->EnableAction('StateOfCharge');
$this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 12);
$this->EnableAction('IsRunning');
$this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 13);
$this->EnableAction('IsReady');
$this->RegisterVariableFloat('MinSoc', 'Min SoC', '', 14);
$this->EnableAction('MinSoc');
$this->RegisterVariableFloat('MaxSoc', 'Max SoC', '', 15);
$this->EnableAction('MaxSoc');
// Remote-Control Variablen (readonly)
$this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 30);
IPS_SetVariableCustomAction($this->GetIDForIdent('PowerSetpoint'), 0);
$this->RegisterVariableString('Strategy', 'Strategy', '', 31);
IPS_SetVariableCustomAction($this->GetIDForIdent('Strategy'), 0);
}
public function ApplyChanges()
{
parent::ApplyChanges();
// RICHTIGER Parent: Offizieller MQTT Client
$this->ConnectParent('{3CFF0FD9-A6AF-4A9A-8C41-62E71D93AF40}');
$this->SubscribeTopics();
}
private function SubscribeTopics()
{
$device = $this->ReadPropertyString('DeviceID');
if ($device == '') {
return;
}
$topics = [
"feedback-request/$device",
"remote-control-request/$device"
];
foreach ($topics as $topic) {
$this->SendDebug("SUBSCRIBE", $topic, 0);
$this->SendDataToParent(json_encode([
"DataID" => "{3CFF0FD9-A6AF-4A9A-8C41-62E71D93AF40}",
"PacketType" => 8, // Subscribe
"Topic" => $topic,
"QoS" => 0
]));
}
}
public function ReceiveData($JSONString)
{
$data = json_decode($JSONString, true);
$topic = $data['Topic'] ?? '';
$payload = $data['Payload'] ?? '';
$this->SendDebug("RX TOPIC", $topic, 0);
$this->SendDebug("RX PAYLOAD", $payload, 0);
$device = $this->ReadPropertyString('DeviceID');
if ($topic === "feedback-request/$device") {
$this->HandleFeedbackRequest($device);
}
if ($topic === "remote-control-request/$device") {
$this->HandleRemoteControlRequest($device, $payload);
}
}
private function HandleFeedbackRequest($device)
{
$response = [
"power_production" => GetValue($this->GetIDForIdent('PowerProduction')),
"state_of_charge" => GetValue($this->GetIDForIdent('StateOfCharge')),
"is_running" => GetValue($this->GetIDForIdent('IsRunning')),
"is_ready" => GetValue($this->GetIDForIdent('IsReady')),
"min_soc" => GetValue($this->GetIDForIdent('MinSoc')),
"max_soc" => GetValue($this->GetIDForIdent('MaxSoc'))
];
$json = json_encode($response);
$topic = "feedback-response/$device";
$this->SendDebug("TX FEEDBACK", $json, 0);
$this->SendDataToParent(json_encode([
"DataID" => "{3CFF0FD9-A6AF-4A9A-8C41-62E71D93AF40}",
"PacketType" => 3, // Publish
"Topic" => $topic,
"Payload" => $json,
"QoS" => 0,
"Retain" => false
]));
}
private function HandleRemoteControlRequest($device, $payload)
{
$json = @json_decode($payload, true);
if (!is_array($json)) {
$this->SendDebug("ERROR", "Ungültiges JSON empfangen!", 0);
return;
}
if (isset($json['power_setpoint'])) {
SetValue($this->GetIDForIdent('PowerSetpoint'), (int)$json['power_setpoint']);
}
if (isset($json['strategy'])) {
SetValueString($this->GetIDForIdent('Strategy'), (string)$json['strategy']);
}
$replyTopic = "remote-control-response/$device";
$replyPayload = json_encode($json);
$this->SendDebug("TX REMOTE", $replyPayload, 0);
$this->SendDataToParent(json_encode([
"DataID" => "{3CFF0FD9-A6AF-4A9A-8C41-62E71D93AF40}",
"PacketType" => 3, // Publish
"Topic" => $replyTopic,
"Payload" => $replyPayload,
"QoS" => 0,
"Retain" => false
]));
}
public function RequestAction($Ident, $Value)
{
SetValue($this->GetIDForIdent($Ident), $Value);
}
}
?>