-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage_eink_system.sh
More file actions
509 lines (441 loc) · 14.7 KB
/
manage_eink_system.sh
File metadata and controls
509 lines (441 loc) · 14.7 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#!/bin/bash
# E-ink Display System Management Script
# Provides easy commands for managing the system without requiring reboots
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Detect virtual environment location
if [ -d "$SCRIPT_DIR/eink_env" ]; then
VENV_PATH="$SCRIPT_DIR/eink_env"
echo "Found venv in project folder: $VENV_PATH"
elif [ -d "$HOME/eink_env" ]; then
VENV_PATH="$HOME/eink_env"
echo "Found venv in home directory: $VENV_PATH"
else
echo "❌ Virtual environment not found in $SCRIPT_DIR/eink_env or $HOME/eink_env"
echo "Please create it first with: python3 -m venv eink_env"
exit 1
fi
SERVICE_NAME="eink-display.service"
DISPLAY_PID_FILE="/tmp/eink_display.pid"
SERVER_PID_FILE="/tmp/eink_server.pid"
LOG_FILE="/tmp/eink_management.log"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
NC='\033[0m' # No Color
# Logging function
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
print_header() {
echo -e "${BLUE}📟 E-ink Display System Manager${NC}"
echo "======================================"
}
# Function to check if a process is running
is_process_running() {
local pid_file="$1"
if [ -f "$pid_file" ]; then
local pid=$(cat "$pid_file")
kill -0 "$pid" 2>/dev/null
else
return 1
fi
}
# Function to get process status
get_process_status() {
local pid_file="$1"
local process_name="$2"
if is_process_running "$pid_file"; then
local pid=$(cat "$pid_file")
echo -e "${GREEN}✅ $process_name: RUNNING${NC} (PID: $pid)"
return 0
else
echo -e "${RED}❌ $process_name: STOPPED${NC}"
return 1
fi
}
# Function to check if run_eink_system.py is running
is_run_eink_system_running() {
pgrep -f "run_eink_system.py" >/dev/null 2>&1
}
# Function to get combined system status
get_combined_system_status() {
local display_status=0
local server_status=0
# Check if run_eink_system.py is running
if is_run_eink_system_running; then
local pid=$(pgrep -f "run_eink_system.py")
echo -e "${GREEN}✅ Display Monitor: RUNNING${NC} (via run_eink_system.py, PID: $pid)"
echo -e "${GREEN}✅ Upload Server: RUNNING${NC} (via run_eink_system.py, PID: $pid)"
return 0
else
# Fall back to individual PID file checks
get_process_status "$DISPLAY_PID_FILE" "Display Monitor"
display_status=$?
get_process_status "$SERVER_PID_FILE" "Upload Server"
server_status=$?
return $((display_status + server_status))
fi
}
# Function to check systemd service status
get_systemd_status() {
if systemctl is-active --quiet "$SERVICE_NAME"; then
echo -e "${GREEN}✅ Systemd Service: RUNNING${NC}"
return 0
else
echo -e "${RED}❌ Systemd Service: STOPPED${NC}"
return 1
fi
}
# Function to start the system
start_system() {
print_header
echo -e "${GREEN}🚀 Starting E-ink Display System...${NC}"
log_message "Starting e-ink system"
# Check if already running
if get_systemd_status >/dev/null 2>&1 || \
(is_process_running "$DISPLAY_PID_FILE" && is_process_running "$SERVER_PID_FILE"); then
echo -e "${YELLOW}⚠️ System is already running${NC}"
echo "Use 'restart' to restart the system"
return 0
fi
# Try systemd service first
if systemctl is-enabled --quiet "$SERVICE_NAME" 2>/dev/null; then
echo -e "${BLUE}🔧 Starting via systemd service...${NC}"
systemctl start "$SERVICE_NAME"
sleep 3
if systemctl is-active --quiet "$SERVICE_NAME"; then
echo -e "${GREEN}✅ System started successfully via systemd${NC}"
log_message "System started successfully via systemd"
return 0
else
echo -e "${YELLOW}⚠️ Systemd start failed, trying manual start${NC}"
fi
fi
# Fall back to manual start
echo -e "${BLUE}🔄 Starting manually...${NC}"
# Check virtual environment
if [ ! -d "$VENV_PATH" ]; then
echo -e "${RED}❌ Virtual environment not found${NC}"
return 1
fi
# Create log directories
mkdir -p "$SCRIPT_DIR/logs"
# Activate virtual environment
source "$VENV_PATH/bin/activate"
# Start display monitor in background with proper logging
echo -e "${YELLOW}🖥️ Starting display monitor...${NC}"
nohup python "$SCRIPT_DIR/display_latest.py" > "$SCRIPT_DIR/logs/display.log" 2>&1 &
DISPLAY_PID=$!
echo $DISPLAY_PID > "$DISPLAY_PID_FILE"
log_message "Display monitor started with PID: $DISPLAY_PID"
# Start upload server in background with proper logging
echo -e "${YELLOW}🌐 Starting upload server...${NC}"
nohup python "$SCRIPT_DIR/upload_server.py" > "$SCRIPT_DIR/logs/server.log" 2>&1 &
SERVER_PID=$!
echo $SERVER_PID > "$SERVER_PID_FILE"
log_message "Upload server started with PID: $SERVER_PID"
sleep 3
# Check if processes are running
if is_process_running "$DISPLAY_PID_FILE" && is_process_running "$SERVER_PID_FILE"; then
echo -e "${GREEN}✅ System started successfully manually${NC}"
echo -e "${BLUE}📋 Logs available at:${NC}"
echo -e " Display: $SCRIPT_DIR/logs/display.log"
echo -e " Server: $SCRIPT_DIR/logs/server.log"
log_message "System started successfully manually"
return 0
else
echo -e "${RED}❌ Failed to start system${NC}"
log_message "ERROR: Failed to start system"
return 1
fi
}
# Function to stop the system
stop_system() {
print_header
echo -e "${YELLOW}🛑 Stopping E-ink Display System...${NC}"
log_message "Stopping e-ink system"
# Stop systemd service if running
if systemctl is-active --quiet "$SERVICE_NAME"; then
echo -e "${BLUE}🔧 Stopping systemd service...${NC}"
systemctl stop "$SERVICE_NAME"
sleep 3
fi
# Stop manual processes
if [ -f "$DISPLAY_PID_FILE" ]; then
local pid=$(cat "$DISPLAY_PID_FILE")
if kill -0 "$pid" 2>/dev/null; then
echo -e "${YELLOW}🖥️ Stopping display monitor (PID: $pid)...${NC}"
kill -TERM "$pid"
sleep 2
if kill -0 "$pid" 2>/dev/null; then
kill -KILL "$pid"
fi
fi
rm -f "$DISPLAY_PID_FILE"
fi
if [ -f "$SERVER_PID_FILE" ]; then
local pid=$(cat "$SERVER_PID_FILE")
if kill -0 "$pid" 2>/dev/null; then
echo -e "${YELLOW}🌐 Stopping upload server (PID: $pid)...${NC}"
kill -TERM "$pid"
sleep 2
if kill -0 "$pid" 2>/dev/null; then
kill -KILL "$pid"
fi
fi
rm -f "$SERVER_PID_FILE"
fi
echo -e "${GREEN}✅ System stopped${NC}"
log_message "System stopped"
}
# Function to restart the system
restart_system() {
print_header
echo -e "${PURPLE}🔄 Restarting E-ink Display System...${NC}"
log_message "Restarting e-ink system"
stop_system
sleep 2
start_system
}
# Function to show status
show_status() {
print_header
echo -e "${BLUE}📊 System Status:${NC}"
echo "=================="
# Check systemd service
get_systemd_status
# Check combined system status
get_combined_system_status
echo ""
echo -e "${BLUE}🔧 Hardware Status:${NC}"
echo "=================="
# Check SPI interface
if [ -e "/dev/spidev0.0" ]; then
echo -e "${GREEN}✅ SPI Interface: Available${NC}"
else
echo -e "${RED}❌ SPI Interface: Not Available${NC}"
fi
# Check GPIO access
if groups | grep -q gpio; then
echo -e "${GREEN}✅ GPIO Access: Available${NC}"
else
echo -e "${YELLOW}⚠️ GPIO Access: Limited${NC}"
fi
# Check virtual environment
if [ -d "$VENV_PATH" ]; then
echo -e "${GREEN}✅ Virtual Environment: Available at $VENV_PATH${NC}"
else
echo -e "${RED}❌ Virtual Environment: Not Found${NC}"
fi
# Check watched folder
local watched_folder="$HOME/watched_files"
if [ -d "$watched_folder" ]; then
local file_count=$(find "$watched_folder" -maxdepth 1 -type f | wc -l)
echo -e "${GREEN}✅ Watched Folder: $watched_folder ($file_count files)${NC}"
else
echo -e "${YELLOW}⚠️ Watched Folder: Not Found${NC}"
fi
}
# Function to show logs
show_logs() {
print_header
echo -e "${BLUE}📋 System Logs:${NC}"
echo "================"
# Show systemd service logs
if systemctl is-active --quiet "$SERVICE_NAME" 2>/dev/null; then
echo -e "${YELLOW}🔧 Systemd Service Logs:${NC}"
journalctl -u "$SERVICE_NAME" --no-pager -n 20
echo ""
fi
# Show management logs
if [ -f "$LOG_FILE" ]; then
echo -e "${YELLOW}🔄 Management Logs:${NC}"
tail -n 20 "$LOG_FILE"
echo ""
fi
# Show application logs
if [ -f "$SCRIPT_DIR/logs/display.log" ]; then
echo -e "${YELLOW}🖥️ Display Monitor Logs:${NC}"
tail -n 15 "$SCRIPT_DIR/logs/display.log"
echo ""
fi
if [ -f "$SCRIPT_DIR/logs/server.log" ]; then
echo -e "${YELLOW}🌐 Upload Server Logs:${NC}"
tail -n 15 "$SCRIPT_DIR/logs/server.log"
echo ""
fi
# Show recent file activity
local watched_folder="$HOME/watched_files"
if [ -d "$watched_folder" ]; then
echo -e "${YELLOW}📁 Recent File Activity:${NC}"
find "$watched_folder" -maxdepth 1 -type f -printf "%T@ %p\n" | sort -nr | head -5 | while read timestamp file; do
local date=$(date -d "@${timestamp%.*}" '+%Y-%m-%d %H:%M:%S')
local filename=$(basename "$file")
echo " $date - $filename"
done
fi
}
# Function to follow logs in real-time
follow_logs() {
print_header
echo -e "${BLUE}📋 Following Logs in Real-Time:${NC}"
echo "Press Ctrl+C to stop following logs"
echo "=================================="
# Check which log files exist
local log_files=()
if [ -f "$SCRIPT_DIR/logs/display.log" ]; then
log_files+=("$SCRIPT_DIR/logs/display.log")
fi
if [ -f "$SCRIPT_DIR/logs/server.log" ]; then
log_files+=("$SCRIPT_DIR/logs/server.log")
fi
if [ -f "$LOG_FILE" ]; then
log_files+=("$LOG_FILE")
fi
if [ ${#log_files[@]} -eq 0 ]; then
echo -e "${RED}❌ No log files found${NC}"
return 1
fi
# Follow all log files
if [ ${#log_files[@]} -eq 1 ]; then
echo -e "${YELLOW}Following: ${log_files[0]}${NC}"
tail -f "${log_files[0]}"
else
echo -e "${YELLOW}Following multiple log files:${NC}"
for log_file in "${log_files[@]}"; do
echo " - $log_file"
done
echo ""
tail -f "${log_files[@]}"
fi
}
# Function to clear display
clear_display() {
print_header
echo -e "${YELLOW}🧹 Clearing E-ink Display...${NC}"
log_message "Clearing display"
if [ -d "$VENV_PATH" ]; then
source "$VENV_PATH/bin/activate"
if python "$SCRIPT_DIR/clear_display.py"; then
echo -e "${GREEN}✅ Display cleared successfully${NC}"
log_message "Display cleared successfully"
else
echo -e "${RED}❌ Failed to clear display${NC}"
log_message "ERROR: Failed to clear display"
return 1
fi
else
echo -e "${RED}❌ Virtual environment not found${NC}"
return 1
fi
}
# Function to show IP address
show_ip() {
print_header
echo -e "${BLUE}🌐 Network Information:${NC}"
echo "======================"
# Get IP address
local ip_address=$(hostname -I | awk '{print $1}')
local hostname=$(hostname)
echo -e "${GREEN}🏠 Hostname:${NC} $hostname"
echo -e "${GREEN}📡 IP Address:${NC} $ip_address"
echo -e "${GREEN}🌐 Web Interface:${NC} http://$ip_address:5000"
# Check if web interface is accessible
if is_process_running "$SERVER_PID_FILE"; then
echo -e "${GREEN}✅ Web Interface: Running${NC}"
else
echo -e "${RED}❌ Web Interface: Not Running${NC}"
fi
}
# Function to cleanup orphaned processes
cleanup() {
print_header
echo -e "${YELLOW}🧹 Cleaning up orphaned processes...${NC}"
log_message "Cleaning up orphaned processes"
# Find and kill orphaned Python processes
local orphaned_pids=$(pgrep -f "display_latest.py\|upload_server.py\|run_eink_system.py" | grep -v $$)
if [ -n "$orphaned_pids" ]; then
echo -e "${YELLOW}Found orphaned processes:${NC}"
for pid in $orphaned_pids; do
echo " PID: $pid"
kill -TERM "$pid" 2>/dev/null
sleep 1
if kill -0 "$pid" 2>/dev/null; then
kill -KILL "$pid" 2>/dev/null
fi
done
else
echo -e "${GREEN}No orphaned processes found${NC}"
fi
# Clean up stale PID files
rm -f "$DISPLAY_PID_FILE" "$SERVER_PID_FILE"
echo -e "${GREEN}✅ Cleanup completed${NC}"
log_message "Cleanup completed"
}
# Function to show help
show_help() {
print_header
echo "Usage: $0 [command]"
echo ""
echo "Commands:"
echo " start - Start the e-ink system"
echo " stop - Stop the e-ink system"
echo " restart - Restart the e-ink system"
echo " status - Show system status"
echo " logs - Show recent logs"
echo " follow - Follow logs in real-time"
echo " clear - Clear the e-ink display"
echo " ip - Show network information"
echo " cleanup - Clean up orphaned processes"
echo " help - Show this help message"
echo ""
echo "Examples:"
echo " $0 start # Start the system"
echo " $0 status # Check system status"
echo " $0 restart # Restart the system"
echo " $0 logs # View recent logs"
echo " $0 follow # Follow logs in real-time"
echo ""
echo "This script manages the e-ink system without requiring system reboots."
}
# Main script logic
case "${1:-help}" in
start)
start_system
;;
stop)
stop_system
;;
restart)
restart_system
;;
status)
show_status
;;
logs)
show_logs
;;
follow)
follow_logs
;;
clear)
clear_display
;;
ip)
show_ip
;;
cleanup)
cleanup
;;
help|--help|-h)
show_help
;;
*)
echo -e "${RED}❌ Unknown command: $1${NC}"
echo "Use '$0 help' for usage information"
exit 1
;;
esac