|
@@ -0,0 +1,83 @@
|
|
1
|
+#!/usr/bin/env python
|
|
2
|
+# -*- coding: utf-8 -*-
|
|
3
|
+#
|
|
4
|
+"""
|
|
5
|
+mqtt (MQTT)
|
|
6
|
+===========
|
|
7
|
+
|
|
8
|
+**Author:**
|
|
9
|
+
|
|
10
|
+* Dirk Alders <sudo-dirk@mount-mockery.de>
|
|
11
|
+
|
|
12
|
+**Description:**
|
|
13
|
+
|
|
14
|
+ This Module supports functionality for mqtt connections
|
|
15
|
+
|
|
16
|
+**Submodules:**
|
|
17
|
+
|
|
18
|
+* :mod:`mqtt_client`
|
|
19
|
+
|
|
20
|
+**Unittest:**
|
|
21
|
+
|
|
22
|
+ See also the :download:`unittest <mqtt/_testresults_/unittest.pdf>` documentation.
|
|
23
|
+
|
|
24
|
+**Module Documentation:**
|
|
25
|
+
|
|
26
|
+"""
|
|
27
|
+
|
|
28
|
+__DEPENDENCIES__ = []
|
|
29
|
+
|
|
30
|
+import logging
|
|
31
|
+import paho.mqtt.client as paho
|
|
32
|
+import socket
|
|
33
|
+
|
|
34
|
+try:
|
|
35
|
+ from config import APP_NAME as ROOT_LOGGER_NAME
|
|
36
|
+except ImportError:
|
|
37
|
+ ROOT_LOGGER_NAME = 'root'
|
|
38
|
+logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+class mqtt_client(object):
|
|
42
|
+ def __init__(self, name, host, port=1883, username=None, password=None):
|
|
43
|
+ logger.info("Initiating mqtt client instance")
|
|
44
|
+ self.__callbacks__ = {}
|
|
45
|
+ self.__client__ = paho.Client(name) # create client object
|
|
46
|
+ if username is not None and password is not None:
|
|
47
|
+ logger.debug("Configuring authentification")
|
|
48
|
+ self.__client__.username_pw_set(username, password) # login with credentials
|
|
49
|
+ self.__client__.on_message = self.__receive__ # attach function to callback
|
|
50
|
+ self.__client__.on_connect = self.__on_connect__
|
|
51
|
+ self.__client__.on_disconnect = self.__on_disconnect__
|
|
52
|
+ try:
|
|
53
|
+ self.__client__.connect(host, port) # establish connection
|
|
54
|
+ except (socket.timeout, OSError) as e:
|
|
55
|
+ logger.warning("Can not connect to MQTT-Server")
|
|
56
|
+ self.__client__.loop_start() # start the loop
|
|
57
|
+
|
|
58
|
+ def add_callback(self, topic, callback):
|
|
59
|
+ self.__callbacks__[topic] = callback
|
|
60
|
+ self.__client__.subscribe(topic)
|
|
61
|
+
|
|
62
|
+ def send(self, topic, payload):
|
|
63
|
+ self.__client__.publish(topic, payload)
|
|
64
|
+
|
|
65
|
+ def __on_connect__(self, client, userdata, flags, rc):
|
|
66
|
+ logger.debug("Connect with rc=%d", rc)
|
|
67
|
+ if rc == 0:
|
|
68
|
+ logger.debug("Registering topics...")
|
|
69
|
+ for topic in self.__callbacks__:
|
|
70
|
+ self.__client__.subscribe(topic)
|
|
71
|
+
|
|
72
|
+ def __on_disconnect__(self, client, flags, rc):
|
|
73
|
+ logger.warning("Disconnect with rc=%d", rc)
|
|
74
|
+
|
|
75
|
+ def __receive__(self, client, userdata, message):
|
|
76
|
+ logger.debug("Received message with topic %s and payload %s", message.topic, str(message.payload))
|
|
77
|
+ for topic in self.__callbacks__:
|
|
78
|
+ if topic.endswith('#'):
|
|
79
|
+ if message.topic.startswith(topic[:-1]):
|
|
80
|
+ self.__callbacks__[topic](client, userdata, message)
|
|
81
|
+ else:
|
|
82
|
+ if topic == message.topic:
|
|
83
|
+ self.__callbacks__[topic](client, userdata, message)
|