308 lines
9.3 KiB
PHP
308 lines
9.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/libs/ShellyParser.php';
|
|
|
|
class Shelly_Parser_MQTT extends IPSModule
|
|
{
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
|
|
// Verbindung zum MQTT-Server herstellen
|
|
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
|
|
|
// Auf alle Topics lauschen, Filter machen wir selbst
|
|
$this->Subscribe('#');
|
|
|
|
// Debug-Property
|
|
$this->RegisterPropertyBoolean('Debug', false);
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
|
|
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
|
$this->Subscribe('#');
|
|
}
|
|
|
|
/* ---------------------------------------------------------
|
|
* MQTT SUBSCRIBE
|
|
* ---------------------------------------------------------*/
|
|
private function Subscribe(string $topic): void
|
|
{
|
|
$packet = [
|
|
'PacketType' => 8,
|
|
'QualityOfService' => 0,
|
|
'Retain' => false,
|
|
'Topic' => $topic,
|
|
'Payload' => ''
|
|
];
|
|
|
|
$this->SendDataToParent(json_encode([
|
|
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
|
|
] + $packet));
|
|
}
|
|
|
|
/* ---------------------------------------------------------
|
|
* MQTT PUBLISH
|
|
* ---------------------------------------------------------*/
|
|
private function Publish(string $topic, string $payload): void
|
|
{
|
|
$packet = [
|
|
'PacketType' => 3,
|
|
'QualityOfService' => 0,
|
|
'Retain' => false,
|
|
'Topic' => $topic,
|
|
'Payload' => $payload
|
|
];
|
|
|
|
$this->SendDataToParent(json_encode([
|
|
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
|
|
] + $packet));
|
|
|
|
if ($this->ReadPropertyBoolean('Debug')) {
|
|
IPS_LogMessage('Shelly_Parser_MQTT', 'Publish: ' . $topic . ' -> ' . $payload);
|
|
}
|
|
}
|
|
|
|
/* ---------------------------------------------------------
|
|
* RequestAction → Schalten von Outputs
|
|
* ---------------------------------------------------------*/
|
|
public function RequestAction($Ident, $Value)
|
|
{
|
|
// Lokale Variable aktualisieren
|
|
$varID = @IPS_GetObjectIDByIdent($Ident, $this->InstanceID);
|
|
if (!$varID) {
|
|
// Falls Variable im Ordner liegt:
|
|
foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) {
|
|
foreach (IPS_GetChildrenIDs($folder) as $cid) {
|
|
if (IPS_GetObject($cid)['ObjectIdent'] === $Ident) {
|
|
$varID = $cid;
|
|
break 2;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($varID) {
|
|
SetValue($varID, $Value);
|
|
}
|
|
|
|
// Prüfen ob das ein Output ist
|
|
if (str_ends_with($Ident, '_output')) {
|
|
|
|
$deviceID = substr($Ident, 0, -strlen('_output'));
|
|
|
|
$topic = $deviceID . '/rpc/Switch.Set';
|
|
|
|
$payload = json_encode([
|
|
'id' => 0,
|
|
'on' => (bool)$Value
|
|
]);
|
|
|
|
$this->Publish($topic, $payload);
|
|
|
|
if ($this->ReadPropertyBoolean('Debug')) {
|
|
IPS_LogMessage("Shelly_Parser_MQTT", "Switching $deviceID -> $Value");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/* ---------------------------------------------------------
|
|
* RECEIVE MQTT DATA
|
|
* ---------------------------------------------------------*/
|
|
public function ReceiveData($JSONString)
|
|
{
|
|
if ($this->ReadPropertyBoolean('Debug')) {
|
|
IPS_LogMessage('Shelly_Parser_MQTT', 'ReceiveData: ' . $JSONString);
|
|
}
|
|
|
|
$data = json_decode($JSONString, true);
|
|
if (!is_array($data)) {
|
|
return;
|
|
}
|
|
|
|
$topic = $data['Topic'] ?? '';
|
|
$payload = $data['Payload'] ?? '';
|
|
|
|
if ($topic === '') {
|
|
return;
|
|
}
|
|
|
|
$parts = explode('/', $topic);
|
|
$deviceID = $parts[0] ?? '';
|
|
|
|
if ($deviceID === '') {
|
|
return;
|
|
}
|
|
|
|
// <GeräteID>/online
|
|
if ((isset($parts[1])) && ($parts[1] === 'online')) {
|
|
$this->HandleOnline($deviceID, $payload);
|
|
return;
|
|
}
|
|
|
|
// <GeräteID>/events/rpc
|
|
if ((isset($parts[1]) && $parts[1] === 'events') &&
|
|
(isset($parts[2]) && $parts[2] === 'rpc')) {
|
|
$this->HandleRPC($deviceID, $payload);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/* ---------------------------------------------------------
|
|
* ONLINE-STATUS
|
|
* ---------------------------------------------------------*/
|
|
private function HandleOnline(string $deviceID, string $payload): void
|
|
{
|
|
$value = ($payload === 'true' || $payload === '1');
|
|
|
|
$varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_online', 'Online');
|
|
SetValue($varID, $value);
|
|
}
|
|
|
|
/* ---------------------------------------------------------
|
|
* RPC-EVENTS
|
|
* ---------------------------------------------------------*/
|
|
private function HandleRPC(string $deviceID, string $payload): void
|
|
{
|
|
$json = json_decode($payload, true);
|
|
if (!is_array($json)) {
|
|
return;
|
|
}
|
|
|
|
// Shelly prüfen
|
|
$src = $json['src'] ?? '';
|
|
if (!is_string($src) || !str_starts_with($src, 'shelly')) {
|
|
return;
|
|
}
|
|
|
|
// Typ extrahieren (z.B. "1minig3", "1g4", "plusplugs", ...)
|
|
$type = ShellyParser::ExtractType($src);
|
|
$typeID = $this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ');
|
|
SetValue($typeID, $type);
|
|
|
|
// Parameter extrahieren
|
|
$params = $json['params'] ?? [];
|
|
if (!is_array($params)) {
|
|
return;
|
|
}
|
|
|
|
$mapped = ShellyParser::MapParams($params);
|
|
|
|
// Output
|
|
if (array_key_exists('output', $mapped)) {
|
|
$outID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_output', 'Output');
|
|
IPS_SetVariableCustomAction($outID, $this->InstanceID);
|
|
SetValue($outID, (bool)$mapped['output']);
|
|
}
|
|
|
|
|
|
// Input (GEN4 / PRO)
|
|
if (array_key_exists('input', $mapped)) {
|
|
$inID = $this->EnsureBooleanVariable(
|
|
$deviceID,
|
|
$deviceID . '_input',
|
|
'Input'
|
|
);
|
|
SetValue($inID, (bool)$mapped['input']);
|
|
}
|
|
|
|
// Temperatur (falls gefunden)
|
|
if (array_key_exists('temperature', $mapped)) {
|
|
$tempID = $this->EnsureFloatVariable(
|
|
$deviceID,
|
|
$deviceID . '_temperature',
|
|
'Temperatur'
|
|
);
|
|
SetValue($tempID, (float)$mapped['temperature']);
|
|
}
|
|
}
|
|
|
|
/* ---------------------------------------------------------
|
|
* Helper für Variablen
|
|
* ---------------------------------------------------------*/
|
|
private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int
|
|
{
|
|
$folderID = $this->GetDeviceFolder($deviceID);
|
|
|
|
// Suche richtige Variable im Ordner
|
|
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
|
|
$obj = IPS_GetObject($cid);
|
|
if ($obj['ObjectIdent'] === $ident) {
|
|
return $cid; // bestehende Variable gefunden
|
|
}
|
|
}
|
|
|
|
// Neue Variable anlegen
|
|
$varID = IPS_CreateVariable(0); // 0 = Boolean
|
|
IPS_SetName($varID, $name);
|
|
IPS_SetIdent($varID, $ident);
|
|
IPS_SetParent($varID, $folderID);
|
|
|
|
return $varID;
|
|
}
|
|
|
|
|
|
private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int
|
|
{
|
|
$folderID = $this->GetDeviceFolder($deviceID);
|
|
|
|
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
|
|
$obj = IPS_GetObject($cid);
|
|
if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) {
|
|
return $cid;
|
|
}
|
|
}
|
|
|
|
$id = $this->RegisterVariableFloat($ident, $name);
|
|
IPS_SetParent($id, $folderID);
|
|
return $id;
|
|
}
|
|
|
|
private function EnsureStringVariable(string $deviceID, string $ident, string $name): int
|
|
{
|
|
$folderID = $this->GetDeviceFolder($deviceID);
|
|
|
|
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
|
|
$obj = IPS_GetObject($cid);
|
|
if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) {
|
|
return $cid;
|
|
}
|
|
}
|
|
|
|
$id = $this->RegisterVariableString($ident, $name);
|
|
IPS_SetParent($id, $folderID);
|
|
return $id;
|
|
}
|
|
|
|
/* ---------------------------------------------------------
|
|
* Geräte-Ordner
|
|
* ---------------------------------------------------------*/
|
|
private function GetDeviceFolder(string $deviceID): int
|
|
{
|
|
$folderIdent = 'folder_' . $deviceID;
|
|
|
|
// nur unter dieser Instanz suchen
|
|
foreach (IPS_GetChildrenIDs($this->InstanceID) as $cid) {
|
|
$obj = IPS_GetObject($cid);
|
|
if ($obj['ObjectIdent'] === $folderIdent && $obj['ObjectType'] === OBJECTTYPE_CATEGORY) {
|
|
return $cid;
|
|
}
|
|
}
|
|
|
|
// neu anlegen
|
|
$folderID = IPS_CreateCategory();
|
|
IPS_SetParent($folderID, $this->InstanceID);
|
|
IPS_SetName($folderID, $deviceID);
|
|
IPS_SetIdent($folderID, $folderIdent);
|
|
|
|
return $folderID;
|
|
}
|
|
}
|
|
?>
|