.
This commit is contained in:
@@ -6,28 +6,40 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
|
|||||||
{
|
{
|
||||||
parent::Create();
|
parent::Create();
|
||||||
|
|
||||||
// MQTT-Verbindungsparameter
|
// MQTT-Verbindungsparameter als Properties
|
||||||
$this->RegisterPropertyString("broker_address", "");
|
$this->RegisterPropertyString("broker_address", "");
|
||||||
$this->RegisterPropertyInteger("broker_port", 1883);
|
$this->RegisterPropertyInteger("broker_port", 1883);
|
||||||
$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
|
||||||
|
|
||||||
// Nachricht-Payload-Parameter
|
// Nachricht-Payload-Parameter als Properties
|
||||||
$this->RegisterPropertyInteger("msg_id", 100);
|
$this->RegisterPropertyInteger("msg_id", 100);
|
||||||
$this->RegisterPropertyString("src", "user1");
|
$this->RegisterPropertyString("src", "user1");
|
||||||
$this->RegisterPropertyString("method", "Switch.Set");
|
$this->RegisterPropertyString("method", "Switch.Set");
|
||||||
$this->RegisterPropertyInteger("switch_bool", 0); // Variable-ID einer Bool-Variable
|
$this->RegisterPropertyInteger("switch_bool", 0); // ID der Bool-Variable
|
||||||
}
|
}
|
||||||
|
|
||||||
public function ApplyChanges()
|
public function ApplyChanges()
|
||||||
{
|
{
|
||||||
parent::ApplyChanges();
|
parent::ApplyChanges();
|
||||||
|
// Hier ggf. weitere Aktionen bei Änderungen, z.B. RegisterTimer o.Ä.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wird aufgerufen, wenn eine Aktion auf ein Ident (z.B. Variable) ausgeführt wird
|
||||||
|
*
|
||||||
|
* @param string $Ident
|
||||||
|
* @param mixed $Value
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
public function RequestAction($Ident, $Value)
|
public function RequestAction($Ident, $Value)
|
||||||
{
|
{
|
||||||
IPS_LogMessage("ShellySwitchSender", "⚙️ RequestAction: $Ident = $Value");
|
$msg = "RequestAction aufgerufen: Ident = $Ident, Value = " . var_export($Value, true);
|
||||||
|
echo $msg . "\n";
|
||||||
|
IPS_LogMessage("ShellySwitchSender", $msg);
|
||||||
|
|
||||||
switch ($Ident) {
|
switch ($Ident) {
|
||||||
case "GetAction":
|
case "GetAction":
|
||||||
$this->GetAction();
|
$this->GetAction();
|
||||||
@@ -37,34 +49,56 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function GetAction()
|
/**
|
||||||
|
* Führt die eigentliche Aktion aus: Liest Werte, baut Payload, sendet MQTT Nachricht
|
||||||
|
*/
|
||||||
|
public function GetAction()
|
||||||
{
|
{
|
||||||
IPS_LogMessage("ShellySwitchSender", " Starte GetAction()...");
|
$msg = "Starte GetAction()...";
|
||||||
|
echo $msg . "\n";
|
||||||
|
IPS_LogMessage("ShellySwitchSender", $msg);
|
||||||
|
|
||||||
// Eigenschaften lesen
|
// MQTT Broker-Daten aus Properties
|
||||||
$broker = $this->ReadPropertyString("broker_address");
|
$broker = $this->ReadPropertyString("broker_address");
|
||||||
$port = $this->ReadPropertyInteger("broker_port");
|
$port = $this->ReadPropertyInteger("broker_port");
|
||||||
$user = $this->ReadPropertyString("username");
|
$user = $this->ReadPropertyString("username");
|
||||||
$pass = $this->ReadPropertyString("password");
|
$pass = $this->ReadPropertyString("password");
|
||||||
$topic = $this->ReadPropertyString("Topic");
|
$topic = $this->ReadPropertyString("Topic");
|
||||||
|
$mqttInstanceID = $this->ReadPropertyInteger("mqtt_instance_id");
|
||||||
|
|
||||||
|
// Payload-Parameter aus Properties
|
||||||
$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");
|
||||||
$boolVarID = $this->ReadPropertyInteger("switch_bool");
|
$boolVarID = $this->ReadPropertyInteger("switch_bool");
|
||||||
|
|
||||||
|
// Prüfe ob Bool-Variable existiert
|
||||||
if (!IPS_VariableExists($boolVarID)) {
|
if (!IPS_VariableExists($boolVarID)) {
|
||||||
IPS_LogMessage("ShellySwitchSender", " Fehler: Bool-Variable mit ID $boolVarID existiert nicht.");
|
$err = "FEHLER: Bool-Variable mit ID $boolVarID existiert nicht.";
|
||||||
|
echo $err . "\n";
|
||||||
|
IPS_LogMessage("ShellySwitchSender", $err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bool-Wert aus Variable lesen
|
||||||
$onValue = GetValueBoolean($boolVarID);
|
$onValue = GetValueBoolean($boolVarID);
|
||||||
|
|
||||||
IPS_LogMessage("ShellySwitchSender", " MQTT-Ziel: $broker:$port");
|
// Log-Ausgaben der Konfiguration
|
||||||
IPS_LogMessage("ShellySwitchSender", " topic: $topic, src: $src, method: $method, msg_id: $msg_id");
|
echo "MQTT Broker: $broker:$port\n";
|
||||||
IPS_LogMessage("ShellySwitchSender", " Bool-Wert: " . ($onValue ? "true" : "false") . " (aus Variable $boolVarID)");
|
echo "Topic: $topic\n";
|
||||||
|
echo "Source: $src\n";
|
||||||
|
echo "Method: $method\n";
|
||||||
|
echo "Message ID: $msg_id\n";
|
||||||
|
echo "Bool-Variable ID: $boolVarID = " . ($onValue ? "true" : "false") . "\n";
|
||||||
|
|
||||||
// JSON-Payload erstellen
|
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"));
|
||||||
|
|
||||||
|
// JSON-Payload erzeugen
|
||||||
$payload = [
|
$payload = [
|
||||||
"id" => 0,
|
"id" => 0,
|
||||||
"src" => $src,
|
"src" => $src,
|
||||||
@@ -76,26 +110,33 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
|
|||||||
];
|
];
|
||||||
|
|
||||||
$json = json_encode($payload);
|
$json = json_encode($payload);
|
||||||
IPS_LogMessage("ShellySwitchSender", "📦 MQTT Payload: $json");
|
echo "MQTT Payload: $json\n";
|
||||||
|
IPS_LogMessage("ShellySwitchSender", "MQTT Payload: $json");
|
||||||
|
|
||||||
// Sende an MQTTClient
|
// MQTTClient-Instanz prüfen und Nachricht senden
|
||||||
$clientID = @IPS_GetInstanceIDByName("MQTTClient", 0);
|
if ($mqttInstanceID > 0 && IPS_InstanceExists($mqttInstanceID)) {
|
||||||
if ($clientID && IPS_InstanceExists($clientID)) {
|
$instanceInfo = IPS_GetInstance($mqttInstanceID);
|
||||||
$moduleID = IPS_GetInstance($clientID)['ModuleInfo']['ModuleID'];
|
$moduleID = $instanceInfo['ModuleInfo']['ModuleID'];
|
||||||
IPS_LogMessage("ShellySwitchSender", "MQTTClient gefunden (ID: $clientID, ModuleID: $moduleID)");
|
echo "MQTT Instanz gefunden: ID $mqttInstanceID, ModuleID $moduleID\n";
|
||||||
|
IPS_LogMessage("ShellySwitchSender", "MQTT Instanz gefunden: ID $mqttInstanceID, ModuleID $moduleID");
|
||||||
|
|
||||||
// Unterstützte Module
|
// Prüfe ob Instanz ein MQTTClient ist
|
||||||
if (
|
if (
|
||||||
$moduleID == "{C03937E3-5F86-EACA-B4A2-39A24ACF945A}" || // alter MQTTClient
|
$moduleID == "{C03937E3-5F86-EACA-B4A2-39A24ACF945A}" || // Alter MQTTClient
|
||||||
$moduleID == "{C8792760-65CF-4C53-B5C7-A30FCC84FEFE}" // offizieller Symcon MQTTClient
|
$moduleID == "{C8792760-65CF-4C53-B5C7-A30FCC84FEFE}" // Offizieller MQTTClient
|
||||||
) {
|
) {
|
||||||
MQTTClient_Publish($clientID, $topic, $json, 0, false);
|
MQTTClient_Publish($mqttInstanceID, $topic, $json, 0, false);
|
||||||
IPS_LogMessage("ShellySwitchSender", " Nachricht an Topic '$topic' gesendet.");
|
echo "Nachricht an Topic '$topic' gesendet.\n";
|
||||||
|
IPS_LogMessage("ShellySwitchSender", "Nachricht an Topic '$topic' gesendet.");
|
||||||
} else {
|
} else {
|
||||||
IPS_LogMessage("ShellySwitchSender", " Fehler: 'MQTTClient' Instanz ist kein gültiges MQTT-Modul.");
|
$err = "FEHLER: MQTT Instanz ist kein gültiges MQTT-Modul.";
|
||||||
|
echo $err . "\n";
|
||||||
|
IPS_LogMessage("ShellySwitchSender", $err);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
IPS_LogMessage("ShellySwitchSender", " Fehler: MQTTClient nicht gefunden. Bitte sicherstellen, dass eine Instanz mit Namen 'MQTTClient' existiert.");
|
$err = "FEHLER: MQTT Instanz nicht gefunden oder ungültig (ID: $mqttInstanceID).";
|
||||||
|
echo $err . "\n";
|
||||||
|
IPS_LogMessage("ShellySwitchSender", $err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user