Last active
April 18, 2017 04:33
-
-
Save JayGoldberg/a24b6a2e91c83d2a96bc to your computer and use it in GitHub Desktop.
Watch an IQeye camera HTTP stream for motion events and echo to the console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## @author Jay Goldberg | |
## @email jaymgoldberg@gmail.com | |
## @license Apache 2.0 | |
## @description Watch an IQeye camera HTTP stream for motion events and echo to the console | |
## @usage camwatch.sh <list of IQeye camera URLS> | |
#======================================================================= | |
# http://stackoverflow.com/questions/10862970/inter-process-communication-without-fifos | |
urlfile='cams.txt' | |
fifo='eventstream.txt' | |
# open bidrectional TCP to a bidirectional file descriptor | |
open_tcp() { | |
fd=$1 | |
hostport=$2 | |
exec ${fd}<> /dev/tcp/${hostport[0]}/${hostport[1]} | |
} | |
# generate data, output to STDOUT | |
camera_worker() { | |
# TODO local vars | |
fd_tcp=$1 | |
grep "Pragma:" <&${fd_tcp} | |
} | |
# read data from an fd, output to STDOUT | |
reader_worker() { | |
# TODO local vars | |
fd=$1 | |
hostport=$2 | |
fifo=$3 | |
while read status | |
do | |
if [[ $status == "motion" ]] | |
then | |
action $hostport $fifo | |
fi | |
done <&${fd} | |
} | |
start_stream() { | |
# TODO local vars | |
fd_tcp=$1 | |
printf "GET /now.jpg?snap=spush0.1&pragma=motion&noimage HTTP/1.0\r\nConnection: Keep-Alive\r\n\r\n" >&${fd_tcp} | |
} | |
action() { | |
# perform an action when motion detectedi | |
hostport=$1 | |
echo "action occurred on http://${hostport[0]}:${hostport[2]}/serverpush.html" | |
} | |
main() { | |
mkfifo "$fifo" | |
# start with a sane fd | |
fd=5 | |
while read url; do | |
# parse the URL | |
strip_http=${url#*http\:\/\/} | |
strip_path=${strip_http%\/**} | |
if [[ $(echo $strip_path| grep ':') ]]; then | |
port=${strip_path#*:} | |
host=${strip_http%:*} | |
else | |
port=80 | |
host=$strip_path | |
fi | |
hostport=( "$host" "$port" ) | |
echo ${hostport[*]} | |
echo $fd | |
# start the tcp worker on the specfied bidirectional fd | |
open_tcp $fd $hostport | |
fd_out=$(( fd + 1 )) | |
# feed motion status for this camera into a new fd | |
exec $fd_out< <(camera_worker $fd) | |
start_stream $fd | |
reader_worker $fd_out $hostport $fifo | |
fd=$((fd + 2 )) | |
done < "$1" | |
rm $fifo | |
} | |
input="${1:-'cams.txt'}" | |
main "$input" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment