This commit is contained in:
belevo\mh
2025-05-26 09:48:28 +02:00
parent c9b6584e86
commit 819f5f9587

View File

@@ -14,12 +14,10 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
$this->RegisterPropertyString("Topic", "");
// Nachricht-Payload-Parameter
$this->RegisterPropertyInteger("msg_id", 1);
$this->RegisterPropertyInteger("msg_id", 100);
$this->RegisterPropertyString("src", "user1");
$this->RegisterPropertyString("method", "Switch.Set");
$this->RegisterPropertyInteger("switch_bool", 0);
$this->RegisterPropertyInteger("switch_bool", 0); // Variable-ID einer Bool-Variable
}
public function ApplyChanges()
@@ -29,34 +27,43 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
public function RequestAction($Ident, $Value)
{
IPS_LogMessage("ShellySwitchSender", " RequestAction: $Ident = $Value");
IPS_LogMessage("ShellySwitchSender", "⚙️ RequestAction: $Ident = $Value");
switch ($Ident) {
case "GetAction":
$this->GetAction();
break;
default:
throw new Exception("Invalid action");
throw new Exception("Invalid action: $Ident");
}
}
private function GetAction()
{
IPS_LogMessage("ShellySwitchSender", " Starte GetAction()...");
// Eigenschaften lesen
$broker = GetValue($this->ReadPropertyString("broker_address"));
$port = GetValue($this->ReadPropertyInteger("broker_port"));
$user = GetValue($this->ReadPropertyString("username"));
$pass = GetValue($this->ReadPropertyString("password"));
$topic = GetValue($this->ReadPropertyString("Topic"));
$broker = $this->ReadPropertyString("broker_address");
$port = $this->ReadPropertyInteger("broker_port");
$user = $this->ReadPropertyString("username");
$pass = $this->ReadPropertyString("password");
$topic = $this->ReadPropertyString("Topic");
$msg_id = GetValue($this->ReadPropertyInteger("msg_id"));
$src = GetValue($this->ReadPropertyString("src"));
$method = GetValue($this->ReadPropertyString("method"));
$switch_bool = GetValueBoolean($this->ReadPropertyInteger("switch_bool"));
$msg_id = $this->ReadPropertyInteger("msg_id");
$src = $this->ReadPropertyString("src");
$method = $this->ReadPropertyString("method");
$boolVarID = $this->ReadPropertyInteger("switch_bool");
if (!IPS_VariableExists($boolVarID)) {
IPS_LogMessage("ShellySwitchSender", " Fehler: Bool-Variable mit ID $boolVarID existiert nicht.");
return;
}
$onValue = GetValueBoolean($boolVarID);
IPS_LogMessage("ShellySwitchSender", " MQTT-Ziel: $broker:$port");
IPS_LogMessage("ShellySwitchSender", " topic: $topic, src: $src, method: $method, msg_id: $msg_id");
IPS_LogMessage("ShellySwitchSender", " Bool-Wert: " . ($onValue ? "true" : "false") . " (aus Variable $boolVarID)");
IPS_LogMessage("ShellySwitchSender", "Broker: $broker:$port, Topic: $topic");
IPS_LogMessage("ShellySwitchSender", " src: $src, method: $method, msg_id: $msg_id");
IPS_LogMessage("ShellySwitchSender", " boolVarID: $switch_bool");
// JSON-Payload erstellen
$payload = [
"id" => 0,
@@ -64,30 +71,31 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
"method" => $method,
"params" => [
"id" => $msg_id,
"on" => $switch_bool
"on" => $onValue
]
];
$json = json_encode($payload);
IPS_LogMessage("ShellySwitchSender", " MQTT Payload: $json");
IPS_LogMessage("ShellySwitchSender", "📦 MQTT Payload: $json");
// Sende an MQTTClient
$clientID = @IPS_GetInstanceIDByName("MQTTClient", 0);
if ($clientID && IPS_InstanceExists($clientID)) {
$moduleID = IPS_GetInstance($clientID)['ModuleInfo']['ModuleID'];
IPS_LogMessage("ShellySwitchSender", "📡 Gefundener MQTTClient mit ModuleID: $moduleID");
IPS_LogMessage("ShellySwitchSender", "MQTTClient gefunden (ID: $clientID, ModuleID: $moduleID)");
if ($moduleID == "{C03937E3-5F86-EACA-B4A2-39A24ACF945A}") {
// Unterstützte Module
if (
$moduleID == "{C03937E3-5F86-EACA-B4A2-39A24ACF945A}" || // alter MQTTClient
$moduleID == "{C8792760-65CF-4C53-B5C7-A30FCC84FEFE}" // offizieller Symcon MQTTClient
) {
MQTTClient_Publish($clientID, $topic, $json, 0, false);
IPS_LogMessage("ShellySwitchSender", " Nachricht erfolgreich an '$topic' gesendet.");
IPS_LogMessage("ShellySwitchSender", " Nachricht an Topic '$topic' gesendet.");
} else {
IPS_LogMessage("ShellySwitchSender", " Fehler: Instanz 'MQTTClient' hat falsches Modul.");
IPS_LogMessage("ShellySwitchSender", " Fehler: 'MQTTClient' Instanz ist kein gültiges MQTT-Modul.");
}
} else {
IPS_LogMessage("ShellySwitchSender", " Kein MQTTClient gefunden oder benannt als 'MQTTClient'.");
IPS_LogMessage("ShellySwitchSender", " Fehler: MQTTClient nicht gefunden. Bitte sicherstellen, dass eine Instanz mit Namen 'MQTTClient' existiert.");
}
}
}
?>