91 lines
2.9 KiB
PHP
91 lines
2.9 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", "");
|
|
$this->RegisterPropertyInteger("mqtt_instance_id", 0);
|
|
// 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
|
|
|
|
|
|
$this->RegisterTimer("Timer_Influx",5000,"IPS_RequestAction(" . $this->InstanceID . ', "GetAction", "");');
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
|
|
}
|
|
public function RequestAction($Ident, $Value)
|
|
{
|
|
IPS_LogMessage("ShellySwitchSender", "RequestAction gestartet");
|
|
switch ($Ident) {
|
|
case "GetAction":
|
|
$this->GetAction();
|
|
break;
|
|
default:
|
|
throw new Exception("Invalid action");
|
|
}
|
|
}
|
|
|
|
|
|
public function GetAction()
|
|
{
|
|
IPS_LogMessage("ShellySwitchSender", "GetAction gestartet");
|
|
|
|
$mqttInstanceID = $this->ReadPropertyInteger("mqtt_instance_id");
|
|
$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);
|
|
|
|
$payload = [
|
|
"id" => 0,
|
|
"src" => $src,
|
|
"method" => $method,
|
|
"params" => [
|
|
"id" => $msg_id,
|
|
"on" => $onValue
|
|
]
|
|
];
|
|
|
|
$jsonPayload = json_encode($payload);
|
|
|
|
IPS_LogMessage("ShellySwitchSender", "MQTT Payload: $jsonPayload");
|
|
|
|
if (!IPS_InstanceExists($mqttInstanceID)) {
|
|
IPS_LogMessage("ShellySwitchSender", "FEHLER: MQTT-Instanz-ID $mqttInstanceID existiert nicht.");
|
|
return;
|
|
}
|
|
|
|
// ✅ RICHTIG: Direkt senden über MQTTClient_SendMessage
|
|
MQTTClient_SendMessage($mqttInstanceID, $topic, $jsonPayload, 0, false);
|
|
IPS_LogMessage("ShellySwitchSender", "Nachricht erfolgreich gesendet");
|
|
}
|
|
|
|
|
|
|
|
}
|