Skip to content

Commit a979e24

Browse files
committed
Enhance get_channels_with_hash()
To set the index 0 name to the preset name from localConfig.lora.modem_prese and compute hash so the output will not have bad index0 name or hash, also removes all disabled for clarity. this outputs `Device1 Channel Hash Table: [{'index': 0, 'role': 'PRIMARY', 'name': 'LongFast', 'hash': 8}` for a LongFast
1 parent dbc0101 commit a979e24

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

meshtastic/node.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,16 +1023,35 @@ def ensureSessionKey(self):
10231023
def get_channels_with_hash(self):
10241024
"""Return a list of dicts with channel info and hash."""
10251025
result = []
1026+
def format_preset_name(name):
1027+
# Convert name like MODEM_PRESET to ModemPreset
1028+
return ''.join(word.capitalize() for word in name.split('_'))
1029+
10261030
if self.channels:
10271031
for c in self.channels:
1028-
if c.settings and hasattr(c.settings, "name") and hasattr(c.settings, "psk"):
1029-
hash_val = generate_channel_hash(c.settings.name, c.settings.psk)
1030-
else:
1031-
hash_val = None
1032-
result.append({
1033-
"index": c.index,
1034-
"role": channel_pb2.Channel.Role.Name(c.role),
1035-
"name": c.settings.name if c.settings and hasattr(c.settings, "name") else "",
1032+
# Ignore DISABLED channels
1033+
if channel_pb2.Channel.Role.Name(getattr(c, "role", 0)) == "DISABLED":
1034+
continue
1035+
hash_val = None
1036+
name = ""
1037+
if getattr(c, "settings", None) is not None:
1038+
if hasattr(c.settings, "name") and hasattr(c.settings, "psk"):
1039+
hash_val = generate_channel_hash(c.settings.name, c.settings.psk)
1040+
name = getattr(c.settings, "name", "")
1041+
# If PRIMARY and name is empty, use formatted preset name from localConfig.lora.modem_preset
1042+
if c.role == channel_pb2.Channel.Role.PRIMARY and not name:
1043+
modem_preset_enum = getattr(getattr(self.localConfig, "lora", None), "modem_preset", None)
1044+
if modem_preset_enum is not None:
1045+
modem_preset_string = self.localConfig.lora.DESCRIPTOR.fields_by_name["modem_preset"].enum_type.values_by_number[modem_preset_enum].name
1046+
name = format_preset_name(modem_preset_string)
1047+
# Recompute hash with new name and key "AQ=="
1048+
hash_val = generate_channel_hash(name, "AQ==")
1049+
channel_info = {
1050+
"index": getattr(c, "index", None),
1051+
"role": channel_pb2.Channel.Role.Name(getattr(c, "role", 0)),
1052+
"name": name,
10361053
"hash": hash_val,
1037-
})
1054+
}
1055+
result.append(channel_info)
10381056
return result
1057+

0 commit comments

Comments
 (0)