REFACTOR: Refactored libs/watchdog.sh

* Shortend Code of MAIN section.
* Added function get_conf_devices
* SHELLCHECK: Enabled requires-variable-braces

Signed-off-by: Stephan Wendel <me@stephanwe.de>
This commit is contained in:
Stephan Wendel 2021-12-17 21:44:32 +01:00
parent 1b89b86545
commit a17d063145
No known key found for this signature in database
GPG Key ID: F465B83ACBA45639

View File

@ -11,61 +11,64 @@
#### This File is distributed under GPLv3 #### This File is distributed under GPLv3
#### ####
# shellcheck enable=requires-variable-braces
# Exit upon Errors # Exit upon Errors
set -e set -e
#### Watchdog Functions and Variables #### Watchdog Functions and Variables
## Do not reuse previous functions! ## Do not reuse functions from other libs/scripts!
# We want watchdog operating independently!
function clean_watchdog { function clean_watchdog {
rm -f $PWD/lost-* rm -f /tmp/lost-*
} }
function webcamd_watchdog { function webcamd_watchdog {
# Helper Functions # Helper Functions
function available { function available {
find ${1} &> /dev/null find "${1}" &> /dev/null
echo $? echo "${?}"
} }
function lost_dev { function lost_dev {
local lostfile local lostfile
lostfile="$(echo ${1} | awk -F '/' '{print $NF}')" lostfile="$(echo "${1}" | awk -F '/' '{print $NF}')"
touch /tmp/lost-${lostfile} touch /tmp/lost-"${lostfile}"
} }
function is_lost { function is_lost {
local lostdev local lostdev
lostdev="$(echo ${1} | awk -F '/' '{print $NF}')" lostdev="$(echo "${1}" | awk -F '/' '{print $NF}')"
find /tmp/lost-${lostdev} &> /dev/null find /tmp/lost-"${lostdev}" &> /dev/null
echo $? echo "${?}"
} }
function returned_dev { function returned_dev {
local lostdev local lostdev
lostdev="$(echo ${1} | awk -F '/' '{print $NF}')" lostdev="$(echo "${1}"| awk -F '/' '{print $NF}')"
rm -f /tmp/lost-${lostdev} &> /dev/null rm -f /tmp/lost-"${lostdev}" &> /dev/null
} }
# local Vars function get_conf_devices {
local get_conf_devices conf_cams avail_cams local gcd
# Init empty Arrays for i in $(crudini --existing=file --get "${WEBCAMD_CFG}" | \
get_conf_devices=() sed '/webcamd/d' | cut -d ' ' -f2); do
conf_cams=() gcd+=("${i}")
# Grab devices from config file
get_conf_devices=("$(crudini --existing=file --get "${WEBCAMD_CFG}" | \
sed '/webcamd/d' | cut -d ' ' -f2)")
# Construct Array with configured Devices
for gcd in ${get_conf_devices[*]}; do
conf_cams+=("$(crudini --get "${WEBCAMD_CFG}" "cam ${gcd}" "device" \
| awk '{print $1}')")
done done
# Send Message if Device available or returned. echo "${gcd[@]}"
for cc in ${conf_cams[*]}; do }
if [ "$(available ${cc})" -ne 0 ] && [ "$(is_lost ${cc})" -ne 0 ]; then
log_msg "WATCHDOG: Lost Device: "${cc}"" ### MAIN
for i in $(get_conf_devices); do
cc="$(crudini --get "${WEBCAMD_CFG}" "cam ${i}" "device" \
| awk '{print $1}')"
if [ "$(available "${cc}")" -ne 0 ] && [ "$(is_lost "${cc}")" -ne 0 ]; then
log_msg "WATCHDOG: Lost Device: '${cc}'"
lost_dev "${cc}" lost_dev "${cc}"
elif [ "$(is_lost ${cc})" -eq 0 ] && [ "$(available ${cc})" -eq 0 ]; then elif [ "$(is_lost "${cc}")" -eq 0 ] && [ "$(available "${cc}")" -eq 0 ]; then
log_msg "WATCHDOG: Device ${cc} returned." log_msg "WATCHDOG: Device '${cc}' returned."
returned_dev "${cc}" returned_dev "${cc}"
fi fi
done done