-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrunner.py
More file actions
306 lines (263 loc) · 11.1 KB
/
Copy pathrunner.py
File metadata and controls
306 lines (263 loc) · 11.1 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
# Now import other modules
import os
from datetime import datetime
import time
from typing import Optional
from dataclasses import dataclass
from data import AppData, DataHub, DataKey
from config.config import config
from ui.display import Display, DisplayIntent
from ui.panes import RenderContext
from ui.screens import screen_manager
from ui.key_input import start_spacebar_listener
from ui.touch_input import start_touch_listener
import logging
import logging.handlers
# Set up logging configuration
log_file = 'log.txt'
max_bytes = 5 * 1024 * 1024 # 5MB max file size
# Configure logging based on environment
quiet_mode = os.getenv('QUIET_MODE', 'false').lower() == 'true'
log_level = logging.WARNING if quiet_mode else logging.DEBUG
# Ensure log directory exists
try:
logging.basicConfig(
level=log_level,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.handlers.RotatingFileHandler(
log_file,
maxBytes=max_bytes,
backupCount=5
),
logging.StreamHandler() if not quiet_mode else logging.NullHandler()
],
force=True
)
except Exception as e:
print(f"Error setting up logging: {str(e)}")
raise
logger = logging.getLogger(__name__)
@dataclass
class DisplayState:
last_display_update: float = 0
last_display_clear: float = 0
class Clock:
"""Time source for the runner's scheduling; injectable so tests can drive the
decision logic deterministically. The real clock preserves prior behavior
exactly: time.time() for intervals, datetime.now() for the hourly check.
"""
def time(self) -> float:
return time.time()
def now(self) -> datetime:
return datetime.now()
class Runner:
def __init__(self, display=None, clock: "Clock" = None, data_hub: DataHub = None):
logger.info("Initializing Runner")
self.display = display if display is not None else Display()
self.clock = clock if clock is not None else Clock()
self.data_hub = data_hub if data_hub is not None else DataHub()
self.state = DisplayState()
self.min_interval = config.timing.DISPLAY_MIN_INTERVAL_SECONDS
self._previous_render_ctx: Optional[RenderContext] = None
self._previous_screen_name: Optional[str] = None
self.data_hub.subscribe(self.handle_data_update)
def handle_data_update(self, key: DataKey, data: AppData):
"""Handle a new source snapshot from the data hub."""
if key == "subway" and data.subway is not None:
now = self.clock.now()
trains = data.subway.trains
logger.info("-" * 40)
logger.info(f"Train update at {now.strftime('%Y-%m-%d %H:%M:%S')}")
logger.info(
f"Number of trains: {len(trains)} "
f"(service_unavailable={data.subway.service_unavailable})"
)
for train in trains:
logger.debug(f"Train: {train.arrival_time} ({train.minutes_until_arrival} min)")
elif key == "bikes" and data.bikes is not None:
logger.info(f"Bike update: {data.bikes.classic_bikes} classic, {data.bikes.ebikes} ebikes")
elif key == "birds" and data.birds is not None:
logger.info(
"Bird update: %s observations over %sh (source_unavailable=%s)",
len(data.birds.observations),
data.birds.window_hours,
data.birds.source_unavailable,
)
self._check_display_update(force=False)
def _build_render_context(self) -> RenderContext:
return RenderContext(
data=self.data_hub.data,
now=self.clock.now(),
)
def _missing_required_data(self, data: AppData) -> list[DataKey]:
required = screen_manager.current().requires()
return [key for key in required if not data.has(key)]
def _check_display_update(
self,
force: bool = False,
clear: bool = False,
intent: DisplayIntent | None = None,
):
"""Check if we should update the display"""
now = self.clock.time()
screen = screen_manager.current()
screen_name = screen_manager.current_name()
ctx = self._build_render_context()
missing = self._missing_required_data(ctx.data)
if missing:
logger.debug(
"[DISPLAY SKIP] Missing data for %s screen: %s",
screen_name,
", ".join(missing),
)
return
# Warn if bike data hasn't arrived yet (but don't block display)
if screen_name == "transit" and not ctx.data.has("bikes"):
logger.warning("[DISPLAY] Bike data not available, displaying without it")
# Always update if this is our first update
if self.state.last_display_update == 0:
logger.info("[DISPLAY UPDATE] First update")
first_update_intent = intent or DisplayIntent.SCREEN_TRANSITION
self._update_display(clear=clear, ctx=ctx, intent=first_update_intent)
return
# If forced (screen switch/manual clean redraw), update immediately
if force:
logger.info("[DISPLAY UPDATE] Forced update")
self._update_display(clear=clear, ctx=ctx, intent=intent)
return
# Clear the display at the top of every hour (aligned to clock time)
current_time = self.clock.now()
if (current_time.minute == 0) and (now - self.state.last_display_clear >= 3500):
logger.info("[DISPLAY UPDATE] Hourly clear")
self._update_display(clear=True, ctx=ctx, intent=DisplayIntent.MAINTENANCE_CLEAR)
return
prev_ctx = self._previous_render_ctx if self._previous_screen_name == screen_name else None
if not screen.should_redraw(ctx, prev_ctx):
logger.debug("[DISPLAY SKIP] %s screen does not need redraw", screen_name)
return
# Respect the minimum interval for regular ticks/data events.
time_since_update = now - self.state.last_display_update
if time_since_update >= self.min_interval:
logger.info(
f"[DISPLAY UPDATE] {screen_name} redraw ({time_since_update:.1f}s >= {self.min_interval}s)"
)
if screen.profile.full_refresh_on_redraw:
self._update_display(
clear=True,
ctx=ctx,
intent=DisplayIntent.MAINTENANCE_CLEAR,
)
else:
self._update_display(ctx=ctx)
return
else:
logger.debug(f"[DISPLAY SKIP] Min interval not met ({time_since_update:.1f}s < {self.min_interval}s)")
def _update_display(
self,
clear: bool = False,
ctx: Optional[RenderContext] = None,
intent: DisplayIntent | None = None,
):
"""Update the display with current state"""
try:
if ctx is None:
ctx = self._build_render_context()
display_intent = intent or (
DisplayIntent.MAINTENANCE_CLEAR if clear else DisplayIntent.NORMAL
)
partial = display_intent == DisplayIntent.NORMAL and not clear
screen_name = screen_manager.current_name()
queued = self.display.update(
app_data=ctx.data,
now=ctx.now,
screen_name=screen_name,
partial=partial,
clear=clear,
intent=display_intent,
)
if queued is not False:
self._prewarm_screen_renders(ctx, screen_name)
else:
return
if (clear == True):
self.state.last_display_clear = self.clock.time()
self.state.last_display_update = self.clock.time()
self._previous_render_ctx = ctx
self._previous_screen_name = screen_name
except Exception as e:
logger.error(f"Error updating display: {str(e)}")
def _prewarm_screen_renders(self, ctx: RenderContext, current_screen_name: str) -> None:
prewarm = getattr(self.display, "prewarm", None)
if prewarm is None:
return
screen_names = self._prewarm_screen_order(current_screen_name, ctx.data)
if not screen_names:
return
prewarm(ctx.data, ctx.now, screen_names)
def _prewarm_screen_order(self, current_screen_name: str, data: AppData) -> list[str]:
names = screen_manager.names()
if current_screen_name not in names:
return []
current_index = names.index(current_screen_name)
ordered = names[current_index + 1:] + names[:current_index]
return [
name
for name in ordered
if name != "transit" and self._screen_has_required_data(name, data)
]
def _screen_has_required_data(self, screen_name: str, data: AppData) -> bool:
required = screen_manager.get(screen_name).requires()
return all(data.has(key) for key in required)
def _advance_screen(self):
"""Advance to the next registered screen and force a transition redraw."""
if screen_manager.advance():
logger.info(f"Advanced to screen {screen_manager.current_name()}")
self._previous_render_ctx = None
self._previous_screen_name = None
self._check_display_update(
force=True,
intent=DisplayIntent.SCREEN_TRANSITION,
)
def _start_input_listeners(self) -> None:
# Interactive screen switching: press space to advance screens.
# No-ops when there's no tty (e.g. running as a systemd service).
if start_spacebar_listener(self._advance_screen):
logger.info(
"Screen switching enabled: press Space to cycle screens (%s)",
", ".join(screen_manager.names()),
)
if config.TOUCH_ENABLED and start_touch_listener(
self._advance_screen,
channel=config.TOUCH_CHANNEL,
address=config.TOUCH_I2C_ADDRESS,
):
logger.info(
"MPR121 touch screen switching enabled on channel %s at address 0x%02x",
config.TOUCH_CHANNEL,
config.TOUCH_I2C_ADDRESS,
)
def run(self):
"""Main run method"""
try:
logger.info("Starting services...")
# Initialize display
self.display.initialize()
# Subscribe to and start all data feeds.
self.data_hub.start()
self._start_input_listeners()
# Keep the main thread running
try:
while True:
time.sleep(1)
self._check_display_update()
except KeyboardInterrupt:
logger.info("Shutting down...")
except Exception as e:
logger.error(f"Error in main runner: {str(e)}")
finally:
# Clean shutdown
self.data_hub.stop()
if __name__ == "__main__":
runner = Runner()
runner.run()