1
|
|
|
import uuid |
2
|
|
|
from datetime import datetime, timedelta |
3
|
|
|
import falcon |
4
|
|
|
import mysql.connector |
5
|
|
|
import simplejson as json |
6
|
|
|
from core.useractivity import user_logger, admin_control, access_control, api_key_control |
7
|
|
|
import re |
8
|
|
|
import config |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class ControlModeCollection: |
12
|
|
|
def __init__(self): |
13
|
|
|
""""Initializes""" |
14
|
|
|
pass |
15
|
|
|
|
16
|
|
|
@staticmethod |
17
|
|
|
def on_options(req, resp): |
18
|
|
|
_ = req |
19
|
|
|
resp.status = falcon.HTTP_200 |
20
|
|
|
|
21
|
|
|
@staticmethod |
22
|
|
|
def on_get(req, resp): |
23
|
|
|
if 'API-KEY' not in req.headers or \ |
24
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
25
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
26
|
|
|
access_control(req) |
27
|
|
|
else: |
28
|
|
|
api_key_control(req) |
29
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
30
|
|
|
cursor = cnx.cursor() |
31
|
|
|
|
32
|
|
|
query = (" SELECT id, name, uuid, is_active " |
33
|
|
|
" FROM tbl_control_modes " |
34
|
|
|
" ORDER BY id ") |
35
|
|
|
cursor.execute(query) |
36
|
|
|
rows = cursor.fetchall() |
37
|
|
|
|
38
|
|
|
result = list() |
39
|
|
|
if rows is not None and len(rows) > 0: |
40
|
|
|
for row in rows: |
41
|
|
|
meta_result = {"id": row[0], "name": row[1], "uuid": row[2], "is_active": bool(row[3]), 'times': list()} |
42
|
|
|
|
43
|
|
|
query = (" SELECT start_time_of_day, end_time_of_day, power_value " |
44
|
|
|
" FROM tbl_control_modes_times " |
45
|
|
|
" WHERE control_mode_id = %s " |
46
|
|
|
" ORDER BY id") |
47
|
|
|
cursor.execute(query, (meta_result['id'],)) |
48
|
|
|
rows_times = cursor.fetchall() |
49
|
|
|
if rows_times is not None and len(rows_times) > 0: |
50
|
|
|
for row_time in rows_times: |
51
|
|
|
start_time_of_day = str(row_time[0]) |
52
|
|
|
if re.search("^\d:", start_time_of_day): |
53
|
|
|
start_time_of_day = '0' + start_time_of_day[:4] |
54
|
|
|
else: |
55
|
|
|
start_time_of_day = start_time_of_day[:5] |
56
|
|
|
end_time_of_day = str(row_time[1]) |
57
|
|
|
if re.search("^\d:", end_time_of_day): |
58
|
|
|
end_time_of_day = '0' + end_time_of_day[:4] |
59
|
|
|
else: |
60
|
|
|
end_time_of_day = end_time_of_day[:5] |
61
|
|
|
meta_data = {"start_time_of_day": start_time_of_day, |
62
|
|
|
"end_time_of_day": end_time_of_day, |
63
|
|
|
"power_value": row_time[2]} |
64
|
|
|
meta_result['times'].append(meta_data) |
65
|
|
|
result.append(meta_result) |
66
|
|
|
|
67
|
|
|
cursor.close() |
68
|
|
|
cnx.close() |
69
|
|
|
|
70
|
|
|
resp.text = json.dumps(result) |
71
|
|
|
|
72
|
|
|
@staticmethod |
73
|
|
|
@user_logger |
74
|
|
|
def on_post(req, resp): |
75
|
|
|
"""Handles POST requests""" |
76
|
|
|
admin_control(req) |
77
|
|
|
try: |
78
|
|
|
raw_json = req.stream.read().decode('utf-8') |
79
|
|
|
except Exception as ex: |
80
|
|
|
print(str(ex)) |
81
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
82
|
|
|
title='API.BAD_REQUEST', |
83
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
84
|
|
|
new_values = json.loads(raw_json) |
85
|
|
|
|
86
|
|
|
if 'name' not in new_values['data'].keys() or \ |
87
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
88
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
89
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
90
|
|
|
description='API.INVALID_CONTROL_MODE_NAME') |
91
|
|
|
name = str.strip(new_values['data']['name']) |
92
|
|
|
|
93
|
|
|
if 'is_active' not in new_values['data'].keys() or \ |
94
|
|
|
not isinstance(new_values['data']['is_active'], bool): |
95
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
96
|
|
|
description='API.INVALID_IS_ACTIVE_VALUE') |
97
|
|
|
is_active = new_values['data']['is_active'] |
98
|
|
|
|
99
|
|
|
if new_values['data']['times'] is None: |
100
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
101
|
|
|
title='API.BAD_REQUEST', |
102
|
|
|
description='API.INVALID_CONTROL_MODE_TIMES') |
103
|
|
|
|
104
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
105
|
|
|
cursor = cnx.cursor() |
106
|
|
|
|
107
|
|
|
cursor.execute(" SELECT name " |
108
|
|
|
" FROM tbl_control_modes " |
109
|
|
|
" WHERE name = %s ", (name,)) |
110
|
|
|
if cursor.fetchone() is not None: |
111
|
|
|
cursor.close() |
112
|
|
|
cnx.close() |
113
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
114
|
|
|
description='API.CONTROL_MODE_NAME_IS_ALREADY_IN_USE') |
115
|
|
|
|
116
|
|
|
add_row = (" INSERT INTO tbl_control_modes " |
117
|
|
|
" (name, uuid, is_active ) " |
118
|
|
|
" VALUES (%s, %s, %s) ") |
119
|
|
|
cursor.execute(add_row, (name, |
120
|
|
|
str(uuid.uuid4()), |
121
|
|
|
is_active)) |
122
|
|
|
new_id = cursor.lastrowid |
123
|
|
|
cnx.commit() |
124
|
|
|
for item in new_values['data']['times']: |
125
|
|
|
add_time = (" INSERT INTO tbl_control_modes_times " |
126
|
|
|
" (control_mode_id, start_time_of_day, end_time_of_day, power_value) " |
127
|
|
|
" VALUES (%s, %s, %s, %s) ") |
128
|
|
|
cursor.execute(add_time, (new_id, |
129
|
|
|
item['start_time_of_day'], |
130
|
|
|
item['end_time_of_day'], |
131
|
|
|
item['power_value'])) |
132
|
|
|
cnx.commit() |
133
|
|
|
|
134
|
|
|
cursor.close() |
135
|
|
|
cnx.close() |
136
|
|
|
|
137
|
|
|
resp.status = falcon.HTTP_201 |
138
|
|
|
resp.location = '/controlmodes/' + str(new_id) |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
class ControlModeItem: |
142
|
|
|
def __init__(self): |
143
|
|
|
""""Initializes""" |
144
|
|
|
pass |
145
|
|
|
|
146
|
|
|
@staticmethod |
147
|
|
|
def on_options(req, resp, id_): |
148
|
|
|
_ = req |
149
|
|
|
resp.status = falcon.HTTP_200 |
150
|
|
|
_ = id_ |
151
|
|
|
|
152
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
153
|
|
|
def on_get(req, resp, id_): |
154
|
|
|
"""Handles GET requests""" |
155
|
|
|
if 'API-KEY' not in req.headers or \ |
156
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
157
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
158
|
|
|
access_control(req) |
159
|
|
|
else: |
160
|
|
|
api_key_control(req) |
161
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
162
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
163
|
|
|
description='API.INVALID_CONTROL_MODE_ID') |
164
|
|
|
|
165
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
166
|
|
|
cursor = cnx.cursor() |
167
|
|
|
|
168
|
|
|
query = (" SELECT id, name, uuid, is_active " |
169
|
|
|
" FROM tbl_control_modes " |
170
|
|
|
" WHERE id = %s ") |
171
|
|
|
cursor.execute(query, (id_,)) |
172
|
|
|
row = cursor.fetchone() |
173
|
|
|
if row is None: |
174
|
|
|
cursor.close() |
175
|
|
|
cnx.close() |
176
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
177
|
|
|
description='API.CONTROL_MODE_NOT_FOUND') |
178
|
|
|
|
179
|
|
|
result = {"id": row[0], "name": row[1], "uuid": row[2], "is_active": bool(row[3]), 'times': list()} |
180
|
|
|
|
181
|
|
|
query = (" SELECT start_time_of_day, end_time_of_day, power_value " |
182
|
|
|
" FROM tbl_control_modes_times " |
183
|
|
|
" WHERE control_mode_id = %s " |
184
|
|
|
" ORDER BY id ") |
185
|
|
|
cursor.execute(query, (result['id'],)) |
186
|
|
|
rows_times = cursor.fetchall() |
187
|
|
|
if rows_times is not None and len(rows_times) > 0: |
188
|
|
|
for row_time in rows_times: |
189
|
|
|
start_time_of_day = str(row_time[0]) |
190
|
|
|
print('start_time_of_day=' + start_time_of_day) |
191
|
|
|
if re.search("^\d:", start_time_of_day): |
192
|
|
|
start_time_of_day = '0' + start_time_of_day[:4] |
193
|
|
|
else: |
194
|
|
|
start_time_of_day = start_time_of_day[:5] |
195
|
|
|
end_time_of_day = str(row_time[1]) |
196
|
|
|
if re.search("^\d:", end_time_of_day): |
197
|
|
|
end_time_of_day = '0' + end_time_of_day[:4] |
198
|
|
|
else: |
199
|
|
|
end_time_of_day = end_time_of_day[:5] |
200
|
|
|
meta_data = {"start_time_of_day": start_time_of_day, |
201
|
|
|
"end_time_of_day": end_time_of_day, |
202
|
|
|
"power_value": row_time[2]} |
203
|
|
|
result['times'].append(meta_data) |
204
|
|
|
|
205
|
|
|
cursor.close() |
206
|
|
|
cnx.close() |
207
|
|
|
|
208
|
|
|
resp.text = json.dumps(result) |
209
|
|
|
|
210
|
|
|
@staticmethod |
211
|
|
|
@user_logger |
212
|
|
|
def on_delete(req, resp, id_): |
213
|
|
|
"""Handles DELETE requests""" |
214
|
|
|
admin_control(req) |
215
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
216
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
217
|
|
|
title='API.BAD_REQUEST', |
218
|
|
|
description='API.INVALID_CONTROL_MODE_ID') |
219
|
|
|
|
220
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
221
|
|
|
cursor = cnx.cursor() |
222
|
|
|
|
223
|
|
|
cursor.execute(" SELECT name " |
224
|
|
|
" FROM tbl_control_modes " |
225
|
|
|
" WHERE id = %s ", (id_,)) |
226
|
|
|
if cursor.fetchone() is None: |
227
|
|
|
cursor.close() |
228
|
|
|
cnx.close() |
229
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
230
|
|
|
description='API.CONTROL_MODE_NOT_FOUND') |
231
|
|
|
|
232
|
|
|
cursor.execute(" DELETE FROM tbl_control_modes_times WHERE control_mode_id = %s ", (id_,)) |
233
|
|
|
cnx.commit() |
234
|
|
|
|
235
|
|
|
cursor.execute(" DELETE FROM tbl_control_modes WHERE id = %s ", (id_,)) |
236
|
|
|
cnx.commit() |
237
|
|
|
|
238
|
|
|
cursor.close() |
239
|
|
|
cnx.close() |
240
|
|
|
|
241
|
|
|
resp.status = falcon.HTTP_204 |
242
|
|
|
|
243
|
|
|
@staticmethod |
244
|
|
|
@user_logger |
245
|
|
|
def on_put(req, resp, id_): |
246
|
|
|
"""Handles PUT requests""" |
247
|
|
|
admin_control(req) |
248
|
|
|
try: |
249
|
|
|
raw_json = req.stream.read().decode('utf-8') |
250
|
|
|
except Exception as ex: |
251
|
|
|
print(str(ex)) |
252
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
253
|
|
|
title='API.BAD_REQUEST', |
254
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
255
|
|
|
|
256
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
257
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
258
|
|
|
description='API.INVALID_CONTROL_MODE_ID') |
259
|
|
|
|
260
|
|
|
new_values = json.loads(raw_json) |
261
|
|
|
|
262
|
|
|
if 'name' not in new_values['data'].keys() or \ |
263
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
264
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
265
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
266
|
|
|
description='API.INVALID_CONTROL_MODE_NAME') |
267
|
|
|
name = str.strip(new_values['data']['name']) |
268
|
|
|
|
269
|
|
|
if 'is_active' not in new_values['data'].keys() or \ |
270
|
|
|
not isinstance(new_values['data']['is_active'], bool): |
271
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
272
|
|
|
description='API.INVALID_IS_ACTIVE_VALUE') |
273
|
|
|
is_active = new_values['data']['is_active'] |
274
|
|
|
|
275
|
|
|
if 'times' not in new_values['data'].keys() or \ |
276
|
|
|
not isinstance(new_values['data']['times'], list) or \ |
277
|
|
|
len(new_values['data']['times']) == 0: |
278
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
279
|
|
|
title='API.BAD_REQUEST', |
280
|
|
|
description='API.INVALID_CONTROL_MODE_TIMES') |
281
|
|
|
|
282
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
283
|
|
|
cursor = cnx.cursor() |
284
|
|
|
|
285
|
|
|
# check if the control mode exists |
286
|
|
|
query = (" SELECT name " |
287
|
|
|
" FROM tbl_control_modes " |
288
|
|
|
" WHERE id = %s ") |
289
|
|
|
cursor.execute(query, (id_,)) |
290
|
|
|
cursor.fetchone() |
291
|
|
|
|
292
|
|
|
if cursor.rowcount != 1: |
293
|
|
|
cursor.close() |
294
|
|
|
cnx.close() |
295
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
296
|
|
|
description='API.CONTROL_MODE_NOT_FOUND') |
297
|
|
|
|
298
|
|
|
cursor.execute(" SELECT name " |
299
|
|
|
" FROM tbl_control_modes " |
300
|
|
|
" WHERE name = %s AND id != %s ", (name, id_)) |
301
|
|
|
if cursor.fetchone() is not None: |
302
|
|
|
cursor.close() |
303
|
|
|
cnx.close() |
304
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
305
|
|
|
description='API.CONTROL_MODE_NAME_IS_ALREADY_IN_USE') |
306
|
|
|
|
307
|
|
|
# update control mode |
308
|
|
|
update_row = (" UPDATE tbl_control_modes " |
309
|
|
|
" SET name = %s, is_active = %s " |
310
|
|
|
" WHERE id = %s ") |
311
|
|
|
cursor.execute(update_row, (name, |
312
|
|
|
is_active, |
313
|
|
|
id_,)) |
314
|
|
|
cnx.commit() |
315
|
|
|
|
316
|
|
|
# remove all (possible) exist times |
317
|
|
|
cursor.execute(" DELETE FROM tbl_control_modes_times " |
318
|
|
|
" WHERE control_mode_id = %s ", |
319
|
|
|
(id_,)) |
320
|
|
|
cnx.commit() |
321
|
|
|
|
322
|
|
|
for item in new_values['data']['times']: |
323
|
|
|
add_time = (" INSERT INTO tbl_control_modes_times " |
324
|
|
|
" (control_mode_id, start_time_of_day, end_time_of_day, power_value) " |
325
|
|
|
" VALUES (%s, %s, %s, %s) ") |
326
|
|
|
cursor.execute(add_time, (id_, |
327
|
|
|
item['start_time_of_day'], |
328
|
|
|
item['end_time_of_day'], |
329
|
|
|
item['power_value'])) |
330
|
|
|
cnx.commit() |
331
|
|
|
cursor.close() |
332
|
|
|
cnx.close() |
333
|
|
|
resp.status = falcon.HTTP_200 |
334
|
|
|
|
335
|
|
|
|
336
|
|
|
class ControlModeExport: |
337
|
|
|
def __init__(self): |
338
|
|
|
""""Initializes""" |
339
|
|
|
pass |
340
|
|
|
|
341
|
|
|
@staticmethod |
342
|
|
|
def on_options(req, resp, id_): |
343
|
|
|
_ = req |
344
|
|
|
resp.status = falcon.HTTP_200 |
345
|
|
|
_ = id_ |
346
|
|
|
|
347
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
348
|
|
|
def on_get(req, resp, id_): |
349
|
|
|
"""Handles GET requests""" |
350
|
|
|
if 'API-KEY' not in req.headers or \ |
351
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
352
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
353
|
|
|
access_control(req) |
354
|
|
|
else: |
355
|
|
|
api_key_control(req) |
356
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
357
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
358
|
|
|
description='API.INVALID_CONTROL_MODE_ID') |
359
|
|
|
|
360
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
361
|
|
|
cursor = cnx.cursor() |
362
|
|
|
|
363
|
|
|
query = (" SELECT id, name, uuid, is_active " |
364
|
|
|
" FROM tbl_control_modes " |
365
|
|
|
" WHERE id = %s ") |
366
|
|
|
cursor.execute(query, (id_,)) |
367
|
|
|
row = cursor.fetchone() |
368
|
|
|
if row is None: |
369
|
|
|
cursor.close() |
370
|
|
|
cnx.close() |
371
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
372
|
|
|
description='API.CONTROL_MODE_NOT_FOUND') |
373
|
|
|
|
374
|
|
|
result = {"id": row[0], "name": row[1], "uuid": row[2], "is_active": bool(row[3]), 'times': list()} |
375
|
|
|
|
376
|
|
|
query = (" SELECT start_time_of_day, end_time_of_day, power_value " |
377
|
|
|
" FROM tbl_control_modes_times " |
378
|
|
|
" WHERE control_mode_id = %s " |
379
|
|
|
" ORDER BY id") |
380
|
|
|
cursor.execute(query, (result['id'],)) |
381
|
|
|
rows_times = cursor.fetchall() |
382
|
|
|
if rows_times is not None and len(rows_times) > 0: |
383
|
|
|
for row_time in rows_times: |
384
|
|
|
start_time_of_day = str(row_time[0]) |
385
|
|
|
if re.search("^\d:", start_time_of_day): |
386
|
|
|
start_time_of_day = '0' + start_time_of_day[:4] |
387
|
|
|
else: |
388
|
|
|
start_time_of_day = start_time_of_day[:5] |
389
|
|
|
end_time_of_day = str(row_time[1]) |
390
|
|
|
if re.search("^\d:", end_time_of_day): |
391
|
|
|
end_time_of_day = '0' + end_time_of_day[:4] |
392
|
|
|
else: |
393
|
|
|
end_time_of_day = end_time_of_day[:5] |
394
|
|
|
meta_data = {"start_time_of_day": start_time_of_day, |
395
|
|
|
"end_time_of_day": end_time_of_day, |
396
|
|
|
"power_value": row_time[2]} |
397
|
|
|
result['times'].append(meta_data) |
398
|
|
|
|
399
|
|
|
cursor.close() |
400
|
|
|
cnx.close() |
401
|
|
|
|
402
|
|
|
resp.text = json.dumps(result) |
403
|
|
|
|
404
|
|
|
|
405
|
|
|
class ControlModeImport: |
406
|
|
|
def __init__(self): |
407
|
|
|
""""Initializes""" |
408
|
|
|
pass |
409
|
|
|
|
410
|
|
|
@staticmethod |
411
|
|
|
def on_options(req, resp): |
412
|
|
|
_ = req |
413
|
|
|
resp.status = falcon.HTTP_200 |
414
|
|
|
|
415
|
|
|
@staticmethod |
416
|
|
|
@user_logger |
417
|
|
|
def on_post(req, resp): |
418
|
|
|
"""Handles POST requests""" |
419
|
|
|
admin_control(req) |
420
|
|
|
try: |
421
|
|
|
raw_json = req.stream.read().decode('utf-8') |
422
|
|
|
except Exception as ex: |
423
|
|
|
print(str(ex)) |
424
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
425
|
|
|
title='API.BAD_REQUEST', |
426
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
427
|
|
|
new_values = json.loads(raw_json) |
428
|
|
|
|
429
|
|
|
if 'name' not in new_values.keys() or \ |
430
|
|
|
not isinstance(new_values['name'], str) or \ |
431
|
|
|
len(str.strip(new_values['name'])) == 0: |
432
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
433
|
|
|
description='API.INVALID_METER_NAME') |
434
|
|
|
name = str.strip(new_values['name']) |
435
|
|
|
|
436
|
|
|
if 'is_active' not in new_values.keys() or \ |
437
|
|
|
not isinstance(new_values['is_active'], bool): |
438
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
439
|
|
|
description='API.INVALID_IS_ACTIVE_VALUE') |
440
|
|
|
is_active = new_values['is_active'] |
441
|
|
|
|
442
|
|
|
if 'times' not in new_values.keys() or \ |
443
|
|
|
not isinstance(new_values['times'], list) or \ |
444
|
|
|
len(new_values['times']) == 0: |
445
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
446
|
|
|
title='API.BAD_REQUEST', |
447
|
|
|
description='API.INVALID_CONTROL_MODE_TIMES') |
448
|
|
|
|
449
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
450
|
|
|
cursor = cnx.cursor() |
451
|
|
|
|
452
|
|
|
cursor.execute(" SELECT name " |
453
|
|
|
" FROM tbl_control_modes " |
454
|
|
|
" WHERE name = %s ", (name,)) |
455
|
|
|
if cursor.fetchone() is not None: |
456
|
|
|
cursor.close() |
457
|
|
|
cnx.close() |
458
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
459
|
|
|
description='API.CONTROL_MODE_NAME_IS_ALREADY_IN_USE') |
460
|
|
|
|
461
|
|
|
add_row = (" INSERT INTO tbl_control_modes " |
462
|
|
|
" (name, uuid, is_active ) " |
463
|
|
|
" VALUES (%s, %s, %s) ") |
464
|
|
|
cursor.execute(add_row, (name, |
465
|
|
|
str(uuid.uuid4()), |
466
|
|
|
is_active,)) |
467
|
|
|
new_id = cursor.lastrowid |
468
|
|
|
cnx.commit() |
469
|
|
|
|
470
|
|
|
for item in new_values['times']: |
471
|
|
|
add_time = (" INSERT INTO tbl_control_modes_times " |
472
|
|
|
" (control_mode_id, start_time_of_day, end_time_of_day, power_value) " |
473
|
|
|
" VALUES (%s, %s, %s, %s) ") |
474
|
|
|
cursor.execute(add_time, (new_id, |
475
|
|
|
item['start_time_of_day'], |
476
|
|
|
item['end_time_of_day'], |
477
|
|
|
item['power_value'])) |
478
|
|
|
cnx.commit() |
479
|
|
|
|
480
|
|
|
cursor.close() |
481
|
|
|
cnx.close() |
482
|
|
|
|
483
|
|
|
resp.status = falcon.HTTP_201 |
484
|
|
|
resp.location = '/controlmodes/' + str(new_id) |
485
|
|
|
|
486
|
|
|
|
487
|
|
|
class ControlModeClone: |
488
|
|
|
def __init__(self): |
489
|
|
|
""""Initializes""" |
490
|
|
|
pass |
491
|
|
|
|
492
|
|
|
@staticmethod |
493
|
|
|
def on_options(req, resp, id_): |
494
|
|
|
_ = req |
495
|
|
|
resp.status = falcon.HTTP_200 |
496
|
|
|
_ = id_ |
497
|
|
|
|
498
|
|
|
@staticmethod |
499
|
|
|
@user_logger |
500
|
|
|
def on_post(req, resp, id_): |
501
|
|
|
admin_control(req) |
502
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
503
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
504
|
|
|
description='API.INVALID_CONTROL_MODE_ID') |
505
|
|
|
|
506
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
507
|
|
|
cursor = cnx.cursor() |
508
|
|
|
|
509
|
|
|
query = (" SELECT id, name, uuid, is_active " |
510
|
|
|
" FROM tbl_control_modes " |
511
|
|
|
" WHERE id = %s ") |
512
|
|
|
cursor.execute(query, (id_,)) |
513
|
|
|
row = cursor.fetchone() |
514
|
|
|
if row is None: |
515
|
|
|
cursor.close() |
516
|
|
|
cnx.close() |
517
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
518
|
|
|
description='API.CONTROL_MODE_NOT_FOUND') |
519
|
|
|
|
520
|
|
|
result = {"id": row[0], "name": row[1], "uuid": row[2], "is_active": bool(row[3]), 'times': list()} |
521
|
|
|
|
522
|
|
|
query = (" SELECT start_time_of_day, end_time_of_day, power_value " |
523
|
|
|
" FROM tbl_control_modes_times " |
524
|
|
|
" WHERE control_mode_id = %s " |
525
|
|
|
" ORDER BY id") |
526
|
|
|
cursor.execute(query, (result['id'],)) |
527
|
|
|
rows_times = cursor.fetchall() |
528
|
|
|
if rows_times is not None and len(rows_times) > 0: |
529
|
|
|
for row_time in rows_times: |
530
|
|
|
start_time_of_day = str(row_time[0]) |
531
|
|
|
if re.search("^\d:", start_time_of_day): |
532
|
|
|
start_time_of_day = '0' + start_time_of_day[:4] |
533
|
|
|
else: |
534
|
|
|
start_time_of_day = start_time_of_day[:5] |
535
|
|
|
end_time_of_day = str(row_time[1]) |
536
|
|
|
if re.search("^\d:", end_time_of_day): |
537
|
|
|
end_time_of_day = '0' + end_time_of_day[:4] |
538
|
|
|
else: |
539
|
|
|
end_time_of_day = end_time_of_day[:5] |
540
|
|
|
meta_data = {"start_time_of_day": start_time_of_day, |
541
|
|
|
"end_time_of_day": end_time_of_day, |
542
|
|
|
"power_value": row_time[2]} |
543
|
|
|
result['times'].append(meta_data) |
544
|
|
|
|
545
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
546
|
|
|
if config.utc_offset[0] == '-': |
547
|
|
|
timezone_offset = -timezone_offset |
548
|
|
|
new_name = (str.strip(result['name']) + |
549
|
|
|
(datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')) |
550
|
|
|
add_row = (" INSERT INTO tbl_control_modes " |
551
|
|
|
" (name, uuid, is_active) " |
552
|
|
|
" VALUES (%s, %s, %s) ") |
553
|
|
|
cursor.execute(add_row, (new_name, |
554
|
|
|
str(uuid.uuid4()), |
555
|
|
|
result['is_active'])) |
556
|
|
|
new_id = cursor.lastrowid |
557
|
|
|
cnx.commit() |
558
|
|
|
for item in result['times']: |
559
|
|
|
add_time = (" INSERT INTO tbl_control_modes_times " |
560
|
|
|
" (control_mode_id, start_time_of_day, end_time_of_day, power_value) " |
561
|
|
|
" VALUES (%s, %s, %s, %s) ") |
562
|
|
|
cursor.execute(add_time, (new_id, |
563
|
|
|
item['start_time_of_day'], |
564
|
|
|
item['end_time_of_day'], |
565
|
|
|
item['power_value'])) |
566
|
|
|
cnx.commit() |
567
|
|
|
cursor.close() |
568
|
|
|
cnx.close() |
569
|
|
|
|
570
|
|
|
resp.status = falcon.HTTP_201 |
571
|
|
|
resp.location = '/controlmodes/' + str(new_id) |
572
|
|
|
|
573
|
|
|
|
574
|
|
|
class ControlModeTimeCollection: |
575
|
|
|
def __init__(self): |
576
|
|
|
"""Initializes""" |
577
|
|
|
pass |
578
|
|
|
|
579
|
|
|
@staticmethod |
580
|
|
|
def on_options(req, resp, id_): |
581
|
|
|
_ = req |
582
|
|
|
resp.status = falcon.HTTP_200 |
583
|
|
|
_ = id_ |
584
|
|
|
|
585
|
|
|
@staticmethod |
586
|
|
|
def on_get(req, resp, id_): |
587
|
|
|
if 'API-KEY' not in req.headers or \ |
588
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
589
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
590
|
|
|
access_control(req) |
591
|
|
|
else: |
592
|
|
|
api_key_control(req) |
593
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
594
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
595
|
|
|
description='API.INVALID_CONTROL_MODE_ID') |
596
|
|
|
|
597
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
598
|
|
|
cursor = cnx.cursor() |
599
|
|
|
|
600
|
|
|
query = (" SELECT id, name " |
601
|
|
|
" FROM tbl_points ") |
602
|
|
|
cursor.execute(query) |
603
|
|
|
rows_points = cursor.fetchall() |
604
|
|
|
|
605
|
|
|
point_dict = dict() |
606
|
|
|
if rows_points is not None and len(rows_points) > 0: |
607
|
|
|
for row in rows_points: |
608
|
|
|
point_dict[row[0]] = {"id": row[0], |
609
|
|
|
"name": row[1]} |
610
|
|
|
|
611
|
|
|
cursor.execute(" SELECT name " |
612
|
|
|
" FROM tbl_control_modes " |
613
|
|
|
" WHERE id = %s ", (id_,)) |
614
|
|
|
if cursor.fetchone() is None: |
615
|
|
|
cursor.close() |
616
|
|
|
cnx.close() |
617
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
618
|
|
|
description='API.CONTROL_MODE_NOT_FOUND') |
619
|
|
|
|
620
|
|
|
query = (" SELECT id, start_time_of_day, end_time_of_day, " |
621
|
|
|
" power_value, power_point_id, power_equation, description " |
622
|
|
|
" FROM tbl_control_modes_times " |
623
|
|
|
" WHERE control_mode_id = %s " |
624
|
|
|
" ORDER BY id ") |
625
|
|
|
cursor.execute(query, (id_,)) |
626
|
|
|
rows_times = cursor.fetchall() |
627
|
|
|
|
628
|
|
|
result = list() |
629
|
|
|
if rows_times is not None and len(rows_times) > 0: |
630
|
|
|
for row_time in rows_times: |
631
|
|
|
start_time_of_day = str(row_time[1]) |
632
|
|
|
if re.search("^\d:", start_time_of_day): |
633
|
|
|
start_time_of_day = '0' + start_time_of_day[:4] |
634
|
|
|
else: |
635
|
|
|
start_time_of_day = start_time_of_day[:5] |
636
|
|
|
end_time_of_day = str(row_time[2]) |
637
|
|
|
if re.search("^\d:", end_time_of_day): |
638
|
|
|
end_time_of_day = '0' + end_time_of_day[:4] |
639
|
|
|
else: |
640
|
|
|
end_time_of_day = end_time_of_day[:5] |
641
|
|
|
meta_result = {"id": row[0], |
|
|
|
|
642
|
|
|
"start_time_of_day": start_time_of_day, |
643
|
|
|
"end_time_of_day": end_time_of_day, |
644
|
|
|
"power_value": row_time[3], |
645
|
|
|
"power_point": point_dict.get(row_time[4], None), |
646
|
|
|
"power_equation": row_time[5], |
647
|
|
|
"description": row_time[6]} |
648
|
|
|
result.append(meta_result) |
649
|
|
|
print(result) |
650
|
|
|
cursor.close() |
651
|
|
|
cnx.close() |
652
|
|
|
resp.text = json.dumps(result) |
653
|
|
|
|
654
|
|
|
@staticmethod |
655
|
|
|
@user_logger |
656
|
|
|
def on_post(req, resp, id_): |
657
|
|
|
"""Handles POST requests""" |
658
|
|
|
admin_control(req) |
659
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
660
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
661
|
|
|
description='API.INVALID_CONTROL_MODE_ID') |
662
|
|
|
try: |
663
|
|
|
raw_json = req.stream.read().decode('utf-8') |
664
|
|
|
except Exception as ex: |
665
|
|
|
print(str(ex)) |
666
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
667
|
|
|
title='API.BAD_REQUEST', |
668
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
669
|
|
|
|
670
|
|
|
new_values = json.loads(raw_json) |
671
|
|
|
|
672
|
|
|
if 'start_time_of_day' not in new_values['data'].keys() or \ |
673
|
|
|
not isinstance(new_values['data']['start_time_of_day'], str) or \ |
674
|
|
|
len(str.strip(new_values['data']['start_time_of_day'])) == 0: |
675
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
676
|
|
|
description='API.INVALID_START_TIME_OF_DAY') |
677
|
|
|
start_time_of_day = str.strip(new_values['data']['start_time_of_day']) |
678
|
|
|
|
679
|
|
|
if 'end_time_of_day' not in new_values['data'].keys() or \ |
680
|
|
|
not isinstance(new_values['data']['end_time_of_day'], str) or \ |
681
|
|
|
len(str.strip(new_values['data']['end_time_of_day'])) == 0: |
682
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
683
|
|
|
description='API.INVALID_END_TIME_OF_DAY') |
684
|
|
|
end_time_of_day = str.strip(new_values['data']['end_time_of_day']) |
685
|
|
|
|
686
|
|
|
power_value = None |
687
|
|
|
if 'power_value' in new_values['data'].keys(): |
688
|
|
|
if new_values['data']['power_value'] is not None and \ |
689
|
|
|
(isinstance(new_values['data']['power_value'], int) or |
690
|
|
|
isinstance(new_values['data']['power_value'], float)): |
691
|
|
|
power_value = str.strip(new_values['data']['power_value']) |
692
|
|
|
|
693
|
|
|
power_point_id = None |
694
|
|
|
if 'power_point_id' in new_values['data'].keys(): |
695
|
|
|
if new_values['data']['power_point_id'] is not None and \ |
696
|
|
|
new_values['data']['power_point_id'] <= 0: |
697
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
698
|
|
|
description='API.INVALID_POWER_POINT_ID') |
699
|
|
|
power_point_id = new_values['data']['power_point_id'] |
700
|
|
|
|
701
|
|
|
power_equation = None |
702
|
|
|
if 'power_equation' in new_values['data'].keys(): |
703
|
|
|
if new_values['data']['power_equation'] is not None and \ |
704
|
|
|
isinstance(new_values['data']['power_equation'], str) and \ |
705
|
|
|
len(str.strip(new_values['data']['power_equation'])) > 0: |
706
|
|
|
power_equation = str.strip(new_values['data']['power_equation']) |
707
|
|
|
|
708
|
|
|
description = None |
709
|
|
|
if 'description' in new_values['data'].keys(): |
710
|
|
|
if new_values['data']['description'] is not None and \ |
711
|
|
|
isinstance(new_values['data']['description'], str) and \ |
712
|
|
|
len(str.strip(new_values['data']['description'])) > 0: |
713
|
|
|
description = str.strip(new_values['data']['description']) |
714
|
|
|
|
715
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
716
|
|
|
cursor = cnx.cursor() |
717
|
|
|
cursor.execute(" SELECT name " |
718
|
|
|
" FROM tbl_control_modes " |
719
|
|
|
" WHERE id = %s ", (id_,)) |
720
|
|
|
if cursor.fetchone() is None: |
721
|
|
|
cursor.close() |
722
|
|
|
cnx.close() |
723
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
724
|
|
|
description='API.CONTROL_MODE_NOT_FOUND') |
725
|
|
|
|
726
|
|
|
add_values = (" INSERT INTO tbl_control_modes_times " |
727
|
|
|
" (control_mode_id, start_time_of_day, end_time_of_day, power_value, " |
728
|
|
|
" power_point_id, power_equation, description) " |
729
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
730
|
|
|
cursor.execute(add_values, (id_, |
731
|
|
|
start_time_of_day, |
732
|
|
|
end_time_of_day, |
733
|
|
|
power_value, |
734
|
|
|
power_point_id, |
735
|
|
|
power_equation, |
736
|
|
|
description)) |
737
|
|
|
new_id = cursor.lastrowid |
738
|
|
|
cnx.commit() |
739
|
|
|
cursor.close() |
740
|
|
|
cnx.close() |
741
|
|
|
|
742
|
|
|
resp.status = falcon.HTTP_201 |
743
|
|
|
resp.location = '/controlmodes/' + str(id_) + 'times/' + str(new_id) |
744
|
|
|
|
745
|
|
|
|
746
|
|
|
class ControlModeTimeItem: |
747
|
|
|
@staticmethod |
748
|
|
|
@user_logger |
749
|
|
|
def __init__(): |
750
|
|
|
"""Initializes""" |
751
|
|
|
pass |
752
|
|
|
|
753
|
|
|
@staticmethod |
754
|
|
|
def on_options(req, resp, id_, tid): |
755
|
|
|
_ = req |
756
|
|
|
resp.status = falcon.HTTP_200 |
757
|
|
|
_ = id_ |
758
|
|
|
|
759
|
|
|
@staticmethod |
760
|
|
|
def on_get(req, resp, id_, tid): |
761
|
|
|
if 'API-KEY' not in req.headers or \ |
762
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
763
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
764
|
|
|
access_control(req) |
765
|
|
|
else: |
766
|
|
|
api_key_control(req) |
767
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
768
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
769
|
|
|
description='API.INVALID_CONTROL_MODE_ID') |
770
|
|
|
|
771
|
|
|
if not tid.isdigit() or int(tid) <= 0: |
772
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
773
|
|
|
description='API.INVALID_CONTROL_MODE_TIME_ID') |
774
|
|
|
|
775
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
776
|
|
|
cursor = cnx.cursor() |
777
|
|
|
|
778
|
|
|
query = (" SELECT id, name " |
779
|
|
|
" FROM tbl_points ") |
780
|
|
|
cursor.execute(query) |
781
|
|
|
rows_points = cursor.fetchall() |
782
|
|
|
|
783
|
|
|
point_dict = dict() |
784
|
|
|
if rows_points is not None and len(rows_points) > 0: |
785
|
|
|
for row in rows_points: |
786
|
|
|
point_dict[row[0]] = {"id": row[0], |
787
|
|
|
"name": row[1]} |
788
|
|
|
|
789
|
|
|
query = (" SELECT id, control_mode_id, start_time_of_day, end_time_of_day, " |
790
|
|
|
" power_value, power_point_id, power_equation, description " |
791
|
|
|
" FROM tbl_control_modes_times " |
792
|
|
|
" WHERE control_mode_id = %s AND id = %s ") |
793
|
|
|
cursor.execute(query, (id_, tid)) |
794
|
|
|
row_time = cursor.fetchone() |
795
|
|
|
cursor.close() |
796
|
|
|
cnx.close() |
797
|
|
|
|
798
|
|
|
if row_time is None: |
799
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
800
|
|
|
description='API.CONTROL_MODE_TIME_NOT_FOUND_OR_NOT_MATCH') |
801
|
|
|
else: |
802
|
|
|
start_time_of_day = str(row_time[2]) |
803
|
|
|
if re.search("^\d:", start_time_of_day): |
804
|
|
|
start_time_of_day = '0' + start_time_of_day[:4] |
805
|
|
|
else: |
806
|
|
|
start_time_of_day = start_time_of_day[:5] |
807
|
|
|
end_time_of_day = str(row_time[3]) |
808
|
|
|
if re.search("^\d:", end_time_of_day): |
809
|
|
|
end_time_of_day = '0' + end_time_of_day[:4] |
810
|
|
|
else: |
811
|
|
|
end_time_of_day = end_time_of_day[:5] |
812
|
|
|
meta_result = {"id": row_time[0], |
813
|
|
|
"control_mode_id": row_time[1], |
814
|
|
|
"start_time_of_day": start_time_of_day, |
815
|
|
|
"end_time_of_day": end_time_of_day, |
816
|
|
|
"power_value": row_time[4], |
817
|
|
|
"power_point": point_dict.get(row_time[5], None), |
818
|
|
|
"power_equation": row_time[6], |
819
|
|
|
"description": row_time[7]} |
820
|
|
|
|
821
|
|
|
resp.text = json.dumps(meta_result) |
822
|
|
|
|
823
|
|
|
@staticmethod |
824
|
|
|
@user_logger |
825
|
|
|
def on_delete(req, resp, id_, tid): |
826
|
|
|
admin_control(req) |
827
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
828
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
829
|
|
|
description='API.INVALID_CONTROL_MODE_ID') |
830
|
|
|
|
831
|
|
|
if not tid.isdigit() or int(tid) <= 0: |
832
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
833
|
|
|
description='API.INVALID_CONTROL_MODE_TIME_ID') |
834
|
|
|
|
835
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
836
|
|
|
cursor = cnx.cursor() |
837
|
|
|
|
838
|
|
|
cursor.execute(" SELECT id " |
839
|
|
|
" FROM tbl_control_modes " |
840
|
|
|
" WHERE id = %s ", |
841
|
|
|
(id_,)) |
842
|
|
|
row = cursor.fetchone() |
843
|
|
|
if row is None: |
844
|
|
|
cursor.close() |
845
|
|
|
cnx.close() |
846
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
847
|
|
|
title='API.NOT_FOUND', |
848
|
|
|
description='API.CONTROL_MODE_NOT_FOUND') |
849
|
|
|
|
850
|
|
|
cursor.execute(" SELECT name " |
851
|
|
|
" FROM tbl_control_modes_times " |
852
|
|
|
" WHERE control_mode_id = %s AND id = %s ", |
853
|
|
|
(id_, tid,)) |
854
|
|
|
row = cursor.fetchone() |
855
|
|
|
if row is None: |
856
|
|
|
cursor.close() |
857
|
|
|
cnx.close() |
858
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
859
|
|
|
title='API.NOT_FOUND', |
860
|
|
|
description='API.CONTROL_MODE_TIME_NOT_FOUND_OR_NOT_MATCH') |
861
|
|
|
|
862
|
|
|
cursor.execute(" DELETE FROM tbl_control_modes_times " |
863
|
|
|
" WHERE id = %s ", (tid,)) |
864
|
|
|
cnx.commit() |
865
|
|
|
|
866
|
|
|
cursor.close() |
867
|
|
|
cnx.close() |
868
|
|
|
|
869
|
|
|
resp.status = falcon.HTTP_204 |
870
|
|
|
|
871
|
|
|
@staticmethod |
872
|
|
|
@user_logger |
873
|
|
|
def on_put(req, resp, id_, tid): |
874
|
|
|
"""Handles PUT requests""" |
875
|
|
|
admin_control(req) |
876
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
877
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
878
|
|
|
description='API.INVALID_CONTROL_MODE_ID') |
879
|
|
|
|
880
|
|
|
if not tid.isdigit() or int(tid) <= 0: |
881
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
882
|
|
|
description='API.INVALID_CONTROL_MODE_TIME_ID') |
883
|
|
|
|
884
|
|
|
try: |
885
|
|
|
raw_json = req.stream.read().decode('utf-8') |
886
|
|
|
except Exception as ex: |
887
|
|
|
print(str(ex)) |
888
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
889
|
|
|
title='API.BAD_REQUEST', |
890
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
891
|
|
|
|
892
|
|
|
new_values = json.loads(raw_json) |
893
|
|
|
|
894
|
|
|
if 'start_time_of_day' not in new_values['data'].keys() or \ |
895
|
|
|
not isinstance(new_values['data']['start_time_of_day'], str) or \ |
896
|
|
|
len(str.strip(new_values['data']['start_time_of_day'])) == 0: |
897
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
898
|
|
|
description='API.INVALID_START_TIME_OF_DAY') |
899
|
|
|
start_time_of_day = str.strip(new_values['data']['start_time_of_day']) |
900
|
|
|
|
901
|
|
|
if 'end_time_of_day' not in new_values['data'].keys() or \ |
902
|
|
|
not isinstance(new_values['data']['end_time_of_day'], str) or \ |
903
|
|
|
len(str.strip(new_values['data']['end_time_of_day'])) == 0: |
904
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
905
|
|
|
description='API.INVALID_END_TIME_OF_DAY') |
906
|
|
|
end_time_of_day = str.strip(new_values['data']['end_time_of_day']) |
907
|
|
|
|
908
|
|
|
power_value = None |
909
|
|
|
if 'power_value' in new_values['data'].keys(): |
910
|
|
|
if new_values['data']['power_value'] is not None and \ |
911
|
|
|
(isinstance(new_values['data']['power_value'], int) or |
912
|
|
|
isinstance(new_values['data']['power_value'], float)): |
913
|
|
|
power_value = str.strip(new_values['data']['power_value']) |
914
|
|
|
|
915
|
|
|
power_point_id = None |
916
|
|
|
if 'power_point_id' in new_values['data'].keys(): |
917
|
|
|
if new_values['data']['power_point_id'] is not None and \ |
918
|
|
|
new_values['data']['power_point_id'] <= 0: |
919
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
920
|
|
|
description='API.INVALID_POWER_POINT_ID') |
921
|
|
|
power_point_id = new_values['data']['power_point_id'] |
922
|
|
|
|
923
|
|
|
power_equation = None |
924
|
|
|
if 'power_equation' in new_values['data'].keys(): |
925
|
|
|
if new_values['data']['power_equation'] is not None and \ |
926
|
|
|
isinstance(new_values['data']['power_equation'], str) and \ |
927
|
|
|
len(str.strip(new_values['data']['power_equation'])) > 0: |
928
|
|
|
power_equation = str.strip(new_values['data']['power_equation']) |
929
|
|
|
|
930
|
|
|
description = None |
931
|
|
|
if 'description' in new_values['data'].keys(): |
932
|
|
|
if new_values['data']['description'] is not None and \ |
933
|
|
|
isinstance(new_values['data']['description'], str) and \ |
934
|
|
|
len(str.strip(new_values['data']['description'])) > 0: |
935
|
|
|
description = str.strip(new_values['data']['description']) |
936
|
|
|
|
937
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
938
|
|
|
cursor = cnx.cursor() |
939
|
|
|
|
940
|
|
|
cursor.execute(" SELECT name " |
941
|
|
|
" FROM tbl_control_modes " |
942
|
|
|
" WHERE id = %s ", (id_,)) |
943
|
|
|
if cursor.fetchone() is None: |
944
|
|
|
cursor.close() |
945
|
|
|
cnx.close() |
946
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
947
|
|
|
description='API.CONTROL_MODE_NOT_FOUND') |
948
|
|
|
|
949
|
|
|
cursor.execute(" SELECT id " |
950
|
|
|
" FROM tbl_control_modes_times " |
951
|
|
|
" WHERE control_mode_id = %s AND id = %s ", |
952
|
|
|
(id_, tid,)) |
953
|
|
|
row = cursor.fetchone() |
954
|
|
|
if row is None: |
955
|
|
|
cursor.close() |
956
|
|
|
cnx.close() |
957
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
958
|
|
|
title='API.NOT_FOUND', |
959
|
|
|
description='API.CONTROL_MODE_TIME_NOT_FOUND_OR_NOT_MATCH') |
960
|
|
|
|
961
|
|
|
add_values = (" UPDATE tbl_control_modes_times " |
962
|
|
|
" SET start_time_of_day = %s , end_time_of_day = %s, power_value = %s, " |
963
|
|
|
" power_point_id = %s, power_equation = %s, description = %s " |
964
|
|
|
" WHERE id = %s ") |
965
|
|
|
cursor.execute(add_values, (start_time_of_day, |
966
|
|
|
end_time_of_day, |
967
|
|
|
power_value, |
968
|
|
|
power_point_id, |
969
|
|
|
power_equation, |
970
|
|
|
description, |
971
|
|
|
tid)) |
972
|
|
|
cnx.commit() |
973
|
|
|
|
974
|
|
|
cursor.close() |
975
|
|
|
cnx.close() |
976
|
|
|
|
977
|
|
|
resp.status = falcon.HTTP_200 |
978
|
|
|
|