Merge pull request #39 from DeanCording/main

Added option to send data as a single MQTT message in JSON format.
This commit is contained in:
sparky8512 2022-02-27 14:07:24 -08:00 committed by GitHub
commit 3977f0b422
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -17,6 +17,8 @@ Where *id_value* is the *id* value from the dish status information.
import logging import logging
import sys import sys
import time import time
import json
try: try:
import ssl import ssl
@ -43,6 +45,7 @@ def parse_args():
group.add_argument("-p", "--port", type=int, help="Port number to use on MQTT broker") group.add_argument("-p", "--port", type=int, help="Port number to use on MQTT broker")
group.add_argument("-P", "--password", help="Set password for username/password authentication") group.add_argument("-P", "--password", help="Set password for username/password authentication")
group.add_argument("-U", "--username", help="Set username for authentication") group.add_argument("-U", "--username", help="Set username for authentication")
group.add_argument("-J", "--json", action="store_true", help="Publish data as JSON")
if ssl_ok: if ssl_ok:
def wrap_ca_arg(arg): def wrap_ca_arg(arg):
@ -91,6 +94,24 @@ def parse_args():
def loop_body(opts, gstate): def loop_body(opts, gstate):
msgs = [] msgs = []
if opts.json:
data = {}
def cb_add_item(key, val, category):
if not "dish_{0}".format(category) in data:
data["dish_{0}".format(category)] = {}
data["dish_{0}".format(category)].update({key: val})
def cb_add_sequence(key, val, category, _):
if not "dish_{0}".format(category) in data:
data["dish_{0}".format(category)] = {}
data["dish_{0}".format(category)].update({key: list(val)})
else:
def cb_add_item(key, val, category): def cb_add_item(key, val, category):
msgs.append(("starlink/dish_{0}/{1}/{2}".format(category, gstate.dish_id, msgs.append(("starlink/dish_{0}/{1}/{2}".format(category, gstate.dish_id,
key), val, 0, False)) key), val, 0, False))
@ -102,6 +123,9 @@ def loop_body(opts, gstate):
rc = dish_common.get_data(opts, gstate, cb_add_item, cb_add_sequence)[0] rc = dish_common.get_data(opts, gstate, cb_add_item, cb_add_sequence)[0]
if opts.json:
msgs.append(("starlink/{0}".format(gstate.dish_id), json.dumps(data), 0, False))
if msgs: if msgs:
try: try:
paho.mqtt.publish.multiple(msgs, client_id=gstate.dish_id, **opts.mqargs) paho.mqtt.publish.multiple(msgs, client_id=gstate.dish_id, **opts.mqargs)