Files
Symcon_Belevo_Energiemanage…/Symcon_Publish_to_Shelly_MQTT/module.php
belevo\mh c9b6584e86 .
2025-05-26 09:37:58 +02:00

93 lines
3.3 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", 1);
$this->RegisterPropertyString("src", "user1");
$this->RegisterPropertyString("method", "Switch.Set");
$this->RegisterPropertyInteger("switch_bool", 0);
}
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");
}
}
private function GetAction()
{
IPS_LogMessage("ShellySwitchSender", " Starte GetAction()...");
// Eigenschaften lesen
$broker = GetValue($this->ReadPropertyString("broker_address"));
$port = GetValue($this->ReadPropertyInteger("broker_port"));
$user = GetValue($this->ReadPropertyString("username"));
$pass = GetValue($this->ReadPropertyString("password"));
$topic = GetValue($this->ReadPropertyString("Topic"));
$msg_id = GetValue($this->ReadPropertyInteger("msg_id"));
$src = GetValue($this->ReadPropertyString("src"));
$method = GetValue($this->ReadPropertyString("method"));
$switch_bool = GetValueBoolean($this->ReadPropertyInteger("switch_bool"));
IPS_LogMessage("ShellySwitchSender", "Broker: $broker:$port, Topic: $topic");
IPS_LogMessage("ShellySwitchSender", " src: $src, method: $method, msg_id: $msg_id");
IPS_LogMessage("ShellySwitchSender", " boolVarID: $switch_bool");
// JSON-Payload erstellen
$payload = [
"id" => 0,
"src" => $src,
"method" => $method,
"params" => [
"id" => $msg_id,
"on" => $switch_bool
]
];
$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", "📡 Gefundener MQTTClient mit ModuleID: $moduleID");
if ($moduleID == "{C03937E3-5F86-EACA-B4A2-39A24ACF945A}") {
MQTTClient_Publish($clientID, $topic, $json, 0, false);
IPS_LogMessage("ShellySwitchSender", " Nachricht erfolgreich an '$topic' gesendet.");
} else {
IPS_LogMessage("ShellySwitchSender", " Fehler: Instanz 'MQTTClient' hat falsches Modul.");
}
} else {
IPS_LogMessage("ShellySwitchSender", " Kein MQTTClient gefunden oder benannt als 'MQTTClient'.");
}
}
}
?>