announce.pl 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 $STATE_SOCK = 'ANNOUNCE';
  12. my $OUTPUT_FILE = $DATA_DIR . $STATE_SOCK;
  13. my $PUSH_TIMEOUT = 20;
  14. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  15. my $DELAY = $PULL_TIMEOUT / 2;
  16. my $SPEAK_DELAY = 5;
  17. # Debug
  18. my $DEBUG = 0;
  19. if ($ENV{'DEBUG'}) {
  20. $DEBUG = 1;
  21. }
  22. # State
  23. my %exists = ();
  24. my %last = ();
  25. my $pullLast = time();
  26. my $speakLast = time();
  27. # Sockets
  28. DMX::stateSocket($STATE_SOCK);
  29. DMX::stateSubscribe($STATE_SOCK);
  30. # Loop forever
  31. while (1) {
  32. # Remember the last state, for comparison
  33. %last = %exists;
  34. # Wait for state updates
  35. my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
  36. # Avoid repeated calls to time()
  37. my $now = time();
  38. # Record only valid states
  39. if (defined($cmdState)) {
  40. $pullLast = $now;
  41. }
  42. # Die if we don't see regular updates
  43. if ($now - $pullLast > $PULL_TIMEOUT) {
  44. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  45. }
  46. # Speak when BRIGHT changes
  47. if (exists($exists{'BRIGHT'}) && exists($last{'BRIGHT'}) && $exists{'BRIGHT'} ne $last{'BRIGHT'}) {
  48. if ($exists{'BRIGHT'}) {
  49. DMX::say('Lights - Full power');
  50. } else {
  51. DMX::say('Lights - Nominal power');
  52. }
  53. }
  54. # Speak when LIGHTS changes
  55. if (exists($exists{'LIGHTS'}) && exists($last{'LIGHTS'}) && $exists{'LIGHTS'} ne $last{'LIGHTS'}) {
  56. if ($exists{'LIGHTS'}) {
  57. DMX::say('Lights up');
  58. } else {
  59. DMX::say('Lights down');
  60. }
  61. }
  62. # Speak when NO_MOTION changes
  63. if (exists($exists{'NO_MOTION'}) && exists($last{'NO_MOTION'}) && $exists{'NO_MOTION'} ne $last{'NO_MOTION'}) {
  64. if ($exists{'NO_MOTION'}) {
  65. DMX::say('Motion detectors: Disabled');
  66. } else {
  67. DMX::say('Motion detectors: Enabled');
  68. }
  69. }
  70. # Speak when LOCK changes
  71. if (exists($exists{'LOCK'}) && exists($last{'LOCK'}) && $exists{'LOCK'} ne $last{'LOCK'}) {
  72. if ($exists{'LOCK'}) {
  73. DMX::say('System locked');
  74. } else {
  75. DMX::say('System unlocked');
  76. }
  77. }
  78. # Ongoing ALARM
  79. if ($exists{'ALARM'}) {
  80. if ($now - $speakLast > $SPEAK_DELAY) {
  81. $speakLast = $now;
  82. DMX::say('Unauthorized access detected.');
  83. }
  84. }
  85. }