announce.pl 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. # Avoid repeated system calls
  35. my $now = time();
  36. # Wait for state updates
  37. my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
  38. if (defined($cmdState)) {
  39. $pullLast = $now;
  40. }
  41. # Die if we don't see regular updates
  42. if ($now - $pullLast > $PULL_TIMEOUT) {
  43. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  44. }
  45. # Speak when BRIGHT changes
  46. if (exists($exists{'BRIGHT'}) && exists($last{'BRIGHT'}) && $exists{'BRIGHT'} ne $last{'BRIGHT'}) {
  47. if ($exists{'BRIGHT'}) {
  48. DMX::say('Lights - Full power');
  49. } else {
  50. DMX::say('Lights - Nominal power');
  51. }
  52. }
  53. # Speak when LIGHTS changes
  54. if (exists($exists{'LIGHTS'}) && exists($last{'LIGHTS'}) && $exists{'LIGHTS'} ne $last{'LIGHTS'}) {
  55. if ($exists{'LIGHTS'}) {
  56. DMX::say('Lights up');
  57. } else {
  58. DMX::say('Lights down');
  59. }
  60. }
  61. # Speak when NO_MOTION changes
  62. if (exists($exists{'NO_MOTION'}) && exists($last{'NO_MOTION'}) && $exists{'NO_MOTION'} ne $last{'NO_MOTION'}) {
  63. if ($exists{'NO_MOTION'}) {
  64. DMX::say('Motion detectors: Disabled');
  65. } else {
  66. DMX::say('Motion detectors: Enabled');
  67. }
  68. }
  69. # Speak when LOCK changes
  70. if (exists($exists{'LOCK'}) && exists($last{'LOCK'}) && $exists{'LOCK'} ne $last{'LOCK'}) {
  71. if ($exists{'LOCK'}) {
  72. DMX::say('System locked');
  73. } else {
  74. DMX::say('System unlocked');
  75. }
  76. }
  77. # Ongoing ALARM
  78. if ($exists{'ALARM'}) {
  79. if ($now - $speakLast > $SPEAK_DELAY) {
  80. $speakLast = $now;
  81. DMX::say('Unauthorized access detected.');
  82. }
  83. }
  84. }