-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbus_service.py
1492 lines (1291 loc) · 66.8 KB
/
bus_service.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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from PIL import Image, ImageDraw, ImageFont
from datetime import datetime, timedelta
import niquests as requests
import logging
from typing import List, Dict, Tuple
import os
import dotenv
from dithering import draw_dithered_box, draw_multicolor_dither_with_text
from color_utils import find_optimal_colors
from font_utils import get_font_paths
import log_config
import socket
from display_adapter import DisplayAdapter, return_display_lock
from functools import lru_cache
from threading import Event
from backoff import ExponentialBackoff
from weather.display import load_svg_icon
from weather.models import WeatherData, TemperatureUnit
from weather.icons import ICONS_DIR
import traceback
import json
from pathlib import Path
import threading
import time
logger = logging.getLogger(__name__)
dotenv.load_dotenv(override=True)
fallback_to_schedule = os.getenv("fallback_to_schedule", "false").lower() == "true"
def _get_stop_config():
"""
Get stop configuration from environment variables.
Handles both 'Stops' and 'Stop' variables for backward compatibility.
Case-insensitive: accepts any variation like STOP, stop, Stops, STOPS, etc.
Also accepts variants with a period like 'stop.'
"""
# Get all environment variables
env_vars = dict(os.environ)
# Try to find any variation of 'stops' first
stops_pattern = 'stops'
for key in env_vars:
if key.lower().rstrip('.') == stops_pattern:
return env_vars[key]
# If not found, try to find any variation of 'stop'
stop_pattern = 'stop'
for key in env_vars:
if key.lower().rstrip('.') == stop_pattern:
logger.warning(f"Using '{key}' instead of 'Stops' in .env file. Please update your configuration to use 'Stops' for consistency.")
return env_vars[key]
return None
show_sunshine = os.getenv('show_sunshine_hours', 'true').lower() == 'true'
show_precipitation = os.getenv('show_precipitation', 'true').lower() == 'true'
Stop = _get_stop_config()
Lines = os.getenv("Lines", "") # Default to empty string instead of None
bus_api_base_url = os.getenv("BUS_API_BASE_URL", "http://localhost:5001/")
bus_schedule_url = os.getenv("BUS_SCHEDULE_URL", bus_api_base_url)
pre_load_bus_schedule = os.getenv("PRE_LOAD_BUS_SCHEDULE", "false").lower() == "true"
bus_provider = os.getenv("Provider", "stib")
logging.debug(f"Bus provider: {bus_provider}. Base URL: {bus_api_base_url}. Monitoring lines: {Lines if Lines else 'all'} and stop: {Stop}")
DISPLAY_SCREEN_ROTATION = int(os.getenv('screen_rotation', 90))
weather_enabled = True if os.getenv("weather_enabled", "true").lower() == "true" else False
display_lock = return_display_lock() # Global lock for display operations
# Define our available colors and their RGB values
DISPLAY_COLORS = {
'black': (0, 0, 0),
'white': (255, 255, 255),
'red': (255, 0, 0),
'yellow': (255, 255, 0)
}
def _parse_lines(lines_str: str) -> list:
"""
Parse bus line numbers from environment variable.
Handles different formats:
- Single number: "64"
- Comma-separated list: "64,59"
- Space-separated list: "64 59"
- Mixed format: "64, 59"
- List format: [59, 64]
- String list format: ["59", "64"]
- Preserves leading zeros: "0090" stays "0090"
- Empty string or None: returns empty list (all lines will be shown)
"""
if not lines_str:
logger.info("No specific bus lines configured, will show all available lines")
return []
# Remove leading/trailing whitespace
lines_str = lines_str.strip()
# Handle list-like formats
if lines_str.startswith('[') and lines_str.endswith(']'):
# Remove brackets and split
content = lines_str[1:-1].strip()
# Handle empty list
if not content:
return []
# Split on comma and handle quotes
items = [item.strip().strip('"\'') for item in content.split(',')]
try:
# Verify each item is a valid number but return original string
for item in items:
int(item) # Just to validate it's a number
return items
except ValueError as e:
logger.error(f"Invalid number in list format: {e}")
return []
try:
# Try to parse as a single number (validate but preserve format)
int(lines_str) # Just to validate it's a number
return [lines_str]
except ValueError:
# If that fails, try to split and parse as list
# First replace commas with spaces
cleaned = lines_str.replace(',', ' ')
# Split on whitespace and filter out empty strings
cleaned = [x.strip() for x in cleaned.split() if x.strip()]
# Validate numbers but preserve original format
try:
for line in cleaned:
int(line) # Just to validate it's a number
return cleaned
except ValueError as e:
logger.error(f"Invalid bus line number format: {e}")
return []
class BusService:
def __init__(self):
self.base_url = self._resolve_base_url()
self.schedule_url = self._resolve_schedule_url()
self.provider = os.getenv("Provider", "stib")
self.current_provider = self.provider # Keep track of current active provider
self.provider_config = self._load_provider_config()
logger.debug(f"Bus provider: {self.provider}. Resolved Base URL: {self.base_url}")
self._update_api_urls()
self.stop_id = Stop
logger.debug(f"Stop ID: {self.stop_id}")
self.lines_of_interest = _parse_lines(Lines)
logger.info(f"Monitoring bus lines: {self.lines_of_interest}")
# Initialize separate backoffs for RT and fallback
self._rt_backoff = ExponentialBackoff(initial_backoff=180, max_backoff=3600)
self._fallback_backoff = ExponentialBackoff(initial_backoff=180, max_backoff=3600)
self._stop_event = Event()
self.epd = None # Will be set later
# Start health check and pre-warming thread
self._start_health_check()
def _resolve_base_url(self) -> str:
"""Resolve the base URL, handling .local domains"""
base_url = bus_api_base_url.lower().rstrip('/')
if '.local' in base_url:
try:
# Extract hostname from URL
hostname = base_url.split('://')[1].split(':')[0]
# Try to resolve the IP address
ip = socket.gethostbyname(hostname)
logger.info(f"Resolved {hostname} to {ip}")
# Replace hostname with IP in URL
return base_url.replace(hostname, ip)
except Exception as e:
logger.warning(f"Could not resolve {hostname}, falling back to IP: {e}")
# Fallback to direct IP if resolution fails
return "http://127.0.0.1:5001"
return base_url
def _resolve_schedule_url(self) -> str:
"""Resolve the schedule URL, handling .local domains"""
schedule_url = bus_schedule_url.lower().rstrip('/')
if '.local' in schedule_url:
try:
# Extract hostname from URL
hostname = schedule_url.split('://')[1].split(':')[0]
# Try to resolve the IP address
ip = socket.gethostbyname(hostname)
logger.info(f"Resolved {hostname} to {ip}")
# Replace hostname with IP in URL
return schedule_url.replace(hostname, ip)
except Exception as e:
logger.warning(f"Could not resolve {hostname}, falling back to IP: {e}")
# Fallback to direct IP if resolution fails
return "http://127.0.0.1:5001"
return schedule_url
def _start_health_check(self):
"""Start a thread to check API health and pre-warm schedule provider"""
def health_check_task():
# First, wait for the API to be healthy
while not self._stop_event.is_set():
try:
logger.info("Checking API health...")
response = requests.get(f"{self.base_url}/health", timeout=10)
if response.status_code == 200:
logger.info("API is healthy")
break
except Exception as e:
logger.warning(f"API health check failed: {e}")
time.sleep(10) # Wait 10 seconds before next attempt
# If pre-load is enabled and we have a schedule provider, start pre-warming
if pre_load_bus_schedule and self._get_provider_type(self.provider) == 'realtime':
fallback = self._get_fallback_provider()
if fallback:
try:
logger.info(f"Pre-warming schedule provider {fallback}")
# Send request directly to schedule URL
response = requests.get(
f"{self.schedule_url}/api/{fallback}/waiting_times",
params={"stop_id": self.stop_id, "download": "true"},
timeout=300 # 5 minute timeout for initial load
)
if response.status_code == 200:
logger.info(f"Successfully pre-warmed schedule provider {fallback}")
else:
logger.warning(f"Pre-warm returned status {response.status_code}: {response.text}")
except Exception as e:
logger.warning(f"Failed to pre-warm schedule provider: {e}")
# Start health check in background thread
thread = threading.Thread(target=health_check_task, name="APIHealthCheck")
thread.daemon = True # Make thread exit when main program exits
thread.start()
logger.debug("Started API health check thread")
def _load_provider_config(self) -> dict:
"""Load provider configuration from JSON file"""
try:
config_path = Path(__file__).parent / 'docs/setup/js/providers.json'
with open(config_path, 'r') as f:
config = json.load(f)
return config
except Exception as e:
logger.error(f"Failed to load provider config: {e}")
return {"providers": []}
def _get_provider_type(self, provider_id: str) -> str:
"""
Determine if a provider is realtime, schedule-only, or unknown
Returns: 'realtime', 'schedule', or 'unknown'
"""
try:
for provider in self.provider_config.get('providers', []):
if provider.get('realtime_provider') == provider_id:
return 'realtime'
if provider.get('schedule_provider') == provider_id:
return 'schedule'
return 'unknown'
except Exception as e:
logger.error(f"Error determining provider type: {e}")
return 'unknown'
def _get_fallback_provider(self) -> str:
"""Get fallback provider for current provider"""
try:
# If current provider is already a schedule provider or unknown, no fallback
provider_type = self._get_provider_type(self.provider)
if provider_type in ['schedule', 'unknown']:
logger.info(f"Provider {self.provider} is {provider_type}, no fallback needed")
return None
# Look for fallback for realtime provider
for provider in self.provider_config.get('providers', []):
if provider.get('realtime_provider') == self.provider:
return provider.get('schedule_provider')
return None
except Exception as e:
logger.error(f"Error getting fallback provider: {e}")
return None
def _update_api_urls(self):
"""Update API URLs based on current provider"""
base = self.schedule_url if self._get_provider_type(self.current_provider) == 'schedule' else self.base_url
# Remove trailing slashes from base URL and ensure single slashes in path
base = base.rstrip('/')
self.api_url = f"{base}/api/{self.current_provider}/waiting_times?stop_id={Stop}&download=true"
self.colors_url = f"{base}/api/{self.current_provider}/colors"
logger.debug(f"Updated URLs - API: {self.api_url}, Colors: {self.colors_url}")
def _try_realtime_provider(self) -> tuple[bool, dict]:
"""Try to fetch data from realtime provider"""
# Only try RT if we're using a known RT provider
if self._get_provider_type(self.provider) != 'realtime':
logger.debug(f"Provider {self.provider} is not a realtime provider, skipping RT check")
return False, None
if not self._rt_backoff.should_retry():
return False, None
try:
# Temporarily switch URLs to RT provider
original_urls = (self.api_url, self.colors_url)
self.current_provider = self.provider
self._update_api_urls()
response = requests.get(self.api_url, timeout=120)
data = response.json()
# Update backoff state
self._rt_backoff.update_backoff_state(True)
return True, data
except requests.exceptions.ConnectionError as e:
logger.debug(f"Realtime provider connection error: {e}")
self._rt_backoff.update_backoff_state(False, error_type='connection')
# Restore URLs if we're staying with fallback
self.api_url, self.colors_url = original_urls
return False, None
except requests.exceptions.Timeout as e:
logger.debug(f"Realtime provider timeout: {e}")
self._rt_backoff.update_backoff_state(False, error_type='timeout')
# Restore URLs if we're staying with fallback
self.api_url, self.colors_url = original_urls
return False, None
except Exception as e:
logger.debug(f"Realtime provider error: {e}")
self._rt_backoff.update_backoff_state(False, error_type='error')
# Restore URLs if we're staying with fallback
self.api_url, self.colors_url = original_urls
return False, None
def _check_schedule_health(self) -> bool:
"""Check if schedule provider is healthy"""
try:
fallback = self._get_fallback_provider()
if not fallback:
logger.warning("No fallback provider configured")
return False
logger.info(f"Checking schedule health for provider {fallback}")
response = requests.get(f"{self.schedule_url}/health", timeout=10)
logger.info(f"Schedule health check response: {response.status_code}")
if response.status_code != 200:
logger.warning(f"Schedule health check failed with status {response.status_code}")
return False
return True
except Exception as e:
logger.warning(f"Schedule health check failed: {e}")
return False
def get_waiting_times(self) -> tuple[List[Dict], str, str]:
"""Fetch and process waiting times for our bus lines"""
# Only check RT if we started with a RT provider
if self.current_provider != self.provider and self._get_provider_type(self.provider) == 'realtime':
logger.info(f"Attempting to switch back to realtime provider {self.provider}")
success, rt_data = self._try_realtime_provider()
if success:
logger.info("Successfully switched back to realtime provider")
return self._process_response_data(rt_data)
else:
logger.warning("Failed to switch back to realtime provider")
# Get current backoff based on provider type
provider_type = self._get_provider_type(self.current_provider)
current_backoff = self._rt_backoff if provider_type == 'realtime' else self._fallback_backoff
logger.debug(f"Current provider: {self.current_provider}, type: {provider_type}")
if not current_backoff.should_retry():
# Only try fallback if we're a RT provider
if provider_type == 'realtime' and fallback_to_schedule:
fallback = self._get_fallback_provider()
if fallback and self._check_schedule_health():
logger.info(f"Switching to fallback provider: {fallback}")
self.current_provider = fallback
self._update_api_urls()
# Only retry fallback if it's not in backoff
if self._fallback_backoff.should_retry():
return self.get_waiting_times()
else:
logger.warning("Fallback provider is in backoff state")
error_type = current_backoff.get_last_error()
error_msg = {
'connection': "Unable to connect to service",
'timeout': "Service not responding",
None: "Service temporarily unavailable"
}.get(error_type, "Service temporarily unavailable")
return self._get_error_data(), f"{error_msg}. Next attempt at {current_backoff.get_retry_time_str()}", ""
try:
logger.info(f"Fetching waiting times from {self.api_url}")
response = requests.get(self.api_url, timeout=120)
logger.debug(f"API response time: {response.elapsed.total_seconds():.3f} seconds")
logger.debug(f"API response status: {response.status_code}")
if response.status_code != 200:
logger.warning(f"API response text: {response.text}")
response.raise_for_status()
data = response.json()
# Update appropriate backoff on success
if provider_type == 'realtime':
self._rt_backoff.update_backoff_state(True)
else:
self._fallback_backoff.update_backoff_state(True)
return self._process_response_data(data)
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:
if provider_type == 'realtime':
self._rt_backoff.update_backoff_state(False, error_type='connection' if isinstance(e, requests.exceptions.ConnectionError) else 'timeout')
else:
self._fallback_backoff.update_backoff_state(False, error_type='connection' if isinstance(e, requests.exceptions.ConnectionError) else 'timeout')
return self._get_error_data(), "Connection failed" if isinstance(e, requests.exceptions.ConnectionError) else "Service not responding", ""
except Exception as e:
if provider_type == 'realtime':
self._rt_backoff.update_backoff_state(False, error_type='error')
else:
self._fallback_backoff.update_backoff_state(False, error_type='error')
# If using realtime provider and failed, try fallback
if self.current_provider == self.provider:
fallback = self._get_fallback_provider()
if fallback:
logger.info(f"Error with realtime provider, switching to fallback: {fallback}")
self.current_provider = fallback
self._update_api_urls()
self._fallback_backoff.reset() # Reset backoff for fallback provider
return self.get_waiting_times() # Retry with fallback
logger.error(f"Error fetching bus times: {e}", exc_info=True)
return self._get_error_data(), "Service error", ""
def _process_response_data(self, data: dict) -> tuple[List[Dict], str, str]:
"""Process the response data into waiting times format"""
try:
# Get provider type once at the start
provider_type = self._get_provider_type(self.current_provider)
stops_data_location_keys = ['stops_data', 'stops']
for key in stops_data_location_keys:
if key in data:
stop_data = data[key].get(self.stop_id, {})
# Debug print all stop IDs found
all_stop_ids = list(data[key].keys())
logger.debug(f"All stop IDs found in response: {all_stop_ids}")
logger.debug(f"Looking for target stop ID: {self.stop_id}")
logger.debug(f"Target stop ID found: {self.stop_id in all_stop_ids}")
break
# Extract waiting times for our stop
if not stop_data:
logger.error(f"Stop '{self.stop_id}' not found in response, as no stop data was found")
return self._get_error_data(), "Stop data not found", ""
# Get stop name
stop_name = stop_data.get("name", "")
logger.debug(f"Stop name: {stop_name}")
# Check if there are any lines in the stop data
if not stop_data.get("lines"):
logger.info(f"No active lines at stop {stop_name}")
return [], None, stop_name
bus_times = []
# If no specific lines are configured, use all lines from the stop data
lines_to_process = self.lines_of_interest if self.lines_of_interest else stop_data.get("lines", {}).keys()
for line in lines_to_process:
logger.debug(f"Processing line {line}")
line_data = stop_data.get("lines", {}).get(line, {})
logger.debug(f"Line data found: {line_data}")
if not line_data:
logger.warning(f"No data found for line {line}")
continue
# Get line display name from metadata if available
display_line = line # Default to route ID
if '_metadata' in line_data:
metadata = line_data['_metadata']
if isinstance(metadata, list) and len(metadata) > 0:
# New format: array of metadata objects
metadata = metadata[0] # Take first metadata entry
if 'route_short_name' in metadata:
display_line = metadata['route_short_name']
logger.debug(f"Using display line number {display_line} for route {line} (array format)")
elif isinstance(metadata, dict) and 'route_short_name' in metadata:
# Old format: direct metadata object
display_line = metadata['route_short_name']
logger.debug(f"Using display line number {display_line} for route {line} (direct format)")
# Process times and messages from all destinations
all_times = []
minutes_source = None
minutes_keys = ['minutes', 'scheduled_minutes', 'realtime_minutes']
# Check if we have any realtime data
has_realtime = False
has_non_scheduled = False
for destination, times in line_data.items():
if destination == '_metadata':
continue
for bus in times:
if 'realtime_minutes' in bus:
has_realtime = True
if 'minutes' in bus or 'realtime_minutes' in bus:
has_non_scheduled = True
for destination, times in line_data.items():
if destination == '_metadata': # Skip metadata
continue
logger.debug(f"Processing destination: {destination}")
for bus in times:
# Get all available minutes values
minutes_values = {}
for key in minutes_keys:
if key in bus:
minutes_values[key] = bus[key]
# Prefer realtime over scheduled over basic minutes
if 'realtime_minutes' in minutes_values:
minutes_source = 'realtime_minutes'
minutes = minutes_values['realtime_minutes']
minutes_emoji = '⚡'
elif 'scheduled_minutes' in minutes_values:
minutes_source = 'scheduled_minutes'
minutes = minutes_values['scheduled_minutes']
# Only show clock if we have a mix of scheduled and realtime/regular times
minutes_emoji = '🕒' if has_non_scheduled else ''
elif 'minutes' in minutes_values:
minutes_source = 'minutes'
minutes = minutes_values['minutes']
minutes_emoji = ''
else:
minutes_source = None
minutes = None
minutes_emoji = ''
# Filter out invalid times (negative times less than -5 minutes)
try:
if minutes is not None and isinstance(minutes, str):
# Only clean and check if it might be a negative number
if '-' in minutes:
# Remove any quotes and non-numeric characters except minus sign
cleaned_minutes = ''.join(c for c in minutes if c.isdigit() or c == '-')
if cleaned_minutes:
minutes_int = int(cleaned_minutes)
if minutes_int < -5: # Skip if less than -5 minutes
logger.warning(f"Skipping invalid negative time: {minutes} minutes")
minutes = None
minutes_emoji = ''
elif minutes == '0' or minutes == "0'": # Handle 0 minutes case
minutes = '0' # Keep the zero
except ValueError as e:
logger.warning(f"Could not parse minutes value '{minutes}': {e}")
minutes = None
minutes_emoji = ''
time = f"{minutes_emoji}{minutes}" if minutes is not None else ""
message = None
# Check for special messages
if 'message' in bus and bus['message']: # Only process if message exists and is non-empty
if isinstance(bus['message'], dict):
msg = bus['message'].get('en', '')
else:
msg = bus['message']
if "Last departure" in msg:
message = "Last"
elif "Theoretical time" in msg:
message = "theor."
elif "End of service" in msg:
time = ""
message = "End of service"
else:
message = msg
logger.debug(f"Time: {time}, Message: {message}, Minutes: {bus.get(minutes_source, None)}, Destination: {destination}")
if time or message: # Only add if we have either a time or a message
all_times.append({
'time': time,
'message': message,
'minutes': bus.get(minutes_source, None),
'destination': destination
})
# Sort times by minutes:
# - Extracts only digits from time strings (e.g., "⚡5" -> 5, "🕒10" -> 10)
# - Valid times (with digits) are sorted normally
# - Invalid times (None, "--", no digits) are pushed to end using infinity
all_times.sort(key=lambda x: int(''.join(filter(str.isdigit, str(x['minutes'])))) if x['minutes'] is not None and str(x['minutes']).strip() and any(c.isdigit() for c in str(x['minutes'])) else float('inf'))
logger.debug(f"All sorted times for line {line}: {all_times}")
# Only add the bus line if we have actual times or messages to display
if all_times:
waiting_times = []
messages = []
# Special handling for end of service
if any(t['message'] == "End of service" for t in all_times):
waiting_times = [""]
messages = ["End of service"]
# Special handling for last departure
elif any(t['message'] == "Last" for t in all_times):
last_bus = next(t for t in all_times if t['message'] == "Last")
waiting_times = [last_bus['time']]
messages = [last_bus['message']]
else:
# Take all times
for time_data in all_times:
waiting_times.append(time_data['time'])
messages.append(time_data['message'])
# Get colors for dithering
colors = self.get_line_color(line)
bus_times.append({
"line": display_line, # Use display line number
"times": waiting_times,
"messages": messages,
"colors": colors
})
# Update backoff state on success
if provider_type == 'realtime':
self._rt_backoff.update_backoff_state(True)
else:
self._fallback_backoff.update_backoff_state(True)
return bus_times, None, stop_name
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:
if provider_type == 'realtime':
self._rt_backoff.update_backoff_state(False, error_type='connection' if isinstance(e, requests.exceptions.ConnectionError) else 'timeout')
else:
self._fallback_backoff.update_backoff_state(False, error_type='connection' if isinstance(e, requests.exceptions.ConnectionError) else 'timeout')
return self._get_error_data(), "Connection failed" if isinstance(e, requests.exceptions.ConnectionError) else "Service not responding", ""
except Exception as e:
if provider_type == 'realtime':
self._rt_backoff.update_backoff_state(False, error_type='error')
else:
self._fallback_backoff.update_backoff_state(False, error_type='error')
# If using realtime provider and failed, try fallback
if self.current_provider == self.provider:
fallback = self._get_fallback_provider()
if fallback:
logger.info(f"Error with realtime provider, switching to fallback: {fallback}")
self.current_provider = fallback
self._update_api_urls()
self._fallback_backoff.reset() # Reset backoff for fallback provider
return self.get_waiting_times() # Retry with fallback
logger.error(f"Error fetching bus times: {e}", exc_info=True)
return self._get_error_data(), "Service error", ""
def _get_error_data(self) -> List[Dict]:
"""Return error data structure when something goes wrong"""
return [
{"line": "98", "times": [""], "colors": [('black', 0.7), ('white', 0.3)]},
{"line": "99", "times": [""], "colors": [('black', 0.7), ('white', 0.3)]}
]
def draw_display(self, epd, bus_data=None, weather_data: WeatherData = None, set_base_image=False):
"""Draw the display with bus times and weather info."""
try:
# Handle different color definitions
BLACK = 0 if epd.is_bw_display else epd.BLACK
WHITE = 1 if epd.is_bw_display else epd.WHITE
RED = getattr(epd, 'RED', BLACK) # Fall back to BLACK if RED not available
YELLOW = getattr(epd, 'YELLOW', BLACK) # Fall back to BLACK if YELLOW not available
# Create a new image with white background
if epd.is_bw_display:
Himage = Image.new('1', (epd.height, epd.width), WHITE)
else:
Himage = Image.new('RGB', (epd.height, epd.width), WHITE)
draw = ImageDraw.Draw(Himage)
font_paths = get_font_paths()
try:
font_large = ImageFont.truetype(font_paths['dejavu_bold'], 28)
font_medium = ImageFont.truetype(font_paths['dejavu'], 16)
font_small = ImageFont.truetype(font_paths['dejavu'], 14)
font_tiny = ImageFont.truetype(font_paths['dejavu'], 10)
except Exception as e:
logger.error(f"Error loading fonts: {e}")
font_large = font_medium = font_small = font_tiny = ImageFont.load_default()
MARGIN = 5
# Draw weather info if available
if weather_data:
draw_weather_info(draw, Himage, weather_data, font_paths, epd, MARGIN)
# Draw bus times if available
if bus_data:
y_pos = MARGIN
for bus in bus_data:
route = bus.get('route', '')
destination = bus.get('destination', '')
due_in = bus.get('due_in', '')
# Format text
text = f"{route} {destination} {due_in}"
draw.text((MARGIN, y_pos), text, font=font_medium, fill=BLACK)
y_pos += font_medium.getbbox(text)[3] + 5
# Rotate the image
Himage = Himage.rotate(DISPLAY_SCREEN_ROTATION, expand=True)
return Himage
except Exception as e:
logger.error(f"Error drawing display: {e}")
traceback.print_exc()
return None
@lru_cache(maxsize=1024)
def _hex_to_rgb(self, hex_color: str) -> Tuple[int, int, int]:
"""Convert hex color to RGB tuple"""
hex_color = hex_color.lstrip('#')
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
def _is_valid_hex_color(self, hex_color: str) -> bool:
"""Validate if a string is a valid hex color code"""
if not hex_color:
return False
# Remove '#' if present
hex_color = hex_color.lstrip('#')
# Check if it's a valid hex color (6 characters, valid hex digits)
return len(hex_color) == 6 and all(c in '0123456789ABCDEFabcdef' for c in hex_color)
@lru_cache(maxsize=1024)
def _get_color_distance(self, color1: Tuple[int, int, int], color2: Tuple[int, int, int]) -> float:
"""Calculate Euclidean distance between two RGB colors"""
return sum((a - b) ** 2 for a, b in zip(color1, color2)) ** 0.5
@lru_cache(maxsize=1024)
def _get_dithering_colors(self, target_rgb: Tuple[int, int, int]) -> Tuple[str, str, float]:
"""
Find the two closest display colors and the mix ratio for dithering.
Returns (primary_color, secondary_color, primary_ratio)
"""
# Find the two closest colors
distances = [(name, self._get_color_distance(target_rgb, rgb))
for name, rgb in DISPLAY_COLORS.items()]
distances.sort(key=lambda x: x[1])
color1, dist1 = distances[0]
color2, dist2 = distances[1]
# Calculate the ratio (how much of color1 to use)
total_dist = dist1 + dist2
if total_dist == 0:
ratio = 1.0
else:
ratio = 1 - (dist1 / total_dist)
# Ensure ratio is between 0.0 and 1.0
ratio = max(0.0, min(1.0, ratio))
# If the primary color is very dominant (>80%), use it exclusively
DOMINANCE_THRESHOLD = 0.8
if ratio >= DOMINANCE_THRESHOLD:
return color1, color2, 1.0
return color1, color2, ratio
def set_epd(self, epd):
"""Set the EPD display object for color optimization"""
self.epd = epd
@lru_cache(maxsize=1024)
def get_line_color(self, line: str) -> list:
"""
Get the optimal colors and ratios for a specific bus line
Returns a list of (color, ratio) tuples
Cached to avoid repeated API calls for the same line number
"""
try:
if not self.epd:
logger.warning("EPD not set, falling back to black and white")
return [('black', 0.7), ('white', 0.3)]
response = requests.get(f"{self.colors_url}/{line}")
response.raise_for_status()
line_colors = response.json()
# Extract the hex color from the response
if isinstance(line_colors, dict) and 'background' in line_colors:
hex_color = line_colors['background']
else:
hex_color = line_colors.get(line)
# Validate hex color
if not hex_color or not self._is_valid_hex_color(hex_color):
logger.warning(f"Invalid hex color received for line {line}: {hex_color}")
return [('black', 0.7), ('white', 0.3)] # Fallback to black and white
# Convert hex to RGB
target_rgb = self._hex_to_rgb(hex_color)
return find_optimal_colors(target_rgb, self.epd)
except Exception as e:
logger.error(f"Error getting line color for {line}: {e}")
return [('black', 0.7), ('white', 0.3)] # Fallback to black and white
def get_api_health(self) -> bool:
"""Check if the API is healthy"""
try:
response = requests.get(f"{self.base_url}/health")
logger.info(f"Health check response: {response.status_code}")
return response.status_code == 200
except Exception as e:
logger.error(f"Health check failed: {e}")
return False
def stop(self):
"""Stop the bus service"""
self._stop_event.set()
def draw_weather_info(draw, Himage, weather_data: WeatherData, font_paths, epd, MARGIN):
"""Draw weather information in the top right corner."""
try:
BLACK = 0 if epd.is_bw_display else epd.BLACK
# Load fonts
font_medium = ImageFont.truetype(font_paths['dejavu'], 16)
# Format temperature text with correct unit
unit_symbol = "°F" if weather_data.current.unit == TemperatureUnit.FAHRENHEIT else "°K" if weather_data.current.unit == TemperatureUnit.KELVIN else "°C"
temp_text = f"{weather_data.current.temperature:.1f}{unit_symbol}"
# Get weather icon
icon_name = weather_data.current.condition.icon
icon_path = ICONS_DIR / f"{icon_name}.svg"
if not icon_path.exists():
logger.warning(f"Icon not found: {icon_path}")
icon_path = ICONS_DIR / "cloud.svg" # fallback icon
# Calculate text size
text_bbox = draw.textbbox((0, 0), temp_text, font=font_medium)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
# Load and size icon to match text height
icon_size = text_height
icon = load_svg_icon(icon_path, (icon_size, icon_size), epd)
if icon:
# Calculate total width and positions
total_width = text_width + icon_size + 5 # 5px spacing between icon and text
start_x = Himage.width - total_width - MARGIN
# Draw icon and text
Himage.paste(icon, (start_x, MARGIN))
draw.text((start_x + icon_size + 5, MARGIN),
temp_text, font=font_medium, fill=BLACK)
except Exception as e:
logger.error(f"Error drawing weather info: {e}")
traceback.print_exc()
def select_lines_to_display(bus_data: List[Dict]) -> List[Dict]:
"""
Select which 2 lines to display based on earliest arrival times.
In case of ties (same arrival time), sort by line number.
Lines with no valid times (e.g. "End of service") are considered last.
Priority order:
1. Negative arrival times (already late, closest to 0 first)
2. Buses at stop (0 minutes)
3. Positive arrival times
4. No valid times
"""
def get_earliest_time(times: List[str]) -> tuple[float, bool]:
"""
Extract the earliest numeric time from a list of time strings
Returns (time, has_zero) where time is the earliest non-zero time
and has_zero indicates if there's a bus at the stop
"""
earliest = float('inf')
has_zero = False
has_negative = False
earliest_negative = float('-inf')
for time_str in times:
# Skip empty times or special messages
if not time_str or time_str == "↓↓" or time_str == "⚡↓↓":
has_zero = True
continue
# Extract numeric value from time string (e.g. "⚡3'" -> 3)
# Keep the minus sign for negative times
digits = ''.join(c for c in time_str if c.isdigit() or c == '-')
if digits:
try:
time = int(digits)
if time == 0:
has_zero = True
elif time < 0:
has_negative = True
earliest_negative = max(earliest_negative, time) # Get the closest to 0
else:
earliest = min(earliest, time)
except ValueError:
continue
# If we have a negative time, return that
if has_negative:
return (earliest_negative, False)
# Otherwise return the earliest positive time
return (earliest, has_zero)
# Create list of lines with their earliest times
lines_with_times = []
configured_lines = os.getenv("Lines", "")
lines_of_interest = _parse_lines(configured_lines) if configured_lines else []
for bus in bus_data:
# If we have configured lines, only process those
if lines_of_interest and bus['line'] not in lines_of_interest:
continue
earliest, has_zero = get_earliest_time(bus['times'])
lines_with_times.append({
'line': bus['line'],
'earliest_time': earliest,
'has_zero': has_zero,
'original_data': bus
})
# Sort by:
# 1. Negative times first (closest to 0)
# 2. Has zero (True comes before False)
# 3. Positive times (smaller first)
# 4. Line number (for consistent tie-breaking)
def sort_key(x):
time = x['earliest_time']
return (
time >= 0, # Negative times first
not x['has_zero'], # Then zeros
abs(time) if time != float('inf') else float('inf'), # Then by absolute time
x['line'] # Then by line number
)
lines_with_times.sort(key=sort_key)
# Take first two lines
selected = lines_with_times[:2]
# Log selection results
if len(bus_data) > 2:
selected_lines = [s['line'] for s in selected]
dropped_lines = [bus['line'] for bus in bus_data if bus['line'] not in selected_lines]
logger.info(f"Selected lines {selected_lines} from {len(bus_data)} available lines")
logger.debug(f"Dropped lines: {dropped_lines}")
if any(s['earliest_time'] == float('inf') and not s['has_zero'] for s in selected):
logger.warning("Selected a line with no valid times - this might not be optimal")
# Return original bus data for selected lines
return [line['original_data'] for line in selected]
def update_display(epd, weather_data: WeatherData = None, bus_data=None, error_message=None, stop_name=None, first_run=False, set_base_image=False):
"""Update the display with new weather and waiting timesdata"""
MARGIN = 6
# Handle different color definitions
BLACK = epd.BLACK if not epd.is_bw_display else 0
WHITE = epd.WHITE if not epd.is_bw_display else 1
RED = getattr(epd, 'RED', BLACK) # Fall back to BLACK if RED not available
YELLOW = getattr(epd, 'YELLOW', BLACK) # Fall back to BLACK if YELLOW not available
logger.info(f"Display dimensions: {epd.height}x{epd.width} (height x width)")
# Create a new image with white background
if epd.is_bw_display:
Himage = Image.new('1', (epd.height, epd.width), 1) # 1 is white in 1-bit mode
else:
Himage = Image.new('RGB', (epd.height, epd.width), WHITE)
draw = ImageDraw.Draw(Himage)
font_paths = get_font_paths()
try:
font_large = ImageFont.truetype(font_paths['dejavu_bold'], 32)
font_medium = ImageFont.truetype(font_paths['dejavu'], 24)
font_small = ImageFont.truetype(font_paths['dejavu'], 16)
font_tiny = ImageFont.truetype(font_paths['dejavu'], 12)
# logger.info(f"Found DejaVu fonts: {font_large}, {font_medium}, {font_small}")
except:
font_large = ImageFont.load_default()
font_medium = font_small = font_large
logger.warning(f"No DejaVu fonts found, using default: {font_large}, {font_medium}, {font_small}. Install DeJaVu fonts with \n sudo apt install fonts-dejavu\n")
try:
emoji_font = ImageFont.truetype(font_paths['emoji'], 16)
emoji_font_medium = ImageFont.truetype(font_paths['emoji'], 20)
except:
emoji_font = font_small
emoji_font_medium = font_medium
logger.warning(f"No Noto Emoji font found, using {emoji_font.getname()} instead.")
if not weather_enabled:
weather_data = None
logger.warning("Weather is not enabled, weather data will not be displayed. Do not forget to set OPENWEATHER_API_KEY in .env to enable it.")
if weather_enabled and weather_data:
# Get weather icon and temperature
icon_name = weather_data.current.condition.icon
temperature = weather_data.current.temperature
icon_path = ICONS_DIR / f"{icon_name}.svg"