diff --git a/src/communiate/__init__.py b/src/communiate/__init__.py index e69de29..77070f3 100644 --- a/src/communiate/__init__.py +++ b/src/communiate/__init__.py @@ -0,0 +1,54 @@ +''' +Created on Dec 19, 2016 + +@author: klaehn +''' +import paho.mqtt.client as mqtt_client + +class Mqtt(object): + ''' Wrapper class for mqtt communication ''' + def __init__(self, hostname, port=1883, keepalive=60, qos=2, retain=True): + #pylint: disable=too-many-arguments + self.__hostname = hostname + self.__port = port + self.__keepalive = keepalive + self.__client = mqtt_client.Client() + self.__is_connected = False + self.__qos = qos + self.__retain = retain + + def connect(self): + ''' Connect to mqtt broker ''' + if not self.__is_connected: + res = self.__client.connect(self.__hostname, self.__port, \ + self.__keepalive) + if res != mqtt_client.MQTT_ERR_SUCCESS: + return False + res = self.__client.loop_start() + if res != mqtt_client.MQTT_ERR_SUCCESS: + return False + self.__is_connected = True + return True + return False + + def disconnect(self): + ''' Disconnect from mqtt broker ''' + if self.__is_connected: + self.__client.loop_stop() + self.__client.disconnect() + self.__is_connected = False + return True + return False + + def transmit(self, topic, payload): + ''' Transmit to subscriber via broker''' + was_connected = True + if not self.__is_connected: + was_connected = False + self.connect() + result = self.__client.publish(topic, payload, self.__qos, self.__retain) + if not was_connected: + self.disconnect() + if result == 0: + return True + return False diff --git a/src/communiate/protocol.py b/src/communiate/protocol.py deleted file mode 100644 index f211f4b..0000000 --- a/src/communiate/protocol.py +++ /dev/null @@ -1,43 +0,0 @@ -import paho.mqtt.client as mqtt_client - -class mqtt: - def __init__(self, hostname, port=1883, keepalive=60, qos=2, retain=True): - self.__hostname = hostname - self.__port = port - self.__keepalive = keepalive - self.__client = mqtt_client.Client() - self.__is_connected = False - self.__qos = qos - self.__retain = retain - - def __on_connect(self, client, data, flags, result): - print "Connected: " + str(result) - - def connect(self): - if not self.__is_connected: - self.__client.on_connect = self.__on_connect - self.__client.connect(self.__hostname, self.__port, self.__keepalive) - self.__client.loop_start() - self.__is_connected = True - return True - return False - - def disconnect(self): - if self.__is_connected: - self.__client.loop_stop() - self.__client.disconnect() - self.__is_connected = False - return True - return False - - def transmit(self, topic, payload): - was_connected = True - if not self.__is_connected: - was_connected = False - self.connect() - result = self.__client.publish(topic, payload, self.__qos, self.__retain) - if not was_connected: - self.disconnect() - if result == 0: - return True - return False diff --git a/src/gate/gate_state.py b/src/gate/gate_state.py index 660bf35..46d68f5 100644 --- a/src/gate/gate_state.py +++ b/src/gate/gate_state.py @@ -4,7 +4,7 @@ Created on Dec 19, 2016 @author: klaehn ''' from time import time, sleep -from communiate.protocol import mqtt +from communiate import Mqtt from data_buffer import DataBuffer from light_sensor.light_sensor import light_sensor from gate.gate_handler import GateHandler @@ -49,7 +49,7 @@ class GateState(object): self.__light_sensor = light_sensor(LIGHT_SENSOR_I2C_BUS, \ LIGHT_SENSOR_I2C_ADDRESS) self.__light_data = DataBuffer(LIGHT_CONSECUTIVE_READS) - self.__comserver = mqtt(MQTT_HOST) + self.__comserver = Mqtt(MQTT_HOST) self.__gate_handler = GateHandler() self.__power_sensor = PowerSensor(POWER_SENSOR_I2C_BUS, \ POWER_SENSOR_I2C_ADDRESS)