From fe49733b7ee9b519fd6f2500e117b69604b594ac Mon Sep 17 00:00:00 2001 From: markooldenburg Date: Wed, 3 Feb 2016 20:06:39 +0000 Subject: [PATCH] 74_AMAD: New Mijor Release 1.2.0 git-svn-id: https://svn.fhem.de/fhem/trunk@10717 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 8 + fhem/FHEM/74_AMAD.pm | 228 ++- fhem/FHEM/lib/74_AMADautomagicFlows_1.2.0.xml | 1796 +++++++++++++++++ 3 files changed, 1959 insertions(+), 73 deletions(-) create mode 100644 fhem/FHEM/lib/74_AMADautomagicFlows_1.2.0.xml diff --git a/fhem/CHANGED b/fhem/CHANGED index fcd405333..83af8f381 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,13 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. + - updated: 74_AMAD: New Mijor Release 1.2.0 + !!! ATTENTION !!! Do not forget to to update the new + Automagic Flowset !!! + - feature: 74_AMAD: shutown the Device, activate Airplanemode, control + Notivicationvolume, vibrates the Device, Locked Device + by PIN + - bugfix: 74_AMAD: modified Error handling, best practice for identify offline + Mode - feature: New module 74_HOMBOT to control LG Hombot vacuum cleaner - bugfix: 49_SSCAM: credentials won't be deleted if "rereadcfg" is used - bugfix: 76_MSGMail: Make use of attribute mailtype again (enabling html) diff --git a/fhem/FHEM/74_AMAD.pm b/fhem/FHEM/74_AMAD.pm index ca02cf336..0ecbeba8a 100644 --- a/fhem/FHEM/74_AMAD.pm +++ b/fhem/FHEM/74_AMAD.pm @@ -2,7 +2,7 @@ # # Developed with Kate # -# (c) 2015 Copyright: Marko Oldenburg (leongaultier at gmail dot com) +# (c) 2015-2016 Copyright: Marko Oldenburg (leongaultier at gmail dot com) # All rights reserved # # This script is free software; you can redistribute it and/or modify @@ -34,8 +34,11 @@ use Time::HiRes qw(gettimeofday); use HttpUtils; use TcpServerUtils; +use Encode qw(encode); + + +my $version = "1.2.0"; -my $version = "1.0.0"; @@ -55,6 +58,7 @@ sub AMAD_Initialize($) { "setScreenOrientation:0,1 ". "setScreenBrightness:0,1 ". "setBluetoothDevice ". + "setScreenlockPIN ". "root:0,1 ". "interval ". "port ". @@ -142,6 +146,8 @@ sub AMAD_Attr(@) { my ( $cmd, $name, $attrName, $attrVal ) = @_; my $hash = $defs{$name}; + + my $orig = $attrVal; if( $attrName eq "disable" ) { if( $cmd eq "set" ) { @@ -154,25 +160,17 @@ my ( $cmd, $name, $attrName, $attrVal ) = @_; readingsSingleUpdate ( $hash, "state", "disabled", 1 ); RemoveInternalTimer( $hash ); Log3 $name, 3, "AMAD ($name) - disabled"; - } - } - elsif( $cmd eq "del" ) { + } + + } else { RemoveInternalTimer( $hash ); InternalTimer( gettimeofday()+2, "AMAD_GetUpdateTimer", $hash, 0 ) if( ReadingsVal( $hash->{NAME}, "state", 0 ) eq "disabled" ); readingsSingleUpdate ( $hash, "state", "active", 1 ); Log3 $name, 3, "AMAD ($name) - enabled"; - - } else { - if($cmd eq "set") { - $attr{$name}{$attrName} = $attrVal; - Log3 $name, 3, "AMAD ($name) - $attrName : $attrVal"; - } - elsif( $cmd eq "del" ) { - } - } + } } - if( $attrName eq "interval" ) { + elsif( $attrName eq "interval" ) { if( $cmd eq "set" ) { if( $attrVal < 60 ) { Log3 $name, 3, "AMAD ($name) - interval too small, please use something > 60 (sec), default is 180 (sec)"; @@ -181,41 +179,40 @@ my ( $cmd, $name, $attrName, $attrVal ) = @_; $hash->{INTERVAL} = $attrVal; Log3 $name, 3, "AMAD ($name) - set interval to $attrVal"; } - } - elsif( $cmd eq "del" ) { + + } else { $hash->{INTERVAL} = 180; Log3 $name, 3, "AMAD ($name) - set interval to default"; - - } else { - if( $cmd eq "set" ) { - $attr{$name}{$attrName} = $attrVal; - Log3 $name, 3, "AMAD ($name) - $attrName : $attrVal"; - } - elsif( $cmd eq "del" ) { - } } } - if( $attrName eq "port" ) { + elsif( $attrName eq "port" ) { if( $cmd eq "set" ) { $hash->{PORT} = $attrVal; Log3 $name, 3, "AMAD ($name) - set port to $attrVal"; - } - elsif( $cmd eq "del" ) { + + } else { $hash->{PORT} = 8090; Log3 $name, 3, "AMAD ($name) - set port to default"; - - } else { - if( $cmd eq "set" ) { - $attr{$name}{$attrName} = $attrVal; - Log3 $name, 3, "AMAD ($name) - $attrName : $attrVal"; - } - elsif( $cmd eq "del" ) { - } - } + } + } + + elsif( $attrName eq "setScreenlockPIN" ) { + if( $cmd eq "set" && $attrVal ) { + $attrVal = AMAD_encrypt($attrVal); + } else { + CommandDeleteReading( undef, "$name screenLock" ); + } + } + + if( $cmd eq "set" ) { + if( $attrVal && $orig ne $attrVal ) { + $attr{$name}{$attrName} = $attrVal; + return $attrName ." set to ". $attrVal if( $init_done ); + } } - return undef; + return; } sub AMAD_GetUpdateLocal($) { @@ -313,11 +310,6 @@ sub AMAD_RetrieveAutomagicInfoFinished($$$) { Log3 $name, 4, "AMAD ($name) - Informations Flow on your Device is inactive, will try to reactivate"; } - elsif($hash->{helper}{infoErrorCounter} > 4 && ReadingsVal( $name, "flow_Informations", "active" ) eq "active" ){ - readingsBulkUpdate( $hash, "lastStatusRequestError", "check automagicApp on your device" ); - - Log3 $name, 4, "AMAD ($name) - Please check the AutomagicAPP on your Device"; - } elsif( $hash->{helper}{infoErrorCounter} > 9 ) { readingsBulkUpdate( $hash, "lastStatusRequestError", "to many errors, check your network or device configuration" ); @@ -327,6 +319,12 @@ sub AMAD_RetrieveAutomagicInfoFinished($$$) { readingsBulkUpdate ( $hash, "state", "To many Errors, device set offline"); $hash->{helper}{infoErrorCounter} = 0; } + elsif($hash->{helper}{infoErrorCounter} > 4 && ReadingsVal( $name, "flow_Informations", "active" ) eq "active" ){ + readingsBulkUpdate( $hash, "lastStatusRequestError", "check automagicApp on your device" ); + + Log3 $name, 4, "AMAD ($name) - Please check the AutomagicAPP on your Device"; + } + readingsEndUpdate( $hash, 1 ); } @@ -459,12 +457,15 @@ sub AMAD_Set($$@) { $list .= "openApp:$apps " if( AttrVal( $name, "setOpenApp", "none" ) ne "none" ); $list .= "nextAlarmTime:time "; $list .= "statusRequest:noArg "; - $list .= "system:reboot " if( AttrVal( $name, "root", "1" ) eq "1" ); + $list .= "system:reboot,shutdown,airplanemodeON " if( AttrVal( $name, "root", "1" ) 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 .= "screenLock:on,off " if( AttrVal( $name, "setScreenlockPIN", "none" ) ne "none" ); + $list .= "volumeNotification:slider,0,1,7 "; + $list .= "vibrate:noArg"; if( lc $cmd eq 'screenmsg' || lc $cmd eq 'ttsmsg' @@ -484,7 +485,10 @@ sub AMAD_Set($$@) { || lc $cmd eq 'changetobtdevice' || lc $cmd eq 'clearnotificationbar' || lc $cmd eq 'activatevoiceinput' - || lc $cmd eq 'statusrequest' ) { + || lc $cmd eq 'volumenotification' + || lc $cmd eq 'screenlock' + || lc $cmd eq 'statusrequest' + || lc $cmd eq 'vibrate') { Log3 $name, 5, "AMAD ($name) - set $name $cmd ".join(" ", @val); @@ -494,7 +498,7 @@ sub AMAD_Set($$@) { return AMAD_SelectSetCmd( $hash, $cmd, @val ) if( @val ) && ( ReadingsVal( $name, "deviceState", "online" ) eq "offline" ) && ( lc $cmd eq 'devicestate' ); return "Cannot set command, FHEM Device is offline" if( ReadingsVal( $name, "deviceState", "online" ) eq "offline" ); - return AMAD_SelectSetCmd( $hash, $cmd, @val ) if( @val ) || ( lc $cmd eq 'statusrequest' ) || ( lc $cmd eq 'activatevoiceinput' ); + return AMAD_SelectSetCmd( $hash, $cmd, @val ) if( @val ) || ( lc $cmd eq 'statusrequest' ) || ( lc $cmd eq 'activatevoiceinput' ) || ( lc $cmd eq 'vibrate' ); } return "Unknown argument $cmd, bearword as argument or wrong parameter(s), choose one of $list"; @@ -561,6 +565,16 @@ sub AMAD_SelectSetCmd($$@) { return AMAD_HTTP_POST( $hash, $url ); } + elsif( lc $cmd eq 'volumenotification' ) { + my $vol = join( " ", @data ); + + my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/setNotifiVolume?notifivolume=$vol"; + + readingsSingleUpdate( $hash, $cmd, $vol, 1 ); + + return AMAD_HTTP_POST( $hash, $url ); + } + elsif( lc $cmd eq 'mediaplayer' ) { my $btn = join( " ", @data ); @@ -602,7 +616,6 @@ sub AMAD_SelectSetCmd($$@) { } elsif( lc $cmd eq 'activatevoiceinput' ) { - #my $cmd = join( " ", @data ); my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/setvoicecmd"; @@ -653,6 +666,9 @@ sub AMAD_SelectSetCmd($$@) { my $systemcmd = join( " ", @data ); my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/systemcommand?syscmd=$systemcmd"; + + readingsSingleUpdate( $hash, $systemcmd, "on", 1 ) if( $systemcmd eq "airplanemodeON" ); + readingsSingleUpdate( $hash, "deviceState", "offline", 1 ) if( $systemcmd eq "airplanemodeON" || $systemcmd eq "shutdown" ); return AMAD_HTTP_POST( $hash,$url ); } @@ -693,6 +709,24 @@ sub AMAD_SelectSetCmd($$@) { return AMAD_HTTP_POST( $hash,$url ); } + + elsif( lc $cmd eq 'screenlock' ) { + my $lockmod = join( " ", @data ); + my $PIN = AttrVal( $name, "setScreenlockPIN", undef ); + $PIN = AMAD_decrypt($PIN); + + my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/screenlock?lockmod=".$lockmod."&lockPIN=".$PIN; + + readingsSingleUpdate( $hash, $cmd, $lockmod, 1 ); + return AMAD_HTTP_POST( $hash,$url ); + } + + elsif( lc $cmd eq 'vibrate' ) { + + my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/setvibrate"; + + return AMAD_HTTP_POST( $hash,$url ); + } return undef; } @@ -759,11 +793,6 @@ sub AMAD_HTTP_POSTerrorHandling($$$) { Log3 $name, 4, "AMAD ($name) - Flow SetCommands on your Device is inactive, will try to reactivate"; } - elsif( $hash->{helper}{setCmdErrorCounter} > 4 && ReadingsVal( $name, "flow_SetCommands", "active" ) eq "active" ){ - readingsBulkUpdate( $hash, "lastSetCommandError", "check automagicApp on your device" ); - - Log3 $name, 4, "AMAD ($name) - Please check the AutomagicAPP on your Device"; - } elsif( $hash->{helper}{setCmdErrorCounter} > 9 ) { readingsBulkUpdate( $hash, "lastSetCommandError", "to many errors, check your network or device configuration" ); @@ -773,6 +802,12 @@ sub AMAD_HTTP_POSTerrorHandling($$$) { 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" ){ + readingsBulkUpdate( $hash, "lastSetCommandError", "check automagicApp on your device" ); + + Log3 $name, 4, "AMAD ($name) - Please check the AutomagicAPP on your Device"; + } + readingsEndUpdate( $hash, 1 ); } @@ -1037,7 +1072,8 @@ sub AMAD_CommBridge_Read($) { $response; } -sub AMAD_Header2Hash($) { +sub AMAD_Header2Hash($) { + my ( $string ) = @_; my %hash = (); @@ -1052,6 +1088,40 @@ sub AMAD_Header2Hash($) { return \%hash; } +sub AMAD_encrypt($) { + + my ($decodedPIN) = @_; + my $key = getUniqueId(); + my $encodedPIN; + + return $decodedPIN if( $decodedPIN =~ /^crypt:(.*)/ ); + + for my $char (split //, $decodedPIN) { + my $encode = chop($key); + $encodedPIN .= sprintf("%.2x",ord($char)^ord($encode)); + $key = $encode.$key; + } + + return 'crypt:'. $encodedPIN; +} + +sub AMAD_decrypt($) { + + my ($encodedPIN) = @_; + my $key = getUniqueId(); + my $decodedPIN; + + $encodedPIN = $1 if( $encodedPIN =~ /^crypt:(.*)/ ); + + for my $char (map { pack('C', hex($_)) } ($encodedPIN =~ /(..)/g)) { + my $decode = chop($key); + $decodedPIN .= chr(ord($char)^ord($decode)); + $key = $decode.$key; + } + + return $decodedPIN; +} + @@ -1088,6 +1158,7 @@ sub AMAD_Header2Hash($) {
  • Default volume
  • Media volume device speaker
  • Media volume Bluetooth speaker
  • +
  • ...

  • With some experience lots of information from the Android device can be shown in FHEM. This requires only small adjustments of the "Informations" flow @@ -1112,6 +1183,7 @@ sub AMAD_Header2Hash($) {
  • Set system commands (reboot)
  • Send a message which will be announced (TTS)
  • Default media volume
  • +
  • ...


  • To trigger actions and to obtain information you need the Android App Automagic and a matching Flow. The App you need to get from the app store (google play), @@ -1157,6 +1229,7 @@ sub AMAD_Header2Hash($) { Readings
    Mit etwas Einarbeitung können jegliche Informationen welche Automagic bereit stellt in FHEM angezeigt werden. Hierzu bedarf es lediglich @@ -1330,11 +1410,11 @@ sub AMAD_Header2Hash($) {
  • neuen Statusreport des Gerätes anfordern
  • Systembefehle setzen (Reboot)
  • eine Nachricht senden welche angesagt wird (TTS)
  • -
  • Medienlautstärke regeln
  • +
  • Medienlautstärke regeln
  • +
  • ...


  • - Für all diese Aktionen und Informationen wird auf dem Androidgerät Automagic und ein so genannter Flow benötigt. Die App müßt - Ihr Euch besorgen, die Flows bekommt Ihr von mir zusammen mit dem AMAD Modul. + Für all diese Aktionen und Informationen wird auf dem Androidgerät Automagic und ein so genannter Flow benötigt. Die App ist über den Google PlayStore zu beziehen. Das benötigte Flowset bekommt Ihr aus dem FHEM Update.

    Wie genau verwendet man nun AMAD?