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