Track dooropen ajar/shut state using a magnetic sensor, log verbosely for now

This commit is contained in:
Petr Baudis 2011-10-19 00:55:46 +02:00
parent 8a9949e15f
commit f5e762c8da
2 changed files with 43 additions and 5 deletions

View file

@ -12,7 +12,8 @@ our $channel = "#brmlab";
our $streamurl = "http://brmlab.cz/stream";
our $devdoor = $ARGV[0]; $devdoor ||= "/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A700e1qB-if00-port0";
our $devasign = $ARGV[1]; $devasign ||= "/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0";
our ($status, $streaming, $topic) = (0, 0, 'BRMLAB OPEN');
our ($status, $streaming, $dooropen, $topic) = (0, 0, 0, 'BRMLAB OPEN');
our $lastunlock = 0;
my $irc = brmd::IRC->new();
my $web = brmd::WWW->new();
@ -25,7 +26,7 @@ my $alphasign = brmd::Alphasign->new();
POE::Session->create(
package_states => [
main => [ qw(_default _start
status_update streaming_update
status_update streaming_update dooropen_update
notify_door_unlocked notify_door_unauth) ],
],
heap => { irc => $irc, web => $web, door => $door, stream => $stream, log => $log, alphasign => $alphasign },
@ -58,6 +59,10 @@ sub status_str {
$status ? 'OPEN' : 'CLOSED';
}
sub dooropen_str {
$dooropen ? 'AJAR' : 'SHUT';
}
sub streaming_str {
$streaming ? 'ON AIR' : 'OFF AIR';
}
@ -87,11 +92,20 @@ sub streaming_update {
$poe_kernel->post( $irc, 'notify_update', 'brmvideo', $st, $streaming ? $streamurl : undef );
}
sub dooropen_update {
my ($self, $newdooropen) = @_[OBJECT, ARG0];
$dooropen = $newdooropen;
my $st = dooropen_str();
$poe_kernel->post( $irc, 'notify_door_open', $st );
}
sub notify_door_unlocked {
my ($self, $nick) = @_[OBJECT, ARG0];
$poe_kernel->post($irc, 'notify_door_unlocked');
$poe_kernel->post($log, 'notify_door_unlocked', $nick);
$lastunlock = time;
}
sub notify_door_unauth {
@ -180,8 +194,8 @@ sub serial_open {
sub serial_input {
my ($self, $input) = @_[OBJECT, ARG0];
print ((scalar localtime)." $input\n");
$input =~ /^(\d) (\d) (.*)$/ or return;
my ($cur_status, $cur_streaming, $brm) = ($1, $2, $3);
$input =~ /^(\d) (\d) (\d) (.*)$/ or return;
my ($cur_status, $cur_streaming, $cur_dooropen, $brm) = ($1, $2, $3, $4);
if ($cur_status != $status) {
foreach (@{$self->{observers}}) {
$poe_kernel->post($_, 'status_update', $cur_status);
@ -204,6 +218,11 @@ sub serial_input {
}
}
}
if ($cur_dooropen != $dooropen) {
foreach (@{$self->{observers}}) {
$poe_kernel->post($_, 'dooropen_update', $cur_dooropen);
}
}
}
sub serial_error {
@ -511,7 +530,7 @@ sub new {
$self => [ qw(_start _default
irc_001 irc_public irc_332 irc_topic
notify_update
notify_door_unauth notify_door_unlocked) ],
notify_door_unauth notify_door_unlocked notify_door_open) ],
],
heap => { irc => $irc, connector => $connector },
);
@ -661,6 +680,19 @@ sub notify_door_unlocked {
$irc->yield (privmsg => $channel => $msg );
}
sub notify_door_open {
my ($sender, $newstate) = @_[SENDER, ARG0];
my $irc = $_[HEAP]->{irc};
my $msg;
my $unlock_timeout = 30;
if ($dooropen == 1 and $status == 0 and time - $lastunlock >= $unlock_timeout) {
$msg = "[door] $newstate \002(alert: closed brmlab, door opened, unlocked before more than $unlock_timeout seconds)";
} else {
$msg = "[door] $newstate";
}
$irc->yield (privmsg => $channel => $msg );
}
1;

View file

@ -5,6 +5,7 @@
#include "NewSoftSerial.h"
// pins
const int magnetPin = 10;
const int soundPin = 9; /* piezo in series with 100R */
const int statusLed = 8;
const int statusBtn = 7;
@ -167,6 +168,8 @@ void readSerial()
void setup()
{
pinMode(magnetPin, INPUT);
digitalWrite(magnetPin, HIGH);
pinMode(doorLock, OUTPUT);
pinMode(soundPin, OUTPUT);
pinMode(statusLed, OUTPUT);
@ -190,11 +193,14 @@ void loop()
/* Update state based on buttons and override. */
if (!statusStateOverride) statusState = statusStateNew;
if (!videoStateOverride) videoState = videoStateNew;
int doorOpen = digitalRead(magnetPin);
digitalWrite(statusLed, !statusState); // will be turned back in readCard()
digitalWrite(videoLed, videoState);
comSerial.print(statusState, DEC); comSerial.write(" ");
comSerial.print(videoState, DEC); comSerial.write(" ");
comSerial.print(doorOpen, DEC); comSerial.write(" ");
readCard();
readSerial();
}