no message
This commit is contained in:
@@ -10,13 +10,11 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
{
|
||||
parent::Create();
|
||||
|
||||
// MQTT-Server verbinden
|
||||
// MQTT verbinden
|
||||
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
|
||||
|
||||
// alle Topics hören
|
||||
// Alles abonnieren
|
||||
$this->Subscribe('#');
|
||||
|
||||
// Debug
|
||||
}
|
||||
|
||||
public function ApplyChanges()
|
||||
@@ -28,12 +26,11 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------
|
||||
* DEBUG WRAPPER
|
||||
* DEBUG
|
||||
* ---------------------------------------------------------*/
|
||||
private function Log($title, $msg){
|
||||
|
||||
|
||||
IPS_LogMessage("ShellyMQTT - $title", $msg);
|
||||
private function Log($title, $msg)
|
||||
{
|
||||
IPS_LogMessage("ShellyMQTT: $title", $msg);
|
||||
$this->SendDebug($title, $msg, 0);
|
||||
}
|
||||
|
||||
@@ -54,7 +51,7 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
|
||||
] + $packet));
|
||||
|
||||
$this->Log("Subscribe", "Topic: $topic");
|
||||
$this->Log("Subscribe", $topic);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------
|
||||
@@ -78,30 +75,30 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------
|
||||
* REQUEST ACTION (Shelly schalten)
|
||||
* REQUEST ACTION (Schalten von Outputs)
|
||||
* ---------------------------------------------------------*/
|
||||
public function RequestAction($Ident, $Value)
|
||||
{
|
||||
$this->Log('RequestAction', "$Ident → " . json_encode($Value));
|
||||
$this->Log('RequestAction', "$Ident → $Value");
|
||||
|
||||
if (!str_contains($Ident, '_output_')) {
|
||||
throw new Exception("Unknown Ident: " . $Ident);
|
||||
throw new Exception("Unknown Ident: $Ident");
|
||||
}
|
||||
|
||||
// lokale Variable setzen
|
||||
// Lokale Variable updaten
|
||||
$varID = $this->FindVariableByIdent($Ident);
|
||||
if ($varID) {
|
||||
if ($varID > 0) {
|
||||
SetValue($varID, $Value);
|
||||
}
|
||||
|
||||
// device + index extrahieren
|
||||
// Gerät + Index extrahieren
|
||||
[$deviceID, $suffix] = explode('_output_', $Ident, 2);
|
||||
$index = intval($suffix);
|
||||
|
||||
// RPC Topic
|
||||
// Shelly RPC Topic
|
||||
$topic = $deviceID . '/rpc';
|
||||
|
||||
// RPC JSON Payload
|
||||
// RPC JSON
|
||||
$payload = json_encode([
|
||||
'id' => 1,
|
||||
'src' => 'ips',
|
||||
@@ -112,23 +109,21 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
]
|
||||
]);
|
||||
|
||||
$this->Log('MQTT SEND', "$topic : $payload");
|
||||
$this->Log("MQTT SEND", "$topic : $payload");
|
||||
|
||||
// absenden
|
||||
// Senden
|
||||
$this->Publish($topic, $payload);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------
|
||||
* MQTT RECEIVE
|
||||
* RECEIVE MQTT
|
||||
* ---------------------------------------------------------*/
|
||||
public function ReceiveData($JSONString)
|
||||
{
|
||||
$this->Log('ReceiveData', $JSONString);
|
||||
|
||||
$data = json_decode($JSONString, true);
|
||||
if (!is_array($data)) {
|
||||
return;
|
||||
}
|
||||
if (!is_array($data)) return;
|
||||
|
||||
$topic = $data['Topic'] ?? '';
|
||||
$payload = $data['Payload'] ?? '';
|
||||
@@ -142,13 +137,13 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
|
||||
if ($deviceID === '') return;
|
||||
|
||||
// <deviceID>/online
|
||||
// Online-Status
|
||||
if (($parts[1] ?? '') === 'online') {
|
||||
$this->HandleOnline($deviceID, $payload);
|
||||
return;
|
||||
}
|
||||
|
||||
// <deviceID>/events/rpc
|
||||
// RPC Event
|
||||
if (($parts[1] ?? '') === 'events' &&
|
||||
($parts[2] ?? '') === 'rpc') {
|
||||
$this->HandleRPC($deviceID, $payload);
|
||||
@@ -163,18 +158,18 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
{
|
||||
$value = ($payload === 'true' || $payload === '1');
|
||||
|
||||
$this->Log("DeviceOnline", "$deviceID = " . json_encode($value));
|
||||
$this->Log("Online", "$deviceID = $value");
|
||||
|
||||
$varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_online', 'Online');
|
||||
SetValue($varID, $value);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------
|
||||
* RPC DATA
|
||||
* RPC-HANDLING
|
||||
* ---------------------------------------------------------*/
|
||||
private function HandleRPC(string $deviceID, string $payload): void
|
||||
{
|
||||
$this->Log("HandleRPC", "$deviceID : $payload");
|
||||
$this->Log("RPC", "$deviceID : $payload");
|
||||
|
||||
$json = json_decode($payload, true);
|
||||
if (!is_array($json)) return;
|
||||
@@ -182,58 +177,76 @@ class Shelly_Parser_MQTT extends IPSModule
|
||||
$src = $json['src'] ?? '';
|
||||
if (!str_starts_with($src, 'shelly')) return;
|
||||
|
||||
// Gerätetyp merken
|
||||
// Typ
|
||||
$type = ShellyParser::ExtractType($src);
|
||||
$typeID = $this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ');
|
||||
SetValue($typeID, $type);
|
||||
|
||||
// params mappen
|
||||
// Parameter
|
||||
$params = $json['params'] ?? [];
|
||||
$mapped = ShellyParser::MapParams($params);
|
||||
|
||||
/* -------------------------
|
||||
* OUTPUTS
|
||||
* -------------------------*/
|
||||
// Outputs
|
||||
foreach ($mapped['outputs'] as $index => $value) {
|
||||
$ident = $deviceID . '_output_' . $index;
|
||||
$name = "Output $index";
|
||||
|
||||
$this->Log("RPC Output", "$ident = " . json_encode($value));
|
||||
|
||||
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
|
||||
$ident = $deviceID . "_output_" . $index;
|
||||
$varID = $this->EnsureBooleanVariable($deviceID, $ident, "Output $index");
|
||||
SetValue($varID, $value);
|
||||
}
|
||||
|
||||
/* -------------------------
|
||||
* INPUTS
|
||||
* -------------------------*/
|
||||
// Inputs
|
||||
foreach ($mapped['inputs'] as $index => $value) {
|
||||
$ident = $deviceID . '_input_' . $index;
|
||||
$name = "Input $index";
|
||||
|
||||
$this->Log("RPC Input", "$ident = " . json_encode($value));
|
||||
|
||||
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
|
||||
$ident = $deviceID . "_input_" . $index;
|
||||
$varID = $this->EnsureBooleanVariable($deviceID, $ident, "Input $index");
|
||||
SetValue($varID, $value);
|
||||
}
|
||||
|
||||
/* -------------------------
|
||||
* TEMPERATUR
|
||||
* -------------------------*/
|
||||
// Temperatur
|
||||
if ($mapped['temperature'] !== null) {
|
||||
$this->Log("RPC Temperature", json_encode($mapped['temperature']));
|
||||
|
||||
$tempID = $this->EnsureFloatVariable($deviceID, $deviceID . '_temperature', 'Temperatur');
|
||||
SetValue($tempID, $mapped['temperature']);
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------
|
||||
* AUTOMATISCHES ACTION-SCRIPT
|
||||
* ACTIONSCRIPT – UNSICHTBAR
|
||||
* ---------------------------------------------------------*/
|
||||
private function EnsureActionScript(): int
|
||||
{
|
||||
$scriptName = "~ShellyMQTT_Action_" . $this->InstanceID;
|
||||
|
||||
foreach (IPS_GetChildrenIDs($this->InstanceID) as $cid) {
|
||||
$obj = IPS_GetObject($cid);
|
||||
|
||||
if ($obj['ObjectType'] === OBJECTTYPE_SCRIPT && $obj['ObjectName'] === $scriptName) {
|
||||
return $cid;
|
||||
}
|
||||
}
|
||||
|
||||
// neues verstecktes Script
|
||||
$scriptID = IPS_CreateScript(0);
|
||||
IPS_SetName($scriptID, $scriptName);
|
||||
IPS_SetParent($scriptID, $this->InstanceID);
|
||||
|
||||
// unsichtbar
|
||||
IPS_SetHidden($scriptID, true);
|
||||
IPS_SetPosition($scriptID, -9999);
|
||||
|
||||
// Scriptcode
|
||||
$php = '<?php
|
||||
$instance = ' . $this->InstanceID . ';
|
||||
$object = IPS_GetObject($_IPS["VARIABLE"]);
|
||||
$ident = $object["ObjectIdent"];
|
||||
$value = $_IPS["VALUE"];
|
||||
IPS_RequestAction($instance, $ident, $value);
|
||||
?>';
|
||||
|
||||
IPS_SetScriptContent($scriptID, $php);
|
||||
|
||||
return $scriptID;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------
|
||||
* BOOLEAN VARIABLE
|
||||
* VARIABLEN – DYNAMISCH
|
||||
* ---------------------------------------------------------*/
|
||||
private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int
|
||||
{
|
||||
@@ -241,11 +254,12 @@ private function EnsureBooleanVariable(string $deviceID, string $ident, string $
|
||||
|
||||
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
|
||||
$o = IPS_GetObject($cid);
|
||||
|
||||
if ($o['ObjectIdent'] === $ident) {
|
||||
|
||||
// OUTPUT → direkte Modulinstanz als Action
|
||||
if (str_contains($ident, '_output_')) {
|
||||
IPS_SetVariableCustomAction($cid, $this->InstanceID);
|
||||
$actionScript = $this->EnsureActionScript();
|
||||
IPS_SetVariableCustomAction($cid, $actionScript);
|
||||
|
||||
$var = IPS_GetVariable($cid);
|
||||
if ($var['VariableProfile'] === '' && $var['VariableCustomProfile'] === '') {
|
||||
@@ -257,24 +271,21 @@ private function EnsureBooleanVariable(string $deviceID, string $ident, string $
|
||||
}
|
||||
}
|
||||
|
||||
// neu
|
||||
// Neu erzeugen
|
||||
$varID = IPS_CreateVariable(0);
|
||||
IPS_SetIdent($varID, $ident);
|
||||
IPS_SetName($varID, $name);
|
||||
IPS_SetParent($varID, $folderID);
|
||||
|
||||
if (str_contains($ident, '_output_')) {
|
||||
IPS_SetVariableCustomAction($varID, $this->InstanceID);
|
||||
$actionScript = $this->EnsureActionScript();
|
||||
IPS_SetVariableCustomAction($varID, $actionScript);
|
||||
IPS_SetVariableCustomProfile($varID, '~Switch');
|
||||
}
|
||||
|
||||
return $varID;
|
||||
}
|
||||
|
||||
|
||||
/* ---------------------------------------------------------
|
||||
* FLOAT VARIABLE
|
||||
* ---------------------------------------------------------*/
|
||||
private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int
|
||||
{
|
||||
$folderID = $this->GetDeviceFolder($deviceID);
|
||||
@@ -289,9 +300,6 @@ private function EnsureBooleanVariable(string $deviceID, string $ident, string $
|
||||
return $id;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------
|
||||
* STRING VARIABLE
|
||||
* ---------------------------------------------------------*/
|
||||
private function EnsureStringVariable(string $deviceID, string $ident, string $name): int
|
||||
{
|
||||
$folderID = $this->GetDeviceFolder($deviceID);
|
||||
|
||||
Reference in New Issue
Block a user