From 77e5d5acd001632c3a7cd08734f95d51a4fb2afb Mon Sep 17 00:00:00 2001 From: Gunter Tim Date: Sun, 18 Oct 2020 11:10:59 -0700 Subject: [PATCH 1/3] Add setOwner() method to MeshInterface --- meshtastic/__init__.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/meshtastic/__init__.py b/meshtastic/__init__.py index 340adc0..02f569a 100644 --- a/meshtastic/__init__.py +++ b/meshtastic/__init__.py @@ -200,6 +200,33 @@ class MeshInterface: t.set_radio.CopyFrom(self.radioConfig) self._sendToRadio(t) + def setOwner(self, long_name, short_name=None): + """Set device owner name""" + nChars = 3 + minChars = 2 + if long_name is not None: + long_name = long_name.strip() + if short_name is None: + words = long_name.split() + if len(long_name) <= nChars: + short_name = long_name + elif len(words) >= minChars: + short_name = ''.join(map(lambda word: word[0], words)) + else: + trans = str.maketrans(dict.fromkeys('aeiouAEIOU')) + short_name = long_name[0] + long_name[1:].translate(trans) + if len(short_name) < nChars: + short_name = long_name[:nChars] + t = mesh_pb2.ToRadio() + if long_name is not None: + t.set_owner.long_name = long_name + if short_name is not None: + short_name = short_name.strip() + if len(short_name) > nChars: + short_name = short_name[:nChars] + t.set_owner.short_name = short_name + self._sendToRadio(t) + @property def channelURL(self): """The sharable URL that describes the current channel From 647b3ec7bcef59e1803c5d9be6172ab5fe0ac59b Mon Sep 17 00:00:00 2001 From: Gunter Tim Date: Sun, 18 Oct 2020 13:16:04 -0700 Subject: [PATCH 2/3] Added --setowner to main --- meshtastic/__main__.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index b667361..3f0c99a 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -133,6 +133,10 @@ def onConnected(interface): # can include lat/long/alt etc: latitude = 37.5, longitude = -122.1 interface.sendPosition() + if args.setowner: + print(f"Setting device owner to {args.setowner}") + interface.setOwner(args.setowner) + if args.sendtext: print(f"Sending text message {args.sendtext} to {args.dest}") interface.sendText(args.sendtext, args.dest, @@ -256,6 +260,9 @@ def main(): parser.add_argument( "--seturl", help="Set a channel URL", action="store") + parser.add_argument( + "--setowner", help="Set device owner name", action="store") + parser.add_argument( "--dest", help="The destination node id for the --send commands, if not set '^all' is assumed", default="^all") From e4c98ab5ba8794440bea48675a27ea89cf5f5559 Mon Sep 17 00:00:00 2001 From: Gunter Tim Date: Sun, 18 Oct 2020 14:42:43 -0700 Subject: [PATCH 3/3] Add get long and short name methods to MeshInterface --- meshtastic/__init__.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/meshtastic/__init__.py b/meshtastic/__init__.py index 02f569a..af053d9 100644 --- a/meshtastic/__init__.py +++ b/meshtastic/__init__.py @@ -200,6 +200,28 @@ class MeshInterface: t.set_radio.CopyFrom(self.radioConfig) self._sendToRadio(t) + def getMyNode(self): + if self.myInfo is None: + return None + myId = self.myInfo.my_node_num + for _, nodeDict in self.nodes.items(): + if 'num' in nodeDict and nodeDict['num'] == myId: + if 'user' in nodeDict: + return nodeDict['user'] + return None + + def getLongName(self): + user = self.getMyNode() + if user is not None: + return user.get('longName', None) + return None + + def getShortName(self): + user = self.getMyNode() + if user is not None: + return user.get('shortName', None) + return None + def setOwner(self, long_name, short_name=None): """Set device owner name""" nChars = 3