.
This commit is contained in:
@@ -19,46 +19,42 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
|
||||
$this->RegisterPropertyString("src", "user1");
|
||||
$this->RegisterPropertyString("method", "Switch.Set");
|
||||
$this->RegisterPropertyInteger("switch_bool", 0); // ID der Bool-Variable
|
||||
|
||||
// Beispiel-Variable zum Triggern der Aktion, damit RequestAction funktioniert
|
||||
$this->RegisterVariableBoolean("Trigger", "Trigger MQTT", "~Switch");
|
||||
$this->EnableAction("Trigger");
|
||||
}
|
||||
|
||||
public function ApplyChanges()
|
||||
{
|
||||
parent::ApplyChanges();
|
||||
// Hier ggf. weitere Aktionen bei Änderungen, z.B. RegisterTimer o.Ä.
|
||||
// Hier ggf. weitere Aktionen bei Änderungen
|
||||
}
|
||||
|
||||
/**
|
||||
* Wird aufgerufen, wenn eine Aktion auf ein Ident (z.B. Variable) ausgeführt wird
|
||||
*
|
||||
* @param string $Ident
|
||||
* @param mixed $Value
|
||||
* @throws Exception
|
||||
* RequestAction wird aufgerufen, wenn die Variable "Trigger" im WebFront oder Script geändert wird
|
||||
*/
|
||||
public function RequestAction($Ident, $Value)
|
||||
{
|
||||
$msg = "RequestAction aufgerufen: Ident = $Ident, Value = " . var_export($Value, true);
|
||||
echo $msg . "\n";
|
||||
IPS_LogMessage("ShellySwitchSender", $msg);
|
||||
IPS_LogMessage("ShellySwitchSender", "RequestAction aufgerufen: Ident = $Ident, Value = " . var_export($Value, true));
|
||||
|
||||
switch ($Ident) {
|
||||
case "GetAction":
|
||||
case "Trigger":
|
||||
SetValue($this->GetIDForIdent("Trigger"), $Value);
|
||||
$this->GetAction();
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Invalid action: $Ident");
|
||||
throw new Exception("Ungültige Aktion: $Ident");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Führt die eigentliche Aktion aus: Liest Werte, baut Payload, sendet MQTT Nachricht
|
||||
* Hauptfunktion: Liest Konfiguration, baut Payload, sendet MQTT Nachricht und loggt alles
|
||||
*/
|
||||
public function GetAction()
|
||||
{
|
||||
$msg = "Starte GetAction()...";
|
||||
echo $msg . "\n";
|
||||
IPS_LogMessage("ShellySwitchSender", $msg);
|
||||
IPS_LogMessage("ShellySwitchSender", "GetAction gestartet");
|
||||
|
||||
// MQTT Broker-Daten aus Properties
|
||||
$broker = $this->ReadPropertyString("broker_address");
|
||||
$port = $this->ReadPropertyInteger("broker_port");
|
||||
$user = $this->ReadPropertyString("username");
|
||||
@@ -66,31 +62,18 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
|
||||
$topic = $this->ReadPropertyString("Topic");
|
||||
$mqttInstanceID = $this->ReadPropertyInteger("mqtt_instance_id");
|
||||
|
||||
// Payload-Parameter aus Properties
|
||||
$msg_id = $this->ReadPropertyInteger("msg_id");
|
||||
$src = $this->ReadPropertyString("src");
|
||||
$method = $this->ReadPropertyString("method");
|
||||
$boolVarID = $this->ReadPropertyInteger("switch_bool");
|
||||
|
||||
// Prüfe ob Bool-Variable existiert
|
||||
if (!IPS_VariableExists($boolVarID)) {
|
||||
$err = "FEHLER: Bool-Variable mit ID $boolVarID existiert nicht.";
|
||||
echo $err . "\n";
|
||||
IPS_LogMessage("ShellySwitchSender", $err);
|
||||
IPS_LogMessage("ShellySwitchSender", "FEHLER: Bool-Variable mit ID $boolVarID existiert nicht.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Bool-Wert aus Variable lesen
|
||||
$onValue = GetValueBoolean($boolVarID);
|
||||
|
||||
// Log-Ausgaben der Konfiguration
|
||||
echo "MQTT Broker: $broker:$port\n";
|
||||
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";
|
||||
|
||||
IPS_LogMessage("ShellySwitchSender", "MQTT Broker: $broker:$port");
|
||||
IPS_LogMessage("ShellySwitchSender", "Topic: $topic");
|
||||
IPS_LogMessage("ShellySwitchSender", "Source: $src");
|
||||
@@ -98,7 +81,6 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
|
||||
IPS_LogMessage("ShellySwitchSender", "Message ID: $msg_id");
|
||||
IPS_LogMessage("ShellySwitchSender", "Bool-Variable ID: $boolVarID = " . ($onValue ? "true" : "false"));
|
||||
|
||||
// JSON-Payload erzeugen
|
||||
$payload = [
|
||||
"id" => 0,
|
||||
"src" => $src,
|
||||
@@ -110,33 +92,24 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
|
||||
];
|
||||
|
||||
$json = json_encode($payload);
|
||||
echo "MQTT Payload: $json\n";
|
||||
IPS_LogMessage("ShellySwitchSender", "MQTT Payload: $json");
|
||||
|
||||
// MQTTClient-Instanz prüfen und Nachricht senden
|
||||
if ($mqttInstanceID > 0 && IPS_InstanceExists($mqttInstanceID)) {
|
||||
$instanceInfo = IPS_GetInstance($mqttInstanceID);
|
||||
$moduleID = $instanceInfo['ModuleInfo']['ModuleID'];
|
||||
echo "MQTT Instanz gefunden: ID $mqttInstanceID, ModuleID $moduleID\n";
|
||||
IPS_LogMessage("ShellySwitchSender", "MQTT Instanz gefunden: ID $mqttInstanceID, ModuleID $moduleID");
|
||||
|
||||
// Prüfe ob Instanz ein MQTTClient ist
|
||||
if (
|
||||
$moduleID == "{C03937E3-5F86-EACA-B4A2-39A24ACF945A}" || // Alter MQTTClient
|
||||
$moduleID == "{C8792760-65CF-4C53-B5C7-A30FCC84FEFE}" // Offizieller MQTTClient
|
||||
$moduleID == "{C03937E3-5F86-EACA-B4A2-39A24ACF945A}" || // Alter MQTTClient
|
||||
$moduleID == "{C8792760-65CF-4C53-B5C7-A30FCC84FEFE}" // Offizieller MQTTClient
|
||||
) {
|
||||
MQTTClient_Publish($mqttInstanceID, $topic, $json, 0, false);
|
||||
echo "Nachricht an Topic '$topic' gesendet.\n";
|
||||
IPS_LogMessage("ShellySwitchSender", "Nachricht an Topic '$topic' gesendet.");
|
||||
} else {
|
||||
$err = "FEHLER: MQTT Instanz ist kein gültiges MQTT-Modul.";
|
||||
echo $err . "\n";
|
||||
IPS_LogMessage("ShellySwitchSender", $err);
|
||||
IPS_LogMessage("ShellySwitchSender", "FEHLER: MQTT Instanz ist kein gültiges MQTT-Modul.");
|
||||
}
|
||||
} else {
|
||||
$err = "FEHLER: MQTT Instanz nicht gefunden oder ungültig (ID: $mqttInstanceID).";
|
||||
echo $err . "\n";
|
||||
IPS_LogMessage("ShellySwitchSender", $err);
|
||||
IPS_LogMessage("ShellySwitchSender", "FEHLER: MQTT Instanz nicht gefunden oder ungültig (ID: $mqttInstanceID).");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user