104 lines
3.6 KiB
PHP
104 lines
3.6 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->RegisterPropertyString("client_id", "");
|
|
|
|
// 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
|
|
|
|
|
|
}
|
|
|
|
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)
|
|
{
|
|
switch ($Ident) {
|
|
case "GetAction":
|
|
$this->GetAction();
|
|
break;
|
|
default:
|
|
throw new Exception("Invalid action");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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");
|
|
$client_id = $this->ReadPropertyString("client_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");
|
|
|
|
$mqtt = new phpMQTT($broker, $port, $client_id);
|
|
|
|
if ($mqtt->connect(true, NULL, $username, $password)) {
|
|
IPS_LogMessage("ShellySwitchSender", "Verbunden mit dem MQTT-Broker");
|
|
$mqtt->publish($topic, $payload, 0);
|
|
IPS_LogMessage("ShellySwitchSender", "Nachricht gesende");
|
|
$mqtt->close();
|
|
} else {
|
|
IPS_LogMessage("ShellySwitchSender", "Verbindung fehlgeschlagen");
|
|
}
|
|
}
|
|
}
|