-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathshutter-button.sh
More file actions
230 lines (189 loc) · 6.51 KB
/
shutter-button.sh
File metadata and controls
230 lines (189 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/bin/bash
# shellcheck disable=SC1090
#
# Pushes the scanner's camera shutter-button
# The script publishs a message to scanner/apparatus/cameras/shutter-button
#
# Author: cdeck3r
#
# Params: none
# Exit codes
# 0: at the script's end
# 255: if BAIL_OUT
# this directory is the script directory
SCRIPT_DIR="$(
cd "$(dirname "$0")" || exit
pwd -P
)"
cd "$SCRIPT_DIR" || exit
# shellcheck disable=SC2034
SCRIPT_NAME=$0
# Vars
MAX_WAIT_SEC_SAVE_IMAGES=30 # seconds to repeatedly try downloading images
MAX_RUNS_BUTTON_RELEASE=15 # number of runs to test cameras in button release state
YIELD_TIME_SEC=1 # time to wait before between each run
ENABLE_HOUSEKEEPING=1 # performs image housekeeping before saving images
# retrieve image directory from housekeeping cronjob
WWW_IMG_DIR=$(crontab -l | grep housekeeping.sh | cut -d' ' -f 6- | cut -d'>' -f1 | cut -d' ' -f2 | grep -v tmp)
IMG_SUFFIX="png"
MQTT_BROKER="" # set empty
[ -f "${SCRIPT_DIR}/common_vars.conf" ] || {
echo "Could find required config file: common_vars.conf"
echo "Abort."
exit 1
}
[ -f "${SCRIPT_DIR}/tap-functions.sh" ] || {
echo "Could find required file: tap-functions.sh"
echo "Abort."
exit 1
}
source "${SCRIPT_DIR}/common_vars.conf"
source "${SCRIPT_DIR}/tap-functions.sh"
#####################################################
# Include Helper functions
#####################################################
[ -f "${SCRIPT_DIR}/funcs.sh" ] || {
echo "Could find required file: funcs.sh"
echo "Abort."
exit 1
}
source "${SCRIPT_DIR}/funcs.sh"
[ -z "${WWW_IMG_DIR}" ] && BAIL_OUT "Could not determine image directory."
[ -d "${WWW_IMG_DIR}" ] || BAIL_OUT "Image directory does not exist: ${WWW_IMG_DIR}"
latest_img_dir() {
local lid
# source: https://stackoverflow.com/a/64466737
lid=$(find "${WWW_IMG_DIR}" -mindepth 1 -maxdepth 1 -type d -printf "%T@\\t%p\\n" | sort -n | cut -f2- | tail -n1)
echo "${lid}"
}
#####################################################
# Main program
#####################################################
# first things first
HR=$(hr) # horizontal line
plan_no_plan
SKIP_CHECK=$(
true
echo $?
)
precheck "${SKIP_CHECK}"
diag "${HR}"
diag "Prepare"
diag "${HR}"
TOPIC='scanner/apparatus/recent-images/last-saved'
LAST_SAVED="mosquitto_sub -v -h ${MQTT_BROKER} -t ${TOPIC} -W 2"
LAST_SAVED_EXE=$(${LAST_SAVED})
is $? 0 "Retrieve last-saved datetime"
LAST_SAVED_RES=$(echo "${LAST_SAVED_EXE}" | head -1)
echo "${LAST_SAVED_RES}"
LAST_SAVED_DT=$(echo "${LAST_SAVED_RES}" | cut -d' ' -f2)
prev_last_saved_sec=$(date -d"${LAST_SAVED_DT}" +"%s")
diag " "
diag "${HR}"
diag "============ STAND STILL ============"
diag "${HR}"
diag " "
sleep 1
diag "${HR}"
diag "Push scanner's shutter button"
diag "${HR}"
TOPIC="scanner/apparatus/cameras/shutter-button/set"
MSG="push"
BUTTON_PUSH="mosquitto_pub -h ${MQTT_BROKER} -t ${TOPIC} -m ${MSG}"
${BUTTON_PUSH}
is $? 0 "Shutter button pushed"
diag "Button push datetime"
TOPIC="scanner/apparatus/cameras/last-button-push"
LAST_BUTTON_PUSH="mosquitto_sub -v -h ${MQTT_BROKER} -t ${TOPIC} -W 2"
LAST_BUTTON_PUSH_EXE=$(${LAST_BUTTON_PUSH})
is $? 0 "Retrieve button push datetime"
LAST_BUTTON_PUSH_RES=$(echo "${LAST_BUTTON_PUSH_EXE}" | head -1)
echo "${LAST_BUTTON_PUSH_RES}"
sleep 1
diag " "
diag "${HR}"
diag "============ THANK YOU :-) ============"
diag "${HR}"
diag " "
diag "Wait until camnodes have taken processed images"
# We wait because values from MQTT are delayed and do not represent
# the current state
((counter = MAX_RUNS_BUTTON_RELEASE))
while ((--counter > 0)); do
sleep ${YIELD_TIME_SEC}
pass "Wait for camnodes... # $((counter)) seconds"
done
# counter shall not be 0 to indicate success
is $((counter >= 0)) 1 "All cameras done takening images"
diag " "
((ENABLE_HOUSEKEEPING)) && {
./housekeeping.sh
diag " "
}
diag "${HR}"
diag "Saving all camera images"
diag "${HR}"
PREV_LATEST_IMG_DIR=$(latest_img_dir)
TOPIC="scanner/apparatus/recent-images/save-all/set"
MSG="run"
SAVE_ALL_IMAGES="mosquitto_pub -h ${MQTT_BROKER} -t ${TOPIC} -m ${MSG}"
${SAVE_ALL_IMAGES}
is $? 0 "Start saving all camera images... wait to complete"
# loop until last-saved changes
((counter = MAX_WAIT_SEC_SAVE_IMAGES))
while ((--counter > 0)); do
TOPIC='scanner/apparatus/recent-images/last-saved'
LAST_SAVED="mosquitto_sub -v -h ${MQTT_BROKER} -t ${TOPIC} -W 2"
LAST_SAVED_EXE=$(${LAST_SAVED})
is $? 0 "Check # $((MAX_WAIT_SEC_SAVE_IMAGES - counter)): Wait to complete..."
LAST_SAVED_RES=$(echo "${LAST_SAVED_EXE}" | head -1)
echo "${LAST_SAVED_RES}"
# compute diff
LAST_SAVED_DT=$(echo "${LAST_SAVED_RES}" | cut -d' ' -f2)
last_saved_sec=$(date -d"${LAST_SAVED_DT}" +"%s")
((prev_last_saved_sec != last_saved_sec)) && { break; }
sleep 1
done
# counter shall not be 0 to indicate success
is $((counter > 0)) 1 "Save all camera images"
((counter > 0)) || fail "Waiting time exceeded"
diag "${HR}"
diag "Zip all camera images"
diag "${HR}"
# zip all images
CURR_LATEST_IMG_DIR=$(latest_img_dir)
CURR_LATEST_IMG_DIRNAME=$(basename "${CURR_LATEST_IMG_DIR}")
find "${CURR_LATEST_IMG_DIR}" -type f -name "*.${IMG_SUFFIX}" | zip -q -0 "${CURR_LATEST_IMG_DIR}/${CURR_LATEST_IMG_DIRNAME}.zip" -@
ok $? "Zip all downloaded image files"
diag "${HR}"
diag "Post-hoc checks"
diag "${HR}"
## Post-hoc checks
# 1. Check for recent image from each camnode
# 2. print number of downloaded images
# 3. Check zip file integrity
## An image was taken recently, if it not older than 10min.
## Only recent images got downloaded by node_recentimages.py
## As a result, we only have recent images in the image directory.
((imgcnt = 0))
((counter > 0)) && {
# find download dir
CURR_LATEST_IMG_DIR=$(latest_img_dir)
if [[ "${PREV_LATEST_IMG_DIR}" == "${CURR_LATEST_IMG_DIR}" ]]; then
fail "Expected a new image directory, but found old one: ${PREV_LATEST_IMG_DIR}"
else
isnt "${PREV_LATEST_IMG_DIR}" "${CURR_LATEST_IMG_DIR}" "New image directory found: ${CURR_LATEST_IMG_DIR}"
fi
# count images
imgcnt=$(find "${CURR_LATEST_IMG_DIR}" -type f -name "*.${IMG_SUFFIX}" -printf '.' | wc -c)
ok $? "Count number of images in ${CURR_LATEST_IMG_DIR}"
is $((imgcnt > 0)) 1 "Recent images available: ${imgcnt}"
}
# Check zip file integrity
zip -q -T "${CURR_LATEST_IMG_DIR}/${CURR_LATEST_IMG_DIRNAME}.zip"
ok $? "Check image zip file integrity"
# Summary
diag "${HR}"
((counter == 0)) && { diag "${RED}[FAIL]${NC} - Possible problem. Check output."; }
((counter > 0)) && { diag "${GREEN}[SUCCESS]${NC} - ${imgcnt} images available."; }
diag "${HR}"