garage.pl 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Time::HiRes qw( usleep );
  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. # User config
  11. my $RELAY_DELAY = 0.20;
  12. my %DIM = (
  13. 'OFF' => [ { 'channel' => 16, 'value' => 0, 'time' => 0 } ],
  14. 'ACTIVATE' => [ { 'channel' => 16, 'value' => 255, 'time' => 0 } ],
  15. );
  16. # App config
  17. my $DATA_DIR = DMX::dataDir();
  18. my $OUTPUT_FILE = $DATA_DIR . 'GARAGE';
  19. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  20. my $PUSH_TIMEOUT = 20;
  21. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  22. my $DELAY = $PULL_TIMEOUT / 2;
  23. # Debug
  24. my $DEBUG = 0;
  25. if ($ENV{'DEBUG'}) {
  26. $DEBUG = 1;
  27. }
  28. # Sockets
  29. DMX::stateSocket($STATE_SOCK);
  30. DMX::stateSubscribe($STATE_SOCK);
  31. # State
  32. my $state = 'OFF';
  33. my $stateLast = $state;
  34. my %exists = ();
  35. my $pushLast = 0;
  36. my $pullLast = time();
  37. my $update = 0;
  38. # Always force lights out at launch
  39. DMX::dim({ 'channel' => 16, 'value' => 0, 'time' => 0 });
  40. # Loop forever
  41. while (1) {
  42. # State is calculated; use newState to gather data
  43. my $newState = 'OFF';
  44. # Wait for state updates
  45. my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
  46. if (defined($cmdState)) {
  47. $newState = $cmdState;
  48. $pullLast = time();
  49. }
  50. # Die if we don't see regular updates
  51. if (time() - $pullLast > $PULL_TIMEOUT) {
  52. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  53. }
  54. # Calculate the new state
  55. $stateLast = $state;
  56. if ($newState eq 'ACTIVATE' || $exists{'GARAGE_CMD'}) {
  57. $state = 'ACTIVATE';
  58. } else {
  59. $state = 'OFF';
  60. }
  61. # Force updates on a periodic basis
  62. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  63. if ($DEBUG) {
  64. print STDERR "Forcing periodic update\n";
  65. }
  66. $update = 1;
  67. }
  68. # Force updates on any state change
  69. if (!$update && $stateLast ne $state) {
  70. if ($DEBUG) {
  71. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  72. }
  73. $update = 1;
  74. }
  75. # Special handling for toggle systems: only push if the state is "ACTIVATE"
  76. if ($update && $state ne 'ACTIVATE') {
  77. if ($DEBUG) {
  78. print STDERR 'Skipping non-activate state: ' . $state . "\n";
  79. }
  80. $update = 0;
  81. }
  82. # Update the relay
  83. if ($update) {
  84. # Toggle
  85. $state = 'ACTIVATE';
  86. DMX::applyDataset($DIM{$state}, $state, $OUTPUT_FILE);
  87. usleep($RELAY_DELAY * 1000000);
  88. $state = 'OFF';
  89. DMX::applyDataset($DIM{$state}, $state, $OUTPUT_FILE);
  90. # Update the push time
  91. $pushLast = time();
  92. # Clear the update flag
  93. $update = 0;
  94. }
  95. }