From 59ea299ad894a86f00ca731732f998009886c9f4 Mon Sep 17 00:00:00 2001 From: thebentern Date: Sat, 4 Dec 2021 17:18:30 -0600 Subject: [PATCH] Added very basic implementation of configure yaml --- example_config.yaml | 15 +++++++++++++ meshtastic/__main__.py | 49 ++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 3 files changed, 65 insertions(+) create mode 100644 example_config.yaml diff --git a/example_config.yaml b/example_config.yaml new file mode 100644 index 0000000..f85efc3 --- /dev/null +++ b/example_config.yaml @@ -0,0 +1,15 @@ +owner: Bob TBeam + +channel_url: https://www.meshtastic.org/d/#CgUYAyIBAQ + +location: + lat: 35.88888 + lon: -93.88888 + alt: 304 + +user_prefs: + region: 1 + is_always_powered: true + send_owner_interval: 2 + screen_on_secs: 31536000 + wait_bluetooth_secs: 31536000 \ No newline at end of file diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 702c828..f1c90c4 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -8,6 +8,7 @@ import logging import sys import time import os +import yaml from pubsub import pub import pyqrcode import pkg_resources @@ -351,6 +352,49 @@ def onConnected(interface): print("Writing modified preferences to device") getNode().writeConfig() + if args.configure: + with open(args.configure[0]) as file: + configuration = yaml.safe_load(file) + closeNow = True + + if 'owner' in configuration: + print(f"Setting device owner to {configuration['owner']}") + getNode().setOwner(configuration['owner']) + + if 'channel_url' in configuration: + print("Setting channel url to", configuration['channel_url']) + getNode().setURL(configuration['channel_url']) + + if 'location' in configuration: + alt = 0 + lat = 0.0 + lon = 0.0 + time = 0 # always set time, but based on the local clock + prefs = interface.localNode.radioConfig.preferences + + if 'alt' in configuration['location']: + alt = int(configuration['location']['alt']) + prefs.fixed_position = True + print(f"Fixing altitude at {alt} meters") + if 'lat' in configuration['location']: + lat = float(configuration['location']['lat']) + prefs.fixed_position = True + print(f"Fixing latitude at {lat} degrees") + if 'lon' in configuration['location']: + lon = float(configuration['location']['lon']) + prefs.fixed_position = True + print(f"Fixing longitude at {lon} degrees") + print("Setting device position") + interface.sendPosition(lat, lon, alt, time) + interface.localNode.writeConfig() + + if 'user_prefs' in configuration: + prefs = getNode().radioConfig.preferences + for pref in configuration['user_prefs']: + setPref(prefs, pref, str(configuration['user_prefs'][pref])) + print("Writing modified preferences to device") + getNode().writeConfig() + if args.seturl: closeNow = True getNode().setURL(args.seturl) @@ -568,6 +612,11 @@ def initParser(): """ Initialize the command line argument parsing.""" global parser, args + parser.add_argument( + "--configure", + help="Specify a path to a yaml(.yml) file containing the desired settings for the connected device.", + action='append') + parser.add_argument( "--port", help="The port the Meshtastic device is connected to, i.e. /dev/ttyUSB0. If unspecified, we'll try to find it.", diff --git a/requirements.txt b/requirements.txt index e9e0f8f..8b43d27 100644 --- a/requirements.txt +++ b/requirements.txt @@ -54,3 +54,4 @@ urllib3==1.26.7 webencodings==0.5.1 wrapt==1.13.3 zipp==3.6.0 +pyyaml==5.3.0 \ No newline at end of file