mod-HEOS/21_HEOSPlayer.pm

918 lines
32 KiB
Perl
Raw Normal View History

###############################################################################
#
# Developed with Kate
#
2017-02-02 21:56:39 +00:00
# (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
# any later version.
#
# The GNU General Public License can be found at
# http://www.gnu.org/copyleft/gpl.html.
# A copy is found in the textfile GPL.txt and important notices to the license
# from the author is found in LICENSE.txt distributed with these scripts.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#
# $Id$
#
###############################################################################
package main;
use strict;
use warnings;
use JSON qw(decode_json);
use Encode qw(encode_utf8);
use Data::Dumper;
2017-02-27 11:26:58 +00:00
my $version = "0.1.61";
# Declare functions
sub HEOSPlayer_Initialize($);
sub HEOSPlayer_Define($$);
sub HEOSPlayer_Undef($$);
sub HEOSPlayer_Attr(@);
sub HEOSPlayer_Parse($$);
sub HEOSPlayer_WriteReadings($$);
sub HEOSPlayer_Set($$@);
sub HEOSPlayer_PreProcessingReadings($$);
2017-01-27 21:22:30 +00:00
sub HEOSPlayer_GetPlayerInfo($);
sub HEOSPlayer_GetPlayState($);
sub HEOSPlayer_GetNowPlayingMedia($);
sub HEOSPlayer_GetPlayMode($);
2017-02-09 10:43:17 +00:00
sub HEOSPlayer_GetVolume($);
sub HEOSPlayer_Get($$@);
sub HEOSPlayer_GetMute($);
2017-01-27 21:22:30 +00:00
sub HEOSPlayer_Initialize($) {
2017-02-27 08:39:48 +00:00
my ($hash) = @_;
$hash->{Match} = '.*{"command":."player.*|.*{"command":."event/player.*|.*{"command":."event\/repeat_mode_changed.*|.*{"command":."event\/shuffle_mode_changed.*|.*{"command":."event\/favorites_changed.*';
2017-02-27 08:39:48 +00:00
# Provider
$hash->{SetFn} = "HEOSPlayer_Set";
$hash->{GetFn} = "HEOSPlayer_Get";
$hash->{DefFn} = "HEOSPlayer_Define";
$hash->{UndefFn} = "HEOSPlayer_Undef";
$hash->{AttrFn} = "HEOSPlayer_Attr";
$hash->{ParseFn} = "HEOSPlayer_Parse";
$hash->{AttrList} = "IODev ".
"disable:1 ".
2017-02-09 10:43:17 +00:00
"mute2play:1 ".
$readingFnAttributes;
foreach my $d(sort keys %{$modules{HEOSPlayer}{defptr}}) {
2017-02-27 08:39:48 +00:00
my $hash = $modules{HEOSPlayer}{defptr}{$d};
$hash->{VERSION} = $version;
}
}
sub HEOSPlayer_Define($$) {
2017-02-27 08:39:48 +00:00
my ( $hash, $def ) = @_;
my @a = split( "[ \t]+", $def );
splice( @a, 1, 1 );
my $iodev;
my $i = 0;
2017-02-27 08:39:48 +00:00
foreach my $param ( @a ) {
if( $param =~ m/IODev=([^\s]*)/ ) {
2017-02-27 08:39:48 +00:00
$iodev = $1;
splice( @a, $i, 3 );
last;
}
2017-02-27 08:39:48 +00:00
$i++;
}
2017-02-27 08:39:48 +00:00
return "too few parameters: define <name> HEOSPlayer <pid>" if( @a < 2 );
2017-02-27 08:39:48 +00:00
my ($name,$pid) = @a;
$hash->{PID} = $pid;
$hash->{VERSION} = $version;
AssignIoPort($hash,$iodev) if( !$hash->{IODev} );
2017-02-27 08:39:48 +00:00
if(defined($hash->{IODev}->{NAME})) {
2017-02-27 08:39:48 +00:00
Log3 $name, 3, "HEOSPlayer ($name) - I/O device is " . $hash->{IODev}->{NAME};
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
Log3 $name, 1, "HEOSPlayer ($name) - no I/O device";
}
2017-02-27 08:39:48 +00:00
$iodev = $hash->{IODev}->{NAME};
my $code = abs($pid);
2017-02-27 08:39:48 +00:00
$code = $iodev."-".$code if( defined($iodev) );
my $d = $modules{HEOSPlayer}{defptr}{$code};
2017-02-27 08:39:48 +00:00
return "HEOSPlayer device $hash->{pid} on HEOSMaster $iodev already defined as $d->{NAME}."
if( defined($d) && $d->{IODev} == $hash->{IODev} && $d->{NAME} ne $name );
2017-02-27 08:39:48 +00:00
Log3 $name, 3, "HEOSPlayer ($name) - defined with Code: $code";
$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} ) );
2017-02-27 08:39:48 +00:00
if( $init_done ) {
2017-02-27 08:39:48 +00:00
2017-02-09 10:43:17 +00:00
InternalTimer( gettimeofday()+int(rand(2)), "HEOSPlayer_GetPlayerInfo", $hash, 0 );
InternalTimer( gettimeofday()+int(rand(4)), "HEOSPlayer_GetPlayState", $hash, 0 );
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 );
2017-02-27 08:39:48 +00:00
InternalTimer( gettimeofday()+int(rand(12)), "HEOSPlayer_GetMute", $hash, 0 );
} else {
2017-02-27 08:39:48 +00:00
2017-02-09 10:43:17 +00:00
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 );
}
2017-02-27 08:39:48 +00:00
readingsBeginUpdate($hash);
readingsBulkUpdate($hash, 'state','Initialized');
readingsBulkUpdate($hash, 'volumeUp', 5);
readingsBulkUpdate($hash, 'volumeDown', 5);
readingsEndUpdate($hash, 1);
2017-02-27 08:39:48 +00:00
2017-02-09 10:43:17 +00:00
$modules{HEOSPlayer}{defptr}{$code} = $hash;
return undef;
}
sub HEOSPlayer_Undef($$) {
2017-02-27 08:39:48 +00:00
my ( $hash, $arg ) = @_;
my $pid = $hash->{PID};
my $name = $hash->{NAME};
2017-02-27 08:39:48 +00:00
RemoveInternalTimer($hash);
my $code = abs($pid);
$code = $hash->{IODev}->{NAME} ."-". $code if( defined($hash->{IODev}->{NAME}) );
delete($modules{HEOSPlayer}{defptr}{$code});
2017-02-27 08:39:48 +00:00
2017-01-27 21:22:30 +00:00
Log3 $name, 3, "HEOSPlayer ($name) - device $name deleted with Code: $code";
return undef;
}
sub HEOSPlayer_Attr(@) {
2017-02-27 08:39:48 +00:00
my ( $cmd, $name, $attrName, $attrVal ) = @_;
my $hash = $defs{$name};
my $token = $hash->{IODev}->{TOKEN};
2017-02-27 08:39:48 +00:00
if( $attrName eq "disable" ) {
if( $cmd eq "set" and $attrVal eq "1" ) {
2017-02-27 08:39:48 +00:00
readingsSingleUpdate ( $hash, "state", "disabled", 1 );
Log3 $name, 3, "HEOSPlayer ($name) - disabled";
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq "del" ) {
readingsSingleUpdate ( $hash, "state", "active", 1 );
Log3 $name, 3, "HEOSPlayer ($name) - enabled";
}
}
2017-02-27 08:39:48 +00:00
if( $attrName eq "disabledForIntervals" ) {
if( $cmd eq "set" ) {
2017-02-27 08:39:48 +00:00
Log3 $name, 3, "HEOSPlayer ($name) - enable disabledForIntervals";
readingsSingleUpdate ( $hash, "state", "Unknown", 1 );
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq "del" ) {
readingsSingleUpdate ( $hash, "state", "active", 1 );
Log3 $name, 3, "HEOSPlayer ($name) - delete disabledForIntervals";
}
}
}
sub HEOSPlayer_Get($$@) {
2017-02-27 08:39:48 +00:00
my ($hash, $name, @aa) = @_;
my ($cmd, @args) = @aa;
my $pid = $hash->{PID};
my $result = "";
2017-02-27 08:39:48 +00:00
#print "CL ###################################################\n".Dumper($hash->{CL});
$hash->{helper}{cl} = $hash->{CL} if( ref($hash->{CL}) eq 'HASH' );
#Leerzeichen müßen für die Rückgabe escaped werden sonst werden sie falsch angezeigt
if( $cmd eq 'playlists' ) {
2017-02-27 08:39:48 +00:00
#gibt die Playlisten durch Komma getrennt zurück
my @playlists = map { my %n; $n{name} = $_->{name}; $n{name} =~ s/\s+/\&nbsp;/g; $n{name} } (@{ $hash->{IODev}{helper}{playlists}});
$result .= join(",",@playlists) if( scalar @playlists > 0 );
return $result;
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'channels' ) {
2017-02-27 08:39:48 +00:00
#gibt die Favoriten durch Komma getrennt zurück
my @channels = map { my %n; $n{name} = $_->{name}; $n{name} =~ s/\s+/\&nbsp;/g; $n{name} } (@{ $hash->{IODev}{helper}{favorites}});
$result .= join(",",@channels) if( scalar @channels > 0 );
return $result;
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'channelscount' ) {
2017-02-27 08:39:48 +00:00
#gibt die Favoritenanzahl zurück
return scalar(@{$hash->{IODev}{helper}{favorites}}) if ( defined $hash->{IODev}{helper}{favorites} );
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'inputs' ) {
2017-02-27 08:39:48 +00:00
#gibt die Quellen durch Komma getrennt zurück
my @inputs = map { my %n; $n{name} = $_->{name}; $n{name} =~ s/\s+/\&nbsp;/g; $n{name} } (@{ $hash->{IODev}{helper}{sources}});
push(@inputs, "Warteschlange");
$result .= join(",",@inputs) if( scalar @inputs > 0 );
return $result;
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'search' ) {
2017-02-27 08:39:48 +00:00
return "usage: search <keywords>" if( @args != 1 );
}
my $list = 'playlists:noArg channels:noArg channelscount:noArg inputs:noArg ls search';
return "Unknown argument $cmd, choose one of $list";
}
sub HEOSPlayer_Set($$@) {
2017-02-27 08:39:48 +00:00
my ($hash, $name, @aa) = @_;
my ($cmd, @args) = @aa;
my $pid = $hash->{PID};
my $action;
my $heosCmd;
my $rvalue;
my $favoritcount = 1;
my $qcount = 1;
2017-02-27 08:39:48 +00:00
my $string = '';
2017-02-27 08:39:48 +00:00
#print "cmd ###################################################\n".Dumper($cmd);
if( $cmd eq 'getPlayerInfo' ) {
return "usage: getPlayerInfo" if( @args != 0 );
2017-02-27 08:39:48 +00:00
$heosCmd = $cmd;
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'getPlayState' ) {
return "usage: getPlayState" if( @args != 0 );
2017-02-27 08:39:48 +00:00
$heosCmd = $cmd;
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'getPlayMode' ) {
return "usage: getPlayMode" if( @args != 0 );
2017-02-27 08:39:48 +00:00
$heosCmd = $cmd;
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'getNowPlayingMedia' ) {
return "usage: getNowPlayingMedia" if( @args != 0 );
2017-02-27 08:39:48 +00:00
$heosCmd = $cmd;
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'repeat' ) {
return "usage: repeat one,all,off" if( @args != 1 );
2017-02-27 08:39:48 +00:00
$heosCmd = 'setPlayMode';
2017-02-27 08:39:48 +00:00
$rvalue = 'on_'.$args[0];
$rvalue = 'off' if($rvalue eq 'on_off');
$action = "repeat=$rvalue&shuffle=".ReadingsVal($name,'shuffle','off');
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'shuffle' ) {
return "usage: shuffle on,off" if( @args != 1 );
2017-02-27 08:39:48 +00:00
$heosCmd = 'setPlayMode';
2017-02-27 08:39:48 +00:00
$rvalue = 'on_'.ReadingsVal($name,'repeat','off');
$rvalue = 'off' if($rvalue eq 'on_off');
$action = "repeat=$rvalue&shuffle=$args[0]";
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'play' ) {
return "usage: play" if( @args != 0 );
2017-02-27 08:39:48 +00:00
$heosCmd = 'setPlayState';
2017-01-27 21:22:30 +00:00
$action = "state=$cmd";
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'stop' ) {
return "usage: stop" if( @args != 0 );
2017-02-27 08:39:48 +00:00
$heosCmd = 'setPlayState';
2017-01-27 21:22:30 +00:00
$action = "state=$cmd";
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'pause' ) {
return "usage: pause" if( @args != 0 );
2017-02-27 08:39:48 +00:00
$heosCmd = 'setPlayState';
2017-01-27 21:22:30 +00:00
$action = "state=$cmd";
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'mute' ) {
return "usage: mute on/off" if( @args != 1 );
2017-02-27 08:39:48 +00:00
$heosCmd = 'setMute';
$action = "state=$args[0]";
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'volume' ) {
return "usage: volume 0-100" if( @args != 1 );
2017-02-27 08:39:48 +00:00
$heosCmd = 'setVolume';
$action = "level=$args[0]";
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'volumeUp' ) {
return "usage: volumeUp 0-10" if( @args != 1 );
2017-02-27 08:39:48 +00:00
2017-02-09 10:43:17 +00:00
$heosCmd = $cmd;
$action = "step=$args[0]";
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'volumeDown' ) {
return "usage: volumeDown 0-10" if( @args != 1 );
2017-02-27 08:39:48 +00:00
2017-02-09 10:43:17 +00:00
$heosCmd = $cmd;
$action = "step=$args[0]";
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'groupWithMember' ) {
return "usage: groupWithMember" if( @args != 1 );
2017-02-27 08:39:48 +00:00
$pid .= ",$defs{$args[0]}->{PID}";
$heosCmd = 'createGroup';
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'clearGroup' ) {
return "usage: clearGroup" if( @args != 0 );
2017-02-27 08:39:48 +00:00
$heosCmd = 'createGroup';
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'next' ) {
return "usage: next" if( @args != 0 );
2017-02-27 08:39:48 +00:00
$heosCmd = 'playNext';
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'prev' ) {
return "usage: prev" if( @args != 0 );
2017-02-27 08:39:48 +00:00
$heosCmd = 'playPrev';
2017-02-27 08:39:48 +00:00
} elsif ( $cmd =~ /channel/ ) {
2017-02-27 08:39:48 +00:00
my $favorit = ReadingsVal($name,"channel", 0);
2017-02-27 08:39:48 +00:00
$favoritcount = scalar(@{$hash->{IODev}{helper}{favorites}}) if ( defined $hash->{IODev}{helper}{favorites} );
$heosCmd = 'playPresetStation';
2017-02-27 08:39:48 +00:00
if ( $cmd eq 'channel' ) {
return "usage: channel 1-$favoritcount" if( @args != 1 );
2017-02-27 08:39:48 +00:00
$action = "preset=$args[0]";
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'channelUp' ) {
return "usage: channelUp" if( @args != 0 );
2017-02-27 08:39:48 +00:00
$favorit = $favoritcount if ( ++$favorit > $favoritcount );
$action = "preset=".$favorit;
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'channelDown' ) {
2017-02-27 08:39:48 +00:00
$favorit = 1 if ( --$favorit <= 0 );
$action = "preset=".$favorit;
}
2017-02-27 08:39:48 +00:00
} elsif ( $cmd =~ /Playlist/ ) {
2017-02-27 08:39:48 +00:00
my @cids = map { $_->{cid} } grep { $_->{name} =~ /$args[0]/i } (@{ $hash->{IODev}{helper}{playlists} });
if ( scalar @args == 1 && scalar @cids > 0 ) {
if ( $cmd eq 'playPlaylist' ) {
2017-02-27 08:39:48 +00:00
$heosCmd = $cmd;
$action = "sid=1025&cid=$cids[0]&aid=4";
2017-02-27 08:39:48 +00:00
} elsif ( $cmd eq 'deletePlaylist' ) {
2017-02-27 08:39:48 +00:00
$heosCmd = $cmd;
$action = "cid=$cids[0]";
$string = "sid=1025";
}
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
IOWrite($hash,'browseSource','sid=1025');
my @playlists = map { $_->{name} } (@{ $hash->{IODev}{helper}{playlists}});
return "usage: $cmd ".join(",",@playlists);
}
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'input' ) {
2017-02-27 08:39:48 +00:00
my @sids;
2017-02-27 08:39:48 +00:00
my $search = $args[0];
$search =~ s/\xC2\xA0/ /g;
#$search =~ s/\s+/\&nbsp;/g;
2017-02-27 08:39:48 +00:00
if ( $search =~ /Warteschlange/ ) {
2017-02-27 08:39:48 +00:00
push(@sids, "9999");
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
@sids = map { $_->{sid} } grep { $_->{name} =~ /\Q$search\E/i } (@{ $hash->{IODev}{helper}{sources} });
}
2017-02-27 08:39:48 +00:00
if ( scalar @args == 1 && scalar @sids > 0 ) {
2017-02-27 08:39:48 +00:00
readingsSingleUpdate($hash, "input", $args[0], 1);
#sid des Input für Container merken
readingsSingleUpdate($hash, ".input", $sids[0], 1);
#alten Container löschen bei Inputwechsel
readingsSingleUpdate($hash, ".cid", 0, 1);
2017-02-27 08:39:48 +00:00
if ( $sids[0] eq "9999" ) {
2017-02-27 08:39:48 +00:00
$heosCmd = 'getQueue';
2017-02-27 08:39:48 +00:00
} else {
$heosCmd = 'browseSource';
$action = "sid=$sids[0]";
2017-02-27 08:39:48 +00:00
}
2017-02-27 08:39:48 +00:00
Log3 $name, 4, "HEOSPlayer ($name) - set input with sid $sids[0] and name $args[0]";
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
my @inputs = map { $_->{name} } (@{ $hash->{IODev}{helper}{sources}});
push(@inputs, "Warteschlange");
return "usage: input ".join(",",@inputs);
}
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq 'media' ) {
2017-02-27 08:39:48 +00:00
my @ids;
2017-02-27 08:39:48 +00:00
my $search = $args[0];
my $sid = ReadingsVal($name,".input", "9999");
2017-02-27 08:39:48 +00:00
return "usage: set input first" unless( defined($sid) );
if ( scalar @args == 1 ) {
2017-02-27 08:39:48 +00:00
$search =~ s/\xC2\xA0/ /g;
if ( $sid eq "9999" ) {
2017-02-27 08:39:48 +00:00
@ids = grep { $_->{song} =~ /\Q$search\E/i } (@{ $hash->{helper}{queue} });
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
@ids = grep { $_->{name} =~ /\Q$search\E/i } (@{ $hash->{IODev}{helper}{media} });
2017-02-27 08:39:48 +00:00
}
if ( scalar @ids > 0 ) {
if ( exists $ids[0]{cid} ) {
#hier Container verarbeiten
if ( $ids[0]{playable} eq "yes" ) {
2017-02-27 08:39:48 +00:00
#alles abspielen
$heosCmd = 'playPlaylist';
$action = "sid=$sid&cid=$ids[0]{cid}&aid=4";
#Container merken
readingsSingleUpdate($hash, ".cid", 0, 1);
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
#mehr einlesen
readingsSingleUpdate($hash, ".cid", $ids[0]{cid}, 1);
$heosCmd = 'browseSource';
$action = "sid=$sid&cid=$ids[0]{cid}";
}
2017-02-27 08:39:48 +00:00
} elsif ( exists $ids[0]{qid} ) {
2017-02-27 08:39:48 +00:00
$heosCmd = 'playQueue';
$action = "qid=$ids[0]{qid}";
2017-02-27 08:39:48 +00:00
} elsif ( exists $ids[0]{mid} ) {
#hier Medien verarbeiten
if ( $ids[0]{mid} =~ /inputs\// ) {
2017-02-27 08:39:48 +00:00
#Input abspielen
$heosCmd = 'playInput';
$action = "input=$ids[0]{mid}";
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
#aktuellen Container holen
my $cid = ReadingsVal($name,".cid", undef);
2017-02-27 08:39:48 +00:00
if ( defined $cid ) {
if ( $ids[0]{type} eq "station" ) {
2017-02-27 08:39:48 +00:00
#Radio abspielen
$heosCmd = 'playStream';
$action = "sid=$sid&cid=$cid&mid=$ids[0]{mid}";
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
#Song abspielen
$heosCmd = 'playPlaylist';
$action = "sid=$sid&cid=$cid&mid=$ids[0]{mid}&aid=4";
}
}
}
}
}
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
my @media;
if ( $sid eq "9999" ) {
2017-02-27 08:39:48 +00:00
@media = map { $_->{song} } (@{ $hash->{helper}{queue}});
} else {
@media = map { $_->{name} } (@{ $hash->{IODev}{helper}{media}});
}
2017-02-27 08:39:48 +00:00
return "usage: media ".join(",",@media);
}
2017-02-27 08:39:48 +00:00
} elsif ( $cmd eq 'clearQueue' ) {
#löscht die Warteschlange
return "usage: clearQueue" if( @args != 0 );
2017-02-27 08:39:48 +00:00
$heosCmd = 'clearQueue';
delete $hash->{helper}{queue};
2017-02-27 08:39:48 +00:00
} elsif ( $cmd eq 'saveQueue' ) {
#speichert die aktuelle Warteschlange als Playlist ab
return "usage: saveQueue" if( @args != 1 );
2017-02-27 08:39:48 +00:00
$heosCmd = 'saveQueue';
2017-02-27 08:39:48 +00:00
$action = "name=$args[0]";
} elsif ( $cmd eq 'history' ) {
return "usage: history track,channel" if( @args != 1 );
2017-02-27 08:39:48 +00:00
$heosCmd = "browseSource";
$action = "sid=1026&cid=TRACKS" if ( $args[0] eq "track" );
2017-02-27 08:39:48 +00:00
$action = "sid=1026&cid=STATIONS" if ( $args[0] eq "channel" );
} else {
2017-02-27 08:39:48 +00:00
my @playlists;
my @inputs;
my @media;
my @queue;
2017-02-27 08:39:48 +00:00
my $sid = ReadingsVal($name,".input", "9999");
my $list = "getPlayerInfo:noArg getPlayState:noArg getNowPlayingMedia:noArg getPlayMode:noArg play:noArg stop:noArg pause:noArg mute:on,off volume:slider,0,5,100 volumeUp:slider,0,1,10 volumeDown:slider,0,1,10 repeat:one,all,off shuffle:on,off channelUp:noArg channelDown:noArg next:noArg prev:noArg history:track,channel ";
$list .= "groupWithMember:" . join( ",", devspec2array("TYPE=HEOSPlayer:FILTER=NAME!=$name") );
#Parameterlisten für FHEMWeb zusammen bauen
$list .= " channel:slider,1,1,".scalar(@{$hash->{IODev}{helper}{favorites}}) if ( defined $hash->{IODev}{helper}{favorites} );
#$list .= " playQueue:slider,1,1,".scalar(@{$hash->{helper}{queue}}) if ( defined $hash->{helper}{queue} );
2017-02-27 08:39:48 +00:00
if ( defined $hash->{IODev}{helper}{playlists} ) {
2017-02-27 08:39:48 +00:00
@playlists = map { my %n; $n{name} = $_->{name}; $n{name} =~ s/\s+/\&nbsp;/g; $n{name} } (@{ $hash->{IODev}{helper}{playlists}});
$list .= " playPlaylist:".join(",",@playlists) if( scalar @playlists > 0 );
$list .= " deletePlaylist:".join(",",@playlists) if( scalar @playlists > 0 );
#$list .= " renamePlaylist:".join(",",@playlists) if( scalar @playlists > 0 );
}
2017-02-27 08:39:48 +00:00
if ( defined $hash->{IODev}{helper}{sources}) {
@inputs = map { my %n; $n{name} = $_->{name}; $n{name} =~ s/\s+/\&nbsp;/g; $n{name} } (@{ $hash->{IODev}{helper}{sources}});
push(@inputs, "Warteschlange");
$list .= " input:".join(",",@inputs) if( scalar @inputs > 0 );
2017-02-27 08:39:48 +00:00
}
if ( $sid eq "9999" ) {
@media = map { my %n; $n{name} = $_->{song}; $n{name} =~ s/\s+/\&nbsp;/g; $n{name} } (@{ $hash->{helper}{queue}});
$list .= " clearQueue:noArg saveQueue";
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
@media = map { my %n; $n{name} = $_->{name}; $n{name} =~ s/\s+/\&nbsp;/g; $n{name} } (@{ $hash->{IODev}{helper}{media}}) if ( defined $hash->{IODev}{helper}{media});
}
2017-02-27 08:39:48 +00:00
$list .= " media:".join(",",@media) if( scalar @media > 0 );
return "Unknown argument $cmd, choose one of $list";
}
2017-02-27 08:39:48 +00:00
$string .= "pid=$pid";
$string .= "&$action" if( defined($action));
IOWrite($hash,"$heosCmd","$string");
Log3 $name, 4, "HEOSPlayer ($name) - IOWrite: $heosCmd $string IODevHash=$hash->{IODev}";
return undef;
}
sub HEOSPlayer_Parse($$) {
2017-02-27 08:39:48 +00:00
my ($io_hash,$json) = @_;
my $name = $io_hash->{NAME};
my $pid;
my $decode_json;
2017-02-27 08:39:48 +00:00
my $code;
2017-02-27 08:39:48 +00:00
$decode_json = decode_json(encode_utf8($json));
Log3 $name, 4, "HEOSPlayer - ParseFn wurde aufgerufen";
if( defined($decode_json->{pid}) ) {
2017-02-27 08:39:48 +00:00
$pid = $decode_json->{pid};
$code = abs($pid);
$code = $io_hash->{NAME} ."-". $code if( defined($io_hash->{NAME}) );
2017-02-27 08:39:48 +00:00
if( my $hash = $modules{HEOSPlayer}{defptr}{$code} ) {
IOWrite($hash,'getPlayerInfo',"pid=$hash->{PID}");
Log3 $hash->{NAME}, 4, "HEOSPlayer ($hash->{NAME}) - find logical device: $hash->{NAME}";
Log3 $hash->{NAME}, 4, "HEOSPlayer ($hash->{NAME}) - find PID in root from decode_json";
return $hash->{NAME};
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
my $devname = "HEOSPlayer".abs($pid);
return "UNDEFINED $devname HEOSPlayer $pid IODev=$name";
}
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
my %message = map { my ( $key, $value ) = split "="; $key => $value } split('&', $decode_json->{heos}{message});
$pid = $message{pid} if( defined($message{pid}) );
$pid = $decode_json->{payload}{pid} if( defined($decode_json->{payload}{pid}) );
Log3 $name, 4, "HEOSPlayer ($name) PID: $pid";
$code = abs($pid);
$code = $io_hash->{NAME} ."-". $code if( defined($io_hash->{NAME}) );
2017-02-27 08:39:48 +00:00
if( my $hash = $modules{HEOSPlayer}{defptr}{$code} ) {
2017-02-27 08:39:48 +00:00
my $name = $hash->{NAME};
2017-02-27 08:39:48 +00:00
if ( $decode_json->{heos}{command} =~ /get_queue/ ) {
Log3 $name, 3, "HEOSPlayer ($name) - call getQueue for $message{pid}";
if ( defined $message{range} ) {
$message{range} =~ s/(\d+)\,\d+/$1/;
} else {
$message{range} = 0;
$hash->{helper}{queue} = [];
}
my $start = $message{range} + $message{returned};
2017-02-27 08:39:48 +00:00
push( @{$hash->{helper}{queue}}, (@{$decode_json->{payload}}) );
if ( $start < $message{count} ) {
2017-02-27 08:39:48 +00:00
IOWrite($hash,'getQueue',"pid=$message{pid}&range=$start,".($start + 100));
Log3 $name, 3, "HEOSMaster ($name) - call getQueue with pid $message{pid} next Range from $message{returned}";
}
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
HEOSPlayer_WriteReadings($hash,$decode_json);
Log3 $name, 4, "HEOSPlayer ($name) - find logical device: $hash->{NAME}";
}
2017-02-27 08:39:48 +00:00
return $hash->{NAME};
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
my $devname = "HEOSPlayer".abs($pid);
return "UNDEFINED $devname HEOSPlayer $pid IODev=$name";
}
}
}
sub HEOSPlayer_WriteReadings($$) {
2017-02-27 08:39:48 +00:00
my ($hash,$decode_json) = @_;
my $name = $hash->{NAME};
2017-02-27 08:39:48 +00:00
Log3 $name, 3, "HEOSPlayer ($name) - processing data to write readings";
############################
#### Aufbereiten der Daten soweit nötig (bei Events zum Beispiel)
2017-01-27 21:22:30 +00:00
my $readingsHash = HEOSPlayer_PreProcessingReadings($hash,$decode_json)
if( $decode_json->{heos}{message} =~ /^pid=/ and $decode_json->{heos}{command} ne "player\/get_now_playing_media");
############################
#### schreiben der Readings
readingsBeginUpdate($hash);
### Event Readings
2017-01-28 16:06:00 +00:00
if( ref($readingsHash) eq "HASH" ) {
2017-02-27 08:39:48 +00:00
2017-01-27 21:22:30 +00:00
Log3 $name, 4, "HEOSPlayer ($name) - response json Hash back from HEOSPlayer_PreProcessingReadings";
my $t;
my $v;
2017-02-27 08:39:48 +00:00
2017-02-09 10:43:17 +00:00
while( ( $t, $v ) = each (%{$readingsHash}) ) {
readingsBulkUpdate( $hash, $t, $v ) if( defined( $v ) );
}
}
### PlayerInfos
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
2017-01-27 21:22:30 +00:00
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} );
#sucht in den Favoriten nach der aktuell gespielten Radiostation und aktualisiert den channel wenn diese enthalten ist
my @presets = map { $_->{name} } (@{ $hash->{IODev}{helper}{favorites} });
my $search = ReadingsVal($name,"currentStation" ,undef);
my( @index )= grep { $presets[$_] eq $search } 0..$#presets if ( defined $search );
2017-02-27 08:39:48 +00:00
readingsBulkUpdate( $hash, 'channel', $index[0]+1 ) if ( scalar @index > 0 );
2017-01-27 21:22:30 +00:00
readingsBulkUpdate( $hash, 'state', 'on' );
readingsEndUpdate( $hash, 1 );
Log3 $name, 5, "HEOSPlayer ($name) - readings set for $name";
return undef;
}
2017-01-27 21:22:30 +00:00
###############
2017-01-27 21:22:30 +00:00
### my little Helpers
sub HEOSPlayer_PreProcessingReadings($$) {
2017-02-27 08:39:48 +00:00
my ($hash,$decode_json) = @_;
my $name = $hash->{NAME};
my $reading;
2017-01-27 21:22:30 +00:00
my %buffer;
2017-02-27 08:39:48 +00:00
my %message = map { my ( $key, $value ) = split "="; $key => $value } split('&', $decode_json->{heos}{message});
Log3 $name, 4, "HEOSPlayer ($name) - preprocessing readings";
if ( $decode_json->{heos}{command} =~ /play_state/ or $decode_json->{heos}{command} =~ /player_state_changed/ ) {
$buffer{'playStatus'} = $message{state};
2017-02-09 10:43:17 +00:00
} elsif ( $decode_json->{heos}{command} =~ /volume_changed/ or $decode_json->{heos}{command} =~ /set_volume/ or $decode_json->{heos}{command} =~ /get_volume/ ) {
2017-01-27 21:22:30 +00:00
my @value = split('&', $decode_json->{heos}{message});
$buffer{'volume'} = $message{level};
$buffer{'mute'} = $message{mute} if( $decode_json->{heos}{command} =~ /volume_changed/ );
2017-02-09 10:43:17 +00:00
if (defined($buffer{'mute'}) && AttrVal($name, 'mute2play', 0) == 1) {
IOWrite($hash,'setPlayState',"pid=$hash->{PID}&state=play") if $buffer{'mute'} eq "off";
IOWrite($hash,'setPlayState',"pid=$hash->{PID}&state=stop") if $buffer{'mute'} eq "on";
}
2017-02-27 08:39:48 +00:00
} elsif ( $decode_json->{heos}{command} =~ /play_mode/ or $decode_json->{heos}{command} =~ /repeat_mode_changed/ or $decode_json->{heos}{command} =~ /shuffle_mode_changed/ ) {
2017-02-27 08:39:48 +00:00
$buffer{'shuffle'} = $message{shuffle};
$buffer{'repeat'} = $message{repeat};
2017-02-27 11:26:58 +00:00
$buffer{'repeat'} =~ s/.*\_(.*)/$1/g;
2017-02-27 08:39:48 +00:00
} elsif ( $decode_json->{heos}{command} =~ /get_mute/ ) {
2017-02-27 08:39:48 +00:00
$buffer{'mute'} = $message{state};
2017-02-27 08:39:48 +00:00
} elsif ( $decode_json->{heos}{command} =~ /volume_up/ or $decode_json->{heos}{command} =~ /volume_down/ ) {
2017-02-27 08:39:48 +00:00
$buffer{'volumeUp'} = $message{step} if( $decode_json->{heos}{command} =~ /volume_up/ );
$buffer{'volumeDown'} = $message{step} if( $decode_json->{heos}{command} =~ /volume_down/ );
2017-02-27 08:39:48 +00:00
} elsif ( $decode_json->{heos}{command} =~ /player_now_playing_changed/ or $decode_json->{heos}{command} =~ /favorites_changed/ ) {
2017-01-27 21:22:30 +00:00
IOWrite($hash,'getNowPlayingMedia',"pid=$hash->{PID}");
2017-02-27 08:39:48 +00:00
} elsif ( $decode_json->{heos}{command} =~ /play_preset/ ) {
2017-02-27 08:39:48 +00:00
$buffer{'channel'} = $message{preset}
} elsif ( $decode_json->{heos}{command} =~ /play_input/ ) {
2017-02-27 08:39:48 +00:00
$buffer{'input'} = $message{input};
} elsif ( $decode_json->{heos}{command} =~ /playback_error/ ) {
2017-02-27 08:39:48 +00:00
$buffer{'error'} = $message{error};
} else {
2017-02-27 08:39:48 +00:00
Log3 $name, 3, "HEOSPlayer ($name) - no match found";
return undef;
}
2017-02-27 08:39:48 +00:00
2017-01-27 21:22:30 +00:00
Log3 $name, 4, "HEOSPlayer ($name) - Match found for decode_json";
return \%buffer;
}
sub HEOSPlayer_GetPlayerInfo($) {
2017-02-27 08:39:48 +00:00
2017-01-27 21:22:30 +00:00
my $hash = shift;
2017-02-27 08:39:48 +00:00
2017-01-27 21:22:30 +00:00
RemoveInternalTimer($hash,'HEOSPlayer_GetPlayerInfo');
IOWrite($hash,'getPlayerInfo',"pid=$hash->{PID}");
}
sub HEOSPlayer_GetPlayState($) {
2017-02-27 08:39:48 +00:00
2017-01-27 21:22:30 +00:00
my $hash = shift;
2017-02-27 08:39:48 +00:00
2017-01-27 21:22:30 +00:00
RemoveInternalTimer($hash,'HEOSPlayer_GetPlayState');
IOWrite($hash,'getPlayState',"pid=$hash->{PID}");
}
sub HEOSPlayer_GetPlayMode($) {
2017-02-27 08:39:48 +00:00
my $hash = shift;
2017-02-27 08:39:48 +00:00
RemoveInternalTimer($hash,'HEOSPlayer_GetPlayMode');
IOWrite($hash,'getPlayMode',"pid=$hash->{PID}");
}
2017-01-27 21:22:30 +00:00
sub HEOSPlayer_GetNowPlayingMedia($) {
2017-02-27 08:39:48 +00:00
2017-01-27 21:22:30 +00:00
my $hash = shift;
2017-02-27 08:39:48 +00:00
2017-01-27 21:22:30 +00:00
RemoveInternalTimer($hash,'HEOSPlayer_GetNowPlayingMedia');
IOWrite($hash,'getNowPlayingMedia',"pid=$hash->{PID}");
}
2017-02-09 10:43:17 +00:00
sub HEOSPlayer_GetVolume($) {
2017-02-27 08:39:48 +00:00
2017-02-09 10:43:17 +00:00
my $hash = shift;
2017-02-27 08:39:48 +00:00
2017-02-09 10:43:17 +00:00
RemoveInternalTimer($hash,'HEOSPlayer_GetVolume');
IOWrite($hash,'getVolume',"pid=$hash->{PID}");
}
sub HEOSPlayer_GetMute($) {
2017-02-27 08:39:48 +00:00
my $hash = shift;
2017-02-27 08:39:48 +00:00
RemoveInternalTimer($hash,'HEOSPlayer_GetMute');
IOWrite($hash,'getMute',"pid=$hash->{PID}");
}
sub HEOSPlayer_GetQueue($) {
2017-02-27 08:39:48 +00:00
my $hash = shift;
2017-02-27 08:39:48 +00:00
RemoveInternalTimer($hash,'HEOSPlayer_GetQueue');
IOWrite($hash,'getQueue',"pid=$hash->{PID}");
}
1;