Skip to content

Commit

Permalink
fix(usbrelay): poll for status to confirm
Browse files Browse the repository at this point in the history
The usbrelay command is async, hence we cannot immediately after
changing the state check if it became active. Instead, we need to poll
for some time and report back either if the state changed, or on
timeout. To leave sufficent time for other parts of the code, we check
until half of the RPC timeout is expired.

Signed-off-by: Felix Moessbauer <felix.moessbauer@siemens.com>
  • Loading branch information
fmoessbauer committed Dec 2, 2023
1 parent 2c54b1c commit ce5b803
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions mtda/power/usbrelay.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
# System imports
import os
import re
import time
import subprocess

# Local imports
from mtda.power.controller import PowerController
import mtda.constants as CONSTS


class UsbRelayPowerController(PowerController):
Expand Down Expand Up @@ -49,12 +51,12 @@ def command(self, args):
def on(self):
if self._set_lines("1") is False:
return False
return self.status() == self.POWER_ON
return self._poll_status_until(self.POWER_ON)

def off(self):
if self._set_lines("0") is False:
return False
return self.status() == self.POWER_OFF
return self._poll_status_until(self.POWER_OFF)

def status(self):
statuses = self._get_lines()
Expand All @@ -75,6 +77,13 @@ def status(self):
result = self.POWER_UNSURE
return result

def _poll_status_until(self, desired_state):
for i in range(0, CONSTS.RPC.TIMEOUT // 2 + 1):
if self.status() == desired_state:
return True
time.sleep(0.5)
return False

def _get_lines(self):
result = {}
try:
Expand Down

0 comments on commit ce5b803

Please sign in to comment.