diff --git a/.vscode/launch.json b/.vscode/launch.json index 8404299..1974ec8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -35,6 +35,14 @@ "module": "meshtastic", "justMyCode": true, "args": ["--test"] + }, + { + "name": "meshtastic sendtext", + "type": "python", + "request": "launch", + "module": "meshtastic", + "justMyCode": true, + "args": ["--debug", "--sendtext", "pytest"] } ] } diff --git a/meshtastic/__init__.py b/meshtastic/__init__.py index 4be463a..53ae30a 100644 --- a/meshtastic/__init__.py +++ b/meshtastic/__init__.py @@ -87,7 +87,7 @@ class MeshInterface: self.isConnected = False self._startConfig() - def sendText(self, text, destinationId=BROADCAST_ADDR, wantAck=False): + def sendText(self, text, destinationId=BROADCAST_ADDR, wantAck=False, wantResponse=False): """Send a utf8 string to some other node, if the node has a display it will also be shown on the device. Arguments: @@ -97,13 +97,14 @@ class MeshInterface: destinationId {nodeId or nodeNum} -- where to send this message (default: {BROADCAST_ADDR}) """ self.sendData(text.encode("utf-8"), destinationId, - dataType=mesh_pb2.Data.CLEAR_TEXT, wantAck=wantAck) + dataType=mesh_pb2.Data.CLEAR_TEXT, wantAck=wantAck, wantResponse=wantResponse) - def sendData(self, byteData, destinationId=BROADCAST_ADDR, dataType=mesh_pb2.Data.OPAQUE, wantAck=False): + def sendData(self, byteData, destinationId=BROADCAST_ADDR, dataType=mesh_pb2.Data.OPAQUE, wantAck=False, wantResponse=False): """Send a data packet to some other node""" meshPacket = mesh_pb2.MeshPacket() meshPacket.decoded.data.payload = byteData meshPacket.decoded.data.typ = dataType + meshPacket.decoded.want_response = wantResponse self.sendPacket(meshPacket, destinationId, wantAck=wantAck) def sendPacket(self, meshPacket, destinationId=BROADCAST_ADDR, wantAck=False): diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 8fcf812..7159503 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -11,10 +11,14 @@ import google.protobuf.json_format args = None -def onReceive(packet): +def onReceive(packet, interface): """Callback invoked when a packet arrives""" print(f"Received: {packet}") + # Exit once we receive a reply + if args.sendtext and packet["to"] == interface.myInfo.my_node_num: + interface.close() # after running command then exit + def onConnection(interface, topic=pub.AUTO_TOPIC): """Callback invoked when we connect/disconnect from a radio""" @@ -28,7 +32,8 @@ def onConnected(interface): try: if args.sendtext: print(f"Sending text message {args.sendtext} to {args.dest}") - interface.sendText(args.sendtext, args.dest) + interface.sendText(args.sendtext, args.dest, + wantAck=True, wantResponse=True) if args.setpref: name = args.setpref[0] @@ -46,7 +51,7 @@ def onConnected(interface): except Exception as ex: print(ex) - if args.info or args.setpref or args.sendtext: + if args.info or args.setpref: interface.close() # after running command then exit diff --git a/setup.py b/setup.py index 17e08a0..740561a 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ with open("README.md", "r") as fh: # This call to setup() does all the work setup( name="meshtastic", - version="0.5.3", + version="0.5.4", description="Python API & client shell for talking to Meshtastic devices", long_description=long_description, long_description_content_type="text/markdown",