Skip to content

Commit 31fd88a

Browse files
committed
modemPresetName
refactoring per comments in meshtastic#859
1 parent ad04c26 commit 31fd88a

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

meshtastic/node.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
stripnl,
1818
message_to_json,
1919
generate_channel_hash,
20+
format_preset_name,
2021
to_node_num,
2122
)
2223

@@ -1027,12 +1028,18 @@ def get_channels_with_hash(self):
10271028
for c in self.channels:
10281029
if c.settings and hasattr(c.settings, "name") and hasattr(c.settings, "psk"):
10291030
hash_val = generate_channel_hash(c.settings.name, c.settings.psk)
1030-
else:
1031-
hash_val = None
1031+
# If name is empty string, use modem preset
1032+
if c.settings.name == '':
1033+
# Assuming c.settings.modem_preset exists and is the enum name
1034+
modem_preset_enum = getattr(getattr(self.localConfig, "lora", None), "modem_preset", None)
1035+
modem_preset_string = self.localConfig.lora.DESCRIPTOR.fields_by_name["modem_preset"].enum_type.values_by_number[modem_preset_enum].name
1036+
name_val = format_preset_name(modem_preset_string)
1037+
else:
1038+
name_val = c.settings.name
10321039
result.append({
10331040
"index": c.index,
10341041
"role": channel_pb2.Channel.Role.Name(c.role),
1035-
"name": c.settings.name if c.settings and hasattr(c.settings, "name") else "",
1042+
"name": name_val,
10361043
"hash": hash_val,
10371044
})
10381045
return result

meshtastic/util.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,29 @@ def generate_channel_hash(name: Union[str, bytes], key: Union[str, bytes]) -> in
392392
result: int = h_name ^ h_key
393393
return result
394394

395+
# Mapping of preset enum names to display names used by get_channels_with_hash(), format_preset_name()
396+
MODEM_PRESET_DISPLAY_NAMES = {
397+
"SHORT_TURBO": ("ShortTurbo", "ShortT"),
398+
"SHORT_SLOW": ("ShortSlow", "ShortS"),
399+
"SHORT_FAST": ("ShortFast", "ShortF"),
400+
"MEDIUM_SLOW": ("MediumSlow", "MedS"),
401+
"MEDIUM_FAST": ("MediumFast", "MedF"),
402+
"LONG_SLOW": ("LongSlow", "LongS"),
403+
"LONG_FAST": ("LongFast", "LongF"),
404+
"LONG_MODERATE": ("LongMod", "LongM"),
405+
}
406+
407+
def format_preset_name(name=None):
408+
# Normalize input: extract the preset part from full enum string
409+
if isinstance(name, str):
410+
parts = name.split('_')
411+
for i in range(len(parts)):
412+
key = '_'.join(parts[i:])
413+
if key in MODEM_PRESET_DISPLAY_NAMES:
414+
names = MODEM_PRESET_DISPLAY_NAMES[key]
415+
return names[0] # Return clean name like "LongFast"
416+
return "Custom" # If no match, return "Custom"
417+
395418
def hexstr(barray: bytes) -> str:
396419
"""Print a string of hex digits"""
397420
return ":".join(f"{x:02x}" for x in barray)

0 commit comments

Comments
 (0)