-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplans.py
165 lines (136 loc) · 4.58 KB
/
plans.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
import time
from datetime import datetime
from enum import Enum
from plyer import gps
from pygame import mixer
class Run:
def __init__(self, duration: int, rest_duration: int, distance: int = None):
self.duration = duration
self.rest_duration = rest_duration
self.distance = distance
class Plan:
def __init__(
self, name, runs: list[Run], warmup_time: int = 300, cool_down_time: int = 300
):
self.name = name
self.runs = runs
self.this_run = None
self.warmup_time = warmup_time
self.cool_down_time = cool_down_time
self.state = self.State.STARTING
self.state_time = 0
# for cleanup purposes
self.clock_event = None
self.state_change_callback = None
# history
self.is_finished = False
self.start_time = None
self.end_time = None
self.running_path = []
class State(Enum):
STARTING = 0
WARMUP = 1
RUNNING = 2
RESTING = 3
COOL_DOWN = 4
FINISHED = 5
@property
def get_duration(self):
return sum(run.duration for run in self.runs)
@property
def get_rest_duration(self):
return sum(run.rest_duration for run in self.runs)
@property
def get_total_time(self):
return (
self.get_duration
+ self.get_rest_duration
+ self.warmup_time
+ self.cool_down_time
)
def begin_workout(self):
self.state = self.State.STARTING
self.play_sound("sounds/begin.mp3")
self.state_time = 10
self.start_time = datetime.now()
def start_warmup(self):
self.state = self.State.WARMUP
self.play_sound("sounds/warmup.mp3")
self.state_time = self.warmup_time
def start_running(self):
self.this_run = self.runs.pop(0)
self.state = self.State.RUNNING
self.play_sound("sounds/running.mp3")
self.state_time = self.this_run.duration
def start_resting(self):
self.state = self.State.RESTING
self.play_sound("sounds/walking.mp3")
self.state_time = self.this_run.rest_duration
def start_cool_down(self):
self.state = self.State.COOL_DOWN
self.play_sound("sounds/cool_down.mp3")
self.state_time = self.cool_down_time
def finish_workout(self):
self.state = self.State.FINISHED
self.play_sound("sounds/finish.mp3")
self.state_time = 0
self.runs = []
self.this_run = None
self.is_finished = True
self.end_time = datetime.now()
self.save_workout()
def save_workout(self):
pass
def update_state(self, dt):
self.state_time -= 1
if self.state_time <= 0:
if self.state == self.State.STARTING:
self.start_warmup()
elif self.state == self.State.WARMUP:
self.start_running()
elif self.state == self.State.RUNNING:
if self.runs:
self.start_resting()
else:
self.start_cool_down()
elif self.state == self.State.RESTING:
self.start_running()
elif self.state == self.State.COOL_DOWN:
self.finish_workout()
if self.state_change_callback:
self.state_change_callback(self.state, self.state_time)
# get current location
self.get_current_location()
@staticmethod
def play_sound( file_location):
mixer.init()
mixer.music.load(file_location)
mixer.music.play()
while mixer.music.get_busy(): # wait for music to finish playing
time.sleep(1)
def get_current_location(self):
try:
gps.configure(on_location=self.on_location)
gps.start(minTime=1000, minDistance=0)
except NotImplementedError:
pass
def on_location(self, **kwargs):
lat = kwargs.get("lat")
lon = kwargs.get("lon")
if lat is not None and lon is not None:
self.running_path.append((lat, lon))
def __str__(self):
name = ""
run_name = f"{self.runs[0].duration / 60:.1f}"
run_time = 1
for run in self.runs[1:]:
if f"{run.duration / 60:.1f}" == run_name:
run_time += 1
continue
name = name + f"{run_time}x{run_name} min \n"
run_name = f"{run.duration / 60:.1f}"
run_time = 1
name = name + f"{run_time}x{run_name} min \n"
return name
def __len__(self):
return len(self.runs)