no message
This commit is contained in:
+108
-101
@@ -70,49 +70,49 @@ class Shelly_Parser_MQTT extends IPSModule
|
|||||||
|
|
||||||
/* ---------------------------------------------------------
|
/* ---------------------------------------------------------
|
||||||
* RequestAction → Schalten von Outputs
|
* RequestAction → Schalten von Outputs
|
||||||
* ---------------------------------------------------------*/
|
* ---------------------------------------------------------*/
|
||||||
public function RequestAction($Ident, $Value)
|
public function RequestAction($Ident, $Value)
|
||||||
{
|
{
|
||||||
IPS_LogMessage("ShellyDebug", "RequestAction($Ident → $Value)");
|
IPS_LogMessage('ShellyDebug', "RequestAction($Ident → " . var_export($Value, true) . ')');
|
||||||
|
|
||||||
// Prüfen ob es eine Output-Variable ist
|
// Prüfen ob es eine Output-Variable ist
|
||||||
if (str_contains($Ident, '_output_')) {
|
if (str_contains($Ident, '_output_')) {
|
||||||
|
|
||||||
// lokale Variable setzen
|
// lokale Variable setzen
|
||||||
$varID = $this->FindVariableByIdent($Ident);
|
$varID = $this->FindVariableByIdent($Ident);
|
||||||
if ($varID) {
|
if ($varID) {
|
||||||
SetValue($varID, $Value);
|
SetValue($varID, $Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// device + index extrahieren
|
||||||
|
[$deviceID, $suffix] = explode('_output_', $Ident, 2);
|
||||||
|
$index = intval($suffix);
|
||||||
|
|
||||||
|
// Shelly-RPC Topic
|
||||||
|
$topic = $deviceID . '/rpc';
|
||||||
|
|
||||||
|
// Shelly-RPC-kompatibles JSON
|
||||||
|
$payloadArr = [
|
||||||
|
'id' => 1,
|
||||||
|
'src' => 'ips',
|
||||||
|
'method' => 'Switch.Set',
|
||||||
|
'params' => [
|
||||||
|
'id' => $index,
|
||||||
|
'on' => (bool) $Value
|
||||||
|
]
|
||||||
|
];
|
||||||
|
$payload = json_encode($payloadArr);
|
||||||
|
|
||||||
|
IPS_LogMessage('ShellyDebug', "SEND → $topic : $payload");
|
||||||
|
|
||||||
|
// absenden
|
||||||
|
$this->Publish($topic, $payload);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// device + index extrahieren
|
throw new Exception('Unknown Ident: ' . $Ident);
|
||||||
list($deviceID, $suffix) = explode('_output_', $Ident);
|
|
||||||
$index = intval($suffix);
|
|
||||||
|
|
||||||
// Shelly-RPC Topic (wie im IPS-Shelly Modul)
|
|
||||||
$topic = $deviceID . '/rpc';
|
|
||||||
|
|
||||||
// Shelly-RPC-kompatibles JSON
|
|
||||||
$payload = json_encode([
|
|
||||||
"id" => 1,
|
|
||||||
"src" => "ips",
|
|
||||||
"method" => "Switch.Set",
|
|
||||||
"params" => [
|
|
||||||
"id" => $index,
|
|
||||||
"on" => (bool)$Value
|
|
||||||
]
|
|
||||||
]);
|
|
||||||
|
|
||||||
IPS_LogMessage("ShellyDebug", "SEND → $topic : $payload");
|
|
||||||
|
|
||||||
// absenden
|
|
||||||
$this->Publish($topic, $payload);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Exception("Unknown Ident: " . $Ident);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* ---------------------------------------------------------
|
/* ---------------------------------------------------------
|
||||||
* RECEIVE MQTT DATA
|
* RECEIVE MQTT DATA
|
||||||
* ---------------------------------------------------------*/
|
* ---------------------------------------------------------*/
|
||||||
@@ -134,7 +134,7 @@ public function RequestAction($Ident, $Value)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$parts = explode('/', $topic);
|
$parts = explode('/', $topic);
|
||||||
$deviceID = $parts[0] ?? '';
|
$deviceID = $parts[0] ?? '';
|
||||||
|
|
||||||
if ($deviceID === '') {
|
if ($deviceID === '') {
|
||||||
@@ -158,18 +158,18 @@ public function RequestAction($Ident, $Value)
|
|||||||
/* ---------------------------------------------------------
|
/* ---------------------------------------------------------
|
||||||
* ONLINE-STATUS
|
* ONLINE-STATUS
|
||||||
* ---------------------------------------------------------*/
|
* ---------------------------------------------------------*/
|
||||||
private function HandleOnline(string $deviceID, string $payload): void
|
private function HandleOnline(string $deviceID, string $payload): void
|
||||||
{
|
{
|
||||||
$value = ($payload === 'true' || $payload === '1');
|
$value = ($payload === 'true' || $payload === '1');
|
||||||
|
|
||||||
$varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_online', 'Online');
|
$varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_online', 'Online');
|
||||||
SetValue($varID, $value);
|
SetValue($varID, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------
|
/* ---------------------------------------------------------
|
||||||
* RPC-EVENTS
|
* RPC-EVENTS
|
||||||
* ---------------------------------------------------------*/
|
* ---------------------------------------------------------*/
|
||||||
private function HandleRPC(string $deviceID, string $payload): void
|
private function HandleRPC(string $deviceID, string $payload): void
|
||||||
{
|
{
|
||||||
$json = json_decode($payload, true);
|
$json = json_decode($payload, true);
|
||||||
if (!is_array($json)) {
|
if (!is_array($json)) {
|
||||||
@@ -182,7 +182,7 @@ public function RequestAction($Ident, $Value)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$type = ShellyParser::ExtractType($src);
|
$type = ShellyParser::ExtractType($src);
|
||||||
$typeID = $this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ');
|
$typeID = $this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ');
|
||||||
SetValue($typeID, $type);
|
SetValue($typeID, $type);
|
||||||
|
|
||||||
@@ -193,69 +193,78 @@ public function RequestAction($Ident, $Value)
|
|||||||
// -----------------------------------
|
// -----------------------------------
|
||||||
// DYNAMISCHE OUTPUTS
|
// DYNAMISCHE OUTPUTS
|
||||||
// -----------------------------------
|
// -----------------------------------
|
||||||
foreach ($mapped['outputs'] as $index => $value) {
|
if (isset($mapped['outputs']) && is_array($mapped['outputs'])) {
|
||||||
|
foreach ($mapped['outputs'] as $index => $value) {
|
||||||
|
$ident = $deviceID . '_output_' . $index;
|
||||||
|
$name = 'Output ' . $index;
|
||||||
|
|
||||||
$ident = $deviceID . "_output_" . $index;
|
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
|
||||||
$name = "Output $index";
|
SetValue($varID, $value);
|
||||||
|
}
|
||||||
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
|
}
|
||||||
|
|
||||||
// damit RequestAction überhaupt ausgeführt wird
|
|
||||||
IPS_SetVariableCustomAction($varID, $this->InstanceID);
|
|
||||||
|
|
||||||
SetValue($varID, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
// -----------------------------------
|
// -----------------------------------
|
||||||
// DYNAMISCHE INPUTS
|
// DYNAMISCHE INPUTS
|
||||||
// -----------------------------------
|
// -----------------------------------
|
||||||
foreach ($mapped['inputs'] as $index => $value) {
|
if (isset($mapped['inputs']) && is_array($mapped['inputs'])) {
|
||||||
|
foreach ($mapped['inputs'] as $index => $value) {
|
||||||
$ident = $deviceID . "_input_" . $index;
|
$ident = $deviceID . '_input_' . $index;
|
||||||
$name = "Input $index";
|
$name = 'Input ' . $index;
|
||||||
|
|
||||||
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
|
|
||||||
|
|
||||||
SetValue($varID, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
|
||||||
|
SetValue($varID, $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// -----------------------------------
|
// -----------------------------------
|
||||||
// TEMPERATUR
|
// TEMPERATUR
|
||||||
// -----------------------------------
|
// -----------------------------------
|
||||||
if ($mapped['temperature'] !== null) {
|
if (array_key_exists('temperature', $mapped) && $mapped['temperature'] !== null) {
|
||||||
$tempID = $this->EnsureFloatVariable($deviceID, $deviceID . '_temperature', 'Temperatur');
|
$tempID = $this->EnsureFloatVariable($deviceID, $deviceID . '_temperature', 'Temperatur');
|
||||||
SetValue($tempID, $mapped['temperature']);
|
SetValue($tempID, $mapped['temperature']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* ---------------------------------------------------------
|
/* ---------------------------------------------------------
|
||||||
* Helper für Variablen
|
* Helper für Variablen
|
||||||
* ---------------------------------------------------------*/
|
* ---------------------------------------------------------*/
|
||||||
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);
|
||||||
|
|
||||||
// vorhandene Variable finden
|
// vorhandene Variable finden
|
||||||
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
|
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
|
||||||
$obj = IPS_GetObject($cid);
|
$obj = IPS_GetObject($cid);
|
||||||
if ($obj['ObjectIdent'] === $ident) {
|
if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) {
|
||||||
return $cid;
|
|
||||||
|
// *** geändert: für Outputs immer CustomAction + Profil setzen ***
|
||||||
|
if (str_contains($ident, '_output_')) {
|
||||||
|
IPS_SetVariableCustomAction($cid, $this->InstanceID);
|
||||||
|
$varInfo = IPS_GetVariable($cid);
|
||||||
|
if ($varInfo['VariableProfile'] === '' && $varInfo['VariableCustomProfile'] === '') {
|
||||||
|
IPS_SetVariableCustomProfile($cid, '~Switch');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $cid;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// neue Variable erzeugen
|
||||||
|
$varID = IPS_CreateVariable(0); // Boolean
|
||||||
|
IPS_SetIdent($varID, $ident);
|
||||||
|
IPS_SetName($varID, $name);
|
||||||
|
IPS_SetParent($varID, $folderID);
|
||||||
|
|
||||||
|
// *** geändert: für Outputs CustomAction + Profil setzen ***
|
||||||
|
if (str_contains($ident, '_output_')) {
|
||||||
|
IPS_SetVariableCustomAction($varID, $this->InstanceID);
|
||||||
|
IPS_SetVariableCustomProfile($varID, '~Switch');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $varID;
|
||||||
}
|
}
|
||||||
|
|
||||||
// neue Variable erzeugen
|
|
||||||
$varID = IPS_CreateVariable(0); // Boolean
|
|
||||||
IPS_SetIdent($varID, $ident);
|
|
||||||
IPS_SetName($varID, $name);
|
|
||||||
IPS_SetParent($varID, $folderID);
|
|
||||||
|
|
||||||
return $varID;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int
|
private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int
|
||||||
{
|
{
|
||||||
$folderID = $this->GetDeviceFolder($deviceID);
|
$folderID = $this->GetDeviceFolder($deviceID);
|
||||||
@@ -312,7 +321,7 @@ private function EnsureBooleanVariable(string $deviceID, string $ident, string $
|
|||||||
return $folderID;
|
return $folderID;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function FindDeviceFolder(string $Ident): int
|
private function FindDeviceFolder(string $Ident): int
|
||||||
{
|
{
|
||||||
foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) {
|
foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) {
|
||||||
foreach (IPS_GetChildrenIDs($folder) as $cid) {
|
foreach (IPS_GetChildrenIDs($folder) as $cid) {
|
||||||
@@ -324,18 +333,16 @@ private function EnsureBooleanVariable(string $deviceID, string $ident, string $
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function FindVariableByIdent(string $Ident)
|
private function FindVariableByIdent(string $Ident)
|
||||||
{
|
{
|
||||||
foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) {
|
foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) {
|
||||||
foreach (IPS_GetChildrenIDs($folder) as $cid) {
|
foreach (IPS_GetChildrenIDs($folder) as $cid) {
|
||||||
if (IPS_GetObject($cid)['ObjectIdent'] === $Ident) {
|
if (IPS_GetObject($cid)['ObjectIdent'] === $Ident) {
|
||||||
return $cid;
|
return $cid;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|||||||
Reference in New Issue
Block a user