Last active
April 15, 2021 11:40
-
-
Save af3556/5553b43037e2fcaacfa8b82a2e78e639 to your computer and use it in GitHub Desktop.
This file contains hidden or 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/sh | |
# elementary PTZ control of a Reolink PTZ camera | |
# example usage: move to position 1 and take a photo: | |
# ./camctrl.sh -i 192.168.1.99 -p secret -c TOPOS -m 1 -f snap.jpg | |
# see also https://github.com/fwestenberg/reolink_dev | |
# alternative approach via ONVIF | |
# https://github.com/ZoneMinder/zoneminder/blob/master/scripts/ZoneMinder/lib/ZoneMinder/Control/Reolink.pm | |
user='admin' | |
param_op='STOP' | |
param_id=1 | |
param_speed=32 | |
while getopts ":i:u:p:c:s:m:f:" opt; do | |
case "${opt}" in | |
i) ip="$OPTARG" ;; | |
u) user="$OPTARG" ;; | |
p) pass="$OPTARG" ;; | |
c) param_op="$OPTARG" ;; | |
s) param_speed="$OPTARG" ;; | |
m) param_id="$OPTARG" ;; | |
f) filename="$OPTARG" ;; | |
*) echo "Usage: $0 -i ip -u username -p password -c command [-s speed] [-m position id] [-f filename]" | |
exit 1 | |
;; | |
esac | |
done | |
shift $((OPTIND -1)) | |
export user pass param_op param_speed param_id | |
_docmd() { | |
result=$(curl --silent "${url}" --data "$(jq --null-input "${cmd}")") | |
if ! code=$(echo "${result}" | jq --raw-output '.[].code'); then | |
echo "invalid result from $url: $result" | |
exit 1 | |
fi | |
case "${code}" in | |
0) ;; | |
1) echo "${result}" | jq --raw-output ; exit 1 ;; | |
esac | |
} | |
login() { | |
url="http://${ip}/cgi-bin/api.cgi?cmd=Login&token=null" | |
cmd=' | |
[ | |
{ | |
"cmd": "Login", | |
"action": 0, | |
"param": { | |
"User": { | |
"userName": env.user, | |
"password": env.pass | |
} | |
} | |
} | |
] | |
' | |
_docmd | |
token=$(echo "${result}" | jq --raw-output '.[].value.Token.name') | |
} | |
logout() { | |
url="http://${ip}/cgi-bin/api.cgi?cmd=Logout&token=${token}" | |
cmd=' | |
[ | |
{ | |
"cmd": "Logout", | |
"action": 0, | |
"param": {} | |
} | |
] | |
' | |
_docmd | |
} | |
ptzctrl() { | |
url="http://${ip}/cgi-bin/api.cgi?cmd=PtzCtrl&token=${token}" | |
cmd=' | |
[ | |
{ | |
"cmd": "PtzCtrl", | |
"action": 0, | |
"param": { | |
"channel": 0, | |
"op": env.param_op, | |
"speed": env.param_speed | tonumber, | |
"id": env.param_id | tonumber | |
} | |
} | |
] | |
' | |
_docmd | |
} | |
snap() { | |
url="http://${ip}/cgi-bin/api.cgi?cmd=Snap&channel=0&rs=y4vTe8zVqu_YFEMQ&token=${token}" | |
curl --silent "${url}" --output "$filename" | |
} | |
login | |
ptzctrl | |
if [ -n "${filename+x}" ]; then | |
sleep 10 # wait for the camera to finish moving and focus | |
snap | |
fi | |
# camera can only handle small number of sessions so should logout to avoid | |
# login errors: "no more available connections" / "max sessions" | |
logout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment