102 lines
3.8 KiB
PHP
102 lines
3.8 KiB
PHP
<?php
|
|
|
|
class Symcon_Publish_to_Shelly_MQTT extends IPSModule
|
|
{
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
|
|
// MQTT-Verbindungsparameter
|
|
$this->RegisterPropertyString("broker_address", "");
|
|
$this->RegisterPropertyInteger("broker_port", 1883);
|
|
$this->RegisterPropertyString("username", "");
|
|
$this->RegisterPropertyString("password", "");
|
|
$this->RegisterPropertyString("Topic", "");
|
|
|
|
// Nachricht-Payload-Parameter
|
|
$this->RegisterPropertyInteger("msg_id", 100);
|
|
$this->RegisterPropertyString("src", "user1");
|
|
$this->RegisterPropertyString("method", "Switch.Set");
|
|
$this->RegisterPropertyInteger("switch_bool", 0); // Variable-ID einer Bool-Variable
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
}
|
|
|
|
public function RequestAction($Ident, $Value)
|
|
{
|
|
IPS_LogMessage("ShellySwitchSender", "⚙️ RequestAction: $Ident = $Value");
|
|
switch ($Ident) {
|
|
case "GetAction":
|
|
$this->GetAction();
|
|
break;
|
|
default:
|
|
throw new Exception("Invalid action: $Ident");
|
|
}
|
|
}
|
|
|
|
private function GetAction()
|
|
{
|
|
IPS_LogMessage("ShellySwitchSender", " Starte GetAction()...");
|
|
|
|
// Eigenschaften lesen
|
|
$broker = $this->ReadPropertyString("broker_address");
|
|
$port = $this->ReadPropertyInteger("broker_port");
|
|
$user = $this->ReadPropertyString("username");
|
|
$pass = $this->ReadPropertyString("password");
|
|
$topic = $this->ReadPropertyString("Topic");
|
|
|
|
$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)");
|
|
|
|
// JSON-Payload erstellen
|
|
$payload = [
|
|
"id" => 0,
|
|
"src" => $src,
|
|
"method" => $method,
|
|
"params" => [
|
|
"id" => $msg_id,
|
|
"on" => $onValue
|
|
]
|
|
];
|
|
|
|
$json = json_encode($payload);
|
|
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", "MQTTClient gefunden (ID: $clientID, ModuleID: $moduleID)");
|
|
|
|
// 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 an Topic '$topic' gesendet.");
|
|
} else {
|
|
IPS_LogMessage("ShellySwitchSender", " Fehler: 'MQTTClient' Instanz ist kein gültiges MQTT-Modul.");
|
|
}
|
|
} else {
|
|
IPS_LogMessage("ShellySwitchSender", " Fehler: MQTTClient nicht gefunden. Bitte sicherstellen, dass eine Instanz mit Namen 'MQTTClient' existiert.");
|
|
}
|
|
}
|
|
}
|