Commit ebf1e801 authored by Olivier Kaufmann's avatar Olivier Kaufmann
Browse files

Removes args from _process_command and parses json message in one pass.

Showing with 37 additions and 34 deletions
+37 -34
...@@ -485,9 +485,9 @@ class OhmPi(object): ...@@ -485,9 +485,9 @@ class OhmPi(object):
self.sequence = sequence self.sequence = sequence
def measure(self, *args, **kwargs): def measure(self, **kwargs):
warnings.warn('This function is deprecated. Use run_multiple_sequences() instead.', DeprecationWarning) warnings.warn('This function is deprecated. Use run_multiple_sequences() instead.', DeprecationWarning)
self.run_multiple_sequences(self, *args, **kwargs) self.run_multiple_sequences(self, **kwargs)
def _process_commands(self, message): def _process_commands(self, message):
"""Processes commands received from the controller(s) """Processes commands received from the controller(s)
...@@ -505,47 +505,50 @@ class OhmPi(object): ...@@ -505,47 +505,50 @@ class OhmPi(object):
self.exec_logger.debug(f'Decoded message {decoded_message}') self.exec_logger.debug(f'Decoded message {decoded_message}')
cmd_id = decoded_message.pop('cmd_id', None) cmd_id = decoded_message.pop('cmd_id', None)
cmd = decoded_message.pop('cmd', None) cmd = decoded_message.pop('cmd', None)
args = decoded_message.pop('args', None) # args = decoded_message.pop('args', None)
if args is not None: # if args is not None:
if len(args) != 0: # if len(args) != 0:
if args[0] != '[': # if args[0] != '[':
args = f'["{args}"]' # args = f'["{args}"]'
self.exec_logger.debug(f'args to decode: {args}') # self.exec_logger.debug(f'args to decode: {args}')
args = json.loads(args) if args != '[]' else None # args = json.loads(args) if args != '[]' else None
self.exec_logger.debug(f'Decoded args {args}') # self.exec_logger.debug(f'Decoded args {args}')
else: # else:
args = None # args = None
kwargs = decoded_message.pop('kwargs', None) kwargs = decoded_message.pop('kwargs', None)
if kwargs is not None: # if kwargs is not None:
if len(kwargs) != 0: # if len(kwargs) != 0:
if kwargs[0] != '{': # if kwargs[0] != '{':
kwargs = '{"' + kwargs + '"}' # kwargs = '{"' + kwargs + '"}'
self.exec_logger.debug(f'kwargs to decode: {kwargs}') # self.exec_logger.debug(f'kwargs to decode: {kwargs}')
kwargs = json.loads(kwargs) if kwargs != '' else None # kwargs = json.loads(kwargs) if kwargs != '' else None
self.exec_logger.debug(f'Decoded kwargs {kwargs}') # self.exec_logger.debug(f'Decoded kwargs {kwargs}')
else: # else:
kwargs = None # kwargs = None
self.exec_logger.debug(f"Calling method {cmd}({str(args) + ', ' if args is not None else ''}" self.exec_logger.debug(f"Calling method {cmd}({str(kwargs) if kwargs is not None else ''})")
f"{str(kwargs) if kwargs is not None else ''})") # self.exec_logger.debug(f"Calling method {cmd}({str(args) + ', ' if args is not None else ''}"
# f"{str(kwargs) if kwargs is not None else ''})")
if cmd_id is None: if cmd_id is None:
self.exec_logger.warning('You should use a unique identifier for cmd_id') self.exec_logger.warning('You should use a unique identifier for cmd_id')
if cmd is not None: if cmd is not None:
try: try:
if args is None: # if args is None:
if kwargs is None: # if kwargs is None:
output = getattr(self, cmd)() # output = getattr(self, cmd)()
else: # else:
output = getattr(self, cmd)(**kwargs) # output = getattr(self, cmd)(**kwargs)
# else:
if kwargs is None:
output = getattr(self, cmd)()
else: else:
if kwargs is None: output = getattr(self, cmd)(**kwargs)
output = getattr(self, cmd)(*args)
else:
output = getattr(self, cmd)(*args, **kwargs)
status = True status = True
except Exception as e: except Exception as e:
print(f'kwargs: {kwargs}')
self.exec_logger.error( self.exec_logger.error(
f"Unable to execute {cmd}({str(args) + ', ' if args is not None else ''}" f"Unable to execute {cmd}({str(kwargs) if kwargs is not None else ''}): {e}")
f"{str(kwargs) if kwargs is not None else ''}): {e}") # f"Unable to execute {cmd}({str(args) + ', ' if args is not None else ''}"
# f"{str(kwargs) if kwargs is not None else ''}): {e}")
status = False status = False
except Exception as e: except Exception as e:
self.exec_logger.warning(f'Unable to decode command {message}: {e}') self.exec_logger.warning(f'Unable to decode command {message}: {e}')
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment