-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.sh
executable file
·79 lines (68 loc) · 2.37 KB
/
controller.sh
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
#!/bin/bash
## CONFIGURATION
myStream="rtsp://68.152.51.100/axis-media/media.amp" # video stream
myFfmpeg="avconv" # ubuntu replaced ffmpeg with avconv
myProcessor="./theapp.bin" # image processor
myTolerance="10" # determines what angles contribute to image score
myThreshold="26000" # minimum score for inclusion
myTweeter="ruby tweet.rb" # what to do when successful
mySleep="60" # how long to sleep between image captures
myTimeout="10800" # seconds between allowable tweets
myCacheTime="10800" # number of seconds an image is kept
function main {
startTime=$(date +%s)
## INIT SOME DIRS AND VARS
if [[ ! -d tmp/ ]]; then
mkdir tmp/
fi
if [[ ! -d images/ ]]; then
mkdir images/
fi
if [[ ! -d log/ ]]; then
mkdir log/
fi
rollTime="0" # last time the tree was rolled (seconds since epoch)
state="unrolled" # state of the corner
## START LOGGING
log="log/${startTime}.log"
echo "Log $log created by controller.sh." >> $log
echo "Started logging at $(date)." >> $log
## START IMAGE SCRAPER
$myFfmpeg -rtsp_transport tcp -y -i "$myStream" -r 1/$mySleep tmp/image%03d.png &
echo "Image scraper $myFfmpeg started at $(date)." >> $log
sleep $(expr 3 \* $mySleep)
while true; do
## FIND AND SCORE THE LATEST IMAGE
friendlyTime=$(date)
currentTime=$(date +%s)
image=$(ls -1t tmp/* | head -1)
score=$($myProcessor $image $myTolerance)
if [[ $score -gt $myThreshold ]]; then
state="rolled"
else
state="unrolled"
fi
echo "At time $friendlyTime, image $image scored $score, interpreted as state $state." >> $log
## TO TWEET OR NOT TO TWEET
let timeout=currentTime-rollTime
if [[ $state == "rolled" ]]; then
if [[ $timeout -gt $myTimeout ]]; then
## IT'S AWAY!
$myTweeter $image &
echo "Tweeted! $friendlyTime - $image - $score - $state." >> $log
else
## NEGATIVE! IT DIDN'T GO IN.
echo "Image was $state, but timeout $timeout was too small. Not tweeting." >> $log
fi
## SOME BOOKKEEPING
rollTime=$currentTime
echo "Image was $state, so timeout was reset." >> $log
cp $image images/${currentTime}-${score}.png
echo "Image saved as images/${currentTime}-${score}.png." >> $log
fi
## DELETE FRAMES OLDER THAN myCacheTime AND SLEEP
find tmp/ -not -newermt "-$myCacheTime seconds" -delete
sleep $mySleep
done
}
main