2020-10-15 14:44:34 +00:00
|
|
|
import json
|
|
|
|
|
2020-10-15 17:07:17 +00:00
|
|
|
import paho.mqtt.client as cl
|
|
|
|
import paho.mqtt.subscribe as mqtt
|
2020-10-15 14:44:34 +00:00
|
|
|
|
2020-10-15 17:07:17 +00:00
|
|
|
datafile = "clients.json"
|
2020-10-15 14:44:34 +00:00
|
|
|
|
2020-10-15 17:07:17 +00:00
|
|
|
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))
|
2020-10-15 14:44:34 +00:00
|
|
|
|
|
|
|
with open(datafile, 'w') as outfile:
|
2020-10-15 17:07:17 +00:00
|
|
|
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
|
|
|
|
})
|