diff --git a/meshtastic/__init__.py b/meshtastic/__init__.py index 88dde56..8708b62 100644 --- a/meshtastic/__init__.py +++ b/meshtastic/__init__.py @@ -210,6 +210,55 @@ 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 + 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 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")