-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·384 lines (344 loc) · 11.9 KB
/
server.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from iremote import IRemote
from iTunes import *
import signal
import time
from threading import Timer
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
import cgi
import thread
from pool import pool
from relay import relay
from wecker import wecker
from sleeptimer import sleeptimer
from projector import projector
from phue import my_hue
PORT_NUMBER = 8080
relay_state = False
relay_request = False
alarm_on = False
alarm_set = False
alarm_time = '00:00'
beamer_on = False
beamer_off = False
lights_on = False
lights_off = False
bed_on = False
sofa_on = False
bar_on = False
relax = False
dimm = False
read = False
work = False
cinema = False
dinner = False
shutdown = False
goodnight = False
class myHandler(BaseHTTPRequestHandler):
#Handler for the GET requests
def do_GET(self):
if not alarm_set:
message = 'no alarm set'
else:
message = 'set to %s'%alarm_time
if relay_state:
status_audio = 'off'
else:
status_audio = 'on'
if bed_on:
status_bed = 'off'
else:
status_bed = 'on'
if sofa_on:
status_sofa = 'off'
else:
status_sofa = 'on'
if bar_on:
status_bar = 'off'
else:
status_bar = 'on'
try:
mimetype='text/html'
self.send_response(200)
self.send_header('Content-type',mimetype)
self.end_headers()
self.wfile.write('''
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
</head>
<header>
<title>Remote</title>
</header>
<h1>Remote</h1>
<form method="POST" action="/set">
<!--Alarm-->
<table width="320" border="0">
<tr>
<td colspan="4" bgcolor="#6699FF"><strong>Alarm Clock</strong></td>
</tr>
<tr>
<td bgcolor="#6699FF"><input name="time" type="text" size="5" maxlength="5"/></td>
<td bgcolor="#6699FF"><input type="submit" name='alarm' value="set"/></td>
<td bgcolor="#6699FF"><input type="submit" name='alarm' value="cancel"/></td>
<td bgcolor="#6699FF">%(message)s</td>
</tr>
<!--Audio/Video-->
<tr>
<td colspan="4"><strong>Entertainment</strong></td>
</tr>
<tr>
<td>Audio</td>
<td><input type="submit" name='audio' value=%(status_audio)s /></td>
<td>Video</td>
<td><input type="submit" name='video' value="on"/> <input type="submit" name='video' value="off"/></td>
</tr>
<!--all Light-->
<tr>
<td colspan="4" bgcolor="#FFFF99"><strong>Lights</strong></td>
</tr>
<tr>
<td bgcolor="#FFFF99">All<br /><input type="submit" name='lights' value="on"/> <input type="submit" name='lights' value="off"/></td>
<td bgcolor="#FFFF99">Bed<br /><input type="submit" name='bed' value=%(status_bed)s /></td>
<td bgcolor="#FFFF99">Sofa<br /><input type="submit" name='sofa' value=%(status_sofa)s /></td>
<td bgcolor="#FFFF99">Bar<br /> <input type="submit" name='bar' value=%(status_bar)s /></td>
</tr>
<tr>
<td colspan="4" bgcolor="#CCCCCC"><strong>Presets</strong></td>
</tr>
<tr>
<td bgcolor="#CCCCCC"><input type="submit" name='presets' value="normal"/></td>
<td bgcolor="#CCCCCC"><input type="submit" name='presets' value="work"/></td>
<td bgcolor="#CCCCCC"><input type="submit" name='presets' value="relax"/></td>
<td bgcolor="#CCCCCC"><input type="submit" name='presets' value="dimm"/></td>
</tr>
<tr>
<td bgcolor="#CCCCCC"><input type="submit" name='presets' value="goodnight"/></td>
<td bgcolor="#CCCCCC"><input type="submit" name='presets' value="read"/></td>
<td bgcolor="#CCCCCC"><input type="submit" name='presets' value="cinema"/></td>
<td bgcolor="#CCCCCC"><input type="submit" name='presets' value="shutdown"/></td>
</tr>
</table>
</form>
</body>
</html>
'''%{'message':message,'status_audio':status_audio,'status_bed':status_bed,'status_sofa':status_sofa,'status_bar':status_bar})
return
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
#Handler for the POST requests
def do_POST(self):
global relay_state
global relay_request
global alarm_set
global alarm_time
global beamer_on
global beamer_off
global lights_on
global lights_off
global bed_on
global sofa_on
global bar_on
global relax
global dimm
global read
global work
global cinema
global dinner
global shutdown
global goodnight
if self.path=="/set":
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
try:
if (form["alarm"].value=="set"):
if not alarm_set:
try:
alarm_time = form["time"].value
print 'got request to set alarm to %s'%alarm_time
alarm_set = True
except:
pass
if (form["alarm"].value=="cancel"):
alarm_set = False
print 'got request to cancel alarm'
except KeyError:
pass
def switch(name,var):
try:
if (form[name].value == "on"):
exec('%s = True'%var) in globals()
print 'got request to turn on %s'%name
if (form[name].value == "off"):
exec('%s = False'%var) in globals()
print 'got request to turn off %s'%name
except KeyError:
pass
def change(name,var,state):
try:
if (form[name].value == state):
exec('%s = True'%var) in globals()
print 'got request to turn %s %s'%(state,name)
except KeyError:
pass
change('lights','lights_on','on')
change('lights','lights_off','off')
change('video','beamer_on','on')
change('video','beamer_off','off')
change('presets','relax','relax')
change('presets','dimm','dimm')
change('presets','work','work')
change('presets','read','read')
change('presets','cinema','cinema')
change('presets','goodnight','goodnight')
change('presets','shutdown','shutdown')
change('audio','relay_request','on')
change('audio','relay_request','off')
switch('bed','bed_on')
switch('sofa','sofa_on')
switch('bar','bar_on')
#Redirect the browser on the main page
self.send_response(302)
self.send_header('Location','/')
self.end_headers()
return
@pool
def my_iremote_handler(event):
if event == IRemote.MENU:
try:
t.cancel()
print 'timer canceled'
except:
pass
if alarm.alarm_on:
alarm.alarm_on = False
alarm.stop_alarm()
else:
if player.status == 'playing':
player.pause()
theRelay.switch_state()
if event == IRemote.PLAY_PAUSE:
if alarm.alarm_on:
alarm.snooze(7)
else:
if not theRelay.state and player.status == 'paused': theRelay.power_on()
#This is a thread that runs the web server
def WebServerThread():
try:
#Create a web server and define the handler to manage the
#incoming request
server = HTTPServer(('', PORT_NUMBER), myHandler)
print 'Started httpserver on port ' , PORT_NUMBER
#Wait forever for incoming htto requests
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down the web server'
server.socket.close()
# Runs the web server thread
thread.start_new_thread(WebServerThread,())
iremote = IRemote()
iremote.add_listener(my_iremote_handler)
iremote.start()
theRelay = relay()
player = iTunes()
alarm = wecker(player,theRelay)
alarm.start()
beamer = projector()
room = my_hue()
#theTimer = sleeptimer(player,theRelay)
print 'Remote catch up and running'
while True:
try:
if alarm_set and not alarm.alarm_set:
alarm.set_time(alarm_time)
#alarm_set = True
#alarm_on = True
if not alarm_set and alarm.alarm_set:
alarm.alarm_set = False
print 'alarm caceled'
#alarm_set = False
alarm_set = alarm.alarm_set
if relay_request:
theRelay.switch_state()
relay_state = theRelay.state
relay_request = False
if beamer_on:
beamer.on()
beamer_on = False
if beamer_off:
beamer.off()
beamer_off = False
if lights_on == True and room.status != True:
bed_on = True
sofa_on = True
bar_on = True
lights_on = False
room.set('all',on=True)
if lights_off == True and room.status != False:
bed_on = False
sofa_on = False
bar_on = False
lights_off = False
room.set('all',on=False)
if not bed_on == room.status('Bed'):
room.set('Bed',on=bed_on)
if not sofa_on == room.status('Sofa'):
room.set('Sofa',on=sofa_on)
if not bar_on == room.status('Bar'):
room.set('Bar',on=bar_on)
if work:
bed_on = True
sofa_on = True
bar_on = True
room.set('all',on=True,brightness=254,colortemp_k=3800)
work = False
if read:
bed_on = False
sofa_on = True
bar_on = False
room.set('Sofa',on=True,brightness=200,colortemp_k=3000)
room.set('Bed',on=False)
room.set('Bar',on=False)
read = False
if relax:
bed_on = True
sofa_on = True
bar_on = True
room.set('all',on=True,brightness=254,colortemp_k=2700)
relax = False
if dimm:
room.set('all',brightness=100)
dimm = False
if cinema:
bed_on = True
sofa_on = True
bar_on = True
room.set('Bed',on=True,brightness=50,colortemp_k=2500)
room.set('Sofa',on=True,brightness=50,colortemp_k=2500)
room.set('Bar',on=True,brightness=50,colortemp_k=2500)
beamer.on()
relay_state = True
theRelay.power_on()
cinema = False
if goodnight:
beamer_off = True
lights_off = True
relay_state = False
theRelay.power_off()
goodnight = False
if shutdown:
beamer_off = True
alarm_set = False
lights_off = True
relay_state = False
theRelay.power_off()
shutdown = False
time.sleep(1)
except:
pass