| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- #!/usr/bin/perl
- use strict;
- use warnings;
- use IO::Socket::UNIX;
- use POSIX qw(ceil floor);
- # App config
- my $DELAY = 5;
- my $TIMEOUT = 5;
- my $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
- chomp($TEMP_DIR);
- my $DATA_DIR = $TEMP_DIR . 'plexMonitor/';
- my $CMD_FILE = $DATA_DIR . 'AMPLIFIER.socket';
- # Debug
- my $DEBUG = 0;
- if ($ENV{'DEBUG'}) {
- $DEBUG = 1;
- }
- # Sanity check
- if (!-d $DATA_DIR || !-S $CMD_FILE) {
- die("Bad config\n");
- }
- # Socket init
- my $sock = IO::Socket::UNIX->new(
- 'Peer' => $CMD_FILE,
- 'Type' => SOCK_DGRAM,
- 'Timeout' => $TIMEOUT
- ) or die('Unable to open socket: ' . $CMD_FILE . ": ${@}\n");
- # State
- my $state = 'INIT';
- my $stateLast = $state;
- my $projector = 0;
- # Loop forever
- while (1) {
- # Monitor the PROJECTOR file for state only
- {
- $projector = 0;
- my $fh;
- open($fh, $DATA_DIR . 'PROJECTOR')
- or die("Unable to open PROJECTOR\n");
- my $text = <$fh>;
- close($fh);
- if ($text =~ /1/) {
- $projector = 1;
- }
- if ($DEBUG) {
- print STDERR 'Projector: ' . $projector . "\n";
- }
- }
- # Monitor the AMPLIFIER file for state only
- {
- $stateLast = 'OFF';
- my $fh;
- open($fh, $DATA_DIR . 'AMPLIFIER')
- or die("Unable to open AMPLIFIER\n");
- my $text = <$fh>;
- close($fh);
- if ($text =~ /1/) {
- $stateLast = 'ON';
- }
- if ($DEBUG) {
- print STDERR 'Amplifier: ' . $stateLast . "\n";
- }
- }
- # Calculate the new state -- track the projector power state
- if ($projector) {
- $state = 'ON';
- } else {
- $state = 'OFF';
- }
- # If the state changed, do something about if
- if ($state ne $stateLast) {
- if ($DEBUG) {
- print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
- }
- if ($state eq 'OFF' || $state eq 'ON') {
- $sock->send($state)
- or die('Unable to write command to socket: ' . $CMD_FILE . ': ' . $state . ": ${!}\n");
- }
- # Reset to TV @ 5.1 at power on
- if ($state eq 'ON') {
- # Wait for the amp to boot
- sleep($DELAY);
- $sock->send('TV')
- or die('Unable to write command to socket: ' . $CMD_FILE . ': TV' . ": ${!}\n");
- $sock->send('SURROUND')
- or die('Unable to write command to socket: ' . $CMD_FILE . ': SURROUND' . ": ${!}\n");
- }
- }
- # Wait and loop
- sleep($DELAY);
- }
- # Cleanup
- $sock->close();
- undef($sock);
- exit(0);
|