This commit is contained in:
belevo\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",
"caption": "Klient Id"
},
{
"type": "ValidationTextBox",
"name": "mqtt_instance_id",
"caption": "MQTT Id"
},
{
"type": "NumberSpinner",
"name": "msg_id",
+18 -23
View File
@@ -13,12 +13,13 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
$this->RegisterPropertyString("password", "");
$this->RegisterPropertyString("Topic", "");
$this->RegisterPropertyString("client_id", "");
$this->RegisterPropertyString("mqtt_instance_id", "");
// Nachricht-Payload-Parameter als Properties
$this->RegisterPropertyInteger("msg_id", 100);
$this->RegisterPropertyString("src", "user1");
$this->RegisterPropertyString("method", "Switch.Set");
$this->RegisterPropertyInteger("switch_bool", 0); // ID der Bool-Variable
$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");
$broker = $this->ReadPropertyString("broker_address");
$port = $this->ReadPropertyInteger("broker_port");
$user = $this->ReadPropertyString("username");
$pass = $this->ReadPropertyString("password");
$mqttInstanceID = $this->ReadPropertyInteger("mqtt_instance_id"); // Instanz-ID deiner MQTT-Instanz
$topic = $this->ReadPropertyString("Topic");
$mqttInstanceID = $this->ReadPropertyInteger("mqtt_instance_id");
$client_id = $this->ReadPropertyString("client_id");
$msg_id = $this->ReadPropertyInteger("msg_id");
$src = $this->ReadPropertyString("src");
$method = $this->ReadPropertyString("method");
@@ -64,13 +61,6 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
$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 = [
"id" => 0,
"src" => $src,
@@ -81,18 +71,23 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
]
];
$json = json_encode($payload);
IPS_LogMessage("ShellySwitchSender", "MQTT Payload: $json");
$jsonPayload = json_encode($payload);
$mqtt = new phpMQTT($broker, $port, $client_id);
IPS_LogMessage("ShellySwitchSender", "MQTT Payload: $jsonPayload");
if ($mqtt->connect(true, NULL, $username, $password)) {
IPS_LogMessage("ShellySwitchSender", "Verbunden mit dem MQTT-Broker");
$mqtt->publish($topic, $payload, 0);
IPS_LogMessage("ShellySwitchSender", "Nachricht gesendet");
$mqtt->close();
if (!IPS_InstanceExists($mqttInstanceID)) {
IPS_LogMessage("ShellySwitchSender", "FEHLER: MQTT-Instanz-ID $mqttInstanceID existiert nicht.");
return;
}
// Nachricht senden
$result = MQTT_Publish($mqttInstanceID, $topic, $jsonPayload, 0);
if ($result) {
IPS_LogMessage("ShellySwitchSender", "Nachricht erfolgreich gesendet");
} else {
IPS_LogMessage("ShellySwitchSender", "Verbindung fehlgeschlagen");
IPS_LogMessage("ShellySwitchSender", "Fehler beim Senden der Nachricht");
}
}
}