-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbodgery_open_cam_daemon.pl
executable file
·129 lines (109 loc) · 3.55 KB
/
bodgery_open_cam_daemon.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/perl
use v5.14;
use warnings;
use Device::WebIO;
use Device::WebIO::RaspberryPi;
use AnyEvent;
use File::Temp 'tempfile';
use Proc::Daemon;
use Imager;
use constant DEBUG => 0;
use constant INPUT_PIN => 17;
use constant PICTURE_INTERVAL_SEC => 1 * 60;
use constant IMG_WIDTH => 800;
use constant IMG_HEIGHT => 600;
use constant IMG_QUALITY => 100;
use constant IMG_IMAGER_QUALITY => 70;
use constant DEFAULT_PIC => 'bodgery_default.jpg';
use constant PRIVATE_KEY_FILE => 'upload_key.rsa';
use constant SERVER_USERNAME => '';
use constant SERVER_HOST => ''; # Fill in hostname or IP
use constant SERVER_UPLOAD_PATH => ''; # Fill in upload path on server
use constant DAEMON_WORKDIR => '/path/to/workdir';
use constant DAEMON_UID => 0;
#use constant DAEMON_LOG => '/path/to/log';
use constant TMP_DIR => '/var/tmp-ramdisk';
use constant FLIP_IMAGE => 1; # Set if your camera is upside-down
my ($INPUT, $LAST_INPUT) = (0, 0);
my $rpi = Device::WebIO::RaspberryPi->new;
$rpi->set_as_input( INPUT_PIN );
$rpi->img_set_width( 0, IMG_WIDTH );
$rpi->img_set_height( 0, IMG_HEIGHT );
$rpi->img_set_quality( 0, IMG_QUALITY );
#my $daemon = Proc::Daemon->new(
# workdir => DAEMON_WORKDIR,
# setuid => DEAMON_UID,
# child_STDOUT => DAEMON_LOG,
# child_STDERR => DAEMON_LOG,
#);
#my $pid = $daemon->Init;
#if( $pid ) {
# say "Forked daemon in process $pid, exiting . . . \n" if DEBUG;
# exit 0;
#}
my $condvar = AnyEvent->condvar;
my $input_timer; $input_timer = AnyEvent->timer(
after => 1,
interval => 1,
cb => sub {
$INPUT = $rpi->input_pin( INPUT_PIN );
say "Input: [$INPUT]" if DEBUG;
$input_timer;
},
);
my $take_picture_timer; $take_picture_timer = AnyEvent->timer(
after => 1,
interval => PICTURE_INTERVAL_SEC,
cb => sub {
if( $INPUT ) {
say "Sending live image" if DEBUG;
my $fh = $rpi->img_stream( 0, 'image/jpeg' );
my ($tmp_fh, $tmp_filename) = tempfile( DIR => TMP_DIR );
my $buffer = '';
while( read( $fh, $buffer, 4096 ) ) {
print $tmp_fh $buffer;
}
close $tmp_fh;
close $fh;
my $img = Imager->new;
$img->read(
file => $tmp_filename,
) or die "Can't open '$tmp_filename': " . $img->errstr;
$img = $img->flip( dir => 'vh' ) if FLIP_IMAGE;
$img->write(
file => $tmp_filename,
type => 'jpeg',
jpegquality => IMG_IMAGER_QUALITY,
) or die "Can't write file: " . $img->errstr;
send_pic( $tmp_filename );
unlink $tmp_filename;
}
else {
if( $INPUT != $LAST_INPUT ) {
say "Sending default pic" if DEBUG;
send_pic( DEFAULT_PIC );
}
else {
say "No need to send default pic" if DEBUG;
}
}
$LAST_INPUT = $INPUT;
$take_picture_timer;
},
);
say "Taking input . . . " if DEBUG;
$condvar->recv;
sub send_pic
{
my ($filename) = @_;
my @scp_command = (
'scp',
'-i', PRIVATE_KEY_FILE,
$filename,
SERVER_USERNAME . '@' . SERVER_HOST . ':' . SERVER_UPLOAD_PATH,
);
say "Executing: @scp_command" if DEBUG;
(system( @scp_command ) == 0)
or warn "Could not exec '@scp_command': $!\n";
return 1;
}