Files
picar/Messaging/mqttsession.py
2018-12-19 16:31:33 +10:30

33 lines
783 B
Python

import paho.mqtt.client as mqtt
client = mqtt.Client()
host = None
"""
Wrapper module for paho mqtt library, providing a singleton instance of the client to be used.
"""
# Arguably not needed, just want to make the client static, but here anyway.
def connect():
global client
if client is None or host is None:
print("Error: Client and/or host are not initialised.")
else:
client.connect(host, port=1883, keepalive=60, bind_address="")
client.loop_start()
def disconnect():
global client
if client is not None:
client.loop_stop()
client.disconnect()
else:
print("Error: Client is not initialised.")
def Client():
global client
if client is None:
client = mqtt.Client()
return client