|
#!/bin/bash |
|
|
|
HOST="$REOLINK_NVR_HOST" # Your NVR or Camera IP Address |
|
USER="$REOLINK_NVR_USER" # Your username |
|
PASS="$REOLINK_NVR_PASS" # Your password |
|
# Set this to true and the script will dump the request/response |
|
# payloads to stderr. |
|
DEBUG=false |
|
|
|
### END OF CONFIGURATION ### |
|
|
|
URL="https://$HOST/cgi-bin/api.cgi" |
|
|
|
# TOKEN must initially be set to null, so that it gets passed to the |
|
# login command as `?cmd=Login&token=null` |
|
TOKEN="null" |
|
|
|
# Takes an API command as the first argument, and JSON-ish payload as |
|
# an optional second argument. If a payload is provided it's |
|
# processed with `jq -n` to make it easier (jq -n doesn't require |
|
# property names to be quoted, you can have trailing commas and other |
|
# stuff that isn't actually valid JSON) |
|
rl-api() { |
|
local CMD="$1" PARAM='{}' |
|
if [ -n "$2" ]; then PARAM="$(jq -n "$2")"; fi |
|
local REQ="$( |
|
jq -n --arg CMD "$CMD" --argjson PARAM "$PARAM" '{ |
|
cmd: $CMD, |
|
action: 0, |
|
param: $PARAM, |
|
}' |
|
)" |
|
local TGT="$URL?cmd=$CMD&token=$TOKEN" |
|
if $DEBUG; then |
|
echo ">>> REQUEST >>>" 1>&2 |
|
echo "TARGET: $TGT" 1>&2 |
|
jq -C . <<<"$REQ" 1>&2 |
|
fi |
|
local RES="$( |
|
curl -kfsSLH 'Content-Type: application/json' -d "[$REQ]" -XPOST "$TGT" | |
|
jq '.[0]' |
|
)" |
|
if $DEBUG; then |
|
echo "<<< RESPONSE <<<" 1>&2 |
|
jq -C . <<<"$RES" 1>&2 |
|
fi |
|
# If the response had "code: 0" then it was successful, otherwise it |
|
# was an error |
|
if [ "$(jq -r '.code' <<<"$RES")" -eq "0" ]; then |
|
jq '.value' <<<"$RES" |
|
return 0 |
|
else |
|
echo -n "$CMD ERROR: " 1>&2 |
|
jq -r '"\(.error.detail) (\(.error.rspCode))"' <<< "$RES" 1>&2 |
|
return 1 |
|
fi |
|
} |
|
# Send a Login command to the API |
|
rl-login() { |
|
rl-api Login "$( |
|
jq -n --arg USER "$USER" --arg PASS "$PASS" '{ |
|
User: { userName: $USER, password: $PASS } |
|
}' |
|
)" | jq -r '.Token.name' |
|
} |
|
# Send a Logout command to the API |
|
rl-logout() { |
|
if [ "$TOKEN" = "null" ] || [ "$TOKEN" = "" ]; then return; fi |
|
rl-api Logout > /dev/null |
|
} |
|
|
|
# Login with username and password and get a session token |
|
TOKEN="$(rl-login)" |
|
if [ -z "$TOKEN" ]; then exit 1; fi |
|
|
|
# Now that we have a token, we add an exit hook to remove it when the |
|
# script exits, if you leave it around you may get the dreaded (and |
|
# annoying) "max session" error. If that happens all you can really |
|
# do is wait, by default the tokens are good for an hour (and the |
|
# session limit is global, so using multiple usernames won't help) |
|
trap 'rl-logout' EXIT |
|
|
|
# Process any arguments on the command line as commands, if the |
|
# command is followed by something that looks like a payload, then |
|
# pass that as the payload to the command. |
|
while (( $# )); do |
|
CMD="$1" ; shift |
|
#if (( $# )) && jq -eR 'try(fromjson)' <<<"$1"; then |
|
if (( $# )) && [[ $1 == *[{}]* ]]; then |
|
PAYLOAD="$1" ; shift |
|
else |
|
PAYLOAD="{}" |
|
fi |
|
rl-api "$CMD" "$PAYLOAD" || exit 1 |
|
done |
How to set differents detection zone for the PTZ Cameras (AI and NON-AI)
Like the method for the privacy masks in the post bellow, here the commands (depends on cameras types) to generate multiple detection zones on the preset you choose.
Here the method, with @jasonk script, how you can apply a new detection zone after moving to a preset (without losing the others detection zones) :
Example : I have 2 presets (id1: -preset garage-, id2 : -preset entrance-)
FOR CAMERAS NON AI (like E1 Zoom)
For the first one which your have defined a detection zone (in my example id:1 -garage-), launch the command bellow and save the result in a file called, for example, json_detectionzone_id1 :
./rl-api GetMdAlarm '{"channel":0}' | jq '.[]|={channel,scope}' -c > json_detectionzone_id1
Switch to the second preset -id:2- and redefined a new detection zone (here the entrance), apply it, and and also launch the command to saved the result in a second file called json_detectionzone_id2:
./rl-api GetMdAlarm '{"channel":0}' | jq '.[]|={channel,scope}' -c > json_detectionzone_id2
After that, when you trigger PTZ Preset by id with the command (taken for @pixeldoc2000) and in my case the id:2 entrance-
./rl-api PtzCtrl '{"channel":0, "op":"ToPos", "id":2, "speed":32}
launch, after that previous command, the command dedicated to apply the detectionzone file for id:2 :
./rl-api SetMdAlarm $(cat json_detectionzone_id2)
NB : all the files json_detectionzone_idX are located in the same repository of the rl-api.sh executable.
FOR CAMERAS AI (without PetDetection) : based on the same example
For the first one which your have defined a detection zone (in my example id:1 -garage-), launch the command bellow and save the result in a file called, for example, json_detectionzoneAI_id1 :
./rl-api GetAiAlarm '{"channel":0,"ai_type":"people"}' | jq '{channel:.[].channel,md:.[].scope,people:.[].scope,vehicle:.[].scope}' -c > json_detectionzoneAI_id1
Switch to the second preset -id:2- and redefined a new detection zone (here the entrance), apply it, and and also launch the command to saved the result in a second file called json_detectionzoneAI_id2:
./rl-api GetAiAlarm '{"channel":0,"ai_type":"people"}' | jq '{channel:.[].channel,md:.[].scope,people:.[].scope,vehicle:.[].scope}' -c > json_detectionzoneAI_id2
After that, when you trigger PTZ Preset by id with the command (taken for @pixeldoc2000) and in my case the id:2 entrance-
./rl-api PtzCtrl '{"channel":0, "op":"ToPos", "id":2, "speed":32}
launch, after that previous command, the command dedicated to apply the json_detectionzoneAI file for id:2 :
./rl-api SetAlarmArea $(cat json_detectionzoneAI_id2)
NB : all the files json_detectionzoneAI_idX are located in the same repository of the rl-api.sh executable.
FOR CAMERAS AI (with PetDetection) : based on the same example
For the first one which your have defined a detection zone (in my example id:1 -garage-), launch the command bellow and save the result in a file called, for example, json_detectionzoneAI_pet_id1 :
./rl-api GetAiAlarm '{"channel":0,"ai_type":"people"}' | jq '{channel:.[].channel,md:.[].scope,people:.[].scope,vehicle:.[].scope,dog_cat:.[].scope}' -c > json_detectionzoneAI_pet_id1
Switch to the second preset -id:2- and redefined a new detection zone, apply it, and and also launch the command to saved the result in a second file called json_detectionzoneAI_pet_id2:
./rl-api GetAiAlarm '{"channel":0,"ai_type":"people"}' | jq '{channel:.[].channel,md:.[].scope,people:.[].scope,vehicle:.[].scope,dog_cat:.[].scope}' -c > json_detectionzoneAI_pet_id2
After that, when you trigger PTZ Preset by id with the command (taken for @pixeldoc2000) and in my case the id:2 -entrance-
./rl-api PtzCtrl '{"channel":0, "op":"ToPos", "id":2, "speed":32}
launch, after that previous command, the command dedicated to apply the json_detectionzoneAI_pet file for id:2 :
./rl-api SetAlarmArea $(cat json_detectionzoneAI_pet_id2)
NB : all the files json_detectionzoneAI_pet_idX are located in the same repository of the rl-api.sh executable.