Files
Symcon_Belevo_Energiemanage…/Symcon_Publish_to_Shelly_MQTT/module.php
belevo\mh 5de506ad16 .
2025-05-26 10:03:12 +02:00

143 lines
5.4 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
}
public function ApplyChanges()
{
parent::ApplyChanges();
// Hier ggf. weitere Aktionen bei Änderungen, z.B. RegisterTimer o.Ä.
}
/**
* Wird aufgerufen, wenn eine Aktion auf ein Ident (z.B. Variable) ausgeführt wird
*
* @param string $Ident
* @param mixed $Value
* @throws Exception
*/
public function RequestAction($Ident, $Value)
{
$msg = "RequestAction aufgerufen: Ident = $Ident, Value = " . var_export($Value, true);
echo $msg . "\n";
IPS_LogMessage("ShellySwitchSender", $msg);
switch ($Ident) {
case "GetAction":
$this->GetAction();
break;
default:
throw new Exception("Invalid action: $Ident");
}
}
/**
* Führt die eigentliche Aktion aus: Liest Werte, baut Payload, sendet MQTT Nachricht
*/
public function GetAction()
{
$msg = "Starte GetAction()...";
echo $msg . "\n";
IPS_LogMessage("ShellySwitchSender", $msg);
// MQTT Broker-Daten aus Properties
$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");
// Payload-Parameter aus Properties
$msg_id = $this->ReadPropertyInteger("msg_id");
$src = $this->ReadPropertyString("src");
$method = $this->ReadPropertyString("method");
$boolVarID = $this->ReadPropertyInteger("switch_bool");
// Prüfe ob Bool-Variable existiert
if (!IPS_VariableExists($boolVarID)) {
$err = "FEHLER: Bool-Variable mit ID $boolVarID existiert nicht.";
echo $err . "\n";
IPS_LogMessage("ShellySwitchSender", $err);
return;
}
// Bool-Wert aus Variable lesen
$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", "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"));
// JSON-Payload erzeugen
$payload = [
"id" => 0,
"src" => $src,
"method" => $method,
"params" => [
"id" => $msg_id,
"on" => $onValue
]
];
$json = json_encode($payload);
echo "MQTT Payload: $json\n";
IPS_LogMessage("ShellySwitchSender", "MQTT Payload: $json");
// MQTTClient-Instanz prüfen und Nachricht senden
if ($mqttInstanceID > 0 && IPS_InstanceExists($mqttInstanceID)) {
$instanceInfo = IPS_GetInstance($mqttInstanceID);
$moduleID = $instanceInfo['ModuleInfo']['ModuleID'];
echo "MQTT Instanz gefunden: ID $mqttInstanceID, ModuleID $moduleID\n";
IPS_LogMessage("ShellySwitchSender", "MQTT Instanz gefunden: ID $mqttInstanceID, ModuleID $moduleID");
// Prüfe ob Instanz ein MQTTClient ist
if (
$moduleID == "{C03937E3-5F86-EACA-B4A2-39A24ACF945A}" || // Alter MQTTClient
$moduleID == "{C8792760-65CF-4C53-B5C7-A30FCC84FEFE}" // Offizieller MQTTClient
) {
MQTTClient_Publish($mqttInstanceID, $topic, $json, 0, false);
echo "Nachricht an Topic '$topic' gesendet.\n";
IPS_LogMessage("ShellySwitchSender", "Nachricht an Topic '$topic' gesendet.");
} else {
$err = "FEHLER: MQTT Instanz ist kein gültiges MQTT-Modul.";
echo $err . "\n";
IPS_LogMessage("ShellySwitchSender", $err);
}
} else {
$err = "FEHLER: MQTT Instanz nicht gefunden oder ungültig (ID: $mqttInstanceID).";
echo $err . "\n";
IPS_LogMessage("ShellySwitchSender", $err);
}
}
}