Skip to content

Commit 27be73c

Browse files
feat:506 show all module settings
Problem: Missing fields are omitted. Solution: This fix sets the flag `always_print_fields_with_no_presence` in the invocation of the protobuff method `MessageToJson` will display the missing fields. see: MessageToJson https://github.com/protocolbuffers/protobuf/blob/6b36eb633ccc9c585ed1ead117fca91702814027/python/google/protobuf/json_format.py#L82 see: issue #506 #506
1 parent bd788ae commit 27be73c

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

meshtastic/mesh_interface.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
our_exit,
3636
remove_keys_from_dict,
3737
stripnl,
38+
message_to_json,
3839
)
3940

4041

@@ -102,10 +103,10 @@ def showInfo(self, file=sys.stdout): # pylint: disable=W0613
102103
owner = f"Owner: {self.getLongName()} ({self.getShortName()})"
103104
myinfo = ""
104105
if self.myInfo:
105-
myinfo = f"\nMy info: {stripnl(MessageToJson(self.myInfo))}"
106+
myinfo = f"\nMy info: {message_to_json(self.myInfo)}"
106107
metadata = ""
107108
if self.metadata:
108-
metadata = f"\nMetadata: {stripnl(MessageToJson(self.metadata))}"
109+
metadata = f"\nMetadata: {message_to_json(self.metadata)}"
109110
mesh = "\n\nNodes in mesh: "
110111
nodes = {}
111112
if self.nodes:

meshtastic/node.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
our_exit,
1616
pskToString,
1717
stripnl,
18+
message_to_json,
1819
)
1920

2021

@@ -47,8 +48,7 @@ def showChannels(self):
4748
if self.channels:
4849
logging.debug(f"self.channels:{self.channels}")
4950
for c in self.channels:
50-
# print('c.settings.psk:', c.settings.psk)
51-
cStr = stripnl(MessageToJson(c.settings))
51+
cStr = message_to_json(c.settings)
5252
# don't show disabled channels
5353
if channel_pb2.Channel.Role.Name(c.role) != "DISABLED":
5454
print(
@@ -64,11 +64,11 @@ def showInfo(self):
6464
"""Show human readable description of our node"""
6565
prefs = ""
6666
if self.localConfig:
67-
prefs = stripnl(MessageToJson(self.localConfig))
67+
prefs = message_to_json(self.localConfig)
6868
print(f"Preferences: {prefs}\n")
6969
prefs = ""
7070
if self.moduleConfig:
71-
prefs = stripnl(MessageToJson(self.moduleConfig))
71+
prefs = message_to_json(self.moduleConfig)
7272
print(f"Module preferences: {prefs}\n")
7373
self.showChannels()
7474

meshtastic/tests/test_util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
"""Meshtastic unit tests for util.py"""
22

3+
import json
34
import logging
45
import re
56
from unittest.mock import patch
67

78
import pytest
89

910
from meshtastic.supported_device import SupportedDevice
11+
from meshtastic.mesh_pb2 import MyNodeInfo
1012
from meshtastic.util import (
1113
Timeout,
1214
active_ports_on_supported_devices,
@@ -30,6 +32,7 @@
3032
snake_to_camel,
3133
stripnl,
3234
support_info,
35+
message_to_json,
3336
)
3437

3538

@@ -545,3 +548,9 @@ def test_active_ports_on_supported_devices_mac_duplicates_check(mock_platform, m
545548
}
546549
mock_platform.assert_called()
547550
mock_sp.assert_called()
551+
552+
@pytest.mark.unit
553+
def test_message_to_json_shows_all():
554+
actual = json.loads(message_to_json(MyNodeInfo()))
555+
expected = { "myNodeNum": 0, "rebootCount": 0, "minAppVersion": 0 }
556+
assert actual == expected

meshtastic/util.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import time
1212
import traceback
1313
from queue import Queue
14+
from google.protobuf.json_format import MessageToJson
1415

1516
import pkg_resources
1617
import requests
@@ -22,7 +23,6 @@
2223
"""Some devices such as a seger jlink we never want to accidentally open"""
2324
blacklistVids = dict.fromkeys([0x1366])
2425

25-
2626
def quoteBooleans(a_string):
2727
"""Quote booleans
2828
given a string that contains ": true", replace with ": 'true'" (or false)
@@ -605,3 +605,7 @@ def check_if_newer_version():
605605
) <= pkg_resources.parse_version(act_version):
606606
return None
607607
return pypi_version
608+
609+
def message_to_json(message):
610+
return stripnl(MessageToJson(message, always_print_fields_with_no_presence=True))
611+

0 commit comments

Comments
 (0)