door.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Time::HiRes qw( usleep sleep time );
  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 $STATE_SOCK = 'DOOR';
  19. my $OUTPUT_FILE = $DATA_DIR . $STATE_SOCK;
  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. # State
  29. my $state = 'OFF';
  30. my $stateLast = $state;
  31. my %exists = ();
  32. my $pushLast = 0;
  33. my $pullLast = time();
  34. my $update = 0;
  35. # Always force lights into OFF at launch
  36. $state = 'OFF';
  37. DMX::applyDataset($DIM{$state}, $state, $OUTPUT_FILE);
  38. # Sockets
  39. DMX::stateSocket($STATE_SOCK);
  40. DMX::stateSubscribe($STATE_SOCK);
  41. # Loop forever
  42. while (1) {
  43. # State is calculated; use newState to gather data
  44. my $newState = 'OFF';
  45. # Wait for state updates
  46. my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
  47. if (defined($cmdState)) {
  48. $newState = $cmdState;
  49. $pullLast = time();
  50. }
  51. # Die if we don't see regular updates
  52. if (time() - $pullLast > $PULL_TIMEOUT) {
  53. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  54. }
  55. # Calculate the new state
  56. $stateLast = $state;
  57. if ($newState eq 'ACTIVATE' || $exists{'DOOR_CMD'}) {
  58. $state = 'ACTIVATE';
  59. } else {
  60. $state = 'OFF';
  61. }
  62. # Force updates on a periodic basis
  63. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  64. if ($DEBUG) {
  65. print STDERR "Forcing periodic update\n";
  66. }
  67. $update = 1;
  68. }
  69. # Force updates on any state change
  70. if (!$update && $stateLast ne $state) {
  71. if ($DEBUG) {
  72. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  73. }
  74. $update = 1;
  75. }
  76. # Special handling for toggle systems: only push if the state is "ACTIVATE"
  77. if ($update && $state ne 'ACTIVATE') {
  78. if ($DEBUG) {
  79. print STDERR 'Skipping non-activate state: ' . $state . "\n";
  80. }
  81. $update = 0;
  82. }
  83. # Do not operate when LOCKed
  84. if ($update && $exists{'LOCK'}) {
  85. DMX::say('Door locked');
  86. if ($exists{'DOOR_CMD'}) {
  87. DMX::say('Access denied to: ' . $exists{'DOOR_CMD'});
  88. }
  89. $update = 0;
  90. }
  91. # Update the relay
  92. if ($update) {
  93. # Annouce the command
  94. if ($exists{'DOOR_CMD'}) {
  95. DMX::say('Door activated by ' . $exists{'DOOR_CMD'});
  96. }
  97. # Toggle
  98. $state = 'ACTIVATE';
  99. DMX::applyDataset($DIM{$state}, $state, $OUTPUT_FILE);
  100. usleep($RELAY_DELAY * 1000000);
  101. $state = 'OFF';
  102. DMX::applyDataset($DIM{$state}, $state, $OUTPUT_FILE);
  103. # Update the push time
  104. $pushLast = time();
  105. # Clear the update flag
  106. $update = 0;
  107. }
  108. }