-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.py
More file actions
465 lines (390 loc) · 17.1 KB
/
scheduler.py
File metadata and controls
465 lines (390 loc) · 17.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
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
#!/usr/bin/env python3
import time
import threading
from datetime import datetime, timedelta, date, time as dtime
from typing import TypeAlias
from enum import Enum
import os
import sys
import heapq
import signal
from math import floor
from PIL import Image # to read img
# local modules
from draw import TotalRenderer, get_timeline_panel_ranges
from routines import rt_workday, routines
from task import find_current_task, Task, TaskStat
from uploader import TimelineUploader
from display import display, INKY_AVAILABLE
from alarm import Sound, bark, shut_up
from btnListener import BtnListener
if INKY_AVAILABLE:
from display import true_display
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
daemon = None
class UpdateTrigger(Enum):
TASK_END = "task_end"
TASK_START = "task_start"
PANEL_SHIFT = "panel_shift"
PERIODIC = "periodic"
NEW_DAY = "new_day" # reserved; not used.
BTN_EVT = "button_event"
BOLUS = "bolus" # reserved
UpdateList: TypeAlias = list[tuple[datetime, UpdateTrigger]]
class ScheduleDaemon:
def __init__(self):
self.routine = None
self.last_update = datetime.now()
self.task_instances = None
self.current_task = None
self.current_panels = None
self.running = False
self.min_interval = timedelta(minutes=3, seconds=1)
self.periodic_interval = timedelta(minutes=30)
self.renderer = TotalRenderer()
self.uploader = TimelineUploader(
os.path.join(BASE_DIR, "cfg", "upload_config.json")
)
self.update_timer = None
self.update_queue: UpdateList = []
self.stop_event = threading.Event()
self.stat_str = ""
self.silent_hrs_wkday: list[tuple[dtime]] = [
(dtime(0, 5), dtime(6, 40)),
(dtime(9, 20), dtime(17, 40)),
]
self.silent_hrs_wkend: list[tuple[dtime]] = [(dtime(0, 5), dtime(7, 00))]
self.btnListener = BtnListener(callback=self.on_button)
self.btn_evt_interval = 0.75 # mins
self.btn_update_timer: threading.Timer = None
self.special_trigger: list[UpdateTrigger] = [
UpdateTrigger.BOLUS,
UpdateTrigger.BTN_EVT,
]
# special trigger that is set halfway and
# omitting this will lead to loss of already planned triggers in _schedule_next_update()
def get_silent_hrs(self, now: datetime) -> list[tuple[dtime]]:
return self.silent_hrs_wkday if now.weekday() < 5 else self.silent_hrs_wkend
def get_next_midnight(self, now: datetime) -> datetime:
return (now + timedelta(days=1)).replace(
hour=0, minute=0, second=0, microsecond=1
)
def start(self):
self.running = True
self.routine = self._get_today_routine()
print("Schedule daemon started")
self._schedule_today_updates()
self._schedule_next_update()
# initial update
self.uploader.upload_png(note=self.stat_str)
time.sleep(1)
self._update_display()
bark(Sound.OB_STAC)
# allow keybd interrupt
while self.running:
self.stop_event.wait(timeout=1) # or it won't stop on ^C
if self.stop_event.is_set():
break
def has_close_updates(
self, time_to_check: datetime = None, interval_multiplier: float = 1
):
time_to_check = time_to_check or datetime.now()
has_close_update = any(
(t - time_to_check).total_seconds() < self.min_interval.total_seconds()
for t, _ in self.update_queue
)
is_too_frequent = (
time_to_check - self.last_update
).total_seconds() < self.min_interval.total_seconds()
return has_close_update # or is_too_frequent
def _build_candidates(self, now: datetime) -> UpdateList:
"""Assemble raw candidates: task starts/ends and panel-shift."""
out: UpdateList = []
for task in self.task_instances:
st = task.start_time
nm = self.get_next_midnight(now)
if now < st < nm:
out.append((st, UpdateTrigger.TASK_START))
et = st + task.duration
if now < et < nm and not self.has_close_updates(et):
out.append((et, UpdateTrigger.TASK_END))
noon = now.replace(hour=12, minute=0, second=0, microsecond=1)
if noon > now and not self.has_close_updates(noon):
out.append((noon, UpdateTrigger.PANEL_SHIFT))
return out
def _filter_edge_conflicts(
self, candidates: UpdateList, next_midnight: datetime
) -> UpdateList:
"""Drop items too close to any TASK_START or to midnight (except NEW_DAY)."""
min_gap = self.min_interval.total_seconds()
starts = [t for t, k in candidates if k == UpdateTrigger.TASK_START]
keep: UpdateList = []
for t, k in candidates:
near_start = k != UpdateTrigger.TASK_START and any(
abs((t - s).total_seconds()) < min_gap for s in starts
)
near_midnight = (
k != UpdateTrigger.NEW_DAY
and abs((t - next_midnight).total_seconds()) < min_gap
)
if not near_start and not near_midnight:
keep.append((t, k))
return keep
def _schedule_today_updates(self):
"""Pre-calc all update times for today (candidates -> conflict filter -> heap -> periodic)."""
now = datetime.now()
self.task_instances = self.routine.create_schedule(date.today())
print(f"{self.get_silent_hrs(now)=}")
candidates = self._build_candidates(now)
final = self._filter_edge_conflicts(candidates, self.get_next_midnight(now))
self.update_queue = []
for t, k in sorted(final):
heapq.heappush(self.update_queue, (t, k))
# add periodic fillers
self._schedule_fill_periodic(final, now)
self.print_update_queue()
def print_update_queue(self, queue: list[tuple[datetime, UpdateTrigger]] = None):
queue = queue or self.update_queue
print("Update Queue:")
for t, k in sorted(self.update_queue):
print(f" {t.strftime('%H:%M:%S')} | {k.value}")
def on_button(self, label) -> None:
# print(f"Button {label} pressed")
if label == "B":
shut_up()
return
# following btn events requires update scheduled
if label == "A":
if self.current_task:
self.current_task.curr_status = TaskStat.ONGOING
elif label == "C":
if self.current_task:
self.current_task.curr_status = TaskStat.IGNORED
bark(Sound.OB_STAC)
# check if any updates are scheduled within 10 mins
now = datetime.now()
is_too_frequent = (now - self.last_update).total_seconds()
if is_too_frequent:
now = now + self.min_interval # not accurate but acceptable
if not self.has_close_updates(time_to_check=now, interval_multiplier=2):
update_time = now + timedelta(minutes=self.btn_evt_interval)
if not self.btn_update_timer:
self.btn_update_timer = threading.Timer(
self.btn_evt_interval * 60,
self._perform_update,
args=[UpdateTrigger.BTN_EVT],
)
self.btn_update_timer.start()
self.stat_str = (
f"Inserted btn update at {update_time.strftime('%H:%M:%S')}"
)
print(self.stat_str)
else:
print("Btn update already exists")
else:
print("Btn event update overridden")
# self.print_update_queue()
self._update_display(no_display=True)
self.uploader.upload_png(note=self.stat_str + " [status change]")
return
def _schedule_fill_periodic(
self, updates_to_fill: UpdateList, now: datetime
) -> None:
"""Fill gaps b/w task events by distributing PERIODIC triggers to minimize
average no-update intervals around periodic_interval, respecting min_interval.
"""
# collect future task boundary times (starts + ends), sorted
events: list[datetime] = [
t
for t, k in updates_to_fill
if k in (UpdateTrigger.TASK_START, UpdateTrigger.TASK_END)
]
events = sorted(set(events))
# day boundary
end_day = now.replace(hour=23, minute=59, second=0, microsecond=0)
# gap anchors: [now] + events + [end_of_day]
anchors: list[datetime] = [now] + events + [end_day]
tgt = self.periodic_interval.total_seconds()
min_gap = self.min_interval.total_seconds()
# for each gap, distrib N evenly: N ~= floor(L / tgt), then clamp by min_interval
for a, b in zip(anchors[:-1], anchors[1:]):
# keep updates away from hard anchors by min_interval
start = max(a, now) + self.min_interval
end = b - self.min_interval
if end <= start:
continue
L = (end - start).total_seconds()
# ideal count near target spacing
n = int(floor(L / tgt))
if n <= 0:
continue
# ensure spacing >= min_interval -- at most floor(L / min_interval) - 1 updates
n_max = max(0, int(L // min_gap) - 1)
n = max(0, min(n, n_max))
if n == 0:
continue
step = L / (n + 1)
# emit updates inside (start, end)
for j in range(1, n + 1):
t = start + timedelta(seconds=j * step)
heapq.heappush(self.update_queue, (t, UpdateTrigger.PERIODIC))
def _schedule_next_update(self):
"""Start a timer for next update. IMPORTANT: it removes earliest trigger from queue"""
if not self.running:
return
now = datetime.now()
earliest_allowed = self.last_update + self.min_interval
eligible_updates = []
temp_queue = []
while self.update_queue:
update_time, trigger = heapq.heappop(self.update_queue)
if update_time <= now:
continue
if update_time >= earliest_allowed:
eligible_updates.append((update_time, trigger))
else:
# Reschedule to earliest allowed time
if not self.has_close_updates(earliest_allowed):
temp_queue.append((earliest_allowed, trigger))
# put back any rescheduled items
for item in temp_queue:
heapq.heappush(self.update_queue, item)
if eligible_updates:
# closet update
eligible_updates.sort(key=lambda x: (x[0], not self._is_task_trigger(x[1])))
next_update, next_trigger = eligible_updates[0]
# Put back non-selected updates
for update in eligible_updates[1:]:
heapq.heappush(self.update_queue, update)
else:
# No eligible updates, try queue again
if self.update_queue:
next_update, next_trigger = heapq.heappop(self.update_queue)
else:
next_update = next_trigger = None
if next_update:
delay = (next_update - now).total_seconds()
decoupled = " [decoupled]" if self._in_silent_hour() else ""
self.stat_str = f"Next update scheduled: {next_update.strftime('%H:%M:%S')} ({next_trigger.value}){decoupled}"
print(self.stat_str)
if self.update_timer:
self.update_timer.cancel()
self.update_timer = threading.Timer(
delay, self._perform_update, args=[next_trigger]
)
self.update_timer.start()
else:
self._schedule_tomorrow(now)
def _schedule_tomorrow(self, now: datetime):
tomorrow = (now + timedelta(days=1)).replace(
hour=0, minute=1, second=0, microsecond=0
)
delay = (tomorrow - now).total_seconds()
self.stat_str = f"No more updates today. Scheduling for tomorrow at {tomorrow.strftime('%H:%M:%S')}"
print(self.stat_str)
if self.update_timer:
self.update_timer.cancel()
self.update_timer = threading.Timer(delay, self._start_new_day)
self.update_timer.start()
def _perform_update(self, trigger: UpdateTrigger):
"""Perform the scheduled update"""
if not self.running:
return
print(f"Update triggered: {trigger.value}")
# alarm
# print(f"Trigger Check: {trigger==UpdateTrigger.TASK_START or trigger == UpdateTrigger.TASK_END} | {'Muted' if self._in_silent_hour() else 'BarkOK'}")
if trigger == UpdateTrigger.TASK_START and not self._in_silent_hour():
now = datetime.now() + timedelta(
seconds=1
) # or it won't get to current task.
today_tasks = self.routine.create_schedule(
date.today()
) # TODO This is not clean.
curr_task: Task = find_current_task(today_tasks, now)
# print(curr_task, f'{curr_task.has_alarm = }')
if curr_task and curr_task.has_alarm:
print(f"Playing alarm {curr_task.alarm_sound} for {curr_task.title}")
bark(curr_task.alarm_sound)
elif trigger == UpdateTrigger.TASK_END and not self._in_silent_hour():
now = datetime.now() + timedelta(
seconds=1
) # or it won't get to current task??
today_tasks = self.routine.create_schedule(
date.today()
) # TODO This is not clean.
curr_task: Task = find_current_task(today_tasks, now)
if curr_task and curr_task.has_end_alarm:
print(
f"Playing alarm {curr_task.end_alarm_sound} for {curr_task.title}"
)
bark(curr_task.end_alarm_sound)
# update display
self._update_display(no_display=self._in_silent_hour())
self.last_update = datetime.now()
self.btn_update_timer = None
if not trigger in self.special_trigger:
self._schedule_next_update()
try:
self.uploader.upload_png(note=self.stat_str)
except Exception as e:
print(f"Upload error: {e}")
def _start_new_day(self):
"""Reset for a new day"""
if not self.running:
return
print("Starting new day...")
self.routine = self._get_today_routine()
# self.task_instances = self.routine.create_schedule(date.today())
self._schedule_today_updates()
self._perform_update(UpdateTrigger.TASK_START)
def _update_display(self, no_display: bool = False):
"""Update display and save preview"""
# update current task/panels tracking
now = datetime.now()
self.current_task = find_current_task(self.task_instances, now)
self.current_panels = get_timeline_panel_ranges(now)
# update display
img = self.renderer.create_schedule_image(self.routine, self.task_instances)
if not no_display:
display.set_image(img)
out_path = os.path.join(BASE_DIR, "output")
if not os.path.exists(out_path):
os.mkdir(out_path)
img.save(os.path.join(out_path, "schedule_preview.png"))
if INKY_AVAILABLE and not no_display:
img_disp = Image.open(os.path.join(out_path, "schedule_preview.png"))
true_display.set_image(img_disp)
true_display.show()
status = " (silent)" if no_display else ""
print(f"Preview saved at {now.strftime('%H:%M:%S')}{status}")
def stop(self):
self.running = False
if self.update_timer:
self.update_timer.cancel()
self.stop_event.set()
print("Daemon stopped")
def _get_today_routine(self):
"""Get routine for current date"""
today_key = datetime.now().strftime("%m%d")
return routines.get(today_key, rt_workday)
def _in_silent_hour(self, dt=None):
"""Check if time is in silent hours (no display updates or alarms)"""
if dt is None:
dt = datetime.now()
t = dt.time()
return any(
start <= t < end if start < end else t >= start or t < end
for start, end in self.get_silent_hrs(dt)
)
def _is_task_trigger(self, trigger):
"""Check if trigger is task-related (high priority)"""
return trigger in [UpdateTrigger.TASK_START, UpdateTrigger.TASK_END]
def signal_handler(signum, frame):
global daemon
if daemon:
daemon.stop()
sys.exit(0)
if __name__ == "__main__":
daemon = ScheduleDaemon()
signal.signal(signal.SIGINT, signal_handler)
daemon.start()