-
Notifications
You must be signed in to change notification settings - Fork 0
/
Web.py
419 lines (324 loc) · 15 KB
/
Web.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
from flask import Flask, request
import json
import datetime
import databaseManager
from pyfcm import FCMNotification
db = databaseManager.DatabaseManager('localhost', 'root', '!Dasom0129', 'shs')
push_service = FCMNotification(
api_key="AAAAXE7sxxA:APA91bGJVD6TxWf_9WBZS97vToJ18SG-OnslG8g-crwm1WjJxPVa9gBvPFm3C4ma0lpHuOZuLOepeOPftXA4DmBQjcsejFFKjt2rFarqpdk8zNm2oay-JCLpu5N_bAWezdSKYGUP8RD1")
app = Flask(__name__)
def json_default(value):
if isinstance(value, (datetime.datetime)):
return value.__str__()
@app.route('/doorlock_image', methods=['GET'])
def GetDoorLockImage():
dong = request.args.get('dong')
ho = request.args.get('ho')
data = None
data = db.executeQuery("select * from doorlock_image where dong = %s and ho = %s order by id desc", (dong, ho))
print(str(json.dumps(data, ensure_ascii=False, default=json_default)))
return json.dumps(data, ensure_ascii=False, default=json_default)
@app.route('/window', methods=['GET'])
def SelectWindow():
data = db.executeQuery2("select `window`.*, `client`.`name` from `window`, `client` where `client`.id = `window`.id;")
return json.dumps(data, ensure_ascii=False, default=json_default)
@app.route('/curtain', methods=['PUT'])
def putCurtain():
print(request.json)
lux_over = request.json['lux_over']
lux_set = request.json['lux_set']
id = request.json['id']
db.updateQuery("update `curtain` SET lux_set=%s, lux_over=%s where id=%s",
(lux_set,lux_over,id))
return 'sucess'
@app.route('/window', methods=['PUT'])
def putWindow():
print(request.json)
temp_over = request.json['temp_over']
temp_set = request.json['temp_set']
humi_over = request.json['humi_over']
humi_set = request.json['humi_set']
rain_over = request.json['rain_over']
rain_set = request.json['rain_set']
dust_over = request.json['dust_over']
dust_set = request.json['dust_set']
id = request.json['id']
db.updateQuery("update `window` SET temp_set=%s, temp_over=%s, humi_set=%s, humi_over=%s, rain_set=%s, rain_over=%s, dust_set=%s, dust_over=%s where id=%s",
(temp_set,temp_over,humi_set,humi_over,rain_set,rain_over,dust_set,dust_over,id))
return 'sucess'
@app.route('/curtain', methods=['GET'])
def SelectCurtain():
data = db.executeQuery2("select `curtain`.*, `client`.`name` from `curtain`, `client` where `client`.id = `curtain`.id;")
return json.dumps(data, ensure_ascii=False, default=json_default)
@app.route('/sensor', methods=['GET'])
def SelectSensor():
data = db.executeQuery2("select * from sensor")
return json.dumps(data, ensure_ascii=False, default=json_default)
'''========================================='''
'''친구 요청 목록'''
@app.route('/requestFriend', methods=['GET'])
def GetRequestFriend():
id = request.args.get('id')
data = db.executeQuery("select c.id, c.someid as clientid, c.clientid as someid, c.newmsg, " +
"cli.name as somename, cli.dong as somedong, cli.ho as someho, cli.id_name as someidname " +
"from contact c inner join `client` cli on cli.id = c.clientid " +
"left join contact d on d.clientid = c.someid and d.someid = c.clientid " +
"where c.someid = %s and d.id is null", (id,))
return json.dumps(data, ensure_ascii=False, default=json_default)
'''친구 요청 거절'''
@app.route('/requestFriend', methods=['DELETE'])
def DeleteRequestFriend():
clientid = request.args.get('clientid')
someid = request.args.get('someid')
db.updateQuery("delete from contact where clientid = %s and someid = %s", (someid, clientid))
return 'sucess'
'''친구 목록'''
@app.route('/friend', methods=['GET'])
def GetFriend():
id = request.args.get('id')
data = db.executeQuery(
"select c.*, cli.name as somename, cli.dong as somedong, cli.ho as someho, cli.id_name as someidname " +
"from contact c inner join `client` cli on cli.id = c.someid " +
"inner join contact d on c.clientid = d.someid " +
"where c.clientid = %s and d.clientid = c.someid", (id,))
return json.dumps(data, ensure_ascii=False, default=json_default)
'''이미 등록된 친구인지 확인'''
@app.route('/friendCheck', methods=['GET'])
def friendCheck():
clientid = request.args.get('clientid')
someidname = request.args.get('someidname')
data = db.executeQuery("select *" +
"from contact where clientid = %s and someid = (select id from `client` where `id_name` = %s)",
(clientid, someidname))
return json.dumps(data, ensure_ascii=False, default=json_default)
'''친구 수락 및 요청'''
@app.route('/friend', methods=['POST'])
def PostFriend():
clientid = request.json['clientid']
someidname = request.json['someidname']
db.updateQuery("insert into contact(clientid, someid) values(%s, (select id from `client` where `id_name` = %s))",
(clientid, someidname))
return 'sucess'
'''친구삭제'''
@app.route('/friend', methods=['DELETE'])
def DeleteFriend():
clientid = request.args.get('clientid')
someid = request.args.get('someid')
db.updateQuery("delete from contact where clientid = %s and someid = %s or clientid = %s and someid = %s",
(clientid, someid, someid, clientid))
return 'sucess'
'''존재하는 친구인지 확인'''
@app.route('/friendCheck2', methods=['GET'])
def friendCheck2():
someidname = request.args.get('someidname')
data = db.executeQuery("select * from client where id_name = %s", (someidname,))
return json.dumps(data, ensure_ascii=False, default=json_default)
'''===================================================================================================================='''
@app.route('/speaker', methods=['GET'])
def GetSpeaker():
dong = request.args.get('dong')
ho = request.args.get('ho')
data = None
data = db.executeQuery("select* from client where dong = %s and ho = %s and type=1", (dong, ho))
return json.dumps(data, ensure_ascii=False, default=json_default)
@app.route('/sendfcm_ho_dong', methods=['POST'])
def sendFCM_Ho_sDong():
print(str(request))
title = request.json['title']
message = request.json['message']
ho = request.json['ho']
dong = request.json['dong']
data = db.executeQuery("SELECT fcm FROM client WHERE ho = %s and dong = %s", (ho, dong,))
data2 = []
for i in data:
if i['fcm'] != None:
data2.append(i['fcm'])
result = push_service.notify_multiple_devices(registration_ids=data2, message_title=title, message_body=message)
return 'sucess'
@app.route('/sendfcm_dong', methods=['POST'])
def sendFCM_Dong():
print(str(request))
title = request.json['title']
message = request.json['message']
dong = request.json['dong']
data = db.executeQuery("SELECT fcm FROM client WHERE dong = %s", (dong,))
data2 = []
for i in data:
if i['fcm'] != None:
data2.append(i['fcm'])
result = push_service.notify_multiple_devices(registration_ids=data2, message_title=title, message_body=message)
return str(data2)
return 'sucess'
@app.route('/sendfcm', methods=['POST'])
def sendFCM():
client_id = request.json['client_id']
title = request.json['title']
message = request.json['message']
data = db.executeQuery("SELECT fcm FROM client WHERE client = %s", (client_id,))
result = push_service.notify_single_device(registration_id=data[0]['fcm'], message_title=title,
message_body=message)
return str(data[0]['fcm'])
@app.route('/board', methods=['GET'])
def SelectBoard():
data = db.executeQuery2("select * from board")
return json.dumps(data, ensure_ascii=False, default=json_default)
@app.route('/client', methods=['GET'])
def SelectClient():
id = request.args.get('id')
data = db.executeQuery("select * from client where id = %s", id)
return json.dumps(data, ensure_ascii=False, default=json_default)
@app.route('/client', methods=['PUT'])
def SelectClientModify():
fcm = request.args.get('fcm')
id = request.args.get('id')
db.updateQuery("update `client` SET fcm=%s where id=%s", (fcm, id))
return 'success'
@app.route('/writing', methods=['POST'])
def PostWritibng():
title = request.json['title']
writer = request.json['writer']
content = request.json['content']
board_id = request.json['board_id']
data = {}
db.updateQuery("insert into writing (title, writer ,content , board_id) values(%s,%s,%s,%s)",
(title, writer, content, board_id))
return "sucess"
@app.route('/last_writing', methods=['GET'])
def GetLast_writing():
data = db.executeQuery2("SELECT * FROM writing order by id desc limit 1")
print(data)
return json.dumps(data, ensure_ascii=False, default=json_default)
@app.route('/writing', methods=['GET'])
def GetWriting():
board_id = request.args.get('board_id')
id = request.args.get('id')
data = None
if id is not None:
data = db.executeQuery(
"SELECT writing.*, (SELECT COUNT(*) FROM comment WHERE writing_id=writing.id AND `show` = 1) AS comment_count, (SELECT name FROM client WHERE id=writing.writer) AS writer_name from writing where id = %s and `show` = 1 order by id desc",
id)
else:
data = db.executeQuery(
"SELECT writing.*, (SELECT COUNT(*) FROM comment WHERE writing_id=writing.id AND `show` = 1) AS comment_count, (SELECT name FROM client WHERE id=writing.writer) AS writer_name from writing where board_id = %s and `show` = 1 order by id desc",
board_id)
return json.dumps(data, ensure_ascii=False, default=json_default)
@app.route('/writing', methods=['PUT'])
def sendWritingModify():
print(request.get_json())
title = request.json['title']
content = request.json['content']
id = request.json['id']
db.updateQuery("update `writing` SET title=%s, content=%s, `update_time`=now() where id=%s", (title, content, id))
return 'sucess'
@app.route('/writing', methods=['DELETE'])
def deleteWritingOne():
id = request.args.get('id')
db.updateQuery("update `writing` SET `show`=0 where id=%s", (id))
return 'sucess'
@app.route('/comment', methods=['POST'])
def postComment():
writer = request.json['writer']
content = request.json['content']
writing_id = request.json['writing_id']
db.updateQuery("insert into `comment`(writer, content ,writing_id) values(%s,%s,%s)",
(writer, content, writing_id))
data = db.executeQuery("SELECT fcm FROM client WHERE id = (Select writer from writing where id=%s)", (writing_id,))
result = push_service.notify_single_device(registration_id=data[0]['fcm'], message_title="댓글 알림", message_body=str(content))
return 'sucess'
@app.route('/comment', methods=['GET'])
def getComment():
writing_id = request.args.get('writing_id')
data = db.executeQuery(
"select *, (SELECT name FROM client WHERE id=comment.writer) AS writer_name from `comment` where `writing_id` = %s and `show` = 1",
writing_id)
return json.dumps(data, ensure_ascii=False, default=json_default)
@app.route('/comment', methods=['DELETE'])
def deleteComment():
id = request.args.get('id')
db.updateQuery("update `comment` SET `show`=0 where id=%s", id)
return 'sucess'
@app.route('/comment', methods=['PUT'])
def putComment():
content = request.json['content']
id = request.json['id']
db.updateQuery("update `comment` SET `content`=%s, `update_time`=now() where id=%s", (content, id))
return 'sucess'
@app.route('/writing_image', methods=['POST'])
def PostWriting_Image():
path = request.json['path']
writing_id = request.json['writing_id']
data = {}
db.updateQuery("insert into writing_image (path, writing_id) values(%s,%s)",
(path, writing_id))
return "sucess"
@app.route('/writing_image_multi', methods=['POST'])
def PostWriting_Image_multi():
jsonArray = request.json
print("asd" + str(jsonArray))
for i in jsonArray:
db.updateQuery("insert into writing_image (path, writing_id, name) values(%s,%s,%s)",
(i['path'], i['writing_id'], i['name']))
return "sucess"
@app.route('/writing_image', methods=['GET'])
def GetWriting_image():
writing_id = request.args.get('writing_id')
data = db.executeQuery("select * from writing_image where writing_id=%s", writing_id)
return json.dumps(data, ensure_ascii=False, default=json_default)
@app.route('/writing_image', methods=['DELETE'])
def Delete_image():
jsonArray = {}
jsonArray = request.json
print("jsonArray" + str(jsonArray))
for i in jsonArray:
id = str(i['id'])
db.updateQuery("delete from writing_image where id like %s", id)
return "sucess"
@app.route('/logincheck', methods=['GET'])
def Logincheck():
id_name = request.args.get('id_name')
password = request.args.get('password')
data = None
data = db.executeQuery("select* from client where id_name = %s and password = sha2(%s,512)", (id_name, password))
return json.dumps(data, ensure_ascii=False, default=json_default)
@app.route('/updatefcm', methods=['PUT'])
def UpdateFCM():
id = request.json['id']
fcm = request.json['fcm']
if fcm == "널":
db.updateQuery("update client set fcm = NULL where id = %s", (id,))
else:
db.updateQuery("update client set fcm = %s where id = %s", (fcm, id))
print("" + str(id) + "fcm" + fcm)
return "sucess"
@app.route('/writing_vote', methods=['POST'])
def postWriting_vote():
jsonArray = request.json
for i in jsonArray:
db.updateQuery("insert into writing_vote (name, writing_id) values(%s,%s)",
(i['name'], i['writing_id']))
return "sucess"
@app.route('/vote_whether', methods=['POST'])
def postVote_whether():
vote_id = request.json['vote_id']
client_id = request.json['client_id']
db.updateQuery("insert into vote_whether (vote_id, client_id) values(%s,%s)", (vote_id, client_id))
return "sucess"
@app.route('/check_vote', methods=['GET'])
def check_Vote():
writing_id = request.args.get('writing_id')
client_id = request.args.get('client_id')
data = db.executeQuery("select a.* from vote_whether a " +
"inner join writing_vote b " +
"on a.vote_id = b.id " +
"where a.client_id = %s and b.writing_id = %s", (client_id, writing_id))
return json.dumps(data, ensure_ascii=False, default=json_default)
@app.route('/writing_vote', methods=['GET'])
def getWriting_vote():
writing_id = request.args.get('writing_id')
sql = "select count(*) from vote_whether where vote_id = id"
data = db.executeQuery("select *, (" + sql + ") as amount from writing_vote where writing_id=%s", (writing_id))
return json.dumps(data, ensure_ascii=False, default=json_default)
if __name__ == '__main__':
app.jinja_env.auto_reload = True
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.run(debug=True, host='0.0.0.0', port=5000, threaded=True)