116 lines
4.5 KiB
PHP
116 lines
4.5 KiB
PHP
<?php
|
|
|
|
class Symcon_Publish_to_Shelly_MQTT extends IPSModule
|
|
{
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
|
|
// MQTT-Verbindungsparameter als Properties
|
|
$this->RegisterPropertyString("broker_address", "");
|
|
$this->RegisterPropertyInteger("broker_port", 1883);
|
|
$this->RegisterPropertyString("username", "");
|
|
$this->RegisterPropertyString("password", "");
|
|
$this->RegisterPropertyString("Topic", "");
|
|
$this->RegisterPropertyInteger("mqtt_instance_id", 0); // ID der MQTT-Instanz als Konfig-Feld
|
|
|
|
// Nachricht-Payload-Parameter als Properties
|
|
$this->RegisterPropertyInteger("msg_id", 100);
|
|
$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
|
|
}
|
|
|
|
/**
|
|
* RequestAction wird aufgerufen, wenn die Variable "Trigger" im WebFront oder Script geändert wird
|
|
*/
|
|
public function RequestAction($Ident, $Value)
|
|
{
|
|
IPS_LogMessage("ShellySwitchSender", "RequestAction aufgerufen: Ident = $Ident, Value = " . var_export($Value, true));
|
|
|
|
switch ($Ident) {
|
|
case "Trigger":
|
|
SetValue($this->GetIDForIdent("Trigger"), $Value);
|
|
$this->GetAction();
|
|
break;
|
|
default:
|
|
throw new Exception("Ungültige Aktion: $Ident");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Hauptfunktion: Liest Konfiguration, baut Payload, sendet MQTT Nachricht und loggt alles
|
|
*/
|
|
public function GetAction()
|
|
{
|
|
IPS_LogMessage("ShellySwitchSender", "GetAction gestartet");
|
|
|
|
$broker = $this->ReadPropertyString("broker_address");
|
|
$port = $this->ReadPropertyInteger("broker_port");
|
|
$user = $this->ReadPropertyString("username");
|
|
$pass = $this->ReadPropertyString("password");
|
|
$topic = $this->ReadPropertyString("Topic");
|
|
$mqttInstanceID = $this->ReadPropertyInteger("mqtt_instance_id");
|
|
|
|
$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 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"));
|
|
|
|
$payload = [
|
|
"id" => 0,
|
|
"src" => $src,
|
|
"method" => $method,
|
|
"params" => [
|
|
"id" => $msg_id,
|
|
"on" => $onValue
|
|
]
|
|
];
|
|
|
|
$json = json_encode($payload);
|
|
IPS_LogMessage("ShellySwitchSender", "MQTT Payload: $json");
|
|
|
|
if ($mqttInstanceID > 0 && IPS_InstanceExists($mqttInstanceID)) {
|
|
$instanceInfo = IPS_GetInstance($mqttInstanceID);
|
|
$moduleID = $instanceInfo['ModuleInfo']['ModuleID'];
|
|
IPS_LogMessage("ShellySwitchSender", "MQTT Instanz gefunden: ID $mqttInstanceID, ModuleID $moduleID");
|
|
|
|
if (
|
|
$moduleID == "{C03937E3-5F86-EACA-B4A2-39A24ACF945A}" || // Alter MQTTClient
|
|
$moduleID == "{C8792760-65CF-4C53-B5C7-A30FCC84FEFE}" // Offizieller MQTTClient
|
|
) {
|
|
MQTTClient_Publish($mqttInstanceID, $topic, $json, 0, false);
|
|
IPS_LogMessage("ShellySwitchSender", "Nachricht an Topic '$topic' gesendet.");
|
|
} else {
|
|
IPS_LogMessage("ShellySwitchSender", "FEHLER: MQTT Instanz ist kein gültiges MQTT-Modul.");
|
|
}
|
|
} else {
|
|
IPS_LogMessage("ShellySwitchSender", "FEHLER: MQTT Instanz nicht gefunden oder ungültig (ID: $mqttInstanceID).");
|
|
}
|
|
}
|
|
}
|