review codeparts

This commit is contained in:
Marko Oldenburg 2022-01-09 10:56:48 +01:00
parent 89625422f7
commit 0bc36c6c74
5 changed files with 2550 additions and 1960 deletions

View File

@ -37,36 +37,13 @@ use warnings;
use JSON qw(decode_json); use JSON qw(decode_json);
use Encode qw(encode_utf8); use Encode qw(encode_utf8);
my $version = "1.0.3"; my $version = "1.0.3";
sub HEOSGroup_Initialize {
my $hash = shift;
# Declare functions
sub HEOSGroup_Initialize($);
sub HEOSGroup_Define($$);
sub HEOSGroup_Undef($$);
sub HEOSGroup_Attr(@);
sub HEOSGroup_Notify($$);
sub HEOSGroup_Parse($$);
sub HEOSGroup_WriteReadings($$);
sub HEOSGroup_Set($$@);
sub HEOSGroup_PreProcessingReadings($$);
sub HEOSGroup_GetGroupInfo($);
sub HEOSGroup_GetGroupVolume($);
sub HEOSGroup_GetGroupMute($);
sub HEOSGroup_Initialize($) {
my ($hash) = @_;
$hash->{Match} = '.*{"command":."group.*|.*{"command":."event\/group.*'; $hash->{Match} = '.*{"command":."group.*|.*{"command":."event\/group.*';
# Provider # Provider
$hash->{SetFn} = "HEOSGroup_Set"; $hash->{SetFn} = "HEOSGroup_Set";
$hash->{DefFn} = "HEOSGroup_Define"; $hash->{DefFn} = "HEOSGroup_Define";
@ -74,9 +51,7 @@ sub HEOSGroup_Initialize($) {
$hash->{NotifyFn} = "HEOSGroup_Notify"; $hash->{NotifyFn} = "HEOSGroup_Notify";
$hash->{AttrFn} = "HEOSGroup_Attr"; $hash->{AttrFn} = "HEOSGroup_Attr";
$hash->{ParseFn} = "HEOSGroup_Parse"; $hash->{ParseFn} = "HEOSGroup_Parse";
$hash->{AttrList} = "IODev ". $hash->{AttrList} = "IODev " . "disable:1 " . $readingFnAttributes;
"disable:1 ".
$readingFnAttributes;
foreach my $d ( sort keys %{ $modules{HEOSGroup}{defptr} } ) { foreach my $d ( sort keys %{ $modules{HEOSGroup}{defptr} } ) {
@ -85,15 +60,15 @@ sub HEOSGroup_Initialize($) {
} }
} }
sub HEOSGroup_Define($$) { sub HEOSGroup_Define {
my ( $hash, $def ) = @_; my ( $hash, $def ) = @_;
my @a = split( "[ \t]+", $def ); my @a = split( "[ \t]+", $def );
splice( @a, 1, 1 ); splice( @a, 1, 1 );
my $iodev; my $iodev;
my $i = 0; my $i = 0;
foreach my $param (@a) { foreach my $param (@a) {
if ( $param =~ m/IODev=([^\s]*)/ ) { if ( $param =~ m/IODev=([^\s]*)/ ) {
@ -116,9 +91,11 @@ sub HEOSGroup_Define($$) {
if ( defined( $hash->{IODev}->{NAME} ) ) { if ( defined( $hash->{IODev}->{NAME} ) ) {
Log3 $name, 3, "HEOSGroup ($name) - I/O device is " . $hash->{IODev}->{NAME}; Log3 $name, 3,
"HEOSGroup ($name) - I/O device is " . $hash->{IODev}->{NAME};
} else { }
else {
Log3 $name, 1, "HEOSGroup ($name) - no I/O device"; Log3 $name, 1, "HEOSGroup ($name) - no I/O device";
} }
@ -129,25 +106,36 @@ sub HEOSGroup_Define($$) {
$code = $iodev . "-" . $code if ( defined($iodev) ); $code = $iodev . "-" . $code if ( defined($iodev) );
my $d = $modules{HEOSGroup}{defptr}{$code}; my $d = $modules{HEOSGroup}{defptr}{$code};
return "HEOSGroup device $hash->{GID} on HEOSMaster $iodev already defined as $d->{NAME}." return
if( defined($d) && $d->{IODev} == $hash->{IODev} && $d->{NAME} ne $name ); "HEOSGroup device $hash->{GID} on HEOSMaster $iodev already defined as $d->{NAME}."
if ( defined($d)
&& $d->{IODev} == $hash->{IODev}
&& $d->{NAME} ne $name );
Log3 $name, 3, "HEOSGroup ($name) - defined with Code: $code"; Log3 $name, 3, "HEOSGroup ($name) - defined with Code: $code";
$attr{$name}{room} = "HEOS" if ( !defined( $attr{$name}{room} ) ); $attr{$name}{room} = "HEOS" if ( !defined( $attr{$name}{room} ) );
$attr{$name}{devStateIcon} = "on:10px-kreis-gruen off:10px-kreis-rot" if( !defined( $attr{$name}{devStateIcon} ) ); $attr{$name}{devStateIcon} = "on:10px-kreis-gruen off:10px-kreis-rot"
if ( !defined( $attr{$name}{devStateIcon} ) );
if ($init_done) { if ($init_done) {
InternalTimer( gettimeofday()+int(rand(2)), "HEOSGroup_GetGroupInfo", $hash, 0 ); InternalTimer( gettimeofday() + int( rand(2) ),
InternalTimer( gettimeofday()+int(rand(4)), "HEOSGroup_GetGroupVolume", $hash, 0 ); "HEOSGroup_GetGroupInfo", $hash, 0 );
InternalTimer( gettimeofday()+int(rand(6)), "HEOSGroup_GetGroupMute", $hash, 0 ); InternalTimer( gettimeofday() + int( rand(4) ),
"HEOSGroup_GetGroupVolume", $hash, 0 );
InternalTimer( gettimeofday() + int( rand(6) ),
"HEOSGroup_GetGroupMute", $hash, 0 );
} else { }
else {
InternalTimer( gettimeofday()+15+int(rand(2)), "HEOSGroup_GetGroupInfo", $hash, 0 ); InternalTimer( gettimeofday() + 15 + int( rand(2) ),
InternalTimer( gettimeofday()+15+int(rand(4)), "HEOSGroup_GetGroupVolume", $hash, 0 ); "HEOSGroup_GetGroupInfo", $hash, 0 );
InternalTimer( gettimeofday()+15+int(rand(6)), "HEOSGroup_GetGroupMute", $hash, 0 ); InternalTimer( gettimeofday() + 15 + int( rand(4) ),
"HEOSGroup_GetGroupVolume", $hash, 0 );
InternalTimer( gettimeofday() + 15 + int( rand(6) ),
"HEOSGroup_GetGroupMute", $hash, 0 );
} }
readingsBeginUpdate($hash); readingsBeginUpdate($hash);
@ -157,38 +145,39 @@ sub HEOSGroup_Define($$) {
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
$modules{HEOSGroup}{defptr}{$code} = $hash; $modules{HEOSGroup}{defptr}{$code} = $hash;
return undef;
return;
} }
sub HEOSGroup_Undef($$) { sub HEOSGroup_Undef {
my ( $hash, $arg ) = @_; my ( $hash, $arg ) = @_;
my $name = $hash->{NAME};
my $name = $hash->{NAME};
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
my $code = abs( $hash->{GID} ); my $code = abs( $hash->{GID} );
$code = $hash->{IODev}->{NAME} ."-". $code if( defined($hash->{IODev}->{NAME}) ); $code = $hash->{IODev}->{NAME} . "-" . $code
if ( defined( $hash->{IODev}->{NAME} ) );
delete( $modules{HEOSGroup}{defptr}{$code} ); delete( $modules{HEOSGroup}{defptr}{$code} );
Log3 $name, 3, "HEOSGroup ($name) - device $name deleted with Code: $code"; Log3 $name, 3, "HEOSGroup ($name) - device $name deleted with Code: $code";
return undef; return;
} }
sub HEOSGroup_Attr(@) { sub HEOSGroup_Attr {
my ( $cmd, $name, $attrName, $attrVal ) = @_; my ( $cmd, $name, $attrName, $attrVal ) = @_;
my $hash = $defs{$name}; my $hash = $defs{$name};
my $token = $hash->{IODev}->{TOKEN}; my $token = $hash->{IODev}->{TOKEN};
if ( $attrName eq "disable" ) { if ( $attrName eq "disable" ) {
if ( $cmd eq "set" and $attrVal eq "1" ) { if ( $cmd eq "set" and $attrVal eq "1" ) {
readingsSingleUpdate( $hash, "state", "disabled", 1 ); readingsSingleUpdate( $hash, "state", "disabled", 1 );
Log3 $name, 3, "HEOSGroup ($name) - disabled"; Log3 $name, 3, "HEOSGroup ($name) - disabled";
} elsif( $cmd eq "del" ) { }
elsif ( $cmd eq "del" ) {
readingsSingleUpdate( $hash, "state", "active", 1 ); readingsSingleUpdate( $hash, "state", "active", 1 );
Log3 $name, 3, "HEOSGroup ($name) - enabled"; Log3 $name, 3, "HEOSGroup ($name) - enabled";
@ -201,19 +190,21 @@ sub HEOSGroup_Attr(@) {
Log3 $name, 3, "HEOSGroup ($name) - enable disabledForIntervals"; Log3 $name, 3, "HEOSGroup ($name) - enable disabledForIntervals";
readingsSingleUpdate( $hash, "state", "Unknown", 1 ); readingsSingleUpdate( $hash, "state", "Unknown", 1 );
} elsif( $cmd eq "del" ) { }
elsif ( $cmd eq "del" ) {
readingsSingleUpdate( $hash, "state", "active", 1 ); readingsSingleUpdate( $hash, "state", "active", 1 );
Log3 $name, 3, "HEOSGroup ($name) - delete disabledForIntervals"; Log3 $name, 3, "HEOSGroup ($name) - delete disabledForIntervals";
} }
} }
return;
} }
sub HEOSGroup_Notify($$) { sub HEOSGroup_Notify {
my ( $hash, $dev ) = @_; my ( $hash, $dev ) = @_;
my $name = $hash->{NAME};
my $name = $hash->{NAME};
return undef if ( IsDisabled($name) ); return undef if ( IsDisabled($name) );
@ -222,21 +213,35 @@ sub HEOSGroup_Notify($$) {
return if ( !$events ); return if ( !$events );
readingsBeginUpdate($hash); readingsBeginUpdate($hash);
my %playerEevents = map { my ( $key, $value ) = split /:\s/; ( $key, $value ) } @$events; my %playerEevents =
map { my ( $key, $value ) = split /:\s/; ( $key, $value ) } @$events;
foreach my $key ( keys %playerEevents ) { foreach my $key ( keys %playerEevents ) {
#### playing Infos #### playing Infos
readingsBulkUpdate( $hash, $key, $playerEevents{$key} ) if( grep { $_ =~ /$key/ } ("channel", "currentAlbum", "currentArtist", "currentImageUrl", "currentMedia", "currentMid", "currentQid", "currentSid", "currentStation", "currentTitle", "error", "playStatus", "repeat", "shuffle" ) ); readingsBulkUpdate( $hash, $key, $playerEevents{$key} )
if (
grep { $_ =~ /$key/ } (
"channel", "currentAlbum",
"currentArtist", "currentImageUrl",
"currentMedia", "currentMid",
"currentQid", "currentSid",
"currentStation", "currentTitle",
"error", "playStatus",
"repeat", "shuffle"
)
);
} }
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
return;
} }
sub HEOSGroup_Set($$@) { sub HEOSGroup_Set {
my ( $hash, $name, @aa ) = @_; my ( $hash, $name, @aa ) = @_;
my ( $cmd, @args ) = @aa; my ( $cmd, @args ) = @aa;
my $gid = $hash->{GID}; my $gid = $hash->{GID};
my $action; my $action;
my $heosCmd; my $heosCmd;
@ -245,7 +250,6 @@ sub HEOSGroup_Set($$@) {
my $favoritcount = 1; my $favoritcount = 1;
my $string = "gid=$gid"; my $string = "gid=$gid";
#senden von Befehlen unterdrücken solange state nicht on ist #senden von Befehlen unterdrücken solange state nicht on ist
return undef unless ( ReadingsVal( $name, "state", "off" ) eq "on" ); return undef unless ( ReadingsVal( $name, "state", "off" ) eq "on" );
@ -254,38 +258,64 @@ sub HEOSGroup_Set($$@) {
$heosCmd = $cmd; $heosCmd = $cmd;
} elsif( $cmd eq 'mute' ) { }
elsif ( $cmd eq 'mute' ) {
my $param = "on|off"; my $param = "on|off";
return "usage: $cmd $param" if( @args != 1 || ! grep { $_ =~ /$args[0]/ } split(/\|/, $param) ); return "usage: $cmd $param"
if ( @args != 1 || !grep { $_ =~ /$args[0]/ } split( /\|/, $param ) );
$heosCmd = 'setGroupMute'; $heosCmd = 'setGroupMute';
$action = "state=$args[0]"; $action = "state=$args[0]";
} elsif( $cmd eq 'volume' ) { }
return "usage: $cmd 0-100" if( @args != 1 || $args[0] !~ /(\d+)/ || $args[0] > 100 || $args[0] < 0 ); elsif ( $cmd eq 'volume' ) {
return "usage: $cmd 0-100"
if ( @args != 1
|| $args[0] !~ /(\d+)/
|| $args[0] > 100
|| $args[0] < 0 );
$heosCmd = 'setGroupVolume'; $heosCmd = 'setGroupVolume';
$action = "level=$args[0]"; $action = "level=$args[0]";
} elsif( $cmd eq 'volumeUp' ) { }
return "usage: $cmd 0-10" if( @args != 1 || $args[0] !~ /(\d+)/ || $args[0] > 10 || $args[0] < 1 ); elsif ( $cmd eq 'volumeUp' ) {
return "usage: $cmd 0-10"
if ( @args != 1
|| $args[0] !~ /(\d+)/
|| $args[0] > 10
|| $args[0] < 1 );
$heosCmd = 'GroupVolumeUp'; $heosCmd = 'GroupVolumeUp';
$action = "step=$args[0]"; $action = "step=$args[0]";
} elsif( $cmd eq 'volumeDown' ) { }
return "usage: $cmd 0-10" if( @args != 1 || $args[0] !~ /(\d+)/ || $args[0] > 10 || $args[0] < 1 ); elsif ( $cmd eq 'volumeDown' ) {
return "usage: $cmd 0-10"
if ( @args != 1
|| $args[0] !~ /(\d+)/
|| $args[0] > 10
|| $args[0] < 1 );
$heosCmd = 'groupVolumeDown'; $heosCmd = 'groupVolumeDown';
$action = "step=$args[0]"; $action = "step=$args[0]";
} elsif( $cmd eq 'clearGroup' ) { }
elsif ( $cmd eq 'clearGroup' ) {
return "usage: $cmd" if ( @args != 0 ); return "usage: $cmd" if ( @args != 0 );
$heosCmd = 'createGroup'; $heosCmd = 'createGroup';
$string = "pid=$gid"; $string = "pid=$gid";
} elsif( grep { $_ eq $cmd } ("play", "stop", "pause", "next", "prev", "channel", "channelUp", "channelDown", "playlist" ) ) { }
elsif (
grep { $_ eq $cmd } (
"play", "stop", "pause", "next",
"prev", "channel", "channelUp", "channelDown",
"playlist"
)
)
{
#ab hier Playerbefehle emuliert #ab hier Playerbefehle emuliert
$string = "pid=$gid"; $string = "pid=$gid";
@ -296,9 +326,11 @@ sub HEOSGroup_Set($$@) {
$heosCmd = 'setPlayMode'; $heosCmd = 'setPlayMode';
$rvalue = 'on_' . $args[0]; $rvalue = 'on_' . $args[0];
$rvalue = 'off' if ( $rvalue eq 'on_off' ); $rvalue = 'off' if ( $rvalue eq 'on_off' );
$action = "repeat=$rvalue&shuffle=".ReadingsVal($name,'shuffle','off'); $action = "repeat=$rvalue&shuffle="
. ReadingsVal( $name, 'shuffle', 'off' );
} elsif( $cmd eq 'shuffle' ) { }
elsif ( $cmd eq 'shuffle' ) {
return "usage: shuffle on,off" if ( @args != 1 ); return "usage: shuffle on,off" if ( @args != 1 );
$heosCmd = 'setPlayMode'; $heosCmd = 'setPlayMode';
@ -306,46 +338,58 @@ sub HEOSGroup_Set($$@) {
$rvalue = 'off' if ( $rvalue eq 'on_off' ); $rvalue = 'off' if ( $rvalue eq 'on_off' );
$action = "repeat=$rvalue&shuffle=$args[0]"; $action = "repeat=$rvalue&shuffle=$args[0]";
} elsif( $cmd eq 'play' ) { }
elsif ( $cmd eq 'play' ) {
return "usage: play" if ( @args != 0 ); return "usage: play" if ( @args != 0 );
$heosCmd = 'setPlayState'; $heosCmd = 'setPlayState';
$action = "state=$cmd"; $action = "state=$cmd";
} elsif( $cmd eq 'stop' ) { }
elsif ( $cmd eq 'stop' ) {
return "usage: stop" if ( @args != 0 ); return "usage: stop" if ( @args != 0 );
$heosCmd = 'setPlayState'; $heosCmd = 'setPlayState';
$action = "state=$cmd"; $action = "state=$cmd";
} elsif( $cmd eq 'pause' ) { }
elsif ( $cmd eq 'pause' ) {
return "usage: pause" if ( @args != 0 ); return "usage: pause" if ( @args != 0 );
$heosCmd = 'setPlayState'; $heosCmd = 'setPlayState';
$action = "state=$cmd"; $action = "state=$cmd";
} elsif( $cmd eq 'next' ) { }
elsif ( $cmd eq 'next' ) {
return "usage: next" if ( @args != 0 ); return "usage: next" if ( @args != 0 );
$heosCmd = 'playNext'; $heosCmd = 'playNext';
} elsif( $cmd eq 'prev' ) { }
elsif ( $cmd eq 'prev' ) {
return "usage: prev" if ( @args != 0 ); return "usage: prev" if ( @args != 0 );
$heosCmd = 'playPrev'; $heosCmd = 'playPrev';
} elsif ( $cmd =~ /channel/ ) { }
elsif ( $cmd =~ /channel/ ) {
my $favorit = ReadingsVal( $name, "channel", 1 ); my $favorit = ReadingsVal( $name, "channel", 1 );
$favoritcount = scalar(@{$hash->{IODev}{helper}{favorites}}) if ( defined $hash->{IODev}{helper}{favorites} ); $favoritcount = scalar( @{ $hash->{IODev}{helper}{favorites} } )
if ( defined $hash->{IODev}{helper}{favorites} );
$heosCmd = 'playPresetStation'; $heosCmd = 'playPresetStation';
if ( $cmd eq 'channel' ) { if ( $cmd eq 'channel' ) {
return "usage: $cmd 1-$favoritcount" if( @args != 1 || $args[0] !~ /(\d+)/ || $args[0] > $favoritcount || $args[0] < 1); return "usage: $cmd 1-$favoritcount"
if ( @args != 1
|| $args[0] !~ /(\d+)/
|| $args[0] > $favoritcount
|| $args[0] < 1 );
$action = "preset=$args[0]"; $action = "preset=$args[0]";
} elsif( $cmd eq 'channelUp' ) { }
elsif ( $cmd eq 'channelUp' ) {
return "usage: $cmd" if ( @args != 0 ); return "usage: $cmd" if ( @args != 0 );
++$favorit; ++$favorit;
@ -354,7 +398,8 @@ sub HEOSGroup_Set($$@) {
$favorit = 1; $favorit = 1;
} else { }
else {
$favorit = $favoritcount; $favorit = $favoritcount;
@ -363,7 +408,8 @@ sub HEOSGroup_Set($$@) {
$action = "preset=" . $favorit; $action = "preset=" . $favorit;
} elsif( $cmd eq 'channelDown' ) { }
elsif ( $cmd eq 'channelDown' ) {
return "usage: $cmd" if ( @args != 0 ); return "usage: $cmd" if ( @args != 0 );
--$favorit; --$favorit;
@ -372,7 +418,8 @@ sub HEOSGroup_Set($$@) {
$favorit = $favoritcount; $favorit = $favoritcount;
} else { }
else {
$favorit = 1; $favorit = 1;
} }
@ -381,9 +428,13 @@ sub HEOSGroup_Set($$@) {
$action = "preset=" . $favorit; $action = "preset=" . $favorit;
} }
} elsif ( $cmd =~ /Playlist/ ) { }
elsif ( $cmd =~ /Playlist/ ) {
my @cids = map { $_->{cid} } grep { $_->{name} =~ /\Q$args[0]\E/i } (@{ $hash->{IODev}{helper}{playlists} }); my @cids =
map { $_->{cid} }
grep { $_->{name} =~ /\Q$args[0]\E/i }
( @{ $hash->{IODev}{helper}{playlists} } );
if ( scalar @args == 1 && scalar @cids > 0 ) { if ( scalar @args == 1 && scalar @cids > 0 ) {
if ( $cmd eq 'playPlaylist' ) { if ( $cmd eq 'playPlaylist' ) {
@ -391,29 +442,43 @@ sub HEOSGroup_Set($$@) {
$heosCmd = $cmd; $heosCmd = $cmd;
$action = "sid=1025&cid=$cids[0]&aid=4"; $action = "sid=1025&cid=$cids[0]&aid=4";
} elsif ( $cmd eq 'deletePlaylist' ) { }
elsif ( $cmd eq 'deletePlaylist' ) {
$heosCmd = $cmd; $heosCmd = $cmd;
$action = "cid=$cids[0]"; $action = "cid=$cids[0]";
$string = "sid=1025"; $string = "sid=1025";
} }
} else { }
else {
IOWrite( $hash, 'browseSource', 'sid=1025' ); IOWrite( $hash, 'browseSource', 'sid=1025' );
my @playlists = map { $_->{name} } (@{ $hash->{IODev}{helper}{playlists}}); my @playlists =
map { $_->{name} } ( @{ $hash->{IODev}{helper}{playlists} } );
return "usage: $cmd " . join( ",", @playlists ); return "usage: $cmd " . join( ",", @playlists );
} }
} }
} else { }
else {
my $list = "getGroupInfo:noArg mute:on,off volume:slider,0,5,100 volumeUp:slider,0,1,10 volumeDown:slider,0,1,10 clearGroup:noArg repeat:one,all,off shuffle:on,off play:noArg stop:noArg pause:noArg next:noArg prev:noArg channelUp:noArg channelDown:noArg "; my $list =
"getGroupInfo:noArg mute:on,off volume:slider,0,5,100 volumeUp:slider,0,1,10 volumeDown:slider,0,1,10 clearGroup:noArg repeat:one,all,off shuffle:on,off play:noArg stop:noArg pause:noArg next:noArg prev:noArg channelUp:noArg channelDown:noArg ";
$list .= " channel:slider,1,1,".scalar(@{$hash->{IODev}{helper}{favorites}}) if ( defined $hash->{IODev}{helper}{favorites} ); $list .=
" channel:slider,1,1,"
. scalar( @{ $hash->{IODev}{helper}{favorites} } )
if ( defined $hash->{IODev}{helper}{favorites} );
if ( defined $hash->{IODev}{helper}{playlists} ) { if ( defined $hash->{IODev}{helper}{playlists} ) {
my @playlists = map { my %n; $n{name} = $_->{name}; $n{name} =~ s/\s+/\&nbsp;/g; $n{name} } (@{ $hash->{IODev}{helper}{playlists}}); my @playlists = map {
$list .= " playlist:".join(",",@playlists) if( scalar @playlists > 0 ); my %n;
$n{name} = $_->{name};
$n{name} =~ s/\s+/\&nbsp;/g;
$n{name}
} ( @{ $hash->{IODev}{helper}{playlists} } );
$list .= " playlist:" . join( ",", @playlists )
if ( scalar @playlists > 0 );
} }
return "Unknown argument $cmd, choose one of $list"; return "Unknown argument $cmd, choose one of $list";
@ -421,19 +486,20 @@ sub HEOSGroup_Set($$@) {
$string .= "&$action" if ( defined($action) ); $string .= "&$action" if ( defined($action) );
IOWrite( $hash, "$heosCmd", "$string" ); IOWrite( $hash, "$heosCmd", "$string" );
Log3 $name, 4, "HEOSGroup ($name) - IOWrite: $heosCmd $string IODevHash=$hash->{IODev}"; Log3 $name, 4,
return undef; "HEOSGroup ($name) - IOWrite: $heosCmd $string IODevHash=$hash->{IODev}";
return;
} }
sub HEOSGroup_Parse($$) { sub HEOSGroup_Parse {
my ( $io_hash, $json ) = @_; my ( $io_hash, $json ) = @_;
my $name = $io_hash->{NAME}; my $name = $io_hash->{NAME};
my $gid; my $gid;
my $decode_json; my $decode_json;
my $code; my $code;
$decode_json = eval { decode_json( encode_utf8($json) ) }; $decode_json = eval { decode_json( encode_utf8($json) ) };
if ($@) { if ($@) {
Log3 $name, 3, "HEOSGroup ($name) - JSON error while request: $@"; Log3 $name, 3, "HEOSGroup ($name) - JSON error while request: $@";
@ -446,43 +512,52 @@ sub HEOSGroup_Parse($$) {
$gid = $decode_json->{gid}; $gid = $decode_json->{gid};
$code = abs($gid); $code = abs($gid);
$code = $io_hash->{NAME} ."-". $code if( defined($io_hash->{NAME}) ); $code = $io_hash->{NAME} . "-" . $code
if ( defined( $io_hash->{NAME} ) );
if ( my $hash = $modules{HEOSGroup}{defptr}{$code} ) { if ( my $hash = $modules{HEOSGroup}{defptr}{$code} ) {
IOWrite( $hash, 'getGroupInfo', "gid=$hash->{GID}" ); IOWrite( $hash, 'getGroupInfo', "gid=$hash->{GID}" );
readingsSingleUpdate( $hash, "state", "on", 1 ); readingsSingleUpdate( $hash, "state", "on", 1 );
Log3 $hash->{NAME}, 4, "HEOSGroup ($hash->{NAME}) - find logical device: $hash->{NAME}"; Log3 $hash->{NAME}, 4,
Log3 $hash->{NAME}, 4, "HEOSGroup ($hash->{NAME}) - find GID in root from decode_json"; "HEOSGroup ($hash->{NAME}) - find logical device: $hash->{NAME}";
Log3 $hash->{NAME}, 4,
"HEOSGroup ($hash->{NAME}) - find GID in root from decode_json";
return $hash->{NAME}; return $hash->{NAME};
} else { }
else {
my $devname = "HEOSGroup" . abs($gid); my $devname = "HEOSGroup" . abs($gid);
return "UNDEFINED $devname HEOSGroup $gid IODev=$name"; return "UNDEFINED $devname HEOSGroup $gid IODev=$name";
} }
} else { }
else {
my %message = map { my ( $key, $value ) = split "="; $key => $value } split('&', $decode_json->{heos}{message}); my %message = map { my ( $key, $value ) = split "="; $key => $value }
split( '&', $decode_json->{heos}{message} );
$gid = $message{pid} if ( defined( $message{pid} ) ); $gid = $message{pid} if ( defined( $message{pid} ) );
$gid = $message{gid} if ( defined( $message{gid} ) ); $gid = $message{gid} if ( defined( $message{gid} ) );
$gid = $decode_json->{payload}{gid} if( defined($decode_json->{payload}{gid}) ); $gid = $decode_json->{payload}{gid}
if ( defined( $decode_json->{payload}{gid} ) );
Log3 $name, 4, "HEOSGroup ($name) - GID: $gid"; Log3 $name, 4, "HEOSGroup ($name) - GID: $gid";
$code = abs($gid); $code = abs($gid);
$code = $io_hash->{NAME} ."-". $code if( defined($io_hash->{NAME}) ); $code = $io_hash->{NAME} . "-" . $code
if ( defined( $io_hash->{NAME} ) );
if ( my $hash = $modules{HEOSGroup}{defptr}{$code} ) { if ( my $hash = $modules{HEOSGroup}{defptr}{$code} ) {
HEOSGroup_WriteReadings( $hash, $decode_json ); HEOSGroup_WriteReadings( $hash, $decode_json );
Log3 $hash->{NAME}, 4, "HEOSGroup ($hash->{NAME}) - find logical device: $hash->{NAME}"; Log3 $hash->{NAME}, 4,
"HEOSGroup ($hash->{NAME}) - find logical device: $hash->{NAME}";
return $hash->{NAME}; return $hash->{NAME};
} else { }
else {
my $devname = "HEOSGroup" . abs($gid); my $devname = "HEOSGroup" . abs($gid);
return "UNDEFINED $devname HEOSGroup $gid IODev=$name"; return "UNDEFINED $devname HEOSGroup $gid IODev=$name";
@ -490,11 +565,10 @@ sub HEOSGroup_Parse($$) {
} }
} }
sub HEOSGroup_WriteReadings($$) { sub HEOSGroup_WriteReadings {
my ( $hash, $decode_json ) = @_; my ( $hash, $decode_json ) = @_;
my $name = $hash->{NAME};
my $name = $hash->{NAME};
Log3 $name, 4, "HEOSGroup ($name) - processing data to write readings"; Log3 $name, 4, "HEOSGroup ($name) - processing data to write readings";
############################ ############################
@ -508,7 +582,8 @@ sub HEOSGroup_WriteReadings($$) {
### Event Readings ### Event Readings
if ( ref($readingsHash) eq "HASH" ) { if ( ref($readingsHash) eq "HASH" ) {
Log3 $name, 4, "HEOSGroup ($name) - response json Hash back from HEOSGroup_PreProcessingReadings"; Log3 $name, 4,
"HEOSGroup ($name) - response json Hash back from HEOSGroup_PreProcessingReadings";
my $t; my $t;
my $v; my $v;
@ -529,15 +604,18 @@ sub HEOSGroup_WriteReadings($$) {
foreach my $player ( @{ $decode_json->{payload}{players} } ) { foreach my $player ( @{ $decode_json->{payload}{players} } ) {
readingsBulkUpdate( $hash, 'leader', $player->{name} ) if ( $player->{role} eq "leader" ); readingsBulkUpdate( $hash, 'leader', $player->{name} )
push( @members, $player->{name}) if ( $player->{role} eq "member" ); if ( $player->{role} eq "leader" );
push( @members, $player->{name} )
if ( $player->{role} eq "member" );
} }
if ( scalar @members > 1 ) { if ( scalar @members > 1 ) {
readingsBulkUpdate( $hash, 'member', join( ",", @members ) ); readingsBulkUpdate( $hash, 'member', join( ",", @members ) );
} else { }
else {
readingsBulkUpdate( $hash, 'member', $members[0] ); readingsBulkUpdate( $hash, 'member', $members[0] );
} }
@ -546,90 +624,91 @@ sub HEOSGroup_WriteReadings($$) {
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
Log3 $name, 5, "HEOSGroup ($name) - readings set for $name"; Log3 $name, 5, "HEOSGroup ($name) - readings set for $name";
return undef;
return;
} }
############### ###############
### my little Helpers ### my little Helpers
sub HEOSGroup_PreProcessingReadings($$) { sub HEOSGroup_PreProcessingReadings {
my ( $hash, $decode_json ) = @_; my ( $hash, $decode_json ) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $reading; my $reading;
my %buffer; my %buffer;
my %message = map { my ( $key, $value ) = split "="; $key => $value } split('&', $decode_json->{heos}{message}); my %message = map { my ( $key, $value ) = split "="; $key => $value }
split( '&', $decode_json->{heos}{message} );
Log3 $name, 4, "HEOSGroup ($name) - preprocessing readings"; Log3 $name, 4, "HEOSGroup ($name) - preprocessing readings";
if ( $decode_json->{heos}{command} =~ /volume_changed/ or $decode_json->{heos}{command} =~ /set_volume/ or $decode_json->{heos}{command} =~ /get_volume/ ) { if ( $decode_json->{heos}{command} =~ /volume_changed/
or $decode_json->{heos}{command} =~ /set_volume/
or $decode_json->{heos}{command} =~ /get_volume/ )
{
my @value = split( '&', $decode_json->{heos}{message} ); my @value = split( '&', $decode_json->{heos}{message} );
$buffer{'volume'} = substr( $value[1], 6 ); $buffer{'volume'} = substr( $value[1], 6 );
$buffer{'mute'} = substr($value[2],5) if( $decode_json->{heos}{command} =~ /volume_changed/ ); $buffer{'mute'} = substr( $value[2], 5 )
if ( $decode_json->{heos}{command} =~ /volume_changed/ );
} elsif ( $decode_json->{heos}{command} =~ /volume_up/ or $decode_json->{heos}{command} =~ /volume_down/ ) { }
elsif ($decode_json->{heos}{command} =~ /volume_up/
or $decode_json->{heos}{command} =~ /volume_down/ )
{
my @value = split( '&', $decode_json->{heos}{message} ); my @value = split( '&', $decode_json->{heos}{message} );
$buffer{'volumeUp'} = substr($value[1],5) if( $decode_json->{heos}{command} =~ /volume_up/ ); $buffer{'volumeUp'} = substr( $value[1], 5 )
$buffer{'volumeDown'} = substr($value[1],5) if( $decode_json->{heos}{command} =~ /volume_down/ ); if ( $decode_json->{heos}{command} =~ /volume_up/ );
$buffer{'volumeDown'} = substr( $value[1], 5 )
if ( $decode_json->{heos}{command} =~ /volume_down/ );
} elsif ( $decode_json->{heos}{command} =~ /get_mute/ ) { }
elsif ( $decode_json->{heos}{command} =~ /get_mute/ ) {
my @value = split( '&', $decode_json->{heos}{message} ); my @value = split( '&', $decode_json->{heos}{message} );
$buffer{'mute'} = substr( $value[1], 6 ); $buffer{'mute'} = substr( $value[1], 6 );
} else { }
else {
Log3 $name, 4, "HEOSGroup ($name) - no match found"; Log3 $name, 4, "HEOSGroup ($name) - no match found";
return undef; return undef;
} }
Log3 $name, 4, "HEOSGroup ($name) - Match found for decode_json"; Log3 $name, 4, "HEOSGroup ($name) - Match found for decode_json";
return \%buffer; return \%buffer;
} }
sub HEOSGroup_GetGroupInfo($) { sub HEOSGroup_GetGroupInfo {
my $hash = shift; my $hash = shift;
RemoveInternalTimer( $hash, 'HEOSGroup_GetGroupInfo' ); RemoveInternalTimer( $hash, 'HEOSGroup_GetGroupInfo' );
IOWrite($hash,'getGroupInfo',"gid=$hash->{GID}");
return IOWrite( $hash, 'getGroupInfo', "gid=$hash->{GID}" );
} }
sub HEOSGroup_GetGroupVolume($) { sub HEOSGroup_GetGroupVolume {
my $hash = shift; my $hash = shift;
RemoveInternalTimer( $hash, 'HEOSGroup_GetGroupVolume' ); RemoveInternalTimer( $hash, 'HEOSGroup_GetGroupVolume' );
IOWrite($hash,'getGroupVolume',"gid=$hash->{GID}");
return IOWrite( $hash, 'getGroupVolume', "gid=$hash->{GID}" );
} }
sub HEOSGroup_GetGroupMute($) { sub HEOSGroup_GetGroupMute {
my $hash = shift; my $hash = shift;
RemoveInternalTimer( $hash, 'HEOSGroup_GetGroupMute' ); RemoveInternalTimer( $hash, 'HEOSGroup_GetGroupMute' );
IOWrite($hash,'getGroupMute',"gid=$hash->{GID}");
return IOWrite( $hash, 'getGroupMute', "gid=$hash->{GID}" );
} }
1; 1;
=pod =pod
=item device =item device
=item summary Modul to controls the Denon multiroom soundsystem =item summary Modul to controls the Denon multiroom soundsystem

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

0
controls_HEOS.txt Normal file
View File

39
hooks/pre-commit Executable file
View File

@ -0,0 +1,39 @@
#!/usr/bin/perl -w
use File::Basename;
use POSIX qw(strftime);
use strict;
my @filenames =
( 'FHEM/21_HEOSGroup.pm', 'FHEM/21_HEOSMaster.pm', 'FHEM/21_HEOSPlayer.pm' );
my $controlsfile = 'controls_HEOS.txt';
open( FH, ">$controlsfile" ) || return ("Can't open $controlsfile: $!");
for my $filename (@filenames) {
my @statOutput = stat($filename);
if ( scalar @statOutput != 13 ) {
printf 'error: stat has unexpected return value for '
. $filename . "\n";
next;
}
my $mtime = $statOutput[9];
my $date = POSIX::strftime( "%Y-%m-%d", localtime($mtime) );
my $time = POSIX::strftime( "%H:%M:%S", localtime($mtime) );
my $filetime = $date . "_" . $time;
my $filesize = $statOutput[7];
printf FH 'UPD ' . $filetime . ' ' . $filesize . ' ' . $filename . "\n";
}
close(FH);
system("git add $controlsfile");
print 'Create controls File succesfully' . "\n";
exit 0;