This commit is contained in:
mh
2025-05-26 11:30:37 +02:00
parent aeb868ab11
commit 9126941991
2 changed files with 14 additions and 16 deletions
+5
View File
@@ -24,6 +24,11 @@
"type": "ValidationTextBox", "type": "ValidationTextBox",
"name": "Topic", "name": "Topic",
"caption": "MQTT Topic" "caption": "MQTT Topic"
},
{
"type": "ValidationTextBox",
"name": "client_id",
"caption": "Klient Id"
}, },
{ {
"type": "NumberSpinner", "type": "NumberSpinner",
+9 -16
View File
@@ -12,7 +12,7 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
$this->RegisterPropertyString("username", ""); $this->RegisterPropertyString("username", "");
$this->RegisterPropertyString("password", ""); $this->RegisterPropertyString("password", "");
$this->RegisterPropertyString("Topic", ""); $this->RegisterPropertyString("Topic", "");
$this->RegisterPropertyInteger("mqtt_instance_id", 0); // ID der MQTT-Instanz als Konfig-Feld $this->RegisterPropertyString("client_id", "");
// Nachricht-Payload-Parameter als Properties // Nachricht-Payload-Parameter als Properties
$this->RegisterPropertyInteger("msg_id", 100); $this->RegisterPropertyInteger("msg_id", 100);
@@ -56,7 +56,7 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
$pass = $this->ReadPropertyString("password"); $pass = $this->ReadPropertyString("password");
$topic = $this->ReadPropertyString("Topic"); $topic = $this->ReadPropertyString("Topic");
$mqttInstanceID = $this->ReadPropertyInteger("mqtt_instance_id"); $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");
@@ -89,22 +89,15 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
$json = json_encode($payload); $json = json_encode($payload);
IPS_LogMessage("ShellySwitchSender", "MQTT Payload: $json"); IPS_LogMessage("ShellySwitchSender", "MQTT Payload: $json");
if ($mqttInstanceID > 0 && IPS_InstanceExists($mqttInstanceID)) { $mqtt = new phpMQTT($broker, $port, $client_id);
$instanceInfo = IPS_GetInstance($mqttInstanceID);
$moduleID = $instanceInfo['ModuleInfo']['ModuleID'];
IPS_LogMessage("ShellySwitchSender", "MQTT Instanz gefunden: ID $mqttInstanceID, ModuleID $moduleID");
if ( if ($mqtt->connect(true, NULL, $username, $password)) {
$moduleID == "{C03937E3-5F86-EACA-B4A2-39A24ACF945A}" || // Alter MQTTClient IPS_LogMessage("ShellySwitchSender", "Verbunden mit dem MQTT-Broker");
$moduleID == "{C8792760-65CF-4C53-B5C7-A30FCC84FEFE}" // Offizieller MQTTClient $mqtt->publish($topic, $payload, 0);
) { IPS_LogMessage("ShellySwitchSender", "Nachricht gesende");
MQTTClient_Publish($mqttInstanceID, $topic, $json, 0, false); $mqtt->close();
IPS_LogMessage("ShellySwitchSender", "Nachricht an Topic '$topic' gesendet.");
} else {
IPS_LogMessage("ShellySwitchSender", "FEHLER: MQTT Instanz ist kein gültiges MQTT-Modul.");
}
} else { } else {
IPS_LogMessage("ShellySwitchSender", "FEHLER: MQTT Instanz nicht gefunden oder ungültig (ID: $mqttInstanceID)."); IPS_LogMessage("ShellySwitchSender", "Verbindung fehlgeschlagen");
} }
} }
} }