Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: cc252a3feb
Fetching contributors…

Cannot retrieve contributors at this time

executable file 153 lines (132 sloc) 3.885 kb
#!/usr/bin/perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../lib";
use VHS;
use AnyEvent::Twitter::Stream;
use Fatal qw/open close/;
use Parallel::ForkManager;
my $arduino_file = '/dev/ttyUSB0';
my $done = AnyEvent->condvar;
my $forkmanager = Parallel::ForkManager->new(1);
open(my $arduino_rfh, $arduino_file);
binmode STDOUT, ":utf8";
$SIG{INT} = sub { print "Exiting... \n" };
my @temp_requests;
my $to_arduino_event = sub {
my $tweet = shift;
my $tweeter = $tweet->{user}{screen_name};
print "TWITTER: $tweeter: $tweet->{text}\n";
# Ignore our own tweets
return if $tweeter eq 'vhs45w';
if ($tweet->{text} =~ m/\btemp(erature)?\b/) {
print " Sending temperature request\n";
open(my $arduino_wfh, ">$arduino_file");
print $arduino_wfh "temperature\n";
close $arduino_wfh;
push @temp_requests, $tweeter;
}
};
my $streamer = AnyEvent::Twitter::Stream->new(
username => VHS->config->{twitter_username},
password => VHS->config->{twitter_password},
method => 'follow',
follow => '61112230',
on_tweet => $to_arduino_event,
on_error => sub {
my $error = shift;
warn "ERROR: $error";
$done->send;
},
on_eof => sub {
$done->send;
},
);
my $hdl = AnyEvent::Handle->new(
fh => $arduino_rfh,
on_error => sub {
my ($hdl, $fatal, $msg) = @_;
warn "Arduino Read filehandle: $msg\n";
$hdl->destroy;
$done->send;
},
on_read => sub {
my $handle = shift;
$handle->push_read(
line => sub {
my ($h, $line, $eol) = @_;
return unless length $line;
if ($line =~ s/^#//) {
warn "From arduino: $line\n";
return;
}
my @args = split m/\s+/, $line;
if ($line =~ m/temperature/ and scalar @temp_requests) {
my $user = shift @temp_requests;
if ($user eq 'STDIN') {
print "$line\n";
return;
}
push @args, $user;
}
run_command_from_arduino( @args );
},
);
},
);
my $stdin_hdl = AnyEvent::Handle->new(
fh => \*STDIN,
on_error => sub {
my ($hdl, $fatal, $msg) = @_;
warn "STDIN: $msg\n";
$hdl->destroy;
},
on_read => sub {
my $handle = shift;
$handle->push_read(
line => sub {
my ($h, $line, $eol) = @_;
return unless length $line;
if ($line eq 'quit') {
$done->send;
return;
}
my $tweet = {
text => $line,
user => { screen_name => 'STDIN' },
};
$to_arduino_event->($tweet);
},
);
},
);
$done->recv;
exit;
sub run_command_from_arduino {
my $command = shift;
my @args = @_;
my $now = localtime;
print "$now: Received command: $command args: ("
. join(', ', @args) . ")\n";
log_it("$command @args");
my $script_dir = "$FindBin::Bin/../hooks/${command}.d";
return unless -d $script_dir;
my @scripts = glob("$script_dir/*");
for my $script (@scripts) {
next unless -x $script;
my $pid = $forkmanager->start and next;
print "Running $script\n";
local $ENV{PERL5LIB} = "$FindBin::Bin/../lib";
system($script, @args);
$forkmanager->finish;
}
}
sub log_it {
my $log_string = shift;
my $log_file = "/var/log/vhs.log";
open(my $fh, ">>$log_file");
my $now = localtime;
print $fh "$now: $log_string\n";
close $fh;
}
Jump to Line
Something went wrong with that request. Please try again.