-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.py
390 lines (304 loc) · 9.72 KB
/
index.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
385
386
387
388
389
390
from bottle import route, run, template,response, get, post,request, static_file
import os
from config import *
import staticFilesForAbout
import staticFilesForLogin
import staticFilesForApplication
import MySQLdb
import serial
import time
import requests
import json
from gcm import GCM
#import RPi.GPIO as GPIO
#GPIO.setmode(GPIO.BCM)
#GPIO.setup(14,GPIO.OUT)
#GPIO.setup(15,GPIO.IN)
#GPIO.setup(18,GPIO.OUT)
#GPIO.setup(7,GPIO.OUT)
#GPIO.setup(25,GPIO.OUT)
#ser=serial.Serial('/dev/ttyAMA0' , 9600)
#ser.open()
db = MySQLdb.connect(configs_db["host"],configs_db["user"],configs_db["password"],configs_db["database"])
cursor = db.cursor()
'''ser = serial.Serial(
port='/dev/ttyAMA0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)'''
@route('/')
def greet(name='Stranger'):
print 'hello'
print os.path.dirname(os.path.abspath(__file__))
return static_file('index.html', root="/home/manchanda/Projects/SMARTHOME/html/ABOUT")
@get('/login')
def login_page(name='Stranger'):
print 'hello'
print os.path.dirname(os.path.abspath(__file__))
return static_file('index.html', root="/home/manchanda/Projects/SMARTHOME/html/Login")
@post('/login') # or @route('/login', method='POST')
def login():
username = request.forms.get('username')
password = request.forms.get('password')
if(username == configs_user['username'] and password == configs_user['password']):
condition_light="SELECT Status FROM Lights ORDER BY Time DESC LIMIT 1"
condition_fan="SELECT Status FROM Fan ORDER BY Time DESC LIMIT 1"
condition_curtain="SELECT Status FROM Curtains ORDER BY Time DESC LIMIT 1"
condition_security="SELECT Status FROM Security ORDER BY Time DESC LIMIT 1"
condition_automaticmode="SELECT Status FROM AutomaticMode ORDER BY Time DESC LIMIT 1"
try:
cursor.execute(condition_light)
results = cursor.fetchall()
print results[0][0]
light_status=results[0][0]
cursor.execute(condition_fan)
results = cursor.fetchall()
print results[0][0]
fan_status=results[0][0]
cursor.execute(condition_curtain)
results = cursor.fetchall()
print results[0][0]
curtain_status=results[0][0]
cursor.execute(condition_security)
results = cursor.fetchall()
print results[0][0]
security_status=results[0][0]
cursor.execute(condition_automaticmode)
results = cursor.fetchall()
print results[0][0]
automatic_status=results[0][0]
if(light_status == 0):
lights="bulb_off.png"
else:
lights="bulb_on.png"
if(automatic_status == 0):
automatic="unchecked"
else:
automatic="checked"
if(curtain_status == 0):
curtains="CURTAINS_CLOSED.png"
else:
curtains="CURTAINS_OPEN.png"
if(security_status == 0):
security="doorSecurityOff.png"
else:
security="doorSecurityOn.png"
if(fan_status == 0):
fans="FAN_speed0.png"
elif(fan_status == 1):
fans="FAN_speed1.png"
elif(fan_status == 2):
fans="FAN_speed2.png"
elif(fan_status == 3):
fans="FAN_speed3.png"
elif(fan_status == 4):
fans="FAN_speed4.png"
else:
fans="FAN_speed5.png"
except:
print "DB Error"
return template('/home/manchanda/Projects/SMARTHOME/html/Application/index.html',light=lights, fan=fans, curtain=curtains, security=security, automatic=automatic)
else:
return template("<p> Login unsuccessful {{name}} {{pass1}} </p>",name=username, pass1=password)
@route('/test')
def testing():
print "hee"
username=configs['username']+configs['password']
return 'Hey'+username
@post('/lights_control')
def toggle():
print "Hello Lights are toggle"
status = request.forms.get('status')
status = int(status)
Interface = request.forms.get('Interface')
print Interface
condition_light="INSERT INTO Lights(Status,Interface,Time) VALUES('%d','%s',NOW())" % (status, Interface)
print condition_light
try:
cursor.execute(condition_light)
db.commit()
except MySQLdb.Error, e:
print e.args[0],e.args[1]
db.rollback()
return request.forms.get('status')
@post('/security_control')
def toggle():
print "Hello Security are toggle"
status = request.forms.get('status')
status = int(status)
Interface = request.forms.get('Interface')
print Interface
condition_security="INSERT INTO Security(Status,Interface,Time) VALUES('%d','%s',NOW())" % (status, Interface)
print condition_security
'''
if(status==1):
ser.write('t')
#senddata('t')
elif(status==0):
#senddata('s')
ser.write('s')
condition_security="INSERT INTO Security(Status,Interface,Time) VALUES('%d','%s',NOW())" % (status, Interface)
'''
print condition_security
try:
cursor.execute(condition_security)
db.commit()
except MySQLdb.Error, e:
print e.args[0],e.args[1]
db.rollback()
return request.forms.get('status')
@post('/fan_control')
def toggle():
print "Hello Fan speed changed"
status = request.forms.get('status')
status = int(status)
Interface = request.forms.get('Interface')
print Interface
condition_fan="INSERT INTO Fan(Status,Interface,Time) VALUES('%d','%s',NOW())" % (status, Interface)
print condition_fan
try:
cursor.execute(condition_fan)
db.commit()
except MySQLdb.Error, e:
print e.args[0],e.args[1]
db.rollback()
return request.forms.get('status')
@post('/curtain_control')
def toggle():
print "Hello Curtains changed"
status = request.forms.get('status')
status = int(status)
Interface = request.forms.get('Interface')
print Interface
condition_curtains="INSERT INTO Curtains(Status,Interface,Time) VALUES('%d','%s',NOW())" % (status, Interface)
print condition_curtains
try:
cursor.execute(condition_curtains)
db.commit()
except MySQLdb.Error, e:
print e.args[0],e.args[1]
db.rollback()
return request.forms.get('status')
@post('/automatic_control')
def toggle():
print "Hello automatic_control changed"
status = request.forms.get('status')
status = int(status)
Interface = request.forms.get('Interface')
print Interface
condition_automaticmode="INSERT INTO AutomaticMode(Status,Interface,Time) VALUES('%d','%s',NOW())" % (status, Interface)
print condition_automaticmode
try:
cursor.execute(condition_automaticmode)
db.commit()
except MySQLdb.Error, e:
print e.args[0],e.args[1]
db.rollback()
return request.forms.get('status')
#Mobile ENDPOINTS
@get('/logintest') # or @route('/login')
def login():
return '''
<form action="/login" method="post">
Username: <input name="username" type="text" />
Password: <input name="password" type="password" />
<input value="Login" type="submit" />
</form>
'''
@post('/data') # or @route('/login', method='POST')
def data():
condition_light="SELECT Status FROM Lights ORDER BY Time DESC LIMIT 1"
condition_fan="SELECT Status FROM Fan ORDER BY Time DESC LIMIT 1"
condition_curtain="SELECT Status FROM Curtains ORDER BY Time DESC LIMIT 1"
condition_security="SELECT Status FROM Security ORDER BY Time DESC LIMIT 1"
condition_automaticmode="SELECT Status FROM AutomaticMode ORDER BY Time DESC LIMIT 1"
data=""
try:
cursor.execute(condition_light)
results = cursor.fetchall()
print results[0][0]
light_status=results[0][0]
cursor.execute(condition_fan)
results = cursor.fetchall()
print results[0][0]
fan_status=results[0][0]
cursor.execute(condition_curtain)
results = cursor.fetchall()
print results[0][0]
curtain_status=results[0][0]
cursor.execute(condition_security)
results = cursor.fetchall()
print results[0][0]
security_status=results[0][0]
cursor.execute(condition_automaticmode)
results = cursor.fetchall()
print results[0][0]
automatic_status=results[0][0]
data={'lights':light_status, 'fan':fan_status, 'curtains':curtain_status, 'security':security_status, 'automatic':automatic_status}
except MySQLdb.Error, e:
print e.args[0],e.args[1]
db.rollback()
return data
@post('/logincheck') # or @route('/login', method='POST')
def checklogin():
username = request.forms.get('username')
password = request.forms.get('password')
if(username == configs_user['username'] and password == configs_user['password']):
return "1"
else:
return "0"
def getdata():
return "Test data"
@post('/send_serialterminer')
def senddata():
return null
@get('/test_gcm')
def testingfunction():
sendMessageGcm('asdasdqwe123123','hello')
@post('/storeGcmUser')
def storeGcmUser():
gcm_regId = request.forms.get('gcm_regId')
insert_data="INSERT INTO gcm_users(gcm_regid, created_at) VALUES('%s', NOW())" % (gcm_regId)
print insert_data
try:
cursor.execute(insert_data)
db.commit()
except MySQLdb.Error, e:
print e.args[0],e.args[1]
db.rollback()
return "0"
def sendMessageGcm(registration_ids,message):
'''url="https://android.googleapis.com/gcm/send"
headers = {'Authorization': 'key='+gcm["GOOGLE_API_KEY"],
'Content-Type':'application/json'}
payload = {'registration_ids': registration_ids, 'data': message}
r = requests.post(url, data, headers=headers)
print json.dumps(payload)
print r'''
results=""
reg=""
gcm = GCM(gcm_api["GOOGLE_API_KEY"])
data = {'warning': message}
condition_gcm="SELECT gcm_regid FROM gcm_users"
try:
cursor.execute(condition_gcm)
results = cursor.fetchall()
#reg=results[0][0]
for reg in results:
reg=[reg[0]]
response = gcm.json_request(registration_ids=reg, data=data)
print response
reg
#print results
except:
print "error"
reg=[reg]
print reg
#regis=["APA91bHJsIRkKDGJQeJgOJAQ_YYHrS0GuB5fTHtuD-Ir0M5TjZbPIVUfKD7IRPB1VkyUjjGYLRxVcmO3tuPYZNsPfYOaGIg5zzWuCIvpgr7xGLVlzo4s2bSGPJ8y_wzSYVjjY6WjGfFqpN83Y2ftoOMEoW-SEdwbEw"]
#print regis
#response = gcm.json_request(registration_ids=reg, data=data)
#print response
run(host='192.168.0.120', port=8080, debug=True)