2017-01-16 12:26:35 +00:00
|
|
|
###############################################################################
|
|
|
|
#
|
|
|
|
# Developed with Kate
|
|
|
|
#
|
|
|
|
# (c) 2017 Copyright: Marko Oldenburg (leongaultier at gmail dot com)
|
|
|
|
# All rights reserved
|
|
|
|
#
|
|
|
|
# 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;
|
2017-01-24 13:29:30 +00:00
|
|
|
use Net::Telnet;
|
2017-01-16 12:26:35 +00:00
|
|
|
|
2017-01-18 13:53:54 +00:00
|
|
|
|
2017-01-24 13:29:30 +00:00
|
|
|
my $version = "0.1.17";
|
2017-01-16 12:26:35 +00:00
|
|
|
|
|
|
|
|
2017-01-24 13:29:30 +00:00
|
|
|
my %heosCmds = (
|
|
|
|
'enableEvents' => 'system/register_for_change_events?enable=',
|
|
|
|
'getPlayers' => 'player/get_players',
|
|
|
|
'getPlayerState' => 'player/get_player_info?',
|
|
|
|
'setPlayState' => 'player/set_play_state?',
|
|
|
|
'setMute' => 'player/set_mute?',
|
|
|
|
'setVolume' => 'player/set_volume?'
|
|
|
|
);
|
|
|
|
|
2017-01-16 12:26:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Declare functions
|
|
|
|
sub HEOSMaster_Initialize($);
|
|
|
|
sub HEOSMaster_Define($$);
|
|
|
|
sub HEOSMaster_Undef($$);
|
|
|
|
sub HEOSMaster_Set($@);
|
2017-01-24 13:29:30 +00:00
|
|
|
sub HEOSMaster_Open($);
|
|
|
|
sub HEOSMaster_Close($);
|
2017-01-18 13:53:54 +00:00
|
|
|
sub HEOSMaster_Read($);
|
2017-01-24 13:29:30 +00:00
|
|
|
sub HEOSMaster_Write($@);
|
|
|
|
sub HEOSMaster_Attr(@);
|
|
|
|
sub HEOSMaster_firstRun($);
|
|
|
|
sub HEOSMaster_ResponseProcessing($$);
|
|
|
|
sub HEOSMaster_WriteReadings($$);
|
2017-01-16 12:26:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub HEOSMaster_Initialize($) {
|
|
|
|
|
|
|
|
my ($hash) = @_;
|
|
|
|
|
|
|
|
# Provider
|
|
|
|
$hash->{ReadFn} = "HEOSMaster_Read";
|
2017-01-24 13:29:30 +00:00
|
|
|
$hash->{WriteFn} = "HEOSMaster_Write";
|
|
|
|
$hash->{Clients} = ":HEOSPlayer:";
|
|
|
|
$hash->{MatchList} = { "1:HEOSPlayer" => '.*{"command":."player.*' };
|
2017-01-16 12:26:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Consumer
|
|
|
|
$hash->{SetFn} = "HEOSMaster_Set";
|
|
|
|
#$hash->{GetFn} = "HEOSMaster_Get";
|
|
|
|
$hash->{DefFn} = "HEOSMaster_Define";
|
|
|
|
$hash->{UndefFn} = "HEOSMaster_Undef";
|
2017-01-24 13:29:30 +00:00
|
|
|
$hash->{AttrFn} = "HEOSMaster_Attr";
|
|
|
|
$hash->{AttrList} = "disable:1 ".
|
|
|
|
$readingFnAttributes;
|
2017-01-16 12:26:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
foreach my $d(sort keys %{$modules{HEOSMaster}{defptr}}) {
|
|
|
|
my $hash = $modules{HEOSMaster}{defptr}{$d};
|
|
|
|
$hash->{VERSION} = $version;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub HEOSMaster_Define($$) {
|
|
|
|
|
|
|
|
my ( $hash, $def ) = @_;
|
|
|
|
|
|
|
|
my @a = split( "[ \t][ \t]*", $def );
|
|
|
|
|
|
|
|
|
|
|
|
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} ) );
|
2017-01-24 13:29:30 +00:00
|
|
|
|
|
|
|
readingsBeginUpdate($hash);
|
|
|
|
readingsBulkUpdate($hash,'state','Initialized');
|
|
|
|
readingsBulkUpdate($hash,'enableEvents', 'off');
|
|
|
|
readingsEndUpdate($hash,1);
|
|
|
|
|
|
|
|
|
|
|
|
if( $init_done ) {
|
|
|
|
|
|
|
|
InternalTimer( gettimeofday()+1, 'HEOSMaster_firstRun', $hash, 0 ) if( ($hash->{HOST}) );
|
|
|
|
} else {
|
|
|
|
|
|
|
|
InternalTimer( gettimeofday()+15, 'HEOSMaster_firstRun', $hash, 0 ) if( ($hash->{HOST}) );
|
|
|
|
}
|
2017-01-16 12:26:35 +00:00
|
|
|
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub HEOSMaster_Undef($$) {
|
|
|
|
|
|
|
|
my ( $hash, $arg ) = @_;
|
|
|
|
|
|
|
|
my $host = $hash->{HOST};
|
|
|
|
my $name = $hash->{NAME};
|
|
|
|
|
2017-01-24 13:29:30 +00:00
|
|
|
HEOSMaster_Close($hash);
|
2017-01-16 12:26:35 +00:00
|
|
|
delete $modules{HEOSMaster}{defptr}{$hash->{HOST}};
|
|
|
|
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
2017-01-24 13:29:30 +00:00
|
|
|
sub HEOSMaster_Attr(@) {
|
|
|
|
|
|
|
|
my ( $cmd, $name, $attrName, $attrVal ) = @_;
|
|
|
|
my $hash = $defs{$name};
|
|
|
|
|
|
|
|
my $orig = $attrVal;
|
|
|
|
|
|
|
|
|
|
|
|
if( $attrName eq "disable" ) {
|
|
|
|
if( $cmd eq "set" and $attrVal eq "1" ) {
|
|
|
|
readingsSingleUpdate ( $hash, "state", "disabled", 1 );
|
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - disabled";
|
|
|
|
}
|
|
|
|
|
|
|
|
elsif( $cmd eq "del" ) {
|
|
|
|
readingsSingleUpdate ( $hash, "state", "active", 1 );
|
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - enabled";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( $attrName eq "disabledForIntervals" ) {
|
|
|
|
if( $cmd eq "set" ) {
|
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - enable disabledForIntervals";
|
|
|
|
readingsSingleUpdate ( $hash, "state", "Unknown", 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
elsif( $cmd eq "del" ) {
|
|
|
|
readingsSingleUpdate ( $hash, "state", "active", 1 );
|
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - delete disabledForIntervals";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
2017-01-16 12:26:35 +00:00
|
|
|
sub HEOSMaster_Set($@) {
|
|
|
|
|
|
|
|
my ($hash, $name, $cmd, @args) = @_;
|
|
|
|
my ($arg, @params) = @args;
|
|
|
|
|
2017-01-24 13:29:30 +00:00
|
|
|
my $action;
|
2017-01-16 12:26:35 +00:00
|
|
|
|
2017-01-24 13:29:30 +00:00
|
|
|
if($cmd eq 'reopen') {
|
|
|
|
return "usage: reopen" if( @args != 0 );
|
2017-01-16 12:26:35 +00:00
|
|
|
|
2017-01-24 13:29:30 +00:00
|
|
|
HEOSMaster_Close($hash);
|
|
|
|
HEOSMaster_Open($hash) if( !$hash->{CD} or !defined($hash->{CD}) );
|
2017-01-16 12:26:35 +00:00
|
|
|
|
|
|
|
return undef;
|
2017-01-18 13:53:54 +00:00
|
|
|
|
2017-01-24 13:29:30 +00:00
|
|
|
} elsif($cmd eq 'getPlayers') {
|
|
|
|
return "usage: getPlayers" if( @args != 0 );
|
2017-01-18 13:53:54 +00:00
|
|
|
|
2017-01-24 13:29:30 +00:00
|
|
|
$action = $cmd;
|
2017-01-18 13:53:54 +00:00
|
|
|
|
|
|
|
} elsif($cmd eq 'send') {
|
|
|
|
return "usage: send" if( @args != 0 );
|
|
|
|
|
|
|
|
HEOSMaster_send($hash);
|
|
|
|
|
|
|
|
return undef;
|
|
|
|
|
2017-01-16 12:26:35 +00:00
|
|
|
} else {
|
|
|
|
my $list = "";
|
2017-01-24 13:29:30 +00:00
|
|
|
$list .= "reopen:noArg getPlayers:noArg send:noArg";
|
2017-01-16 12:26:35 +00:00
|
|
|
return "Unknown argument $cmd, choose one of $list";
|
|
|
|
}
|
2017-01-24 13:29:30 +00:00
|
|
|
|
|
|
|
HEOSMaster_Write($hash,$action,undef);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub HEOSMaster_send($) {
|
|
|
|
|
|
|
|
my $hash = shift;
|
|
|
|
|
|
|
|
HEOSMaster_Write($hash,'getPlayerState',"pid=-512565195");
|
|
|
|
}
|
|
|
|
|
|
|
|
sub HEOSMaster_firstRun($) {
|
|
|
|
|
|
|
|
my $hash = shift;
|
|
|
|
my $name = $hash->{NAME};
|
|
|
|
|
|
|
|
RemoveInternalTimer($hash);
|
|
|
|
|
|
|
|
#if( !IsDisabled($name) ) {
|
|
|
|
|
|
|
|
HEOSMaster_Open($hash);
|
|
|
|
if( $hash->{CD} ) {
|
|
|
|
|
|
|
|
HEOSMaster_Write($hash,$heosCmds{enableEvents},'on');
|
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - enable events at HEOS CLI";
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - failed enable events at HEOS CLI";
|
|
|
|
}
|
|
|
|
#}
|
2017-01-16 12:26:35 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 13:29:30 +00:00
|
|
|
sub HEOSMaster_Open($) {
|
2017-01-16 12:26:35 +00:00
|
|
|
|
|
|
|
my $hash = shift;
|
|
|
|
my $name = $hash->{NAME};
|
|
|
|
my $host = $hash->{HOST};
|
|
|
|
my $port = 1255;
|
2017-01-18 13:53:54 +00:00
|
|
|
my $timeout = 1;
|
2017-01-16 12:26:35 +00:00
|
|
|
|
|
|
|
|
2017-01-24 13:29:30 +00:00
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - Baue Socket Verbindung auf";
|
2017-01-18 13:53:54 +00:00
|
|
|
|
2017-01-24 13:29:30 +00:00
|
|
|
|
2017-01-18 13:53:54 +00:00
|
|
|
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";
|
|
|
|
|
2017-01-18 13:53:54 +00:00
|
|
|
$hash->{FD} = $socket->fileno();
|
|
|
|
$hash->{CD} = $socket; # sysread / close won't work on fileno
|
|
|
|
$selectlist{$name} = $hash;
|
|
|
|
|
|
|
|
readingsSingleUpdate($hash, 'state', 'connected', 1 );
|
|
|
|
|
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - Socket Connected";
|
|
|
|
}
|
|
|
|
|
2017-01-24 13:29:30 +00:00
|
|
|
sub HEOSMaster_Close($) {
|
2017-01-18 13:53:54 +00:00
|
|
|
|
|
|
|
my $hash = shift;
|
|
|
|
my $name = $hash->{NAME};
|
|
|
|
|
|
|
|
return if( !$hash->{CD} );
|
|
|
|
|
|
|
|
close($hash->{CD}) if($hash->{CD});
|
|
|
|
delete($hash->{FD});
|
|
|
|
delete($hash->{CD});
|
|
|
|
delete($selectlist{$name});
|
|
|
|
readingsSingleUpdate($hash, 'state', 'not connected', 1 );
|
|
|
|
}
|
|
|
|
|
2017-01-24 13:29:30 +00:00
|
|
|
sub HEOSMaster_Write($@) {
|
|
|
|
|
|
|
|
my ($hash,$heosCmd,$value) = @_;
|
|
|
|
my $name = $hash->{NAME};
|
2017-01-18 13:53:54 +00:00
|
|
|
|
2017-01-24 13:29:30 +00:00
|
|
|
my $string = "heos://$heosCmds{$heosCmd}";
|
|
|
|
$string .= "${value}" if(defined($value));
|
|
|
|
$string .= "\r\n";
|
|
|
|
|
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - WriteFn called";
|
2017-01-18 13:53:54 +00:00
|
|
|
|
2017-01-24 13:29:30 +00:00
|
|
|
return Log3 $name, 3, "HEOSMaster ($name) - socket not connected"
|
|
|
|
unless($hash->{CD});
|
|
|
|
|
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - $string";
|
|
|
|
syswrite($hash->{CD}, $string);
|
|
|
|
return undef;
|
2017-01-18 13:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub HEOSMaster_Read($) {
|
|
|
|
|
|
|
|
my $hash = shift;
|
|
|
|
my $name = $hash->{NAME};
|
|
|
|
|
|
|
|
my $len;
|
|
|
|
my $buf;
|
|
|
|
|
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - ReadFn gestartet";
|
|
|
|
|
2017-01-24 13:29:30 +00:00
|
|
|
$len = sysread($hash->{CD},$buf,1024);
|
2017-01-18 13:53:54 +00:00
|
|
|
|
|
|
|
if( !defined($len) || !$len ) {
|
|
|
|
Log 1, "Länge? !!!!!!!!!!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unless( defined $buf) {
|
2017-01-16 12:26:35 +00:00
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - Keine Daten empfangen";
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
2017-01-18 13:53:54 +00:00
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - Daten: $buf";
|
2017-01-24 13:29:30 +00:00
|
|
|
HEOSMaster_ResponseProcessing($hash,$buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub HEOSMaster_ResponseProcessing($$) {
|
|
|
|
|
|
|
|
my ($hash,$json) = @_;
|
|
|
|
my $name = $hash->{NAME};
|
|
|
|
|
|
|
|
my $decode_json;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - JSON String: $json";
|
|
|
|
|
|
|
|
|
|
|
|
return Log3 $name, 3, "HEOSMaster ($name) - empty answer received"
|
|
|
|
unless( defined($json));
|
|
|
|
|
|
|
|
$decode_json = decode_json($json);
|
|
|
|
|
|
|
|
return Log3 $name, 3, "HEOSMaster ($name) - decode_json has no Hash"
|
|
|
|
unless(ref($decode_json) eq "HASH");
|
|
|
|
|
|
|
|
|
|
|
|
if( defined($decode_json->{heos}{result}) and defined($decode_json->{heos}{command}) ) {
|
|
|
|
|
|
|
|
HEOSMaster_WriteReadings($hash,$decode_json);
|
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - call Sub HEOSMaster_WriteReadings";
|
|
|
|
}
|
|
|
|
|
|
|
|
if( $decode_json->{heos}{command} =~ /^player/ ) {
|
|
|
|
if( ref($decode_json->{payload}) eq "ARRAY" and scalar(@{$decode_json->{payload}}) > 0) {
|
|
|
|
|
|
|
|
foreach my $payload (@{$decode_json->{payload}}) {
|
|
|
|
|
|
|
|
$json = '{"pid": "';
|
|
|
|
$json .= "$payload->{pid}";
|
|
|
|
$json .= '","heos": {"command": "player/get_players"}}';
|
|
|
|
|
|
|
|
Dispatch($hash,$json,undef);
|
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - call Dispatcher";
|
|
|
|
|
|
|
|
}
|
|
|
|
} elsif( defined($decode_json->{payload}{pid}) ) {
|
|
|
|
|
|
|
|
Dispatch($hash,$json,undef);
|
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - call Dispatcher";
|
|
|
|
|
|
|
|
} elsif( $decode_json->{heos}{message} =~ /^pid=/ ) {
|
|
|
|
|
|
|
|
Dispatch($hash,$json,undef);
|
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - call Dispatcher";
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Log3 $name, 3, "HEOSMaster ($name) - no Match for processing data";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub HEOSMaster_WriteReadings($$) {
|
|
|
|
|
|
|
|
my ($hash,$decode_json) = @_;
|
|
|
|
my $name = $hash->{NAME};
|
|
|
|
|
|
|
|
|
|
|
|
readingsBeginUpdate($hash);
|
|
|
|
readingsBulkUpdate( $hash, "lastCommand", $decode_json->{heos}{command} );
|
|
|
|
readingsBulkUpdate( $hash, "lastResult", $decode_json->{heos}{result} );
|
|
|
|
|
|
|
|
if( ref($decode_json->{payload}) ne "ARRAY" ) {
|
|
|
|
readingsBulkUpdate( $hash, "lastPlayerId", $decode_json->{payload}{pid} );
|
|
|
|
readingsBulkUpdate( $hash, "lastPlayerName", $decode_json->{payload}{name} );
|
|
|
|
}
|
|
|
|
|
|
|
|
readingsEndUpdate( $hash, 1 );
|
|
|
|
|
|
|
|
return undef;
|
2017-01-16 12:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-18 13:53:54 +00:00
|
|
|
|
|
|
|
|
2017-01-16 12:26:35 +00:00
|
|
|
1;
|