AndroidDB: Fixed bug in attribute createReadings

git-svn-id: https://svn.fhem.de/fhem/trunk@25897 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
zap 2022-03-30 12:13:03 +00:00
parent f91abc569f
commit e4e5dc7630
3 changed files with 335 additions and 303 deletions

View File

@ -1,5 +1,6 @@
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # 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. # Do not insert empty lines here, update check depends on it.
- bugfix: 89_AndroidDB: Fixed bug in attribute createReadings
- feature: 89_AndroidDB: Added creation of readings for shell commands - feature: 89_AndroidDB: Added creation of readings for shell commands
- feature: 98_archetype: new option for FILTER in actual_.* attributes - feature: 98_archetype: new option for FILTER in actual_.* attributes
- change: 98_archetype: now packaged, extend commandref - change: 98_archetype: now packaged, extend commandref

View File

@ -4,7 +4,7 @@
# #
# 89_AndroidDB # 89_AndroidDB
# #
# Version 0.8 # Version 0.9
# #
# FHEM Integration for Android Devices # FHEM Integration for Android Devices
# #
@ -25,23 +25,24 @@ use strict;
use warnings; use warnings;
use SetExtensions; use SetExtensions;
sub AndroidDB_Initialize ($) sub AndroidDB_Initialize ($) {
{ my ($hash) = @_;
my ($hash) = @_;
$hash->{DefFn} = "AndroidDB::Define"; $hash->{DefFn} = "AndroidDB::Define";
$hash->{UndefFn} = "AndroidDB::Undef"; $hash->{UndefFn} = "AndroidDB::Undef";
$hash->{SetFn} = "AndroidDB::Set"; $hash->{SetFn} = "AndroidDB::Set";
$hash->{GetFn} = "AndroidDB::Get"; $hash->{GetFn} = "AndroidDB::Get";
$hash->{AttrFn} = "AndroidDB::Attr"; $hash->{AttrFn} = "AndroidDB::Attr";
$hash->{ShutdownFn} = "AndroidDB::Shutdown"; $hash->{ShutdownFn} = "AndroidDB::Shutdown";
$hash->{parseParams} = 1; $hash->{parseParams} = 1;
$hash->{AttrList} = 'connect:0,1 createReadings macros:textField-long preset presetFile '.$readingFnAttributes; $hash->{AttrList} =
'connect:0,1 createReadings macros:textField-long preset presetFile '
. $readingFnAttributes;
$data{RC_layout}{MagentaTVStick} = "AndroidDB::RCLayoutMagentaTVStick"; $data{RC_layout}{MagentaTVStick} = "AndroidDB::RCLayoutMagentaTVStick";
$data{RC_layout}{MagentaOne} = "AndroidDB::RCLayoutMagentaOne"; $data{RC_layout}{MagentaOne} = "AndroidDB::RCLayoutMagentaOne";
$data{RC_layout}{MagentaTVExtended} = "AndroidDB::RCLayoutMagentaTVExt"; $data{RC_layout}{MagentaTVExtended} = "AndroidDB::RCLayoutMagentaTVExt";
} }
package AndroidDB; package AndroidDB;
@ -50,10 +51,12 @@ use strict;
use warnings; use warnings;
use SetExtensions; use SetExtensions;
use Storable qw(dclone); use Storable qw(dclone);
use GPUtils qw(:all); use GPUtils qw(:all);
BEGIN { BEGIN {
GP_Import(qw( GP_Import(
qw(
readingsSingleUpdate readingsSingleUpdate
readingsBulkUpdate readingsBulkUpdate
readingsBulkUpdateIfChanged readingsBulkUpdateIfChanged
@ -77,274 +80,298 @@ BEGIN {
modules modules
data data
init_done init_done
)) )
}; );
}
# Remote control presets # Remote control presets
my $PRESET = { my $PRESET = {
'MagentaTVStick' => { 'MagentaTVStick' => {
'ASSISTANT' => 'KEYCODE_ASSIST', 'ASSISTANT' => 'KEYCODE_ASSIST',
'APPS' => 'KEYCODE_ALL_APPS', 'APPS' => 'KEYCODE_ALL_APPS',
'BACK' => 'KEYCODE_BACK', 'BACK' => 'KEYCODE_BACK',
'EPG' => 'KEYCODE_TV_INPUT_HDMI_2', 'EPG' => 'KEYCODE_TV_INPUT_HDMI_2',
'HOME' => 'KEYCODE_HOME', 'HOME' => 'KEYCODE_HOME',
'LEFT' => 'KEYCODE_DPAD_LEFT', 'LEFT' => 'KEYCODE_DPAD_LEFT',
'RIGHT' => 'KEYCODE_DPAD_RIGHT', 'RIGHT' => 'KEYCODE_DPAD_RIGHT',
'UP' => 'KEYCODE_DPAD_UP', 'UP' => 'KEYCODE_DPAD_UP',
'DOWN' => 'KEYCODE_DPAD_DOWN', 'DOWN' => 'KEYCODE_DPAD_DOWN',
'INFO' => 'KEYCODE_INFO', 'INFO' => 'KEYCODE_INFO',
'MEGATHEK' => 'KEYCODE_TV_INPUT_HDMI_3', 'MEGATHEK' => 'KEYCODE_TV_INPUT_HDMI_3',
'MUTE' => 'KEYCODE_VOLUME_MUTE', 'MUTE' => 'KEYCODE_VOLUME_MUTE',
'OK' => 'KEYCODE_DPAD_CENTER', 'OK' => 'KEYCODE_DPAD_CENTER',
'PLAYPAUSE' => 'KEYCODE_MEDIA_PLAY_PAUSE', 'PLAYPAUSE' => 'KEYCODE_MEDIA_PLAY_PAUSE',
'POWER' => 'KEYCODE_POWER', 'POWER' => 'KEYCODE_POWER',
'PROG+' => 'KEYCODE_CHANNEL_UP', 'PROG+' => 'KEYCODE_CHANNEL_UP',
'PROG-' => 'KEYCODE_CHANNEL_DOWN', 'PROG-' => 'KEYCODE_CHANNEL_DOWN',
'RECORD' => 'KEYCODE_MEDIA_RECORD', 'RECORD' => 'KEYCODE_MEDIA_RECORD',
'SEARCH' => 'KEYCODE_TV_INPUT_HDMI_1', 'SEARCH' => 'KEYCODE_TV_INPUT_HDMI_1',
'STREAMINFO' => '--longpress,KEYCODE_INFO', 'STREAMINFO' => '--longpress,KEYCODE_INFO',
'TV' => 'KEYCODE_TV_INPUT_HDMI_4', 'TV' => 'KEYCODE_TV_INPUT_HDMI_4',
'VOL+' => 'KEYCODE_VOLUME_UP', 'VOL+' => 'KEYCODE_VOLUME_UP',
'VOL-' => 'KEYCODE_VOLUME_DOWN' 'VOL-' => 'KEYCODE_VOLUME_DOWN'
}, },
'MagentaOne' => { 'MagentaOne' => {
'ASSISTANT' => 'KEYCODE_ASSIST', 'ASSISTANT' => 'KEYCODE_ASSIST',
'APPS' => 'KEYCODE_ALL_APPS', 'APPS' => 'KEYCODE_ALL_APPS',
'BACK' => 'KEYCODE_BACK', 'BACK' => 'KEYCODE_BACK',
'EPG' => 'KEYCODE_TV_INPUT_HDMI_2', 'EPG' => 'KEYCODE_TV_INPUT_HDMI_2',
'HOME' => 'KEYCODE_HOME', 'HOME' => 'KEYCODE_HOME',
'LEFT' => 'KEYCODE_DPAD_LEFT', 'LEFT' => 'KEYCODE_DPAD_LEFT',
'RIGHT' => 'KEYCODE_DPAD_RIGHT', 'RIGHT' => 'KEYCODE_DPAD_RIGHT',
'UP' => 'KEYCODE_DPAD_UP', 'UP' => 'KEYCODE_DPAD_UP',
'DOWN' => 'KEYCODE_DPAD_DOWN', 'DOWN' => 'KEYCODE_DPAD_DOWN',
'INFO' => 'KEYCODE_INFO', 'INFO' => 'KEYCODE_INFO',
'MEGATHEK' => 'KEYCODE_TV_INPUT_HDMI_3', 'MEGATHEK' => 'KEYCODE_TV_INPUT_HDMI_3',
'MUTE' => 'KEYCODE_VOLUME_MUTE', 'MUTE' => 'KEYCODE_VOLUME_MUTE',
'OK' => 'KEYCODE_DPAD_CENTER', 'OK' => 'KEYCODE_DPAD_CENTER',
'PLAYPAUSE' => 'KEYCODE_MEDIA_PLAY_PAUSE', 'PLAYPAUSE' => 'KEYCODE_MEDIA_PLAY_PAUSE',
'POWER' => 'KEYCODE_POWER', 'POWER' => 'KEYCODE_POWER',
'PROG+' => 'KEYCODE_CHANNEL_UP', 'PROG+' => 'KEYCODE_CHANNEL_UP',
'PROG-' => 'KEYCODE_CHANNEL_DOWN', 'PROG-' => 'KEYCODE_CHANNEL_DOWN',
'RECORD' => 'KEYCODE_MEDIA_RECORD', 'RECORD' => 'KEYCODE_MEDIA_RECORD',
'SEARCH' => 'KEYCODE_TV_INPUT_HDMI_1', 'SEARCH' => 'KEYCODE_TV_INPUT_HDMI_1',
'STREAMINFO' => '--longpress,KEYCODE_INFO', 'STREAMINFO' => '--longpress,KEYCODE_INFO',
'TV' => 'KEYCODE_TV_INPUT_HDMI_4', 'TV' => 'KEYCODE_TV_INPUT_HDMI_4',
'VOL+' => 'KEYCODE_VOLUME_UP', 'VOL+' => 'KEYCODE_VOLUME_UP',
'VOL-' => 'KEYCODE_VOLUME_DOWN' 'VOL-' => 'KEYCODE_VOLUME_DOWN'
}, },
'AndroidTV' => { 'AndroidTV' => {
'APPS' => 'KEYCODE_ALL_APPS', 'APPS' => 'KEYCODE_ALL_APPS',
'BACK' => 'KEYCODE_BACK', 'BACK' => 'KEYCODE_BACK',
'EPG' => 'KEYCODE_GUIDE', 'EPG' => 'KEYCODE_GUIDE',
'HOME' => 'KEYCODE_HOME', 'HOME' => 'KEYCODE_HOME',
'LEFT' => 'KEYCODE_DPAD_LEFT', 'LEFT' => 'KEYCODE_DPAD_LEFT',
'RIGHT' => 'KEYCODE_DPAD_RIGHT', 'RIGHT' => 'KEYCODE_DPAD_RIGHT',
'UP' => 'KEYCODE_DPAD_UP', 'UP' => 'KEYCODE_DPAD_UP',
'DOWN' => 'KEYCODE_DPAD_DOWN', 'DOWN' => 'KEYCODE_DPAD_DOWN',
'INFO' => 'KEYCODE_INFO', 'INFO' => 'KEYCODE_INFO',
'MUTE' => 'KEYCODE_VOLUME_MUTE', 'MUTE' => 'KEYCODE_VOLUME_MUTE',
'OK' => 'KEYCODE_DPAD_CENTER', 'OK' => 'KEYCODE_DPAD_CENTER',
'PLAYPAUSE' => 'KEYCODE_MEDIA_PLAY_PAUSE', 'PLAYPAUSE' => 'KEYCODE_MEDIA_PLAY_PAUSE',
'POWER' => 'KEYCODE_POWER', 'POWER' => 'KEYCODE_POWER',
'PROG+' => 'KEYCODE_CHANNEL_UP', 'PROG+' => 'KEYCODE_CHANNEL_UP',
'PROG-' => 'KEYCODE_CHANNEL_DOWN', 'PROG-' => 'KEYCODE_CHANNEL_DOWN',
'RECORD' => 'KEYCODE_MEDIA_RECORD', 'RECORD' => 'KEYCODE_MEDIA_RECORD',
'SEARCH' => 'KEYCODE_SEARCH', 'SEARCH' => 'KEYCODE_SEARCH',
'VOL+' => 'KEYCODE_VOLUME_UP', 'VOL+' => 'KEYCODE_VOLUME_UP',
'VOL-' => 'KEYCODE_VOLUME_DOWN', 'VOL-' => 'KEYCODE_VOLUME_DOWN',
'RED' => 'KEYCODE_PROG_RED', 'RED' => 'KEYCODE_PROG_RED',
'GREEN' => 'KEYCODE_PROG_GREEN', 'GREEN' => 'KEYCODE_PROG_GREEN',
'BLUE' => 'KEYCODE_PROG_BLUE', 'BLUE' => 'KEYCODE_PROG_BLUE',
'YELLOW' => 'KEYCODE_PROG_YELLOW' 'YELLOW' => 'KEYCODE_PROG_YELLOW'
} }
}; };
# Command presets # Command presets
my $MACRO = { }; my $MACRO = { };
sub Define ($$$) sub Define ($$$) {
{ my ( $hash, $a, $h ) = @_;
my ($hash, $a, $h) = @_;
my $usage = "define $hash->{NAME} AndroidDB {NameOrIP[:Port]}"; my $usage = "define $hash->{NAME} AndroidDB {NameOrIP[:Port]}";
return $usage if (scalar(@$a) < 3); return $usage if ( scalar(@$a) < 3 );
# Set parameters # Set parameters
my ($devName, $devPort) = split (':', $$a[2]); my ( $devName, $devPort ) = split( ':', $$a[2] );
$hash->{ADBDevice} = $devName.':'.($devPort // '5555'); $hash->{ADBDevice} = $devName . ':' . ( $devPort // '5555' );
AssignIoPort ($hash); AssignIoPort($hash);
$attr{$hash->{NAME}}{webCmd} = 'remoteControl'; $attr{ $hash->{NAME} }{webCmd} = 'remoteControl';
# Clone predefined presets and macros # Clone predefined presets and macros
$hash->{adb}{preset} = dclone $PRESET; $hash->{adb}{preset} = dclone $PRESET;
$hash->{adb}{macro} = dclone $MACRO; $hash->{adb}{macro} = dclone $MACRO;
InitAfterStart ($hash) if ($init_done); InitAfterStart($hash) if ($init_done);
return undef; return undef;
} }
sub InitAfterStart ($) sub InitAfterStart ($) {
{ my ($hash) = @_;
my ($hash) = @_;
my @presets = map { $_ eq '_custom_' ? () : $_ } keys %{$hash->{adb}{preset}};
if (scalar(@presets) > 0) { my @presets =
my $attrPreset = 'preset:select,'.join(',',@presets); map { $_ eq '_custom_' ? () : $_ } keys %{ $hash->{adb}{preset} };
my $attributes = $modules{AndroidDB}{AttrList};
$attributes =~ s/preset/$attrPreset/; if ( scalar(@presets) > 0 ) {
setDevAttrList ($hash->{NAME}, $attributes); my $attrPreset = 'preset:select,' . join( ',', @presets );
} my $attributes = $modules{AndroidDB}{AttrList};
$attributes =~ s/preset/$attrPreset/;
setDevAttrList( $hash->{NAME}, $attributes );
}
} }
sub Undef ($$) sub Undef ($$) {
{ my ( $hash, $name ) = @_;
my ($hash, $name) = @_;
AndroidDBHost::Disconnect($hash);
AndroidDBHost::Disconnect ($hash);
return undef; return undef;
} }
sub Shutdown ($)
{ sub Shutdown ($) {
my ($hash) = @_; my ($hash) = @_;
AndroidDBHost::Disconnect ($hash); AndroidDBHost::Disconnect($hash);
} }
sub Set ($@) sub Set ($@) {
{ my ( $hash, $a, $h ) = @_;
my ($hash, $a, $h) = @_;
my $name = shift @$a;
my $opt = shift @$a // return 'No set command specified';
# my $name = shift @$a;
# Preprare list of available commands my $opt = shift @$a // return 'No set command specified';
#
# Standard commands
my $options = 'exportPresets reboot rollHorz rollVert sendKey sendNumKeys sendText shell tap';
# Add remote control key presets to command remoteControl
my @presetList = ();
my $preset = AttrVal ($name, 'preset', '');
push @presetList, sort keys %{$hash->{adb}{preset}{$preset}} if ($preset ne '' && exists($hash->{adb}{preset}{$preset}));
push @presetList, sort keys %{$hash->{adb}{preset}{_custom_}} if (exists($hash->{adb}{preset}{_custom_}));
my %e1;
$options .= ' remoteControl:'.join(',', sort grep { !$e1{$_}++ } @presetList) if (scalar(@presetList) > 0);
# Add remote control layouts to command createRemote
my @layouts = keys %{$data{RC_layout}};
$options .= ' createRemote:'.join(',', sort @layouts) if (scalar(@layouts) > 0);
# Add command macros to command list
my @macroList = ();
push @macroList, sort keys %{$hash->{adb}{macro}{$preset}} if ($preset ne '' && exists($hash->{adb}{macro}{$preset}));
push @macroList, sort keys %{$hash->{adb}{macro}{_custom_}} if (exists($hash->{adb}{macro}{_custom_}));
my %e2;
$options .= ' '.join(' ', sort grep { !$e2{$_}++ } @macroList) if (scalar(@macroList) > 0);
my $lcopt = lc($opt); #
# Preprare list of available commands
#
if ($lcopt eq 'sendkey') { # Standard commands
return "Usage: set $name $opt [--longpress] KeyCode [...]" if (scalar(@$a) == 0); my $options =
return "Only one KeyCode allowed when option '--longpress' is specified" 'exportPresets reboot rollHorz rollVert sendKey sendNumKeys sendText shell tap';
if ($$a[0] eq '--longpress' && scalar(@$a) > 2);
my ($rc, $result, $error) = AndroidDBHost::Run ($hash, 'shell', '.*', 'input', 'keyevent', @$a); # Add remote control key presets to command remoteControl
return $error if ($rc == 0); my @presetList = ();
} my $preset = AttrVal( $name, 'preset', '' );
elsif ($lcopt eq 'sendnumkeys') { push @presetList, sort keys %{ $hash->{adb}{preset}{$preset} }
my $number = shift @$a // return "Usage: set $name $opt Number"; if ( $preset ne '' && exists( $hash->{adb}{preset}{$preset} ) );
return 'Parameter Number must be in range 1-9999' if ($number !~ /^[0-9]+$/ || $number < 1 || $number > 9999); push @presetList, sort keys %{ $hash->{adb}{preset}{_custom_} }
my ($rc, $result, $error) = AndroidDBHost::Run ($hash, 'shell', '.*', 'input', 'text', $number); if ( exists( $hash->{adb}{preset}{_custom_} ) );
return $error if ($rc == 0); my %e1;
} $options .=
elsif ($lcopt eq 'sendtext') { ' remoteControl:' . join( ',', sort grep { !$e1{$_}++ } @presetList )
return "Usage: set $name $opt Text" if (scalar(@$a) < 1); if ( scalar(@presetList) > 0 );
my ($rc, $result, $error) = AndroidDBHost::Run ($hash, 'shell', '.*', 'input', 'text', join(' ',@$a));
return $error if ($rc == 0); # Add remote control layouts to command createRemote
} my @layouts = keys %{ $data{RC_layout} };
elsif ($lcopt eq 'reboot') { $options .= ' createRemote:' . join( ',', sort @layouts )
my ($rc, $result, $error) = AndroidDBHost::Run ($hash, $opt); if ( scalar(@layouts) > 0 );
return $error if ($rc == 0);
} # Add command macros to command list
elsif ($lcopt eq 'shell') { my @macroList = ();
return "Usage: set $name $opt ShellCommand" if (scalar(@$a) == 0); push @macroList, sort keys %{ $hash->{adb}{macro}{$preset} }
my ($rc, $result, $error) = AndroidDBHost::Run ($hash, $opt, '.*', @$a); if ( $preset ne '' && exists( $hash->{adb}{macro}{$preset} ) );
return $error if ($rc == 0); push @macroList, sort keys %{ $hash->{adb}{macro}{_custom_} }
my $createReadings = AttrVal ($name, 'createReadings', ''); if ( exists( $hash->{adb}{macro}{_custom_} ) );
return $result if ($createReadings eq '' || $createReadings !~ /$createReadings/); my %e2;
UpdateReadings ($hash, $result); $options .= ' ' . join( ' ', sort grep { !$e2{$_}++ } @macroList )
} if ( scalar(@macroList) > 0 );
elsif ($lcopt eq 'remotecontrol') {
my $macroName = shift @$a // return "Usage: set $name $opt MacroName"; my $lcopt = lc($opt);
$preset = '_custom_' if (exists($hash->{adb}{preset}{_custom_}) && exists($hash->{adb}{preset}{_custom_}{$macroName}));
return "Preset and/or macro $macroName not defined in preset $preset" if ( $lcopt eq 'sendkey' ) {
if ($preset eq '' || !exists($hash->{adb}{preset}{$preset}{$macroName})); return "Usage: set $name $opt [--longpress] KeyCode [...]"
my ($rc, $result, $error) = AndroidDBHost::Run ($hash, 'shell', '.*', 'input', 'keyevent', if ( scalar(@$a) == 0 );
split (',', $hash->{adb}{preset}{$preset}{$macroName})); return "Only one KeyCode allowed when option '--longpress' is specified"
return $error if ($rc == 0); if ( $$a[0] eq '--longpress' && scalar(@$a) > 2 );
} my ( $rc, $result, $error ) = AndroidDBHost::Run( $hash, 'shell', '.*', 'input', 'keyevent', @$a );
elsif ($lcopt eq 'tap') { return $error if ( $rc == 0 );
my ($x, $y) = @$a; }
return "Usage: set $name $opt tap X Y" if (!defined($y)); elsif ( $lcopt eq 'sendnumkeys' ) {
my ($rc, $result, $error) = AndroidDBHost::Run ($hash, 'shell', '.*', 'input', 'tap', $x, $y); my $number = shift @$a // return "Usage: set $name $opt Number";
return $error if ($rc == 0); return 'Parameter Number must be in range 1-9999'
} if ( $number !~ /^[0-9]+$/ || $number < 1 || $number > 9999 );
elsif ($lcopt eq 'rollhorz' || $lcopt eq 'rollvert') { my ( $rc, $result, $error ) = AndroidDBHost::Run( $hash, 'shell', '.*', 'input', 'text', $number );
my $delta = shift @$a // return "Usage: set $name $opt Delta"; return $error if ( $rc == 0 );
my ($dx, $dy) = $opt eq 'rollhorz' ? ($delta, 0) : (0, $delta); }
my ($rc, $result, $error) = AndroidDBHost::Run ($hash, 'shell', '.*', 'input', 'roll', $dx, $dy); elsif ( $lcopt eq 'sendtext' ) {
return $error if ($rc == 0); return "Usage: set $name $opt Text" if ( scalar(@$a) < 1 );
} my ( $rc, $result, $error ) = AndroidDBHost::Run( $hash, 'shell', '.*', 'input', 'text', join( ' ', @$a ) );
elsif ($lcopt eq 'createremote') { return $error if ( $rc == 0 );
my $layout = shift @$a // return "Usage: set $name $opt LayoutName"; }
my $rcName = $name.'_RC'; elsif ( $lcopt eq 'reboot' ) {
return "$name: Can't create remotecontrol device $rcName" my ( $rc, $result, $error ) = AndroidDBHost::Run( $hash, $opt );
if (CommandDefine (undef, "$rcName remotecontrol")); return $error if ( $rc == 0 );
Log3 $name, 2, "$name: Created remotecontrol device $rcName"; }
return "$name: Can't select layout $layout for remotecontrol device $rcName" elsif ( $lcopt eq 'shell' ) {
if (CommandSet (undef, "$rcName layout $layout")); return "Usage: set $name $opt ShellCommand" if ( scalar(@$a) == 0 );
Log3 $name, 2, "Selected layout $layout for $rcName"; my $shellCommand = @$a[0];
my $room = AttrVal ($name, 'room', ''); my ( $rc, $result, $error ) = AndroidDBHost::Run( $hash, $opt, '.*', @$a );
if ($room ne '') { return $error if ( $rc == 0 );
Log3 $name, 2, "$name: Assigning $rcName to room $room"; my $createReadings = AttrVal( $name, 'createReadings', '' );
CommandAttr (undef, "$rcName room $room"); return $result
} if ( $createReadings eq '' || $shellCommand !~ /$createReadings/ );
CommandSet (undef, "$rcName makenotify $name"); UpdateReadings( $hash, $result );
CommandAttr (undef, "$name group $name"); }
CommandAttr (undef, "$rcName group $name"); elsif ( $lcopt eq 'remotecontrol' ) {
Log3 $name, 2, "Created notify device notify_$rcName"; my $macroName = shift @$a // return "Usage: set $name $opt MacroName";
} $preset = '_custom_'
elsif ($lcopt eq 'exportpresets') { if (exists( $hash->{adb}{preset}{_custom_} )
my $filename = shift @$a // return "Usage: set $name $opt Filename"; && exists( $hash->{adb}{preset}{_custom_}{$macroName} ) );
my $rc = ExportPresets ($hash, $filename); return "Preset and/or macro $macroName not defined in preset $preset"
return "Error while saving presets to file $filename" if ($rc == 0); if ( $preset eq ''
return "Presets saved to file $filename"; || !exists( $hash->{adb}{preset}{$preset}{$macroName} ) );
} my ( $rc, $result, $error ) = AndroidDBHost::Run( $hash, 'shell', '.*', 'input', 'keyevent',
elsif (exists($hash->{adb}{macro}{_custom_}) && exists($hash->{adb}{macro}{_custom_}{$opt})) { split( ',', $hash->{adb}{preset}{$preset}{$macroName} ) );
my ($args, $pars) = parseParams ($hash->{adb}{macro}{_custom_}{$opt}); return $error if ( $rc == 0 );
my $cmd = shift @$args; }
my ($rc, $result, $error) = AndroidDBHost::Run ($hash, $cmd, '.*', @$args); elsif ( $lcopt eq 'tap' ) {
return $rc == 0 ? $error : $result; my ( $x, $y ) = @$a;
} return "Usage: set $name $opt tap X Y" if ( !defined($y) );
elsif ($preset ne '' && exists($hash->{adb}{macro}{$preset}) && exists($hash->{adb}{macro}{$preset}{$opt})) { my ( $rc, $result, $error ) =
my ($args, $pars) = parseParams ($hash->{adb}{macro}{$preset}{$opt}); AndroidDBHost::Run( $hash, 'shell', '.*', 'input', 'tap', $x, $y );
my $cmd = shift @$args; return $error if ( $rc == 0 );
my ($rc, $result, $error) = AndroidDBHost::Run ($hash, $cmd, '.*', @$args); }
return $rc == 0 ? $error : $result; elsif ( $lcopt eq 'rollhorz' || $lcopt eq 'rollvert' ) {
} my $delta = shift @$a // return "Usage: set $name $opt Delta";
else { my ( $dx, $dy ) = $opt eq 'rollhorz' ? ( $delta, 0 ) : ( 0, $delta );
return "Unknown argument $opt, choose one of $options"; my ( $rc, $result, $error ) = AndroidDBHost::Run( $hash, 'shell', '.*', 'input', 'roll', $dx, $dy );
} return $error if ( $rc == 0 );
}
elsif ( $lcopt eq 'createremote' ) {
my $layout = shift @$a // return "Usage: set $name $opt LayoutName";
my $rcName = $name . '_RC';
return "$name: Can't create remotecontrol device $rcName"
if ( CommandDefine( undef, "$rcName remotecontrol" ) );
Log3 $name, 2, "$name: Created remotecontrol device $rcName";
return
"$name: Can't select layout $layout for remotecontrol device $rcName"
if ( CommandSet( undef, "$rcName layout $layout" ) );
Log3 $name, 2, "Selected layout $layout for $rcName";
my $room = AttrVal( $name, 'room', '' );
if ( $room ne '' ) {
Log3 $name, 2, "$name: Assigning $rcName to room $room";
CommandAttr( undef, "$rcName room $room" );
}
CommandSet( undef, "$rcName makenotify $name" );
CommandAttr( undef, "$name group $name" );
CommandAttr( undef, "$rcName group $name" );
Log3 $name, 2, "Created notify device notify_$rcName";
}
elsif ( $lcopt eq 'exportpresets' ) {
my $filename = shift @$a // return "Usage: set $name $opt Filename";
my $rc = ExportPresets( $hash, $filename );
return "Error while saving presets to file $filename" if ( $rc == 0 );
return "Presets saved to file $filename";
}
elsif ( exists( $hash->{adb}{macro}{_custom_} )
&& exists( $hash->{adb}{macro}{_custom_}{$opt} ) )
{
my ( $args, $pars ) = parseParams( $hash->{adb}{macro}{_custom_}{$opt} );
my $cmd = shift @$args;
my ( $rc, $result, $error ) = AndroidDBHost::Run( $hash, $cmd, '.*', @$args );
return $rc == 0 ? $error : $result;
}
elsif ( $preset ne ''
&& exists( $hash->{adb}{macro}{$preset} )
&& exists( $hash->{adb}{macro}{$preset}{$opt} ) )
{
my ( $args, $pars ) = parseParams( $hash->{adb}{macro}{$preset}{$opt} );
my $cmd = shift @$args;
my ( $rc, $result, $error ) = AndroidDBHost::Run( $hash, $cmd, '.*', @$args );
return $rc == 0 ? $error : $result;
}
else {
return "Unknown argument $opt, choose one of $options";
}
} }
sub Get ($@) sub Get ($@)
{ {
my ($hash, $a, $h) = @_; my ($hash, $a, $h) = @_;
@ -389,44 +416,45 @@ sub Get ($@)
sub Attr ($@) sub Attr ($@)
{ {
my ($cmd, $name, $attrName, $attrVal) = @_; my ($cmd, $name, $attrName, $attrVal) = @_;
my $hash = $defs{$name}; my $hash = $defs{$name};
if ($cmd eq 'set') { if ($cmd eq 'set') {
if ($attrName eq 'macros') { if ($attrName eq 'macros') {
delete $hash->{adb}{preset}{_custom_} if (exists($hash->{adb}{preset}{_custom_})); delete $hash->{adb}{preset}{_custom_} if (exists($hash->{adb}{preset}{_custom_}));
delete $hash->{adb}{macro}{_custom_} if (exists($hash->{adb}{macro}{_custom_})); delete $hash->{adb}{macro}{_custom_} if (exists($hash->{adb}{macro}{_custom_}));
foreach my $macroDef (split /;/, $attrVal) { foreach my $macroDef (split /;/, $attrVal) {
my ($macroName, $macroPar) = split (':', $macroDef, 2); my ($macroName, $macroPar) = split (':', $macroDef, 2);
if (!defined($macroDef)) { if (!defined($macroDef)) {
Log3 $name, 2, "Missing defintion for macro $macroName"; Log3 $name, 2, "Missing defintion for macro $macroName";
return "Missing definition for macro $macroName"; return "Missing definition for macro $macroName";
}
if ($macroPar =~ /^[0-9]+/ || $macroPar =~ /^KEYCODE_/) {
$hash->{adb}{preset}{_custom_}{$macroName} = $macroPar;
}
else {
$hash->{adb}{macro}{_custom_}{$macroName} = $macroPar;
}
} }
} if ($macroPar =~ /^[0-9]+/ || $macroPar =~ /^KEYCODE_/) {
elsif ($attrName eq 'presetFile') { $hash->{adb}{preset}{_custom_}{$macroName} = $macroPar;
if (!LoadPresets ($hash, $attrVal)) {
return "Cannot load presets from file $attrVal";
} }
} else {
elsif ($attrName eq 'connect') { $hash->{adb}{macro}{_custom_}{$macroName} = $macroPar;
AndroidDBHost::Connect ($hash) if (!$init_done && $attrVal eq '1'); }
} }
} }
elsif ($cmd eq 'del') { elsif ($attrName eq 'presetFile') {
delete $hash->{adb}{preset}{_custom_} if (exists($hash->{adb}{preset}{_custom_})); if (!LoadPresets ($hash, $attrVal)) {
delete $hash->{adb}{macro}{_custom_} if (exists($hash->{adb}{macro}{_custom_})); return "Cannot load presets from file $attrVal";
} }
}
elsif ($attrName eq 'connect') {
AndroidDBHost::Connect ($hash) if (!$init_done && $attrVal eq '1');
}
}
elsif ($cmd eq 'del') {
delete $hash->{adb}{preset}{_custom_} if (exists($hash->{adb}{preset}{_custom_}));
delete $hash->{adb}{macro}{_custom_} if (exists($hash->{adb}{macro}{_custom_}));
}
return undef; return undef;
} }
############################################################################## ##############################################################################
# Load macro definitions from file # Load macro definitions from file
# File format: # File format:

View File

@ -37,6 +37,7 @@ sub AndroidDBHost_Initialize ($)
$hash->{ShutdownFn} = "AndroidDBHost::Shutdown"; $hash->{ShutdownFn} = "AndroidDBHost::Shutdown";
$hash->{parseParams} = 1; $hash->{parseParams} = 1;
$hash->{AttrList} = $readingFnAttributes;
} }
package AndroidDBHost; package AndroidDBHost;
@ -53,13 +54,14 @@ use IPC::Open3;
use GPUtils qw(:all); use GPUtils qw(:all);
BEGIN { BEGIN {
GP_Import(qw( GP_Import(
qw(
readingsSingleUpdate readingsSingleUpdate
readingsBulkUpdate readingsBulkUpdate
readingsBulkUpdateIfChanged readingsBulkUpdateIfChanged
readingsBeginUpdate readingsBeginUpdate
readingsEndUpdate readingsEndUpdate
devspec2array devspec2array
Log3 Log3
AttrVal AttrVal
ReadingsVal ReadingsVal
@ -69,32 +71,33 @@ BEGIN {
deviceEvents deviceEvents
gettimeofday gettimeofday
defs defs
)) )
)
}; };
sub Define ($$$) sub Define ($$$)
{ {
my ($hash, $a, $h) = @_; my ($hash, $a, $h) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $usage = "define $name AndroidDB [server={host}[:{port}]] [adb={path}]"; my $usage = "define $name AndroidDB [server={host}[:{port}]] [adb={path}]";
# Set parameters # Set parameters
my ($host, $port) = split (':', $h->{ADB} // 'localhost:5037'); my ($host, $port) = split (':', $h->{ADB} // 'localhost:5037');
$hash->{adb}{host} = $host; $hash->{adb}{host} = $host;
$hash->{adb}{port} = $port // 5037; $hash->{adb}{port} = $port // 5037;
$hash->{adb}{cmd} = $h->{adb} // '/usr/bin/adb'; $hash->{adb}{cmd} = $h->{adb} // '/usr/bin/adb';
$hash->{Clients} = ':AndroidDB:'; $hash->{Clients} = ':AndroidDB:';
$hash->{NOTIFYDEV} = 'global,TYPE=AndroidDBHost'; $hash->{NOTIFYDEV} = 'global,TYPE=AndroidDBHost';
# Check path and rights of platform tools # Check path and rights of platform tools
return "ADB command not found or is not executable in $hash->{adb}{pt}" if (! -x "$hash->{adb}{cmd}"); return "ADB command not found or is not executable in $hash->{adb}{pt}" if (! -x "$hash->{adb}{cmd}");
# Check ADB settings, start adb server # Check ADB settings, start adb server
CheckADBServer ($hash); CheckADBServer ($hash);
return "ADB server not running or cannot be started on host $hash->{adb}{host}" if ($hash->{STATE} eq 'stopped'); return "ADB server not running or cannot be started on host $hash->{adb}{host}" if ($hash->{STATE} eq 'stopped');
return undef; return undef;
} }