Files
Symcon_Belevo_Energiemanage…/Symcon_Publish_to_Shelly_MQTT/module.php
belevo\mh f7ce5ffe5b .
2025-05-26 09:19:47 +02:00

84 lines
2.7 KiB
PHP

<?php
class Symcon_Publish_to_Shelly_MQTT extends IPSModule
{
public function Create()
{
parent::Create();
// MQTT-Verbindungsparameter
$this->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", false);
}
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->ReadPropertyBoolean("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.");
}
}
?>