Module mqtt implemented
This commit is contained in:
parent
82521d16c1
commit
0e2a063bd1
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -10,3 +10,6 @@
|
||||
[submodule "task"]
|
||||
path = task
|
||||
url = https://git.mount-mockery.de/pylib/task.git
|
||||
[submodule "mqtt"]
|
||||
path = mqtt
|
||||
url = https://git.mount-mockery.de/pylib/mqtt.git
|
||||
|
2
leyk.py
2
leyk.py
@ -20,4 +20,4 @@ if __name__ == '__main__':
|
||||
|
||||
while True:
|
||||
time.sleep(30)
|
||||
#l.publish_states()
|
||||
l.publish_states()
|
||||
|
1
mqtt
Submodule
1
mqtt
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit cf97fa066cdff0e2f7eda0ff4d3c8c0f59c3f2ec
|
@ -9,7 +9,7 @@ import config
|
||||
import geo
|
||||
import json
|
||||
import logging
|
||||
import paho.mqtt.client as mqtt
|
||||
import mqtt
|
||||
import state_machine
|
||||
import task
|
||||
import random
|
||||
@ -309,7 +309,7 @@ class leyk(object):
|
||||
RX_TOPIC_MODE = config.MQTT_TOPIC + '/set/mode'
|
||||
RX_TOPIC_PLOENLEIN = config.MQTT_TOPIC + '/set/Ploenlein'
|
||||
RX_TOPIC_REESE_HOUSE = config.MQTT_TOPIC + '/set/Reese House'
|
||||
|
||||
|
||||
RX_TOPICS = [
|
||||
RX_TOPIC_BAKE_HOUSE,
|
||||
RX_TOPIC_BAKERY,
|
||||
@ -319,18 +319,15 @@ class leyk(object):
|
||||
RX_TOPIC_REESE_HOUSE
|
||||
]
|
||||
|
||||
|
||||
def __init__(self):
|
||||
self.__client__ = mqtt.Client("mqtt_leyk") # create client object
|
||||
self.__client__.on_message = self.__receive__ # attach function to callback
|
||||
self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS) # login with credentials
|
||||
try:
|
||||
self.__client__.connect(config.MQTT_SERVER, 1883) # establish connection
|
||||
self.__client__.loop_start() # start the loop
|
||||
self.__topics__ = []
|
||||
for topic in self.RX_TOPICS:
|
||||
self.__client__.subscribe(topic) # subscibe a topic
|
||||
except (socket.timeout, OSError) as e:
|
||||
logger.warning("Error while setting up mqtt instance and listener")
|
||||
self.__client__ = mqtt.mqtt_client(config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
|
||||
self.__client__.add_callback(self.RX_TOPIC_BAKE_HOUSE, self.__rx_bake_house__)
|
||||
self.__client__.add_callback(self.RX_TOPIC_BAKERY, self.__rx_bakery__)
|
||||
self.__client__.add_callback(self.RX_TOPIC_MILL, self.__rx_mill__)
|
||||
self.__client__.add_callback(self.RX_TOPIC_PLOENLEIN, self.__rx_ploenlein__)
|
||||
self.__client__.add_callback(self.RX_TOPIC_REESE_HOUSE, self.__rx_reese_house__)
|
||||
self.__client__.add_callback(self.RX_TOPIC_MODE, self.__rx_mode__)
|
||||
|
||||
self.TOPIC_DATA = {
|
||||
self.TOPIC_BAKE_HOUSE: self.get_bake_house,
|
||||
@ -368,22 +365,23 @@ class leyk(object):
|
||||
self.publish(self.TOPIC_MODE_BOOL)
|
||||
self.publish(self.TOPIC_STATE)
|
||||
|
||||
def __receive__(self, client, userdata, message):
|
||||
logger.info("Received message %s with %s", message.topic, str(message.payload))
|
||||
if message.topic == self.RX_TOPIC_MODE:
|
||||
self.set_mode(self.sm_mode.STATE_AUTOMATIC if message.payload == b'true' else self.sm_mode.STATE_MANUAL)
|
||||
elif message.topic == self.RX_TOPIC_BAKE_HOUSE:
|
||||
self.set_bake_house(message.payload == b'true')
|
||||
elif message.topic == self.RX_TOPIC_BAKERY:
|
||||
self.set_bakery(message.payload == b'true')
|
||||
elif message.topic == self.RX_TOPIC_MILL:
|
||||
self.set_mill(message.payload == b'true')
|
||||
elif message.topic == self.RX_TOPIC_PLOENLEIN:
|
||||
self.set_ploenlein(message.payload == b'true')
|
||||
elif message.topic == self.RX_TOPIC_REESE_HOUSE:
|
||||
self.set_reese_house(message.payload == b'true')
|
||||
else:
|
||||
logger.warning("Ignoring unknown mqtt topic %s", message.topic)
|
||||
def __rx_bake_house__(self, client, userdata, message):
|
||||
self.set_bake_house(message.payload == b'true')
|
||||
|
||||
def __rx_bakery__(self, client, userdata, message):
|
||||
self.set_bakery(message.payload == b'true')
|
||||
|
||||
def __rx_mill__(self, client, userdata, message):
|
||||
self.set_mill(message.payload == b'true')
|
||||
|
||||
def __rx_ploenlein__(self, client, userdata, message):
|
||||
self.set_ploenlein(message.payload == b'true')
|
||||
|
||||
def __rx_reese_house__(self, client, userdata, message):
|
||||
self.set_reese_house(message.payload == b'true')
|
||||
|
||||
def __rx_mode__(self, client, userdata, message):
|
||||
self.set_mode(self.sm_mode.STATE_AUTOMATIC if message.payload == b'true' else self.sm_mode.STATE_MANUAL)
|
||||
|
||||
def publish_states(self):
|
||||
for name in self.TOPIC_DATA:
|
||||
@ -392,7 +390,7 @@ class leyk(object):
|
||||
def publish(self, topic):
|
||||
logger.info("Sending Leyk status information to mqtt %s = %s", topic, json.dumps(self.TOPIC_DATA[topic]()))
|
||||
try:
|
||||
self.__client__.publish(topic, json.dumps(self.TOPIC_DATA[topic]()))
|
||||
self.__client__.send(topic, json.dumps(self.TOPIC_DATA[topic]()))
|
||||
except (socket.timeout, OSError) as e:
|
||||
logger.warning("Error while sending state information information")
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user