ScreenLock on/off mit verschlüsselter PIN als Attribut
This commit is contained in:
		
							
								
								
									
										129
									
								
								74_AMAD.pm
									
									
									
									
									
								
							
							
						
						
									
										129
									
								
								74_AMAD.pm
									
									
									
									
									
								
							@@ -35,7 +35,11 @@ use Time::HiRes qw(gettimeofday);
 | 
				
			|||||||
use HttpUtils;
 | 
					use HttpUtils;
 | 
				
			||||||
use TcpServerUtils;
 | 
					use TcpServerUtils;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
my $version = "1.0.0";
 | 
					use Encode qw(encode);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					my $version = "1.1.1";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -55,6 +59,7 @@ sub AMAD_Initialize($) {
 | 
				
			|||||||
			  "setScreenOrientation:0,1 ".
 | 
								  "setScreenOrientation:0,1 ".
 | 
				
			||||||
			  "setScreenBrightness:0,1 ".
 | 
								  "setScreenBrightness:0,1 ".
 | 
				
			||||||
			  "setBluetoothDevice ".
 | 
								  "setBluetoothDevice ".
 | 
				
			||||||
 | 
								  "setScreenlockPIN ".
 | 
				
			||||||
			  "root:0,1 ".
 | 
								  "root:0,1 ".
 | 
				
			||||||
			  "interval ".
 | 
								  "interval ".
 | 
				
			||||||
			  "port ".
 | 
								  "port ".
 | 
				
			||||||
@@ -143,6 +148,8 @@ sub AMAD_Attr(@) {
 | 
				
			|||||||
my ( $cmd, $name, $attrName, $attrVal ) = @_;
 | 
					my ( $cmd, $name, $attrName, $attrVal ) = @_;
 | 
				
			||||||
    my $hash = $defs{$name};
 | 
					    my $hash = $defs{$name};
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
					    my $orig = $attrVal;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if( $attrName eq "disable" ) {
 | 
					    if( $attrName eq "disable" ) {
 | 
				
			||||||
	if( $cmd eq "set" ) {
 | 
						if( $cmd eq "set" ) {
 | 
				
			||||||
	    if( $attrVal eq "0" ) {
 | 
						    if( $attrVal eq "0" ) {
 | 
				
			||||||
@@ -154,25 +161,17 @@ my ( $cmd, $name, $attrName, $attrVal ) = @_;
 | 
				
			|||||||
		readingsSingleUpdate ( $hash, "state", "disabled", 1 );
 | 
							readingsSingleUpdate ( $hash, "state", "disabled", 1 );
 | 
				
			||||||
		RemoveInternalTimer( $hash );
 | 
							RemoveInternalTimer( $hash );
 | 
				
			||||||
		Log3 $name, 3, "AMAD ($name) - disabled";
 | 
							Log3 $name, 3, "AMAD ($name) - disabled";
 | 
				
			||||||
	    }
 | 
					            }
 | 
				
			||||||
	}
 | 
					            
 | 
				
			||||||
	elsif( $cmd eq "del" ) {
 | 
					        } else {
 | 
				
			||||||
	    RemoveInternalTimer( $hash );
 | 
						    RemoveInternalTimer( $hash );
 | 
				
			||||||
	    InternalTimer( gettimeofday()+2, "AMAD_GetUpdateTimer", $hash, 0 ) if( ReadingsVal( $hash->{NAME}, "state", 0 ) eq "disabled" );
 | 
						    InternalTimer( gettimeofday()+2, "AMAD_GetUpdateTimer", $hash, 0 ) if( ReadingsVal( $hash->{NAME}, "state", 0 ) eq "disabled" );
 | 
				
			||||||
	    readingsSingleUpdate ( $hash, "state", "active", 1 );
 | 
						    readingsSingleUpdate ( $hash, "state", "active", 1 );
 | 
				
			||||||
	    Log3 $name, 3, "AMAD ($name) - enabled";
 | 
						    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( $cmd eq "set" ) {
 | 
				
			||||||
	    if( $attrVal < 60 ) {
 | 
						    if( $attrVal < 60 ) {
 | 
				
			||||||
		Log3 $name, 3, "AMAD ($name) - interval too small, please use something > 60 (sec), default is 180 (sec)";
 | 
							Log3 $name, 3, "AMAD ($name) - interval too small, please use something > 60 (sec), default is 180 (sec)";
 | 
				
			||||||
@@ -181,41 +180,38 @@ my ( $cmd, $name, $attrName, $attrVal ) = @_;
 | 
				
			|||||||
		$hash->{INTERVAL} = $attrVal;
 | 
							$hash->{INTERVAL} = $attrVal;
 | 
				
			||||||
		Log3 $name, 3, "AMAD ($name) - set interval to $attrVal";
 | 
							Log3 $name, 3, "AMAD ($name) - set interval to $attrVal";
 | 
				
			||||||
	    }
 | 
						    }
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	elsif( $cmd eq "del" ) {
 | 
					 | 
				
			||||||
	    $hash->{INTERVAL} = 180;
 | 
					 | 
				
			||||||
	    Log3 $name, 3, "AMAD ($name) - set interval to default";
 | 
					 | 
				
			||||||
	    
 | 
						    
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
	    if( $cmd eq "set" ) {
 | 
						    $hash->{INTERVAL} = 180;
 | 
				
			||||||
		$attr{$name}{$attrName} = $attrVal;
 | 
						    Log3 $name, 3, "AMAD ($name) - set interval to default";
 | 
				
			||||||
		Log3 $name, 3, "AMAD ($name) - $attrName : $attrVal";
 | 
					 | 
				
			||||||
	    }
 | 
					 | 
				
			||||||
	    elsif( $cmd eq "del" ) {
 | 
					 | 
				
			||||||
	    }
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    if( $attrName eq "port" ) {
 | 
					    elsif( $attrName eq "port" ) {
 | 
				
			||||||
	if( $cmd eq "set" ) {
 | 
						if( $cmd eq "set" ) {
 | 
				
			||||||
	    $hash->{PORT} = $attrVal;
 | 
						    $hash->{PORT} = $attrVal;
 | 
				
			||||||
	    Log3 $name, 3, "AMAD ($name) - set port to $attrVal";
 | 
						    Log3 $name, 3, "AMAD ($name) - set port to $attrVal";
 | 
				
			||||||
	}
 | 
					
 | 
				
			||||||
	elsif( $cmd eq "del" ) {
 | 
					        } else {
 | 
				
			||||||
	    $hash->{PORT} = 8090;
 | 
						    $hash->{PORT} = 8090;
 | 
				
			||||||
	    Log3 $name, 3, "AMAD ($name) - set port to default";
 | 
						    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" ) {
 | 
					 | 
				
			||||||
	    }
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    return undef;
 | 
					    elsif( $attrName eq "setScreenlockPIN" ) {
 | 
				
			||||||
 | 
						if( $cmd eq "set" && $attrVal ) {
 | 
				
			||||||
 | 
						    $attrVal = AMAD_encrypt($attrVal);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    if( $cmd eq "set" ) {
 | 
				
			||||||
 | 
					        if( $attrVal && $orig ne $attrVal ) {
 | 
				
			||||||
 | 
					            $attr{$name}{$attrName} = $attrVal;
 | 
				
			||||||
 | 
					            return $attrName ." set to ". $attrVal if( $init_done );
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
sub AMAD_GetUpdateLocal($) {
 | 
					sub AMAD_GetUpdateLocal($) {
 | 
				
			||||||
@@ -465,6 +461,8 @@ sub AMAD_Set($$@) {
 | 
				
			|||||||
	$list .= "clearNotificationBar:All,Automagic ";
 | 
						$list .= "clearNotificationBar:All,Automagic ";
 | 
				
			||||||
	$list .= "changetoBTDevice:$btdev " if( AttrVal( $name, "setBluetoothDevice", "none" ) ne "none" );
 | 
						$list .= "changetoBTDevice:$btdev " if( AttrVal( $name, "setBluetoothDevice", "none" ) ne "none" );
 | 
				
			||||||
	$list .= "activateVoiceInput:noArg ";
 | 
						$list .= "activateVoiceInput:noArg ";
 | 
				
			||||||
 | 
						$list .= "screenLock:on,off " if( AttrVal( $name, "setScreenlockPIN", "none" ) ne "none" );
 | 
				
			||||||
 | 
						$list .= "notifiVolume:slider,0,1,7 ";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if( lc $cmd eq 'screenmsg'
 | 
						if( lc $cmd eq 'screenmsg'
 | 
				
			||||||
	    || lc $cmd eq 'ttsmsg'
 | 
						    || lc $cmd eq 'ttsmsg'
 | 
				
			||||||
@@ -484,6 +482,8 @@ sub AMAD_Set($$@) {
 | 
				
			|||||||
	    || lc $cmd eq 'changetobtdevice'
 | 
						    || lc $cmd eq 'changetobtdevice'
 | 
				
			||||||
	    || lc $cmd eq 'clearnotificationbar'
 | 
						    || lc $cmd eq 'clearnotificationbar'
 | 
				
			||||||
	    || lc $cmd eq 'activatevoiceinput'
 | 
						    || lc $cmd eq 'activatevoiceinput'
 | 
				
			||||||
 | 
						    || lc $cmd eq 'notifivolume'
 | 
				
			||||||
 | 
						    || lc $cmd eq 'screenlock'
 | 
				
			||||||
	    || lc $cmd eq 'statusrequest' ) {
 | 
						    || lc $cmd eq 'statusrequest' ) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	    Log3 $name, 5, "AMAD ($name) - set $name $cmd ".join(" ", @val);
 | 
						    Log3 $name, 5, "AMAD ($name) - set $name $cmd ".join(" ", @val);
 | 
				
			||||||
@@ -561,6 +561,16 @@ sub AMAD_SelectSetCmd($$@) {
 | 
				
			|||||||
	return AMAD_HTTP_POST( $hash, $url );
 | 
						return AMAD_HTTP_POST( $hash, $url );
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
					    elsif( lc $cmd eq 'notifivolume' ) {
 | 
				
			||||||
 | 
						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' ) {
 | 
					    elsif( lc $cmd eq 'mediaplayer' ) {
 | 
				
			||||||
	my $btn = join( " ", @data );
 | 
						my $btn = join( " ", @data );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -694,6 +704,16 @@ sub AMAD_SelectSetCmd($$@) {
 | 
				
			|||||||
	return AMAD_HTTP_POST( $hash,$url );
 | 
						return AMAD_HTTP_POST( $hash,$url );
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
					    elsif( lc $cmd eq 'screenlock' ) {
 | 
				
			||||||
 | 
						my $lockmode = join( " ", @data );
 | 
				
			||||||
 | 
						my $PIN = AttrVal( $name, "setScreenlockPIN", undef );
 | 
				
			||||||
 | 
						my $PIN = AMAD_decrypt($PIN);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						my $url = "http://" . $host . ":" . $port . "/fhem-amad/setCommands/screenlock?mode=".$lockmode."&lockPIN=".$PIN;
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
						return AMAD_HTTP_POST( $hash,$url );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return undef;
 | 
					    return undef;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1038,6 +1058,7 @@ sub AMAD_CommBridge_Read($) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
sub AMAD_Header2Hash($) {
 | 
					sub AMAD_Header2Hash($) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    my ( $string ) = @_;
 | 
					    my ( $string ) = @_;
 | 
				
			||||||
    my %hash = ();
 | 
					    my %hash = ();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1052,6 +1073,40 @@ sub AMAD_Header2Hash($) {
 | 
				
			|||||||
    return \%hash;
 | 
					    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;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user