announce.pl 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. # Debug
  17. my $DEBUG = 0;
  18. if ($ENV{'DEBUG'}) {
  19. $DEBUG = 1;
  20. }
  21. # State
  22. my %exists = ();
  23. my %last = ();
  24. my $pullLast = time();
  25. # Sockets
  26. DMX::stateSocket($STATE_SOCK);
  27. DMX::stateSubscribe($STATE_SOCK);
  28. # Loop forever
  29. while (1) {
  30. # State is calculated; use newState to gather data
  31. my $newState = $state;
  32. %last = %exists;
  33. # Wait for state updates
  34. my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
  35. if (defined($cmdState)) {
  36. $newState = $cmdState;
  37. $pullLast = time();
  38. }
  39. # Die if we don't see regular updates
  40. if (time() - $pullLast > $PULL_TIMEOUT) {
  41. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  42. }
  43. # Speak when BRIGHT changes
  44. if (exists($exists{'BRIGHT'}) && exists($last{'BRIGHT'}) && $exists{'BRIGHT'} ne $last{'BRIGHT'}) {
  45. if ($exists{'BRIGHT'}) {
  46. DMX::say('Lights - Full power');
  47. } else {
  48. DMX::say('Lights - Nominal power');
  49. }
  50. }
  51. # Speak when LIGHTS changes
  52. if (exists($exists{'LIGHTS'}) && exists($last{'LIGHTS'}) && $exists{'LIGHTS'} ne $last{'LIGHTS'}) {
  53. if ($exists{'LIGHTS'}) {
  54. DMX::say('Lights up');
  55. } else {
  56. DMX::say('Lights down');
  57. }
  58. }
  59. # Speak when NO_MOTION changes
  60. if (exists($exists{'NO_MOTION'}) && exists($last{'NO_MOTION'}) && $exists{'NO_MOTION'} ne $last{'NO_MOTION'}) {
  61. if ($exists{'NO_MOTION'}) {
  62. DMX::say('Motion detectors: Disabled');
  63. } else {
  64. DMX::say('Motion detectors: Enabled');
  65. }
  66. }
  67. }