Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
8ffb50e60e | |||
c1b8cbfa0b | |||
608d89ebea | |||
8a7b14e017 |
2
setup.py
2
setup.py
@ -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',
|
||||
|
@ -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"
|
@ -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():
|
||||
|
Loading…
Reference in New Issue
Block a user