amplifierPower.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. # App config
  11. my $DATA_DIR = DMX::dataDir();
  12. my $OUTPUT_FILE = $DATA_DIR . 'AMPLIFIER_POWER';
  13. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  14. my $AMP_SOCK = $DATA_DIR . 'AMPLIFIER.socket';
  15. my $PUSH_TIMEOUT = 20;
  16. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  17. my $DELAY = $PULL_TIMEOUT / 2;
  18. # Debug
  19. my $DEBUG = 0;
  20. if ($ENV{'DEBUG'}) {
  21. $DEBUG = 1;
  22. }
  23. # Sockets
  24. DMX::stateSocket($STATE_SOCK);
  25. DMX::stateSubscribe($STATE_SOCK);
  26. my $amp = DMX::clientSock($AMP_SOCK);
  27. # State
  28. my $state = 'OFF';
  29. my $mode = 'SURROUND';
  30. my $input = 'TV';
  31. my $stateLast = $state;
  32. my %exists = ();
  33. my $pushLast = 0;
  34. my $pullLast = time();
  35. my $update = 0;
  36. # Loop forever
  37. while (1) {
  38. # State is calculated; use newState to gather data
  39. my $newState = $state;
  40. # Wait for state updates
  41. my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
  42. if (defined($cmdState)) {
  43. $newState = $cmdState;
  44. $pullLast = time();
  45. }
  46. # Die if we don't see regular updates
  47. if (time() - $pullLast > $PULL_TIMEOUT) {
  48. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  49. }
  50. # Calculate the new state
  51. $stateLast = $state;
  52. if ($newState eq 'PLAY') {
  53. $state = 'ON';
  54. } elsif ($exists{'RAVE'}) {
  55. $state = 'RAVE';
  56. } elsif ($newState eq 'PAUSE') {
  57. $state = 'ON';
  58. } else {
  59. $state = 'OFF';
  60. }
  61. # Cacluate the channel mode
  62. if ($exists{'STEREO_CMD'} || $state eq 'RAVE') {
  63. $mode = 'STEREO';
  64. } else {
  65. $mode = 'SURROUND';
  66. }
  67. # Force updates on a periodic basis
  68. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  69. # Not for the amp
  70. #if ($DEBUG) {
  71. # print STDERR "Forcing periodic update\n";
  72. #}
  73. #$update = 1;
  74. }
  75. # Force updates on any state change
  76. if (!$update && $stateLast ne $state) {
  77. if ($DEBUG) {
  78. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  79. }
  80. $update = 1;
  81. }
  82. # Force updates when there is a physical state mistmatch
  83. if (!$update) {
  84. if (($state eq 'OFF' && $exists{'AMPLIFIER'}) || ($state eq 'ON' && !$exists{'AMPLIFIER'})) {
  85. if ($DEBUG) {
  86. print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'AMPLIFIER'} . "\n";
  87. }
  88. $update = 1;
  89. }
  90. }
  91. # Set the channel mode as needed
  92. if ($exists{'AMPLIFIER_MODE'} ne $mode) {
  93. if ($DEBUG) {
  94. print STDERR 'Setting mode to: ' . $mode . "\n";
  95. }
  96. $amp->send($mode)
  97. or die('Unable to write command to amp socket: ' . $input . ": ${!}\n");
  98. }
  99. # Set the amplifier input as needed
  100. if ($exists{'AMPLIFIER_INPUT'} ne $input) {
  101. if ($DEBUG) {
  102. print STDERR 'Setting input to: ' . $input . "\n";
  103. }
  104. $amp->send($input)
  105. or die('Unable to write command to amp socket: ' . $input . ": ${!}\n");
  106. }
  107. # Update the amp
  108. if ($update) {
  109. # Extra debugging to record pushes
  110. if ($DEBUG) {
  111. print STDERR 'State: ' . $state . "\n";
  112. }
  113. # Send master power state
  114. my $cmd = undef();
  115. if ($state eq 'OFF' || $state eq 'ON') {
  116. $cmd = $state;
  117. } elsif ($state eq 'RAVE') {
  118. $cmd = 'ON';
  119. }
  120. if (defined($cmd)) {
  121. $amp->send($cmd)
  122. or die('Unable to write command to amp socket: ' . $cmd . ": ${!}\n");
  123. }
  124. # No output file
  125. # Update the push time
  126. $pushLast = time();
  127. # Clear the update flag
  128. $update = 0;
  129. }
  130. }