extended group support and bugfixes. special thanks to hanseis

This commit is contained in:
Marko Oldenburg 2017-02-21 06:50:31 +01:00
parent 8ae4f661be
commit 739617ae5d
3 changed files with 101 additions and 42 deletions

View File

@ -5,6 +5,10 @@
# (c) 2017 Copyright: Marko Oldenburg (leongaultier at gmail dot com)
# All rights reserved
#
# Special thanks goes to comitters:
# - Olaf Schnicke
#
#
# This script is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
@ -34,7 +38,7 @@ use JSON qw(decode_json);
use Encode qw(encode_utf8);
my $version = "0.1.51";
my $version = "0.1.56";
@ -50,6 +54,7 @@ sub HEOSGroup_Set($$@);
sub HEOSGroup_PreProcessingReadings($$);
sub HEOSGroup_GetGroupInfo($);
sub HEOSGroup_GetGroupVolume($);
sub HEOSGroup_GetGroupMute($);
@ -59,7 +64,7 @@ sub HEOSGroup_Initialize($) {
my ($hash) = @_;
$hash->{Match} = '.*{"command":."group.*';
$hash->{Match} = '.*{"command":."group.*|.*{"command":."event\/group.*';
# Provider
$hash->{SetFn} = "HEOSGroup_Set";
@ -139,9 +144,11 @@ sub HEOSGroup_Define($$) {
if( $init_done ) {
InternalTimer( gettimeofday()+int(rand(2)), "HEOSGroup_GetGroupInfo", $hash, 0 );
InternalTimer( gettimeofday()+int(rand(4)), "HEOSGroup_GetGroupVolume", $hash, 0 );
InternalTimer( gettimeofday()+int(rand(6)), "HEOSGroup_GetGroupMute", $hash, 0 );
} else {
InternalTimer( gettimeofday()+15+int(rand(2)), "HEOSGroup_GetGroupInfo", $hash, 0 );
InternalTimer( gettimeofday()+15+int(rand(10)), "HEOSGroup_GetGroupVolume", $hash, 0 );
InternalTimer( gettimeofday()+15+int(rand(4)), "HEOSGroup_GetGroupVolume", $hash, 0 );
InternalTimer( gettimeofday()+15+int(rand(6)), "HEOSGroup_GetGroupMute", $hash, 0 );
}
readingsBeginUpdate($hash);
@ -287,6 +294,7 @@ sub HEOSGroup_Parse($$) {
my $name = $hash->{NAME};
IOWrite($hash,'getGroupInfo',"gid=$hash->{GID}");
Log3 $name, 4, "HEOSGroup ($name) - find logical device: $hash->{NAME}";
Log3 $name, 4, "HEOSGroup ($name) - find GID in root from decode_json";
@ -373,26 +381,32 @@ sub HEOSGroup_WriteReadings($$) {
}
### PlayerInfos
### GroupInfos
readingsBulkUpdate( $hash, 'name', $decode_json->{payload}{name} );
readingsBulkUpdate( $hash, 'gid', $decode_json->{payload}{gid} );
readingsBulkUpdate( $hash, 'model', $decode_json->{payload}{model} );
readingsBulkUpdate( $hash, 'version', $decode_json->{payload}{version} );
readingsBulkUpdate( $hash, 'network', $decode_json->{payload}{network} );
readingsBulkUpdate( $hash, 'lineout', $decode_json->{payload}{lineout} );
readingsBulkUpdate( $hash, 'control', $decode_json->{payload}{control} );
readingsBulkUpdate( $hash, 'ip-address', $decode_json->{payload}{ip} );
### playing Infos
readingsBulkUpdate( $hash, 'currentMedia', $decode_json->{payload}{type} );
readingsBulkUpdate( $hash, 'currentTitle', $decode_json->{payload}{song} );
readingsBulkUpdate( $hash, 'currentAlbum', $decode_json->{payload}{album} );
readingsBulkUpdate( $hash, 'currentArtist', $decode_json->{payload}{artist} );
readingsBulkUpdate( $hash, 'currentImageUrl', $decode_json->{payload}{image_url} );
readingsBulkUpdate( $hash, 'currentMid', $decode_json->{payload}{mid} );
readingsBulkUpdate( $hash, 'currentQid', $decode_json->{payload}{qid} );
readingsBulkUpdate( $hash, 'currentSid', $decode_json->{payload}{sid} );
readingsBulkUpdate( $hash, 'currentStation', $decode_json->{payload}{station} );
my @members;
if( ref($decode_json->{payload}{players}) eq "ARRAY" ) {
foreach my $player (@{ $decode_json->{payload}{players} }) {
readingsBulkUpdate( $hash, 'leader', $player->{name} ) if ( $player->{role} eq "leader" );
push( @members, $player->{name}) if ( $player->{role} eq "member" );
print"Player ###################################\n".Dumper($player);
}
}
if( scalar @members > 1 ) {
readingsBulkUpdate( $hash, 'member', join(",",@members) );
} else {
readingsBulkUpdate( $hash, 'member', $members[0] );
}
print"Member ##################################\n".Dumper(@members);
readingsBulkUpdate( $hash, 'state', 'on' );
@ -429,6 +443,10 @@ sub HEOSGroup_PreProcessingReadings($$) {
$buffer{'volumeUp'} = substr($value[1],5) 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/ ) {
my @value = split('&', $decode_json->{heos}{message});
$buffer{'mute'} = substr($value[1],6);
} else {
Log3 $name, 3, "HEOSGroup ($name) - no match found";
@ -458,6 +476,15 @@ sub HEOSGroup_GetGroupVolume($) {
}
sub HEOSGroup_GetGroupMute($) {
my $hash = shift;
RemoveInternalTimer($hash,'HEOSGroup_GetGroupMute');
IOWrite($hash,'getGroupMute',"gid=$hash->{GID}");
}

View File

@ -50,7 +50,7 @@ use Encode qw(encode_utf8);
use Net::Telnet;
my $version = "0.1.55";
my $version = "0.1.56";
my %heosCmds = (
@ -67,6 +67,8 @@ my %heosCmds = (
'getGroupInfo' => 'group/get_group_info?',
'getPlayState' => 'player/get_play_state?',
'getPlayMode' => 'player/get_play_mode?',
'getMute' => 'player/get_mute?'
'getGroupMute' => 'group/get_mute?',
'clearQueue' => 'player/clear_queue?',
'saveQueue' => 'player/save_queue?',
'getVolume' => 'player/get_volume?',
@ -74,12 +76,13 @@ my %heosCmds = (
'setPlayState' => 'player/set_play_state?',
'setPlayMode' => 'player/set_play_mode?',
'setMute' => 'player/set_mute?',
'setGroupMute' => 'group/set_mute?',
'playNext' => 'player/play_next?',
'playPrev' => 'player/play_prev?',
'playPresetStation' => 'browse/play_preset?',
'playInput' => 'browse/play_input?',
'playStream' => 'browse/play_stream?',
'playPlaylist' => 'browse/add_to_queue?',
'playPrev' => 'player/play_prev?',
'playPresetStation' => 'browse/play_preset?',
'playInput' => 'browse/play_input?',
'playStream' => 'browse/play_stream?',
'playPlaylist' => 'browse/add_to_queue?',
'setVolume' => 'player/set_volume?',
'setGroupVolume' => 'group/set_volume?',
'volumeUp' => 'player/volume_up?',
@ -137,7 +140,7 @@ sub HEOSMaster_Initialize($) {
$hash->{WriteFn} = "HEOSMaster_Write";
$hash->{Clients} = ":HEOSPlayer:";
$hash->{MatchList} = { "1:HEOSPlayer" => '.*{"command":."player.*|.*{"command":."event\/player.*|.*{"command":."event\/repeat_mode_changed.*|.*{"command":."event\/shuffle_mode_changed.*|.*{"command":."event\/favorites_changed.*',
"2:HEOSGroup" => '.*{"command":."group.*'
"2:HEOSGroup" => '.*{"command":."group.*|.*{"command":."event\/group.*'
};
@ -386,6 +389,7 @@ sub HEOSMaster_Open($) {
HEOSMaster_GetPlayers($hash);
InternalTimer( gettimeofday()+1, 'HEOSMaster_EnableChangeEvents', $hash, 0 );
InternalTimer( gettimeofday()+2, 'HEOSMaster_GetMusicSources', $hash, 0 );
InternalTimer( gettimeofday()+3, 'HEOSMaster_GetGroups', $hash, 0 );
}
sub HEOSMaster_Close($) {
@ -685,6 +689,14 @@ sub HEOSMaster_ResponseProcessing($$) {
Log3 $name, 4, "HEOSMaster ($name) - user changed";
return;
}
#Gruppen neu einlesen
if( $decode_json->{heos}{command} =~ /^event\/groups_changed/ ) {
#InternalTimer( gettimeofday()+5, 'HEOSMaster_GetGroups', $hash, 0 );
HEOSMaster_Write($hash,'getGroups',undef);
Log3 $name, 4, "HEOSMaster ($name) - groups changed";
return;
}
if( $decode_json->{heos}{command} =~ /^player/ or $decode_json->{heos}{command} =~ /^event\/player/ or $decode_json->{heos}{command} =~ /^group/ or $decode_json->{heos}{command} =~ /^event\/group/ or $decode_json->{heos}{command} =~ /^event\/repeat_mode_changed/ or $decode_json->{heos}{command} =~ /^event\/shuffle_mode_changed/ ) {
@ -703,7 +715,7 @@ sub HEOSMaster_ResponseProcessing($$) {
$json .= '","heos": {"command": "player/get_players"}}';
Dispatch($hash,$json,undef);
Log3 $name, 4, "HEOSMaster ($name) - call Dispatcher";
Log3 $name, 4, "HEOSMaster ($name) - call Dispatcher for Players";
}
return;
@ -718,7 +730,7 @@ sub HEOSMaster_ResponseProcessing($$) {
$json .= '","heos": {"command": "group/get_groups"}}';
Dispatch($hash,$json,undef);
Log3 $name, 4, "HEOSMaster ($name) - call Dispatcher";
Log3 $name, 4, "HEOSMaster ($name) - call Dispatcher for Groups";
}
return;
@ -727,10 +739,16 @@ sub HEOSMaster_ResponseProcessing($$) {
} elsif( defined($decode_json->{payload}{pid}) ) {
Dispatch($hash,$json,undef);
Log3 $name, 4, "HEOSMaster ($name) - call Dispatcher";
Log3 $name, 4, "HEOSMaster ($name) - call Dispatcher for PlayerInfo";
return;
} elsif( defined($decode_json->{heos}{message}) and $decode_json->{heos}{message} =~ /^pid=/ ) {
} elsif( defined($decode_json->{payload}{gid}) and defined($decode_json->{payload}{players}) ) {
Dispatch($hash,$json,undef);
Log3 $name, 4, "HEOSMaster ($name) - call Dispatcher for GroupInfo";
return;
} elsif( (defined($decode_json->{heos}{message})) and ($decode_json->{heos}{message} =~ /^pid=/ or $decode_json->{heos}{message} =~ /^gid=/) ) {
Dispatch($hash,$json,undef);
Log3 $name, 4, "HEOSMaster ($name) - call Dispatcher";
@ -863,6 +881,7 @@ sub HEOSMaster_PreProcessingReadings($$) {
} elsif ( $decode_json->{heos}{command} eq 'system/check_account' or $decode_json->{heos}{command} eq 'system/sign_in' ) {
my @value = split('&', $decode_json->{heos}{message});
if( $decode_json->{heos}{message} eq 'signed_out' ) {
$buffer{'heosAccount'} = $value[0];
@ -870,6 +889,7 @@ sub HEOSMaster_PreProcessingReadings($$) {
} else {
$buffer{'heosAccount'} = $value[0] . ' as ' . substr($value[1],3);
HEOSMaster_GetFavorites($hash) if( ReadingsVal($name,"enableChangeEvents", "off") eq "on" );
}
} else {
@ -914,7 +934,7 @@ sub HEOSMaster_GetGroups($) {
RemoveInternalTimer($hash,'HEOSMaster_GetGroups');
HEOSMaster_Write($hash,'getGroups',undef);
Log3 $name, 4, "HEOSMaster ($name) - getPlayers";
Log3 $name, 4, "HEOSMaster ($name) - getGroups";
}
sub HEOSMaster_EnableChangeEvents($) {

View File

@ -38,7 +38,7 @@ use JSON qw(decode_json);
use Encode qw(encode_utf8);
my $version = "0.1.55";
my $version = "0.1.56";
@ -58,6 +58,7 @@ sub HEOSPlayer_GetNowPlayingMedia($);
sub HEOSPlayer_GetPlayMode($);
sub HEOSPlayer_GetVolume($);
sub HEOSPlayer_Get($$@);
sub HEOSPlayer_GetMute($);
@ -150,12 +151,14 @@ sub HEOSPlayer_Define($$) {
InternalTimer( gettimeofday()+int(rand(6)), "HEOSPlayer_GetNowPlayingMedia", $hash, 0 );
InternalTimer( gettimeofday()+int(rand(8)), "HEOSPlayer_GetPlayMode", $hash, 0 );
InternalTimer( gettimeofday()+int(rand(10)), "HEOSPlayer_GetVolume", $hash, 0 );
InternalTimer( gettimeofday()+int(rand(12)), "HEOSPlayer_GetMute", $hash, 0 );
} else {
InternalTimer( gettimeofday()+15+int(rand(2)), "HEOSPlayer_GetPlayerInfo", $hash, 0 );
InternalTimer( gettimeofday()+15+int(rand(4)), "HEOSPlayer_GetPlayState", $hash, 0 );
InternalTimer( gettimeofday()+15+int(rand(6)), "HEOSPlayer_GetNowPlayingMedia", $hash, 0 );
InternalTimer( gettimeofday()+15+int(rand(8)), "HEOSPlayer_GetPlayMode", $hash, 0 );
InternalTimer( gettimeofday()+15+int(rand(10)), "HEOSPlayer_GetVolume", $hash, 0 );
InternalTimer( gettimeofday()+15+int(rand(12)), "HEOSPlayer_GetMute", $hash, 0 );
}
readingsBeginUpdate($hash);
@ -385,6 +388,11 @@ sub HEOSPlayer_Set($$@) {
$pid .= ",$defs{$args[0]}->{PID}";
$heosCmd = 'createGroup';
} elsif( $cmd eq 'clearGroup' ) {
return "usage: clearGroup" if( @args != 0 );
$heosCmd = 'createGroup';
} elsif( $cmd eq 'next' ) {
@ -451,10 +459,6 @@ sub HEOSPlayer_Set($$@) {
#$search =~ s/\s+/\ /g;
$search =~ s/\xC2\xA0/ /g;
#print "Suchkriterium\n".Dumper($search);
#print hexdump(\$search);
#print "Quellen\n".Dumper($hash->{IODev}{helper}{sources});
if ( scalar @args == 1 && scalar @sids == 1 ) {
readingsSingleUpdate($hash, "input", $args[0], 1);
@ -485,10 +489,6 @@ sub HEOSPlayer_Set($$@) {
my $search = $args[0];
$search =~ s/\xC2\xA0/ /g;
#print "Suchkriterium\n".Dumper($search);
#print hexdump(\$search);
#print "Medien\n".Dumper($hash->{IODev}{helper}{media});
my @ids = grep { $_->{name} =~ /\Q$search\E/i } (@{ $hash->{IODev}{helper}{media} });
#print "gefundenes Medium\n".Dumper(@ids);
@ -593,7 +593,6 @@ sub HEOSPlayer_Set($$@) {
@media = map { my %n; $n{name} = $_->{name}; $n{name} =~ s/\s+/\ /g; $n{name} } (@{ $hash->{IODev}{helper}{media}});
push(@media,"Alle") if grep { $_->{container} =~ /yes/i && $_->{playable} =~ /yes/i} (@{ $hash->{IODev}{helper}{media} });
#print "#####################################################\n".Dumper(@media);
$list .= " media:".join(",",@media) if( scalar @media > 0 );
}
@ -807,6 +806,10 @@ sub HEOSPlayer_PreProcessingReadings($$) {
$buffer{'shuffle'} = substr($value[2],8);
} elsif ( $decode_json->{heos}{command} =~ /get_mute/ ) {
my @value = split('&', $decode_json->{heos}{message});
$buffer{'mute'} = substr($value[1],6);
} elsif ( $decode_json->{heos}{command} =~ /volume_up/ or $decode_json->{heos}{command} =~ /volume_down/ ) {
my @value = split('&', $decode_json->{heos}{message});
@ -905,6 +908,15 @@ sub HEOSPlayer_GetVolume($) {
}
sub HEOSPlayer_GetMute($) {
my $hash = shift;
RemoveInternalTimer($hash,'HEOSPlayer_GetMute');
IOWrite($hash,'getMute',"pid=$hash->{PID}");
}