Skip to content

Commit 158cac6

Browse files
authored
Merge pull request #555 from ianmcorvidae/channel-indices
Better support --ch-index for other commands (traceroute, telemetry, position)
2 parents bb6e3b7 + 4d10b6e commit 158cac6

2 files changed

Lines changed: 43 additions & 30 deletions

File tree

meshtastic/__main__.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from meshtastic import channel_pb2, config_pb2, portnums_pb2, remote_hardware, BROADCAST_ADDR
2121
from meshtastic.version import get_active_version
2222
from meshtastic.ble_interface import BLEInterface
23+
from meshtastic.mesh_interface import MeshInterface
2324

2425
def onReceive(packet, interface):
2526
"""Callback invoked when a packet arrives"""
@@ -56,6 +57,11 @@ def onConnection(interface, topic=pub.AUTO_TOPIC): # pylint: disable=W0613
5657
"""Callback invoked when we connect/disconnect from a radio"""
5758
print(f"Connection changed: {topic.getName()}")
5859

60+
def checkChannel(interface: MeshInterface, channelIndex: int) -> bool:
61+
"""Given an interface and channel index, return True if that channel is non-disabled on the local node"""
62+
ch = interface.localNode.getChannelByChannelIndex(channelIndex)
63+
logging.debug(f"ch:{ch}")
64+
return (ch and ch.role != channel_pb2.Channel.Role.DISABLED)
5965

6066
def getPref(node, comp_name):
6167
"""Get a channel or preferences value"""
@@ -403,12 +409,8 @@ def onConnected(interface):
403409

404410
if args.sendtext:
405411
closeNow = True
406-
channelIndex = 0
407-
if args.ch_index is not None:
408-
channelIndex = int(args.ch_index)
409-
ch = interface.localNode.getChannelByChannelIndex(channelIndex)
410-
logging.debug(f"ch:{ch}")
411-
if ch and ch.role != channel_pb2.Channel.Role.DISABLED:
412+
channelIndex = mt_config.channel_index or 0
413+
if checkChannel(interface, channelIndex):
412414
print(
413415
f"Sending text message {args.sendtext} to {args.dest} on channelIndex:{channelIndex}"
414416
)
@@ -428,22 +430,28 @@ def onConnected(interface):
428430
loraConfig = getattr(interface.localNode.localConfig, "lora")
429431
hopLimit = getattr(loraConfig, "hop_limit")
430432
dest = str(args.traceroute)
431-
print(f"Sending traceroute request to {dest} (this could take a while)")
432-
interface.sendTraceRoute(dest, hopLimit)
433+
channelIndex = mt_config.channel_index or 0
434+
if checkChannel(interface, channelIndex):
435+
print(f"Sending traceroute request to {dest} on channelIndex:{channelIndex} (this could take a while)")
436+
interface.sendTraceRoute(dest, hopLimit, channelIndex=channelIndex)
433437

434438
if args.request_telemetry:
435439
if args.dest == BROADCAST_ADDR:
436440
meshtastic.util.our_exit("Warning: Must use a destination node ID.")
437441
else:
438-
print(f"Sending telemetry request to {args.dest} (this could take a while)")
439-
interface.sendTelemetry(destinationId=args.dest, wantResponse=True)
442+
channelIndex = mt_config.channel_index or 0
443+
if checkChannel(interface, channelIndex):
444+
print(f"Sending telemetry request to {args.dest} on channelIndex:{channelIndex} (this could take a while)")
445+
interface.sendTelemetry(destinationId=args.dest, wantResponse=True, channelIndex=channelIndex)
440446

441447
if args.request_position:
442448
if args.dest == BROADCAST_ADDR:
443449
meshtastic.util.our_exit("Warning: Must use a destination node ID.")
444450
else:
445-
print(f"Sending position request to {args.dest} (this could take a while)")
446-
interface.sendPosition(destinationId=args.dest, wantResponse=True)
451+
channelIndex = mt_config.channel_index or 0
452+
if checkChannel(interface, channelIndex):
453+
print(f"Sending position request to {args.dest} on channelIndex:{channelIndex} (this could take a while)")
454+
interface.sendPosition(destinationId=args.dest, wantResponse=True, channelIndex=channelIndex)
447455

448456
if args.gpio_wrb or args.gpio_rd or args.gpio_watch:
449457
if args.dest == BROADCAST_ADDR:

meshtastic/mesh_interface.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ def sendPosition(
351351
destinationId: Union[int, str]=BROADCAST_ADDR,
352352
wantAck: bool=False,
353353
wantResponse: bool=False,
354+
channelIndex: int=0,
354355
):
355356
"""
356357
Send a position packet to some other node (normally a broadcast)
@@ -393,6 +394,7 @@ def sendPosition(
393394
wantAck=wantAck,
394395
wantResponse=wantResponse,
395396
onResponse=onResponse,
397+
channelIndex=channelIndex,
396398
)
397399
if wantResponse:
398400
self.waitForPosition()
@@ -426,7 +428,7 @@ def onResponsePosition(self, p):
426428
if p["decoded"]["routing"]["errorReason"] == 'NO_RESPONSE':
427429
our_exit("No response from node. At least firmware 2.1.22 is required on the destination node.")
428430

429-
def sendTraceRoute(self, dest: Union[int, str], hopLimit: int):
431+
def sendTraceRoute(self, dest: Union[int, str], hopLimit: int, channelIndex: int=0):
430432
"""Send the trace route"""
431433
r = mesh_pb2.RouteDiscovery()
432434
self.sendData(
@@ -435,6 +437,7 @@ def sendTraceRoute(self, dest: Union[int, str], hopLimit: int):
435437
portNum=portnums_pb2.PortNum.TRACEROUTE_APP,
436438
wantResponse=True,
437439
onResponse=self.onResponseTraceRoute,
440+
channelIndex=channelIndex,
438441
)
439442
# extend timeout based on number of nodes, limit by configured hopLimit
440443
waitFactor = min(len(self.nodes) - 1 if self.nodes else 0, hopLimit)
@@ -456,26 +459,27 @@ def onResponseTraceRoute(self, p):
456459

457460
self._acknowledgment.receivedTraceRoute = True
458461

459-
def sendTelemetry(self, destinationId=BROADCAST_ADDR, wantResponse=False):
462+
def sendTelemetry(self, destinationId: Union[int,str]=BROADCAST_ADDR, wantResponse: bool=False, channelIndex: int=0):
460463
"""Send telemetry and optionally ask for a response"""
461464
r = telemetry_pb2.Telemetry()
462465

463-
node = next(n for n in self.nodes.values() if n["num"] == self.localNode.nodeNum)
464-
if node is not None:
465-
metrics = node.get("deviceMetrics")
466-
if metrics:
467-
batteryLevel = metrics.get("batteryLevel")
468-
if batteryLevel is not None:
469-
r.device_metrics.battery_level = batteryLevel
470-
voltage = metrics.get("voltage")
471-
if voltage is not None:
472-
r.device_metrics.voltage = voltage
473-
channel_utilization = metrics.get("channelUtilization")
474-
if channel_utilization is not None:
475-
r.device_metrics.channel_utilization = channel_utilization
476-
air_util_tx = metrics.get("airUtilTx")
477-
if air_util_tx is not None:
478-
r.device_metrics.air_util_tx = air_util_tx
466+
if self.nodes is not None:
467+
node = next(n for n in self.nodes.values() if n["num"] == self.localNode.nodeNum)
468+
if node is not None:
469+
metrics = node.get("deviceMetrics")
470+
if metrics:
471+
batteryLevel = metrics.get("batteryLevel")
472+
if batteryLevel is not None:
473+
r.device_metrics.battery_level = batteryLevel
474+
voltage = metrics.get("voltage")
475+
if voltage is not None:
476+
r.device_metrics.voltage = voltage
477+
channel_utilization = metrics.get("channelUtilization")
478+
if channel_utilization is not None:
479+
r.device_metrics.channel_utilization = channel_utilization
480+
air_util_tx = metrics.get("airUtilTx")
481+
if air_util_tx is not None:
482+
r.device_metrics.air_util_tx = air_util_tx
479483

480484
if wantResponse:
481485
onResponse = self.onResponseTelemetry
@@ -488,6 +492,7 @@ def sendTelemetry(self, destinationId=BROADCAST_ADDR, wantResponse=False):
488492
portNum=portnums_pb2.PortNum.TELEMETRY_APP,
489493
wantResponse=wantResponse,
490494
onResponse=onResponse,
495+
channelIndex=channelIndex,
491496
)
492497
if wantResponse:
493498
self.waitForTelemetry()

0 commit comments

Comments
 (0)