Skip to content

Commit

Permalink
Fix syntax errors; set state sooner
Browse files Browse the repository at this point in the history
  • Loading branch information
nref committed Jun 30, 2022
1 parent afdef1f commit 3d084cf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
6 changes: 3 additions & 3 deletions services/metricsBoardBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MetricsBoardBase:
def __init__(self, treadmill):
self.run = True
self.treadmill = treadmill
self.treadmill.state_changed_callbacks.append(handle_state_changed)
self.treadmill.state_changed_callbacks.append(self.handle_state_changed)
self.metrics_callbacks = {}

self.timestamp = "00:00"
Expand Down Expand Up @@ -53,8 +53,8 @@ def close(self):
self.run = False
self.run_thread.join()

def handle_state_changed(state):
self.notify_metric_changed(TreadmillMetric.State, state))
def handle_state_changed(self, state):
self.notify_metric_changed(TreadmillMetric.State, state)

def handle_data(self, chars):
"""
Expand Down
32 changes: 13 additions & 19 deletions services/precor956i.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Precor956i:
def __init__(self):

self.run = True
self.set_state(TreadmillState.Ready)
self.state = TreadmillState.Ready
self.state_changed_callbacks = []
self.timestamp = "00:00"

Expand Down Expand Up @@ -66,7 +66,7 @@ def start_threads(self):
self.keep_incline_thread.start()

def set_state(self, state):
if (self.state == state)
if self.state == state:
return
self.state = state
self.notify_state_changed(state)
Expand Down Expand Up @@ -157,10 +157,17 @@ def control_loop(self, rate_limit_func, interrupt_func, increment_func, decremen
continue

if diff < 0:
print(self.state)
increment_func()
elif diff > 0:
decrement_func()

# Do not change speed or incline outside of the Started state
def state_ok(self):
self.validate_state()
return self.state in [TreadmillState.Started]

# Stop workout if we haven't heard from teh treadmill in a while
def validate_state(self):

heartbeat = datetime.datetime.now() - self.lastUpdate
Expand All @@ -169,19 +176,6 @@ def validate_state(self):
print(f"No contact from treadmill in {self.heartbeat.total_seconds()}s. Ending workout.")
self.end_workout()

# if (self.state in [TreadmillState.Started] and self.speed_feedback < ZERO):
# print(f"speed is zero. Assuming paused.")
# self.set_state(TreadmillState.Paused)

# if (self.state not in [TreadmillState.Started] and self.speed_feedback > ZERO):
# print(f"speed is nonzero. Assuming started.")
# self.set_state(TreadmillState.Started)

# Do not change speed or incline outside of the Started state
def state_ok(self):
self.validate_state()
return self.state in [TreadmillState.Started]

# Do not change speed or incline when the setpoint has been reached
def setpoint_reached(self, diff):
return abs(diff) < ZERO
Expand Down Expand Up @@ -253,23 +247,23 @@ def start_workout(self):

def pause(self):
if self.state == TreadmillState.Started:
self.pulse(self.reset, then_wait_s=1) # Started -> Paused
self.set_state(TreadmillState.Paused)
self.pulse(self.reset, then_wait_s=1) # Started -> Paused

def resume(self):
if self.state == TreadmillState.Paused:
self.pulse(self.start, then_wait_s=1) # Paused -> Started
self.set_state(TreadmillState.Started)
self.pulse(self.start, then_wait_s=1) # Paused -> Started

def end_from_pause(self):
if self.state == TreadmillState.Paused:
self.pulse(self.reset, then_wait_s=1) # Paused -> Summary
self.set_state(TreadmillState.Summary)
self.pulse(self.reset, then_wait_s=1) # Paused -> Summary

def close_summary(self):
if self.state == TreadmillState.Summary:
self.set_state(TreadmillState.Ready)
self.pulse(self.reset, then_wait_s=1) # Summary -> Ready
self.set_state(TreadmillState.Ready)

def end_workout(self):

Expand Down

0 comments on commit 3d084cf

Please sign in to comment.