-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulbserver.py
executable file
·340 lines (297 loc) · 11.4 KB
/
bulbserver.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env python2
import sys
import logging
import socket
import threading
import time
from datetime import datetime
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
from shutil import copyfileobj
#from draw import Mockup
#from urllib import parse
#from apscheduler.scheduler import Scheduler
import urlparse as parse
from urllib import urlopen
#import pygame
import json
import urllib2
import simplejson
from PIL import Image
from StringIO import StringIO
strips = []
ledmockups = []
broadcastlisten = True
class StripHandler:
ip = ""
port = 0
cnt = 0
#sched = Scheduler()
def __init__(self,ip,port,cnt):
self.ip = ip
self.port = port
self.cnt = cnt
#StripHandler.sched.start()
#@sched.cron_schedule(hour='9-17')
def update_breathing(self):
t = datetime.now().hour
if (t >= 10 and t <= 16):
self.breathe(8-(t-10))
self.breathe(8-(t-9))
elif (t == 17):
self.breathe(0)
elif (t == 9):
self.breathe(7)
def __str__(self):
return "LED Strip @{0}".format(self.ip)
def setled(self,port, num,r,g,b):
self.socket = socket.socket()
self.socket.connect((self.ip, self.port))
self.socket.sendall(b'\xff'+str.encode('={0}{1:02d}{2:c}{3:c}{4:c}'.format(port, num, r, g, b)))
self.socket.close()
def setstrip(self, port, arr):
data = b''
self.socket = socket.socket()
self.socket.connect((self.ip, self.port))
for i in range(0, len(arr)):
#data += b'\xff'+str.encode('={0}{1:02d}{2:c}{3:c}{4:c}'.format(port, i, arr[i][0], arr[i][1], arr[i][2]))
self.socket.sendall(b'\xff'+str.encode('={0}{1:02d}{2:c}{3:c}{4:c}'.format(port, i, arr[i][0], arr[i][1], arr[i][2])))
time.sleep(0.05)
#self.socket.sendall(data) #Does not work at present - sending too fast, receiver can't cope.
self.socket.close()
def breathe(self, port, pos):
self.socket = socket.socket()
self.socket.connect((self.ip, self.port))
self.socket.sendall(b'\xff'+str.encode('>{0}{1:02d}000'.format(port, pos)))
self.socket.close()
def show(self, port):
self.socket = socket.socket()
self.socket.connect((self.ip, self.port))
self.socket.sendall(b'\xff!'+str.encode(str(port))+b'!!!!!')
self.socket.close()
class ServerHandler(SimpleHTTPRequestHandler):
interest = []
strips = []
avatars = {} #Key: IP, Value: avatar binary data
options = {} #Key: IP, Value: List [now, weather, project, name]
def __init__(self,request,client_address,server):
#self.mockup = Mockup()
#pygame.init()
#self.window = pygame.display.set_mode((848, 480))#, pygame.FULLSCREEN)
SimpleHTTPRequestHandler.__init__(self, request,client_address,server)
def dispatch_notification(self, remove=False):
#We're still in the request handler, let's make use of what's available to us
res = parse.urlparse(self.path)
params = dict(parse.parse_qsl(res.query))
action = "Removed" if remove else "New"
client = self.client_address[0] #IP
if not self.avatars.has_key(self.client_address[0]) and params.has_key('account'):
picasainfo = urllib2.urlopen("http://picasaweb.google.com/data/entry/api/user/{0}?alt=json".format(params['account']))
avatarurl = simplejson.loads(picasainfo.read())['entry']['gphoto$thumbnail']['$t']
self.avatars[self.client_address[0]] = urllib2.urlopen(avatarurl).read()
print("Adding avatar for account {0} and IP {1}".format(params['account'], self.client_address[0]))
with open("notifications.log", "a") as logfile:
try:
logfile.write("{4} {0}: {1} notification #{2} from {3}\n".format(client, action, params['id'], params['pkg'], time.time()))
except:
logfile.write("{4} {0}: {1} unknown notification format. {2}".format(client,action,params,time.time()))
def handle_tag(self, tag):
strip = StripHandler('10.10.10.31', 80, 12)
if tag not in self.interest:
self.interest.append(tag)
#get green and flash the strip with it
arr = [ServerHandler.getcolor(1) for i in range(0,strip.cnt)]
strip.setstrip(0, arr)
strip.show(0)
else:
arr = [ServerHandler.getcolor(3) for i in range(0,strip.cnt)]
strip.setstrip(0, arr)
strip.show(0)
time.sleep(1)
ratio = len(self.interest)/(1.0*strip.cnt)
color = 0
if ratio < 0.2:
color = 3
elif ratio < 0.5:
color = 2
else:
color = 1
arr = [ServerHandler.getcolor(color) for i in range (0, len(self.interest))]
arr += [ServerHandler.getcolor(0) for i in range (len(self.interest), strip.cnt)]
strip.setstrip(0, arr)
strip.show(0)
print("Interest for {0} is now {1}".format(tag, self.interest))
with open("pizza.txt", "a") as logfile:
logfile.write("{0}: PizzaStrip interaction: {1}\n".format(datetime.now(), tag))
@staticmethod
def get_skype_status(username):
#res = urlopen('http://mystatus.skype.com/{0}.num'.format(username))
res = urlopen('http://www.dcs.gla.ac.uk/L311_D/ben.txt')
status = str(res.read())
res.close()
return status
@staticmethod
def get_weather(city="Glasgow"):
"""Get weather data and convert its format to a tuple (temperature, rain, cloud)"""
weather = json.loads(urlopen('http://api.openweathermap.org/data/2.5/weather?units=metric&q={0}'.format(city)).read())
temp = weather['main']['temp']
try:
rain = weather['rain']['3h']
cond = weather['weather'][0]['id']
except KeyError:
rain = 0
cond = 0
clouds = weather['clouds']['all']
if temp < 8:
temp = (0,0,127)
elif temp < 13:
temp = ServerHandler.getcolor(1)
else:
temp = ServerHandler.getcolor(3)
if rain < 3:
rain = ServerHandler.getcolor(1)
elif rain < 10:
rain = ServerHandler.getcolor(2)
else:
rain = ServerHandler.getcolor(3)
if clouds < 30:
clouds = ServerHandler.getcolor(1)
elif clouds < 70:
clouds = ServerHandler.getcolor(2)
else:
clouds = ServerHandler.getcolor(3)
return (temp, rain, clouds)
def do_GET(self):
#logging.error(self.headers)
#SimpleHTTPRequestHandler.do_GET(self)
res = parse.urlparse(self.path)
params = parse.parse_qsl(res.query)
if res.path == '/skype': #id and status
pass
elif res.path == '/registerstrip':
found = False
for strip in self.strips:
if strip.ip == self.client_address[0]:
found = True
print("Strip already registered.")
if not found:
self.strips.append(StripHandler(self.client_address[0], 80, 20))
print("Strip list updated: {0}".format(str(self.strips)))
#self.log_request(200)
self.send_response(200, "OK")
self.end_headers()
#self.wfile.close()
@staticmethod
def getcolor(num):
num = int(num)
if num == 0:
return (0,0,0)
elif num == 1:
return (0,127,0)
elif num == 2:
return (127,80,0)
elif num == 3:
return (127,0,0)
else:
return (0,0,127)
def do_POST(self):
#logging.error(self.headers)
res = parse.urlparse(self.path)
params = parse.parse_qsl(res.query)
if res.path == '/notify':
self.dispatch_notification()
elif res.path == '/denotify':
self.dispatch_notification(remove=True)
elif res.path == '/nfctag':
self.handle_tag(params[0][1]) #harcoded value of first query param
elif res.path == '/availastrip':
states = params[0][1][::-1] #string of values
strip = StripHandler('127.0.0.1', 8001, 8)
for i in range(0, strip.cnt):
c = ServerHandler.getcolor(states[i])
strip.setled(0, i, c[0], c[1], c[2])
print("{0} {1} {2} {3}".format(i, c[0], c[1], c[2]))
strip.show(0)
with open("rod.txt", "a") as logfile:
logfile.write("{0}: AvailaStrip interaction: {1} \n".format(datetime.now(), states))
elif res.path == '/options':
addr = self.client_address[0]
self.options[addr] = [params[0][1], params[1][1], params[2][1], params[3][1]]
#self.log_request(200)
self.send_response(200, "OK")
self.end_headers()
length = int(self.headers.get('Content-Length', 0))
if length > 0 and res.path != '/denotify':
data = self.rfile.read(length)
self.rfile.close()
self.wfile.close()
if res.path == '/avatar':
self.avatars[self.client_address[0]] = data
return
im = Image.open(StringIO(data))
imsize = im.size
if self.avatars.has_key(self.client_address[0]):
av = Image.open(StringIO(self.avatars[self.client_address[0]]))
avsize = (imsize[0]/4, imsize[1]/4)
av = av.resize(avsize, Image.ANTIALIAS)
im.paste(av, (imsize[0]-avsize[0], imsize[1]-avsize[1], imsize[0], imsize[1]))
data = StringIO()
im.save(data, "PNG")
for addr in ledmockups:
urllib2.urlopen("http://{0}:8000/undim".format(addr))
urllib2.urlopen("http://{0}:8000".format(addr), data=data.getvalue())
#f.write(data)
#f.close()
#self.mockup.filename = 'test.png'
#matrix = self.mockup.load_image()
#self.mockup.draw_matrix(self.window, matrix, (self.mockup.radius, self.mockup.radius))
#pygame.display.flip()
else: #Denotify was called
for addr in ledmockups:
urllib2.urlopen("http://{0}:8000/dim".format(addr))
#count = int(self.path[self.path.find('=')+1:])
#self.mockup.draw_progress(self.window, count/5)
#self.wfile.close()
def detectdevices():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('0.0.0.0', 55555))
while broadcastlisten:
data, addr = sock.recvfrom(1024)
#print("UDP Broadcast detected: ", data)
if "LedMockup" in data and addr[0] not in ledmockups:
print("Adding LedMockup @{0}".format(addr[0]))
ledmockups.append(addr[0])
#else:
# strips.append(StripHandler(addr, 80, 8))
def updateskype():
while True:
strip = StripHandler('10.10.10.38', 80, 2)
status = ServerHandler.get_skype_status('skypebulb')
if status == "ONLINE":
strip.setled(1, 0, 0, 127,0 )
elif status == "BUSY":
strip.setled(1, 0, 127, 0,0 )
elif status == "AWAY":
strip.setled(1, 0, 127, 80, 0 )
else:
strip.setled(1, 0, 0, 0, 0 )
strip.show(1)
time.sleep(30)
if __name__ == "__main__":
HandlerClass = ServerHandler
ServerClass = HTTPServer
Protocol = "HTTP/1.0"
port = 8000
server_address = ('0.0.0.0', port)
HandlerClass.protocol_version = Protocol
httpd = ServerClass(server_address, HandlerClass)
sa = httpd.socket.getsockname()
keepalive = threading.Thread(target=detectdevices)
keepalive.start()
#skypethread = threading.Thread(target=updateskype)
#skypethread.start()
print ("Serving HTTP on", sa[0], "port", sa[1], "...")
try:
httpd.serve_forever()
except:
broadcastlisten = False