113 lines
3.4 KiB
PHP
113 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
class Int_VGT extends IPSModule
|
|
{
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
|
|
// Properties
|
|
$this->RegisterPropertyInteger("MQTTServer", 0);
|
|
$this->RegisterPropertyString("DeviceID", "");
|
|
|
|
// Create script handler only once
|
|
if (!@$this->GetIDForIdent("ReceiveScript")) {
|
|
$scriptID = IPS_CreateScript(0);
|
|
IPS_SetName($scriptID, "INTVGT_ReceiveHandler");
|
|
IPS_SetIdent($scriptID, "ReceiveScript");
|
|
IPS_SetParent($scriptID, $this->InstanceID);
|
|
|
|
IPS_SetScriptContent($scriptID, $this->GenerateReceiveScript());
|
|
}
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
|
|
$mqttServer = $this->ReadPropertyInteger("MQTTServer");
|
|
$deviceId = $this->ReadPropertyString("DeviceID");
|
|
|
|
if ($mqttServer == 0 || $deviceId == "") {
|
|
$this->SetStatus(104); // missing config
|
|
return;
|
|
}
|
|
|
|
// Create MQTT Server Devices
|
|
$this->RegisterMQTTDevice("FeedbackRequest", "feedback-request/" . $deviceId);
|
|
$this->RegisterMQTTDevice("RemoteControlResponse", "remote-control-response/" . $deviceId);
|
|
|
|
$this->SetStatus(102);
|
|
}
|
|
|
|
private function RegisterMQTTDevice(string $ident, string $topic)
|
|
{
|
|
$instanceID = @$this->GetIDForIdent($ident);
|
|
|
|
if ($instanceID === false) {
|
|
$instanceID = IPS_CreateInstance("{058A5647-0C2F-4D7D-9F4B-50A28C7D83BA}"); // MQTT Server Device
|
|
IPS_SetParent($instanceID, $this->InstanceID);
|
|
IPS_SetIdent($instanceID, $ident);
|
|
IPS_SetName($instanceID, "MQTT Device: " . $ident);
|
|
}
|
|
|
|
IPS_SetProperty($instanceID, "Topic", $topic);
|
|
IPS_SetProperty($instanceID, "Server", $this->ReadPropertyInteger("MQTTServer"));
|
|
IPS_SetProperty($instanceID, "Script", $this->GetIDForIdent("ReceiveScript"));
|
|
IPS_ApplyChanges($instanceID);
|
|
|
|
return $instanceID;
|
|
}
|
|
|
|
/** Script to be executed when MQTT message is received */
|
|
private function GenerateReceiveScript(): string
|
|
{
|
|
return <<<SCR
|
|
<?php
|
|
|
|
\$parent = IPS_GetParent(\$_IPS['SELF']);
|
|
IPS_RequestAction(\$parent, "OnReceive", json_encode([
|
|
"Topic" => \$_IPS['Topic'],
|
|
"Value" => \$_IPS['VALUE']
|
|
]));
|
|
|
|
SCR;
|
|
}
|
|
|
|
/** Called by ReceiveScript */
|
|
public function RequestAction($ident, $value)
|
|
{
|
|
if ($ident == "OnReceive") {
|
|
$data = json_decode($value, true);
|
|
$this->OnReceiveMQTT($data["Topic"], $data["Value"]);
|
|
}
|
|
}
|
|
|
|
/** THIS IS YOUR CALLBACK FOR INCOMING MQTT MESSAGES */
|
|
private function OnReceiveMQTT(string $topic, string $value)
|
|
{
|
|
IPS_LogMessage("Int_VGT", "Received on $topic: $value");
|
|
|
|
// Here you implement later your handling logic
|
|
// Example: respond to feedback-request
|
|
$mqttServer = $this->ReadPropertyInteger("MQTTServer");
|
|
|
|
if (str_contains($topic, "feedback-request")) {
|
|
$response = json_encode([
|
|
"status" => "ok",
|
|
"timestamp" => time()
|
|
]);
|
|
MQTT_Publish($mqttServer, str_replace("request", "response", $topic), $response, 0, false);
|
|
}
|
|
|
|
if (str_contains($topic, "remote-control-response")) {
|
|
// Echo back the same payload
|
|
MQTT_Publish($mqttServer, str_replace("response", "feedback", $topic), $value, 0, false);
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|