Commit ba9c7073 authored by Guillaume Blanchy's avatar Guillaume Blanchy
Browse files

mqtt pure JSON

Showing with 23 additions and 21 deletions
+23 -21
...@@ -157,6 +157,7 @@ class MyServer(SimpleHTTPRequestHandler): ...@@ -157,6 +157,7 @@ class MyServer(SimpleHTTPRequestHandler):
# we pass the sequence as a list of list as this object is easier to parse for the json.loads() # we pass the sequence as a list of list as this object is easier to parse for the json.loads()
# of ohmpi._process_commands() # of ohmpi._process_commands()
payload = json.dumps({'cmd_id': cmd_id, 'cmd': 'set_sequence', 'kwargs': {'sequence': sequence}}) payload = json.dumps({'cmd_id': cmd_id, 'cmd': 'set_sequence', 'kwargs': {'sequence': sequence}})
print('payload ===', payload)
publish.single(payload=payload, **publisher_config) publish.single(payload=payload, **publisher_config)
payload = json.dumps({'cmd_id': cmd_id + '_settings', 'cmd': 'update_settings', 'kwargs': {'config': dic['config']}}) payload = json.dumps({'cmd_id': cmd_id + '_settings', 'cmd': 'update_settings', 'kwargs': {'config': dic['config']}})
cdic = dic['config'] cdic = dic['config']
......
...@@ -510,27 +510,28 @@ class OhmPi(object): ...@@ -510,27 +510,28 @@ 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 = None
if args is not None: # args = decoded_message.pop('args', None)
if len(args) != 0: # if args is not None:
if args[0] != '[': # if len(args) != 0:
args = f'["{args}"]' # if args[0] != '[':
self.exec_logger.debug(f'args to decode: {args}') # args = f'["{args}"]'
args = json.loads(args) if args != '[]' else None # self.exec_logger.debug(f'args to decode: {args}')
self.exec_logger.debug(f'Decoded args {args}') # args = json.loads(args) if args != '[]' else None
else: # self.exec_logger.debug(f'Decoded args {args}')
args = None # else:
# 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}("
f"{str(kwargs) if kwargs 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')
...@@ -549,7 +550,7 @@ class OhmPi(object): ...@@ -549,7 +550,7 @@ class OhmPi(object):
status = True status = True
except Exception as e: except Exception as e:
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(args) + ', ' if args is not None else ''}"
f"{str(kwargs) if kwargs is not None else ''}): {e}") f"{str(kwargs) if kwargs is not None else ''}): {e}")
status = False status = False
except Exception as e: except Exception as 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