-
Notifications
You must be signed in to change notification settings - Fork 0
/
odometer.py
31 lines (24 loc) · 825 Bytes
/
odometer.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
import threading
import time
from state import State
from config import read_config, write_config
config = read_config()
class Odometer:
def __init__(self, state: State):
self.state = state
self.interval = config['odo_interval']
self.thread = threading.Thread(target=self.start, daemon=True)
self.stop_event = threading.Event()
self.thread.start()
def start(self):
last = self.state.odo
while not self.stop_event.is_set():
if (self.state.odo > last):
write_config({'odo_miles': self.state.odo})
last = self.state.odo
time.sleep(self.interval)
def stop(self):
# daemon threads will self terminate
self.stop_event.set()
def miles(self):
return f"{self.state.odo:.2f}"