This repository has been archived by the owner on Apr 6, 2020. It is now read-only.
forked from eschava/psmqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsmqtt.py
232 lines (182 loc) · 7.06 KB
/
psmqtt.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python
import os
import sys
import time
import socket
import logging
import sched
from threading import Thread
from datetime import datetime
import paho.mqtt.client as paho # pip install paho-mqtt
from recurrent import RecurringEvent # pip install recurrent
from dateutil.rrule import * # pip install python-dateutil
from handlers import handlers
from format import Formatter
CONFIG = os.getenv('PSMQTTCONFIG', 'psmqtt.conf')
class Config(object):
def __init__(self, filename=CONFIG):
self.config = {}
execfile(filename, self.config)
def get(self, key, default=None):
return self.config.get(key, default)
try:
cf = Config()
except Exception, e:
print "Cannot load configuration from file %s: %s" % (CONFIG, str(e))
sys.exit(2)
qos = cf.get('mqtt_qos', 0)
retain = cf.get('mqtt_retain', False)
topic_prefix = cf.get('mqtt_topic_prefix', 'psmqtt/' + socket.gethostname() + '/')
request_topic = cf.get('mqtt_request_topic', 'request')
if request_topic != '':
request_topic = topic_prefix + request_topic + '/'
# fix for error 'No handlers could be found for logger "recurrent"'
reccurrent_logger = logging.getLogger('recurrent')
if len(reccurrent_logger.handlers) == 0:
reccurrent_logger.addHandler(logging.NullHandler())
def run_task(task, topic):
if task.startswith(topic_prefix):
task = task[len(topic_prefix):]
topic = Topic(topic if topic.startswith(topic_prefix) else topic_prefix + topic)
try:
payload = get_value(task)
is_seq = isinstance(payload, list) or isinstance(payload, dict)
if is_seq and not topic.is_multitopic():
raise Exception("Result of task '" + task + "' has several values but topic doesn't contain '*' char")
if isinstance(payload, list):
for i, v in enumerate(payload):
topic = topic.get_subtopic(str(i))
mqttc.publish(topic, str(v), qos=qos, retain=retain)
elif isinstance(payload, dict):
for key in payload:
topic = topic.get_subtopic(str(key))
v = payload[key]
mqttc.publish(topic, str(v), qos=qos, retain=retain)
else:
mqttc.publish(topic.get_topic(), str(payload), qos=qos, retain=retain)
except Exception, ex:
mqttc.publish(topic.get_error_topic(), str(ex), qos=qos, retain=retain)
logging.exception(task + ": " + str(ex))
def get_value(path):
path, _format = Formatter.get_format(path)
head, tail = split(path)
if head in handlers:
value = handlers[head].handle(tail)
if _format is not None:
value = Formatter.format(_format, value)
return value
else:
raise Exception("Element '" + head + "' in '" + path + "' is not supported")
class Topic:
def __init__(self, topic):
self.topic = topic
self.wildcard_index, self.wildcard_len = self._find_wildcard(topic)
@staticmethod
def _find_wildcard(topic):
start = 0
# search for * or ** (but not *; or **;) outside of []
while start < len(topic):
wildcard_index = topic.find('*', start)
if wildcard_index < 0:
break
bracket_index = topic.find('[', start)
if 0 <= bracket_index < wildcard_index:
start = topic.find(']', bracket_index)
continue
wildcard_len = 1
if wildcard_index + 1 < len(topic) and topic[wildcard_index + 1] == '*': # ** sequence
wildcard_len += 1
if wildcard_index + wildcard_len < len(topic) and topic[wildcard_index + wildcard_len] == ';':
start = wildcard_index + wildcard_len
continue
return wildcard_index, wildcard_len
return -1, -1
def is_multitopic(self):
return self.wildcard_index > 0
def get_subtopic(self, param):
if self.wildcard_index < 0:
raise Exception("Topic " + self.topic + " have no wildcard")
return self.topic[:self.wildcard_index] + param + self.topic[self.wildcard_index + self.wildcard_len:]
def get_topic(self):
return self.topic
def get_error_topic(self):
return self.topic + "/error"
# noinspection PyUnusedLocal
def on_message(mosq, userdata, msg):
logging.debug(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
if msg.topic.startswith(request_topic):
task = msg.topic[len(request_topic):]
run_task(task, task)
else:
logging.warn('Unknown topic: ' + msg.topic)
def on_timer(s, rrule, tasks):
if isinstance(tasks, dict):
for k in tasks:
run_task(k, tasks[k])
elif isinstance(tasks, list):
for task in tasks:
if isinstance(task, dict):
for k in task:
run_task(k, task[k])
else:
run_task(task, task)
else:
run_task(tasks, tasks)
# add next timer task
now = datetime.now()
delay = (rrule.after(now) - now).total_seconds()
s.enter(delay, 1, on_timer, [s, rrule, tasks])
# noinspection PyUnusedLocal
def on_connect(client, userdata, flags, result_code):
if request_topic != '':
topic = request_topic + '#'
logging.debug("Connected to MQTT broker, subscribing to topic " + topic)
mqttc.subscribe(topic, qos)
# noinspection PyUnusedLocal
def on_disconnect(mosq, userdata, rc):
logging.debug("OOOOPS! psmqtt disconnects")
time.sleep(10)
def split(s):
parts = s.split("/", 1)
return parts if len(parts) == 2 else [parts[0], '']
class TimerThread(Thread):
def __init__(self, s):
Thread.__init__(self)
self.s = s
def run(self):
self.s.run()
if __name__ == '__main__':
clientid = cf.get('mqtt_clientid', 'psmqtt-%s' % os.getpid())
# initialise MQTT broker connection
mqttc = paho.Client(clientid, clean_session=cf.get('mqtt_clean_session', False))
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_disconnect = on_disconnect
mqttc.will_set('clients/psmqtt', payload="Adios!", qos=0, retain=False)
# Delays will be: 3, 6, 12, 24, 30, 30, ...
# mqttc.reconnect_delay_set(delay=3, delay_max=30, exponential_backoff=True)
mqttc.username_pw_set(cf.get('mqtt_username'), cf.get('mqtt_password'))
mqttc.connect(cf.get('mqtt_broker', 'localhost'), int(cf.get('mqtt_port', '1883')), 60)
# parse schedule
schedule = cf.get('schedule', {})
s = sched.scheduler(time.time, time.sleep)
now = datetime.now()
for t in schedule:
r = RecurringEvent()
dt = r.parse(t)
if not r.is_recurring:
logging.error(t + " is not recurring time. Skipping")
continue
rrule = rrulestr(dt)
delay = (rrule.after(now) - now).total_seconds()
s.enter(delay, 1, on_timer, [s, rrule, schedule[t]])
tt = TimerThread(s)
tt.daemon = True
tt.start()
while True:
try:
mqttc.loop_forever()
except socket.error:
time.sleep(5)
except KeyboardInterrupt:
sys.exit(0)