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

107 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
class VGT_Sub extends IPSModule
{
public function Create()
{
parent::Create();
$this->RegisterPropertyString('DeviceID', '');
$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');
$this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 20);
IPS_SetVariableCustomAction($this->GetIDForIdent('PowerSetpoint'), 0);
$this->RegisterVariableString('Strategy', 'Strategy', '', 21);
IPS_SetVariableCustomAction($this->GetIDForIdent('Strategy'), 0);
}
public function ForwardData($JSONString)
{
$data = json_decode($JSONString, true);
if (!isset($data['Topic']) || !isset($data['Payload'])) {
return '';
}
$topic = $data['Topic'];
$payload = $data['Payload'];
$device = $this->ReadPropertyString('DeviceID');
if ($topic === "feedback-request/$device") {
$this->handleFeedback($device);
}
if ($topic === "remote-control-request/$device") {
$this->handleRemote($device, $payload);
}
return '';
}
private function handleFeedback($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'))
];
$this->publish("feedback-response/$device", json_encode($response));
}
private function handleRemote($device, $payload)
{
$in = json_decode($payload, true);
if (!is_array($in)) {
return;
}
if (isset($in['power_setpoint'])) {
SetValue($this->GetIDForIdent('PowerSetpoint'), (int)$in['power_setpoint']);
}
if (isset($in['strategy'])) {
SetValueString($this->GetIDForIdent('Strategy'), (string)$in['strategy']);
}
$this->publish("remote-control-response/$device", json_encode($in));
}
private function publish($topic, $payload)
{
$this->SendDataToParent(json_encode([
"DataID" => "{018EF6B5-AB94-40C6-AA53-46943E824ACF}",
"Topic" => $topic,
"Payload" => $payload
]));
}
public function RequestAction($Ident, $Value)
{
SetValue($this->GetIDForIdent($Ident), $Value);
}
}
?>