2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-02-27 20:34:52 +00:00

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

@ -1,5 +1,6 @@
# 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.
- bugfix: 89_AndroidDB: Fixed bug in attribute createReadings
- feature: 89_AndroidDB: Added creation of readings for shell commands
- feature: 98_archetype: new option for FILTER in actual_.* attributes
- change: 98_archetype: now packaged, extend commandref

@ -4,7 +4,7 @@
#
# 89_AndroidDB
#
# Version 0.8
# Version 0.9
#
# FHEM Integration for Android Devices
#
@ -25,8 +25,7 @@ use strict;
use warnings;
use SetExtensions;
sub AndroidDB_Initialize ($)
{
sub AndroidDB_Initialize ($) {
my ($hash) = @_;
$hash->{DefFn} = "AndroidDB::Define";
@ -37,7 +36,9 @@ sub AndroidDB_Initialize ($)
$hash->{ShutdownFn} = "AndroidDB::Shutdown";
$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}{MagentaOne} = "AndroidDB::RCLayoutMagentaOne";
@ -52,8 +53,10 @@ use SetExtensions;
use Storable qw(dclone);
use GPUtils qw(:all);
BEGIN {
GP_Import(qw(
GP_Import(
qw(
readingsSingleUpdate
readingsBulkUpdate
readingsBulkUpdateIfChanged
@ -77,8 +80,10 @@ BEGIN {
modules
data
init_done
))
};
)
);
}
# Remote control presets
my $PRESET = {
@ -159,11 +164,11 @@ my $PRESET = {
}
};
# Command presets
my $MACRO = { };
sub Define ($$$)
{
sub Define ($$$) {
my ( $hash, $a, $h ) = @_;
my $usage = "define $hash->{NAME} AndroidDB {NameOrIP[:Port]}";
@ -187,11 +192,11 @@ sub Define ($$$)
return undef;
}
sub InitAfterStart ($)
{
sub InitAfterStart ($) {
my ($hash) = @_;
my @presets = map { $_ eq '_custom_' ? () : $_ } keys %{$hash->{adb}{preset}};
my @presets =
map { $_ eq '_custom_' ? () : $_ } keys %{ $hash->{adb}{preset} };
if ( scalar(@presets) > 0 ) {
my $attrPreset = 'preset:select,' . join( ',', @presets );
@ -201,8 +206,7 @@ sub InitAfterStart ($)
}
}
sub Undef ($$)
{
sub Undef ($$) {
my ( $hash, $name ) = @_;
AndroidDBHost::Disconnect($hash);
@ -210,15 +214,14 @@ sub Undef ($$)
return undef;
}
sub Shutdown ($)
{
sub Shutdown ($) {
my ($hash) = @_;
AndroidDBHost::Disconnect($hash);
}
sub Set ($@)
{
sub Set ($@) {
my ( $hash, $a, $h ) = @_;
my $name = shift @$a;
@ -229,31 +232,41 @@ sub Set ($@)
#
# Standard commands
my $options = 'exportPresets reboot rollHorz rollVert sendKey sendNumKeys sendText shell tap';
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_}));
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);
$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);
$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_}));
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);
$options .= ' ' . join( ' ', sort grep { !$e2{$_}++ } @macroList )
if ( scalar(@macroList) > 0 );
my $lcopt = lc($opt);
if ( $lcopt eq 'sendkey' ) {
return "Usage: set $name $opt [--longpress] KeyCode [...]" if (scalar(@$a) == 0);
return "Usage: set $name $opt [--longpress] KeyCode [...]"
if ( scalar(@$a) == 0 );
return "Only one KeyCode allowed when option '--longpress' is specified"
if ( $$a[0] eq '--longpress' && scalar(@$a) > 2 );
my ( $rc, $result, $error ) = AndroidDBHost::Run( $hash, 'shell', '.*', 'input', 'keyevent', @$a );
@ -261,7 +274,8 @@ sub Set ($@)
}
elsif ( $lcopt eq 'sendnumkeys' ) {
my $number = shift @$a // return "Usage: set $name $opt Number";
return 'Parameter Number must be in range 1-9999' if ($number !~ /^[0-9]+$/ || $number < 1 || $number > 9999);
return 'Parameter Number must be in range 1-9999'
if ( $number !~ /^[0-9]+$/ || $number < 1 || $number > 9999 );
my ( $rc, $result, $error ) = AndroidDBHost::Run( $hash, 'shell', '.*', 'input', 'text', $number );
return $error if ( $rc == 0 );
}
@ -276,17 +290,22 @@ sub Set ($@)
}
elsif ( $lcopt eq 'shell' ) {
return "Usage: set $name $opt ShellCommand" if ( scalar(@$a) == 0 );
my $shellCommand = @$a[0];
my ( $rc, $result, $error ) = AndroidDBHost::Run( $hash, $opt, '.*', @$a );
return $error if ( $rc == 0 );
my $createReadings = AttrVal( $name, 'createReadings', '' );
return $result if ($createReadings eq '' || $createReadings !~ /$createReadings/);
return $result
if ( $createReadings eq '' || $shellCommand !~ /$createReadings/ );
UpdateReadings( $hash, $result );
}
elsif ( $lcopt eq 'remotecontrol' ) {
my $macroName = shift @$a // return "Usage: set $name $opt MacroName";
$preset = '_custom_' if (exists($hash->{adb}{preset}{_custom_}) && exists($hash->{adb}{preset}{_custom_}{$macroName}));
$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 ($preset eq '' || !exists($hash->{adb}{preset}{$preset}{$macroName}));
if ( $preset eq ''
|| !exists( $hash->{adb}{preset}{$preset}{$macroName} ) );
my ( $rc, $result, $error ) = AndroidDBHost::Run( $hash, 'shell', '.*', 'input', 'keyevent',
split( ',', $hash->{adb}{preset}{$preset}{$macroName} ) );
return $error if ( $rc == 0 );
@ -294,7 +313,8 @@ sub Set ($@)
elsif ( $lcopt eq 'tap' ) {
my ( $x, $y ) = @$a;
return "Usage: set $name $opt tap X Y" if ( !defined($y) );
my ($rc, $result, $error) = AndroidDBHost::Run ($hash, 'shell', '.*', 'input', 'tap', $x, $y);
my ( $rc, $result, $error ) =
AndroidDBHost::Run( $hash, 'shell', '.*', 'input', 'tap', $x, $y );
return $error if ( $rc == 0 );
}
elsif ( $lcopt eq 'rollhorz' || $lcopt eq 'rollvert' ) {
@ -309,7 +329,8 @@ sub Set ($@)
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"
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', '' );
@ -328,13 +349,18 @@ sub Set ($@)
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})) {
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})) {
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 );
@ -345,6 +371,7 @@ sub Set ($@)
}
}
sub Get ($@)
{
my ($hash, $a, $h) = @_;
@ -427,6 +454,7 @@ sub Attr ($@)
return undef;
}
##############################################################################
# Load macro definitions from file
# File format:

@ -37,6 +37,7 @@ sub AndroidDBHost_Initialize ($)
$hash->{ShutdownFn} = "AndroidDBHost::Shutdown";
$hash->{parseParams} = 1;
$hash->{AttrList} = $readingFnAttributes;
}
package AndroidDBHost;
@ -53,7 +54,8 @@ use IPC::Open3;
use GPUtils qw(:all);
BEGIN {
GP_Import(qw(
GP_Import(
qw(
readingsSingleUpdate
readingsBulkUpdate
readingsBulkUpdateIfChanged
@ -69,7 +71,8 @@ BEGIN {
deviceEvents
gettimeofday
defs
))
)
)
};
sub Define ($$$)