diff --git a/Symcon_Publish_to_Shelly_MQTT/README.md b/Symcon_Publish_to_Shelly_MQTT/README.md new file mode 100644 index 0000000..e69de29 diff --git a/Symcon_Publish_to_Shelly_MQTT/form.json b/Symcon_Publish_to_Shelly_MQTT/form.json new file mode 100644 index 0000000..6fc805e --- /dev/null +++ b/Symcon_Publish_to_Shelly_MQTT/form.json @@ -0,0 +1,50 @@ +{ + "elements": [ + { + "type": "ValidationTextBox", + "name": "broker_address", + "caption": "Broker-Adresse" + }, + { + "type": "NumberSpinner", + "name": "broker_port", + "caption": "Broker-Port" + }, + { + "type": "ValidationTextBox", + "name": "username", + "caption": "Benutzername" + }, + { + "type": "PasswordTextBox", + "name": "password", + "caption": "Passwort" + }, + { + "type": "ValidationTextBox", + "name": "Topic", + "caption": "MQTT Topic" + }, + { + "type": "NumberSpinner", + "name": "msg_id", + "caption": "Message ID" + }, + { + "type": "ValidationTextBox", + "name": "src", + "caption": "Source" + }, + { + "type": "ValidationTextBox", + "name": "method", + "caption": "Method" + }, + { + "type": "SelectVariable", + "name": "switch_bool", + "caption": "Variable mit dem zu regelnden Shelly Kontakt" + } + + ] +} diff --git a/Symcon_Publish_to_Shelly_MQTT/module.json b/Symcon_Publish_to_Shelly_MQTT/module.json new file mode 100644 index 0000000..069997e --- /dev/null +++ b/Symcon_Publish_to_Shelly_MQTT/module.json @@ -0,0 +1,12 @@ +{ + "id": "{C03937E3-5F86-EACA-B4A2-39A24ACF945A}", + "name": "Symcon_Publish_to_Shelly_MQTT", + "type": 3, + "vendor": "Belevo AG", + "aliases": [], + "parentRequirements": [], + "childRequirements": [], + "implemented": [], + "prefix": "GEF", + "url": "" +} \ No newline at end of file diff --git a/Symcon_Publish_to_Shelly_MQTT/module.php b/Symcon_Publish_to_Shelly_MQTT/module.php new file mode 100644 index 0000000..bded71f --- /dev/null +++ b/Symcon_Publish_to_Shelly_MQTT/module.php @@ -0,0 +1,84 @@ +RegisterPropertyString("broker_address", ""); + $this->RegisterPropertyInteger("broker_port", 1883); + $this->RegisterPropertyString("username", ""); + $this->RegisterPropertyString("password", ""); + $this->RegisterPropertyString("Topic", ""); + + // Nachricht-Payload-Parameter + $this->RegisterPropertyInteger("msg_id", 1); + $this->RegisterPropertyString("src", "user1"); + $this->RegisterPropertyString("method", "Switch.Set"); + $this->RegisterPropertyBoolean("switch_bool", ""); + + + } + + public function ApplyChanges() + { + parent::ApplyChanges(); + } + + public function RequestAction($Ident, $Value) + { + switch ($Ident) { + case "GetAction": + $this->GetAction(); + break; + default: + throw new Exception("Invalid action"); + } + } + + private function GetAction() + { + // Eigenschaften lesen + $broker = $this->ReadPropertyString("broker_address"); + $port = $this->ReadPropertyInteger("broker_port"); + $user = $this->ReadPropertyString("username"); + $pass = $this->ReadPropertyString("password"); + $topic = $this->ReadPropertyString("Topic"); + + $msg_id = $this->ReadPropertyInteger("msg_id"); + $src = $this->ReadPropertyString("src"); + $method = $this->ReadPropertyString("method"); + $switch_bool = $this->ReadPropertyString("switch_bool"); + // JSON-Payload erstellen + $payload = [ + "id" => 0, + "src" => $src, + "method" => $method, + "params" => [ + "id" => $msg_id, + "on" => $switch_bool + ] + ]; + + $json = json_encode($payload); + + // Sende an MQTTClient (Symcon MQTTClient muss existieren!) + $clientID = @IPS_GetInstanceIDByName("MQTTClient", 0); // Name anpassen, falls nötig + if ($clientID && IPS_InstanceExists($clientID)) { + // Prüfe ob kompatibler MQTTClient + if (IPS_GetInstance($clientID)['ModuleInfo']['ModuleID'] == "{C03937E3-5F86-EACA-B4A2-39A24ACF945A}") { + // Senden mit interner Funktion + MQTTClient_Publish($clientID, $topic, $json, 0, false); + IPS_LogMessage("ShellySwitchSender", "📤 Gesendet an '$topic': $json"); + return; + } + } + + // Fallback: Logging für manuelle MQTT-Sendung + IPS_LogMessage("ShellySwitchSender", "⚠️ Kein gültiger MQTTClient gefunden. Bitte in der Instanz 'MQTTClient' benennen."); + } +} + +?> \ No newline at end of file