no message
This commit is contained in:
@@ -13,9 +13,10 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
// MQTT-Server verbinden
|
// MQTT-Server verbinden
|
||||||
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
||||||
|
|
||||||
// Auf alle Topics hören
|
// alle Topics hören
|
||||||
$this->Subscribe('#');
|
$this->Subscribe('#');
|
||||||
|
|
||||||
|
// Debug
|
||||||
$this->RegisterPropertyBoolean('Debug', false);
|
$this->RegisterPropertyBoolean('Debug', false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,6 +28,19 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
$this->Subscribe('#');
|
$this->Subscribe('#');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------
|
||||||
|
* DEBUG WRAPPER
|
||||||
|
* ---------------------------------------------------------*/
|
||||||
|
private function Log($title, $msg)
|
||||||
|
{
|
||||||
|
if (!$this->ReadPropertyBoolean('Debug')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
IPS_LogMessage("ShellyMQTT - $title", $msg);
|
||||||
|
$this->SendDebug($title, $msg, 0);
|
||||||
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------
|
/* ---------------------------------------------------------
|
||||||
* MQTT SUBSCRIBE
|
* MQTT SUBSCRIBE
|
||||||
* ---------------------------------------------------------*/
|
* ---------------------------------------------------------*/
|
||||||
@@ -43,6 +57,8 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
$this->SendDataToParent(json_encode([
|
$this->SendDataToParent(json_encode([
|
||||||
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
|
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
|
||||||
] + $packet));
|
] + $packet));
|
||||||
|
|
||||||
|
$this->Log("Subscribe", "Topic: $topic");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------
|
/* ---------------------------------------------------------
|
||||||
@@ -62,7 +78,7 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
|
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
|
||||||
] + $packet));
|
] + $packet));
|
||||||
|
|
||||||
$this->SendDebug("Publish", "$topic → $payload", 0);
|
$this->Log("Publish", "$topic → $payload");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------
|
/* ---------------------------------------------------------
|
||||||
@@ -70,7 +86,7 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
* ---------------------------------------------------------*/
|
* ---------------------------------------------------------*/
|
||||||
public function RequestAction($Ident, $Value)
|
public function RequestAction($Ident, $Value)
|
||||||
{
|
{
|
||||||
$this->SendDebug('RequestAction', "$Ident → " . json_encode($Value), 0);
|
$this->Log('RequestAction', "$Ident → " . json_encode($Value));
|
||||||
|
|
||||||
if (!str_contains($Ident, '_output_')) {
|
if (!str_contains($Ident, '_output_')) {
|
||||||
throw new Exception("Unknown Ident: " . $Ident);
|
throw new Exception("Unknown Ident: " . $Ident);
|
||||||
@@ -100,18 +116,18 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->SendDebug('MQTT SEND', "$topic : $payload", 0);
|
$this->Log('MQTT SEND', "$topic : $payload");
|
||||||
|
|
||||||
// absenden
|
// absenden
|
||||||
$this->Publish($topic, $payload);
|
$this->Publish($topic, $payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------
|
/* ---------------------------------------------------------
|
||||||
* RECEIVE MQTT DATA
|
* MQTT RECEIVE
|
||||||
* ---------------------------------------------------------*/
|
* ---------------------------------------------------------*/
|
||||||
public function ReceiveData($JSONString)
|
public function ReceiveData($JSONString)
|
||||||
{
|
{
|
||||||
$this->SendDebug('ReceiveData', $JSONString, 0);
|
$this->Log('ReceiveData', $JSONString);
|
||||||
|
|
||||||
$data = json_decode($JSONString, true);
|
$data = json_decode($JSONString, true);
|
||||||
if (!is_array($data)) {
|
if (!is_array($data)) {
|
||||||
@@ -121,26 +137,24 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
$topic = $data['Topic'] ?? '';
|
$topic = $data['Topic'] ?? '';
|
||||||
$payload = $data['Payload'] ?? '';
|
$payload = $data['Payload'] ?? '';
|
||||||
|
|
||||||
if ($topic === '') {
|
if ($topic === '') return;
|
||||||
return;
|
|
||||||
}
|
$this->Log('ReceiveTopic', "$topic → $payload");
|
||||||
|
|
||||||
$parts = explode('/', $topic);
|
$parts = explode('/', $topic);
|
||||||
$deviceID = $parts[0] ?? '';
|
$deviceID = $parts[0] ?? '';
|
||||||
|
|
||||||
if ($deviceID === '') {
|
if ($deviceID === '') return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// <deviceID>/online
|
// <deviceID>/online
|
||||||
if ((isset($parts[1])) && ($parts[1] === 'online')) {
|
if (($parts[1] ?? '') === 'online') {
|
||||||
$this->HandleOnline($deviceID, $payload);
|
$this->HandleOnline($deviceID, $payload);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// <deviceID>/events/rpc
|
// <deviceID>/events/rpc
|
||||||
if ((isset($parts[1]) && $parts[1] === 'events') &&
|
if (($parts[1] ?? '') === 'events' &&
|
||||||
(isset($parts[2]) && $parts[2] === 'rpc')) {
|
($parts[2] ?? '') === 'rpc') {
|
||||||
$this->HandleRPC($deviceID, $payload);
|
$this->HandleRPC($deviceID, $payload);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -153,24 +167,24 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
{
|
{
|
||||||
$value = ($payload === 'true' || $payload === '1');
|
$value = ($payload === 'true' || $payload === '1');
|
||||||
|
|
||||||
|
$this->Log("DeviceOnline", "$deviceID = " . json_encode($value));
|
||||||
|
|
||||||
$varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_online', 'Online');
|
$varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_online', 'Online');
|
||||||
SetValue($varID, $value);
|
SetValue($varID, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------
|
/* ---------------------------------------------------------
|
||||||
* RPC-EVENTS VERARBEITEN
|
* RPC DATA
|
||||||
* ---------------------------------------------------------*/
|
* ---------------------------------------------------------*/
|
||||||
private function HandleRPC(string $deviceID, string $payload): void
|
private function HandleRPC(string $deviceID, string $payload): void
|
||||||
{
|
{
|
||||||
|
$this->Log("HandleRPC", "$deviceID : $payload");
|
||||||
|
|
||||||
$json = json_decode($payload, true);
|
$json = json_decode($payload, true);
|
||||||
if (!is_array($json)) {
|
if (!is_array($json)) return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$src = $json['src'] ?? '';
|
$src = $json['src'] ?? '';
|
||||||
if (!str_starts_with($src, 'shelly')) {
|
if (!str_starts_with($src, 'shelly')) return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gerätetyp merken
|
// Gerätetyp merken
|
||||||
$type = ShellyParser::ExtractType($src);
|
$type = ShellyParser::ExtractType($src);
|
||||||
@@ -186,7 +200,9 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
* -------------------------*/
|
* -------------------------*/
|
||||||
foreach ($mapped['outputs'] as $index => $value) {
|
foreach ($mapped['outputs'] as $index => $value) {
|
||||||
$ident = $deviceID . '_output_' . $index;
|
$ident = $deviceID . '_output_' . $index;
|
||||||
$name = 'Output ' . $index;
|
$name = "Output $index";
|
||||||
|
|
||||||
|
$this->Log("RPC Output", "$ident = " . json_encode($value));
|
||||||
|
|
||||||
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
|
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
|
||||||
SetValue($varID, $value);
|
SetValue($varID, $value);
|
||||||
@@ -197,7 +213,9 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
* -------------------------*/
|
* -------------------------*/
|
||||||
foreach ($mapped['inputs'] as $index => $value) {
|
foreach ($mapped['inputs'] as $index => $value) {
|
||||||
$ident = $deviceID . '_input_' . $index;
|
$ident = $deviceID . '_input_' . $index;
|
||||||
$name = 'Input ' . $index;
|
$name = "Input $index";
|
||||||
|
|
||||||
|
$this->Log("RPC Input", "$ident = " . json_encode($value));
|
||||||
|
|
||||||
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
|
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
|
||||||
SetValue($varID, $value);
|
SetValue($varID, $value);
|
||||||
@@ -207,27 +225,59 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
* TEMPERATUR
|
* TEMPERATUR
|
||||||
* -------------------------*/
|
* -------------------------*/
|
||||||
if ($mapped['temperature'] !== null) {
|
if ($mapped['temperature'] !== null) {
|
||||||
|
$this->Log("RPC Temperature", json_encode($mapped['temperature']));
|
||||||
|
|
||||||
$tempID = $this->EnsureFloatVariable($deviceID, $deviceID . '_temperature', 'Temperatur');
|
$tempID = $this->EnsureFloatVariable($deviceID, $deviceID . '_temperature', 'Temperatur');
|
||||||
SetValue($tempID, $mapped['temperature']);
|
SetValue($tempID, $mapped['temperature']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------
|
/* ---------------------------------------------------------
|
||||||
* HELPER: BOOLEAN VARIABLE
|
* AUTOMATISCHES ACTION-SCRIPT
|
||||||
|
* ---------------------------------------------------------*/
|
||||||
|
private function EnsureActionScript(): int
|
||||||
|
{
|
||||||
|
$scriptName = "ShellyMQTT_Action_" . $this->InstanceID;
|
||||||
|
|
||||||
|
// existiert bereits?
|
||||||
|
foreach (IPS_GetChildrenIDs($this->InstanceID) as $cid) {
|
||||||
|
$obj = IPS_GetObject($cid);
|
||||||
|
if ($obj['ObjectType'] === OBJECTTYPE_SCRIPT && $obj['ObjectName'] === $scriptName) {
|
||||||
|
return $cid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// neues Script erzeugen
|
||||||
|
$scriptID = IPS_CreateScript(0);
|
||||||
|
IPS_SetName($scriptID, $scriptName);
|
||||||
|
IPS_SetParent($scriptID, $this->InstanceID);
|
||||||
|
|
||||||
|
$php = '<?php
|
||||||
|
$inst = ' . $this->InstanceID . ';
|
||||||
|
$ident = IPS_GetObject($_IPS["VARIABLE"])["ObjectIdent"];
|
||||||
|
$value = $_IPS["VALUE"];
|
||||||
|
IPS_RequestAction($inst, $ident, $value);
|
||||||
|
?>';
|
||||||
|
|
||||||
|
IPS_SetScriptContent($scriptID, $php);
|
||||||
|
|
||||||
|
return $scriptID;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------
|
||||||
|
* BOOLEAN VARIABLE
|
||||||
* ---------------------------------------------------------*/
|
* ---------------------------------------------------------*/
|
||||||
private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int
|
private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int
|
||||||
{
|
{
|
||||||
$folderID = $this->GetDeviceFolder($deviceID);
|
$folderID = $this->GetDeviceFolder($deviceID);
|
||||||
|
|
||||||
// bestehende Variable?
|
|
||||||
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
|
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
|
||||||
$obj = IPS_GetObject($cid);
|
$o = IPS_GetObject($cid);
|
||||||
|
if ($o['ObjectIdent'] === $ident) {
|
||||||
|
|
||||||
if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) {
|
|
||||||
|
|
||||||
// OUTPUT → immer CustomAction setzen
|
|
||||||
if (str_contains($ident, '_output_')) {
|
if (str_contains($ident, '_output_')) {
|
||||||
IPS_SetVariableCustomAction($cid, $this->InstanceID);
|
$actionScript = $this->EnsureActionScript();
|
||||||
|
IPS_SetVariableCustomAction($cid, $actionScript);
|
||||||
|
|
||||||
$var = IPS_GetVariable($cid);
|
$var = IPS_GetVariable($cid);
|
||||||
if ($var['VariableProfile'] === '' && $var['VariableCustomProfile'] === '') {
|
if ($var['VariableProfile'] === '' && $var['VariableCustomProfile'] === '') {
|
||||||
@@ -239,15 +289,15 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// neue Variable
|
// neu
|
||||||
$varID = IPS_CreateVariable(0);
|
$varID = IPS_CreateVariable(0);
|
||||||
IPS_SetIdent($varID, $ident);
|
IPS_SetIdent($varID, $ident);
|
||||||
IPS_SetName($varID, $name);
|
IPS_SetName($varID, $name);
|
||||||
IPS_SetParent($varID, $folderID);
|
IPS_SetParent($varID, $folderID);
|
||||||
|
|
||||||
// OUTPUT-Profil + Action
|
|
||||||
if (str_contains($ident, '_output_')) {
|
if (str_contains($ident, '_output_')) {
|
||||||
IPS_SetVariableCustomAction($varID, $this->InstanceID);
|
$actionScript = $this->EnsureActionScript();
|
||||||
|
IPS_SetVariableCustomAction($varID, $actionScript);
|
||||||
IPS_SetVariableCustomProfile($varID, '~Switch');
|
IPS_SetVariableCustomProfile($varID, '~Switch');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,10 +312,8 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
$folderID = $this->GetDeviceFolder($deviceID);
|
$folderID = $this->GetDeviceFolder($deviceID);
|
||||||
|
|
||||||
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
|
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
|
||||||
$obj = IPS_GetObject($cid);
|
if (IPS_GetObject($cid)['ObjectIdent'] === $ident)
|
||||||
if ($obj['ObjectIdent'] === $ident) {
|
|
||||||
return $cid;
|
return $cid;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$id = $this->RegisterVariableFloat($ident, $name);
|
$id = $this->RegisterVariableFloat($ident, $name);
|
||||||
@@ -281,10 +329,8 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
$folderID = $this->GetDeviceFolder($deviceID);
|
$folderID = $this->GetDeviceFolder($deviceID);
|
||||||
|
|
||||||
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
|
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
|
||||||
$obj = IPS_GetObject($cid);
|
if (IPS_GetObject($cid)['ObjectIdent'] === $ident)
|
||||||
if ($obj['ObjectIdent'] === $ident) {
|
|
||||||
return $cid;
|
return $cid;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$id = $this->RegisterVariableString($ident, $name);
|
$id = $this->RegisterVariableString($ident, $name);
|
||||||
@@ -300,13 +346,10 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
$folderIdent = 'folder_' . $deviceID;
|
$folderIdent = 'folder_' . $deviceID;
|
||||||
|
|
||||||
foreach (IPS_GetChildrenIDs($this->InstanceID) as $cid) {
|
foreach (IPS_GetChildrenIDs($this->InstanceID) as $cid) {
|
||||||
$obj = IPS_GetObject($cid);
|
if (IPS_GetObject($cid)['ObjectIdent'] === $folderIdent)
|
||||||
if ($obj['ObjectIdent'] === $folderIdent && $obj['ObjectType'] === OBJECTTYPE_CATEGORY) {
|
|
||||||
return $cid;
|
return $cid;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// neu anlegen
|
|
||||||
$folderID = IPS_CreateCategory();
|
$folderID = IPS_CreateCategory();
|
||||||
IPS_SetParent($folderID, $this->InstanceID);
|
IPS_SetParent($folderID, $this->InstanceID);
|
||||||
IPS_SetName($folderID, $deviceID);
|
IPS_SetName($folderID, $deviceID);
|
||||||
@@ -316,7 +359,7 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------
|
/* ---------------------------------------------------------
|
||||||
* HELPER: VARIABLE FINDEN
|
* VARIABLE FINDEN
|
||||||
* ---------------------------------------------------------*/
|
* ---------------------------------------------------------*/
|
||||||
private function FindVariableByIdent(string $Ident)
|
private function FindVariableByIdent(string $Ident)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user