mod-HEOS/21_HEOSMaster.pm

1100 lines
34 KiB
Perl
Raw Normal View History

2017-01-16 12:26:35 +00:00
###############################################################################
#
2017-01-16 12:26:35 +00:00
# Developed with Kate
#
# (c) 2017 Copyright: Marko Oldenburg (leongaultier at gmail dot com)
# All rights reserved
#
# Special thanks goes to comitters:
# - Olaf Schnicke
#
#
2017-01-16 12:26:35 +00:00
# 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$
#
###############################################################################
#################################
######### Wichtige Hinweise und Links #################
##
#
################################
package main;
use strict;
use warnings;
use JSON qw(decode_json);
2017-02-03 15:08:10 +00:00
use Encode qw(encode_utf8);
use Net::Telnet;
use Data::Dumper;
2017-01-16 12:26:35 +00:00
2017-02-27 13:59:46 +00:00
my $version = "0.1.63";
2017-01-16 12:26:35 +00:00
my %heosCmds = (
'enableChangeEvents' => 'system/register_for_change_events?enable=',
'checkAccount' => 'system/check_account',
'signAccountIn' => 'system/sign_in?',
'signAccountOut' => 'system/sign_out',
'reboot' => 'system/reboot',
'getMusicSources' => 'browse/get_music_sources',
'browseSource' => 'browse/browse?',
'getPlayers' => 'player/get_players',
'getGroups' => 'group/get_groups',
'getPlayerInfo' => 'player/get_player_info?',
'getGroupInfo' => 'group/get_group_info?',
'getPlayState' => 'player/get_play_state?',
'getPlayMode' => 'player/get_play_mode?',
'getMute' => 'player/get_mute?',
'getGroupMute' => 'group/get_mute?',
'getQueue' => 'player/get_queue?',
'playQueue' => 'player/play_queue?',
'clearQueue' => 'player/clear_queue?',
'saveQueue' => 'player/save_queue?',
2017-02-09 10:43:17 +00:00
'getVolume' => 'player/get_volume?',
'getGroupVolume' => 'group/get_volume?',
'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?',
'renamePlaylist' => 'browse/rename_playlist?',
'deletePlaylist' => 'browse/delete_playlist?',
'setVolume' => 'player/set_volume?',
2017-02-09 10:43:17 +00:00
'setGroupVolume' => 'group/set_volume?',
'volumeUp' => 'player/volume_up?',
'volumeDown' => 'player/volume_down?',
2017-02-09 10:43:17 +00:00
'GroupVolumeUp' => 'group/volume_up?',
'GroupVolumeDown' => 'group/volume_down?',
'getNowPlayingMedia' => 'player/get_now_playing_media?',
'eventChangeVolume' => 'event/player_volume_changed',
'createGroup' => 'group/set_group?'
);
2017-01-16 12:26:35 +00:00
# Declare functions
sub HEOSMaster_Initialize($);
sub HEOSMaster_Define($$);
sub HEOSMaster_Undef($$);
sub HEOSMaster_Set($@);
sub HEOSMaster_Open($);
sub HEOSMaster_Close($);
sub HEOSMaster_Read($);
sub HEOSMaster_Write($@);
sub HEOSMaster_Attr(@);
sub HEOSMaster_firstRun($);
sub HEOSMaster_ResponseProcessing($$);
sub HEOSMaster_WriteReadings($$);
2017-01-27 21:22:30 +00:00
sub HEOSMaster_GetPlayers($);
sub HEOSMaster_EnableChangeEvents($);
sub HEOSMaster_PreProcessingReadings($$);
sub HEOSMaster_ReOpen($);
2017-02-09 10:43:17 +00:00
sub HEOSMaster_ReadPassword($);
sub HEOSMaster_StorePassword($$);
sub HEOSMaster_GetGroups($);
sub HEOSMaster_ProcessRead($$);
sub HEOSMaster_ParseMsg($$);
sub HEOSMaster_CheckAccount($);
sub HEOSMaster_Get($$@);
sub HEOSMaster_GetFavorites($);
sub HEOSMaster_GetHistory($);
sub HEOSMaster_GetInputs($);
sub HEOSMaster_GetMusicSources($);
sub HEOSMaster_GetPlaylists($);
sub HEOSMaster_GetServers($);
2017-01-16 12:26:35 +00:00
sub HEOSMaster_Initialize($) {
2017-02-27 08:39:48 +00:00
2017-01-16 12:26:35 +00:00
my ($hash) = @_;
2017-02-27 08:39:48 +00:00
2017-01-16 12:26:35 +00:00
# Provider
2017-02-09 10:43:17 +00:00
$hash->{ReadFn} = "HEOSMaster_Read";
$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.*|.*{"command":."event\/group.*'
2017-02-09 10:43:17 +00:00
};
2017-01-16 12:26:35 +00:00
# Consumer
$hash->{SetFn} = "HEOSMaster_Set";
$hash->{GetFn} = "HEOSMaster_Get";
2017-01-16 12:26:35 +00:00
$hash->{DefFn} = "HEOSMaster_Define";
$hash->{UndefFn} = "HEOSMaster_Undef";
$hash->{AttrFn} = "HEOSMaster_Attr";
$hash->{AttrList} = "disable:1 ".
"heosUsername ".
$readingFnAttributes;
2017-01-16 12:26:35 +00:00
foreach my $d(sort keys %{$modules{HEOSMaster}{defptr}}) {
2017-02-27 08:39:48 +00:00
2017-01-16 12:26:35 +00:00
my $hash = $modules{HEOSMaster}{defptr}{$d};
$hash->{VERSION} = $version;
2017-01-16 12:26:35 +00:00
}
}
sub HEOSMaster_Define($$) {
2017-02-27 08:39:48 +00:00
2017-01-16 12:26:35 +00:00
my ( $hash, $def ) = @_;
my @a = split( "[ \t][ \t]*", $def );
2017-02-27 08:39:48 +00:00
2017-01-16 12:26:35 +00:00
return "too few parameters: define <name> HEOSMaster <HOST>" if( @a != 3 );
my $name = $a[0];
my $host = $a[2];
$hash->{HOST} = $host;
$hash->{VERSION} = $version;
Log3 $name, 3, "HEOSMaster ($name) - defined with host $host";
$attr{$name}{room} = "HEOS" if( !defined( $attr{$name}{room} ) );
readingsBeginUpdate($hash);
readingsBulkUpdate($hash,'state','Initialized');
readingsBulkUpdate($hash,'enableChangeEvents', 'off');
readingsEndUpdate($hash,1);
if( $init_done ) {
2017-02-27 08:39:48 +00:00
HEOSMaster_firstRun($hash);
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
InternalTimer( gettimeofday()+15, 'HEOSMaster_firstRun', $hash, 0 ) if( ($hash->{HOST}) );
}
2017-02-27 08:39:48 +00:00
2017-02-09 10:43:17 +00:00
$modules{HEOSPlayer}{defptr}{$host} = $hash;
2017-01-16 12:26:35 +00:00
return undef;
}
sub HEOSMaster_Undef($$) {
2017-02-27 08:39:48 +00:00
2017-01-16 12:26:35 +00:00
my ( $hash, $arg ) = @_;
my $host = $hash->{HOST};
my $name = $hash->{NAME};
2017-02-27 08:39:48 +00:00
HEOSMaster_Close($hash);
2017-01-16 12:26:35 +00:00
delete $modules{HEOSMaster}{defptr}{$hash->{HOST}};
2017-02-27 08:39:48 +00:00
2017-01-28 16:06:00 +00:00
Log3 $name, 3, "HEOSPlayer ($name) - device $name deleted";
2017-01-16 12:26:35 +00:00
return undef;
}
sub HEOSMaster_Attr(@) {
2017-02-27 08:39:48 +00:00
my ( $cmd, $name, $attrName, $attrVal ) = @_;
my $hash = $defs{$name};
my $orig = $attrVal;
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, "HEOSMaster ($name) - disabled";
2017-02-27 08:39:48 +00:00
} elsif( $cmd eq "del" ) {
readingsSingleUpdate ( $hash, "state", "active", 1 );
Log3 $name, 3, "HEOSMaster ($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, "HEOSMaster ($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, "HEOSMaster ($name) - delete disabledForIntervals";
}
}
2017-02-27 08:39:48 +00:00
return undef;
}
sub HEOSMaster_Get($$@) {
2017-02-27 08:39:48 +00:00
my ($hash, $name, @aa) = @_;
my ($cmd, @args) = @aa;
my $pid = $hash->{PID};
2017-02-27 08:39:48 +00:00
if( $cmd eq 'showAccount' ) {
2017-02-27 08:39:48 +00:00
return AttrVal($name,'heosUsername',0) . ":" .HEOSMaster_ReadPassword($hash);
}
2017-02-27 08:39:48 +00:00
my $list = 'showAccount:noArg';
return "Unknown argument $cmd, choose one of $list";
}
2017-01-16 12:26:35 +00:00
sub HEOSMaster_Set($@) {
2017-02-27 08:39:48 +00:00
2017-01-16 12:26:35 +00:00
my ($hash, $name, $cmd, @args) = @_;
my ($arg, @params) = @args;
my $action;
my $heosCmd;
2017-02-27 08:39:48 +00:00
if($cmd eq 'reopen') {
2017-02-27 08:39:48 +00:00
return "usage: reopen" if( @args != 0 );
HEOSMaster_ReOpen($hash);
2017-01-16 12:26:35 +00:00
return undef;
2017-02-27 08:39:48 +00:00
} elsif($cmd eq 'getPlayers') {
2017-02-27 08:39:48 +00:00
return "usage: getPlayers" if( @args != 0 );
2017-01-27 21:22:30 +00:00
$heosCmd = 'getPlayers';
$action = undef;
2017-02-27 08:39:48 +00:00
2017-02-09 10:43:17 +00:00
} elsif($cmd eq 'getGroups') {
2017-02-27 08:39:48 +00:00
2017-02-09 10:43:17 +00:00
return "usage: getGroups" if( @args != 0 );
$heosCmd = 'getGroups';
$action = undef;
2017-02-27 08:39:48 +00:00
} elsif($cmd eq 'enableChangeEvents') {
2017-02-27 08:39:48 +00:00
return "usage: enableChangeEvents" if( @args != 1 );
$heosCmd = $cmd;
2017-01-27 21:22:30 +00:00
$action = $args[0];
2017-02-27 08:39:48 +00:00
} elsif($cmd eq 'checkAccount') {
2017-02-27 08:39:48 +00:00
return "usage: checkAccount" if( @args != 0 );
$heosCmd = $cmd;
$action = undef;
2017-02-27 08:39:48 +00:00
} elsif($cmd eq 'signAccount') {
2017-02-27 08:39:48 +00:00
return "usage: signAccountIn" if( @args != 1 );
2017-02-09 10:43:17 +00:00
return "please set account informattion first" if(AttrVal($name,'heosUsername','none') eq 'none');
$heosCmd = $cmd . $args[0];
2017-02-09 10:43:17 +00:00
$action = 'un='. AttrVal($name,'heosUsername','none') . '&pw=' . HEOSMaster_ReadPassword($hash) if($args[0] eq 'In');
2017-02-27 08:39:48 +00:00
} elsif($cmd eq 'password') {
2017-02-27 08:39:48 +00:00
return "usage: password" if( @args != 1 );
return HEOSMaster_StorePassword( $hash, $args[0] );
2017-02-27 08:39:48 +00:00
} elsif($cmd eq 'reboot') {
2017-02-27 08:39:48 +00:00
return "usage: reboot" if( @args != 0 );
return HEOSMaster_StorePassword( $hash, $args[0] );
2017-02-27 08:39:48 +00:00
###################################################
### Dieser Menüpunkt ist nur zum testen
} elsif($cmd eq 'eventSend') {
return "usage: eventSend" if( @args != 0 );
HEOSMaster_send($hash);
return undef;
###################################################
2017-02-27 08:39:48 +00:00
2017-01-16 12:26:35 +00:00
} else {
2017-02-27 08:39:48 +00:00
my $list = "";
2017-02-09 10:43:17 +00:00
$list .= "reopen:noArg getPlayers:noArg getGroups:noArg enableChangeEvents:on,off checkAccount:noArg signAccount:In,Out password reboot";
2017-01-16 12:26:35 +00:00
return "Unknown argument $cmd, choose one of $list";
}
2017-02-27 08:39:48 +00:00
HEOSMaster_Write($hash,$heosCmd,$action);
}
sub HEOSMaster_Open($) {
2017-02-27 08:39:48 +00:00
my $hash = shift;
my $name = $hash->{NAME};
my $host = $hash->{HOST};
my $port = 1255;
my $timeout = 0.1;
my $user = AttrVal($name,'heosUsername',undef);
my $password = HEOSMaster_ReadPassword($hash);
2017-02-27 08:39:48 +00:00
2017-01-27 21:22:30 +00:00
Log3 $name, 4, "HEOSMaster ($name) - Baue Socket Verbindung auf";
my $socket = new Net::Telnet ( Host=>$host,
Port => $port,
Timeout=>$timeout,
Errmode=>'return')
2017-01-16 12:26:35 +00:00
or return Log3 $name, 3, "HEOSMaster ($name) Couldn't connect to $host:$port";
$hash->{FD} = $socket->fileno();
$hash->{CD} = $socket; # sysread / close won't work on fileno
$selectlist{$name} = $hash;
readingsSingleUpdate($hash, 'state', 'connected', 1 );
2017-01-27 21:22:30 +00:00
Log3 $name, 4, "HEOSMaster ($name) - Socket Connected";
#hinzugefügt laut Protokoll 2.1.1 Initsequenz
HEOSMaster_Write($hash,'enableChangeEvents','off');
Log3 $name, 4, "HEOSMaster ($name) - set enableChangeEvents off";
#hinzugefügt laut Protokoll 2.1.1 Initsequenz
if( defined($user) and defined($password) ) {
2017-02-27 08:39:48 +00:00
HEOSMaster_Write($hash,'signAccountIn',"un=$user&pw=$password");
Log3 $name, 4, "HEOSMaster ($name) - sign in";
}
2017-02-27 08:39:48 +00:00
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($) {
2017-02-27 08:39:48 +00:00
my $hash = shift;
my $name = $hash->{NAME};
2017-02-27 08:39:48 +00:00
return if( !$hash->{CD} );
2017-02-27 08:39:48 +00:00
close($hash->{CD}) if($hash->{CD});
delete($hash->{FD});
delete($hash->{CD});
delete($selectlist{$name});
2017-02-27 08:39:48 +00:00
readingsSingleUpdate($hash, 'state', 'not connected', 1 );
}
sub HEOSMaster_ReOpen($) {
2017-02-27 08:39:48 +00:00
my $hash = shift;
my $name = $hash->{NAME};
2017-02-27 08:39:48 +00:00
HEOSMaster_Close($hash);
HEOSMaster_Open($hash) if( !$hash->{CD} or !defined($hash->{CD}) );
}
sub HEOSMaster_Write($@) {
2017-02-27 08:39:48 +00:00
my ($hash,$heosCmd,$value) = @_;
my $name = $hash->{NAME};
my $string = "heos://$heosCmds{$heosCmd}";
2017-02-27 08:39:48 +00:00
if( defined($value) ) {
2017-02-27 08:39:48 +00:00
$string .= "${value}" if( $value ne '&' );
}
2017-02-27 08:39:48 +00:00
$string .= "\r\n";
2017-01-27 21:22:30 +00:00
Log3 $name, 4, "HEOSMaster ($name) - WriteFn called";
2017-02-27 08:39:48 +00:00
2017-01-27 21:22:30 +00:00
return Log3 $name, 4, "HEOSMaster ($name) - socket not connected"
unless($hash->{CD});
2017-02-27 08:39:48 +00:00
2017-01-27 21:22:30 +00:00
Log3 $name, 5, "HEOSMaster ($name) - $string";
syswrite($hash->{CD}, $string);
2017-02-27 08:39:48 +00:00
return undef;
}
sub HEOSMaster_Read($) {
2017-02-27 08:39:48 +00:00
my $hash = shift;
my $name = $hash->{NAME};
my $len;
my $buf;
2017-02-27 08:39:48 +00:00
Log3 $name, 4, "HEOSMaster ($name) - ReadFn gestartet";
2017-01-27 21:22:30 +00:00
$len = sysread($hash->{CD},$buf,1024); # die genaue Puffergröße wird noch ermittelt
2017-02-27 08:39:48 +00:00
if( !defined($len) || !$len ) {
2017-02-27 08:39:48 +00:00
2017-02-09 10:43:17 +00:00
Log3 $name, 5, "HEOSMaster ($name) - connection closed by remote Host";
HEOSMaster_Close($hash);
return;
}
2017-02-27 08:39:48 +00:00
unless( defined $buf) {
2017-02-27 08:39:48 +00:00
2017-01-16 12:26:35 +00:00
Log3 $name, 3, "HEOSMaster ($name) - Keine Daten empfangen";
return;
}
2017-02-27 08:39:48 +00:00
Log3 $name, 5, "HEOSMaster ($name) - received buffer data, start HEOSMaster_ProcessRead: $buf";
HEOSMaster_ProcessRead($hash,$buf);
}
sub HEOSMaster_ProcessRead($$) {
2017-02-27 08:39:48 +00:00
my ($hash, $data) = @_;
my $name = $hash->{NAME};
my $buffer = '';
2017-02-27 08:39:48 +00:00
Log3 $name, 4, "HEOSMaster ($name) - process read";
#include previous partial message
2017-02-27 08:39:48 +00:00
if(defined($hash->{PARTIAL}) && $hash->{PARTIAL}) {
2017-02-27 08:39:48 +00:00
Log3 $name, 5, "HEOSMaster ($name) - PARTIAL: " . $hash->{PARTIAL};
$buffer = $hash->{PARTIAL};
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
Log3 $name, 4, "HEOSMaster ($name) - No PARTIAL buffer";
}
2017-02-27 08:39:48 +00:00
Log3 $name, 5, "HEOSMaster ($name) - Incoming data: " . $data;
2017-02-27 08:39:48 +00:00
$buffer = $buffer . $data;
2017-02-27 08:39:48 +00:00
Log3 $name, 5, "HEOSMaster ($name) - Current processing buffer (PARTIAL + incoming data): " . $buffer;
2017-02-27 08:39:48 +00:00
my ($json,$tail) = HEOSMaster_ParseMsg($hash, $buffer);
#processes all complete messages
2017-02-27 08:39:48 +00:00
while($json) {
2017-02-27 08:39:48 +00:00
$hash->{LAST_RECV} = time();
Log3 $name, 5, "HEOSMaster ($name) - Decoding JSON message. Length: " . length($json) . " Content: " . $json;
my $obj = JSON->new->utf8(0)->decode($json);
2017-02-27 08:39:48 +00:00
if(defined($obj->{heos})) {
2017-02-27 08:39:48 +00:00
HEOSMaster_ResponseProcessing($hash,$json);
Log3 $name, 4, "HEOSMaster ($name) - starte HEOSMaster_ResponseProcessing";
2017-02-27 08:39:48 +00:00
} elsif(defined($obj->{error})) {
2017-02-27 08:39:48 +00:00
Log3 $name, 3, "HEOSMaster ($name) - Received error message: " . $json;
}
2017-02-27 08:39:48 +00:00
($json,$tail) = HEOSMaster_ParseMsg($hash, $tail);
}
2017-02-27 08:39:48 +00:00
$hash->{PARTIAL} = $tail;
Log3 $name, 5, "HEOSMaster ($name) - Tail: " . $tail;
Log3 $name, 5, "HEOSMaster ($name) - PARTIAL: " . $hash->{PARTIAL};
return;
}
sub HEOSMaster_ResponseProcessing($$) {
2017-02-27 08:39:48 +00:00
my ($hash,$json) = @_;
my $name = $hash->{NAME};
my $decode_json;
2017-02-27 08:39:48 +00:00
2017-02-03 15:08:10 +00:00
Log3 $name, 5, "HEOSMaster ($name) - JSON String: $json";
return Log3 $name, 3, "HEOSMaster ($name) - empty answer received"
unless( defined($json));
2017-02-03 15:08:10 +00:00
Log3 $name, 4, "HEOSMaster ($name) - JSON detected!";
$decode_json = decode_json(encode_utf8($json));
return Log3 $name, 3, "HEOSMaster ($name) - decode_json has no Hash"
unless(ref($decode_json) eq "HASH");
return Log3 $name, 4, "HEOSMaster ($name) - heos worked"
if( $decode_json->{heos}{message} =~ /command\sunder\sprocess/ );
if( defined($decode_json->{heos}{result}) or $decode_json->{heos}{command} =~ /^system/ ) {
2017-02-27 08:39:48 +00:00
HEOSMaster_WriteReadings($hash,$decode_json);
2017-01-27 21:22:30 +00:00
Log3 $name, 4, "HEOSMaster ($name) - call Sub HEOSMaster_WriteReadings";
}
my %message = map { my ( $key, $value ) = split "="; $key => $value } split('&', $decode_json->{heos}{message});
return Log3 $name, 4, "HEOSMaster ($name) - general error ID $message{eid} - $message{text}"
if( defined($decode_json->{heos}{result}) && $decode_json->{heos}{result} =~ /fail/ );
#Quellen neu einlesen
if( $decode_json->{heos}{command} =~ /^event\/sources_changed/ ) {
2017-02-27 08:39:48 +00:00
HEOSMaster_Write($hash,'getMusicSources',undef);
return Log3 $name, 4, "HEOSMaster ($name) - source changed";
}
#Player neu einlesen
if( $decode_json->{heos}{command} =~ /^event\/players_changed/ ) {
2017-02-27 08:39:48 +00:00
HEOSMaster_Write($hash,'getPlayers',undef);
return Log3 $name, 4, "HEOSMaster ($name) - player changed";
}
#User neu einlesen
if( $decode_json->{heos}{command} =~ /^event\/user_changed/ ) {
2017-02-27 08:39:48 +00:00
HEOSMaster_Write($hash,'checkAccount',undef);
return Log3 $name, 4, "HEOSMaster ($name) - user changed";
}
#Gruppen neu einlesen
if( $decode_json->{heos}{command} =~ /^event\/groups_changed/ ) {
2017-02-27 08:39:48 +00:00
#InternalTimer( gettimeofday()+5, 'HEOSMaster_GetGroups', $hash, 0 );
HEOSMaster_Write($hash,'getGroups',undef);
return Log3 $name, 4, "HEOSMaster ($name) - groups changed";
}
if( $decode_json->{heos}{command} =~ /^browse\/get_music_sources/ and ref($decode_json->{payload}) eq "ARRAY" and scalar(@{$decode_json->{payload}}) > 0) {
#liest nur die Onlinequellen der Rest wird extra eingelesen
$hash->{helper}{sources} = [];
my $i = 4;
foreach my $payload ( @{$decode_json->{payload}} ) {
if( $payload->{sid} eq "1024" ) {
2017-02-27 08:39:48 +00:00
$i += 2;
InternalTimer( gettimeofday()+$i, 'HEOSMaster_GetServers', $hash, 0 );
Log3 $name, 4, "HEOSMaster ($name) - GetServers in $i seconds";
2017-02-27 08:39:48 +00:00
} elsif( $payload->{sid} eq "1025" ) {
2017-02-27 08:39:48 +00:00
$i += 2;
InternalTimer( gettimeofday()+$i, 'HEOSMaster_GetPlaylists', $hash, 0 );
Log3 $name, 4, "HEOSMaster ($name) - GetPlaylists in $i seconds";
2017-02-27 08:39:48 +00:00
} elsif( $payload->{sid} eq "1026" ) {
2017-02-27 08:39:48 +00:00
$i += 2;
InternalTimer( gettimeofday()+$i, 'HEOSMaster_GetHistory', $hash, 0 );
Log3 $name, 4, "HEOSMaster ($name) - GetHistory in $i seconds";
2017-02-27 08:39:48 +00:00
} elsif( $payload->{sid} eq "1027" ) {
2017-02-27 08:39:48 +00:00
$i += 2;
InternalTimer( gettimeofday()+$i, 'HEOSMaster_GetInputs', $hash, 0 );
Log3 $name, 4, "HEOSMaster ($name) - GetInputs in $i seconds";
2017-02-27 08:39:48 +00:00
} elsif( $payload->{sid} eq "1028" ) {
2017-02-27 08:39:48 +00:00
$i += 2;
InternalTimer( gettimeofday()+$i, 'HEOSMaster_GetFavorites', $hash, 0 );
Log3 $name, 4, "HEOSMaster ($name) - GetFavorites in $i seconds";
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
#Onlinedienste
push( @{$hash->{helper}{sources}},$payload);
Log3 $name, 4, "HEOSMaster ($name) - GetRadioSource {$payload->{name} with sid $payload->{sid}";
}
}
2017-02-27 08:39:48 +00:00
return Log3 $name, 3, "HEOSMaster ($name) - call Sourcebrowser";
}
if( $decode_json->{heos}{command} =~ /^browse\/browse/ and ref($decode_json->{payload}) eq "ARRAY" and scalar(@{$decode_json->{payload}}) > 0) {
if ( defined $message{sid} ) {
2017-02-27 08:39:48 +00:00
if ( defined $message{range} ) {
$message{range} =~ s/(\d+)\,\d+/$1/;
} else {
$message{range} = 0;
}
my $start = $message{range} + $message{returned};
if( $message{sid} eq '1028' ) {
2017-02-27 08:39:48 +00:00
#Favoriten einlesen
$hash->{helper}{favorites} = [] if ( $message{range} == 0 );
2017-02-27 08:39:48 +00:00
push( @{$hash->{helper}{favorites}}, (@{$decode_json->{payload}}) );
if ( $start >= $message{count} ) {
2017-02-27 08:39:48 +00:00
#Nachricht an die Player das sich die Favoriten geändert haben
foreach my $dev ( devspec2array("TYPE=HEOSPlayer") ) {
2017-02-27 08:39:48 +00:00
$json = '{"heos": {"command": "event/favorites_changed", "message": "pid='.$defs{$dev}->{PID}.'"}}';
Dispatch($hash,$json,undef);
Log3 $name, 4, "HEOSMaster ($name) - call Dispatcher for Favorites Changed";
}
}
2017-02-27 08:39:48 +00:00
} elsif( $message{sid} eq '1026' ) {
2017-02-27 08:39:48 +00:00
#History einlesen
$hash->{helper}{history} = [] if ( $message{range} == 0 );
push( @{$hash->{helper}{history}}, (@{$decode_json->{payload}}) );
2017-02-27 08:39:48 +00:00
} elsif( $message{sid} eq '1025' ) {
2017-02-27 08:39:48 +00:00
#Playlisten einlesen
$hash->{helper}{playlists} = [] if ( $message{range} == 0 );
2017-02-27 08:39:48 +00:00
push( @{$hash->{helper}{playlists}}, (@{$decode_json->{payload}}) );
} elsif( $message{sid} eq '1027' ) {
2017-02-27 08:39:48 +00:00
#Inputs einlesen
push( @{$hash->{helper}{sources}}, map { $_->{name} .= " AUX"; $_ } (@{$decode_json->{payload}}) );
2017-02-27 08:39:48 +00:00
} elsif( $message{sid} eq '1024' ) {
2017-02-27 08:39:48 +00:00
#Lokal einlesen
push( @{$hash->{helper}{sources}}, map { $_->{name} .= " USB" if ( $_->{sid} < 0 ); $_ } (@{$decode_json->{payload}}) );
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
#aktuellen Input/Media einlesen
$hash->{helper}{media} = [] if ( $message{range} == 0 );
push( @{$hash->{helper}{media}}, (@{$decode_json->{payload}}) );
2017-02-09 10:43:17 +00:00
}
2017-02-27 08:39:48 +00:00
Log3 $name, 4, "HEOSMaster ($name) - call Browser with sid $message{sid} and $message{returned} items from $message{count} items";
2017-02-27 08:39:48 +00:00
if ( $start < $message{count} ) {
2017-02-27 08:39:48 +00:00
HEOSMaster_Write($hash,'browseSource',"sid=$message{sid}&range=$start,".($start + 100) );
Log3 $name, 3, "HEOSMaster ($name) - call Browser with sid $message{sid} next Range from $message{returned}";
}
2017-02-27 08:39:48 +00:00
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/ ) {
if( $decode_json->{heos}{command} =~ /player\/get_players/ ) {
2017-02-27 08:39:48 +00:00
return Log3 $name, 4, "HEOSMaster ($name) - empty ARRAY received"
unless(scalar(@{$decode_json->{payload}}) > 0);
foreach my $payload (@{$decode_json->{payload}}) {
2017-02-27 08:39:48 +00:00
$json = '{"pid": "';
$json .= "$payload->{pid}";
$json .= '","heos": {"command": "player/get_player_info"}}';
Dispatch($hash,$json,undef);
Log3 $name, 4, "HEOSMaster ($name) - call Dispatcher for Players";
}
2017-02-27 08:39:48 +00:00
} elsif( $decode_json->{heos}{command} =~ /group\/get_groups/ ) {
2017-02-27 08:39:48 +00:00
my $filter = "TYPE=HEOSGroup";
if ( scalar(@{$decode_json->{payload}}) > 0 ) {
2017-02-27 08:39:48 +00:00
$filter .= ":FILTER=GID!=";
2017-02-27 08:39:48 +00:00
foreach my $payload (@{$decode_json->{payload}}) {
2017-02-27 08:39:48 +00:00
$json = '{"gid": "';
$json .= "$payload->{gid}";
$json .= '","heos": {"command": "group/get_group_info"}}';
Dispatch($hash,$json,undef);
Log3 $name, 4, "HEOSMaster ($name) - call Dispatcher for Groups";
$filter .= $payload->{gid}."|";
}
2017-02-27 08:39:48 +00:00
chop($filter); #letztes | wieder abschneiden
}
2017-02-27 08:39:48 +00:00
#alle Gruppe ausschalten die nicht mehr im HEOS System existieren
foreach my $dev ( devspec2array($filter) ) {
2017-02-27 08:39:48 +00:00
my $ghash = $defs{$dev};
readingsSingleUpdate( $ghash, "state", "off", 1 );
}
2017-02-27 08:39:48 +00:00
} elsif( $decode_json->{heos}{command} =~ /player\/get_queue/ ) {
return Log3 $name, 4, "HEOSMaster ($name) - empty ARRAY received"
unless(scalar(@{$decode_json->{payload}}) > 0);
Dispatch($hash,$json,undef);
Log3 $name, 4, "HEOSMaster ($name) - call Dispatcher for QueueInfo";
2017-02-27 08:39:48 +00:00
} elsif( $decode_json->{heos}{command} =~ /player\/get_player_info/ ) { # ist vielleicht verständlicher?
2017-02-27 08:39:48 +00:00
Dispatch($hash,$json,undef);
Log3 $name, 4, "HEOSMaster ($name) - call Dispatcher for PlayerInfo";
2017-02-27 08:39:48 +00:00
} elsif( $decode_json->{heos}{command} =~ /group\/get_group_info/ ) { # ist vielleicht verständlicher?
2017-02-27 08:39:48 +00:00
Dispatch($hash,$json,undef);
Log3 $name, 4, "HEOSMaster ($name) - call Dispatcher for GroupInfo";
2017-02-27 08:39:48 +00:00
} elsif( defined($message{pid}) or defined($message{gid}) ) {
2017-02-27 08:39:48 +00:00
Dispatch($hash,$json,undef);
2017-01-27 21:22:30 +00:00
Log3 $name, 4, "HEOSMaster ($name) - call Dispatcher";
}
2017-02-27 08:39:48 +00:00
return;
}
2017-02-27 08:39:48 +00:00
2017-01-27 21:22:30 +00:00
Log3 $name, 4, "HEOSMaster ($name) - no Match for processing data";
}
sub HEOSMaster_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
############################
#### Aufbereiten der Daten soweit nötig
my $readingsHash = HEOSMaster_PreProcessingReadings($hash,$decode_json)
if( $decode_json->{heos}{command} eq 'system/register_for_change_events'
or $decode_json->{heos}{command} eq 'system/check_account'
or $decode_json->{heos}{command} eq 'system/sign_in'
or $decode_json->{heos}{command} eq 'system/sign_out' );
############################
#### schreiben der Readings
readingsBeginUpdate($hash);
### Event Readings
if( ref($readingsHash) eq "HASH" ) {
2017-02-27 08:39:48 +00:00
Log3 $name, 4, "HEOSMaster ($name) - response json Hash back from HEOSMaster_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}) ) {
2017-02-27 08:39:48 +00:00
readingsBulkUpdate( $hash, $t, $v ) if( defined($v) );
}
}
2017-02-27 08:39:48 +00:00
readingsBulkUpdate( $hash, "lastCommand", $decode_json->{heos}{command} );
readingsBulkUpdate( $hash, "lastResult", $decode_json->{heos}{result} );
if( ref($decode_json->{payload}) ne "ARRAY" ) {
2017-02-27 08:39:48 +00:00
readingsBulkUpdate( $hash, "lastPlayerId", $decode_json->{payload}{pid} );
readingsBulkUpdate( $hash, "lastPlayerName", $decode_json->{payload}{name} );
}
2017-02-27 08:39:48 +00:00
readingsEndUpdate( $hash, 1 );
return undef;
2017-01-16 12:26:35 +00:00
}
2017-01-27 21:22:30 +00:00
###################
### my little Helpers
sub HEOSMaster_ParseMsg($$) {
2017-02-27 08:39:48 +00:00
my ($hash, $buffer) = @_;
my $name = $hash->{NAME};
my $open = 0;
my $close = 0;
my $msg = '';
my $tail = '';
2017-02-27 08:39:48 +00:00
if($buffer) {
foreach my $c (split //, $buffer) {
if($open == $close && $open > 0) {
$tail .= $c;
#Log3 $name, 5, "HEOSMaster ($name) - $open == $close && $open > 0";
2017-02-27 08:39:48 +00:00
} elsif(($open == $close) && ($c ne '{')) {
2017-02-27 08:39:48 +00:00
Log3 $name, 5, "HEOSMaster ($name) - Garbage character before message: " . $c;
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
if($c eq '{') {
2017-02-27 08:39:48 +00:00
$open++;
2017-02-27 08:39:48 +00:00
} elsif($c eq '}') {
2017-02-27 08:39:48 +00:00
$close++;
}
2017-02-27 08:39:48 +00:00
$msg .= $c;
}
}
2017-02-27 08:39:48 +00:00
if($open != $close) {
2017-02-27 08:39:48 +00:00
$tail = $msg;
$msg = '';
}
}
2017-02-27 08:39:48 +00:00
Log3 $name, 5, "HEOSMaster ($name) - return msg: $msg and tail: $tail";
return ($msg,$tail);
}
sub HEOSMaster_PreProcessingReadings($$) {
2017-02-27 08:39:48 +00:00
my ($hash,$decode_json) = @_;
my $name = $hash->{NAME};
my $reading;
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, "HEOSMaster ($name) - preprocessing readings";
2017-02-27 08:39:48 +00:00
if ( $decode_json->{heos}{command} eq 'system/register_for_change_events' ) {
2017-02-27 08:39:48 +00:00
$buffer{'enableChangeEvents'} = $message{enable};
2017-02-27 08:39:48 +00:00
} elsif ( $decode_json->{heos}{command} eq 'system/check_account' or $decode_json->{heos}{command} eq 'system/sign_in' ) {
if ( exists $message{signed_out} ) {
2017-02-27 08:39:48 +00:00
$buffer{'heosAccount'} = "signed_out";
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
$buffer{'heosAccount'} = "signed_in as $message{un}";
HEOSMaster_GetFavorites($hash) if( ReadingsVal($name,"enableChangeEvents", "off") eq "on" );
}
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
Log3 $name, 3, "HEOSMaster ($name) - no match found";
return undef;
}
2017-02-27 08:39:48 +00:00
Log3 $name, 4, "HEOSMaster ($name) - Match found for decode_json";
return \%buffer;
}
2017-01-27 21:22:30 +00:00
sub HEOSMaster_firstRun($) {
2017-02-27 08:39:48 +00:00
2017-01-27 21:22:30 +00:00
my $hash = shift;
my $name = $hash->{NAME};
2017-02-27 08:39:48 +00:00
2017-02-09 10:43:17 +00:00
RemoveInternalTimer($hash,'HEOSMaster_firstRun');
2017-01-27 21:22:30 +00:00
HEOSMaster_Open($hash) if( !IsDisabled($name) );
}
sub HEOSMaster_GetPlayers($) {
2017-02-27 08:39:48 +00:00
2017-01-27 21:22:30 +00:00
my $hash = shift;
my $name = $hash->{NAME};
2017-02-27 08:39:48 +00:00
2017-02-09 10:43:17 +00:00
RemoveInternalTimer($hash,'HEOSMaster_GetPlayers');
2017-01-27 21:22:30 +00:00
HEOSMaster_Write($hash,'getPlayers',undef);
Log3 $name, 4, "HEOSMaster ($name) - getPlayers";
}
2017-02-09 10:43:17 +00:00
sub HEOSMaster_GetGroups($) {
2017-02-27 08:39:48 +00:00
2017-02-09 10:43:17 +00:00
my $hash = shift;
my $name = $hash->{NAME};
2017-02-27 08:39:48 +00:00
2017-02-09 10:43:17 +00:00
RemoveInternalTimer($hash,'HEOSMaster_GetGroups');
HEOSMaster_Write($hash,'getGroups',undef);
Log3 $name, 4, "HEOSMaster ($name) - getGroups";
2017-02-09 10:43:17 +00:00
}
2017-01-27 21:22:30 +00:00
sub HEOSMaster_EnableChangeEvents($) {
2017-02-27 08:39:48 +00:00
2017-01-27 21:22:30 +00:00
my $hash = shift;
my $name = $hash->{NAME};
2017-02-27 08:39:48 +00:00
2017-02-09 10:43:17 +00:00
RemoveInternalTimer($hash,'HEOSMaster_EnableChangeEvents');
2017-01-27 21:22:30 +00:00
HEOSMaster_Write($hash,'enableChangeEvents','on');
Log3 $name, 4, "HEOSMaster ($name) - set enableChangeEvents on";
2017-01-27 21:22:30 +00:00
}
sub HEOSMaster_GetMusicSources($) {
2017-02-27 08:39:48 +00:00
my $hash = shift;
my $name = $hash->{NAME};
2017-02-27 08:39:48 +00:00
RemoveInternalTimer($hash, 'HEOSMaster_GetMusicSources');
HEOSMaster_Write($hash,'getMusicSources',undef);
Log3 $name, 4, "HEOSMaster ($name) - getMusicSources";
}
sub HEOSMaster_GetFavorites($) {
2017-02-27 08:39:48 +00:00
my $hash = shift;
my $name = $hash->{NAME};
2017-02-27 08:39:48 +00:00
RemoveInternalTimer($hash, 'HEOSMaster_GetFavorites');
HEOSMaster_Write($hash,'browseSource','sid=1028');
Log3 $name, 4, "HEOSMaster ($name) - getFavorites";
}
sub HEOSMaster_GetInputs($) {
2017-02-27 08:39:48 +00:00
my $hash = shift;
my $name = $hash->{NAME};
2017-02-27 08:39:48 +00:00
RemoveInternalTimer($hash, 'HEOSMaster_GetInputs');
HEOSMaster_Write($hash,'browseSource','sid=1027');
Log3 $name, 4, "HEOSMaster ($name) - getInputs";
}
sub HEOSMaster_GetServers($) {
2017-02-27 08:39:48 +00:00
my $hash = shift;
my $name = $hash->{NAME};
2017-02-27 08:39:48 +00:00
RemoveInternalTimer($hash, 'HEOSMaster_GetServers');
HEOSMaster_Write($hash,'browseSource','sid=1024');
Log3 $name, 4, "HEOSMaster ($name) - getServers";
}
sub HEOSMaster_GetPlaylists($) {
2017-02-27 08:39:48 +00:00
my $hash = shift;
my $name = $hash->{NAME};
2017-02-27 08:39:48 +00:00
RemoveInternalTimer($hash, 'HEOSMaster_GetPlaylists');
HEOSMaster_Write($hash,'browseSource','sid=1025');
Log3 $name, 4, "HEOSMaster ($name) - getPlaylists";
}
sub HEOSMaster_GetHistory($) {
2017-02-27 08:39:48 +00:00
my $hash = shift;
my $name = $hash->{NAME};
2017-02-27 08:39:48 +00:00
RemoveInternalTimer($hash, 'HEOSMaster_GetHistory');
HEOSMaster_Write($hash,'browseSource','sid=1026');
Log3 $name, 4, "HEOSMaster ($name) - getHistory";
}
sub HEOSMaster_CheckAccount($) {
2017-02-27 08:39:48 +00:00
my $hash = shift;
my $name = $hash->{NAME};
2017-02-27 08:39:48 +00:00
RemoveInternalTimer($hash, 'HEOSMaster_CheckAccount');
HEOSMaster_Write($hash,'checkAccount',undef);
Log3 $name, 4, "HEOSMaster ($name) - checkAccount";
}
sub HEOSMaster_StorePassword($$) {
2017-02-27 08:39:48 +00:00
my ($hash, $password) = @_;
my $index = $hash->{TYPE}."_".$hash->{NAME}."_passwd";
my $key = getUniqueId().$index;
my $enc_pwd = "";
2017-02-27 08:39:48 +00:00
if(eval "use Digest::MD5;1") {
2017-02-27 08:39:48 +00:00
$key = Digest::MD5::md5_hex(unpack "H*", $key);
$key .= Digest::MD5::md5_hex($key);
}
2017-02-27 08:39:48 +00:00
for my $char (split //, $password) {
2017-02-27 08:39:48 +00:00
my $encode=chop($key);
$enc_pwd.=sprintf("%.2x",ord($char)^ord($encode));
$key=$encode.$key;
}
2017-02-27 08:39:48 +00:00
my $err = setKeyValue($index, $enc_pwd);
return "error while saving the password - $err" if(defined($err));
return "password successfully saved";
}
sub HEOSMaster_ReadPassword($) {
2017-02-27 08:39:48 +00:00
my ($hash) = @_;
my $name = $hash->{NAME};
my $index = $hash->{TYPE}."_".$hash->{NAME}."_passwd";
my $key = getUniqueId().$index;
my ($password, $err);
2017-02-27 08:39:48 +00:00
Log3 $name, 4, "HEOSMaster ($name) - Read password from file";
($err, $password) = getKeyValue($index);
if ( defined($err) ) {
2017-02-27 08:39:48 +00:00
Log3 $name, 4, "HEOSMaster ($name) - unable to read password from file: $err";
return undef;
2017-02-27 08:39:48 +00:00
}
2017-02-27 08:39:48 +00:00
if ( defined($password) ) {
if ( eval "use Digest::MD5;1" ) {
2017-02-27 08:39:48 +00:00
$key = Digest::MD5::md5_hex(unpack "H*", $key);
$key .= Digest::MD5::md5_hex($key);
}
2017-02-27 08:39:48 +00:00
my $dec_pwd = '';
2017-02-27 08:39:48 +00:00
for my $char (map { pack('C', hex($_)) } ($password =~ /(..)/g)) {
2017-02-27 08:39:48 +00:00
my $decode=chop($key);
$dec_pwd.=chr(ord($char)^ord($decode));
$key=$decode.$key;
}
2017-02-27 08:39:48 +00:00
return $dec_pwd;
2017-02-27 08:39:48 +00:00
} else {
2017-02-27 08:39:48 +00:00
Log3 $name, 4, "HEOSMaster ($name) - No password in file";
return undef;
}
}
2017-01-28 16:06:00 +00:00
################
### Nur für mich um dem Emulator ein Event ab zu jagen
sub HEOSMaster_send($) {
2017-02-27 08:39:48 +00:00
2017-01-28 16:06:00 +00:00
my $hash = shift;
2017-02-27 08:39:48 +00:00
2017-01-28 16:06:00 +00:00
HEOSMaster_Write($hash,'eventChangeVolume',undef);
}
2017-01-27 21:22:30 +00:00
2017-01-16 12:26:35 +00:00
2017-01-16 12:26:35 +00:00
1;