]>
zdv.bktei.com Git - EVA-2020-02.git/blob - exec/photograph/update_temp_photograph.sh
2 # Desc: Ninfacyzga-1: Capture and store photograph from a camera to temporary directory for 24 hours
3 # Usage: update_temp_photo.sh arg1
4 # Input: arg1: camera tag name (e.g. "DC1")
5 # Depends: age, gnu coreutils, libgps, sleepRand.py
7 #==BEGIN Define Parameters==
8 temp_dir
="/dev/shm"; # default
9 iso_date
="$(date +%Y%m%dT%H%M%S%z)";
10 #iso_date_ns="$(date +%Y%m%dT%H%M%S.%N%z)";
11 device_hostname
="$(hostname)";
13 script_path
="$(dirname "$(realpath -s "$0")")"; # Get script dir Ref/Attrib: https://stackoverflowcom/questions/4774054/
14 nfg_base_dir="$script_path/..
/..
/"; # root directory of ninfacyzga-1 git repo
15 nfg_unitproc_path="$nfg_base_dir"/exec/unitproc;
16 sleep_rand_path="$nfg_base_dir"/exec/unitproc/sleepRand.py;
17 PATH="$nfg_unitproc_path:$PATH"; # include unitprocess nfg executables
19 declare -Ag appRollCall # Associative array for storing app status
20 declare -Ag fileRollCall # Associative array for storing file status
21 declare -Ag dirRollCall # Associative array for storing dir status
22 #==END Define Parameters==
24 #==BEGIN Define Functions==
25 yell() { echo "$0: $
*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique
26 die() { yell "$
*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370
27 try() { "$@
" || die "cannot $
*"; } #o
29 # Desc: If arg is a command, save result in assoc array 'appRollCall'
30 # Usage: checkapp arg1 arg2 arg3 ...
32 # Input: global assoc. array 'appRollCall'
33 # Output: adds/updates key(value) to global assoc array 'appRollCall'
39 if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command
40 appRollCall[$arg]="true
";
41 if ! [ "$returnState" = "false
" ]; then returnState="true
"; fi;
43 appRollCall[$arg]="false
"; returnState="false
";
47 #===Determine function return code===
48 if [ "$returnState" = "true
" ]; then
53 } # Check that app exists
55 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
56 # Usage: checkfile arg1 arg2 arg3 ...
58 # Input: global assoc. array 'fileRollCall'
59 # Output: adds/updates key(value) to global assoc array 'fileRollCall';
60 # Output: returns 0 if app found, 1 otherwise
66 if [ -f "$arg" ]; then
67 fileRollCall["$arg"]="true
";
68 if ! [ "$returnState" = "false
" ]; then returnState="true
"; fi;
70 fileRollCall["$arg"]="false
"; returnState="false
";
74 #===Determine function return code===
75 if [ "$returnState" = "true
" ]; then
80 } # Check that file exists
82 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
83 # Usage: checkdir arg1 arg2 arg3 ...
85 # Input: global assoc. array 'dirRollCall'
86 # Output: adds/updates key(value) to global assoc array 'dirRollCall';
87 # Output: returns 0 if app found, 1 otherwise
93 if [ -d "$arg" ]; then
94 dirRollCall["$arg"]="true
";
95 if ! [ "$returnState" = "false
" ]; then returnState="true
"; fi
97 dirRollCall["$arg"]="false
"; returnState="false
";
101 #===Determine function return code===
102 if [ "$returnState" = "true
" ]; then
107 } # Check that dir exists
109 # Desc: Displays missing apps, files, and dirs
110 # Usage: displayMissing
112 # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
113 # Output: stderr: messages indicating missing apps, file, or dirs
114 # Depends: bash 5, checkAppFileDir()
115 local missingApps value appMissing missingFiles fileMissing
116 local missingDirs dirMissing
118 #==BEGIN Display errors==
119 #===BEGIN Display Missing Apps===
120 missingApps="Missing apps
:";
121 #for key in "${!appRollCall[@]}"; do echo "DEBUG
:$key => ${appRollCall[$key]}"; done
122 for key in "${!appRollCall[@]}"; do
123 value="${appRollCall[$key]}";
124 if [ "$value" = "false
" ]; then
125 #echo "DEBUG
:Missing apps
: $key => $value";
126 missingApps="$missingApps""$key ";
130 if [ "$appMissing" = "true
" ]; then # Only indicate if an app is missing.
131 echo "$missingApps" 1>&2;
134 #===END Display Missing Apps===
136 #===BEGIN Display Missing Files===
137 missingFiles="Missing files
:";
138 #for key in "${!fileRollCall[@]}"; do echo "DEBUG
:$key => ${fileRollCall[$key]}"; done
139 for key in "${!fileRollCall[@]}"; do
140 value="${fileRollCall[$key]}";
141 if [ "$value" = "false
" ]; then
142 #echo "DEBUG
:Missing files
: $key => $value";
143 missingFiles="$missingFiles""$key ";
147 if [ "$fileMissing" = "true
" ]; then # Only indicate if an app is missing.
148 echo "$missingFiles" 1>&2;
151 #===END Display Missing Files===
153 #===BEGIN Display Missing Directories===
154 missingDirs="Missing
dirs:";
155 #for key in "${!dirRollCall[@]}"; do echo "DEBUG
:$key => ${dirRollCall[$key]}"; done
156 for key in "${!dirRollCall[@]}"; do
157 value="${dirRollCall[$key]}";
158 if [ "$value" = "false
" ]; then
159 #echo "DEBUG
:Missing
dirs: $key => $value";
160 missingDirs="$missingDirs""$key ";
164 if [ "$dirMissing" = "true
" ]; then # Only indicate if an dir is missing.
165 echo "$missingDirs" 1>&2;
168 #===END Display Missing Directories===
170 #==END Display errors==
171 } # Display missing apps, files, dirs
173 # Desc: Report seconds until next day.
175 # Output: stdout: integer seconds until next day
176 # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0
177 # Usage: timeUntilNextDay
178 # Usage: if ! myTTL="$(timeUntilNextDay)"; then yell "ERROR
in if statement
"; exit 1; fi
179 # Depends: date 8, echo 8, yell, try
181 local returnState timeCurrent timeNextDay secondsUntilNextDay
182 timeCurrent="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
183 timeNextDay="$(date -d "$timeCurrent next day" --iso-8601=date)"; # Produce timestamp of beginning of tomorrow with resolution of 1 second.
184 secondsUntilNextDay="$(( $(date +%s -d "$timeNextDay") - $(date +%s -d "$timeCurrent") ))" ; # Calculate seconds until closest future midnight (res. 1 second).
185 if [[ "$secondsUntilNextDay" -gt 0 ]]; then
187 elif [[ "$secondsUntilNextDay" -eq 0 ]]; then
188 returnState="warning_zero
";
189 yell "WARNING
:Reported
time until next day exactly zero.
";
190 elif [[ "$secondsUntilNextDay" -lt 0 ]]; then
191 returnState="warning_negative
";
192 yell "WARNING
:Reported
time until next day is negative.
";
195 try echo "$secondsUntilNextDay"; # Report
197 # Determine function return code
198 if [[ "$returnState" = "true
" ]]; then
200 elif [[ "$returnState" = "warning_zero
" ]]; then
202 elif [[ "$returnState" = "warning_negative
" ]]; then
205 } # Report seconds until next day
207 #Desc: Sets script_ttl seconds
208 #Usage: set_script_ttl
210 #Output: var: script_ttl (integer seconds)
211 #Depends: timeUntilNextDay()
213 # Set script lifespan to end at start of next day
214 if ! script_ttl="$(timeUntilNextDay)"; then # sets scriptTTL, then checks exit code
215 if [[ "$script_ttl" -eq 0 ]]; then
216 ((script_ttl++)); # Add 1 because 0 would cause 'timeout' to never timeout.
219 } # Set script_ttl in seconds until next day
221 # Input: var: $temp_photo_path file path to save photo
222 # Depends: checkapp(), raspistill
224 try raspistill -gps -t 0 -q 95 -ex auto -e "$encoding" -o "$temp_photo_path";
227 # Desc: Display script usage information
232 # Depends: GNU-coreutils 8.30 (cat)
235 update_temp_photograph.sh [camera_name]
238 update_temp_photograph.sh DC1
240 } # Display information on how to use this script.
244 #===BEGIN process arguments===
245 if [[ $# -ge 1 ]]; then
248 die "ERROR
:Not enough arguments.
";
252 temp_photo_dir="$temp_dir"/ninfacyzga/photograph; # generic-use dir
253 temp_photo_filename="$camera_name".jpg; # use jpg encoding
254 temp_photo_path="$temp_photo_dir"/"$temp_photo_filename";
255 #===END process arguments===
256 #===BEGIN Check for required files, dirs, and apps===
257 # Create output dir for photo dir
258 flag_exit_early="false
";
259 if ! checkdir "$temp_photo_dir"; then
260 try mkdir -p "$temp_photo_dir";
263 # Check for missing files
264 if ! checkfile "$sleep_rand_path"; then
265 flag_exit_early="true
";
268 # Check for missing apps
269 if ! checkapp raspistill; then
270 flag_exit_early="true
";
275 if [[ $flag_exit_early == "true
" ]]; then
276 die "ERROR
:Exiting early.
";
278 #===END Check for required files, dirs, and apps===
280 #===BEGIN main loop===
282 while [[ $SECONDS -lt "$script_ttl" ]]; do
283 # Capture new photograph
284 try update_temp_file;
285 try python3 "$sleep_rand_path" --upper 3600.0 60.0;
290 #==END Define Functions==