no message
This commit is contained in:
@@ -5,19 +5,5 @@
|
|||||||
"name": "DeviceID",
|
"name": "DeviceID",
|
||||||
"caption": "Device ID"
|
"caption": "Device ID"
|
||||||
}
|
}
|
||||||
],
|
|
||||||
"actions": [
|
|
||||||
{
|
|
||||||
"type": "Label",
|
|
||||||
"label": "Dieses Modul registriert automatisch die MQTT Topics:"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "Label",
|
|
||||||
"label": "→ feedback-request/<DeviceID>"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "Label",
|
|
||||||
"label": "→ remote-control-response/<DeviceID>"
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,6 @@
|
|||||||
"{7F8A48DD-F8AD-4F9C-8E6B-9E4D92DDB8F9}"
|
"{7F8A48DD-F8AD-4F9C-8E6B-9E4D92DDB8F9}"
|
||||||
],
|
],
|
||||||
"childRequirements": [],
|
"childRequirements": [],
|
||||||
"implemented": [],
|
"implemented": ["MQTTSERVER"],
|
||||||
"prefix": "INTVGT"
|
"prefix": "INTVGT"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,108 +9,82 @@ class Int_VGT extends IPSModule
|
|||||||
parent::Create();
|
parent::Create();
|
||||||
|
|
||||||
$this->RegisterPropertyString("DeviceID", "");
|
$this->RegisterPropertyString("DeviceID", "");
|
||||||
|
|
||||||
// Create handler script
|
|
||||||
if (!@$this->GetIDForIdent("ReceiveScript")) {
|
|
||||||
$id = IPS_CreateScript(0);
|
|
||||||
IPS_SetName($id, "INTVGT_ReceiveHandler");
|
|
||||||
IPS_SetIdent($id, "ReceiveScript");
|
|
||||||
IPS_SetParent($id, $this->InstanceID);
|
|
||||||
IPS_SetScriptContent($id, $this->GenerateReceiveScript());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function ApplyChanges()
|
public function ApplyChanges()
|
||||||
{
|
{
|
||||||
parent::ApplyChanges();
|
parent::ApplyChanges();
|
||||||
|
|
||||||
$gatewayID = $this->GetGatewayID();
|
// MQTT-Server verbinden
|
||||||
$deviceID = $this->ReadPropertyString("DeviceID");
|
$this->ConnectParent("{7F8A48DD-F8AD-4F9C-8E6B-9E4D92DDB8F9}");
|
||||||
|
|
||||||
if ($gatewayID == 0 || $deviceID == "") {
|
// Status prüfen
|
||||||
|
if ($this->ReadPropertyString("DeviceID") == "") {
|
||||||
$this->SetStatus(104);
|
$this->SetStatus(104);
|
||||||
|
} else {
|
||||||
|
$this->SetStatus(102);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Wird vom MQTT Server aufgerufen */
|
||||||
|
public function ReceiveData($JSONString)
|
||||||
|
{
|
||||||
|
$data = json_decode($JSONString, true);
|
||||||
|
|
||||||
|
// Beispiel MQTT-Datenformat:
|
||||||
|
// [
|
||||||
|
// "Topic" => "...",
|
||||||
|
// "Payload" => "..."
|
||||||
|
// ]
|
||||||
|
|
||||||
|
if (!isset($data["Topic"]) || !isset($data["Payload"])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create MQTT devices
|
$topic = $data["Topic"];
|
||||||
$this->RegisterMQTTDevice("FeedbackRequest",
|
$value = $data["Payload"];
|
||||||
"feedback-request/" . $deviceID);
|
|
||||||
|
|
||||||
$this->RegisterMQTTDevice("RemoteControlResponse",
|
IPS_LogMessage("Int_VGT", "MQTT RX: $topic → $value");
|
||||||
"remote-control-response/" . $deviceID);
|
|
||||||
|
|
||||||
$this->SetStatus(102);
|
$this->HandleIncoming($topic, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function RegisterMQTTDevice(string $ident, string $topic)
|
/** Deine Logik */
|
||||||
|
private function HandleIncoming(string $topic, string $value)
|
||||||
{
|
{
|
||||||
$guidMQTTDevice = "{043EA491-84DA-4DFD-B9D4-44B4E63F23D1}";
|
$deviceID = $this->ReadPropertyString("DeviceID");
|
||||||
|
$gateway = $this->InstanceID;
|
||||||
|
|
||||||
$id = @$this->GetIDForIdent($ident);
|
// Feedback Request
|
||||||
if ($id === false) {
|
if ($topic === "feedback-request/$deviceID") {
|
||||||
$id = IPS_CreateInstance($guidMQTTDevice);
|
|
||||||
IPS_SetParent($id, $this->InstanceID);
|
$response = json_encode([
|
||||||
IPS_SetIdent($id, $ident);
|
"status" => "ok",
|
||||||
IPS_SetName($id, "MQTT Device: " . $ident);
|
"timestamp" => time()
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->Publish("feedback-response/$deviceID", $response);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
IPS_SetProperty($id, "Active", true);
|
// Remote Control
|
||||||
IPS_SetProperty($id, "Topic", $topic);
|
if ($topic === "remote-control-response/$deviceID") {
|
||||||
IPS_SetProperty($id, "Script", $this->GetIDForIdent("ReceiveScript"));
|
$this->Publish("remote-control-feedback/$deviceID", $value);
|
||||||
IPS_ApplyChanges($id);
|
return;
|
||||||
|
}
|
||||||
return $id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function GetGatewayID(): int
|
/** MQTT Publish Wrapper */
|
||||||
|
private function Publish(string $topic, string $payload)
|
||||||
{
|
{
|
||||||
$instance = IPS_GetInstance($this->InstanceID);
|
$msg = [
|
||||||
return $instance['ConnectionID'];
|
"Topic" => $topic,
|
||||||
}
|
"Payload" => $payload,
|
||||||
|
"Retain" => false,
|
||||||
|
"QoS" => 0
|
||||||
|
];
|
||||||
|
|
||||||
private function GenerateReceiveScript(): string
|
$this->SendDataToParent(json_encode(["DataID" => "{7F8A48DD-F8AD-4F9C-8E6B-9E4D92DDB8F9}", "Packet" => $msg]));
|
||||||
{
|
|
||||||
return <<<PHP
|
|
||||||
<?php
|
|
||||||
|
|
||||||
\$parent = IPS_GetParent(\$_IPS['SELF']);
|
|
||||||
|
|
||||||
IPS_RequestAction(\$parent, "OnReceive", json_encode([
|
|
||||||
"Topic" => \$_IPS['Topic'],
|
|
||||||
"Value" => \$_IPS['VALUE']
|
|
||||||
]));
|
|
||||||
|
|
||||||
PHP;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function RequestAction($ident, $value)
|
|
||||||
{
|
|
||||||
if ($ident === "OnReceive") {
|
|
||||||
$data = json_decode($value, true);
|
|
||||||
$this->OnReceiveMQTT($data["Topic"], $data["Value"]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private function OnReceiveMQTT(string $topic, string $value)
|
|
||||||
{
|
|
||||||
IPS_LogMessage("Int_VGT", "Received: $topic -> $value");
|
|
||||||
|
|
||||||
$gatewayID = $this->GetGatewayID();
|
|
||||||
|
|
||||||
// Auto responses (you can modify later)
|
|
||||||
if (str_contains($topic, "feedback-request")) {
|
|
||||||
MQTT_Publish($gatewayID,
|
|
||||||
str_replace("request", "response", $topic),
|
|
||||||
json_encode(["status" => "ok"])
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (str_contains($topic, "remote-control-response")) {
|
|
||||||
MQTT_Publish($gatewayID,
|
|
||||||
str_replace("response", "feedback", $topic),
|
|
||||||
$value
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user