From: Steven Baltakatei Sandoval Date: Mon, 18 Oct 2021 06:46:10 +0000 (+0000) Subject: feat(exec/photograph):Add photographing script X-Git-Url: https://zdv2.bktei.com/gitweb/EVA-2020-02.git/commitdiff_plain/8233253bc3fe8f2f4216143a5a5e3ec1bf00db59?ds=inline;hp=-c feat(exec/photograph):Add photographing script --- 8233253bc3fe8f2f4216143a5a5e3ec1bf00db59 diff --git a/exec/photograph/update_temp_photograph.sh b/exec/photograph/update_temp_photograph.sh new file mode 100644 index 0000000..d531dd9 --- /dev/null +++ b/exec/photograph/update_temp_photograph.sh @@ -0,0 +1,290 @@ +#!/bin/bash +# Desc: Ninfacyzga-1: Capture and store photograph from a camera to temporary directory for 24 hours +# Usage: update_temp_photo.sh arg1 +# Input: arg1: camera tag name (e.g. "DC1") +# Depends: age, gnu coreutils, libgps, sleepRand.py + +#==BEGIN Define Parameters== +temp_dir="/dev/shm"; # default +iso_date="$(date +%Y%m%dT%H%M%S%z)"; +#iso_date_ns="$(date +%Y%m%dT%H%M%S.%N%z)"; +device_hostname="$(hostname)"; +nfg_base_dir="../../"; # root directory of ninfacyzga-1 git repo +nfg_unitproc_path="$nfg_base_dir"/exec/unitproc; +sleep_rand_path="$nfg_base_dir"/exec/unitproc/sleepRand.py; +PATH="$nfg_unitproc_path:$PATH"; # include unitprocess nfg executables + +declare -Ag appRollCall # Associative array for storing app status +declare -Ag fileRollCall # Associative array for storing file status +declare -Ag dirRollCall # Associative array for storing dir status +#==END Define Parameters== + +#==BEGIN Define Functions== +yell() { echo "$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique +die() { yell "$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370 +try() { "$@" || die "cannot $*"; } #o +checkapp() { + # Desc: If arg is a command, save result in assoc array 'appRollCall' + # Usage: checkapp arg1 arg2 arg3 ... + # Version: 0.1.1 + # Input: global assoc. array 'appRollCall' + # Output: adds/updates key(value) to global assoc array 'appRollCall' + # Depends: bash 5.0.3 + local returnState + + #===Process Args=== + for arg in "$@"; do + if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command + appRollCall[$arg]="true"; + if ! [ "$returnState" = "false" ]; then returnState="true"; fi; + else + appRollCall[$arg]="false"; returnState="false"; + fi; + done; + + #===Determine function return code=== + if [ "$returnState" = "true" ]; then + return 0; + else + return 1; + fi; +} # Check that app exists +checkfile() { + # Desc: If arg is a file path, save result in assoc array 'fileRollCall' + # Usage: checkfile arg1 arg2 arg3 ... + # Version: 0.1.1 + # Input: global assoc. array 'fileRollCall' + # Output: adds/updates key(value) to global assoc array 'fileRollCall'; + # Output: returns 0 if app found, 1 otherwise + # Depends: bash 5.0.3 + local returnState + + #===Process Args=== + for arg in "$@"; do + if [ -f "$arg" ]; then + fileRollCall["$arg"]="true"; + if ! [ "$returnState" = "false" ]; then returnState="true"; fi; + else + fileRollCall["$arg"]="false"; returnState="false"; + fi; + done; + + #===Determine function return code=== + if [ "$returnState" = "true" ]; then + return 0; + else + return 1; + fi; +} # Check that file exists +checkdir() { + # Desc: If arg is a dir path, save result in assoc array 'dirRollCall' + # Usage: checkdir arg1 arg2 arg3 ... + # Version 0.1.1 + # Input: global assoc. array 'dirRollCall' + # Output: adds/updates key(value) to global assoc array 'dirRollCall'; + # Output: returns 0 if app found, 1 otherwise + # Depends: Bash 5.0.3 + local returnState + + #===Process Args=== + for arg in "$@"; do + if [ -d "$arg" ]; then + dirRollCall["$arg"]="true"; + if ! [ "$returnState" = "false" ]; then returnState="true"; fi + else + dirRollCall["$arg"]="false"; returnState="false"; + fi + done + + #===Determine function return code=== + if [ "$returnState" = "true" ]; then + return 0; + else + return 1; + fi +} # Check that dir exists +displayMissing() { + # Desc: Displays missing apps, files, and dirs + # Usage: displayMissing + # Version 0.1.1 + # Input: associative arrays: appRollCall, fileRollCall, dirRollCall + # Output: stderr: messages indicating missing apps, file, or dirs + # Depends: bash 5, checkAppFileDir() + local missingApps value appMissing missingFiles fileMissing + local missingDirs dirMissing + + #==BEGIN Display errors== + #===BEGIN Display Missing Apps=== + missingApps="Missing apps :"; + #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done + for key in "${!appRollCall[@]}"; do + value="${appRollCall[$key]}"; + if [ "$value" = "false" ]; then + #echo "DEBUG:Missing apps: $key => $value"; + missingApps="$missingApps""$key "; + appMissing="true"; + fi; + done; + if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing. + echo "$missingApps" 1>&2; + fi; + unset value; + #===END Display Missing Apps=== + + #===BEGIN Display Missing Files=== + missingFiles="Missing files:"; + #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done + for key in "${!fileRollCall[@]}"; do + value="${fileRollCall[$key]}"; + if [ "$value" = "false" ]; then + #echo "DEBUG:Missing files: $key => $value"; + missingFiles="$missingFiles""$key "; + fileMissing="true"; + fi; + done; + if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing. + echo "$missingFiles" 1>&2; + fi; + unset value; + #===END Display Missing Files=== + + #===BEGIN Display Missing Directories=== + missingDirs="Missing dirs:"; + #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done + for key in "${!dirRollCall[@]}"; do + value="${dirRollCall[$key]}"; + if [ "$value" = "false" ]; then + #echo "DEBUG:Missing dirs: $key => $value"; + missingDirs="$missingDirs""$key "; + dirMissing="true"; + fi; + done; + if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing. + echo "$missingDirs" 1>&2; + fi; + unset value; + #===END Display Missing Directories=== + + #==END Display errors== +} # Display missing apps, files, dirs +timeUntilNextDay(){ + # Desc: Report seconds until next day. + # Version: 1.0.3 + # Output: stdout: integer seconds until next day + # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0 + # Usage: timeUntilNextDay + # Usage: if ! myTTL="$(timeUntilNextDay)"; then yell "ERROR in if statement"; exit 1; fi + # Depends: date 8, echo 8, yell, try + + local returnState timeCurrent timeNextDay secondsUntilNextDay + timeCurrent="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second. + timeNextDay="$(date -d "$timeCurrent next day" --iso-8601=date)"; # Produce timestamp of beginning of tomorrow with resolution of 1 second. + secondsUntilNextDay="$(( $(date +%s -d "$timeNextDay") - $(date +%s -d "$timeCurrent") ))" ; # Calculate seconds until closest future midnight (res. 1 second). + if [[ "$secondsUntilNextDay" -gt 0 ]]; then + returnState="true"; + elif [[ "$secondsUntilNextDay" -eq 0 ]]; then + returnState="warning_zero"; + yell "WARNING:Reported time until next day exactly zero."; + elif [[ "$secondsUntilNextDay" -lt 0 ]]; then + returnState="warning_negative"; + yell "WARNING:Reported time until next day is negative."; + fi + + try echo "$secondsUntilNextDay"; # Report + + # Determine function return code + if [[ "$returnState" = "true" ]]; then + return 0; + elif [[ "$returnState" = "warning_zero" ]]; then + return 1; + elif [[ "$returnState" = "warning_negative" ]]; then + return 2; + fi +} # Report seconds until next day +set_script_ttl() { + #Desc: Sets script_ttl seconds + #Usage: set_script_ttl + #Input: none + #Output: var: script_ttl (integer seconds) + #Depends: timeUntilNextDay() + + # Set script lifespan to end at start of next day + if ! script_ttl="$(timeUntilNextDay)"; then # sets scriptTTL, then checks exit code + if [[ "$script_ttl" -eq 0 ]]; then + ((script_ttl++)); # Add 1 because 0 would cause 'timeout' to never timeout. + fi; + fi; +} # Set script_ttl in seconds until next day +update_temp_file() { + # Input: var: $temp_photo_path file path to save photo + # Depends: checkapp(), raspistill + encoding="jpg"; + try raspistill -gps -t 0 -q 95 -ex auto -e "$encoding" -o "$temp_photo_path"; +}; +showUsage() { + # Desc: Display script usage information + # Usage: showUsage + # Version 0.0.1 + # Input: none + # Output: stdout + # Depends: GNU-coreutils 8.30 (cat) + cat <<'EOF' + USAGE: + update_temp_photograph.sh [camera_name] + + EXAMPLE: + update_temp_photograph.sh DC1 +EOF +} # Display information on how to use this script. + +main() { + : + #===BEGIN process arguments=== + if [[ $# -ge 1 ]]; then + camera_name="$1"; + else + die "ERROR:Not enough arguments."; + showUsage; + fi; + + temp_photo_dir="$temp_dir"/ninfacyzga/photograph; # generic-use dir + temp_photo_filename="$camera_name".jpg; # use jpg encoding + temp_photo_path="$temp_photo_dir"/"$temp_photo_filename"; + #===END process arguments=== + #===BEGIN Check for required files, dirs, and apps=== + # Create output dir for photo dir + flag_exit_early="false"; + if ! checkdir "$temp_photo_dir"; then + try mkdir -p "$temp_photo_dir"; + fi; + + # Check for missing files + if ! checkfile "$sleep_rand_path"; then + flag_exit_early="true"; + fi; + + # Check for missing apps + if ! checkapp raspistill; then + flag_exit_early="true"; + fi; + + # Display missing + displayMissing; + if [[ $flag_exit_early == "true" ]]; then + die "ERROR:Exiting early."; + fi; + #===END Check for required files, dirs, and apps=== + + #===BEGIN main loop=== + set_script_ttl; + while [[ $SECONDS -lt "$script_ttl" ]]; do + # Capture new photograph + try update_temp_file; + try python3 "$sleep_rand_path" --upper 3600.0 10.0; + done; + #===END main loop=== +}; + +#==END Define Functions== + +main "$@";