145 lines
4.6 KiB
PHP
145 lines
4.6 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
class VGT_Sub extends IPSModule
|
||
{
|
||
private bool $Subscribed = false;
|
||
|
||
public function Create()
|
||
{
|
||
parent::Create();
|
||
|
||
// MQTT CLIENT – NICHT Splitter!
|
||
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
||
|
||
$this->RegisterPropertyString('DeviceID', '');
|
||
|
||
// Feedback Variablen
|
||
$this->RegisterVariableFloat('PowerProduction', 'Power Production', '', 10);
|
||
$this->RegisterVariableFloat('StateOfCharge', 'State of Charge', '', 11);
|
||
$this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 12);
|
||
$this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 13);
|
||
$this->RegisterVariableFloat('MinSOC', 'Min SOC', '', 14);
|
||
$this->RegisterVariableFloat('MaxSOC', 'Max SOC', '', 15);
|
||
|
||
// Remote Control
|
||
$this->RegisterVariableFloat('PowerSetpoint', 'Power Setpoint', '', 20);
|
||
$this->RegisterVariableString('Strategy', 'Strategy', '', 21);
|
||
|
||
// Debug
|
||
$this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request', '', 90);
|
||
$this->RegisterVariableString('RemoteControlPayload', 'Remote Control', '', 91);
|
||
}
|
||
|
||
public function ApplyChanges()
|
||
{
|
||
parent::ApplyChanges();
|
||
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
||
$this->Subscribed = false;
|
||
}
|
||
|
||
private function Log($t, $m)
|
||
{
|
||
IPS_LogMessage("VGT_Sub/$t", $m);
|
||
$this->SendDebug($t, $m, 0);
|
||
}
|
||
|
||
private function SafeSend(array $p)
|
||
{
|
||
$parent = @IPS_GetInstance($this->InstanceID)['ConnectionID'] ?? 0;
|
||
if ($parent === 0) return;
|
||
if (IPS_GetInstance($parent)['InstanceStatus'] !== 102) return;
|
||
@$this->SendDataToParent(json_encode($p));
|
||
}
|
||
|
||
private function EnsureSubscribe()
|
||
{
|
||
if ($this->Subscribed) return;
|
||
$this->Subscribed = true;
|
||
|
||
$this->SafeSend([
|
||
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}',
|
||
'PacketType' => 8,
|
||
'QualityOfService' => 0,
|
||
'Retain' => false,
|
||
'Topic' => '#',
|
||
'Payload' => ''
|
||
]);
|
||
|
||
$this->Log("Subscribe", "#");
|
||
}
|
||
|
||
private function Publish(string $topic, string $payload)
|
||
{
|
||
$this->SafeSend([
|
||
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}',
|
||
'PacketType' => 3,
|
||
'QualityOfService' => 0,
|
||
'Retain' => false,
|
||
'Topic' => $topic,
|
||
'Payload' => $payload
|
||
]);
|
||
|
||
$this->Log("Publish", "$topic → $payload");
|
||
}
|
||
|
||
|
||
public function ReceiveData($JSONString)
|
||
{
|
||
$this->EnsureSubscribe();
|
||
|
||
$data = json_decode($JSONString, true);
|
||
if (!is_array($data)) return;
|
||
|
||
$topic = $data['Topic'] ?? '';
|
||
$payload = $data['Payload'] ?? '';
|
||
|
||
if ($topic === '') return;
|
||
|
||
$this->Log("RX", "$topic → $payload");
|
||
|
||
$device = $this->ReadPropertyString('DeviceID');
|
||
|
||
/* ================== FEEDBACK ================== */
|
||
|
||
if ($topic === "feedback-request/$device") {
|
||
|
||
$this->SetValue('FeedbackRequestPayload', $payload);
|
||
|
||
$resp = [
|
||
"power_production" => GetValueFloat($this->GetIDForIdent("PowerProduction")),
|
||
"is_ready" => GetValueBoolean($this->GetIDForIdent("IsReady")),
|
||
"is_running" => GetValueBoolean($this->GetIDForIdent("IsRunning")),
|
||
"state_of_charge" => GetValueFloat($this->GetIDForIdent("StateOfCharge")),
|
||
"min_soc" => GetValueFloat($this->GetIDForIdent("MinSOC")),
|
||
"max_soc" => GetValueFloat($this->GetIDForIdent("MaxSOC"))
|
||
];
|
||
|
||
$this->Publish("feedback-response/$device", json_encode($resp));
|
||
return;
|
||
}
|
||
|
||
/* ================== REMOTE CONTROL ================== */
|
||
|
||
if ($topic === "remote-control-request/$device") {
|
||
|
||
$this->SetValue("RemoteControlPayload", $payload);
|
||
|
||
$json = json_decode($payload, true);
|
||
if (!is_array($json)) return;
|
||
|
||
if (isset($json["power_setpoint"]))
|
||
SetValueFloat($this->GetIDForIdent("PowerSetpoint"), floatval($json["power_setpoint"]));
|
||
|
||
if (isset($json["strategy"]))
|
||
SetValueString($this->GetIDForIdent("Strategy"), $json["strategy"]);
|
||
|
||
$this->Publish("remote-control-response/$device", json_encode($json));
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
|
||
?>
|