amplifierPower.pl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IPC::System::Simple qw( system capture );
  5. # Local modules
  6. use Cwd qw(abs_path);
  7. use File::Basename qw(dirname);
  8. use lib dirname(abs_path($0));
  9. use DMX;
  10. # Config
  11. my $USB_DEV = 'USB Audio CODEC ';
  12. my $AMP_DEV = 'Built-in Output';
  13. my $DEFAULT_DEV = $USB_DEV;
  14. my $RAVE_DEV = $AMP_DEV;
  15. # App config
  16. my $DATA_DIR = DMX::dataDir();
  17. my $OUTPUT_FILE = $DATA_DIR . 'AMPLIFIER_POWER';
  18. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  19. my $AMP_SOCK = $DATA_DIR . 'AMPLIFIER.socket';
  20. my $PUSH_TIMEOUT = 20;
  21. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  22. my $DELAY = $PULL_TIMEOUT / 2;
  23. my $START_DELAY = 3.5;
  24. my @AUDIO_SET = ('/Users/tv/bin/SwitchAudioSource', '-s');
  25. # Debug
  26. my $DEBUG = 0;
  27. if ($ENV{'DEBUG'}) {
  28. $DEBUG = 1;
  29. }
  30. # Sockets
  31. DMX::stateSocket($STATE_SOCK);
  32. DMX::stateSubscribe($STATE_SOCK);
  33. my $amp = DMX::clientSock($AMP_SOCK);
  34. # State
  35. my $state = 'OFF';
  36. my $mode = 'SURROUND';
  37. my $input = 'TV';
  38. my $output = $DEFAULT_DEV;
  39. my $stateLast = $state;
  40. my %exists = ();
  41. my $pushLast = 0;
  42. my $pullLast = time();
  43. my $update = 0;
  44. # Loop forever
  45. while (1) {
  46. # State is calculated; use newState to gather data
  47. my $newState = $state;
  48. # Wait for state updates
  49. my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
  50. if (defined($cmdState)) {
  51. $newState = $cmdState;
  52. $pullLast = time();
  53. }
  54. # Die if we don't see regular updates
  55. if (time() - $pullLast > $PULL_TIMEOUT) {
  56. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  57. }
  58. # Calculate the new state
  59. $stateLast = $state;
  60. if ($newState eq 'PLAY') {
  61. $state = 'ON';
  62. } elsif ($exists{'RAVE'}) {
  63. $state = 'RAVE';
  64. } elsif ($newState eq 'PAUSE') {
  65. $state = 'ON';
  66. } else {
  67. $state = 'OFF';
  68. }
  69. # Cacluate the channel mode
  70. if ($exists{'STEREO_CMD'} || $state eq 'RAVE') {
  71. $mode = 'STEREO';
  72. } else {
  73. $mode = 'SURROUND';
  74. }
  75. # Calculate the output device
  76. if ($state eq 'RAVE') {
  77. $output = $RAVE_DEV;
  78. } else {
  79. $output = $DEFAULT_DEV;
  80. }
  81. # Force updates on a periodic basis
  82. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  83. # Not for the amp
  84. #if ($DEBUG) {
  85. # print STDERR "Forcing periodic update\n";
  86. #}
  87. #$update = 1;
  88. }
  89. # Force updates on any state change
  90. if (!$update && $stateLast ne $state) {
  91. if ($DEBUG) {
  92. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  93. }
  94. $update = 1;
  95. }
  96. # Force updates when there is a physical state mistmatch
  97. if (!$update) {
  98. if (($state eq 'OFF' && $exists{'AMPLIFIER'}) || ($state eq 'ON' && !$exists{'AMPLIFIER'})) {
  99. if ($DEBUG) {
  100. print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'AMPLIFIER'} . "\n";
  101. }
  102. $update = 1;
  103. }
  104. }
  105. # Set the channel mode as needed
  106. if ($exists{'AMPLIFIER_MODE'} ne $mode) {
  107. if ($DEBUG) {
  108. print STDERR 'Setting mode to: ' . $mode . "\n";
  109. }
  110. $amp->send($mode)
  111. or die('Unable to write command to amp socket: ' . $input . ": ${!}\n");
  112. }
  113. # Set the amplifier input as needed
  114. if ($exists{'AMPLIFIER_INPUT'} ne $input) {
  115. if ($DEBUG) {
  116. print STDERR 'Setting input to: ' . $input . "\n";
  117. }
  118. $amp->send($input)
  119. or die('Unable to write command to amp socket: ' . $input . ": ${!}\n");
  120. }
  121. # Update the output device as needed
  122. if ($exists{'AUDIO_OUTPUT'} ne $output) {
  123. if ($DEBUG) {
  124. print STDERR 'Setting output to: ' . $output . "\n";
  125. }
  126. system(@AUDIO_SET, $output);
  127. }
  128. # Update the amp
  129. if ($update) {
  130. # Extra debugging to record pushes
  131. if ($DEBUG) {
  132. print STDERR 'State: ' . $state . "\n";
  133. }
  134. # Send master power state
  135. my $cmd = undef();
  136. if ($state eq 'OFF' || $state eq 'ON') {
  137. $cmd = $state;
  138. } elsif ($state eq 'RAVE') {
  139. $cmd = 'ON';
  140. }
  141. if (defined($cmd)) {
  142. $amp->send($cmd)
  143. or die('Unable to write command to amp socket: ' . $cmd . ": ${!}\n");
  144. }
  145. # No output file
  146. # Update the push time
  147. $pushLast = time();
  148. # Clear the update flag
  149. $update = 0;
  150. }
  151. }