-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.py
694 lines (613 loc) · 32.6 KB
/
basic.py
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
#!/usr/bin/python
# -*- coding:utf-8 -*-
import sys
import os
from pathlib import Path
import logging
from display_adapter import display_full_refresh, initialize_display, display_cleanup
import time
from datetime import datetime, timedelta
from weather.display import WeatherService, draw_weather_display
from bus_service import BusService, update_display
import importlib
import log_config
import random
import traceback
from debug_server import start_debug_server
from wifi_manager import is_connected, no_wifi_loop, get_hostname
from display_adapter import return_display_lock
import threading
import math
from flights import check_flights, gather_flights_within_radius, update_display_with_flights, enhance_flight_data
from threading import Lock, Event
from PIL import Image
logger = logging.getLogger(__name__)
# Set logging level for PIL.PngImagePlugin and urllib3.connectionpool to warning
logging.getLogger('PIL.PngImagePlugin').setLevel(logging.WARNING)
logging.getLogger('urllib3.connectionpool').setLevel(logging.WARNING)
display_lock = return_display_lock() # Global lock for display operations
DISPLAY_REFRESH_INTERVAL = int(os.getenv("refresh_interval", 90))
DISPLAY_REFRESH_MINIMAL_TIME = int(os.getenv("refresh_minimal_time", 30))
DISPLAY_REFRESH_FULL_INTERVAL = int(os.getenv("refresh_full_interval", 3600))
WEATHER_UPDATE_INTERVAL = int(os.getenv("refresh_weather_interval", 600))
BUS_DATA_MAX_AGE = max(90, DISPLAY_REFRESH_INTERVAL) # Ensure bus data doesn't become stale before next refresh
weather_enabled = True if os.getenv("weather_enabled", "true").lower() == "true" else False
transit_enabled = True if os.getenv("transit_enabled", "true").lower() == "true" else False
HOTSPOT_ENABLED = os.getenv('hotspot_enabled', 'true').lower() == 'true'
hostname = get_hostname()
HOTSPOT_SSID = os.getenv('hotspot_ssid', f'PiHotspot-{hostname}')
HOTSPOT_PASSWORD = os.getenv('hotspot_password', 'YourPassword')
DISPLAY_SCREEN_ROTATION = int(os.getenv('screen_rotation', 90))
# Default coordinates (Brussels)
DEFAULT_LAT = 50.8503
DEFAULT_LNG = 4.3517
try:
COORDINATES_LAT = float(os.getenv('Coordinates_LAT', DEFAULT_LAT))
COORDINATES_LNG = float(os.getenv('Coordinates_LNG', DEFAULT_LNG))
except (ValueError, TypeError):
logger.warning("Invalid or missing coordinates, using default coordinates (Brussels)")
COORDINATES_LAT = DEFAULT_LAT
COORDINATES_LNG = DEFAULT_LNG
flights_enabled = True if os.getenv('flights_enabled', 'false').lower() == 'true' else False
aeroapi_enabled = True if os.getenv('aeroapi_enabled', 'false').lower() == 'true' else False
flight_check_interval = max(1, int(os.getenv('flight_check_interval', 5)))
FLIGHT_MAX_RADIUS = int(os.getenv('flight_max_radius', 3))
flight_altitude_convert_feet = True if os.getenv('flight_altitude_convert_feet', 'false').lower() == 'true' else False
iss_enabled = True if os.getenv('iss_enabled', 'true').lower() == 'true' else False
if not weather_enabled:
logger.warning("Weather is not enabled, weather data will not be displayed. Please set OPENWEATHER_API_KEY in .env to enable it.")
class WeatherManager:
def __init__(self):
self.weather_service = WeatherService() if weather_enabled else None
# Initialize with a default WeatherData object
from weather.models import WeatherData, CurrentWeather, WeatherCondition
self.weather_data = WeatherData(
current=CurrentWeather(
temperature=0.0,
feels_like=0.0,
humidity=0,
pressure=0.0,
condition=WeatherCondition(
description="Unknown",
icon="unknown"
)
),
is_day=True
)
self.last_update = None
self._lock = threading.Lock()
self._thread = None
self._stop_event = threading.Event()
logger.info("WeatherManager initialized")
def start(self):
if weather_enabled and self.weather_service:
logger.info("Starting weather manager thread...")
self._thread = threading.Thread(target=self._update_weather, daemon=True)
self._thread.start()
logger.info("Weather manager thread started")
# Get initial weather data
logger.info("Getting initial weather data...")
self._update_weather_once()
else:
logger.info("Weather manager not started (weather disabled or service unavailable)")
def _update_weather_once(self):
"""Get weather data once"""
try:
if self.weather_service:
logger.debug("Fetching new weather data...")
new_data = self.weather_service.get_weather_data()
logger.debug(f"Received weather data: {new_data}")
# Add detailed logging for sunshine hours
if new_data and new_data.daily_forecast:
logger.info(f"Number of daily forecasts: {len(new_data.daily_forecast)}")
for i, forecast in enumerate(new_data.daily_forecast):
logger.info(f"Day {i} sunshine duration: {forecast.sunshine_duration}")
else:
logger.warning("No daily forecast data available")
with self._lock:
if new_data: # Only update if we got valid data
self.weather_data = new_data
self.last_update = datetime.now()
logger.info(f"Weather data updated at {self.last_update.strftime('%H:%M:%S')}")
logger.debug(f"Current temperature: {new_data.current.temperature}°C")
# Log the state after update
if self.weather_data.daily_forecast:
logger.info(f"Stored sunshine duration: {self.weather_data.daily_forecast[0].sunshine_duration}")
else:
logger.warning("Received empty weather data")
except Exception as e:
logger.error(f"Error updating weather: {e}")
logger.debug(traceback.format_exc())
def _update_weather(self):
"""Weather update loop"""
logger.info("Weather update loop started")
while not self._stop_event.is_set():
try:
current_time = datetime.now()
if not self.last_update:
logger.debug("No previous update, updating weather now")
self._update_weather_once()
else:
time_since_update = (current_time - self.last_update).total_seconds()
logger.debug(f"Time since last weatherupdate: {time_since_update:.1f} seconds")
if time_since_update >= WEATHER_UPDATE_INTERVAL:
logger.debug("Update interval reached, updating weather")
self._update_weather_once()
else:
logger.debug(f"Next update in {WEATHER_UPDATE_INTERVAL - time_since_update:.1f} seconds")
except Exception as e:
logger.error(f"Error in weather update loop: {e}")
logger.debug(traceback.format_exc())
sleep_time = min(60, WEATHER_UPDATE_INTERVAL)
logger.debug(f"Sleeping for {sleep_time} seconds")
time.sleep(sleep_time)
def get_weather(self):
"""Get current weather data"""
if not weather_enabled:
logger.debug("Weather is disabled, returning None")
return None
with self._lock:
logger.debug(f"Returning weather data from {self.last_update.strftime('%H:%M:%S') if self.last_update else 'never'}")
return self.weather_data
def stop(self):
if self._thread:
logger.info("Stopping weather manager thread...")
self._stop_event.set()
try:
self._thread.join(timeout=1.0)
logger.info("Weather manager thread stopped")
except TimeoutError:
logger.warning("Weather thread did not stop cleanly")
def get_weather_data(self):
"""Return the current weather data with thread safety"""
with self._lock:
return self.weather_data
class BusManager:
def __init__(self):
self.bus_service = BusService() if transit_enabled else None
self.bus_data = {
'data': [],
'error_message': None if transit_enabled else "Transit display is disabled",
'stop_name': None
}
self.last_update = None
self._lock = threading.Lock()
logger.info("BusManager initialized" + (" (disabled)" if not transit_enabled else ""))
def fetch_data(self):
"""Fetch new bus data on demand"""
if not transit_enabled:
return
try:
logger.debug("Fetching new bus data...")
data, error_message, stop_name = self.bus_service.get_waiting_times()
with self._lock:
self.bus_data = {
'data': data,
'error_message': error_message,
'stop_name': stop_name
}
self.last_update = datetime.now()
logger.info(f"Bus data updated at {self.last_update.strftime('%H:%M:%S')}")
if data:
logger.debug(f"Received {len(data)} bus entries")
if error_message:
logger.warning(f"Bus error message: {error_message}")
except Exception as e:
logger.error(f"Error updating bus data: {e}")
logger.debug(traceback.format_exc())
def get_bus_data(self):
"""Get current bus data"""
with self._lock:
current_time = datetime.now()
if self.last_update is None:
logger.warning("No bus data has been received yet")
return [], "Waiting for initial bus data...", None
time_since_update = (current_time - self.last_update).total_seconds()
if time_since_update > BUS_DATA_MAX_AGE:
logger.warning(f"Bus data is stale (last update: {time_since_update:.0f} seconds ago)")
return [], f"Data stale ({time_since_update:.0f}s old)", self.bus_data['stop_name']
logger.debug(f"Returning bus data from {self.last_update.strftime('%H:%M:%S')} (age: {time_since_update:.1f}s)")
return (
self.bus_data['data'],
self.bus_data['error_message'],
self.bus_data['stop_name']
)
def get_valid_bus_data(self):
"""Get current bus data if it's valid"""
data, error_message, stop_name = self.get_bus_data()
if error_message:
return None
return data
def get_stop_name(self):
"""Get the current stop name"""
with self._lock:
return self.bus_data.get('stop_name')
class DisplayManager:
def __init__(self, epd):
self.epd = epd
self.update_count = 0
self.last_weather_data = None
self.last_weather_update = datetime.now()
self.last_display_update = datetime.now()
self.last_flight_update = datetime.now()
self.in_weather_mode = False
self.weather_manager = WeatherManager()
self.bus_manager = BusManager()
if transit_enabled and self.bus_manager and self.bus_manager.bus_service:
self.bus_manager.bus_service.set_epd(epd) # Set the EPD object for the bus service
self._display_lock = threading.Lock()
self._check_data_thread = None
self._flight_thread = None
self._stop_event = threading.Event()
self.min_refresh_interval = int(os.getenv("refresh_minimal_time", 30))
self.flight_check_interval = int(os.getenv("flight_check_interval", 10))
self.flights_enabled = os.getenv("flights_enabled", "false").lower() == "true"
# Default to Brussels coordinates if not set
self.coordinates_lat = float(os.getenv('Coordinates_LAT', '50.8503'))
self.coordinates_lng = float(os.getenv('Coordinates_LNG', '4.3517'))
self.flight_getter = None
self.in_flight_mode = False
self.flight_mode_start = None
self.flight_mode_duration = 30 # Duration in seconds for flight mode
self.flight_mode_cooldown = 30 # Cooldown period before showing flights again
self.last_flight_mode_end = None
self.flight_monitoring_paused = False
self._flight_lock = threading.Lock()
self.iss_enabled = os.getenv("iss_enabled", "true").lower() == "true"
self.iss_priority = os.getenv("iss_priority", "false").lower() == "true"
self.iss_tracker = None
self._iss_thread = None
self.in_iss_mode = False
self.prefetch_offset = 10 # seconds before display update to fetch new data
self.display_interval = DISPLAY_REFRESH_INTERVAL
self.next_update_time = None
self.next_prefetch_time = None
logger.info(f"DisplayManager initialized with min refresh interval: {self.min_refresh_interval}s")
logger.info(f"DisplayManager initialized with coordinates: {self.coordinates_lat}, {self.coordinates_lng}")
logger.info(f"DisplayManager initialized with flight mode duration: {self.flight_mode_duration}s")
def start(self):
logger.info("Starting display manager components...")
# Start weather manager
self.weather_manager.start()
# Initialize flight monitoring if enabled
if self.flights_enabled:
self.initialize_flight_monitoring()
logger.info("Flight monitoring initialized")
# Initialize ISS tracking if enabled
self.initialize_iss_tracking()
# Get initial bus data
self.bus_manager.fetch_data()
time.sleep(2)
# Force first display update
self._force_display_update()
# Start the update checker thread
self._check_data_thread = threading.Thread(target=self._check_display_updates, daemon=True)
self._check_data_thread.start()
logger.info("Display manager started - all components running")
def initialize_flight_monitoring(self):
"""Initialize flight monitoring with cooldown control"""
search_radius = FLIGHT_MAX_RADIUS * 2
self.flight_getter = gather_flights_within_radius(
COORDINATES_LAT,
COORDINATES_LNG,
search_radius,
FLIGHT_MAX_RADIUS,
flight_check_interval=flight_check_interval,
aeroapi_enabled=aeroapi_enabled
)
self.flight_thread = threading.Thread(
target=self._check_flights,
daemon=True
)
self.flight_thread.start()
def initialize_iss_tracking(self):
if not self.iss_enabled:
return
try:
from iss import ISSTracker
self.iss_tracker = ISSTracker()
self._iss_thread = threading.Thread(
target=self._run_iss_tracker,
name="ISS_Tracker",
daemon=True
)
self._iss_thread.start()
logger.info("ISS tracking initialized")
except Exception as e:
logger.error(f"Failed to initialize ISS tracking: {e}")
self.iss_enabled = False
def _run_iss_tracker(self):
"""Run ISS tracker with display mode management"""
def on_pass_start():
if self.iss_priority:
# Force exit from flight mode if needed
if self.in_flight_mode:
logger.info("ISS pass starting - forcing exit from flight mode")
self.in_flight_mode = False
self.flight_mode_start = None
self.in_iss_mode = True
def on_pass_end():
self.in_iss_mode = False
self._force_display_update()
try:
self.iss_tracker.run(self.epd, on_pass_start, on_pass_end)
except Exception as e:
logger.error(f"Error in ISS tracker: {e}")
def _can_enter_flight_mode(self, current_time):
"""Check if we can enter flight mode based on cooldown"""
with self._flight_lock:
if self.last_flight_mode_end is None:
return True
cooldown_passed = (current_time - self.last_flight_mode_end).total_seconds() >= self.flight_mode_cooldown
logger.debug(f"Time since last flight mode: {(current_time - self.last_flight_mode_end).total_seconds():.1f}s, cooldown: {self.flight_mode_cooldown}s")
return cooldown_passed
def _check_flights(self):
"""Monitor flights and display when relevant"""
while not self._stop_event.is_set():
try:
current_time = datetime.now()
# First, check if we're in cooldown
if not self._can_enter_flight_mode(current_time):
logger.debug("In flight cooldown period, skipping flight processing entirely")
# Sleep for a longer period during cooldown
time.sleep(DISPLAY_REFRESH_MINIMAL_TIME)
continue
# Only check flights if enough time has passed since last check
if (current_time - self.last_flight_update).total_seconds() >= self.flight_check_interval:
if self.flight_getter:
flights_within_3km = self.flight_getter()
if flights_within_3km:
logger.info(f"Flights within the set radius: {len(flights_within_3km)}")
closest_flight = flights_within_3km[0]
# If the closest flight is on the ground, don't show it and get the next one
if closest_flight.get('altitude') == "ground":
logger.debug("Closest flight is on the ground. Looking for next airborne flight.")
found_airborne = False
for flight in flights_within_3km[1:]:
if flight.get('altitude') != "ground":
closest_flight = flight
found_airborne = True
break
if not found_airborne:
logger.debug("No airborne flights found in the list.")
closest_flight = None
# Enhance the flight data with additional details
if closest_flight:
logger.debug(f"Closest flight: {closest_flight}")
enhanced_flight = enhance_flight_data(closest_flight)
logger.debug(f"Enhanced flight: {enhanced_flight}")
with self._display_lock:
with self._flight_lock:
if not self.in_flight_mode:
logger.debug("Entering flight mode")
self.in_flight_mode = True
self.flight_mode_start = current_time
logger.debug(f"Flight mode start time: {self.flight_mode_start}")
else:
logger.debug("Updating flight display while in flight mode")
update_display_with_flights(self.epd, [enhanced_flight])
self.last_display_update = datetime.now()
self.last_flight_update = current_time
else:
logger.debug("No flights found within the radius")
# Sleep for the flight check interval
time.sleep(self.flight_check_interval)
except Exception as e:
logger.error(f"Error in flight check loop: {e}")
logger.debug(traceback.format_exc())
time.sleep(self.flight_check_interval)
def _force_display_update(self):
"""Force an immediate display update"""
logger.info("Forcing initial display update...")
try:
with self._display_lock:
bus_data, error_message, stop_name = self.bus_manager.get_bus_data()
weather_data = self.weather_manager.get_weather_data() if weather_enabled else None
valid_bus_data = [
bus for bus in bus_data
if any(time and time != "--" and time != "" for time in bus["times"])
]
# Check if we have any bus data at all
if not bus_data and not error_message and weather_enabled and weather_data:
logger.info("Initial display: no bus data available, showing weather data")
if not self.in_weather_mode:
# We're switching to weather mode, set base image for partial updates
self.in_weather_mode = True
draw_weather_display(self.epd, weather_data, set_base_image=True)
else:
draw_weather_display(self.epd, weather_data)
elif valid_bus_data and not error_message:
logger.info(f"Initial display: showing bus data ({len(valid_bus_data)} entries)")
if self.in_weather_mode:
# We're switching from weather mode, set base image for partial updates
self.in_weather_mode = False
update_display(self.epd, weather_data, valid_bus_data, error_message, stop_name, set_base_image=True)
else:
update_display(self.epd, weather_data, valid_bus_data, error_message, stop_name)
elif weather_enabled and weather_data:
logger.info("Initial display: showing weather data")
if not self.in_weather_mode:
# We're switching to weather mode, set base image for partial updates
self.in_weather_mode = True
draw_weather_display(self.epd, weather_data, set_base_image=True)
else:
draw_weather_display(self.epd, weather_data)
else:
logger.warning("No valid data for initial display")
self.last_display_update = datetime.now()
except Exception as e:
logger.error(f"Error in initial display update: {e}")
logger.debug(traceback.format_exc())
def _can_update_display(self, current_time):
"""Check if enough time has passed since last display update"""
time_since_last_update = (current_time - self.last_display_update).total_seconds()
return time_since_last_update >= self.min_refresh_interval
def _schedule_next_update(self):
"""Schedule the next update and prefetch times"""
current_time = datetime.now()
self.next_update_time = current_time + timedelta(seconds=self.display_interval)
if transit_enabled:
self.next_prefetch_time = self.next_update_time - timedelta(seconds=self.prefetch_offset)
logger.debug(f"Next prefetch scheduled for {self.next_prefetch_time.strftime('%H:%M:%S')}")
logger.debug(f"Next update scheduled for {self.next_update_time.strftime('%H:%M:%S')}")
def _check_display_updates(self):
"""Continuously check for updates and switch modes as needed"""
self._schedule_next_update() # Initial schedule
last_flight_log = 0 # Add this back
while not self._stop_event.is_set():
try:
if self.in_iss_mode:
# Skip normal updates during ISS passes
time.sleep(1)
continue
current_time = datetime.now()
# Handle flight mode checks
with self._flight_lock:
if self.in_flight_mode:
time_in_flight_mode = (current_time - self.flight_mode_start).total_seconds()
if time_in_flight_mode - last_flight_log >= 5:
logger.debug(f"Time in flight mode: {time_in_flight_mode:.1f}s of {self.flight_mode_duration}s")
last_flight_log = time_in_flight_mode
if time_in_flight_mode >= self.flight_mode_duration:
logger.info(f"Exiting flight mode after {time_in_flight_mode:.1f} seconds")
self.in_flight_mode = False
self.last_flight_mode_end = current_time
self.flight_mode_start = None
logger.info(f"Starting flight cooldown period of {self.flight_mode_cooldown} seconds")
# Force an immediate update to normal display
self._force_display_update()
time.sleep(1) # Add sleep when in flight mode
continue
# Check if it's time to prefetch data
if transit_enabled and current_time >= self.next_prefetch_time:
logger.debug("Prefetching bus data...")
self.bus_manager.fetch_data()
# Check if it's time to update display
if current_time >= self.next_update_time:
logger.debug("Updating display...")
weather_data = self.weather_manager.get_weather_data() if weather_enabled else None
valid_bus_data = self.bus_manager.get_valid_bus_data() if transit_enabled else None
error_message = None
stop_name = self.bus_manager.get_stop_name() if transit_enabled else None
with self._display_lock:
# Check if we have any bus data at all
if not valid_bus_data and not error_message and weather_enabled and weather_data:
logger.info("No bus data available, switching to weather mode...")
if not self.in_weather_mode:
# We're switching to weather mode, set base image for partial updates
self.in_weather_mode = True
draw_weather_display(self.epd, weather_data, set_base_image=True)
else:
draw_weather_display(self.epd, weather_data)
self.last_weather_data = weather_data
self.last_weather_update = current_time
self.last_display_update = datetime.now()
logger.info("Weather display updated successfully")
elif valid_bus_data and not error_message:
logger.info("Updating bus display...")
# Pass the full weather data object to update_display
if self.in_weather_mode:
# We're switching from weather mode, set base image for partial updates
self.in_weather_mode = False
update_display(self.epd, weather_data, valid_bus_data, error_message, stop_name, set_base_image=True)
else:
update_display(self.epd, weather_data, valid_bus_data, error_message, stop_name)
self.last_display_update = datetime.now()
self.update_count += 1
logger.info("Bus display updated successfully")
elif weather_enabled and weather_data:
logger.info("Updating weather display...")
if not self.in_weather_mode:
# We're switching to weather mode, set base image for partial updates
self.in_weather_mode = True
draw_weather_display(self.epd, weather_data, set_base_image=True)
else:
draw_weather_display(self.epd, weather_data)
self.last_weather_data = weather_data
self.last_weather_update = current_time
self.last_display_update = datetime.now()
logger.info("Weather display updated successfully")
# Schedule next update cycle
self._schedule_next_update()
except Exception as e:
logger.error(f"Error in display update checker: {e}")
logger.debug(traceback.format_exc())
# Sleep for a short time to prevent CPU spinning
time.sleep(1)
def needs_full_refresh(self):
return self.update_count >= (DISPLAY_REFRESH_FULL_INTERVAL // DISPLAY_REFRESH_INTERVAL)
def perform_full_refresh(self):
if self.needs_full_refresh():
logger.info("Performing hourly full refresh...")
display_full_refresh(self.epd)
self.update_count = 0
# Reinitialize partial mode if supported
if hasattr(self.epd, 'displayPartial'):
logger.info("Reinitializing partial mode after full refresh")
self.epd.init()
if hasattr(self.epd, 'displayPartBaseImage'):
logger.debug("Setting base image for partial updates")
# Create a blank base image
if self.epd.is_bw_display:
base_image = Image.new('1', (self.epd.height, self.epd.width), 1)
else:
base_image = Image.new('RGB', (self.epd.height, self.epd.width), 'white')
base_image = base_image.rotate(DISPLAY_SCREEN_ROTATION, expand=True)
self.epd.displayPartBaseImage(self.epd.getbuffer(base_image))
def get_next_update_message(self, wait_time):
if self.in_weather_mode:
return f"weather update in {wait_time} seconds"
updates_until_refresh = (DISPLAY_REFRESH_FULL_INTERVAL // DISPLAY_REFRESH_INTERVAL) - self.update_count - 1
return f"public transport update in {wait_time} seconds ({updates_until_refresh} until full refresh)"
def cleanup(self):
logger.info("Starting display manager cleanup...")
self._stop_event.set()
if self.iss_tracker:
self.iss_tracker.stop()
for thread in [self._check_data_thread, self._flight_thread, self._iss_thread]:
if thread:
try:
thread.join(timeout=1.0)
logger.info(f"{thread.name} stopped")
except TimeoutError:
logger.warning(f"{thread.name} did not stop cleanly")
self.weather_manager.stop()
logger.info("Display manager cleanup completed")
def exit_flight_mode(self):
"""Handle exiting flight mode and trigger a display update."""
logger.info("Exiting flight mode")
self.in_flight_mode = False
self.flight_mode_start = None
self.last_flight_mode_end = datetime.now()
# Force an immediate update to normal display
self._force_display_update()
def main():
epd = None
display_manager = None
try:
logger.info("E-ink Display Starting")
start_debug_server()
epd = initialize_display()
if not is_connected():
no_wifi_loop(epd)
display_manager = DisplayManager(epd)
display_manager.start()
# Main loop just keeps the program running and handles interrupts
while True:
time.sleep(1)
except KeyboardInterrupt:
logger.info("Ctrl+C pressed - Cleaning up...")
except Exception as e:
logger.error(f"Main error: {str(e)}\n{traceback.format_exc()}")
finally:
logger.info("Cleaning up...")
if display_manager:
display_manager.cleanup()
if epd is not None:
try:
display_cleanup(epd)
except Exception as e:
logger.error(f"Error during cleanup: {str(e)}\n{traceback.format_exc()}")
sys.exit(0)
if __name__ == "__main__":
main()