-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrl_emergency.py
executable file
·70 lines (56 loc) · 1.78 KB
/
rl_emergency.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
import json
import sys
from datetime import datetime, timedelta
import backend.time_util as tu
from backend.led_controller import LedController
from backend.program import Program
from backend.schedule import Schedule
def get_schedule_time() -> str:
time_arg = sys.argv[2]
time_now = datetime.now()
today = time_now
year, month, day = today.year, today.month, today.day
iso_time_str_today = f"{year}-{month:02}-{day:02}T{time_arg}"
tomorrow = today + timedelta(days=1)
year, month, day = tomorrow.year, tomorrow.month, tomorrow.day
iso_time_str_tomorrow = f"{year}-{month:02}-{day:02}T{time_arg}"
time_today = tu.string_to_datetime(iso_time_str_today)
return (
iso_time_str_today
if time_today > time_now
else iso_time_str_tomorrow
)
def get_program():
with open(sys.argv[1], 'r', encoding='utf-8') as file:
return Program.from_json(
"emergency_program", json_data=json.load(file)
)
def report(program: Program, scheduled_time: str):
print(f"Filename: {sys.argv[1]}")
print(program)
print()
print(f"Running program at: {scheduled_time}")
def main():
led_controller = LedController()
led_controller.load_preset('idle')
program = get_program()
schedule_time = get_schedule_time()
schedule = Schedule(schedule_time, lambda: program.run(lambda: None))
schedule.start()
report(program, schedule_time)
try:
schedule.join()
program.join()
except Exception:
print("Unexpected exception!")
except KeyboardInterrupt:
schedule.cancel()
if program.is_running:
program.stop()
print("Stopped.")
else:
print("Done.")
finally:
led_controller.off()
if __name__ == "__main__":
main()