-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathperpetual-motion
executable file
·262 lines (211 loc) · 6.31 KB
/
perpetual-motion
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/perl
use strict;
use warnings;
use FindBin qw($Bin $Script);
use Fcntl qw(:flock SEEK_END);
use File::Path;
use lib $Bin;
use Cammy;
my ($camName, $cmd) = @ARGV;
die "$Script <camera> (start|softstart|stop|status|test|auto)?" unless $camName && $cmd;
init("$ENV{HOME}/.dont-be-a-dick.config");
if ($camName eq 'all') {
my $err = 0;
for my $cam (cfgCameraNames) {
$err ++ if system("$Bin/$Script", $cam, $cmd);
}
exit 0;
}
my $motion = cfgMotion;
my $ram = cfgRAM;
my $disk = cfgLocalStorage;
my $camCfg = cfgCamera($camName) or die "Invalid camera: $camName";
my $tmpDir = "$ram/motion/$camName";
my $motionCfg = "$tmpDir/motion.cfg";
my $pidFile = "$tmpDir/motion.pid";
mkpath "$tmpDir/img";
my $currentPassphraseFile = "$ram/current-passphrase";
my $PIDFILE = "$tmpDir/$Script.pid";
sub daemonIsRunning() {
return 0 unless -f $PIDFILE;
# Note: The + requests read/write access, this is to make it possible to grab the
# exclusive lock on Solaris OSX and Linux both follow the original BSD semantics,
# so they work fine with pure read-access to the file.
open PPID, "+<$PIDFILE" or return 0;
my $pid = <PPID>;
if ( flock( PPID, LOCK_EX | LOCK_NB ) ) {
$pid = 0; # If we can get the lock the daemon has died.
flock( PPID, LOCK_UN );
}
close PPID;
unlink $PIDFILE unless $pid;
return $pid;
}
sub catchZap {
my $sig = shift;
print "Caught SIG$sig, but staying alive anyway.\n";
}
my $pid = daemonIsRunning();
if ($cmd eq 'stop') {
print STDERR "exiting\n" unless $pid;
exit 0 unless $pid;
print STDERR "Shutting down $Script" unless $ENV{BQ};
kill 9, $pid;
while ( daemonIsRunning() ) {
sleep 1;
print STDERR ".";
}
print STDERR " Done\n" unless $ENV{BQ};
unlink $PIDFILE;
exit 0;
} elsif ( $cmd eq 'status' ) {
if ($pid) {
print STDERR "$Script for $camName is running with process id: $pid\n" unless $ENV{BQ};
exit 0;
} else {
print STDERR "$Script for $camName is not running\n" unless $ENV{BQ};
exit 1;
}
} elsif ( $cmd eq 'restart' ) {
system("$Bin/$Script stop");
system("$Bin/$Script start") and die "Failed while starting";
} elsif ($cmd ne 'start' and $cmd ne 'softstart' and $cmd ne 'test') {
die "Syntax: $Script (start|stop|restart|status) (not: $cmd)";
}
if ($cmd ne 'test') {
$SIG{HUP} = $SIG{INT} = \&catchZap;
if ($cmd eq 'softstart') {
exit 0 if $pid;
} else {
die "Error: Process is already running with pid: $pid." if $pid;
}
# Double fork idiom: avoids leaving a zombie around.
if (fork) {
print STDERR "$Script has been started see log file for further info\n" unless $ENV{BQ};
exit 0;
}
exit if fork;
open PPID, "+>$PIDFILE" or die "Unable to write pid file $PIDFILE: $!";
seek PPID, 0, 0;
print PPID "$$\n";
flock(PPID, LOCK_EX) or die "Unable to lock the pid file $PIDFILE";
my $log = "$tmpDir/$Script.log";
print "Output will continue in $log\n";
rename $log, "$log.old" if -f $log;
open( STDOUT, ">>$log" ) or die "Unable to write to $log: $!";
open( STDERR, ">>$log" ) or die "Unable to redirect stderr to $log: $!";
close STDIN;
$| = 1;
}
my $maxAge = 10;
while (1) {
my $mtime = (stat $pidFile)[9];
my $ok = 1;
my $age = time;
if ($mtime) {
open PID, "<$pidFile" or die "Failed to read $pidFile: $!";
my $pid = <PID>;
close PID;
chomp $pid if $pid;
if (!$pid or !-d "/proc/$pid") {
$ok = 0;
} else {
$age -= $mtime;
#print STDERR "$age/$maxAge\n";
if ($age > $maxAge) {
print STDERR "$camName motion hung, killing it until it's dead\n";
my $patience = 20;
while ($patience-- and kill 'QUIT', $pid) { # Please go away
print STDERR "quit ";
sleep 1;
}
while (kill 'KILL', $pid) { # DIAF
print STDERR "kill ";
sleep 1;
}
print STDERR "Killed $pid\n";
unlink $pidFile;
$ok = 0;
} else {
# Time is ok
}
}
} else {
$ok = 0; # The pid file doesn't exist
}
if ($ok) {
sleep 1; # The daemon is running, everything is fine, don't burn all the CPU looking for it.
$maxAge = 20 if $age < 2;
next;
}
# If we get this far, then the daemon has stopped and needs to be restarted.
print STDERR "Motion isn't running, starting it...\n";
if (!-f $currentPassphraseFile) {
system("$Bin/rekey") and die "Failed to create the missing key";
die "Failed to create the missing key: $currentPassphraseFile"
unless -f $currentPassphraseFile;
}
open P, "<$currentPassphraseFile" or die "Failed to read current passphrase from $currentPassphraseFile: $!";
flock(P, LOCK_SH);
open T, "<$ram/key-time" or die "Failed to read key-time: $!";
my $keyTime = <T>;
close T;
close P;
my $logDir = "$disk/$keyTime/$camName";
mkpath $logDir unless -d $logDir;
my %cfg = (
daemon=>'on',
process_id_file =>$pidFile,
logfile =>"$logDir/motion.log",
rotate => 0,
framerate => 2,
minimum_frame_time => 1,
threshold=> 5000,
threshold_tune => 'off',
noise_level => 32,
noise_tune => 'on',
despeckle_filter => 'EedDl',
minimum_motion_frames =>1,
pre_capture =>2,
post_capture => 2,
event_gap => 60,
output_pictures => 'on',
snapshot_interval => 60,
text_left=>$camName,
text_double =>'on',
target_dir => "$tmpDir/img",
snapshot_filename => "%Y%m%d%H%M%S-snapshot",
picture_filename => "%Y%m%d%H%M%S-%q",
on_picture_save => "$Bin/encrypt '$camName' %f",
);
for my $k (keys %$camCfg) {
if ($k eq 'size') {
my $s = $camCfg->{$k};
if ($s eq '720p') {
$s = "1280x720";
} elsif ($s eq '1080p') {
$s = "1920x1080";
}
($cfg{width}, $cfg{height}) = split /x/, $s;
} else {
$cfg{$k} = $camCfg->{$k};
}
}
print STDERR "Calculated configuration for $camName:\n";
open C, ">$motionCfg" or die "Failed to write $motionCfg: $!";
for my $k (sort keys %cfg) {
print STDERR "$k=$cfg{$k}\n";
print C "$k=$cfg{$k}\n";
}
close C;
# Daemon not running.
system("$motion/motion", "-c", $motionCfg) and die "Failed to start motion with $motionCfg";
print STDERR "Waiting for pid file $pidFile ";
while (!-f $pidFile) {
print STDERR ".";
sleep 1;
}
print STDERR "Ok\n";
sleep 3;
$maxAge = 60;
}