ready for use - play stop volume mute pause

This commit is contained in:
Marko Oldenburg 2017-01-24 14:29:30 +01:00
parent 3755560a87
commit 2a0eb3a723
2 changed files with 593 additions and 48 deletions

View File

@ -42,14 +42,21 @@ use strict;
use warnings;
use JSON;
use Net::Telnet;
my $missingModulRemote;
eval "use Net::Telnet;1" or $missingModulRemote .= "Net::Telnet ";
use IO::Socket::INET;
my $version = "0.1.17";
my $version = "0.0.22";
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?'
);
@ -58,10 +65,14 @@ sub HEOSMaster_Initialize($);
sub HEOSMaster_Define($$);
sub HEOSMaster_Undef($$);
sub HEOSMaster_Set($@);
sub HEOSMaster_Connect($);
sub HEOSMaster_Disconnect($);
sub HEOSMaster_send($);
sub HEOSMaster_Open($);
sub HEOSMaster_Close($);
sub HEOSMaster_Read($);
sub HEOSMaster_Write($@);
sub HEOSMaster_Attr(@);
sub HEOSMaster_firstRun($);
sub HEOSMaster_ResponseProcessing($$);
sub HEOSMaster_WriteReadings($$);
@ -72,8 +83,9 @@ sub HEOSMaster_Initialize($) {
# Provider
$hash->{ReadFn} = "HEOSMaster_Read";
#$hash->{WriteFn} = "HEOSMaster_Read";
#$hash->{Clients} = ":HEOSPlayer:";
$hash->{WriteFn} = "HEOSMaster_Write";
$hash->{Clients} = ":HEOSPlayer:";
$hash->{MatchList} = { "1:HEOSPlayer" => '.*{"command":."player.*' };
# Consumer
@ -81,9 +93,9 @@ sub HEOSMaster_Initialize($) {
#$hash->{GetFn} = "HEOSMaster_Get";
$hash->{DefFn} = "HEOSMaster_Define";
$hash->{UndefFn} = "HEOSMaster_Undef";
#$hash->{AttrFn} = "HEOSMaster_Attr";
#$hash->{AttrList} = "disable:1 ".
# $readingFnAttributes;
$hash->{AttrFn} = "HEOSMaster_Attr";
$hash->{AttrList} = "disable:1 ".
$readingFnAttributes;
foreach my $d(sort keys %{$modules{HEOSMaster}{defptr}}) {
@ -113,10 +125,20 @@ sub HEOSMaster_Define($$) {
Log3 $name, 3, "HEOSMaster ($name) - defined with host $host";
$attr{$name}{room} = "HEOS" if( !defined( $attr{$name}{room} ) );
readingsSingleUpdate($hash, 'state', 'Initialized', 1 );
$modules{HEOSMaster}{defptr}{$hash->{HOST}} = $hash;
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}) );
}
return undef;
}
@ -128,31 +150,66 @@ sub HEOSMaster_Undef($$) {
my $host = $hash->{HOST};
my $name = $hash->{NAME};
HEOSMaster_Close($hash);
delete $modules{HEOSMaster}{defptr}{$hash->{HOST}};
return undef;
}
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;
}
sub HEOSMaster_Set($@) {
my ($hash, $name, $cmd, @args) = @_;
my ($arg, @params) = @args;
my $action;
if($cmd eq 'startConnect') {
return "usage: startConnect" if( @args != 0 );
if($cmd eq 'reopen') {
return "usage: reopen" if( @args != 0 );
HEOSMaster_Connect($hash);
HEOSMaster_Close($hash);
HEOSMaster_Open($hash) if( !$hash->{CD} or !defined($hash->{CD}) );
return undef;
} elsif($cmd eq 'stopConnect') {
return "usage: stopConnect" if( @args != 0 );
HEOSMaster_Disconnect($hash);
} elsif($cmd eq 'getPlayers') {
return "usage: getPlayers" if( @args != 0 );
return undef;
$action = $cmd;
} elsif($cmd eq 'send') {
return "usage: send" if( @args != 0 );
@ -163,30 +220,54 @@ sub HEOSMaster_Set($@) {
} else {
my $list = "";
$list .= "startConnect:noArg stopConnect:noArg send:noArg";
$list .= "reopen:noArg getPlayers:noArg send:noArg";
return "Unknown argument $cmd, choose one of $list";
}
HEOSMaster_Write($hash,$action,undef);
}
sub HEOSMaster_Connect($) {
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";
}
#}
}
sub HEOSMaster_Open($) {
my $hash = shift;
my $name = $hash->{NAME};
my $host = $hash->{HOST};
my $port = 1255;
my $timeout = 1;
my $msg;
Log3 $name, 3, "HEOSMaster ($name) - Baue Socket Verbindung auf";
#my $socket = IO::Socket::INET->new(PeerAddr => $host,
# PeerPort => $port,
# Proto => 'tcp',
# Type => SOCK_STREAM,
# Timeout => $timeout )
# or return Log3 $name, 3, "HEOSMaster ($name) Couldn't connect to $host:$port";
my $socket = new Net::Telnet ( Host=>$host,
Port => $port,
Timeout=>$timeout,
@ -200,11 +281,9 @@ sub HEOSMaster_Connect($) {
readingsSingleUpdate($hash, 'state', 'connected', 1 );
Log3 $name, 3, "HEOSMaster ($name) - Socket Connected";
syswrite($hash->{CD}, "heos://system/register_for_change_events?enable=on\r\n") if( defined($hash->{CD}) );
}
sub HEOSMaster_Disconnect($) {
sub HEOSMaster_Close($) {
my $hash = shift;
my $name = $hash->{NAME};
@ -218,16 +297,23 @@ sub HEOSMaster_Disconnect($) {
readingsSingleUpdate($hash, 'state', 'not connected', 1 );
}
sub HEOSMaster_send($) {
sub HEOSMaster_Write($@) {
my ($hash,$heosCmd,$value) = @_;
my $name = $hash->{NAME};
my $hash = shift;
my $name = $hash->{NAME};
my $buf;
my $string = "heos://$heosCmds{$heosCmd}";
$string .= "${value}" if(defined($value));
$string .= "\r\n";
return Log3 $name, 3, "HEOSMaster ($name) - CD nicht vorhanden" unless( defined($hash->{CD}));
syswrite($hash->{CD}, "heos://player/get_players\r\n");
Log3 $name, 3, "HEOSMaster ($name) - Syswrite ausgeführt";
Log3 $name, 3, "HEOSMaster ($name) - WriteFn called";
return Log3 $name, 3, "HEOSMaster ($name) - socket not connected"
unless($hash->{CD});
Log3 $name, 3, "HEOSMaster ($name) - $string";
syswrite($hash->{CD}, $string);
return undef;
}
sub HEOSMaster_Read($) {
@ -240,7 +326,7 @@ sub HEOSMaster_Read($) {
Log3 $name, 3, "HEOSMaster ($name) - ReadFn gestartet";
$len = sysread($hash->{CD},$buf,4096);
$len = sysread($hash->{CD},$buf,1024);
if( !defined($len) || !$len ) {
Log 1, "Länge? !!!!!!!!!!";
@ -253,6 +339,84 @@ sub HEOSMaster_Read($) {
}
Log3 $name, 3, "HEOSMaster ($name) - Daten: $buf";
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;
}

View File

@ -0,0 +1,381 @@
###############################################################################
#
# Developed with Kate
#
# (c) 2016-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$
#
###############################################################################
package main;
use strict;
use warnings;
use JSON;
my $version = "0.1.17";
# 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_GetUpdate($);
sub HEOSPlayer_Initialize($) {
my ($hash) = @_;
$hash->{Match} = '.*{"command":."player.*';
# Provider
$hash->{SetFn} = "HEOSPlayer_Set";
$hash->{DefFn} = "HEOSPlayer_Define";
$hash->{UndefFn} = "HEOSPlayer_Undef";
$hash->{AttrFn} = "HEOSPlayer_Attr";
$hash->{ParseFn} = "HEOSPlayer_Parse";
$hash->{AttrList} = "IODev ".
"disable:1 ".
$readingFnAttributes;
foreach my $d(sort keys %{$modules{HEOSPlayer}{defptr}}) {
my $hash = $modules{HEOSPlayer}{defptr}{$d};
$hash->{VERSION} = $version;
}
}
sub HEOSPlayer_Define($$) {
my ( $hash, $def ) = @_;
my @a = split( "[ \t]+", $def );
splice( @a, 1, 1 );
my $iodev;
my $i = 0;
foreach my $param ( @a ) {
if( $param =~ m/IODev=([^\s]*)/ ) {
$iodev = $1;
splice( @a, $i, 3 );
last;
}
$i++;
}
return "too few parameters: define <name> HEOSPlayer <pid>" if( @a < 2 );
my ($name,$pid) = @a;
$hash->{PID} = $pid;
$hash->{VERSION} = $version;
$hash->{STATE} = 'Initialized';
AssignIoPort($hash,$iodev) if( !$hash->{IODev} );
if(defined($hash->{IODev}->{NAME})) {
Log3 $name, 3, "HEOSPlayer ($name) - I/O device is " . $hash->{IODev}->{NAME};
} else {
Log3 $name, 1, "HEOSPlayer ($name) - no I/O device";
}
$iodev = $hash->{IODev}->{NAME};
my $code = abs($pid);
$code = $iodev."-".$code if( defined($iodev) );
my $d = $modules{HEOSPlayer}{defptr}{$code};
return "HEOSPlayer device $hash->{pid} on HEOSMaster $iodev already defined as $d->{NAME}."
if( defined($d)
&& $d->{IODev} == $hash->{IODev}
&& $d->{NAME} ne $name );
$modules{HEOSPlayer}{defptr}{$code} = $hash;
Log3 $name, 3, "HEOSPlayer ($name) - defined with Code: $code";
$attr{$name}{room} = "HEOS" if( !defined( $attr{$name}{room} ) );
if( $init_done ) {
HEOSPlayer_GetUpdate($hash);
} else {
InternalTimer( gettimeofday()+15, "HEOSPlayer_GetUpdate", $hash, 0 );
}
return undef;
}
sub HEOSPlayer_Undef($$) {
my ( $hash, $arg ) = @_;
my $pid = $hash->{PID};
my $name = $hash->{NAME};
RemoveInternalTimer($hash);
my $code = abs($pid);
$code = $hash->{IODev}->{NAME} ."-". $code if( defined($hash->{IODev}->{NAME}) );
Log3 $name, 3, "HEOSPlayer ($name) - undefined with Code: $code";
delete($modules{HEOSPlayer}{defptr}{$code});
return undef;
}
sub HEOSPlayer_Attr(@) {
my ( $cmd, $name, $attrName, $attrVal ) = @_;
my $hash = $defs{$name};
my $token = $hash->{IODev}->{TOKEN};
if( $attrName eq "disable" ) {
if( $cmd eq "set" and $attrVal eq "1" ) {
readingsSingleUpdate ( $hash, "state", "disabled", 1 );
Log3 $name, 3, "HEOSPlayer ($name) - disabled";
}
elsif( $cmd eq "del" ) {
readingsSingleUpdate ( $hash, "state", "active", 1 );
Log3 $name, 3, "HEOSPlayer ($name) - enabled";
}
}
if( $attrName eq "disabledForIntervals" ) {
if( $cmd eq "set" ) {
Log3 $name, 3, "HEOSPlayer ($name) - enable disabledForIntervals";
readingsSingleUpdate ( $hash, "state", "Unknown", 1 );
}
elsif( $cmd eq "del" ) {
readingsSingleUpdate ( $hash, "state", "active", 1 );
Log3 $name, 3, "HEOSPlayer ($name) - delete disabledForIntervals";
}
}
}
sub HEOSPlayer_Set($$@) {
my ($hash, $name, @aa) = @_;
my ($cmd, @args) = @aa;
my $action;
my $heosCmd;
if( $cmd eq 'statusRequest' ) {
return "usage: statusRequest" if( @args != 0 );
HEOSPlayer_GetUpdate($hash);
return undef;
} elsif( $cmd eq 'play' ) {
return "usage: play" if( @args != 0 );
$heosCmd = 'setPlayState';
$action = 'state=play';
} elsif( $cmd eq 'stop' ) {
return "usage: stop" if( @args != 0 );
$heosCmd = 'setPlayState';
$action = 'state=stop';
} elsif( $cmd eq 'pause' ) {
return "usage: pause" if( @args != 0 );
$heosCmd = 'setPlayState';
$action = 'state=pause';
} elsif( $cmd eq 'mute' ) {
return "usage: mute on/off" if( @args != 1 );
$heosCmd = 'setMute';
$action = "state=$args[0]";
} elsif( $cmd eq 'volume' ) {
return "usage: volume 0-100" if( @args != 1 );
$heosCmd = 'setVolume';
$action = "level=$args[0]";
} else {
my $list = "statusRequest:noArg play:noArg stop:noArg pause:noArg mute:on,off volume:slider,0,5,100";
return "Unknown argument $cmd, choose one of $list";
}
IOWrite($hash,"$heosCmd","pid=$hash->{PID}&$action");
Log3 $name, 3, "HEOSPlayer ($name) - IOWrite: pid=$hash->{PID}&state=$action IODevHash=$hash->{IODev}";
return undef;
}
sub HEOSPlayer_GetUpdate($) {
my $hash = shift;
IOWrite($hash,'getPlayerState',"pid=$hash->{PID}");
return undef;
}
sub HEOSPlayer_Parse($$) {
my ($io_hash,$json) = @_;
my $name = $io_hash->{NAME};
my $pid;
my $decode_json;
$decode_json = decode_json($json);
Log3 $name, 3, "HEOSPlayer ($name) - ParseFn wurde aufgerufen";
if( defined($decode_json->{pid}) ) {
$pid = $decode_json->{pid};
my $code = abs($pid);
$code = $io_hash->{NAME} ."-". $code if( defined($io_hash->{NAME}) );
if( my $hash = $modules{HEOSPlayer}{defptr}{$code} ) {
my $name = $hash->{NAME};
HEOSPlayer_GetUpdate($hash);
Log3 $name, 3, "HEOSPlayer ($name) - find logical device: $hash->{NAME}";
return $hash->{NAME};
} else {
my $devname = "HEOSPlayer".abs($pid);
return "UNDEFINED $devname HEOSPlayer $pid IODev=$name";
}
} else {
return Log3 $name, 3, "result not success"
unless($decode_json->{heos}{result} eq "success");
if( defined($decode_json->{payload}{pid}) ) {
$pid = $decode_json->{payload}{pid};
} elsif ( $decode_json->{heos}{message} =~ /^pid=/ ) {
my @pid = split('&', $decode_json->{heos}{message});
$pid = substr($pid[0],4);
Log3 $name, 3, "HEOSPlayer ($name) - PID[0]: $pid[0] and PID: $pid";
}
my $code = abs($pid);
$code = $io_hash->{NAME} ."-". $code if( defined($io_hash->{NAME}) );
if( my $hash = $modules{HEOSPlayer}{defptr}{$code} ) {
my $name = $hash->{NAME};
HEOSPlayer_WriteReadings($hash,$decode_json);
Log3 $name, 3, "HEOSPlayer ($name) - find logical device: $hash->{NAME}";
return $hash->{NAME};
} else {
my $devname = "HEOSPlayer".abs($pid);
return "UNDEFINED $devname HEOSPlayer $pid IODev=$name";
}
}
}
sub HEOSPlayer_WriteReadings($$) {
my ($hash,$decode_json) = @_;
my $name = $hash->{NAME};
my $value;
Log3 $name, 3, "HEOSPlayer ($name) - write data to readings";
############################
#### Status des Players
readingsBeginUpdate($hash);
if ( $decode_json->{heos}{command} =~ /set_play_state/ ) {
my @value = split('&', $decode_json->{heos}{message});
$value = substr($value[1],6);
readingsBulkUpdate( $hash, 'state', "$value" );
} elsif ( $decode_json->{heos}{command} =~ /set_volume/ ) {
my @value = split('&', $decode_json->{heos}{message});
$value = substr($value[1],6);
readingsBulkUpdate( $hash, 'volume', "$value" );
} else {
readingsBulkUpdate( $hash, 'state', 'Unknown' );
}
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} );
Log3 $name, 5, "HEOSPlayer ($name) - readings set for $name";
readingsEndUpdate( $hash, 1 );
return undef;
}
1;