torch-subscriber-simple/torch_sub/torch_sub.py

44 lines
1.1 KiB
Python

import json
import paho.mqtt.client as cl
import paho.mqtt.subscribe as mqtt
datafile = "clients.json"
def updateOnion(client, userdata, message):
with open(datafile, 'r') as infile:
database = json.load(infile)
payload = message.payload.decode('utf-8')
print("Payload: %s" % (payload))
response = json.loads(payload)
print(response)
print("Response: %s" % (response))
print("Database: %s" % (database))
database[response['clientId']] = response
print("got one! %s %s %s" % (client, userdata, payload))
with open(datafile, 'w') as outfile:
json.dump(database, outfile)
def attach(host, port, datafileIn, mqtt_ca_file=None, mqtt_cert_file=None, mqtt_key_file=None, topic=None):
datafile = datafileIn
mqtt.callback(updateOnion,
topic,
hostname=host,
port=port,
tls={
'ca_certs': mqtt_ca_file,
'certfile': mqtt_cert_file,
'keyfile': mqtt_key_file
})