amplifierPower.pl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. # Local modules
  5. use Cwd qw(abs_path);
  6. use File::Basename qw(dirname);
  7. use lib dirname(abs_path($0));
  8. use DMX;
  9. # App config
  10. my $DATA_DIR = DMX::dataDir();
  11. my $OUTPUT_FILE = $DATA_DIR . 'AMPLIFIER_POWER';
  12. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  13. my $AMP_SOCK = $DATA_DIR . 'AMPLIFIER.socket';
  14. my $PUSH_TIMEOUT = 20;
  15. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  16. my $DELAY = $PULL_TIMEOUT / 2;
  17. # Debug
  18. my $DEBUG = 0;
  19. if ($ENV{'DEBUG'}) {
  20. $DEBUG = 1;
  21. }
  22. # Sockets
  23. DMX::stateSocket($STATE_SOCK);
  24. DMX::stateSubscribe($STATE_SOCK);
  25. my $amp = DMX::clientSock($AMP_SOCK);
  26. # State
  27. my $state = 'OFF';
  28. my $stateLast = $state;
  29. my %exists = ();
  30. my $pushLast = 0;
  31. my $pullLast = time();
  32. my $update = 0;
  33. # Loop forever
  34. while (1) {
  35. # State is calculated; use newState to gather data
  36. my $newState = $state;
  37. # Wait for state updates
  38. my $cmdState = DMX::readState($DELAY, \%exists, undef());
  39. if (defined($cmdState)) {
  40. $newState = $cmdState;
  41. $pullLast = time();
  42. }
  43. # Calculate the new state
  44. $stateLast = $state;
  45. if ($newState eq 'ON' || $exists{'RAVE'}) {
  46. $state = 'ON';
  47. } else {
  48. $state = 'OFF';
  49. }
  50. # Force updates on a periodic basis
  51. if (time() - $pushLast > $PUSH_TIMEOUT) {
  52. # Not for the amp
  53. #$update = 1;
  54. }
  55. # Die if we don't see regular updates
  56. if (time() - $pullLast > $PULL_TIMEOUT) {
  57. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  58. }
  59. # Force updates on any state change
  60. if ($stateLast ne $state) {
  61. if ($DEBUG) {
  62. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  63. }
  64. $update = 1;
  65. }
  66. # Update the amp
  67. if ($update) {
  68. # Extra debugging to record pushes
  69. if ($DEBUG) {
  70. print STDERR 'State: ' . $state . "\n";
  71. }
  72. # Send master power state
  73. if ($state eq 'OFF' || $state eq 'ON') {
  74. $amp->send($state)
  75. or die('Unable to write command to amp socket: ' . $state . ": ${!}\n");
  76. }
  77. # Reset to TV @ 5.1 at power on
  78. if ($state eq 'ON') {
  79. # Wait for the amp to boot
  80. sleep($DELAY);
  81. $amp->send('TV')
  82. or die('Unable to write command to amp socket: TV' . ": ${!}\n");
  83. $amp->send('SURROUND')
  84. or die('Unable to write command to amp socket: SURROUND' . ": ${!}\n");
  85. }
  86. # No output file
  87. # Update the push time
  88. $pushLast = time();
  89. # Clear the update flag
  90. $update = 0;
  91. }
  92. }