tvPower.pl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. # Config
  10. my $TIMEOUT = 900;
  11. my $COUNTDOWN = 119;
  12. my $OFF_DELAY = 15;
  13. my $CMD_DELAY = 10;
  14. # App config
  15. my $DATA_DIR = DMX::dataDir();
  16. my $STATE_SOCK = 'TV_POWER';
  17. my $PROJ_SOCK = 'TV';
  18. my $OUTPUT_FILE = $DATA_DIR . $STATE_SOCK;
  19. my $PUSH_TIMEOUT = 20;
  20. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  21. my $DELAY = $PULL_TIMEOUT / 2;
  22. # Prototypes
  23. sub sayShutdown($);
  24. # Debug
  25. my $DEBUG = 0;
  26. if ($ENV{'DEBUG'}) {
  27. $DEBUG = 1;
  28. }
  29. # Sockets
  30. DMX::stateSocket($STATE_SOCK);
  31. DMX::stateSubscribe($STATE_SOCK);
  32. my $proj = DMX::clientSock($PROJ_SOCK);
  33. # State
  34. my $state = 'OFF';
  35. my $stateLast = $state;
  36. my %exists = ();
  37. my %mtime = ();
  38. my $pushLast = 0;
  39. my $pullLast = time();
  40. my $update = 0;
  41. my $lastAnnounce = 0;
  42. my $lastUser = time();
  43. my $shutdown = 0;
  44. my $life = 0;
  45. my $lastPlay = 0;
  46. # Loop forever
  47. while (1) {
  48. # State is calculated; use newState to gather data
  49. my $newState = $state;
  50. # Wait for state updates
  51. my $cmdState = DMX::readState($DELAY, \%exists, \%mtime, undef());
  52. # Avoid repeated calls to time()
  53. my $now = time();
  54. # Record only valid states
  55. if (defined($cmdState)) {
  56. $newState = $cmdState;
  57. $pullLast = $now;
  58. }
  59. # Die if we don't see regular updates
  60. if ($now - $pullLast > $PULL_TIMEOUT) {
  61. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  62. }
  63. # Menu changes count as activity
  64. if ($mtime{'GUI'} && $lastUser < $mtime{'GUI'}) {
  65. $lastUser = $mtime{'GUI'};
  66. }
  67. # TV starts count as activity
  68. if ($exists{'TV'} && $lastUser < $mtime{'TV'}) {
  69. $lastUser = $mtime{'TV'};
  70. } elsif ($newState eq 'ON') {
  71. $lastUser = $now;
  72. }
  73. # Playing counts as activity
  74. if ($exists{'PLAYING'} && $lastUser < $now) {
  75. $lastUser = $now;
  76. }
  77. # Clear the shutdown timestamp if there is new user activity
  78. if ($shutdown && (!$exists{'TV'} || $lastUser > $shutdown)) {
  79. $shutdown = 0;
  80. }
  81. # Record the shutdown timestamp, if the TV is on
  82. if ($newState eq 'SHUTDOWN' && $exists{'TV'}) {
  83. if ($DEBUG) {
  84. print STDERR "Recorded shutdown timestamp\n";
  85. }
  86. $shutdown = $now;
  87. $lastUser = $now;
  88. }
  89. # Calculate the elapsed time
  90. my $elapsed = $now - $lastUser;
  91. if ($DEBUG) {
  92. print STDERR 'Time since last user action: ' . $elapsed . "\n";
  93. }
  94. # Calculate the new state
  95. $stateLast = $state;
  96. if ($exists{'TV'}) {
  97. if ($shutdown) {
  98. $state = 'SHUTDOWN';
  99. if ($elapsed > $OFF_DELAY) {
  100. $state = 'OFF';
  101. }
  102. } else {
  103. if ($elapsed > $TIMEOUT) {
  104. $state = 'COUNTDOWN';
  105. if ($elapsed > $TIMEOUT + $COUNTDOWN) {
  106. $state = 'OFF';
  107. }
  108. } else {
  109. $state = 'ON';
  110. }
  111. }
  112. } else {
  113. if ($newState eq 'ON') {
  114. $state = 'ON';
  115. }
  116. }
  117. # Force updates on a periodic basis
  118. if (!$update && $now - $pushLast > $PUSH_TIMEOUT) {
  119. # Not for the TV
  120. #if ($DEBUG) {
  121. # print STDERR "Forcing periodic update\n";
  122. #}
  123. #$update = 1;
  124. }
  125. # Force updates on any state change
  126. if (!$update && $stateLast ne $state) {
  127. if ($DEBUG) {
  128. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  129. }
  130. $update = 1;
  131. }
  132. # Force updates when there is a physical state mistmatch
  133. if (!$update && $state eq 'OFF' && $exists{'TV'}) {
  134. if ($DEBUG) {
  135. print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'TV'} . "\n";
  136. }
  137. $update = 1;
  138. }
  139. # Announce a pending shutdown
  140. if ($state eq 'COUNTDOWN' || $state eq 'SHUTDOWN') {
  141. my $timeLeft = 0;
  142. if ($state eq 'SHUTDOWN') {
  143. $timeLeft = $OFF_DELAY - $elapsed;
  144. } else {
  145. $timeLeft = ($TIMEOUT + $COUNTDOWN) - $elapsed;
  146. }
  147. sayShutdown($timeLeft / 60);
  148. }
  149. # Only allow updates to "ON" or "OFF" -- the TV knows no other states
  150. if ($update && ($state ne 'ON' && $state ne 'OFF')) {
  151. if ($DEBUG) {
  152. print STDERR 'Ignoring update state other than ON/OFF: ' . $state . "\n";
  153. }
  154. $update = 0;
  155. }
  156. # Only allow updates every few seconds -- the TV goes dumb during power state changes
  157. if ($update && $now < $pushLast + $CMD_DELAY) {
  158. if ($DEBUG) {
  159. print STDERR 'Ignoring overrate update: ' . $state . "\n";
  160. }
  161. $update = 0;
  162. }
  163. # Update the TV
  164. if ($update) {
  165. # Extra debugging to record pushes
  166. if ($DEBUG) {
  167. print STDERR 'State: ' . $state . "\n";
  168. }
  169. # Send master power state
  170. $proj->send($state)
  171. or die('Unable to write command to proj socket: ' . $state . ": ${!}\n");
  172. # Annouce the state change, after the fact
  173. DMX::say('TV ' . $state);
  174. # No output file
  175. # Update the push time
  176. $pushLast = $now;
  177. # Clear the lastAnnounce timer
  178. $lastAnnounce = 0;
  179. # Clear the update flag
  180. $update = 0;
  181. }
  182. }
  183. sub sayShutdown($) {
  184. my ($minutesLeft) = @_;
  185. # Only allow annoucements once per minute
  186. my $now = time();
  187. if ($now < $lastAnnounce + 60) {
  188. return;
  189. }
  190. $lastAnnounce = $now;
  191. # Determine the unit
  192. my $unit = 'minute';
  193. my $timeLeft = $minutesLeft;
  194. if ($minutesLeft < 1) {
  195. $timeLeft = $minutesLeft * 60;
  196. $unit = 'second';
  197. }
  198. # Avoid saying "0" unless we *really* mean it
  199. $timeLeft = ceil($timeLeft);
  200. # Add an "s" as needed
  201. my $plural = 's';
  202. if ($timeLeft == 1) {
  203. $plural = '';
  204. }
  205. $unit .= $plural;
  206. # Speak
  207. DMX::say('TV shutdown in about ' . $timeLeft . ' ' . $unit);
  208. }