This commit is contained in:
mh
2025-05-26 13:54:06 +02:00
parent 8254553775
commit 84a438c1f5
2 changed files with 23 additions and 23 deletions
+5
View File
@@ -30,6 +30,11 @@
"name": "client_id", "name": "client_id",
"caption": "Klient Id" "caption": "Klient Id"
}, },
{
"type": "ValidationTextBox",
"name": "mqtt_instance_id",
"caption": "MQTT Id"
},
{ {
"type": "NumberSpinner", "type": "NumberSpinner",
"name": "msg_id", "name": "msg_id",
+18 -23
View File
@@ -13,13 +13,14 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
$this->RegisterPropertyString("password", ""); $this->RegisterPropertyString("password", "");
$this->RegisterPropertyString("Topic", ""); $this->RegisterPropertyString("Topic", "");
$this->RegisterPropertyString("client_id", ""); $this->RegisterPropertyString("client_id", "");
$this->RegisterPropertyString("mqtt_instance_id", "");
// Nachricht-Payload-Parameter als Properties // Nachricht-Payload-Parameter als Properties
$this->RegisterPropertyInteger("msg_id", 100); $this->RegisterPropertyInteger("msg_id", 100);
$this->RegisterPropertyString("src", "user1"); $this->RegisterPropertyString("src", "user1");
$this->RegisterPropertyString("method", "Switch.Set"); $this->RegisterPropertyString("method", "Switch.Set");
$this->RegisterPropertyInteger("switch_bool", 0); // ID der Bool-Variable $this->RegisterPropertyInteger("switch_bool", 0); // ID der Bool-Variable
$this->RegisterTimer("Timer_Influx",5000,"IPS_RequestAction(" . $this->InstanceID . ', "GetAction", "");'); $this->RegisterTimer("Timer_Influx",5000,"IPS_RequestAction(" . $this->InstanceID . ', "GetAction", "");');
} }
@@ -45,13 +46,9 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
{ {
IPS_LogMessage("ShellySwitchSender", "GetAction gestartet"); IPS_LogMessage("ShellySwitchSender", "GetAction gestartet");
$broker = $this->ReadPropertyString("broker_address"); $mqttInstanceID = $this->ReadPropertyInteger("mqtt_instance_id"); // Instanz-ID deiner MQTT-Instanz
$port = $this->ReadPropertyInteger("broker_port");
$user = $this->ReadPropertyString("username");
$pass = $this->ReadPropertyString("password");
$topic = $this->ReadPropertyString("Topic"); $topic = $this->ReadPropertyString("Topic");
$mqttInstanceID = $this->ReadPropertyInteger("mqtt_instance_id");
$client_id = $this->ReadPropertyString("client_id");
$msg_id = $this->ReadPropertyInteger("msg_id"); $msg_id = $this->ReadPropertyInteger("msg_id");
$src = $this->ReadPropertyString("src"); $src = $this->ReadPropertyString("src");
$method = $this->ReadPropertyString("method"); $method = $this->ReadPropertyString("method");
@@ -64,13 +61,6 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
$onValue = GetValueBoolean($boolVarID); $onValue = GetValueBoolean($boolVarID);
IPS_LogMessage("ShellySwitchSender", "MQTT Broker: $broker:$port");
IPS_LogMessage("ShellySwitchSender", "Topic: $topic");
IPS_LogMessage("ShellySwitchSender", "Source: $src");
IPS_LogMessage("ShellySwitchSender", "Method: $method");
IPS_LogMessage("ShellySwitchSender", "Message ID: $msg_id");
IPS_LogMessage("ShellySwitchSender", "Bool-Variable ID: $boolVarID = " . ($onValue ? "true" : "false"));
$payload = [ $payload = [
"id" => 0, "id" => 0,
"src" => $src, "src" => $src,
@@ -81,18 +71,23 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
] ]
]; ];
$json = json_encode($payload); $jsonPayload = json_encode($payload);
IPS_LogMessage("ShellySwitchSender", "MQTT Payload: $json");
$mqtt = new phpMQTT($broker, $port, $client_id); IPS_LogMessage("ShellySwitchSender", "MQTT Payload: $jsonPayload");
if ($mqtt->connect(true, NULL, $username, $password)) { if (!IPS_InstanceExists($mqttInstanceID)) {
IPS_LogMessage("ShellySwitchSender", "Verbunden mit dem MQTT-Broker"); IPS_LogMessage("ShellySwitchSender", "FEHLER: MQTT-Instanz-ID $mqttInstanceID existiert nicht.");
$mqtt->publish($topic, $payload, 0); return;
IPS_LogMessage("ShellySwitchSender", "Nachricht gesendet"); }
$mqtt->close();
// Nachricht senden
$result = MQTT_Publish($mqttInstanceID, $topic, $jsonPayload, 0);
if ($result) {
IPS_LogMessage("ShellySwitchSender", "Nachricht erfolgreich gesendet");
} else { } else {
IPS_LogMessage("ShellySwitchSender", "Verbindung fehlgeschlagen"); IPS_LogMessage("ShellySwitchSender", "Fehler beim Senden der Nachricht");
} }
} }
} }