Compare commits

...

4 Commits
v0.1.2 ... main

3 changed files with 29 additions and 8 deletions

View File

@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup(
name="meshtastic-mqtt-json",
version='0.1.2',
version='1.0.4',
author='acidvegas',
author_email='acid.vegas@acid.vegas',
description='A lightweight Python library for parsing Meshtastic MQTT messages',

View File

@ -4,6 +4,6 @@ Meshtastic MQTT Interface - A lightweight Python library for parsing Meshtastic
from .client import MeshtasticMQTT
__version__ = "0.1.2"
__version__ = "1.0.4"
__author__ = "acidvegas"
__license__ = "ISC"

View File

@ -135,15 +135,20 @@ class MeshtasticMQTT(object):
:param client: The client instance for this callback
:param userdata: The private user data as set in Client() or user_data_set()
:param msg: An instance of MQTTMessage. This is a
:param msg: An instance of MQTTMessage
'''
try:
# Define the service envelope
service_envelope = mqtt_pb2.ServiceEnvelope()
# Parse the message payload
service_envelope.ParseFromString(msg.payload)
try:
# Parse the message payload
service_envelope.ParseFromString(msg.payload)
except Exception as e:
print(f'Error parsing service envelope: {e}')
print(f'Raw payload: {msg.payload}')
return
# Extract the message packet from the service envelope
mp = service_envelope.packet
@ -162,7 +167,22 @@ class MeshtasticMQTT(object):
if self.filters and portnum_name not in self.filters:
return
json_packet = json.loads(MessageToJson(mp))
# Convert to JSON with parsing options to handle NaN
json_packet = json.loads(MessageToJson(mp, including_default_value_fields=True,
preserving_proto_field_name=True,
float_precision=10))
# Replace NaN values with null in the JSON structure
def replace_nan(obj):
if isinstance(obj, dict):
return {k: replace_nan(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [replace_nan(x) for x in obj]
elif isinstance(obj, str) and obj == 'NaN':
return None
return obj
json_packet = replace_nan(json_packet)
# Process the message based on its type
if mp.decoded.portnum == portnums_pb2.ADMIN_APP:
@ -299,7 +319,8 @@ class MeshtasticMQTT(object):
except Exception as e:
print(f'Error processing message: {e}')
print(mp)
print(f'Topic: {msg.topic}')
print(f'Payload: {msg.payload}')
def main():