no message

This commit is contained in:
dh
2025-11-13 08:04:40 +01:00
parent c2161abb34
commit b223f944b8
3 changed files with 51 additions and 52 deletions
+1 -7
View File
@@ -1,11 +1,5 @@
{ {
"elements": [ "elements": [
{
"type": "SelectInstance",
"name": "MQTTServer",
"caption": "MQTT Server",
"filter": "moduleID:{043EA491-84DA-4DFD-B9D4-44B4E63F23D1}"
},
{ {
"type": "ValidationTextBox", "type": "ValidationTextBox",
"name": "DeviceID", "name": "DeviceID",
@@ -15,7 +9,7 @@
"actions": [ "actions": [
{ {
"type": "Label", "type": "Label",
"label": "Dieses Modul erzeugt automatisch MQTT Server Devices für die beiden Topics:" "label": "Dieses Modul registriert automatisch die MQTT Topics:"
}, },
{ {
"type": "Label", "type": "Label",
+1 -1
View File
@@ -5,7 +5,7 @@
"vendor": "", "vendor": "",
"aliases": ["INT VGT Integration"], "aliases": ["INT VGT Integration"],
"parentRequirements": [ "parentRequirements": [
"{7F8A48DD-F8AD-4F9C-8E6B-9E4D92DDB8F9}" "{7F8A48DD-F8AD-4F9C-8E6B-9E4D92DDB8F9}"
], ],
"childRequirements": [], "childRequirements": [],
"implemented": [], "implemented": [],
+49 -44
View File
@@ -8,18 +8,15 @@ class Int_VGT extends IPSModule
{ {
parent::Create(); parent::Create();
// Properties
$this->RegisterPropertyInteger("MQTTServer", 0);
$this->RegisterPropertyString("DeviceID", ""); $this->RegisterPropertyString("DeviceID", "");
// Create script handler only once // Create handler script
if (!@$this->GetIDForIdent("ReceiveScript")) { if (!@$this->GetIDForIdent("ReceiveScript")) {
$scriptID = IPS_CreateScript(0); $id = IPS_CreateScript(0);
IPS_SetName($scriptID, "INTVGT_ReceiveHandler"); IPS_SetName($id, "INTVGT_ReceiveHandler");
IPS_SetIdent($scriptID, "ReceiveScript"); IPS_SetIdent($id, "ReceiveScript");
IPS_SetParent($scriptID, $this->InstanceID); IPS_SetParent($id, $this->InstanceID);
IPS_SetScriptContent($id, $this->GenerateReceiveScript());
IPS_SetScriptContent($scriptID, $this->GenerateReceiveScript());
} }
} }
@@ -27,84 +24,92 @@ class Int_VGT extends IPSModule
{ {
parent::ApplyChanges(); parent::ApplyChanges();
$mqttServer = $this->ReadPropertyInteger("MQTTServer"); $gatewayID = $this->GetGatewayID();
$deviceId = $this->ReadPropertyString("DeviceID"); $deviceID = $this->ReadPropertyString("DeviceID");
if ($mqttServer == 0 || $deviceId == "") { if ($gatewayID == 0 || $deviceID == "") {
$this->SetStatus(104); // missing config $this->SetStatus(104);
return; return;
} }
// Create MQTT Server Devices // Create MQTT devices
$this->RegisterMQTTDevice("FeedbackRequest", "feedback-request/" . $deviceId); $this->RegisterMQTTDevice("FeedbackRequest",
$this->RegisterMQTTDevice("RemoteControlResponse", "remote-control-response/" . $deviceId); "feedback-request/" . $deviceID);
$this->RegisterMQTTDevice("RemoteControlResponse",
"remote-control-response/" . $deviceID);
$this->SetStatus(102); $this->SetStatus(102);
} }
private function RegisterMQTTDevice(string $ident, string $topic) private function RegisterMQTTDevice(string $ident, string $topic)
{ {
$instanceID = @$this->GetIDForIdent($ident); $guidMQTTDevice = "{043EA491-84DA-4DFD-B9D4-44B4E63F23D1}";
if ($instanceID === false) { $id = @$this->GetIDForIdent($ident);
$instanceID = IPS_CreateInstance("{058A5647-0C2F-4D7D-9F4B-50A28C7D83BA}"); // MQTT Server Device if ($id === false) {
IPS_SetParent($instanceID, $this->InstanceID); $id = IPS_CreateInstance($guidMQTTDevice);
IPS_SetIdent($instanceID, $ident); IPS_SetParent($id, $this->InstanceID);
IPS_SetName($instanceID, "MQTT Device: " . $ident); IPS_SetIdent($id, $ident);
IPS_SetName($id, "MQTT Device: " . $ident);
} }
IPS_SetProperty($instanceID, "Topic", $topic); IPS_SetProperty($id, "Active", true);
IPS_SetProperty($instanceID, "Server", $this->ReadPropertyInteger("MQTTServer")); IPS_SetProperty($id, "Topic", $topic);
IPS_SetProperty($instanceID, "Script", $this->GetIDForIdent("ReceiveScript")); IPS_SetProperty($id, "Script", $this->GetIDForIdent("ReceiveScript"));
IPS_ApplyChanges($instanceID); IPS_ApplyChanges($id);
return $instanceID; return $id;
}
private function GetGatewayID(): int
{
$instance = IPS_GetInstance($this->InstanceID);
return $instance['ConnectionID'];
} }
/** Script to be executed when MQTT message is received */
private function GenerateReceiveScript(): string private function GenerateReceiveScript(): string
{ {
return <<<SCR return <<<PHP
<?php <?php
\$parent = IPS_GetParent(\$_IPS['SELF']); \$parent = IPS_GetParent(\$_IPS['SELF']);
IPS_RequestAction(\$parent, "OnReceive", json_encode([ IPS_RequestAction(\$parent, "OnReceive", json_encode([
"Topic" => \$_IPS['Topic'], "Topic" => \$_IPS['Topic'],
"Value" => \$_IPS['VALUE'] "Value" => \$_IPS['VALUE']
])); ]));
SCR; PHP;
} }
/** Called by ReceiveScript */
public function RequestAction($ident, $value) public function RequestAction($ident, $value)
{ {
if ($ident == "OnReceive") { if ($ident === "OnReceive") {
$data = json_decode($value, true); $data = json_decode($value, true);
$this->OnReceiveMQTT($data["Topic"], $data["Value"]); $this->OnReceiveMQTT($data["Topic"], $data["Value"]);
} }
} }
/** THIS IS YOUR CALLBACK FOR INCOMING MQTT MESSAGES */
private function OnReceiveMQTT(string $topic, string $value) private function OnReceiveMQTT(string $topic, string $value)
{ {
IPS_LogMessage("Int_VGT", "Received on $topic: $value"); IPS_LogMessage("Int_VGT", "Received: $topic -> $value");
// Here you implement later your handling logic $gatewayID = $this->GetGatewayID();
// Example: respond to feedback-request
$mqttServer = $this->ReadPropertyInteger("MQTTServer");
// Auto responses (you can modify later)
if (str_contains($topic, "feedback-request")) { if (str_contains($topic, "feedback-request")) {
$response = json_encode([ MQTT_Publish($gatewayID,
"status" => "ok", str_replace("request", "response", $topic),
"timestamp" => time() json_encode(["status" => "ok"])
]); );
MQTT_Publish($mqttServer, str_replace("request", "response", $topic), $response, 0, false);
} }
if (str_contains($topic, "remote-control-response")) { if (str_contains($topic, "remote-control-response")) {
// Echo back the same payload MQTT_Publish($gatewayID,
MQTT_Publish($mqttServer, str_replace("response", "feedback", $topic), $value, 0, false); str_replace("response", "feedback", $topic),
$value
);
} }
} }
} }