Merge pull request #29 from timgunter/set_owner

Add setOwner() method to MeshInterface
This commit is contained in:
Kevin Hester
2020-10-28 18:42:15 -07:00
committed by GitHub
2 changed files with 56 additions and 0 deletions

View File

@@ -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

View File

@@ -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")