bias.pl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. # User config
  10. my %DIM = (
  11. 'OFF' => [
  12. # Handled by rope.pl
  13. ],
  14. 'PLAY' => [
  15. { 'channel' => 12, 'value' => 255, 'time' => 0 }
  16. ],
  17. 'PAUSE' => [
  18. { 'channel' => 12, 'value' => 0, 'time' => 0, 'delay' => 10000 }
  19. ],
  20. 'MOTION' => [
  21. { 'channel' => 12, 'value' => 0, 'time' => 0 }
  22. ],
  23. );
  24. # App config
  25. my $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
  26. chomp($TEMP_DIR);
  27. my $DATA_DIR = $TEMP_DIR . 'plexMonitor/';
  28. my $OUTPUT_FILE = $DATA_DIR . 'BIAS';
  29. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  30. my $PUSH_TIMEOUT = 20;
  31. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  32. # Debug
  33. my $DEBUG = 0;
  34. if ($ENV{'DEBUG'}) {
  35. $DEBUG = 1;
  36. }
  37. # Command-line arguments
  38. my ($DELAY) = @ARGV;
  39. if (!$DELAY) {
  40. $DELAY = $PULL_TIMEOUT / 2;
  41. }
  42. # Sanity check
  43. if (!-d $DATA_DIR) {
  44. die("Bad config\n");
  45. }
  46. # Construct a list of valid states
  47. my %VALID = ();
  48. foreach my $key (keys(%DIM)) {
  49. $VALID{$key} = 1;
  50. }
  51. # Sockets
  52. DMX::stateSocket($STATE_SOCK);
  53. DMX::stateSubscribe($STATE_SOCK);
  54. # State
  55. my $state = 'OFF';
  56. my $stateLast = $state;
  57. my %exists = ();
  58. my $pushLast = 0;
  59. my $pullLast = time();
  60. my $update = 0;
  61. # Always force lights out at launch
  62. DMX::dim({ 'channel' => 12, 'value' => 0, 'time' => 0 });
  63. # Loop forever
  64. while (1) {
  65. # State is calculated; use newState to gather data
  66. my $newState = $state;
  67. # Wait for state updates
  68. my $cmdState = DMX::readState($DELAY, \%exists, \%VALID);
  69. if (defined($cmdState)) {
  70. $newState = $cmdState;
  71. $pullLast = time();
  72. }
  73. # Accept the new state directly
  74. $stateLast = $state;
  75. $state = $newState;
  76. # Force updates on a periodic basis
  77. if (time() - $pushLast > $PUSH_TIMEOUT) {
  78. $update = 1;
  79. }
  80. # Die if we don't see regular updates
  81. if (time() - $pullLast > $PULL_TIMEOUT) {
  82. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  83. }
  84. # Force updates on any state change
  85. if ($stateLast ne $state) {
  86. if ($DEBUG) {
  87. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  88. }
  89. $update = 1;
  90. }
  91. # Update the lighting
  92. if ($update) {
  93. # Update
  94. DMX::applyDataset($DIM{$state}, $state, $OUTPUT_FILE);
  95. # Update the push time
  96. $pushLast = time();
  97. # Clear the update flag
  98. $update = 0;
  99. }
  100. }