Max Spicer's perl script, intended to be launched from a reoccurring
cron task, pretty much does what you want, I think.

Code:
--------------------
    
  #!/usr/bin/perl -w
  # $Date: 2005-10-30 14:26:26 +0000 (Sun, 30 Oct 2005) $ $Rev: 9 $
  # Copyright 2005 Max Spicer.
  # Feel free to reuse and modify, but please consider passing modifications 
back
  # to me so they can be included in future versions.
  # If you use this script, please let me know!
  
  # Shuts down the server if there aren't any players currently playing.
  # Designed to be cronned every x minutes during a time range when the server
  # should be off.
  #
  # Example crontab entry:
  # Shutdown every 30 minutes from 1am to 5:30am
  # 0,30 1-5 * * * /usr/local/sbin/shutdown.pl
  
  use strict;
  use IO::Socket;
  use POSIX qw(strftime);
  
  # Print debug output if true.  Higher values increase verbosity.
  my $debug = 0;
  
  # Turnoff players..
  my $turnoff = 1;
  
  # Amount of warning time in minutes to give before shutting down
  my $warnTime = 1;
  
  # Change server details below if necessary
  my $socket = IO::Socket::INET->new (PeerAddr => '127.0.0.1',
  PeerPort => 9090,
  Proto    => 'tcp',
  Type     => SOCK_STREAM)
  or die 'Couldn\'t connect to server';
  
  # Get the number of players
  my $playerCount = sendAndReceive('player count ?');
  $debug && print "$playerCount players found\n";
  
  # Interrogate current state of each player
  my $playersPlaying = 0;
  my @playerIds;
  for (my $i = 0; $i <  $playerCount; $i++) {
  # Get the player's internal id and store for future reference
  $playerIds[$i] = sendAndReceive("player id $i ?");
  $debug && print "Player $i has ID $playerIds[$i]\n";
  my $playerMode = sendAndReceive("$playerIds[$i] mode ?"); 
  $debug && print "Player ${i}'s mode is $playerMode\n";
  if ($playerMode eq 'play') {
  $playersPlaying++;
  }
  }
  
  $debug && print "$playersPlaying/$playerCount players playing\n";
  if (!$playersPlaying) {
  $debug && print "Shutting down\n";
  
  my $timeString = POSIX::strftime("%H:%m:%S", localtime());
  
  # Display a warning on each powered-on player
  for (my $i = 0; $i <  $playerCount; $i++) {
  sendAndReceive("$playerIds[$i] display $timeString%20-%20Warning: 
Server%20shutting%20down%20in%20$warnTime%20minutes 30");
  }
  
  if ($turnoff) { 
  # Turn off each powered-on player (this should trigger amp turn-off
  #print "Turning off SqueezeBoxen..\n";
  for (my $i = 0; $i <  $playerCount; $i++) {
        #turn off only the Transporter
        if ( $playerIds[$i] eq '00%3A04%3A20%3A10%3A01%3A04') {
                print "Turning off Transporter!\n";
                sendAndReceive("$playerIds[$i] power 0");
        }
  }
  }
  
  
  #stop Squeeze Center
  system("/sbin/service squeezecenter stop");
  
  # rotate the slimserver logs..
  print "Rotating logs..\n";
  system("/usr/sbin/logrotate /etc/logrotate.d/squeezecenter");
  
  # Shutdown!
  print "OK..now we are shutting down..\n";
  system("/sbin/shutdown -h +$warnTime No Slim players playing so system is 
shutting down.");
  
  
  close $socket;
  
  # Send given cmd to $socket and return answer with original command removed 
from
  # front if present.  Routine nicked from code by Felix Mueller. :-)
  sub sendAndReceive {
  my $cmd = shift;
  
  return if( $cmd eq "");
  
  print $socket "$cmd\n";
  $debug > 1 && print "Sent $cmd to server\n";
  my $answer = <$socket>;
  $debug > 1 && print "Server replied: $answer\n";
  $answer =~ s/$cmd //i;
  $answer =~ s/\n//;
  
  return $answer;
  }
  
--------------------

There might be a few of my own modifications in there.  I don't
remember the url to the original.

I should say that I don't think this script distinguishes between a
player being "stopped" and being "paused."


-- 
gharris999
------------------------------------------------------------------------
gharris999's Profile: http://forums.slimdevices.com/member.php?userid=115
View this thread: http://forums.slimdevices.com/showthread.php?t=49028

_______________________________________________
unix mailing list
[email protected]
http://lists.slimdevices.com/lists/listinfo/unix

Reply via email to