This commit is contained in:
Lynn Ochs 2022-11-19 17:33:52 +01:00
parent 1fc7162a40
commit 9dbed1a51e

View File

@ -86,8 +86,6 @@ class Control(util.Loggable):
self._idle = False
def _run_control(self):
# Last known state
st = state.IDLE
# Not controlling the lock
controlling = False
cmd = {
@ -135,7 +133,7 @@ class Control(util.Loggable):
self._logger().debug(f"State update, target is {state_names[self.target()]}")
last_state = self.state()
self._logger().info("Reached state "
f"{state_names.get(st, st)}")
f"{state_names.get(last_state, last_state)}")
if action == None and self.state() != state.ERROR:
self._logger().info("Probably somebody using the key")
self.target(last_state)
@ -143,6 +141,7 @@ class Control(util.Loggable):
elif last_state == last_target:
# Reached target
timeouts = 0
self._logger().debug("Target reached")
if last_target == state.CLOSE \
and self.position() > constants.CLOSED_WANT:
self._logger().info(
@ -152,14 +151,17 @@ class Control(util.Loggable):
controlling = False
elif self.state() == state.ERROR:
# Position too high, restart
self._logger().debug("Error")
self._comm.cmd_restart()
self.target(state.CLOSE)
else:
self._logger().debug("Reached wrong state")
if timeouts < 3:
timeouts += 1
if self.target() == last_target:
# Initially, switch to the other one
# and execute that
self._logger().debug("Switching targets")
last_target = {
state.CLOSE: state.OPEN_THEN_CLOSE,
state.OPEN: state.CLOSE_THEN_OPEN
@ -167,10 +169,12 @@ class Control(util.Loggable):
cmd[last_target]()
else:
# Then go back
self._logger().debug("Going back to actual target")
last_target = self.target()
cmd[last_target]()
else:
# Tried too often, restart
self._logger().debug("Something's wrong")
self.target(state.RESTART)
def start(self):
@ -198,11 +202,13 @@ class Control(util.Loggable):
def open(self):
with self._mutex:
self._logger().info("Opening")
self.target(state.OPEN)
self._control_update.notify()
def close(self):
with self._mutex:
self._logger().info("Closing")
self.target(state.CLOSE)
self._control_update.notify()