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
version="1.0.2"
version="1.0.3"
release="20221001"
main () {
declare -A camera
username="admin"
@ -20,6 +21,7 @@ if [ -f "$confdir/$config" ]; then
else
echo "configuration file ($confdir/$config) missing"
echo "new file created, edit it and restart program"
mkdir "$confdir"
cat <<-EOT > "$confdir/$config"
camera=()
username=
@ -123,6 +125,73 @@ declare -A video_saturation=()
declare -A video_hue=()
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 () {
jq -e . >/dev/null 2>&1 <<<"$@"
}
@ -468,76 +537,10 @@ exit_with_error () {
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
# used with GET or PUT requests as defined in NK_N1Device_InitV2()
#

View file

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

65
zmapi
View file

@ -4,9 +4,10 @@ trap "trap_error" TERM
trap "trap_clean" EXIT
export TOP_PID=$$
version="1.1.2"
version="1.1.3"
release="20221001"
main () {
username=""
password=""
server=""
@ -30,7 +31,8 @@ if [ -f "$confdir/$config" ]; then
else
echo "configuration file ($confdir/$config) missing"
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:
username=
password=
@ -66,6 +68,36 @@ declare -A API=(
[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 () {
cmd="$1"
shift
@ -234,31 +266,4 @@ EOF
exit
}
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
main "$@"