Better idle handling
This commit is contained in:
parent
4ea571dc20
commit
11c900c2e5
|
@ -87,12 +87,9 @@ class Control(util.Loggable):
|
||||||
def _run_control(self):
|
def _run_control(self):
|
||||||
# Last known state
|
# Last known state
|
||||||
st = state.IDLE
|
st = state.IDLE
|
||||||
# Current action
|
# Not controlling the lock
|
||||||
action = state.IDLE
|
controlling = False
|
||||||
cmd = {
|
cmd = {
|
||||||
None: lambda: None,
|
|
||||||
state.IDLE: lambda: None,
|
|
||||||
state.ERROR: lambda: None,
|
|
||||||
state.RESTART: self._comms.cmd_restart,
|
state.RESTART: self._comms.cmd_restart,
|
||||||
state.OPEN: self._comms.cmd_open,
|
state.OPEN: self._comms.cmd_open,
|
||||||
state.OPEN_THEN_CLOSE: self._comms.cmd_open,
|
state.OPEN_THEN_CLOSE: self._comms.cmd_open,
|
||||||
|
@ -102,6 +99,8 @@ class Control(util.Loggable):
|
||||||
|
|
||||||
with self._control_update:
|
with self._control_update:
|
||||||
last_target = state.IDLE
|
last_target = state.IDLE
|
||||||
|
last_state = None
|
||||||
|
action = None
|
||||||
# When starting, reset the MCU once
|
# When starting, reset the MCU once
|
||||||
self.target(state.CLOSE)
|
self.target(state.CLOSE)
|
||||||
# Starting time of the current action
|
# Starting time of the current action
|
||||||
|
@ -118,40 +117,38 @@ class Control(util.Loggable):
|
||||||
# Update was that the target has changed
|
# Update was that the target has changed
|
||||||
if self.target() != last_target:
|
if self.target() != last_target:
|
||||||
self._logger().debug(f"Target update: {state_names[self.target()]}")
|
self._logger().debug(f"Target update: {state_names[self.target()]}")
|
||||||
self._logger().debug(f"{cmd[self.target()]}, {cmd[last_target]}")
|
action = cmd.get(self.target(), None)
|
||||||
if cmd.get(action, None) != cmd[self.target()]:
|
last_action = cmd.get(last_target, None)
|
||||||
|
if action != None and last_action != action:
|
||||||
# We need to send a different command for this
|
# We need to send a different command for this
|
||||||
self._logger().debug(f"Calling {cmd[self.target()]}")
|
self._logger().debug(f"Calling {action}")
|
||||||
cmd[self.target()]()
|
action()
|
||||||
else:
|
elif action == last_action:
|
||||||
self._logger().debug(f"Same command as {state_names[last_target]}")
|
self._logger().debug(f"Same command as {state_names[last_target]}")
|
||||||
# Update last known target and starting time
|
# Update last known target and starting time
|
||||||
last_target = self.target()
|
last_target = self.target()
|
||||||
start_time = datetime.now()
|
start_time = datetime.now()
|
||||||
|
|
||||||
# Update current target
|
if self.state() != last_state:
|
||||||
target = last_target
|
|
||||||
|
|
||||||
if self.state() != st:
|
|
||||||
# State from position handling differs from last known state
|
# State from position handling differs from last known state
|
||||||
self._logger().debug(f"State update, target is {state_names[target]}")
|
self._logger().debug(f"State update, target is {state_names[self.target()]}")
|
||||||
st = self.state()
|
last_state = self.state()
|
||||||
self._logger().info("Reached state "
|
self._logger().info("Reached state "
|
||||||
f"{state_names.get(st, st)}")
|
f"{state_names.get(st, st)}")
|
||||||
if action == state.IDLE:
|
if action == None and self.state() != state.ERROR:
|
||||||
self._logger().info("Probably somebody using the key")
|
self._logger().info("Probably somebody using the key")
|
||||||
self.target(st)
|
self.target(last_state)
|
||||||
target = last_target = st
|
last_target = last_state
|
||||||
elif st == target:
|
elif last_state == last_target:
|
||||||
# Reached target
|
# Reached target
|
||||||
timeouts = 0
|
timeouts = 0
|
||||||
if target == state.CLOSE \
|
if last_target == state.CLOSE \
|
||||||
and self.position() > constants.CLOSED_WANT:
|
and self.position() > constants.CLOSED_WANT:
|
||||||
self._logger().info(
|
self._logger().info(
|
||||||
f"Position is {self.position()}, "
|
f"Position is {self.position()}, "
|
||||||
"closing some more")
|
"closing some more")
|
||||||
self._comms.cmd_close()
|
self._comms.cmd_close()
|
||||||
action = state.IDLE
|
controlling = False
|
||||||
elif self.state() == state.ERROR:
|
elif self.state() == state.ERROR:
|
||||||
# Position too high, restart
|
# Position too high, restart
|
||||||
self._comm.cmd_restart()
|
self._comm.cmd_restart()
|
||||||
|
@ -159,25 +156,22 @@ class Control(util.Loggable):
|
||||||
else:
|
else:
|
||||||
if timeouts < 3:
|
if timeouts < 3:
|
||||||
timeouts += 1
|
timeouts += 1
|
||||||
if action == target:
|
if self.target() == last_target:
|
||||||
# Initially, switch to the other one
|
# Initially, switch to the other one
|
||||||
# and execute that
|
# and execute that
|
||||||
action = {
|
last_target = {
|
||||||
state.CLOSE: state.OPEN_THEN_CLOSE,
|
state.CLOSE: state.OPEN_THEN_CLOSE,
|
||||||
state.OPEN: state.CLOSE_THEN_OPEN
|
state.OPEN: state.CLOSE_THEN_OPEN
|
||||||
}.get(target, state.RESTART)
|
}.get(last_target, state.RESTART)
|
||||||
cmd[action]()
|
cmd[last_target]()
|
||||||
else:
|
else:
|
||||||
# Then go back
|
# Then go back
|
||||||
action = target
|
last_target = self.target()
|
||||||
cmd[action]()
|
cmd[last_target]()
|
||||||
else:
|
else:
|
||||||
# Tried too often, restart
|
# Tried too often, restart
|
||||||
self.target(state.RESTART)
|
self.target(state.RESTART)
|
||||||
|
|
||||||
if action == state.IDLE:
|
|
||||||
continue
|
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
with self._mutex:
|
with self._mutex:
|
||||||
if not self._started:
|
if not self._started:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user