-
Notifications
You must be signed in to change notification settings - Fork 0
/
images_slide_show.sh
executable file
·203 lines (174 loc) · 5.91 KB
/
images_slide_show.sh
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
#!/bin/bash
# Function to be executed when the script is terminated
cleanup() {
kill_display_processes
echo "Slideshow terminated."
}
# Trap the EXIT signal and execute the cleanup function
trap cleanup EXIT
# Configuration Variables
SCRIPT_PATH="$(realpath "$(dirname "$0")")"
CONFIG_FILE="$SCRIPT_PATH/app_config.json"
LOGO_PATH="$SCRIPT_PATH/LOGO.png"
UPDATE_CHECK_SCRIPT="$SCRIPT_PATH/file_manager/drive_local_file_manager.py"
CURRENT_FILES_FILE="$SCRIPT_PATH/app_data/current_files.json"
BLACK_IMAGE_FILE="$SCRIPT_PATH/black.png"
OFF_TIME=$(jq -r '.OFF_TIME' $CONFIG_FILE)
OFF_TIME_S=$(date -d"$OFF_TIME" +%s)
ON_TIME=$(jq -r '.ON_TIME' $CONFIG_FILE)
ON_TIME_S=$(date -d"$ON_TIME" +%s)
DISPLAYTIME=$(jq -r '.DISPLAYTIME' $CONFIG_FILE) # in seconds
BLENDTIME=$(jq -r '.BLENDTIME' $CONFIG_FILE) # in milliseconds
PYENV=$(jq -r '.PYENV' $CONFIG_FILE) # path to python virtual environment
# Activate python virtual environment
if [[ -n "$PYENV" ]]; then
source "$PYENV/bin/activate"
fi
# Other Constants
DISPLAYPID=""a
is_first_run=true
# Find an unused TTY
unused_tty=""
for tty in {1..7}; do
if ! who | grep -q "tty$tty"; then
unused_tty=$tty
break
fi
done
if [ -z "$unused_tty" ]; then
echo "No unused TTY found!"
return 1
else
echo "Using TTY $unused_tty"
fi
# Function to turn off the cursor
turn_off_cursor() {
setterm -cursor off
echo -e "\e[?25h"a
}
# Function to kill display processes
kill_display_processes() {
sudo pkill -x "fbi"
sudo pkill -x "cvlc"
}
# Function to check if drive folder has been updated
check_for_updates() {
python "$UPDATE_CHECK_SCRIPT"
return $?
}
# Function to display images and videos in terminal
display() {
local -n image_files=$1
local -n video_files=$2
IMAGE_FILES_COUNT=${#image_files[@]}
kill_display_processes
if [[ ${#image_files[@]} -gt 0 && ${#video_files[@]} -gt 0 ]]; then
while true; do
sudo fbi -a -r 3 -t $DISPLAYTIME --blend $BLENDTIME -vt $unused_tty --noverbose -1 "${image_files[@]}"
sleep $((IMAGE_FILES_COUNT * DISPLAYTIME))
sudo pkill -x "fbi"
cvlc "$video_files"
done
elif [ -n "$image_files" ]; then
sudo fbi -a -r 5 -t $DISPLAYTIME --blend $BLENDTIME -vt $unused_tty --noverbose "${image_files[@]}"
elif [ -n "$video_files" ]; then
#clear
cvlc --loop "$video_files"
fi
}
# Function to display black screen
display_black_screen() {
kill_display_processes
sudo fbi -a -r 5 -vt $unused_tty --noverbose "$BLACK_IMAGE_FILE" &
}
# Function to calculate and sleep until target time
calculate_and_sleep_until_target_time() {
local current_time="$1"
local target_time=""
if [[ "$current_time" -le "$ON_TIME_S" ]]; then
target_time=$ON_TIME
else
target_time="$ON_TIME tomorrow"
fi
local target_timestamp=$(date -d "$target_time" +"%s")
local seconds_until_target=$((target_timestamp - current_time))
kill_display_processes
echo "Sleeping for $seconds_until_target seconds"
sleep $seconds_until_target
}
# Function to handle the main loop
main_loop() {
display_off=false
while true; do
local current_time=$(date +%s)
local changes_detected=0
# Run Python file and get return value to check if drive folder has been updated (1 = yes, 0 = no)
check_for_updates
changes_detected=$?
# If current time is between off and on time, turn off the display
if [[ "$ON_TIME_S" -gt "$OFF_TIME_S" && "$current_time" -lt "$ON_TIME_S" && "$current_time" -gt "$OFF_TIME_S" ]] ||
[[ "$ON_TIME_S" -le "$OFF_TIME_S" && ("$current_time" -gt "$OFF_TIME_S" || "$current_time" -lt "$ON_TIME_S") ]]; then
echo "Going to sleep as no one is here"
display_black_screen
display_off=true
calculate_and_sleep_until_target_time "$current_time"
echo "Waking up as someone is here"
elif [[ "$is_first_run" = true || "$changes_detected" = 1 ]]; then
is_first_run=false
# get from file the current images and videos to display
mapfile -t current_images < <(jq -r '.IMAGES[]' "$CURRENT_FILES_FILE")
mapfile -t current_videos < <(jq -r '.VIDEOS[]' "$CURRENT_FILES_FILE")
wait
echo "Images to display: ${current_images[*]}"
echo "Videos to display: ${current_videos[*]}"
# display images and videos if any or changed
if [[ ${#current_images[@]} -gt 0 || ${#current_videos[@]} -gt 0 ]]; then
sudo kill "$DISPLAYPID"
kill_display_processes
display_off=false
display "current_images" "current_videos" &
DISPLAYPID=$!
elif [[ "$display_off" = false ]]; then
kill_display_processes
display_black_screen
display_off=true
fi
fi
sleep 10
done
}
# Function to create a default configuration file
create_default_config() {
# Define the default configuration
default_config=$(jq -n \
--arg td "./app_data/content" \
--argjson ug false \
--arg ga "" \
--arg dd "" \
--arg ot "19:30:00" \
--arg ont "07:30:00" \
--arg dt "10" \
--arg bt "900" \
--arg py "./venv" \
'{
TARGET_DIR: $td,
USE_GDRIVE: $ug,
GOOGLE_API_ACCESS: $ga,
DRIVE_DIR_ID: $dd,
OFF_TIME: $ot,
ON_TIME: $ont,
DISPLAYTIME: $dt,
BLENDTIME: $bt,
PYENV: $py
}')
echo "$default_config" >$CONFIG_FILE
}
# Main scripts
clear
if [ ! -f "$CONFIG_FILE" ]; then
# If not, create a default one
echo "Creating default config file..."
create_default_config
fi
sudo fbi -a -r 5 -t 5 -vt $unused_tty --noverbose "$SCRIPT_PATH/LOGO.png" &
main_loop #>/dev/null 2>/dev/null