PDA

View Full Version : power on/off status change event for players?



starcat
2010-03-14, 17:04
Hi guys,

For an automation project with several players and a single Squeezebox Server, I would like to ask if it is possible to have a player power on/off status change event generated by the server. Currently I am parsing the status each 30 sec or so but having an event triggered for such sort of status change is way more elegant and would save a lot of bandwidth.

Is there anything similar planned and could something like this be implemented? Or any other hints how I can acomplish this?

Thanks much and greetings!

wernerL
2010-03-14, 22:54
Hi starcat,

I don't know which kind of change event you want to be generated?

But you could write a little Squeezebox Server plugin to monitor the player power states and for example send an UDP network packet.

Here is a code snippet:

...

use IO::Socket;
use Slim::Utils::Log;
use Slim::Control::Request;

my $log = Slim::Utils::Log->addLogCategory( {
category => 'plugin.powerMonitor',
defaultLevel => 'ERROR',
description => 'PLUGIN_POWER_MONITOR',
} );

...

sub initPlugin {
# Subscribe to power events
Slim::Control::Request::subscribe(
\&powerCallback,
[['power']]
);
}

sub shutdownPlugin {
Slim::Control::Request::unsubscribe( \&powerCallback );
}

sub powerCallback {
my $request = shift;
my $client = $request->client() || return;

my $sock = IO::Socket::INET->new(
Proto => 'udp',
PeerPort => 5000,
PeerAddr => '192.168.0.11',
);
if(!$sock) {
$log->error("Could not create socket: $!");
return;
}

my $msg = $client->name() . ':' . $client->power();
$log->debug($msg);
my $rc = $sock->send($msg);
if(!$rc) {
$log->error("Send error: $!");
}
}

In the initPlugin() method you subscribe to player power events and give a callback method wich will be called everytime the power state of a player changes.
In the callback method you could do whatever you want. In the example a udp network packet will be sent to 192.168.0.11:5000 with the messages "playerName:on" or "playerName:off"

Maybe it helps

Best regard
Werner

starcat
2010-03-15, 02:02
Thanks for the hint! Looks very promising. Although I have some programming experience, I haven't written any plugins for Slim Server so far. Could somebody show how the complete plugin may look like? Sending some strings like "playerName on" and "playerName off" to a specific IP address and port would be perfectly fine. I am looking to have an event generated for power on/off only, nothing more fancy. The one above is what I need, just coplete it please so that it can be plugedin.

wernerL
2010-03-15, 10:23
Hello starcat,

I attached a working copy of a little plugin that sends an udp network packet when the power state of a player changes.

You need to modify the receiver ip adress and port which are hardcoded in the Plugin.pm file.
You can also modify the string which is sent when a player changes its power state. It's currently player-mac:player-name:0 for power off and player-mac:player-name:1 for power on.

This is the first time i wrote a perl programm (my first Squeezebox Server plugin also) but for me it's working at least with version 7.4.2. Maybe it has some "don't do this" that I am not aware. (In the the install.xml file of other plugins for example they use an id-tag value which I just picked up randomly).

After unzipping, customize it to your needs and copy the folder PowerMonitor to your Squeezebox Server plugin folder (on Windows C:\Programs\Squeezebox\server\Plugins and restart the squeezebox server.

Good luck
Werner

starcat
2010-03-15, 15:02
Looks great! Just installed the plugin successfuly and it shows up in the SlimServer web interface plugin list. I am running Version 7.3.2.

The specified IP address in the plugin is the one of the peer I am connecting to, right.

viczena
2010-03-16, 08:43
Hi, your program is a really great help for my projects. i need to get status informations provided by functions like "listen","subscribe" or "displaystatus" by a UDP connection.

Couls you provide some more informations how to incorporate these functions?

thanks peter

wernerL
2010-03-16, 10:06
Hello Peter,

I am not sure what you want to achieve?

I guess you know the document about the command line interface? (http://yourSqueezeboxServerIp:9000/html/docs/help.html -> The Squeezebox Server Command Line Interface)

I don't know if I can help you further. As I said before I am not a perl programmer and this is my first Squeezebox-Server plugin.

But I will have a look if you could give me more detailed information. I also run a Gira homeserver use the hsslim.exe.

Since I think you are german too, it would be much easier for me if proceed this discussion in the german forum.

Bis dann
Werner

starcat
2010-03-16, 11:07
Hi, your program is a really great help for my projects. i need to get status informations provided by functions like "listen","subscribe" or "displaystatus" by a UDP connection.

Couls you provide some more informations how to incorporate these functions?
thanks peter

Peter, in order to subscribe or listen to status updates, you just telnet to the Slimserver IP and port and execute a command described in detail on the Help page in SlimServer web GUI. It is basically something like this

telnet Slim_IP 9090

Command Examples:

Request: "subscribe mixer,pause<LF>"
Response: "subscribe mixer,pause<LF>"
"04:20:00:12:23:45 mixer volume 25<LF>"
"04:20:00:12:23:45 pause<LF>"

starcat
2010-03-16, 13:10
Guys, found an easier and more portable way to register an event driven notification for power status change, however all status changes can be registered this way.

Just telnet to slimIP on port 9090 (where the CLI is running) and execute the command
subscribe power

Eeach time the power status of a player changes the server will respond with
MAC-address-of-player power 0
or
MAC-address-of-player power 1

You can register the event handler for several notifications by separating them by commas, like
subscribe power,mixer

See the CLI documentation in the server's Web GUI for a full list and documentation.

WernerL, thanks once again!

viczena
2010-03-16, 16:49
Hi, i cannot establish a telnet communication from my homeserver. i am dependendent on UDP packets which i can listen for.

peter

starcat
2010-03-16, 17:18
Hi, i cannot establish a telnet communication from my homeserver. i am dependendent on UDP packets which i can listen for.


Ok, then you would need a plugin as the one WernerL has provided. It transmits over UDP.

viczena
2010-03-17, 14:23
Hi i have absolutely no experiece with perl, as with hash or client().

I have modified the sample from WernerL as far as i could (and understood). Now I am stuck. I need the artworkID for the current player. And i do not know how to get it. does anybode have an idea? attached is my plugin.pm. The only thing i could manage is to set a 1, if coverart exist and a 0 if not.

Does anybody have an explanation of the whole meaning and function of the datatypes ($request, $client)? how to acces all the functions in the libraries?

Thanks Peter

robbrad
2014-07-10, 15:03
If it helps I changes the plugin to TCP and wrote the following script to control the devices vis HDMI CEC from a Raspberry Pi


#!/bin/bash

netcat -lk 6500 | while read line

do
match=$(echo $line | grep -c '00:0f:13:40:19:05:Dining_Room:1')
match2=$(echo $line | grep -c '00:0f:13:40:19:05:Dining_Room:0')

if [ $match -eq 1 ]; then
powerStatus=$(echo "pow 5" | cec-client -s -d 1 |grep "power status" |cut -d ' ' -f 3)
if [ "$powerStatus" = "standby" ]; then
echo "Powering On Amp....."
echo "on 5" | cec-client -s -d 1
fi
elif [ $match2 -eq 1 ]; then
powerStatus=$(echo "pow 5" | cec-client -s -d 1 |grep "power status" |cut -d ' ' -f 3)
if [ "$powerStatus" = "on" ]; then
echo "Powering Off Amp....."
echo "standby 5" | cec-client -s -d 1
fi
fi
done