mirror of
https://github.com/meshtastic/python.git
synced 2026-01-18 12:48:03 -05:00
fix #20 - allow setting binary arrays (i.e. psk) with strings of hex digits
This commit is contained in:
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@@ -26,7 +26,7 @@
|
||||
"request": "launch",
|
||||
"module": "meshtastic",
|
||||
"justMyCode": true,
|
||||
"args": ["--debug", "--set", "a", "1", "--set", "b", "2"]
|
||||
"args": ["--debug", "--setchan", "psk", "fish"]
|
||||
},
|
||||
{
|
||||
"name": "meshtastic shell",
|
||||
|
||||
@@ -48,6 +48,18 @@ If you want to see all packets, simply subscribe to "meshtastic.receive".</li>
|
||||
<li>meshtastic.receive.data(packet)</li>
|
||||
<li>meshtastic.node.updated(node = NodeInfo) - published when a node in the DB changes (appears, location changed, username changed, etc…)</li>
|
||||
</ul>
|
||||
<p>We receive position, user, or data packets from the mesh.
|
||||
You probably only care about meshtastic.receive.data.
|
||||
The first argument for
|
||||
that publish will be the packet.
|
||||
Text or binary data packets (from sendData or sendText) will both arrive this way.
|
||||
If you print packet
|
||||
you'll see the fields in the dictionary.
|
||||
decoded.data.payload will contain the raw bytes that were sent.
|
||||
If the packet was sent with
|
||||
sendText, decoded.data.text will <strong>also</strong> be populated with the decoded string.
|
||||
For ASCII these two strings will be the same, but for
|
||||
unicode scripts they can be different.</p>
|
||||
<h1 id="example-usage">Example Usage</h1>
|
||||
<pre><code>import meshtastic
|
||||
from pubsub import pub
|
||||
@@ -97,6 +109,12 @@ type of packet, you should subscribe to the full topic name. If you want to see
|
||||
- meshtastic.receive.data(packet)
|
||||
- meshtastic.node.updated(node = NodeInfo) - published when a node in the DB changes (appears, location changed, username changed, etc...)
|
||||
|
||||
We receive position, user, or data packets from the mesh. You probably only care about meshtastic.receive.data. The first argument for
|
||||
that publish will be the packet. Text or binary data packets (from sendData or sendText) will both arrive this way. If you print packet
|
||||
you'll see the fields in the dictionary. decoded.data.payload will contain the raw bytes that were sent. If the packet was sent with
|
||||
sendText, decoded.data.text will **also** be populated with the decoded string. For ASCII these two strings will be the same, but for
|
||||
unicode scripts they can be different.
|
||||
|
||||
# Example Usage
|
||||
```
|
||||
import meshtastic
|
||||
|
||||
@@ -7,6 +7,8 @@ import sys
|
||||
from pubsub import pub
|
||||
import google.protobuf.json_format
|
||||
import pyqrcode
|
||||
import traceback
|
||||
import codecs
|
||||
|
||||
"""The command line arguments"""
|
||||
args = None
|
||||
@@ -87,39 +89,35 @@ def onConnected(interface):
|
||||
if args.set or args.setstr or args.setchan:
|
||||
closeNow = True
|
||||
|
||||
# Handle the int/float/bool arguments
|
||||
for pref in (args.set or []):
|
||||
name = pref[0]
|
||||
#
|
||||
def setPref(attributes, name, val):
|
||||
"""Set a preferences value"""
|
||||
print(f"Setting {name} to {val}")
|
||||
try:
|
||||
valstr = pref[1]
|
||||
val = fromStr(valstr)
|
||||
print(f"Setting preference {name} to {val}")
|
||||
setattr(interface.radioConfig.preferences, name, val)
|
||||
try:
|
||||
setattr(attributes, name, val)
|
||||
except TypeError as ex:
|
||||
# The setter didn't like our arg type - try again as a byte array (so we can convert strings to bytearray)
|
||||
if isinstance(val, str):
|
||||
setattr(attributes, name,
|
||||
codecs.decode(val, "hex"))
|
||||
else:
|
||||
print(f"Incorrect type for {name} {ex}")
|
||||
except Exception as ex:
|
||||
print(f"Can't set {name} due to {ex}")
|
||||
|
||||
# Handle the int/float/bool arguments
|
||||
for pref in (args.set or []):
|
||||
setPref(
|
||||
interface.radioConfig.preferences, pref[0], fromStr(pref[1]))
|
||||
|
||||
# Handle the string arguments
|
||||
for pref in (args.setstr or []):
|
||||
name = pref[0]
|
||||
# try to parse as int, float or bool
|
||||
try:
|
||||
val = pref[1]
|
||||
print(f"Setting preference {name} to {val}")
|
||||
setattr(interface.radioConfig.preferences, name, val)
|
||||
except Exception as ex:
|
||||
print(f"Can't set {name} due to {ex}")
|
||||
interface.radioConfig.preferences, setPref(pref[0], pref[1])
|
||||
|
||||
# Handle the channel settings
|
||||
for pref in (args.setchan or []):
|
||||
name = pref[0]
|
||||
#
|
||||
try:
|
||||
valstr = pref[1]
|
||||
val = fromStr(valstr)
|
||||
print(f"Setting channel parameter {name} to {val}")
|
||||
setattr(interface.radioConfig.channel_settings, name, val)
|
||||
except Exception as ex:
|
||||
print(f"Can't set {name} due to {ex}")
|
||||
setPref(interface.radioConfig.channel_settings,
|
||||
pref[0], fromStr(pref[1]))
|
||||
|
||||
print("Writing modified preferences to device")
|
||||
interface.writeConfig()
|
||||
|
||||
2
setup.py
2
setup.py
@@ -12,7 +12,7 @@ with open("README.md", "r") as fh:
|
||||
# This call to setup() does all the work
|
||||
setup(
|
||||
name="meshtastic",
|
||||
version="1.0.10",
|
||||
version="1.0.11",
|
||||
description="Python API & client shell for talking to Meshtastic devices",
|
||||
long_description=long_description,
|
||||
long_description_content_type="text/markdown",
|
||||
|
||||
Reference in New Issue
Block a user