-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame_functions
231 lines (191 loc) · 7.08 KB
/
frame_functions
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
231
. ./frame_configuration
function fail() {
error_message="$1"
if [ -n "$error_message" ]; then
echo $error_message
fi
disable_swap
umount $SD_CARD_MOUNT_POINT
sd_switch off
exit 1
}
function get_device_mount_point() {
device="$1"
egrep "^$device" /proc/mounts | awk '{print $2}'
}
function mount_sd_card() {
[ -b "$SD_CARD_DEVICE" ] || fail "Can't find SD card device $SD_CARD_DEVICE"
[ -d "$SD_CARD_MOUNT_POINT" ] || fail "The specified mount point ($SD_CARD_MOUNT_POINT) is not a directory"
current_mount_point=`get_device_mount_point "$SD_CARD_DEVICE"`
if [ -n "$current_mount_point" ]; then
if [ "$current_mount_point" != "$SD_CARD_MOUNT_POINT" ]; then
fail "$SD_CARD_DEVICE is already mounted at $current_mount_point"
fi
# Already mounted at the requested location
return
fi
fsck.fat -a $SD_CARD_DEVICE
mount $SD_CARD_DEVICE "$SD_CARD_MOUNT_POINT" || fail "Could not mount $SD_CARD_DEVICE at $SD_CARD_MOUNT_POINT"
find "$SD_CARD_MOUNT_POINT" -iname "fsck????.rec" -print0 | xargs -0 rm -f
}
function unmount_sd_card() {
current_mount_point=`get_device_mount_point "$SD_CARD_DEVICE"`
if [ "$current_mount_point" == "$SD_CARD_MOUNT_POINT" ]; then
umount "$SD_CARD_MOUNT_POINT"
fi
}
function enable_swap() {
[ -f $SD_CARD_SWAP_FILE ] || fail "Swap file $SD_CARD_SWAP_FILE doesn't exist"
swapon $SD_CARD_SWAP_FILE
}
function disable_swap() {
if grep -q $SD_CARD_SWAP_FILE /proc/swaps; then
swapoff $SD_CARD_SWAP_FILE
fi
}
function sd_switch() {
[ "$1" == "on" ] || [ "$1" == "off" ] || fail "Invalid SD switch action $1"
[ -e $SYSFS_SD_CARD_SWITCH ] || fail "sysfs SD switch not available ($SYSFS_SD_CARD_SWITCH)"
action="$1"
current_switch_value=`cat $SYSFS_SD_CARD_SWITCH`
if [ $action == "on" ]; then
target_switch_value=0
else
target_switch_value=1
fi
if [ $current_switch_value != $target_switch_value ]; then
echo $target_switch_value > $SYSFS_SD_CARD_SWITCH
sleep 5
fi
}
function verify_seafile_server_is_reachable() {
[ "`curl --silent $SEAFILE_URL/api2/ping/`" == '"pong"' ] || fail "Seafile server is unreachable"
}
function remove_images_not_in_seafile_library() {
[ -d "$SD_CARD_IMAGE_DIRECTORY" ] || fail "Can't find image directory '$SD_CARD_IMAGE_DIRECTORY'"
echo "Removing files that have been removed on server..."
server_file_list=`curl --silent --header "Authorization: Token $SEAFILE_LIBRARY_API_TOKEN" --header "Accept: application/json" $SEAFILE_URL/api/v2.1/via-repo-token/dir/`
[ $? == 0 ] || fail "Can't get file list from seafile server"
server_file_names=`echo "$server_file_list" | jsonfilter -e "@.dirent_list.*.name"`
find "$SD_CARD_IMAGE_DIRECTORY" -maxdepth 1 -type f | while read line; do
# Executed in a sub shell
if [ "$line" == "$SD_CARD_SWAP_FILE" ]; then
continue
fi
local_file_name=`basename "$line"`
if ! echo "$server_file_names" | grep -q "^$local_file_name$"; then
echo Removing "$local_file_name"
rm -f "$SD_CARD_IMAGE_DIRECTORY/$local_file_name"
fi
done
}
function url_encode() {
echo "$1" | sed \
-e "s/%/%25/g" \
-e "s/ /%20/g"
# -e "s/!/%21/g" \
# -e 's/"/%22/g' \
# -e "s/#/%23/g" \
# -e "s/\\$/%24/g" \
# -e "s/&/%26/g" \
# -e "s/'/%27/g" \
# -e "s/(/%28/g" \
# -e "s/)/%29/g" \
# -e "s/\\*/%2a/g" \
# -e "s/\\+/%2b/g" \
# -e "s/,/%2c/g" \
# -e "s/-/%2d/g" \
# -e "s/\./%2e/g" \
# -e "s#/#%2f#g" \
# -e "s/:/%3a/g" \
# -e "s/;/%3b/g" \
# -e 's/\\</%3c/g' \
# -e 's/\\>/%3e/g' \
# -e "s/=/%3d/g" \
# -e "s/?/%3f/g" \
# -e "s/@/%40/g" \
# -e "s/\\[/%5b/g" \
# -e "s/\\]/%5d/g" \
# -e "s/\\\/%5c/g" \
# -e "s/\\^/%5e/g" \
# -e "s/_/%5f/g" \
# -e 's/`/%60/g' \
# -e "s/{/%7b/g" \
# -e "s/|/%7c/g" \
# -e "s/}/%7d/g" \
# -e "s/~/%7e/g"
}
function download_and_process_images_from_seafile_library() {
[ -d "$SD_CARD_IMAGE_DIRECTORY" ] || fail "Can't find image directory '$SD_CARD_IMAGE_DIRECTORY'"
mkdir -p "$SD_CARD_TMP_DIRECTORY" || fail "Can't create temporary download directory '$SD_CARD_TMP_DIRECTORY'"
echo "Downloading and processing images from server..."
server_file_data=`curl --silent --header "Authorization: Token $SEAFILE_LIBRARY_API_TOKEN" --header "Accept: application/json" $SEAFILE_URL/api/v2.1/via-repo-token/dir/`
echo "$server_file_data" | jsonfilter -e "@.dirent_list.*" | while read file_data; do
# Executed in a sub shell
file_name=`echo "$file_data" | jsonfilter -e '$.name'`
echo "$file_name"
cd $SD_CARD_IMAGE_DIRECTORY
if [ -e "$file_name" ]; then
if [ `stat --printf %s "$file_name"` -eq 0 ]; then
echo " - corrupt, zero sized file"
else
local_file_mtime=`stat --printf %Y "$file_name"`
server_file_mtime_iso=`echo "$file_data" | jsonfilter -e '$.mtime'`
server_file_mtime_busybox=`echo "$server_file_mtime_iso" | sed "s/T/ /"`
server_file_mtime=`date -d "$server_file_mtime_busybox" +%s`
if [ "$server_file_mtime" -lt "$local_file_mtime" ]; then
echo " - ok"
continue
fi
fi
fi
url_encoded_file_name=`url_encode "$file_name"`
download_url=`curl --silent --header "Authorization: Token $SEAFILE_LIBRARY_API_TOKEN" --header "Accept: application/json" $SEAFILE_URL/api/v2.1/via-repo-token/download-link/?path=/$url_encoded_file_name`
temp_file="$SD_CARD_TMP_DIRECTORY/downloaded.tmp"
echo " - downloading..."
curl --silent `echo $download_url | tr -d '"'` -o $temp_file
if [ $? == 0 ]; then
current_orientation=`gm identify -format '%[EXIF:orientation]' $temp_file`
if [ $? == 0 ] && [ -n "$current_orientation" ] && \
[ "$current_orientation" != "unknown" ] && [ "$current_orientation" -gt 4 ]; then
# 90 or 270 degrees rotation
resize_resolution=`echo $FRAME_RESOLUTION | awk -Fx '{print $2"x"$1}'`
else
resize_resolution=$FRAME_RESOLUTION
fi
echo " - resizing and rotating..."
MAGICK_TMPDIR="$SD_CARD_TMP_DIRECTORY" gm convert -scale $resize_resolution -auto-orient $temp_file "$file_name"
else
echo " FAILED"
fi
rm -f $temp_file
done
}
function shuffle_images() {
[ -d "$SD_CARD_IMAGE_DIRECTORY" ] || fail "Can't find image directory '$SD_CARD_IMAGE_DIRECTORY'"
unshuffle_images
echo "Shuffling images..."
counter=0
find "$SD_CARD_IMAGE_DIRECTORY" -maxdepth 1 -type f ! -name "unshuffle.sh" | shuf | while read line; do
# Executed in a sub shell
if [ "$line" == "$SD_CARD_SWAP_FILE" ]; then
continue
fi
cd $SD_CARD_IMAGE_DIRECTORY
filename=`basename "$line"`
mv "$filename" "`printf '%04d' $counter`_$filename"
echo mv "'`printf '%04d' $counter`_$filename'" "'$filename'" >> unshuffle.sh
let counter=$counter+1
done
}
function unshuffle_images() {
[ -d "$SD_CARD_IMAGE_DIRECTORY" ] || fail "Can't find image directory '$SD_CARD_IMAGE_DIRECTORY'"
( # Use a sub shell to avoid messing up cwd
cd $SD_CARD_IMAGE_DIRECTORY
if [ -f unshuffle.sh ]; then
echo "Unshuffling images..."
/bin/sh unshuffle.sh
rm -f unshuffle.sh
fi
)
}