Add disconnect handler
This commit is contained in:
parent
73dcd8879e
commit
f849f0f95b
1 changed files with 11 additions and 3 deletions
|
@ -12,9 +12,13 @@ import psycopg2
|
||||||
import json
|
import json
|
||||||
import base64
|
import base64
|
||||||
import os
|
import os
|
||||||
|
import logging
|
||||||
|
|
||||||
import lorautil
|
import lorautil
|
||||||
|
|
||||||
|
log = logging.getLogger("ttn")
|
||||||
|
log.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
#Call back functions
|
#Call back functions
|
||||||
|
|
||||||
# gives connection message
|
# gives connection message
|
||||||
|
@ -30,19 +34,23 @@ def on_message(client,userdata,msg):
|
||||||
deveui = mydict['dev_eui']
|
deveui = mydict['dev_eui']
|
||||||
payload = base64.b64decode(mydict['payload']).decode('utf-8')
|
payload = base64.b64decode(mydict['payload']).decode('utf-8')
|
||||||
|
|
||||||
print("{}: {}".format(deveui, payload))
|
log.info("Message received: {}: {}".format(deveui, payload))
|
||||||
lorautil.db_insert_json("ttn", myjson, payload, deveui)
|
lorautil.db_insert_json("ttn", myjson, payload, deveui)
|
||||||
lorautil.db_notify("ttn", payload, deveui)
|
lorautil.db_notify("ttn", payload, deveui)
|
||||||
|
|
||||||
def on_log(client,userdata,level,buf):
|
def on_log(client,userdata,level,buf):
|
||||||
print("message:" + msg)
|
log.debug("message:" + msg)
|
||||||
print("userdata:" + str(userdata))
|
log.debug("userdata:" + str(userdata))
|
||||||
|
|
||||||
|
def on_disconnect(client, userdata, rc):
|
||||||
|
log.debug("reconnecting...")
|
||||||
|
client.reconnect()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
mqttc= mqtt.Client()
|
mqttc= mqtt.Client()
|
||||||
mqttc.on_connect=on_connect
|
mqttc.on_connect=on_connect
|
||||||
mqttc.on_message=on_message
|
mqttc.on_message=on_message
|
||||||
|
mqttc.on_disconnect=on_disconnect
|
||||||
|
|
||||||
print("Connecting to ttn")
|
print("Connecting to ttn")
|
||||||
username=os.environ['ttn_user']
|
username=os.environ['ttn_user']
|
||||||
|
|
Loading…
Add table
Reference in a new issue