.
This commit is contained in:
@@ -19,46 +19,42 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
|
|||||||
$this->RegisterPropertyString("src", "user1");
|
$this->RegisterPropertyString("src", "user1");
|
||||||
$this->RegisterPropertyString("method", "Switch.Set");
|
$this->RegisterPropertyString("method", "Switch.Set");
|
||||||
$this->RegisterPropertyInteger("switch_bool", 0); // ID der Bool-Variable
|
$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()
|
public function ApplyChanges()
|
||||||
{
|
{
|
||||||
parent::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
|
* RequestAction wird aufgerufen, wenn die Variable "Trigger" im WebFront oder Script geändert wird
|
||||||
*
|
|
||||||
* @param string $Ident
|
|
||||||
* @param mixed $Value
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
public function RequestAction($Ident, $Value)
|
public function RequestAction($Ident, $Value)
|
||||||
{
|
{
|
||||||
$msg = "RequestAction aufgerufen: Ident = $Ident, Value = " . var_export($Value, true);
|
IPS_LogMessage("ShellySwitchSender", "RequestAction aufgerufen: Ident = $Ident, Value = " . var_export($Value, true));
|
||||||
echo $msg . "\n";
|
|
||||||
IPS_LogMessage("ShellySwitchSender", $msg);
|
|
||||||
|
|
||||||
switch ($Ident) {
|
switch ($Ident) {
|
||||||
case "GetAction":
|
case "Trigger":
|
||||||
|
SetValue($this->GetIDForIdent("Trigger"), $Value);
|
||||||
$this->GetAction();
|
$this->GetAction();
|
||||||
break;
|
break;
|
||||||
default:
|
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()
|
public function GetAction()
|
||||||
{
|
{
|
||||||
$msg = "Starte GetAction()...";
|
IPS_LogMessage("ShellySwitchSender", "GetAction gestartet");
|
||||||
echo $msg . "\n";
|
|
||||||
IPS_LogMessage("ShellySwitchSender", $msg);
|
|
||||||
|
|
||||||
// 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");
|
||||||
@@ -66,31 +62,18 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
|
|||||||
$topic = $this->ReadPropertyString("Topic");
|
$topic = $this->ReadPropertyString("Topic");
|
||||||
$mqttInstanceID = $this->ReadPropertyInteger("mqtt_instance_id");
|
$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)) {
|
||||||
$err = "FEHLER: Bool-Variable mit ID $boolVarID existiert nicht.";
|
IPS_LogMessage("ShellySwitchSender", "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);
|
||||||
|
|
||||||
// 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", "MQTT Broker: $broker:$port");
|
||||||
IPS_LogMessage("ShellySwitchSender", "Topic: $topic");
|
IPS_LogMessage("ShellySwitchSender", "Topic: $topic");
|
||||||
IPS_LogMessage("ShellySwitchSender", "Source: $src");
|
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", "Message ID: $msg_id");
|
||||||
IPS_LogMessage("ShellySwitchSender", "Bool-Variable ID: $boolVarID = " . ($onValue ? "true" : "false"));
|
IPS_LogMessage("ShellySwitchSender", "Bool-Variable ID: $boolVarID = " . ($onValue ? "true" : "false"));
|
||||||
|
|
||||||
// JSON-Payload erzeugen
|
|
||||||
$payload = [
|
$payload = [
|
||||||
"id" => 0,
|
"id" => 0,
|
||||||
"src" => $src,
|
"src" => $src,
|
||||||
@@ -110,33 +92,24 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
|
|||||||
];
|
];
|
||||||
|
|
||||||
$json = json_encode($payload);
|
$json = json_encode($payload);
|
||||||
echo "MQTT Payload: $json\n";
|
|
||||||
IPS_LogMessage("ShellySwitchSender", "MQTT Payload: $json");
|
IPS_LogMessage("ShellySwitchSender", "MQTT Payload: $json");
|
||||||
|
|
||||||
// MQTTClient-Instanz prüfen und Nachricht senden
|
|
||||||
if ($mqttInstanceID > 0 && IPS_InstanceExists($mqttInstanceID)) {
|
if ($mqttInstanceID > 0 && IPS_InstanceExists($mqttInstanceID)) {
|
||||||
$instanceInfo = IPS_GetInstance($mqttInstanceID);
|
$instanceInfo = IPS_GetInstance($mqttInstanceID);
|
||||||
$moduleID = $instanceInfo['ModuleInfo']['ModuleID'];
|
$moduleID = $instanceInfo['ModuleInfo']['ModuleID'];
|
||||||
echo "MQTT Instanz gefunden: ID $mqttInstanceID, ModuleID $moduleID\n";
|
|
||||||
IPS_LogMessage("ShellySwitchSender", "MQTT Instanz gefunden: ID $mqttInstanceID, ModuleID $moduleID");
|
IPS_LogMessage("ShellySwitchSender", "MQTT Instanz gefunden: ID $mqttInstanceID, ModuleID $moduleID");
|
||||||
|
|
||||||
// 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 MQTTClient
|
$moduleID == "{C8792760-65CF-4C53-B5C7-A30FCC84FEFE}" // Offizieller MQTTClient
|
||||||
) {
|
) {
|
||||||
MQTTClient_Publish($mqttInstanceID, $topic, $json, 0, false);
|
MQTTClient_Publish($mqttInstanceID, $topic, $json, 0, false);
|
||||||
echo "Nachricht an Topic '$topic' gesendet.\n";
|
|
||||||
IPS_LogMessage("ShellySwitchSender", "Nachricht an Topic '$topic' gesendet.");
|
IPS_LogMessage("ShellySwitchSender", "Nachricht an Topic '$topic' gesendet.");
|
||||||
} else {
|
} else {
|
||||||
$err = "FEHLER: MQTT Instanz ist kein gültiges MQTT-Modul.";
|
IPS_LogMessage("ShellySwitchSender", "FEHLER: MQTT Instanz ist kein gültiges MQTT-Modul.");
|
||||||
echo $err . "\n";
|
|
||||||
IPS_LogMessage("ShellySwitchSender", $err);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$err = "FEHLER: MQTT Instanz nicht gefunden oder ungültig (ID: $mqttInstanceID).";
|
IPS_LogMessage("ShellySwitchSender", "FEHLER: MQTT Instanz nicht gefunden oder ungültig (ID: $mqttInstanceID).");
|
||||||
echo $err . "\n";
|
|
||||||
IPS_LogMessage("ShellySwitchSender", $err);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user