-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
executable file
·221 lines (167 loc) · 6.65 KB
/
backend.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
#!/usr/bin/python3
#
# CrapLab SmartPlug simple backend server
#
# Copyright (c) 2017 Sven Gregori <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import time
from datetime import datetime
import asyncio
from autobahn.asyncio.websocket import WebSocketServerProtocol, WebSocketServerFactory
import json
import bottle
from threading import Thread
import twitter
# Backend configuration
#
# Set the port for the HTTP REST API
WWW_SERVER_PORT = 9000
#
# Set the port for the WebSocket API
WS_SERVER_PORT = 9001
# Twitter configuration
#
# Set flag to True to use the Twitter API, otherwise set to False
TWITTER_ENABLED = False
#
# Twitter token setup.
# See https://github.com/bear/python-twitter for instructions
#
CONSUMER_KEY=''
CONSUMER_SECRET=''
ACCESS_TOKEN_KEY=''
ACCESS_TOKEN_SECRET=''
class Smartplug:
def __init__(self, _id):
self.id = _id
self.value = 0
def __str__(self):
return "Smartplug id {} with value {}".format(self.id, self.value)
def toJson(self):
return json.dumps({'id': self.id, 'value': self.value})
class ServerProtocol(WebSocketServerProtocol):
def onConnect(self, request):
print("Websocket connection from " + request.peer)
def onOpen(self):
self.factory.register(self)
def onMessage(self, payload, isBinary):
payloadString = payload.decode('utf-8')
print("Received message from {}: '{}'".format(self.peer, payloadString))
data = json.loads(payloadString)
if 'status' in data.keys():
print("Received status request for {} from {}".format(data['status'], self.peer))
if data['status'] in self.factory.smartplugs.keys():
status = self.factory.smartplugs[data['status']].toJson()
print("Sending status: " + status)
self.sendMessage(status.encode('utf-8'))
else:
print("No such smartplug id")
def onClose(self, wasClean, code, reason):
print("Websocket connection closed: " + str(reason))
WebSocketServerProtocol.onClose(self, wasClean, code, reason)
self.factory.unregister(self)
class ServerFactory(WebSocketServerFactory):
def __init__(self, listenUrl):
WebSocketServerFactory.__init__(self, listenUrl)
# List of connected websocket clients
self.clients = []
# List of received smartplug data
self.smartplugs = {}
def register(self, client):
if client not in self.clients:
print("registered client {}".format(client.peer))
self.clients.append(client)
for c in self.clients:
print(c.peer)
def unregister(self, client):
if client in self.clients:
print("unregistered client {}".format(client.peer))
self.clients.remove(client)
def sendClients(self, msg):
print("Sending to all clients: '{}'".format(msg))
for c in self.clients:
c.sendMessage(msg.encode('utf-8'))
class Backend(bottle.Bottle):
def __init__(self, useTwitter=False):
super(Backend, self).__init__()
self.route('/', callback=self.index)
self.route('/data', method='POST', callback=self.postData)
self.factory = None
self.twitter = None
if useTwitter:
print("Setting up Twitter API")
self.twitter = twitter.Api(consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
access_token_key=ACCESS_TOKEN_KEY,
access_token_secret=ACCESS_TOKEN_SECRET)
try:
self.twitterUser = self.twitter.VerifyCredentials()
except twitter.error.TwitterError as e:
print("Error creating API connection: " + str(e))
self.twitter = None
else:
print("Twitter API is disabled")
def index(self):
return "hallo."
def postData(self):
data = bottle.request.json
if data['id'] not in self.factory.smartplugs.keys():
self.factory.smartplugs[data['id']] = Smartplug(data['id'])
smartplug = self.factory.smartplugs[data['id']]
smartplug.value = data['value']
if self.twitter is not None:
if smartplug.value == 1:
tweet = 'Look at that, my #craplab #smartplug just got plugged in!'
else:
tweet = 'It was fun while it lasted. I unplugged my #craplab #smartplug.'
print("Tweeting..")
self.twitter.PostUpdate(tweet)
else:
print("Skipping tweeting")
print("Updated: " + str(smartplug))
self.factory.sendClients(json.dumps(data))
return {}
def websocketsLoop(self, port):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
self.factory = ServerFactory("ws://0.0.0.0:" + str(port))
self.factory.protocol = ServerProtocol
loop = asyncio.get_event_loop()
coro = loop.create_server(self.factory, '0.0.0.0', port)
server = loop.run_until_complete(coro)
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
server.close()
loop.close()
def startWsServer(self, port):
websockets = Thread(target=self.websocketsLoop, args=(port,))
websockets.setDaemon(True)
websockets.start()
print ("Websockets server started")
def startWwwServer(self, port):
self.run(host='0.0.0.0', port=port)
if __name__ == '__main__':
backend = Backend(useTwitter=TWITTER_ENABLED)
backend.startWsServer(WS_SERVER_PORT)
backend.startWwwServer(WWW_SERVER_PORT)