all: create $confdir if it does not exist

zmapi, cam: refactor, use main() so that bash finds trap_error/trap_clean
This commit is contained in:
Frank de Lange 2022-10-02 10:49:16 +00:00
parent 858b168b05
commit 5bb1b8cef2
3 changed files with 267 additions and 258 deletions

141
cam
View file

@ -2,9 +2,10 @@
shopt -s extglob shopt -s extglob
version="1.0.2" version="1.0.3"
release="20221001" release="20221001"
main () {
declare -A camera declare -A camera
username="admin" username="admin"
@ -20,6 +21,7 @@ if [ -f "$confdir/$config" ]; then
else else
echo "configuration file ($confdir/$config) missing" echo "configuration file ($confdir/$config) missing"
echo "new file created, edit it and restart program" echo "new file created, edit it and restart program"
mkdir "$confdir"
cat <<-EOT > "$confdir/$config" cat <<-EOT > "$confdir/$config"
camera=() camera=()
username= username=
@ -123,6 +125,73 @@ declare -A video_saturation=()
declare -A video_hue=() declare -A video_hue=()
declare -A video_sharpness=() declare -A video_sharpness=()
if is_ip "$1"; then
cam=$(cam_for_ip "$1")
parameters=( "$@" )
parameters[0]=$cam
set -- "${parameters[@]}"
fi
case $1 in
@($camlst))
cam_s=$1
cam_f=$1
shift
;;
all)
cam_s=1
cam_f=8
shift
;;
help)
help
exit
;;
*)
exit_with_error "camera number, IP address or 'all' missing"
;;
esac
if [[ -z $1 ]]; then
help
else
command="$1"
shift
fi
if ! test ${SDK[$command]+_}; then
printf "unknown command $command\n\n"
help
fi
for cam in $(seq "$cam_s" "$cam_f");do
case "$command" in
load)
get_json "$cam" "$command" ".CPU.Usage"
;;
health|config|log)
get_text "$cam"
;;
reboot)
put_json "$cam" "$command" '{true}'
;;
snapshot)
view "$cam" "$command"
;;
view_main|view_sub)
watch "$cam" "$command"
;;
help)
help
;;
*)
config "$cam" "$command" "$@"
;;
esac
done
}
is_json () { is_json () {
jq -e . >/dev/null 2>&1 <<<"$@" jq -e . >/dev/null 2>&1 <<<"$@"
} }
@ -468,76 +537,10 @@ exit_with_error () {
exit 1 exit 1
} }
main "$@"
# --------------------------------------------------- # ---------------------------------------------------
if is_ip "$1"; then
cam=$(cam_for_ip "$1")
parameters=( "$@" )
parameters[0]=$cam
set -- "${parameters[@]}"
fi
case $1 in
@($camlst))
cam_s=$1
cam_f=$1
shift
;;
all)
cam_s=1
cam_f=8
shift
;;
help)
help
exit
;;
*)
exit_with_error "camera number, IP address or 'all' missing"
;;
esac
if [[ -z $1 ]]; then
help
else
command="$1"
shift
fi
if ! test ${SDK[$command]+_}; then
printf "unknown command $command\n\n"
help
fi
for cam in $(seq "$cam_s" "$cam_f");do
case "$command" in
load)
get_json "$cam" "$command" ".CPU.Usage"
;;
health|config|log)
get_text "$cam"
;;
reboot)
put_json "$cam" "$command" '{true}'
;;
snapshot)
view "$cam" "$command"
;;
view_main|view_sub)
watch "$cam" "$command"
;;
help)
help
;;
*)
config "$cam" "$command" "$@"
;;
esac
done
exit
# based on strings pulled from firmware dump, these represent api endpoints # based on strings pulled from firmware dump, these represent api endpoints
# used with GET or PUT requests as defined in NK_N1Device_InitV2() # used with GET or PUT requests as defined in NK_N1Device_InitV2()
# #

View file

@ -16,6 +16,7 @@ if [ -f "$confdir/$config" ]; then
else else
echo "configuration file ($confdir/$config) missing" echo "configuration file ($confdir/$config) missing"
echo "new file created, edit it and restart program" echo "new file created, edit it and restart program"
mkdir -p "$confdir"
cat <<-EOT > "$confdir/$config" cat <<-EOT > "$confdir/$config"
monitors=() monitors=()
threshold=3 threshold=3

65
zmapi
View file

@ -4,9 +4,10 @@ trap "trap_error" TERM
trap "trap_clean" EXIT trap "trap_clean" EXIT
export TOP_PID=$$ export TOP_PID=$$
version="1.1.2" version="1.1.3"
release="20221001" release="20221001"
main () {
username="" username=""
password="" password=""
server="" server=""
@ -30,7 +31,8 @@ if [ -f "$confdir/$config" ]; then
else else
echo "configuration file ($confdir/$config) missing" echo "configuration file ($confdir/$config) missing"
echo "new file created, edit it and restart program" echo "new file created, edit it and restart program"
echo <<-EOT > "$confdir/$config" mkdir -p "$confdir"
cat <<-EOT > "$confdir/$config"
# ZoneMinder username, password and server: # ZoneMinder username, password and server:
username= username=
password= password=
@ -66,6 +68,36 @@ declare -A API=(
[configs]='configs.json|G' [configs]='configs.json|G'
) )
token=$(get_token)
cmd="$1"
shift
if [ -z "$cmd" -o "$cmd" == "help" ]; then
help
exit
fi
if ! test ${API[$cmd]+_}; then
printf "zmapi: unknown command '$cmd'\n"
exit 1
fi
method=$(echo "${API[$cmd]}"|cut -d '|' -f 2)
case "$method" in
G)
get "$cmd" "$token" "$@"
;;
P)
post "$cmd" "$token" "$@"
;;
*)
printf "unknown method $method for command $1"
;;
esac
}
get () { get () {
cmd="$1" cmd="$1"
shift shift
@ -234,31 +266,4 @@ EOF
exit exit
} }
token=$(get_token) main "$@"
cmd="$1"
shift
if [ -z "$cmd" -o "$cmd" == "help" ]; then
help
exit
fi
if ! test ${API[$cmd]+_}; then
printf "zmapi: unknown command '$cmd'\n"
exit 1
fi
method=$(echo "${API[$cmd]}"|cut -d '|' -f 2)
case "$method" in
G)
get "$cmd" "$token" "$@"
;;
P)
post "$cmd" "$token" "$@"
;;
*)
printf "unknown method $method for command $1"
;;
esac