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