80 lines
2.1 KiB
PHP
80 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
class Int_VGT extends IPSModule
|
|
{
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
$this->RegisterPropertyString("DeviceID", "");
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
// Verbindung zur ausgewählten Gateway-Instanz
|
|
$this->ConnectParent("{7F8A48DD-F8AD-4F9C-8E6B-9E4D92DDB8F9}");
|
|
$deviceID = $this->ReadPropertyString("DeviceID");
|
|
|
|
if ($deviceID === "") {
|
|
$this->SetStatus(104);
|
|
} else {
|
|
$this->SetStatus(102);
|
|
}
|
|
}
|
|
|
|
public function ReceiveData(string $JSONString)
|
|
{
|
|
$data = json_decode($JSONString, true);
|
|
if ($data === null) {
|
|
return;
|
|
}
|
|
if (!isset($data["Topic"]) || !isset($data["Payload"])) {
|
|
return;
|
|
}
|
|
|
|
$topic = $data["Topic"];
|
|
$payload = $data["Payload"];
|
|
|
|
IPS_LogMessage("Int_VGT", "Received topic: {$topic}, payload: {$payload}");
|
|
$this->HandleIncoming($topic, $payload);
|
|
}
|
|
|
|
private function HandleIncoming(string $topic, string $payload)
|
|
{
|
|
$deviceID = $this->ReadPropertyString("DeviceID");
|
|
|
|
if ($topic === "feedback-request/{$deviceID}") {
|
|
$response = json_encode([
|
|
"status" => "ok",
|
|
"timestamp" => time()
|
|
]);
|
|
$this->Publish("feedback-response/{$deviceID}", $response);
|
|
return;
|
|
}
|
|
|
|
if ($topic === "remote-control-response/{$deviceID}") {
|
|
// Echo same payload
|
|
$this->Publish("remote-control-feedback/{$deviceID}", $payload);
|
|
return;
|
|
}
|
|
}
|
|
|
|
private function Publish(string $topic, string $payload)
|
|
{
|
|
$message = [
|
|
"Topic" => $topic,
|
|
"Payload" => $payload,
|
|
"Retain" => false,
|
|
"QoS" => 0
|
|
];
|
|
$data = [
|
|
"DataID" => "{7F8A48DD-F8AD-4F9C-8E6B-9E4D92DDB8F9}",
|
|
"Packet" => $message
|
|
];
|
|
$this->SendDataToParent(json_encode($data));
|
|
}
|
|
}
|
|
?>
|