From 874cdb7bf11c8730d7248d33928a28d30da349f0 Mon Sep 17 00:00:00 2001 From: Marko Oldenburg Date: Wed, 24 May 2017 22:59:17 +0200 Subject: [PATCH] voller Funktionsumfang der Module abgeschlossen --- 73_AMADCommBridge.pm | 641 +++++++++++++++++++++---------------------- 74_AMADDevice.pm | 456 +++++++++++------------------- 2 files changed, 468 insertions(+), 629 deletions(-) diff --git a/73_AMADCommBridge.pm b/73_AMADCommBridge.pm index 49f0934..0d03b07 100644 --- a/73_AMADCommBridge.pm +++ b/73_AMADCommBridge.pm @@ -40,7 +40,13 @@ # ###### Möglicher Aufbau eines JSON Strings für die AMADCommBridge # -# {"amad": {"AMADDEVICE": "nexus7-Wohnzimmer","FHEMCMD": "setreading"},"payload": {"reading0": "value0","reading1": "value1","readingX": "valueX"}} +# first initial String +# {"amad": {"amad_id": "37836534","fhemcmd": "setreading"},"payload": {"fhemdevice": "TabletWohnzimmer","fhemserverip": "192.168.1.25","amaddevice_ip": "192.168.1.123"}} +# +# default String +# {"amad": {"amad_id": "37836534","fhemcmd": "setreading"},"payload": {"reading0": "value0","reading1": "value1","readingX": "valueX"}} +# Aufruf zum testens +# curl --data '{"amad": {"amad_id": "37836534","fhemcmd": "setreading"},"payload": {"reading0": "value0","reading1": "value1","readingX": "valueX"}}' localhost:8090 # # ## @@ -61,7 +67,7 @@ use TcpServerUtils; eval "use JSON;1" or $missingModul .= "JSON "; -my $modulversion = "3alpha9"; +my $modulversion = "3alpha27"; my $flowsetversion = "2.6.12"; @@ -72,16 +78,14 @@ sub AMADCommBridge_Attr(@); sub AMADCommBridge_Open($); sub AMADCommBridge_Read($); sub AMADCommBridge_Define($$); -sub AMADCommBridge_Header2Hash($); -sub AMADCommBridge_HTTP_POST($$); -sub AMADCommBridge_HTTP_POSTerrorHandling($$$); +sub AMADCommBridge_WriteRequest($$$); sub AMADCommBridge_Initialize($); sub AMADCommBridge_Set($@); -sub AMADCommBridge_statusRequest($); -sub AMADCommBridge_statusRequestErrorHandling($$$); +sub AMADCommBridge_Write($@); sub AMADCommBridge_Undef($$); sub AMADCommBridge_ResponseProcessing($$); sub AMADCommBridge_Close($); +sub AMADCommBridge_ErrorHandling($$$); @@ -124,25 +128,20 @@ sub AMADCommBridge_Define($$) { my @a = split( "[ \t][ \t]*", $def ); - return "too few parameters: define AMADCommBridge'" if( @a != 2); + return "too few parameters: define AMADCommBridge ''" if( @a < 2) and ( @a > 3); return "Cannot define a HEOS device. Perl modul $missingModul is missing." if ( $missingModul ); my $name = $a[0]; - my $host = $a[2]; my $port; - $port = $a[3] if($a[3]); + $port = $a[2] if($a[2]); $port = 8090 if( not defined($port) and (!$port) ); $hash->{BRIDGE} = 1; $hash->{PORT} = $port; $hash->{VERSIONMODUL} = $modulversion; $hash->{VERSIONFLOWSET} = $flowsetversion; - - ### Muss in das logische Modul eingepflegt werden -# $hash->{helper}{infoErrorCounter} = 0 if( $hash->{HOST} ); -# $hash->{helper}{setCmdErrorCounter} = 0 if( $hash->{HOST} ); -# $hash->{helper}{deviceStateErrorCounter} = 0 if( $hash->{HOST} ); + $attr{$name}{room} = "AMAD" if( !defined( $attr{$name}{room} ) ); @@ -221,179 +220,6 @@ sub AMADCommBridge_Attr(@) { return undef; } -sub AMADCommBridge_statusRequest($) { - - my ($hash) = @_; - my $name = $hash->{NAME}; - my $fhemip = $hash->{HOST}; - my $port = $hash->{PORT}; - my $host; - my $activetask = AttrVal( $name, "checkActiveTask", "none" ); - my $userFlowState = AttrVal( $name, "setUserFlowState", "none" ); - my $apssid = "none"; - $apssid = $hash->{APSSID} if( defined($hash->{APSSID}) ); - $apssid = $attr{$name}{setAPSSID} if( defined($attr{$name}{setAPSSID}) ); - - - my $url = "http://" . $host . ":" . $port . "/fhem-amad/deviceInfo/"; # Pfad muß so im Automagic als http request Trigger drin stehen - - HttpUtils_NonblockingGet( - { - url => $url, - timeout => 5, - hash => $hash, - method => "GET", - header => "Connection: close\r\nfhemip: $fhemip\r\nfhemdevice: $name\r\nactivetask: $activetask\r\napssid: $apssid\r\nbport: $port\r\nuserflowstate: $userFlowState", - doTrigger => 1, - callback => \&AMADCommBridge_statusRequestErrorHandling, - } - ); - - Log3 $name, 5, "AMADCommBridge ($name) - Send statusRequest with URL: \"$url\" and Header: \"fhemIP: $fhemip\r\nfhemDevice: $name\r\nactiveTask: $activetask\r\napSSID: $apssid\""; -} - -sub AMADCommBridge_statusRequestErrorHandling($$$) { - - my ( $param, $err, $data ) = @_; - my $hash = $param->{hash}; - my $doTrigger = $param->{doTrigger}; - my $name = $hash->{NAME}; - my $host = $hash->{HOST}; - - - ### Begin Error Handling - if( $hash->{helper}{infoErrorCounter} > 0 ) { - - readingsBeginUpdate( $hash ); - readingsBulkUpdate( $hash, "lastStatusRequestState", "statusRequest_error" ); - - if( ReadingsVal( $name, "flow_Informations", "active" ) eq "inactive" && ReadingsVal( $name, "flow_SetCommands", "active" ) eq "inactive" ) { - - Log3 $name, 5, "AMADCommBridge ($name) - statusRequestERROR: CHECK THE LAST ERROR READINGS FOR MORE INFO, DEVICE IS SET OFFLINE"; - - readingsBulkUpdate( $hash, "deviceState", "offline" ); - readingsBulkUpdate ( $hash, "state", "AMAD Flows inactive, device set offline"); - } - - elsif( $hash->{helper}{infoErrorCounter} > 7 && $hash->{helper}{setCmdErrorCounter} > 4 ) { - - Log3 $name, 5, "AMADCommBridge ($name) - statusRequestERROR: UNKNOWN ERROR, PLEASE CONTACT THE DEVELOPER, DEVICE DISABLED"; - - $attr{$name}{disable} = 1; - readingsBulkUpdate ( $hash, "state", "Unknown Error, device disabled"); - - $hash->{helper}{infoErrorCounter} = 0; - $hash->{helper}{setCmdErrorCounter} = 0; - - return; - } - - elsif( ReadingsVal( $name, "flow_Informations", "active" ) eq "inactive" ) { - - Log3 $name, 5, "AMADCommBridge ($name) - statusRequestERROR: Informations Flow on your Device is inactive, will try to reactivate"; - } - - elsif( $hash->{helper}{infoErrorCounter} > 7 ) { - - Log3 $name, 5, "AMADCommBridge ($name) - statusRequestERROR: To many Errors please check your Network or Device Configuration, DEVICE IS SET OFFLINE"; - - readingsBulkUpdate( $hash, "deviceState", "offline" ); - readingsBulkUpdate ( $hash, "state", "To many Errors, device set offline"); - $hash->{helper}{infoErrorCounter} = 0; - } - - elsif($hash->{helper}{infoErrorCounter} > 2 && ReadingsVal( $name, "flow_Informations", "active" ) eq "active" ){ - - Log3 $name, 5, "AMADCommBridge ($name) - statusRequestERROR: Please check the AutomagicAPP on your Device"; - } - - readingsEndUpdate( $hash, 1 ); - } - - if( defined( $err ) ) { - if( $err ne "" ) { - - readingsBeginUpdate( $hash ); - readingsBulkUpdate ( $hash, "state", "$err") if( ReadingsVal( $name, "state", 1 ) ne "initialized" ); - $hash->{helper}{infoErrorCounter} = ( $hash->{helper}{infoErrorCounter} + 1 ); - - readingsBulkUpdate( $hash, "lastStatusRequestState", "statusRequest_error" ); - - if( $err =~ /timed out/ ) { - - Log3 $name, 5, "AMADCommBridge ($name) - statusRequestERROR: connect to your device is timed out. check network"; - } - - elsif( ( $err =~ /Keine Route zum Zielrechner/ ) && $hash->{helper}{infoErrorCounter} > 1 ) { - - Log3 $name, 5, "AMADCommBridge ($name) - statusRequestERROR: no route to target. bad network configuration or network is down"; - - } else { - - Log3 $name, 5, "AMADCommBridge ($name) - statusRequestERROR: $err"; - } - - readingsEndUpdate( $hash, 1 ); - - Log3 $name, 5, "AMADCommBridge ($name) - statusRequestERROR: AMADCommBridge_statusRequestErrorHandling: error while requesting AutomagicInfo: $err"; - - return; - } - } - - if( $data eq "" and exists( $param->{code} ) && $param->{code} ne 200 ) { - - readingsBeginUpdate( $hash ); - readingsBulkUpdate ( $hash, "state", $param->{code} ) if( ReadingsVal( $name, "state", 1 ) ne "initialized" ); - $hash->{helper}{infoErrorCounter} = ( $hash->{helper}{infoErrorCounter} + 1 ); - - readingsBulkUpdate( $hash, "lastStatusRequestState", "statusRequest_error" ); - - if( $param->{code} ne 200 ) { - - Log3 $name, 5, "AMADCommBridge ($name) - statusRequestERROR: ".$param->{code}; - } - - readingsEndUpdate( $hash, 1 ); - - Log3 $name, 5, "AMADCommBridge ($name) - statusRequestERROR: received http code ".$param->{code}." without any data after requesting AMAD AutomagicInfo"; - - return; - } - - if( ( $data =~ /Error/i ) and exists( $param->{code} ) ) { - readingsBeginUpdate( $hash ); - readingsBulkUpdate( $hash, "state", $param->{code} ) if( ReadingsVal( $name, "state" ,0) ne "initialized" ); - $hash->{helper}{infoErrorCounter} = ( $hash->{helper}{infoErrorCounter} + 1 ); - - readingsBulkUpdate( $hash, "lastStatusRequestState", "statusRequest_error" ); - - if( $param->{code} eq 404 && ReadingsVal( $name, "flow_Informations", "inactive" ) eq "inactive" ) { - - Log3 $name, 5, "AMADCommBridge ($name) - statusRequestERROR: check the informations flow on your device"; - } - - elsif( $param->{code} eq 404 && ReadingsVal( $name, "flow_Informations", "active" ) eq "active" ) { - - Log3 $name, 5, "AMADCommBridge ($name) - statusRequestERROR: check the automagicApp on your device"; - - } else { - - Log3 $name, 5, "AMADCommBridge ($name) - statusRequestERROR: http error ".$param->{code}; - } - - readingsEndUpdate( $hash, 1 ); - - Log3 $name, 5, "AMADCommBridge ($name) - statusRequestERROR: received http code ".$param->{code}." receive Error after requesting AMAD AutomagicInfo"; - - return; - } - - ### End Error Handling - - $hash->{helper}{infoErrorCounter} = 0; -} - sub AMADCommBridge_Set($@) { my ($hash, $name, $cmd, @args) = @_; @@ -418,163 +244,304 @@ sub AMADCommBridge_Set($@) { } } -sub AMADCommBridge_HTTP_POST($$) { +sub AMADCommBridge_Write($@) { + + my ($hash,$amad_id,$uri,$method) = @_; + my $header = "Connection: close\r\n"; + $header .= shift; + my $name = $hash->{NAME}; + - my ( $hash, $url ) = @_; - my $name = $hash->{NAME}; - - my $state = ReadingsVal( $name, "state", 0 ); - - readingsSingleUpdate( $hash, "state", "Send HTTP POST", 1 ); - HttpUtils_NonblockingGet( { - url => $url, - timeout => 15, - hash => $hash, - method => "POST", - header => "Connection: close", - doTrigger => 1, - callback => \&AMADCommBridge_HTTP_POSTerrorHandling, + url => "http://" . $uri, + timeout => 15, + hash => $hash, + amad_id => $amad_id, + method => $method, + header => $header, + doTrigger => 1, + callback => \&AMADCommBridge_ErrorHandling, } ); - Log3 $name, 4, "AMADCommBridge ($name) - Send HTTP POST with URL $url"; - - readingsSingleUpdate( $hash, "state", $state, 1 ); - - return undef; + Log3 $name, 5, "AMADCommBridge ($name) - Send with URI: $uri, HEADER: $header, METHOD: $method"; } -sub AMADCommBridge_HTTP_POSTerrorHandling($$$) { +sub AMADCommBridge_ErrorHandling($$$) { - my ( $param, $err, $data ) = @_; - my $hash = $param->{hash}; - my $name = $hash->{NAME}; + my ($param,$err,$data) = @_; + my $hash = $param->{hash}; + #my $name = $hash->{NAME}; + my $dhash = $modules{AMADDevice}{defptr}{$param->{'amad_id'}}; + my $dname = $dhash->{NAME}; + - ### Begin Error Handling - if( $hash->{helper}{setCmdErrorCounter} > 2 ) { - readingsBeginUpdate( $hash ); - readingsBulkUpdate( $hash, "lastSetCommandState", "statusRequest_error" ); - - if( ReadingsVal( $name, "flow_Informations", "active" ) eq "inactive" && ReadingsVal( $name, "flow_SetCommands", "active" ) eq "inactive" ) { - Log3 $name, 5, "AMADCommBridge ($name) - setCommandERROR: CHECK THE LAST ERROR READINGS FOR MORE INFO, DEVICE IS SET OFFLINE"; - - readingsBulkUpdate( $hash, "deviceState", "offline" ); - readingsBulkUpdate( $hash, "state", "AMAD Flows inactive, device set offline" ); - } - - elsif( $hash->{helper}{infoErrorCounter} > 7 && $hash->{helper}{setCmdErrorCounter} > 4 ) { + if( $param->{method} eq 'GET' ) { - Log3 $name, 5, "AMADCommBridge ($name) - setCommandERROR: UNKNOWN ERROR, PLEASE CONTACT THE DEVELOPER, DEVICE DISABLED"; - - $attr{$name}{disable} = 1; - readingsBulkUpdate( $hash, "state", "Unknown Error, device disabled" ); - $hash->{helper}{infoErrorCounter} = 0; - $hash->{helper}{setCmdErrorCounter} = 0; - - return; - } - - elsif( ReadingsVal( $name, "flow_SetCommands", "active" ) eq "inactive" ) { - - Log3 $name, 5, "AMADCommBridge ($name) - setCommandERROR: Flow SetCommands on your Device is inactive, will try to reactivate"; - } - - elsif( $hash->{helper}{setCmdErrorCounter} > 9 ) { - - Log3 $name, 5, "AMADCommBridge ($name) - setCommandERROR: To many Errors please check your Network or Device Configuration, DEVICE IS SET OFFLINE"; - - readingsBulkUpdate( $hash, "deviceState", "offline" ); - readingsBulkUpdate( $hash, "state", "To many Errors, device set offline" ); - $hash->{helper}{setCmdErrorCounter} = 0; - } - - elsif( $hash->{helper}{setCmdErrorCounter} > 4 && ReadingsVal( $name, "flow_SetCommands", "active" ) eq "active" ){ - - Log3 $name, 5, "AMADCommBridge ($name) - setCommandERROR: Please check the AutomagicAPP on your Device"; - } - - readingsEndUpdate( $hash, 1 ); - } - - if( defined( $err ) ) { - if( $err ne "" ) { - readingsBeginUpdate( $hash ); - readingsBulkUpdate( $hash, "state", $err ) if( ReadingsVal( $name, "state", 0 ) ne "initialized" ); - $hash->{helper}{setCmdErrorCounter} = ($hash->{helper}{setCmdErrorCounter} + 1); - - readingsBulkUpdate( $hash, "lastSetCommandState", "setCmd_error" ); - - if( $err =~ /timed out/ ) { - - Log3 $name, 5, "AMADCommBridge ($name) - setCommandERROR: connect to your device is timed out. check network"; - } - - elsif( $err =~ /Keine Route zum Zielrechner/ ) { - - Log3 $name, 5, "AMADCommBridge ($name) - setCommandERROR: no route to target. bad network configuration or network is down"; - - } else { - - Log3 $name, 5, "AMADCommBridge ($name) - setCommandERROR: $err"; - } - - readingsEndUpdate( $hash, 1 ); - - Log3 $name, 5, "AMADCommBridge ($name) - setCommandERROR: error while POST Command: $err"; - - return; - } - } - - if( $data eq "" and exists( $param->{code} ) && $param->{code} ne 200 ) { - - readingsBeginUpdate( $hash ); - readingsBulkUpdate( $hash, "state", $param->{code} ) if( ReadingsVal( $hash, "state", 0 ) ne "initialized" ); - - $hash->{helper}{setCmdErrorCounter} = ( $hash->{helper}{setCmdErrorCounter} + 1 ); - - readingsBulkUpdate($hash, "lastSetCommandState", "setCmd_error" ); - - readingsEndUpdate( $hash, 1 ); - - Log3 $name, 5, "AMADCommBridge ($name) - setCommandERROR: received http code ".$param->{code}; - - return; - } + ### Begin Error Handling + if( $dhash->{helper}{infoErrorCounter} > 0 ) { - if( ( $data =~ /Error/i ) and exists( $param->{code} ) ) { - - readingsBeginUpdate( $hash ); - readingsBulkUpdate( $hash, "state", $param->{code} ) if( ReadingsVal( $name, "state", 0 ) ne "initialized" ); + readingsBeginUpdate( $dhash ); + readingsBulkUpdate( $dhash, "lastStatusRequestState", "statusRequest_error" ); - $hash->{helper}{setCmdErrorCounter} = ( $hash->{helper}{setCmdErrorCounter} + 1 ); + if( ReadingsVal( $dname, "flow_Informations", "active" ) eq "inactive" && ReadingsVal( $dname, "flow_SetCommands", "active" ) eq "inactive" ) { + + Log3 $dname, 5, "AMADCommBridge ($dname) - statusRequestERROR: CHECK THE LAST ERROR READINGS FOR MORE INFO, DEVICE IS SET OFFLINE"; + + readingsBulkUpdate( $dhash, "deviceState", "offline" ); + readingsBulkUpdate ( $dhash, "state", "AMAD Flows inactive, device set offline"); + } - readingsBulkUpdate( $hash, "lastSetCommandState", "setCmd_error" ); - - if( $param->{code} eq 404 ) { - - readingsBulkUpdate( $hash, "lastSetCommandError", "" ); - Log3 $name, 5, "AMADCommBridge ($name) - setCommandERROR: setCommands flow is inactive on your device!"; - - } else { - - Log3 $name, 5, "AMADCommBridge ($name) - setCommandERROR: http error ".$param->{code}; + elsif( $dhash->{helper}{infoErrorCounter} > 7 && $dhash->{helper}{setCmdErrorCounter} > 4 ) { + + Log3 $dname, 5, "AMADCommBridge ($dname) - statusRequestERROR: UNKNOWN ERROR, PLEASE CONTACT THE DEVELOPER, DEVICE DISABLED"; + + $attr{$dname}{disable} = 1; + readingsBulkUpdate ( $dhash, "state", "Unknown Error, device disabled"); + + $dhash->{helper}{infoErrorCounter} = 0; + $dhash->{helper}{setCmdErrorCounter} = 0; + + return; + } + + elsif( ReadingsVal( $dname, "flow_Informations", "active" ) eq "inactive" ) { + + Log3 $dname, 5, "AMADCommBridge ($dname) - statusRequestERROR: Informations Flow on your Device is inactive, will try to reactivate"; + } + + elsif( $dhash->{helper}{infoErrorCounter} > 7 ) { + + Log3 $dname, 5, "AMADCommBridge ($dname) - statusRequestERROR: To many Errors please check your Network or Device Configuration, DEVICE IS SET OFFLINE"; + + readingsBulkUpdate( $dhash, "deviceState", "offline" ); + readingsBulkUpdate ( $dhash, "state", "To many Errors, device set offline"); + $dhash->{helper}{infoErrorCounter} = 0; + } + + elsif($dhash->{helper}{infoErrorCounter} > 2 && ReadingsVal( $dname, "flow_Informations", "active" ) eq "active" ){ + + Log3 $dname, 5, "AMADCommBridge ($dname) - statusRequestERROR: Please check the AutomagicAPP on your Device"; + } + + readingsEndUpdate( $dhash, 1 ); + } + + if( defined( $err ) ) { + if( $err ne "" ) { + + readingsBeginUpdate( $dhash ); + readingsBulkUpdate ( $dhash, "state", "$err") if( ReadingsVal( $dname, "state", 1 ) ne "initialized" ); + $dhash->{helper}{infoErrorCounter} = ( $dhash->{helper}{infoErrorCounter} + 1 ); + + readingsBulkUpdate( $dhash, "lastStatusRequestState", "statusRequest_error" ); + + if( $err =~ /timed out/ ) { + + Log3 $dname, 5, "AMADCommBridge ($dname) - statusRequestERROR: connect to your device is timed out. check network"; + } + + elsif( ( $err =~ /Keine Route zum Zielrechner/ ) && $dhash->{helper}{infoErrorCounter} > 1 ) { + + Log3 $dname, 5, "AMADCommBridge ($dname) - statusRequestERROR: no route to target. bad network configuration or network is down"; + + } else { + + Log3 $dname, 5, "AMADCommBridge ($dname) - statusRequestERROR: $err"; + } + + readingsEndUpdate( $dhash, 1 ); + + Log3 $dname, 5, "AMADCommBridge ($dname) - statusRequestERROR: AMADCommBridge_statusRequestErrorHandling: error while requesting AutomagicInfo: $err"; + + return; + } } - return; + if( $data eq "" and exists( $param->{code} ) && $param->{code} ne 200 ) { + + readingsBeginUpdate( $dhash ); + readingsBulkUpdate ( $dhash, "state", $param->{code} ) if( ReadingsVal( $dname, "state", 1 ) ne "initialized" ); + $dhash->{helper}{infoErrorCounter} = ( $dhash->{helper}{infoErrorCounter} + 1 ); + + readingsBulkUpdate( $dhash, "lastStatusRequestState", "statusRequest_error" ); + + if( $param->{code} ne 200 ) { + + Log3 $dname, 5, "AMADCommBridge ($dname) - statusRequestERROR: ".$param->{code}; + } + + readingsEndUpdate( $dhash, 1 ); + + Log3 $dname, 5, "AMADCommBridge ($dname) - statusRequestERROR: received http code ".$param->{code}." without any data after requesting AMAD AutomagicInfo"; + + return; + } + + if( ( $data =~ /Error/i ) and exists( $param->{code} ) ) { + readingsBeginUpdate( $dhash ); + readingsBulkUpdate( $dhash, "state", $param->{code} ) if( ReadingsVal( $dname, "state" ,0) ne "initialized" ); + $dhash->{helper}{infoErrorCounter} = ( $dhash->{helper}{infoErrorCounter} + 1 ); + + readingsBulkUpdate( $dhash, "lastStatusRequestState", "statusRequest_error" ); + + if( $param->{code} eq 404 && ReadingsVal( $dname, "flow_Informations", "inactive" ) eq "inactive" ) { + + Log3 $dname, 5, "AMADCommBridge ($dname) - statusRequestERROR: check the informations flow on your device"; + } + + elsif( $param->{code} eq 404 && ReadingsVal( $dname, "flow_Informations", "active" ) eq "active" ) { + + Log3 $dname, 5, "AMADCommBridge ($dname) - statusRequestERROR: check the automagicApp on your device"; + + } else { + + Log3 $dname, 5, "AMADCommBridge ($dname) - statusRequestERROR: http error ".$param->{code}; + } + + readingsEndUpdate( $dhash, 1 ); + + Log3 $dname, 5, "AMADCommBridge ($dname) - statusRequestERROR: received http code ".$param->{code}." receive Error after requesting AMAD AutomagicInfo"; + + return; + } + + ### End Error Handling + + $dhash->{helper}{infoErrorCounter} = 0; } - ### End Error Handling + elsif( $param->{method} eq 'POST' ) { + + ### Begin Error Handling + if( $dhash->{helper}{setCmdErrorCounter} > 2 ) { + + readingsBeginUpdate( $dhash ); + readingsBulkUpdate( $dhash, "lastSetCommandState", "statusRequest_error" ); + + if( ReadingsVal( $dname, "flow_Informations", "active" ) eq "inactive" && ReadingsVal( $dname, "flow_SetCommands", "active" ) eq "inactive" ) { + + Log3 $dname, 5, "AMADCommBridge ($dname) - setCommandERROR: CHECK THE LAST ERROR READINGS FOR MORE INFO, DEVICE IS SET OFFLINE"; + + readingsBulkUpdate( $dhash, "deviceState", "offline" ); + readingsBulkUpdate( $dhash, "state", "AMAD Flows inactive, device set offline" ); + } + + elsif( $dhash->{helper}{infoErrorCounter} > 7 && $dhash->{helper}{setCmdErrorCounter} > 4 ) { + + Log3 $dname, 5, "AMADCommBridge ($dname) - setCommandERROR: UNKNOWN ERROR, PLEASE CONTACT THE DEVELOPER, DEVICE DISABLED"; + + $attr{$dname}{disable} = 1; + readingsBulkUpdate( $dhash, "state", "Unknown Error, device disabled" ); + $dhash->{helper}{infoErrorCounter} = 0; + $dhash->{helper}{setCmdErrorCounter} = 0; + + return; + } + + elsif( ReadingsVal( $dname, "flow_SetCommands", "active" ) eq "inactive" ) { + + Log3 $dname, 5, "AMADCommBridge ($dname) - setCommandERROR: Flow SetCommands on your Device is inactive, will try to reactivate"; + } + + elsif( $dhash->{helper}{setCmdErrorCounter} > 9 ) { + + Log3 $dname, 5, "AMADCommBridge ($dname) - setCommandERROR: To many Errors please check your Network or Device Configuration, DEVICE IS SET OFFLINE"; + + readingsBulkUpdate( $dhash, "deviceState", "offline" ); + readingsBulkUpdate( $dhash, "state", "To many Errors, device set offline" ); + $dhash->{helper}{setCmdErrorCounter} = 0; + } + + elsif( $dhash->{helper}{setCmdErrorCounter} > 4 && ReadingsVal( $dname, "flow_SetCommands", "active" ) eq "active" ){ + + Log3 $dname, 5, "AMADCommBridge ($dname) - setCommandERROR: Please check the AutomagicAPP on your Device"; + } + + readingsEndUpdate( $dhash, 1 ); + } + + if( defined( $err ) ) { + if( $err ne "" ) { + readingsBeginUpdate( $dhash ); + readingsBulkUpdate( $dhash, "state", $err ) if( ReadingsVal( $dname, "state", 0 ) ne "initialized" ); + $dhash->{helper}{setCmdErrorCounter} = ($dhash->{helper}{setCmdErrorCounter} + 1); + + readingsBulkUpdate( $dhash, "lastSetCommandState", "setCmd_error" ); + + if( $err =~ /timed out/ ) { + + Log3 $dname, 5, "AMADCommBridge ($dname) - setCommandERROR: connect to your device is timed out. check network"; + } + + elsif( $err =~ /Keine Route zum Zielrechner/ ) { + + Log3 $dname, 5, "AMADCommBridge ($dname) - setCommandERROR: no route to target. bad network configuration or network is down"; + + } else { + + Log3 $dname, 5, "AMADCommBridge ($dname) - setCommandERROR: $err"; + } + + readingsEndUpdate( $dhash, 1 ); + + Log3 $dname, 5, "AMADCommBridge ($dname) - setCommandERROR: error while POST Command: $err"; + + return; + } + } + + if( $data eq "" and exists( $param->{code} ) && $param->{code} ne 200 ) { + + readingsBeginUpdate( $dhash ); + readingsBulkUpdate( $dhash, "state", $param->{code} ) if( ReadingsVal( $dhash, "state", 0 ) ne "initialized" ); + + $dhash->{helper}{setCmdErrorCounter} = ( $dhash->{helper}{setCmdErrorCounter} + 1 ); + + readingsBulkUpdate($dhash, "lastSetCommandState", "setCmd_error" ); + + readingsEndUpdate( $dhash, 1 ); + + Log3 $dname, 5, "AMADCommBridge ($dname) - setCommandERROR: received http code ".$param->{code}; + + return; + } + + if( ( $data =~ /Error/i ) and exists( $param->{code} ) ) { + + readingsBeginUpdate( $dhash ); + readingsBulkUpdate( $dhash, "state", $param->{code} ) if( ReadingsVal( $dname, "state", 0 ) ne "initialized" ); + + $dhash->{helper}{setCmdErrorCounter} = ( $dhash->{helper}{setCmdErrorCounter} + 1 ); + + readingsBulkUpdate( $dhash, "lastSetCommandState", "setCmd_error" ); + + if( $param->{code} eq 404 ) { + + readingsBulkUpdate( $dhash, "lastSetCommandError", "" ); + Log3 $dname, 5, "AMADCommBridge ($dname) - setCommandERROR: setCommands flow is inactive on your device!"; + + } else { + + Log3 $dname, 5, "AMADCommBridge ($dname) - setCommandERROR: http error ".$param->{code}; + } + + return; + } + + ### End Error Handling + + readingsSingleUpdate( $dhash, "lastSetCommandState", "setCmd_done", 1 ); + $dhash->{helper}{setCmdErrorCounter} = 0; + + return undef; + } - readingsSingleUpdate( $hash, "lastSetCommandState", "setCmd_done", 1 ); - $hash->{helper}{setCmdErrorCounter} = 0; - return undef; } sub AMADCommBridge_Open($) { @@ -673,8 +640,15 @@ sub AMADCommBridge_ResponseProcessing($$) { return; } - my $amadDevice = $decode_json->{amad}{AMADDEVICE}; - my $fhemcmd = $decode_json->{amad}{FHEMCMD}; + my $amad_id = $decode_json->{amad}{amad_id}; + my $fhemcmd = $decode_json->{amad}{fhemcmd}; + my $fhemDevice; + + if( defined($decode_json->{payload}{fhemdevice}) and ($decode_json->{payload}{fhemdevice}) ) { + $fhemDevice = $decode_json->{payload}{fhemdevice} if( defined($decode_json->{payload}{fhemdevice}) ); + } else { + $fhemDevice = $modules{AMADDevice}{defptr}{$amad_id}->{NAME}; + } @@ -710,7 +684,7 @@ sub AMADCommBridge_ResponseProcessing($$) { - elsif( !defined($amadDevice) ) { + elsif( !defined($amad_id) ) { readingsSingleUpdate( $bhash, "transmitterERROR", $hash->{NAME}." has no device name sends", 1 ) if( AttrVal( $bname, "expertMode", 0 ) eq "1" ); Log3 $bname, 4, "AMADCommBridge ($name) - ERROR - no device name given. please check your global variable in automagic"; @@ -728,16 +702,15 @@ sub AMADCommBridge_ResponseProcessing($$) { if( defined($fhemcmd) and ($fhemcmd) ) { - if ( $fhemcmd =~ /setreading\b/ ) { - my $tv = $data[1]; - return Log3 $bname, 3, "AMADCommBridge ($name) - AMADCommBridge: processing receive no reading values from Device: $amadDevice" - unless( defined($tv) and ($tv) ); + if ( $fhemcmd eq 'setreading' ) { + return Log3 $bname, 3, "AMADCommBridge ($name) - AMADCommBridge: processing receive no reading values from Device: $fhemDevice" + unless( defined($decode_json->{payload}) and ($decode_json->{payload}) ); - Log3 $bname, 4, "AMADCommBridge ($name) - AMADCommBridge: processing receive reading values - Device: $amadDevice Data: $tv"; + Log3 $bname, 4, "AMADCommBridge ($bname) - AMADCommBridge: processing receive reading values - Device: $fhemDevice Data: $decode_json->{payload}"; Dispatch($bhash,$json,undef); - Log3 $name, 4, "AMADCommBridge ($name) - call Dispatcher"; - readingsSingleUpdate($bhash,'fhemServerIP',$decode_json->{payload}{'DEVICE-IP'},1) if( defined($decode_json->{payload}{'DEVICE-IP'})); + Log3 $bname, 4, "AMADCommBridge ($bname) - call Dispatcher"; + readingsSingleUpdate($bhash,'fhemServerIP',$decode_json->{payload}{'fhemserverip'},1) if( defined($decode_json->{payload}{'fhemserverip'})); $response = "header lines: \r\n AMADCommBridge receive Data complete\r\n FHEM was processes\r\n"; $c = $hash->{CD}; @@ -773,9 +746,9 @@ sub AMADCommBridge_ResponseProcessing($$) { readingsBeginUpdate( $bhash); readingsBulkUpdate( $bhash, "receiveVoiceCommand", $fhemCmd ); - readingsBulkUpdate( $bhash, "receiveVoiceDevice", $amadDevice ); + readingsBulkUpdate( $bhash, "receiveVoiceDevice", $fhemDevice ); readingsEndUpdate( $bhash, 1 ); - Log3 $bname, 4, "AMADCommBridge ($name) - AMADCommBridge_CommBridge: set reading receive voice command: $fhemCmd from Device $amadDevice"; + Log3 $bname, 4, "AMADCommBridge ($name) - AMADCommBridge_CommBridge: set reading receive voice command: $fhemCmd from Device $fhemDevice"; $response = "header lines: \r\n AMADCommBridge receive Data complete\r\n FHEM was processes\r\n"; $c = $hash->{CD}; diff --git a/74_AMADDevice.pm b/74_AMADDevice.pm index 5da3e24..3797ef5 100644 --- a/74_AMADDevice.pm +++ b/74_AMADDevice.pm @@ -38,15 +38,7 @@ # } # # -###### Möglicher Aufbau eines JSON Strings für die AMADCommBridge -# -# {"amad": {"AMADDEVICE": "nexus7-Wohnzimmer","FHEMCMD": "setreading"},"payload": {"reading0": "value0","reading1": "value1","readingX": "valueX"}} -# -# -## -## -## -## + @@ -62,7 +54,7 @@ use Encode qw(encode); eval "use JSON;1" or $missingModul .= "JSON "; -my $modulversion = "3alpha9"; +my $modulversion = "3alpha27"; my $flowsetversion = "2.6.12"; @@ -144,19 +136,22 @@ sub AMADDevice_Define($$) { $i++; } - return "too few parameters: define AMADDevice " if( @a < 2 ); - return "Cannot define a HEOS device. Perl modul $missingModul is missing." if ( $missingModul ); + return "too few parameters: define AMADDevice " if( @a != 3 ); + return "Cannot define a AMAD device. Perl modul $missingModul is missing." if ( $missingModul ); - my ($name,$host) = @a; + my ($name,$host,$amad_id) = @a; $hash->{HOST} = $host; + $hash->{AMAD_ID} = $amad_id; $hash->{PORT} = 8090; $hash->{VERSIONMODUL} = $modulversion; $hash->{VERSIONFLOWSET} = $flowsetversion; - $hash->{helper}{infoErrorCounter} = 0 if( $hash->{HOST} ); - $hash->{helper}{setCmdErrorCounter} = 0 if( $hash->{HOST} ); - $hash->{helper}{deviceStateErrorCounter} = 0 if( $hash->{HOST} ); + $hash->{helper}{infoErrorCounter} = 0; + $hash->{helper}{setCmdErrorCounter} = 0; + $hash->{helper}{deviceStateErrorCounter} = 0; + + AssignIoPort($hash,$iodev) if( !$hash->{IODev} ); @@ -173,13 +168,12 @@ sub AMADDevice_Define($$) { $iodev = $hash->{IODev}->{NAME}; - my $code = $iodev ."-". $name if( defined($iodev) ); - my $d = $modules{AMADDevice}{defptr}{$code}; + my $d = $modules{AMADDevice}{defptr}{$amad_id}; return "AMADDevice device $name on AMADCommBridge $iodev already defined." if( defined($d) && $d->{IODev} == $hash->{IODev} && $d->{NAME} ne $name ); - Log3 $name, 3, "AMADDevice ($name) - defined with Code: $code on port $hash->{PORT}"; + Log3 $name, 3, "AMADDevice ($name) - defined with AMAD_ID: $amad_id on port $hash->{PORT}"; $attr{$name}{room} = "AMAD" if( !defined( $attr{$name}{room} ) ); @@ -198,7 +192,7 @@ sub AMADDevice_Define($$) { #InternalTimer( gettimeofday()+30, "AMADDevice_GetUpdate", $hash, 0 ) if( ($hash->{HOST}) ); } - $modules{AMADDevice}{defptr}{$code} = $hash; + $modules{AMADDevice}{defptr}{$amad_id} = $hash; return undef; } @@ -207,11 +201,11 @@ sub AMADDevice_Undef($$) { my ( $hash, $arg ) = @_; my $name = $hash->{NAME}; + my $amad_id = $hash->{AMAD_ID}; - my $code = $hash->{IODev}->{NAME} ."-". $name if( defined($hash->{IODev}->{NAME}) ); RemoveInternalTimer( $hash ); - delete $modules{AMADDevice}{defptr}{$code}; + delete $modules{AMADDevice}{defptr}{$amad_id}; return undef; } @@ -348,12 +342,10 @@ sub AMADDevice_WriteReadings($$) { my ( $hash, $decode_json ) = @_; my $name = $hash->{NAME}; - my $host = $hash->{HOST}; ############################ #### schreiben der Readings - readingsBeginUpdate($hash); Log3 $name, 5, "AMADDevice ($name) - Processing data: $decode_json"; readingsSingleUpdate( $hash, "state", "active", 1) if( ReadingsVal( $name, "state", 0 ) ne "initialized" or ReadingsVal( $name, "state", 0 ) ne "active" ); @@ -363,6 +355,8 @@ sub AMADDevice_WriteReadings($$) { my $v; + readingsBeginUpdate($hash); + while( ( $t, $v ) = each %{$decode_json->{payload}} ) { readingsBulkUpdate( $hash, $t, $v ) if( defined( $v ) ); $v =~ s/\bnull\b/off/g if( ($t eq "nextAlarmDay" || $t eq "nextAlarmTime") && $v eq "null" ); @@ -386,179 +380,55 @@ sub AMADDevice_WriteReadings($$) { } sub AMADDevice_Set($$@) { - - my ( $hash, $name, $cmd, @val ) = @_; - - my $bhash = $modules{AMADDevice}{defptr}{BRIDGE}; - my $bname = $bhash->{NAME}; - - if( $name ne "$bname" ) { - my $apps = AttrVal( $name, "setOpenApp", "none" ); - my $btdev = AttrVal( $name, "setBluetoothDevice", "none" ); - my $activetask = AttrVal( $name, "setActiveTask", "none" ); - - my $list = ""; - $list .= "screenMsg "; - $list .= "ttsMsg "; - $list .= "volume:slider,0,1,15 "; - $list .= "mediaGoogleMusic:play/pause,stop,next,back " if( ReadingsVal( $bname, "fhemServerIP", "none" ) ne "none"); - $list .= "mediaAmazonMusic:play/pause,stop,next,back " if( ReadingsVal( $bname, "fhemServerIP", "none" ) ne "none"); - $list .= "mediaSpotifyMusic:play/pause,stop,next,back " if( ReadingsVal( $bname, "fhemServerIP", "none" ) ne "none"); - $list .= "mediaTuneinRadio:play/pause,stop,next,back " if( ReadingsVal( $bname, "fhemServerIP", "none" ) ne "none"); - $list .= "mediaAldiMusic:play/pause,stop,next,back " if( ReadingsVal( $bname, "fhemServerIP", "none" ) ne "none"); - $list .= "mediaYouTube:play/pause,stop,next,back " if( ReadingsVal( $bname, "fhemServerIP", "none" ) ne "none"); - $list .= "mediaVlcPlayer:play/pause,stop,next,back " if( ReadingsVal( $bname, "fhemServerIP", "none" ) ne "none"); - $list .= "mediaAudible:play/pause,stop,next,back " if( ReadingsVal( $bname, "fhemServerIP", "none" ) ne "none"); - $list .= "screenBrightness:slider,0,1,255 "; - $list .= "screen:on,off,lock,unlock "; - $list .= "screenOrientation:auto,landscape,portrait " if( AttrVal( $name, "setScreenOrientation", "0" ) eq "1" ); - $list .= "screenFullscreen:on,off " if( AttrVal( $name, "setFullscreen", "0" ) eq "1" ); - $list .= "openURL "; - $list .= "openApp:$apps " if( AttrVal( $name, "setOpenApp", "none" ) ne "none" ); - $list .= "nextAlarmTime:time "; - $list .= "timer:slider,1,1,60 "; - $list .= "statusRequest:noArg "; - $list .= "system:reboot,shutdown,airplanemodeON " if( AttrVal( $name, "root", "0" ) eq "1" ); - $list .= "bluetooth:on,off "; - $list .= "notifySndFile "; - $list .= "clearNotificationBar:All,Automagic "; - $list .= "changetoBTDevice:$btdev " if( AttrVal( $name, "setBluetoothDevice", "none" ) ne "none" ); - $list .= "activateVoiceInput:noArg "; - $list .= "volumeNotification:slider,0,1,7 "; - $list .= "volumeRingSound:slider,0,1,7 "; - $list .= "vibrate:noArg "; - $list .= "sendIntent "; - $list .= "openCall "; - $list .= "closeCall:noArg "; - $list .= "currentFlowsetUpdate:noArg "; - $list .= "installFlowSource "; - $list .= "doNotDisturb:never,always,alarmClockOnly,onlyImportant "; - $list .= "userFlowState "; - $list .= "sendSMS "; - $list .= "startDaydream:noArg "; - if( lc $cmd eq 'screenmsg' - || lc $cmd eq 'ttsmsg' - || lc $cmd eq 'volume' - || lc $cmd eq 'mediagooglemusic' - || lc $cmd eq 'mediaamazonmusic' - || lc $cmd eq 'mediaspotifymusic' - || lc $cmd eq 'mediatuneinradio' - || lc $cmd eq 'mediaaldimusic' - || lc $cmd eq 'mediayoutube' - || lc $cmd eq 'mediavlcplayer' - || lc $cmd eq 'mediaaudible' - || lc $cmd eq 'screenbrightness' - || lc $cmd eq 'screenorientation' - || lc $cmd eq 'screenfullscreen' - || lc $cmd eq 'screen' - || lc $cmd eq 'openurl' - || lc $cmd eq 'openapp' - || lc $cmd eq 'nextalarmtime' - || lc $cmd eq 'timer' - || lc $cmd eq 'bluetooth' - || lc $cmd eq 'system' - || lc $cmd eq 'notifysndfile' - || lc $cmd eq 'changetobtdevice' - || lc $cmd eq 'clearnotificationbar' - || lc $cmd eq 'activatevoiceinput' - || lc $cmd eq 'volumenotification' - || lc $cmd eq 'volumeringsound' - || lc $cmd eq 'screenlock' - || lc $cmd eq 'statusrequest' - || lc $cmd eq 'sendsms' - || lc $cmd eq 'sendintent' - || lc $cmd eq 'currentflowsetupdate' - || lc $cmd eq 'installflowsource' - || lc $cmd eq 'opencall' - || lc $cmd eq 'closecall' - || lc $cmd eq 'donotdisturb' - || lc $cmd eq 'userflowstate' - || lc $cmd eq 'startdaydream' - || lc $cmd eq 'vibrate') { + my ($hash, $name, @aa) = @_; + my ($cmd, @args) = @aa; - Log3 $name, 5, "AMADDevice ($name) - set $name $cmd ".join(" ", @val); - - return AMADDevice_SelectSetCmd( $hash, $cmd, @val ) if( lc $cmd eq 'statusrequest' ); - return "set command only works if state not equal initialized" if( ReadingsVal( $hash->{NAME}, "state", 0 ) eq "initialized"); - return "Cannot set command, FHEM Device is disabled" if( AttrVal( $name, "disable", "0" ) eq "1" ); - - return "Cannot set command, FHEM Device is unknown" if( ReadingsVal( $name, "deviceState", "online" ) eq "unknown" ); - return "Cannot set command, FHEM Device is offline" if( ReadingsVal( $name, "deviceState", "online" ) eq "offline" ); - - return AMADDevice_SelectSetCmd( $hash, $cmd, @val ) if( (@val) or (lc $cmd eq 'activatevoiceinput') or (lc $cmd eq 'vibrate') or (lc $cmd eq 'currentflowsetupdate') or (lc $cmd eq 'closecall') or (lc $cmd eq 'startdaydream') ); - } + my $host = $hash->{HOST}; + my $port = $hash->{PORT}; + my $amad_id = $hash->{AMAD_ID}; + my $uri; + my $header; + my $method; - return "Unknown argument $cmd, bearword as argument or wrong parameter(s), choose one of $list"; - - } elsif( $modules{AMADDevice}{defptr}{BRIDGE} ) { - - my $list = ""; - - ## set Befehle für die AMADDevice_CommBridge - $list .= "expertMode:0,1 " if( $modules{AMADDevice}{defptr}{BRIDGE} ); - $list .= "fhemServerIP " if( $modules{AMADDevice}{defptr}{BRIDGE} ); - - if( lc $cmd eq 'expertmode' - || lc $cmd eq 'fhemserverip' ) { - - readingsSingleUpdate( $hash, $cmd, $val[0], 0 ); - - return; - } - - return "Unknown argument $cmd, bearword as argument or wrong parameter(s), choose one of $list"; - } -} - -sub AMADDevice_SelectSetCmd($$@) { - - my ( $hash, $cmd, @data ) = @_; - my $name = $hash->{NAME}; - my $host = $hash->{HOST}; - my $port = $hash->{PORT}; if( lc $cmd eq 'screenmsg' ) { - my $msg = join( " ", @data ); + my $msg = join( " ", @args ); - $msg =~ s/%/%25/g; - $msg =~ s/\s/%20/g; + $msg =~ s/%/%25/g; + $msg =~ s/\s/%20/g; - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/screenMsg?message=$msg"; - Log3 $name, 4, "AMADDevice ($name) - Sub AMADDevice_SetScreenMsg"; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/screenMsg?message=$msg"; + $method = "POST"; } elsif( lc $cmd eq 'ttsmsg' ) { - my $msg = join( " ", @data ); + my $msg = join( " ", @args ); my $speed = AttrVal( $name, "setTtsMsgSpeed", "1.0" ); my $lang = AttrVal( $name, "setTtsMsgLang","de" ); - $msg =~ s/%/%25/g; - $msg =~ s/\s/%20/g; + $msg =~ s/%/%25/g; + $msg =~ s/\s/%20/g; - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/ttsMsg?message=".$msg."&msgspeed=".$speed."&msglang=".$lang; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/ttsMsg?message=".$msg."&msgspeed=".$speed."&msglang=".$lang; + $method = "POST"; } elsif( lc $cmd eq 'userflowstate' ) { - my $datas = join( " ", @data ); - my ($flow,$state) = split( ":", $datas); + my $datas = join( " ", @args ); + my ($flow,$state) = split( ":", $datas); - $flow =~ s/\s/%20/g; + $flow =~ s/\s/%20/g; - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/flowState?flowstate=".$state."&flowname=".$flow; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/flowState?flowstate=".$state."&flowname=".$flow; + $method = "POST"; } elsif( lc $cmd eq 'volume' ) { - my $vol = join( " ", @data ); + my $vol = join( " ", @args ); if( $vol =~ /^\+(.*)/ or $vol =~ /^-(.*)/ ) { @@ -576,58 +446,52 @@ sub AMADDevice_SelectSetCmd($$@) { } } - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/setVolume?volume=$vol"; - - return AMADDevice_HTTP_POST( $hash, $url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/setVolume?volume=$vol"; + $method = "POST"; } elsif( lc $cmd eq 'volumenotification' ) { - my $vol = join( " ", @data ); + my $vol = join( " ", @args ); - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/setNotifiVolume?notifivolume=$vol"; - - return AMADDevice_HTTP_POST( $hash, $url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/setNotifiVolume?notifivolume=$vol"; + $method = "POST"; } elsif( lc $cmd eq 'volumeringsound' ) { - my $vol = join( " ", @data ); + my $vol = join( " ", @args ); - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/setRingSoundVolume?ringsoundvolume=$vol"; - - return AMADDevice_HTTP_POST( $hash, $url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/setRingSoundVolume?ringsoundvolume=$vol"; + $method = "POST"; } elsif( lc $cmd =~ /^media/ ) { - my $btn = join( " ", @data ); + my $btn = join( " ", @args ); - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/multimediaControl?mplayer=".$cmd."&button=".$btn; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/multimediaControl?mplayer=".$cmd."&button=".$btn; + $method = "POST"; } elsif( lc $cmd eq 'screenbrightness' ) { - my $bri = join( " ", @data ); + my $bri = join( " ", @args ); - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/setBrightness?brightness=$bri"; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/setBrightness?brightness=$bri"; + $method = "POST"; } elsif( lc $cmd eq 'screen' ) { - my $mod = join( " ", @data ); + my $mod = join( " ", @args ); my $scot = AttrVal( $name, "setScreenOnForTimer", undef ); $scot = 60 if( !$scot ); if ($mod eq "on" || $mod eq "off") { - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/setScreenOnOff?screen=".$mod."&screenontime=".$scot if ($mod eq "on" || $mod eq "off"); - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/setScreenOnOff?screen=".$mod."&screenontime=".$scot if ($mod eq "on" || $mod eq "off"); + $method = "POST"; } elsif ($mod eq "lock" || $mod eq "unlock") { @@ -636,220 +500,225 @@ sub AMADDevice_SelectSetCmd($$@) { my $PIN = AttrVal( $name, "setScreenlockPIN", undef ); $PIN = AMADDevice_decrypt($PIN); - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/screenlock?lockmod=".$mod."&lockPIN=".$PIN; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/screenlock?lockmod=".$mod."&lockPIN=".$PIN; + $method = "POST"; } } elsif( lc $cmd eq 'screenorientation' ) { - my $mod = join( " ", @data ); + my $mod = join( " ", @args ); - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/setScreenOrientation?orientation=$mod"; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/setScreenOrientation?orientation=$mod"; + $method = "POST"; } elsif( lc $cmd eq 'activatevoiceinput' ) { - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/setvoicecmd"; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/setvoicecmd"; + $method = "POST"; } elsif( lc $cmd eq 'screenfullscreen' ) { - my $mod = join( " ", @data ); - - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/setScreenFullscreen?fullscreen=$mod"; + my $mod = join( " ", @args ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/setScreenFullscreen?fullscreen=$mod"; + $method = "POST"; readingsSingleUpdate( $hash, $cmd, $mod, 1 ); - - return AMADDevice_HTTP_POST( $hash, $url ); } elsif( lc $cmd eq 'openurl' ) { - my $openurl = join( " ", @data ); + my $openurl = join( " ", @args ); my $browser = AttrVal( $name, "setOpenUrlBrowser", "com.android.chrome|com.google.android.apps.chrome.Main" ); my @browserapp = split( /\|/, $browser ); - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/openURL?url=".$openurl."&browserapp=".$browserapp[0]."&browserappclass=".$browserapp[1]; - - return AMADDevice_HTTP_POST( $hash, $url ); + my $uri = $host . ":" . $port . "/fhem-amad/setCommands/openURL?url=".$openurl."&browserapp=".$browserapp[0]."&browserappclass=".$browserapp[1]; + $method = "POST"; } elsif (lc $cmd eq 'nextalarmtime') { - my $value = join( " ", @data ); - my @alarm = split( ":", $value ); + my $value = join( " ", @args ); + my @alarm = split( ":", $value ); - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/setAlarm?hour=".$alarm[0]."&minute=".$alarm[1]; - - return AMADDevice_HTTP_POST( $hash, $url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/setAlarm?hour=".$alarm[0]."&minute=".$alarm[1]; + $method = "POST"; } elsif (lc $cmd eq 'timer') { - my $timer = join( " ", @data ); + my $timer = join( " ", @args ); - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/setTimer?minute=$timer"; - - return AMADDevice_HTTP_POST( $hash, $url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/setTimer?minute=$timer"; + $method = "POST"; } - + elsif( lc $cmd eq 'statusrequest' ) { - - AMADDevice_statusRequest( $hash ); - return undef; + + my $activetask = AttrVal( $name, "checkActiveTask", "none" ); + my $userFlowState = AttrVal( $name, "setUserFlowState", "none" ); + my $apssid = AttrVal( $name, "setAPSSID", "none" ); + my $fhemip = ReadingsVal($hash->{IODev}->{NAME}, "fhemServerIP", "none"); + my $bport = $hash->{IODev}->{PORT}; + + + $uri = $host . ":" . $port . "/fhem-amad/deviceInfo/"; # Pfad muß so im Automagic als http request Trigger drin stehen + $header = "fhemip: $fhemip\r\nfhemdevice: $name\r\nactivetask: $activetask\r\napssid: $apssid\r\nbport: $bport\r\nuserflowstate: $userFlowState"; + $method = "GET"; } - + elsif( lc $cmd eq 'openapp' ) { - my $app = join( " ", @data ); + my $app = join( " ", @args ); - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/openApp?app=$app"; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/openApp?app=$app"; + $method = "POST"; } elsif( lc $cmd eq 'system' ) { - my $systemcmd = join( " ", @data ); - - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/systemcommand?syscmd=$systemcmd"; + my $systemcmd = join( " ", @args ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/systemcommand?syscmd=$systemcmd"; + $method = "POST"; readingsSingleUpdate( $hash, "airplanemode", "on", 1 ) if( $systemcmd eq "airplanemodeON" ); readingsSingleUpdate( $hash, "deviceState", "offline", 1 ) if( $systemcmd eq "airplanemodeON" || $systemcmd eq "shutdown" ); - - return AMADDevice_HTTP_POST( $hash,$url ); } elsif( lc $cmd eq 'donotdisturb' ) { - my $disturbmod = join( " ", @data ); + my $disturbmod = join( " ", @args ); - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/donotdisturb?disturbmod=$disturbmod"; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/donotdisturb?disturbmod=$disturbmod"; + $method = "POST"; } elsif( lc $cmd eq 'bluetooth' ) { - my $mod = join( " ", @data ); + my $mod = join( " ", @args ); - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/setbluetooth?bluetooth=$mod"; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/setbluetooth?bluetooth=$mod"; + $method = "POST"; } elsif( lc $cmd eq 'notifysndfile' ) { - my $notify = join( " ", @data ); - my $filepath = AttrVal( $name, "setNotifySndFilePath", "/storage/emulated/0/Notifications/" ); + my $notify = join( " ", @args ); + my $filepath = AttrVal( $name, "setNotifySndFilePath", "/storage/emulated/0/Notifications/" ); - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/playnotifysnd?notifyfile=".$notify."¬ifypath=".$filepath; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/playnotifysnd?notifyfile=".$notify."¬ifypath=".$filepath; + $method = "POST"; } elsif( lc $cmd eq 'changetobtdevice' ) { - my $swToBtDevice = join( " ", @data ); + my $swToBtDevice = join( " ", @args ); my @swToBtMac = split( /\|/, $swToBtDevice ); my $btDevices = AttrVal( $name, "setBluetoothDevice", "none" ) if( AttrVal( $name, "setBluetoothDevice", "none" ) ne "none" ); my @btDevice = split( ',', $btDevices ); my @btDeviceOne = split( /\|/, $btDevice[0] ); my @btDeviceTwo = split( /\|/, $btDevice[1] ); - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/setbtdevice?swToBtDeviceMac=".$swToBtMac[1]."&btDeviceOne=".$btDeviceOne[1]."&btDeviceTwo=".$btDeviceTwo[1]; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/setbtdevice?swToBtDeviceMac=".$swToBtMac[1]."&btDeviceOne=".$btDeviceOne[1]."&btDeviceTwo=".$btDeviceTwo[1]; + $method = "POST"; } elsif( lc $cmd eq 'clearnotificationbar' ) { - my $appname = join( " ", @data ); + my $appname = join( " ", @args ); - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/clearnotificationbar?app=$appname"; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/clearnotificationbar?app=$appname"; + $method = "POST"; } elsif( lc $cmd eq 'vibrate' ) { - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/setvibrate"; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/setvibrate"; + $method = "POST"; } elsif( lc $cmd eq 'sendintent' ) { - my $intentstring = join( " ", @data ); + my $intentstring = join( " ", @args ); my ( $action, $exkey1, $exval1, $exkey2, $exval2 ) = split( "[ \t][ \t]*", $intentstring ); $exkey1 = "" if( !$exkey1 ); $exval1 = "" if( !$exval1 ); $exkey2 = "" if( !$exkey2 ); $exval2 = "" if( !$exval2 ); - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/sendIntent?action=".$action."&exkey1=".$exkey1."&exval1=".$exval1."&exkey2=".$exkey2."&exval2=".$exval2; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/sendIntent?action=".$action."&exkey1=".$exkey1."&exval1=".$exval1."&exkey2=".$exkey2."&exval2=".$exval2; + $method = "POST"; } elsif( lc $cmd eq 'installflowsource' ) { - my $flowname = join( " ", @data ); + my $flowname = join( " ", @args ); - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/installFlow?flowname=$flowname"; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/installFlow?flowname=$flowname"; + $method = "POST"; } elsif( lc $cmd eq 'opencall' ) { - my $string = join( " ", @data ); + my $string = join( " ", @args ); my ($callnumber, $time) = split( "[ \t][ \t]*", $string ); - $time = "none" if( !$time ); + $time = "none" if( !$time ); - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/openCall?callnumber=".$callnumber."&hanguptime=".$time; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/openCall?callnumber=".$callnumber."&hanguptime=".$time; + $method = "POST"; } elsif( lc $cmd eq 'closecall' ) { - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/closeCall"; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/closeCall"; + $method = "POST"; } elsif( lc $cmd eq 'startdaydream' ) { - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/startDaydream"; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/setCommands/startDaydream"; + $method = "POST"; } elsif( lc $cmd eq 'currentflowsetupdate' ) { - my $url = "http://" . $host . ":" . $port . "/fhem-amad/currentFlowsetUpdate"; - - return AMADDevice_HTTP_POST( $hash,$url ); + $uri = $host . ":" . $port . "/fhem-amad/currentFlowsetUpdate"; + $method = "POST"; } elsif( lc $cmd eq 'sendsms' ) { - my $string = join( " ", @data ); + my $string = join( " ", @args ); my ($smsmessage, $smsnumber) = split( "\\|", $string ); $smsmessage =~ s/%/%25/g; $smsmessage =~ s/\s/%20/g; - my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/sendSms?smsmessage=".$smsmessage."&smsnumber=".$smsnumber; + $uri = $host . ":" . $port . "/fhem-amad/setCommands/sendSms?smsmessage=".$smsmessage."&smsnumber=".$smsnumber; + $method = "POST"; + + } else { + + my $apps = AttrVal( $name, "setOpenApp", "none" ); + my $btdev = AttrVal( $name, "setBluetoothDevice", "none" ); + + + my $list = "screenMsg ttsMsg volume:slider,0,1,15 mediaGoogleMusic:play/pause,stop,next,back mediaAmazonMusic:play/pause,stop,next,back mediaSpotifyMusic:play/pause,stop,next,back mediaTuneinRadio:play/pause,stop,next,back mediaAldiMusic:play/pause,stop,next,back mediaYouTube:play/pause,stop,next,back mediaVlcPlayer:play/pause,stop,next,back mediaAudible:play/pause,stop,next,back screenBrightness:slider,0,1,255 screen:on,off,lock,unlock openURL nextAlarmTime:time timer:slider,1,1,60 statusRequest:noArg bluetooth:on,off notifySndFile clearNotificationBar:All,Automagic activateVoiceInput:noArg volumeNotification:slider,0,1,7 volumeRingSound:slider,0,1,7 vibrate:noArg sendIntent openCall closeCall:noArg currentFlowsetUpdate:noArg installFlowSource doNotDisturb:never,always,alarmClockOnly,onlyImportant userFlowState sendSMS startDaydream:noArg"; - return AMADDevice_HTTP_POST( $hash,$url ); + $list .= " screenOrientation:auto,landscape,portrait" if( AttrVal( $name, "setScreenOrientation", "0" ) eq "1" ); + $list .= " screenFullscreen:on,off" if( AttrVal( $name, "setFullscreen", "0" ) eq "1" ); + $list .= " openApp:$apps" if( AttrVal( $name, "setOpenApp", "none" ) ne "none" ); + $list .= " system:reboot,shutdown,airplanemodeON" if( AttrVal( $name, "root", "0" ) eq "1" ); + $list .= " changetoBTDevice:$btdev" if( AttrVal( $name, "setBluetoothDevice", "none" ) ne "none" ); + + + return "Unknown argument $cmd, choose one of $list"; } + + $header = undef unless( defined($header) and ($header) ); + IOWrite($hash,$amad_id,$uri,$method,$header); + Log3 $name, 5, "AMADDevice ($name) - IOWrite: $uri $method IODevHash=$hash->{IODev}"; return undef; } @@ -858,11 +727,9 @@ sub AMADDevice_Parse($$) { my ($io_hash,$json) = @_; my $name = $io_hash->{NAME}; - my $amadDevice; - my $decode_json; - $decode_json = eval{decode_json($json)}; + my $decode_json = eval{decode_json($json)}; if($@){ Log3 $name, 3, "AMADDevice ($name) - error while request: $@"; #readingsSingleUpdate($hash, "state", "error", 1); @@ -871,13 +738,13 @@ sub AMADDevice_Parse($$) { Log3 $name, 3, "AMADDevice ($name) - ParseFn was called"; Log3 $name, 3, "AMADDevice ($name) - ParseFn was called, !!! JSON: $json"; - Log3 $name, 3, "AMADDevice ($name) - ParseFn was called, !!! AMAD: $decode_json->{amad}{AMADDEVICE}"; + Log3 $name, 3, "AMADDevice ($name) - ParseFn was called, !!! AMAD_ID: $decode_json->{amad}{amad_id}"; - $amadDevice = $decode_json->{amad}{AMADDEVICE}; - my $code = $io_hash->{NAME} ."-". $amadDevice; + my $fhemDevice = $decode_json->{payload}{fhemdevice} if( defined($decode_json->{payload}{fhemdevice}) ); + my $amad_id = $decode_json->{amad}{amad_id}; - if( my $hash = $modules{AMADDevice}{defptr}{$code} ) { + if( my $hash = $modules{AMADDevice}{defptr}{$amad_id} ) { my $name = $hash->{NAME}; AMADDevice_WriteReadings($hash,$decode_json); @@ -887,11 +754,10 @@ sub AMADDevice_Parse($$) { } else { - return "UNDEFINED $amadDevice AMADDevice $decode_json->{payload}{'DEVICE-IP'} IODev=$name"; + return "UNDEFINED $fhemDevice AMADDevice $decode_json->{payload}{'amaddevice_ip'} $decode_json->{amad}{'amad_id'} IODev=$name"; } } - ################################## ################################## #### my little helpers ###########