-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqttclient.py
executable file
·61 lines (49 loc) · 1.86 KB
/
mqttclient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
import time
import paho.mqtt.client as mqtt
import re
import sys
import configparser
class MqttClient():
def on_connect(self,client, userdata, flags, rc):
print("connected with result code "+str(rc))
topiclist = []
for t in list(self.handler):
topiclist.append((t,0))
self.client.subscribe(topiclist)
def on_message(self,client, userdata, msg):
found = False
for topic,v in self.handler.items():
if re.match(v[0],msg.topic):
v[1](msg.topic,msg.payload)
found = True
if not found:
print("not found: ",msg.topic)
def on_subscribe(self,client,userdata,mid,granted_qos):
pass
def subscribe(self,topic=None,function=None):
self.handler[topic] = (topic,function)
self.client.subscribe(topic)
def subscribeRegex(self,topic=None,regex=None,function=None):
self.handler[topic] = (regex,function)
self.client.subscribe(topic)
def __init__(self):
self.handler = {}
config = configparser.ConfigParser()
config.read('wallclock.conf')
self.config = config['mqtt']
self.client = mqtt.Client(clean_session=True)
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
self.client.on_subscribe = self.on_subscribe
self.client.username_pw_set(self.config['user'],password=self.config['password'])
self.client.connect(self.config['host'],int(self.config['port']),10)
self.client.loop_start()
if __name__ == "__main__":
def mytest(topic,msg):
print(topic+":"+str(msg))
myclient = MqttClient()
myclient.subscribe("/Chattenweg5/2OG-Flur/temperature",mytest)
myclient.subscribe("/Chattenweg5/2OG-Flur/humitiy",mytest)
while True:
time.sleep(1)