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:
commit
3977f0b422
1 changed files with 31 additions and 7 deletions
|
@ -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,17 +94,38 @@ def parse_args():
|
||||||
def loop_body(opts, gstate):
|
def loop_body(opts, gstate):
|
||||||
msgs = []
|
msgs = []
|
||||||
|
|
||||||
def cb_add_item(key, val, category):
|
if opts.json:
|
||||||
msgs.append(("starlink/dish_{0}/{1}/{2}".format(category, gstate.dish_id,
|
|
||||||
key), val, 0, False))
|
|
||||||
|
|
||||||
def cb_add_sequence(key, val, category, _):
|
data = {}
|
||||||
msgs.append(
|
|
||||||
("starlink/dish_{0}/{1}/{2}".format(category, gstate.dish_id,
|
def cb_add_item(key, val, category):
|
||||||
key), ",".join(str(x) for x in val), 0, False))
|
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):
|
||||||
|
msgs.append(("starlink/dish_{0}/{1}/{2}".format(category, gstate.dish_id,
|
||||||
|
key), val, 0, False))
|
||||||
|
|
||||||
|
def cb_add_sequence(key, val, category, _):
|
||||||
|
msgs.append(
|
||||||
|
("starlink/dish_{0}/{1}/{2}".format(category, gstate.dish_id,
|
||||||
|
key), ",".join(str(x) for x in val), 0, False))
|
||||||
|
|
||||||
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)
|
||||||
|
|
Loading…
Reference in a new issue