-
Notifications
You must be signed in to change notification settings - Fork 1
/
ws_api.py
executable file
·488 lines (420 loc) · 15.6 KB
/
ws_api.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C)2018-24 Maurizio Montel (dr-prodigy) <[email protected]>
# This file is part of hompi <https://github.com/dr-prodigy/hompi>.
#
# hompi is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# hompi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with hompi. If not, see <http://www.gnu.org/licenses/>.
from flask import Flask
from flask import request
from flask import send_file
from werkzeug import exceptions
import traceback
import json
import signal
import hashlib
import os
import urllib.request
from PIL import Image
import db
import config
import io_data
HOMPI_PROCNAME = 'bin/hompi'
app = Flask(__name__)
print('HOMPI WS')
print('========')
API_KEY = ''
try:
# calculate API_KEY (MD5 hash), if required
if config.API_KEY:
m = hashlib.md5()
m.update(config.API_KEY.encode('utf-8'))
API_KEY = m.hexdigest().upper()
print('API_KEY: {}'.format(API_KEY))
except:
pass
def _signal_server():
for line in os.popen("ps ax | grep " + HOMPI_PROCNAME + " | grep -v grep"):
fields = line.split()
pid = fields[0]
os.kill(int(pid), signal.SIGHUP)
def _check_sharedkey():
if API_KEY:
api_key = request.args.get('api_key', '').upper()
if api_key == API_KEY:
return True
else:
return False
else:
return True
def _get_remote_address(server):
dbmgr = db.DatabaseManager()
try:
status = json.loads(
dbmgr.query("SELECT data FROM gm_output").fetchone()[0])
return status["hompi_slaves"][server]["address"]
except Exception:
print("_get_remote_address({}): error".format(server))
print(traceback.format_exc())
return None
# READER METHODS (GET)
@app.route('/hompi/_get_system_info', methods = ['GET'])
def get_system_info():
if not _check_sharedkey():
return "Forbidden", 403
return io_data.SystemInfo().get_output()
@app.route('/hompi/_get_status', methods = ['GET'])
def get_status():
if not _check_sharedkey():
return "Forbidden", 403
dbmgr = db.DatabaseManager()
try:
return dbmgr.query("SELECT data FROM gm_output").fetchone()[0]
except Exception:
print("get_status(): error")
print(traceback.format_exc())
return "Error", 500 # INTERNAL_SERVER_ERROR
@app.route('/hompi/_get_list/<data_list>', methods = ['GET'])
@app.route('/hompi/_get_list/<data_list>/<key>', methods = ['GET'])
def get_list(data_list, key=None):
if not _check_sharedkey():
return "Forbidden", 403
list_table = ('gm_timetable' if data_list == 'timetable' else
('gm_timetable_day_type' if data_list == 'daytype' else
('gm_temp' if data_list == 'temp' else
('gm_timetable_type_data' if data_list == 'typedata' else
'gm_control'))))
try:
dbmgr = db.DatabaseManager()
if data_list == 'temp':
if key:
_key = int(key)
cur = dbmgr.query("""
SELECT `{}`.*, `timetable_id` FROM `{}`
INNER JOIN `gm_timetable_temp` ON
`temp_id` = `gm_temp`.`id`
WHERE `gm_timetable_temp`.`timetable_id` = ?
ORDER BY `gm_temp`.`id`
""".format(list_table, list_table), (_key,))
else:
cur = dbmgr.query("""
SELECT `{}`.*, `timetable_id` FROM `{}`
INNER JOIN `gm_timetable_temp` ON `temp_id` = `gm_temp`.`id`
ORDER BY `gm_temp`.`id`
""".format(list_table, list_table))
else:
if key:
_key = int(key)
if data_list == 'typedata':
cur = dbmgr.query("""
SELECT * FROM {}
WHERE `day_type_id` = ?
ORDER BY `id`
""".format(list_table), (_key,))
else:
cur = dbmgr.query("""
SELECT * FROM {}
WHERE `id` = ?
ORDER BY `id`
""".format(list_table), (_key,))
else:
cur = dbmgr.query(
"SELECT * FROM {} ORDER BY id".format(list_table))
r = [dict((cur.description[i][0], value)
for i, value in enumerate(row)) for row in cur.fetchall()]
# one = False
# return json.dumps((r[0] if r else None) if one else r)
return json.dumps(r)
except Exception:
print("get_list({},{}): error".format(data_list, key))
print(traceback.format_exc())
return "Error", 500 # INTERNAL_SERVER_ERROR
@app.route('/hompi/_get_temp_chart', methods = ['GET'])
def get_temp_chart():
if not _check_sharedkey():
return "Forbidden", 403
dbmgr = db.DatabaseManager()
try:
# , description
cur = dbmgr.query(
"""
SELECT
datetime(datetime,'unixepoch') AS datetime,
datetime AS datetime_epoch, int_temp_c,
ext_temp_c, req_temp_c, description
FROM gm_log
WHERE event = '.'
AND date(datetime,'unixepoch') > datetime(date('now','-7 day'))
"""
)
r = [dict((cur.description[i][0], value)
for i, value in enumerate(row)) for row in cur.fetchall()]
# one = False
# return json.dumps((r[0] if r else None) if one else r)
return json.dumps(r)
except Exception:
print("get_temp_chart(): error")
print(traceback.format_exc())
return "Error", 500 # INTERNAL_SERVER_ERROR
@app.route('/hompi/_get_server_list', methods = ['GET'])
def get_server_list():
if not _check_sharedkey():
return "Forbidden", 403
dbmgr = db.DatabaseManager()
try:
status = json.loads(
dbmgr.query("SELECT data FROM gm_output").fetchone()[0])
return json.dumps(status["hompi_slaves"].keys())
except Exception:
print("get_server_list(): error")
print(traceback.format_exc())
return "Error", 500 # INTERNAL_SERVER_ERROR
@app.route('/hompi/_get_list2/<server>/<list>', methods = ['GET'])
@app.route('/hompi/_get_list2/<server>/<list>/<key>', methods = ['GET'])
def get_list2(server, list, key=None):
if not _check_sharedkey():
return "Forbidden", 403
try:
# get local list
if server == "local":
return get_list(list, key)
api_key = urllib.request.args.get('api_key', '').upper()
address = _get_remote_address(server)
if key:
return urllib.request.urlopen(
"{}/hompi/_get_list/{}/{}?api_key={}".format(address,
list, key,
api_key),
timeout=2
).read()
else:
return urllib.request.urlopen(
"{}/hompi/_get_list/{}?api_key={}".format(address, list,
api_key),
timeout=2
).read()
except Exception:
print("get_list2({},{},{}): error".format(server, list, key))
print(traceback.format_exc())
return "Error", 500 # INTERNAL_SERVER_ERROR
@app.route('/hompi/_get_image/<image_name>', methods = ['GET'])
def get_image(image_name):
if not _check_sharedkey():
return "Forbidden", 403
try:
filename = '{}/{}'.format(
os.path.dirname(os.path.abspath(config.IMAGE_PATH)), image_name)
thumb_filename = '{}/thumbs/{}'.format(
os.path.dirname(os.path.abspath(config.IMAGE_PATH)), image_name)
if not os.path.isfile(thumb_filename):
try:
image = Image.open(filename)
image.thumbnail(config.THUMB_SIZE, Image.ANTIALIAS)
image.save(thumb_filename, 'JPEG')
except Exception:
print("get_image({}): error".format(image_name))
print(traceback.format_exc())
return "Error", 500 # INTERNAL_SERVER_ERROR
return send_file(thumb_filename)
except Exception:
print("get_image({}): error".format(image_name))
print(traceback.format_exc())
return "Error", 500 # INTERNAL_SERVER_ERROR
# UPDATE METHODS (GET/PUT)
@app.route('/hompi/_send_command', methods=['PUT'])
@app.route('/hompi/_send_command/<command>', methods=['GET'])
def send_command(command = None):
_data = ''
if not _check_sharedkey():
return "Forbidden", 403
try:
_command = []
if request.method == 'PUT':
_data = request.get_data()
elif request.method == 'GET':
_command = json.loads(command)
_data = _command['data']
else:
return "Method not allowed", 405
dbmgr = db.DatabaseManager()
# insert with de-bounce
dbmgr.query("""
INSERT INTO `gm_input`(`data`)
SELECT ? WHERE NOT EXISTS
(SELECT * FROM `gm_input` WHERE `data` = ?)
""", (_data, _data))
_signal_server()
return "Ok", 200
except exceptions.UnsupportedMediaType:
return "Unsupported Media Type", 415
except Exception:
print("send_command({}): error".format(command))
print(traceback.format_exc())
return "Error", 400 # BAD_REQUEST
@app.route('/hompi/_set_control', methods=['PUT'])
@app.route('/hompi/_set_control/<data>', methods=['GET'])
def set_control(data = None):
if not _check_sharedkey():
return "Forbidden", 403
try:
_data = None
if request.method == 'PUT':
_data = request.get_json()
elif request.method == 'GET':
_data = json.loads(data)
else:
return "Method not allowed", 405
dbmgr = db.DatabaseManager()
_id = None
if 'timetable_id' in _data:
_id = int(_data['timetable_id'])
elif 'timetable_desc' in _data:
row = dbmgr.query("SELECT id FROM gm_timetable WHERE description = ?",
(_data['timetable_desc'],)).fetchone()
if row and len(row) > 0:
_id = row[0]
if _id:
dbmgr.query("UPDATE gm_control SET timetable_id = ?", (_id,))
_signal_server()
return "Ok", 200
else:
return "Error", 400 # BAD_REQUEST
except exceptions.UnsupportedMediaType:
return "Unsupported Media Type", 415
except Exception:
print("set_control({}): error".format(data))
print(traceback.format_exc())
return "Error", 400 # BAD_REQUEST
@app.route('/hompi/_set_temp/<data>', methods=['PUT','GET'])
def set_temp(data):
_id = _temp_c = 0
if not _check_sharedkey():
return "Forbidden", 403
try:
_data = []
if request.method == 'PUT':
_data = request.get_json()
_id = int(data)
elif request.method == 'GET':
_data = json.loads(data)
_id = int(_data['id'])
else:
return "Method not allowed", 405
_temp_c = float(_data['temp_c'])
dbmgr = db.DatabaseManager()
dbmgr.query("""UPDATE gm_temp SET temp_c = ?
WHERE id = ?""", (_temp_c, _id))
_signal_server()
return "Ok", 200
except exceptions.UnsupportedMediaType:
return "Unsupported Media Type", 415
except Exception:
print("set_temp({}): error".format(data))
print(traceback.format_exc())
return "Error", 400 # BAD_REQUEST
@app.route('/hompi/_set_temp2/<server>/<data>', methods=['PUT','GET'])
def set_temp2(server, data = None):
if not _check_sharedkey():
return "Forbidden", 403
try:
_data = []
if request.method == 'PUT':
_data = request.get_json()
_data['id'] = data
elif request.method == 'GET':
_data = data
else:
return "Method not allowed", 405
# set local temp
if server == "local":
return set_temp(data)
api_key = urllib.request.args.get('api_key', '').upper()
address = _get_remote_address(server)
return urllib.request.urlopen(
"{}/hompi/_set_temp/{}?api_key={}".format(address, data,
api_key), timeout=2
).read()
return "Ok", 200
except exceptions.UnsupportedMediaType:
return "Unsupported Media Type", 415
except Exception:
print("set_temp2({},{}): error".format(server, data))
print(traceback.format_exc())
return "Error", 400 # BAD_REQUEST
# TO DO
@app.route('/hompi/_set_timetable/<data_json>')
def set_timetable(data_json):
if not _check_sharedkey():
return "Forbidden", 403
try:
days = ['monday','tuesday','wednesday','thursday',
'friday','saturday','sunday','pre_holiday',
'holiday']
_data = json.loads(data_json)
_id = int(_data['id'])
_day_type_id = int(_data['day_type_id'])
dbmgr = db.DatabaseManager()
if _data['day'] in days:
dbmgr.query("""UPDATE gm_timetable
SET {} = ?
WHERE id = ?
""".format(_data['day']),
(_day_type_id, _id))
else:
return "Error", 400 # BAD_REQUEST
except Exception:
print("set_timetable_data({}): error".format(data_json))
print(traceback.format_exc())
return "Error", 400 # BAD_REQUEST
return "Ok"
@app.route('/hompi/_set_timetable_data/<data_json>')
def set_timetable_data(data_json):
if not _check_sharedkey():
return "Forbidden", 403
try:
_data = json.loads(data_json)
dbmgr = db.DatabaseManager()
first = True
for _tt_item in _data:
_orderby = int(_tt_item['orderby'])
_temp_id = int(_tt_item['temp_id'])
_time_hhmm = int(_tt_item['time_hhmm'])
_day_type_id = int(_tt_item['day_type_id'])
if first:
first = False
dbmgr.query("""DELETE FROM gm_timetable_type_data
WHERE day_type_id = ?""", (_tt_item['day_type_id'],))
dbmgr.query("""INSERT INTO gm_timetable_type_data
(orderby, temp_id, time_hhmm, day_type_id)
VALUES(?,?,?,?)""",
(_orderby, _temp_id,
_time_hhmm, _day_type_id))
_signal_server()
except Exception:
print("set_timetable_data({}): error".format(data_json))
print(traceback.format_exc())
return "Error", 400 # BAD_REQUEST
return "Ok"
@app.route('/hompi/_refresh', methods=['GET'])
def refresh():
try:
if not _check_sharedkey():
return "Forbidden", 403
_signal_server()
except Exception:
print("_refresh(): error")
print(traceback.format_exc())
return "Error", 400 # BAD_REQUEST
return "Ok"
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)