Commit | Line | Data |
---|---|---|
8233253b SBS |
1 | #!/bin/bash |
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 | |
6 | ||
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)"; | |
a4c49e2a SBS |
12 | |
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 | |
8233253b SBS |
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 | |
18 | ||
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== | |
23 | ||
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 | |
28 | checkapp() { | |
29 | # Desc: If arg is a command, save result in assoc array 'appRollCall' | |
30 | # Usage: checkapp arg1 arg2 arg3 ... | |
31 | # Version: 0.1.1 | |
32 | # Input: global assoc. array 'appRollCall' | |
33 | # Output: adds/updates key(value) to global assoc array 'appRollCall' | |
34 | # Depends: bash 5.0.3 | |
35 | local returnState | |
36 | ||
37 | #===Process Args=== | |
38 | for arg in "$@"; do | |
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; | |
42 | else | |
43 | appRollCall[$arg]="false"; returnState="false"; | |
44 | fi; | |
45 | done; | |
46 | ||
47 | #===Determine function return code=== | |
48 | if [ "$returnState" = "true" ]; then | |
49 | return 0; | |
50 | else | |
51 | return 1; | |
52 | fi; | |
53 | } # Check that app exists | |
54 | checkfile() { | |
55 | # Desc: If arg is a file path, save result in assoc array 'fileRollCall' | |
56 | # Usage: checkfile arg1 arg2 arg3 ... | |
57 | # Version: 0.1.1 | |
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 | |
61 | # Depends: bash 5.0.3 | |
62 | local returnState | |
63 | ||
64 | #===Process Args=== | |
65 | for arg in "$@"; do | |
66 | if [ -f "$arg" ]; then | |
67 | fileRollCall["$arg"]="true"; | |
68 | if ! [ "$returnState" = "false" ]; then returnState="true"; fi; | |
69 | else | |
70 | fileRollCall["$arg"]="false"; returnState="false"; | |
71 | fi; | |
72 | done; | |
73 | ||
74 | #===Determine function return code=== | |
75 | if [ "$returnState" = "true" ]; then | |
76 | return 0; | |
77 | else | |
78 | return 1; | |
79 | fi; | |
80 | } # Check that file exists | |
81 | checkdir() { | |
82 | # Desc: If arg is a dir path, save result in assoc array 'dirRollCall' | |
83 | # Usage: checkdir arg1 arg2 arg3 ... | |
84 | # Version 0.1.1 | |
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 | |
88 | # Depends: Bash 5.0.3 | |
89 | local returnState | |
90 | ||
91 | #===Process Args=== | |
92 | for arg in "$@"; do | |
93 | if [ -d "$arg" ]; then | |
94 | dirRollCall["$arg"]="true"; | |
95 | if ! [ "$returnState" = "false" ]; then returnState="true"; fi | |
96 | else | |
97 | dirRollCall["$arg"]="false"; returnState="false"; | |
98 | fi | |
99 | done | |
100 | ||
101 | #===Determine function return code=== | |
102 | if [ "$returnState" = "true" ]; then | |
103 | return 0; | |
104 | else | |
105 | return 1; | |
106 | fi | |
107 | } # Check that dir exists | |
108 | displayMissing() { | |
109 | # Desc: Displays missing apps, files, and dirs | |
110 | # Usage: displayMissing | |
111 | # Version 0.1.1 | |
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 | |
117 | ||
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 "; | |
127 | appMissing="true"; | |
128 | fi; | |
129 | done; | |
130 | if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing. | |
131 | echo "$missingApps" 1>&2; | |
132 | fi; | |
133 | unset value; | |
134 | #===END Display Missing Apps=== | |
135 | ||
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 "; | |
144 | fileMissing="true"; | |
145 | fi; | |
146 | done; | |
147 | if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing. | |
148 | echo "$missingFiles" 1>&2; | |
149 | fi; | |
150 | unset value; | |
151 | #===END Display Missing Files=== | |
152 | ||
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 "; | |
161 | dirMissing="true"; | |
162 | fi; | |
163 | done; | |
164 | if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing. | |
165 | echo "$missingDirs" 1>&2; | |
166 | fi; | |
167 | unset value; | |
168 | #===END Display Missing Directories=== | |
169 | ||
170 | #==END Display errors== | |
171 | } # Display missing apps, files, dirs | |
172 | timeUntilNextDay(){ | |
173 | # Desc: Report seconds until next day. | |
174 | # Version: 1.0.3 | |
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 | |
180 | ||
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 | |
186 | returnState="true"; | |
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."; | |
193 | fi | |
194 | ||
195 | try echo "$secondsUntilNextDay"; # Report | |
196 | ||
197 | # Determine function return code | |
198 | if [[ "$returnState" = "true" ]]; then | |
199 | return 0; | |
200 | elif [[ "$returnState" = "warning_zero" ]]; then | |
201 | return 1; | |
202 | elif [[ "$returnState" = "warning_negative" ]]; then | |
203 | return 2; | |
204 | fi | |
205 | } # Report seconds until next day | |
206 | set_script_ttl() { | |
207 | #Desc: Sets script_ttl seconds | |
208 | #Usage: set_script_ttl | |
209 | #Input: none | |
210 | #Output: var: script_ttl (integer seconds) | |
211 | #Depends: timeUntilNextDay() | |
212 | ||
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. | |
217 | fi; | |
218 | fi; | |
219 | } # Set script_ttl in seconds until next day | |
220 | update_temp_file() { | |
221 | # Input: var: $temp_photo_path file path to save photo | |
222 | # Depends: checkapp(), raspistill | |
223 | encoding="jpg"; | |
224 | try raspistill -gps -t 0 -q 95 -ex auto -e "$encoding" -o "$temp_photo_path"; | |
225 | }; | |
226 | showUsage() { | |
227 | # Desc: Display script usage information | |
228 | # Usage: showUsage | |
229 | # Version 0.0.1 | |
230 | # Input: none | |
231 | # Output: stdout | |
232 | # Depends: GNU-coreutils 8.30 (cat) | |
233 | cat <<'EOF' | |
234 | USAGE: | |
235 | update_temp_photograph.sh [camera_name] | |
236 | ||
237 | EXAMPLE: | |
238 | update_temp_photograph.sh DC1 | |
239 | EOF | |
240 | } # Display information on how to use this script. | |
241 | ||
242 | main() { | |
243 | : | |
244 | #===BEGIN process arguments=== | |
245 | if [[ $# -ge 1 ]]; then | |
246 | camera_name="$1"; | |
247 | else | |
248 | die "ERROR:Not enough arguments."; | |
249 | showUsage; | |
250 | fi; | |
251 | ||
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"; | |
261 | fi; | |
262 | ||
263 | # Check for missing files | |
264 | if ! checkfile "$sleep_rand_path"; then | |
265 | flag_exit_early="true"; | |
266 | fi; | |
267 | ||
268 | # Check for missing apps | |
269 | if ! checkapp raspistill; then | |
270 | flag_exit_early="true"; | |
271 | fi; | |
272 | ||
273 | # Display missing | |
274 | displayMissing; | |
275 | if [[ $flag_exit_early == "true" ]]; then | |
276 | die "ERROR:Exiting early."; | |
277 | fi; | |
278 | #===END Check for required files, dirs, and apps=== | |
279 | ||
280 | #===BEGIN main loop=== | |
281 | set_script_ttl; | |
282 | while [[ $SECONDS -lt "$script_ttl" ]]; do | |
283 | # Capture new photograph | |
284 | try update_temp_file; | |
eba44c51 | 285 | try python3 "$sleep_rand_path" --upper 3600.0 60.0; |
8233253b SBS |
286 | done; |
287 | #===END main loop=== | |
288 | }; | |
289 | ||
290 | #==END Define Functions== | |
291 | ||
292 | main "$@"; |