From d015da3ca1bb02955274fa9cb703a0f9e7aa4cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sat, 18 Jun 2022 14:53:04 +0200 Subject: [PATCH] Get and Set general attributes almost working. --- meshtastic/__main__.py | 70 ++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 49e5777..8bdea0b 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -52,18 +52,27 @@ def onConnection(interface, topic=pub.AUTO_TOPIC): # pylint: disable=W0613 print(f"Connection changed: {topic.getName()}") -def getPref(attributes, name): +def getPref(attributes, comp_name): """Get a channel or preferences value""" - camel_name = meshtastic.util.snake_to_camel(name) + name=comp_name.split(".",1) + if len(name) != 2: + name[0]=comp_name + name.append(comp_name) + + camel_name = meshtastic.util.snake_to_camel(name[1]) # Note: protobufs has the keys in snake_case, so snake internally - snake_name = meshtastic.util.camel_to_snake(name) + snake_name = meshtastic.util.camel_to_snake(name[1]) logging.debug(f'snake_name:{snake_name} camel_name:{camel_name}') logging.debug(f'use camel:{Globals.getInstance().get_camel_case()}') objDesc = attributes.DESCRIPTOR - field = objDesc.fields_by_name.get(snake_name) - if not field: + section = objDesc.fields_by_name.get(name[0]) + field = False + if section: + field = section.message_type.fields_by_name.get(snake_name) + + if (not field) or (not section): if Globals.getInstance().get_camel_case(): print(f"{attributes.__class__.__name__} does not have an attribute called {camel_name}, so you can not get it.") else: @@ -71,16 +80,19 @@ def getPref(attributes, name): print(f"Choices in sorted order are:") names = [] for f in objDesc.fields: - tmp_name = f'{f.name}' - if Globals.getInstance().get_camel_case(): - tmp_name = meshtastic.util.snake_to_camel(tmp_name) - names.append(tmp_name) + tmp_path = f'{f.name}' + if(f.message_type): + for ff in f.message_type.fields: + tmp_name = f'{ff.name}' + if Globals.getInstance().get_camel_case(): + tmp_name = meshtastic.util.snake_to_camel(tmp_name) + names.append(tmp_path + "." +tmp_name) for temp_name in sorted(names): print(f" {temp_name}") return # read the value - val = getattr(attributes, snake_name) + val = getattr(section.message_type, snake_name) if Globals.getInstance().get_camel_case(): print(f"{camel_name}: {str(val)}") @@ -90,17 +102,26 @@ def getPref(attributes, name): logging.debug(f"{snake_name}: {str(val)}") -def setPref(attributes, name, valStr): +def setPref(attributes, comp_name, valStr): """Set a channel or preferences value""" - snake_name = meshtastic.util.camel_to_snake(name) - camel_name = meshtastic.util.snake_to_camel(name) + name=comp_name.split(".",1) + if len(name) != 2: + name[0]=comp_name + name.append(comp_name) + + snake_name = meshtastic.util.camel_to_snake(name[1]) + camel_name = meshtastic.util.snake_to_camel(name[1]) logging.debug(f'snake_name:{snake_name}') logging.debug(f'camel_name:{camel_name}') objDesc = attributes.DESCRIPTOR - field = objDesc.fields_by_name.get(snake_name) - if not field: + section = objDesc.fields_by_name.get(name[0]) + field = False + if section: + field = section.message_type.fields_by_name.get(snake_name) + + if (not field) or (not section): if Globals.getInstance().get_camel_case(): print(f"{attributes.__class__.__name__} does not have an attribute called {camel_name}, so you can not set it.") else: @@ -108,10 +129,13 @@ def setPref(attributes, name, valStr): print(f"Choices in sorted order are:") names = [] for f in objDesc.fields: - tmp_name = f'{f.name}' - if Globals.getInstance().get_camel_case(): - tmp_name = meshtastic.util.snake_to_camel(tmp_name) - names.append(tmp_name) + tmp_path = f'{f.name}' + if(f.message_type): + for ff in f.message_type.fields: + tmp_name = f'{ff.name}' + if Globals.getInstance().get_camel_case(): + tmp_name = meshtastic.util.snake_to_camel(tmp_name) + names.append(tmp_path + "." + tmp_name) for temp_name in sorted(names): print(f" {temp_name}") return @@ -147,18 +171,18 @@ def setPref(attributes, name, valStr): # note: 'ignore_incoming' is a repeating field if snake_name != 'ignore_incoming': try: - setattr(attributes, snake_name, val) + setattr(section.message_type, snake_name, val) except TypeError: # The setter didn't like our arg type guess try again as a string - setattr(attributes, snake_name, valStr) + setattr(section.message_type, snake_name, valStr) else: if val == 0: # clear values print("Clearing ignore_incoming list") - del attributes.ignore_incoming[:] + del section.message_type.ignore_incoming[:] else: print(f"Adding '{val}' to the ignore_incoming list") - attributes.ignore_incoming.extend([val]) + section.message_type.ignore_incoming.extend([val]) if Globals.getInstance().get_camel_case(): print(f"Set {camel_name} to {valStr}")