refactore comm module

This commit is contained in:
Thomas Klaehn 2016-12-23 16:35:44 +01:00 committed by Thomas Klaehn
parent 22cd563d42
commit a07d62de10
3 changed files with 56 additions and 45 deletions

View File

@ -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

View File

@ -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

View File

@ -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)