-
Notifications
You must be signed in to change notification settings - Fork 0
/
weio.py
116 lines (93 loc) · 2.9 KB
/
weio.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
#!/usr/bin/python -u
#
# WEIO Web Of Things Platform
# Copyright (C) 2013 Nodesign.net, Uros PETREVSKI, Drasko DRASKOVIC
# All rights reserved
#
# ## ## ######## #### #######
# ## ## ## ## ## ## ##
# ## ## ## ## ## ## ##
# ## ## ## ###### ## ## ##
# ## ## ## ## ## ## ##
# ## ## ## ## ## ## ##
# ### ### ######## #### #######
#
# Web Of Things Platform
#
# This file is part of WEIO and is published under BSD license.
# All rights not explicitly granted in the BSD license are reserved.
# See the included LICENSE file for more details.
#
###
import paho.mqtt.client as mqtt
import sys, json, signal
import control
# MQTT clinet
mqttClient = None
# weioCtrl
weioCtrl = control.WeioControl()
###
# MQTT cb handlers
###
###
# on_connect
###
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("weio/api/cmd")
###
# on_message
###
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
#req = json.loads(msg.payload)
# Test with: mosquitto_pub -t "weio/api/in" -m '{ "request": "tst-event", "data" : "data" }'
from StringIO import StringIO
io = StringIO(msg.payload)
req = json.load(io)
print req['method']
if (req['method'] == 'start'):
# Init and start WeIO stuff
weioCtrl.init()
weioCtrl.start()
elif (req['method'] == 'stop'):
# Init and start WeIO stuff
weioCtrl.stop()
else:
res = weioCtrl.execute(req)
mqttClient.publish("weio/api/msg", res)
###
# Signal handler
###
def signalHandler(sig, frame):
# Stop WeIO
weioCtrl.stop()
# Stop the MQTT loop
if (mqttClient is not None):
mqttClient.disconnect()
# Bail out
sys.exit(0)
###
# CLIENT MAIN
###
if __name__ == '__main__':
# Install signal handlers
signal.signal(signal.SIGTERM, signalHandler)
signal.signal(signal.SIGINT, signalHandler)
###
# MQTT stuff
###
mqttClient = mqtt.Client()
mqttClient.on_connect = on_connect
mqttClient.on_message = on_message
mqttClient.connect("localhost", 1883, 60)
print "if anything goes wrong kill process with: kill -9 `ps aux | grep mqtt | grep python | awk '{print $2}'`"
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
mqttClient.loop_forever()