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 config |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class EquipmentCollection: |
11
|
|
|
def __init__(self): |
12
|
|
|
"""Initializes EquipmentCollection""" |
13
|
|
|
pass |
14
|
|
|
|
15
|
|
|
@staticmethod |
16
|
|
|
def on_options(req, resp): |
17
|
|
|
_ = req |
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 " |
32
|
|
|
" FROM tbl_cost_centers ") |
33
|
|
|
cursor.execute(query) |
34
|
|
|
rows_cost_centers = cursor.fetchall() |
35
|
|
|
|
36
|
|
|
cost_center_dict = dict() |
37
|
|
|
if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
38
|
|
|
for row in rows_cost_centers: |
39
|
|
|
cost_center_dict[row[0]] = {"id": row[0], |
40
|
|
|
"name": row[1], |
41
|
|
|
"uuid": row[2]} |
42
|
|
|
|
43
|
|
|
query_svg = (" SELECT id, name, uuid " |
44
|
|
|
" FROM tbl_svgs ") |
45
|
|
|
cursor.execute(query_svg) |
46
|
|
|
rows_svgs = cursor.fetchall() |
47
|
|
|
|
48
|
|
|
svg_dict = dict() |
49
|
|
|
if rows_svgs is not None and len(rows_svgs) > 0: |
50
|
|
|
for row in rows_svgs: |
51
|
|
|
svg_dict[row[0]] = {"id": row[0], |
52
|
|
|
"name": row[1], |
53
|
|
|
"uuid": row[2]} |
54
|
|
|
|
55
|
|
|
search_query = req.get_param('q') |
56
|
|
|
query_base = (" SELECT id, name, uuid, " |
57
|
|
|
" is_input_counted, is_output_counted, " |
58
|
|
|
" cost_center_id, svg_id, camera_url, description " |
59
|
|
|
" FROM tbl_equipments ") |
60
|
|
|
params = [] |
61
|
|
|
|
62
|
|
|
if search_query and isinstance(search_query, str) and len(str.strip(search_query)) > 0: |
63
|
|
|
query_base += " WHERE name LIKE %s OR description LIKE %s " |
64
|
|
|
trimmed_query = str.strip(search_query) |
65
|
|
|
params = [f'%{trimmed_query}%', f'%{trimmed_query}%'] |
66
|
|
|
|
67
|
|
|
query_base += " ORDER BY id " |
68
|
|
|
cursor.execute(query_base, params) |
69
|
|
|
rows_equipments = cursor.fetchall() |
70
|
|
|
|
71
|
|
|
result = list() |
72
|
|
|
if rows_equipments is not None and len(rows_equipments) > 0: |
73
|
|
|
for row in rows_equipments: |
74
|
|
|
meta_result = {"id": row[0], |
75
|
|
|
"name": row[1], |
76
|
|
|
"uuid": row[2], |
77
|
|
|
"is_input_counted": bool(row[3]), |
78
|
|
|
"is_output_counted": bool(row[4]), |
79
|
|
|
"cost_center": cost_center_dict.get(row[5], None), |
80
|
|
|
"svg": svg_dict.get(row[6], None), |
81
|
|
|
"camera_url": row[7], |
82
|
|
|
"description": row[8], |
83
|
|
|
"qrcode": 'equipment:' + row[2]} |
84
|
|
|
result.append(meta_result) |
85
|
|
|
|
86
|
|
|
cursor.close() |
87
|
|
|
cnx.close() |
88
|
|
|
resp.text = json.dumps(result) |
89
|
|
|
|
90
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
91
|
|
|
@user_logger |
92
|
|
|
def on_post(req, resp): |
93
|
|
|
"""Handles POST requests""" |
94
|
|
|
admin_control(req) |
95
|
|
|
try: |
96
|
|
|
raw_json = req.stream.read().decode('utf-8') |
97
|
|
|
except Exception as ex: |
98
|
|
|
print(str(ex)) |
99
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
100
|
|
|
title='API.BAD_REQUEST', |
101
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
102
|
|
|
|
103
|
|
|
new_values = json.loads(raw_json) |
104
|
|
|
|
105
|
|
|
if 'name' not in new_values['data'].keys() or \ |
106
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
107
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
108
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
109
|
|
|
description='API.INVALID_EQUIPMENT_NAME') |
110
|
|
|
name = str.strip(new_values['data']['name']) |
111
|
|
|
|
112
|
|
|
if 'is_input_counted' not in new_values['data'].keys() or \ |
113
|
|
|
not isinstance(new_values['data']['is_input_counted'], bool): |
114
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
115
|
|
|
description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
116
|
|
|
is_input_counted = new_values['data']['is_input_counted'] |
117
|
|
|
|
118
|
|
|
if 'is_output_counted' not in new_values['data'].keys() or \ |
119
|
|
|
not isinstance(new_values['data']['is_output_counted'], bool): |
120
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
121
|
|
|
description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
122
|
|
|
is_output_counted = new_values['data']['is_output_counted'] |
123
|
|
|
|
124
|
|
|
if 'cost_center_id' not in new_values['data'].keys() or \ |
125
|
|
|
not isinstance(new_values['data']['cost_center_id'], int) or \ |
126
|
|
|
new_values['data']['cost_center_id'] <= 0: |
127
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
128
|
|
|
description='API.INVALID_COST_CENTER_ID') |
129
|
|
|
cost_center_id = new_values['data']['cost_center_id'] |
130
|
|
|
|
131
|
|
|
if 'svg_id' in new_values['data'].keys() and \ |
132
|
|
|
isinstance(new_values['data']['svg_id'], int) and \ |
133
|
|
|
new_values['data']['svg_id'] > 0: |
134
|
|
|
svg_id = new_values['data']['svg_id'] |
135
|
|
|
else: |
136
|
|
|
svg_id = None |
137
|
|
|
|
138
|
|
|
if 'camera_url' in new_values['data'].keys() and \ |
139
|
|
|
new_values['data']['camera_url'] is not None and \ |
140
|
|
|
len(str(new_values['data']['camera_url'])) > 0: |
141
|
|
|
camera_url = str.strip(new_values['data']['camera_url']) |
142
|
|
|
else: |
143
|
|
|
camera_url = None |
144
|
|
|
|
145
|
|
|
if 'description' in new_values['data'].keys() and \ |
146
|
|
|
new_values['data']['description'] is not None and \ |
147
|
|
|
len(str(new_values['data']['description'])) > 0: |
148
|
|
|
description = str.strip(new_values['data']['description']) |
149
|
|
|
else: |
150
|
|
|
description = None |
151
|
|
|
|
152
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
153
|
|
|
cursor = cnx.cursor() |
154
|
|
|
|
155
|
|
|
cursor.execute(" SELECT name " |
156
|
|
|
" FROM tbl_equipments " |
157
|
|
|
" WHERE name = %s ", (name,)) |
158
|
|
|
if cursor.fetchone() is not None: |
159
|
|
|
cursor.close() |
160
|
|
|
cnx.close() |
161
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
162
|
|
|
description='API.EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
163
|
|
|
|
164
|
|
|
if cost_center_id is not None: |
165
|
|
|
cursor.execute(" SELECT name " |
166
|
|
|
" FROM tbl_cost_centers " |
167
|
|
|
" WHERE id = %s ", |
168
|
|
|
(new_values['data']['cost_center_id'],)) |
169
|
|
|
row = cursor.fetchone() |
170
|
|
|
if row is None: |
171
|
|
|
cursor.close() |
172
|
|
|
cnx.close() |
173
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
174
|
|
|
description='API.COST_CENTER_NOT_FOUND') |
175
|
|
|
|
176
|
|
|
if svg_id is not None: |
177
|
|
|
cursor.execute(" SELECT name " |
178
|
|
|
" FROM tbl_svgs " |
179
|
|
|
" WHERE id = %s ", |
180
|
|
|
(svg_id,)) |
181
|
|
|
row = cursor.fetchone() |
182
|
|
|
if row is None: |
183
|
|
|
cursor.close() |
184
|
|
|
cnx.close() |
185
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
186
|
|
|
description='API.SVG_NOT_FOUND') |
187
|
|
|
|
188
|
|
|
add_values = (" INSERT INTO tbl_equipments " |
189
|
|
|
" (name, uuid, is_input_counted, is_output_counted, " |
190
|
|
|
" cost_center_id, svg_id, camera_url, description) " |
191
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
192
|
|
|
cursor.execute(add_values, (name, |
193
|
|
|
str(uuid.uuid4()), |
194
|
|
|
is_input_counted, |
195
|
|
|
is_output_counted, |
196
|
|
|
cost_center_id, |
197
|
|
|
svg_id, |
198
|
|
|
camera_url, |
199
|
|
|
description)) |
200
|
|
|
new_id = cursor.lastrowid |
201
|
|
|
cnx.commit() |
202
|
|
|
cursor.close() |
203
|
|
|
cnx.close() |
204
|
|
|
|
205
|
|
|
resp.status = falcon.HTTP_201 |
206
|
|
|
resp.location = '/equipments/' + str(new_id) |
207
|
|
|
|
208
|
|
|
|
209
|
|
|
class EquipmentItem: |
210
|
|
|
def __init__(self): |
211
|
|
|
"""Initializes EquipmentItem""" |
212
|
|
|
pass |
213
|
|
|
|
214
|
|
|
@staticmethod |
215
|
|
|
def on_options(req, resp, id_): |
216
|
|
|
_ = req |
217
|
|
|
resp.status = falcon.HTTP_200 |
218
|
|
|
_ = id_ |
219
|
|
|
|
220
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
221
|
|
|
def on_get(req, resp, id_): |
222
|
|
|
if 'API-KEY' not in req.headers or \ |
223
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
224
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
225
|
|
|
access_control(req) |
226
|
|
|
else: |
227
|
|
|
api_key_control(req) |
228
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
229
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
230
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
231
|
|
|
|
232
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
233
|
|
|
cursor = cnx.cursor() |
234
|
|
|
|
235
|
|
|
query = (" SELECT id, name, uuid " |
236
|
|
|
" FROM tbl_cost_centers ") |
237
|
|
|
cursor.execute(query) |
238
|
|
|
rows_cost_centers = cursor.fetchall() |
239
|
|
|
|
240
|
|
|
cost_center_dict = dict() |
241
|
|
|
if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
242
|
|
|
for row in rows_cost_centers: |
243
|
|
|
cost_center_dict[row[0]] = {"id": row[0], |
244
|
|
|
"name": row[1], |
245
|
|
|
"uuid": row[2]} |
246
|
|
|
|
247
|
|
|
svg_dict = dict() |
248
|
|
|
query = (" SELECT id, name, uuid " |
249
|
|
|
" FROM tbl_svgs ") |
250
|
|
|
cursor.execute(query) |
251
|
|
|
rows_svgs = cursor.fetchall() |
252
|
|
|
if rows_svgs is not None and len(rows_svgs) > 0: |
253
|
|
|
for row in rows_svgs: |
254
|
|
|
svg_dict[row[0]] = {"id": row[0], |
255
|
|
|
"name": row[1], |
256
|
|
|
"uuid": row[2]} |
257
|
|
|
|
258
|
|
|
query = (" SELECT id, name, uuid, " |
259
|
|
|
" is_input_counted, is_output_counted, " |
260
|
|
|
" cost_center_id, svg_id, camera_url, description " |
261
|
|
|
" FROM tbl_equipments " |
262
|
|
|
" WHERE id = %s ") |
263
|
|
|
cursor.execute(query, (id_,)) |
264
|
|
|
row = cursor.fetchone() |
265
|
|
|
cursor.close() |
266
|
|
|
cnx.close() |
267
|
|
|
|
268
|
|
|
if row is None: |
269
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
270
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
271
|
|
|
else: |
272
|
|
|
meta_result = {"id": row[0], |
273
|
|
|
"name": row[1], |
274
|
|
|
"uuid": row[2], |
275
|
|
|
"is_input_counted": bool(row[3]), |
276
|
|
|
"is_output_counted": bool(row[4]), |
277
|
|
|
"cost_center": cost_center_dict.get(row[5], None), |
278
|
|
|
"svg": svg_dict.get(row[6], None), |
279
|
|
|
"camera_url": row[7], |
280
|
|
|
"description": row[8], |
281
|
|
|
"qrcode": 'equipment:' + row[2]} |
282
|
|
|
|
283
|
|
|
resp.text = json.dumps(meta_result) |
284
|
|
|
|
285
|
|
|
@staticmethod |
286
|
|
|
@user_logger |
287
|
|
|
@user_logger |
288
|
|
|
def on_delete(req, resp, id_): |
289
|
|
|
admin_control(req) |
290
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
291
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
292
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
293
|
|
|
|
294
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
295
|
|
|
cursor = cnx.cursor() |
296
|
|
|
|
297
|
|
|
# check relation with space |
298
|
|
|
cursor.execute(" SELECT space_id " |
299
|
|
|
" FROM tbl_spaces_equipments " |
300
|
|
|
" WHERE equipment_id = %s ", |
301
|
|
|
(id_,)) |
302
|
|
|
rows_equipments = cursor.fetchall() |
303
|
|
|
if rows_equipments is not None and len(rows_equipments) > 0: |
304
|
|
|
cursor.close() |
305
|
|
|
cnx.close() |
306
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
307
|
|
|
title='API.BAD_REQUEST', |
308
|
|
|
description='API.THERE_IS_RELATION_WITH_SPACES') |
309
|
|
|
|
310
|
|
|
# check relation with combined equipments |
311
|
|
|
cursor.execute(" SELECT combined_equipment_id " |
312
|
|
|
" FROM tbl_combined_equipments_equipments " |
313
|
|
|
" WHERE equipment_id = %s ", |
314
|
|
|
(id_,)) |
315
|
|
|
rows_combined_equipments = cursor.fetchall() |
316
|
|
|
if rows_combined_equipments is not None and len(rows_combined_equipments) > 0: |
317
|
|
|
cursor.close() |
318
|
|
|
cnx.close() |
319
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
320
|
|
|
title='API.BAD_REQUEST', |
321
|
|
|
description='API.THERE_IS_RELATION_WITH_COMBINED_EQUIPMENTS') |
322
|
|
|
|
323
|
|
|
# check relation with shopfloor |
324
|
|
|
cursor.execute(" SELECT shopfloor_id " |
325
|
|
|
" FROM tbl_shopfloors_equipments " |
326
|
|
|
" WHERE equipment_id = %s ", |
327
|
|
|
(id_,)) |
328
|
|
|
rows_combined_shopfloor = cursor.fetchall() |
329
|
|
|
if rows_combined_shopfloor is not None and len(rows_combined_shopfloor) > 0: |
330
|
|
|
cursor.close() |
331
|
|
|
cnx.close() |
332
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
333
|
|
|
title='API.BAD_REQUEST', |
334
|
|
|
description='API.THERE_IS_RELATION_WITH_SHOPFLOORS') |
335
|
|
|
|
336
|
|
|
# delete relation with meter |
337
|
|
|
cursor.execute(" DELETE FROM tbl_equipments_meters WHERE equipment_id = %s ", (id_,)) |
338
|
|
|
|
339
|
|
|
# delete relation with offline meter |
340
|
|
|
cursor.execute(" DELETE FROM tbl_equipments_offline_meters WHERE equipment_id = %s ", (id_,)) |
341
|
|
|
|
342
|
|
|
# delete relation with virtual meter |
343
|
|
|
cursor.execute(" DELETE FROM tbl_equipments_virtual_meters WHERE equipment_id = %s ", (id_,)) |
344
|
|
|
|
345
|
|
|
# delete relation with commands |
346
|
|
|
cursor.execute(" DELETE FROM tbl_equipments_commands WHERE equipment_id = %s ", (id_,)) |
347
|
|
|
|
348
|
|
|
# delete all associated parameters |
349
|
|
|
cursor.execute(" DELETE FROM tbl_equipments_parameters WHERE equipment_id = %s ", (id_,)) |
350
|
|
|
cnx.commit() |
351
|
|
|
|
352
|
|
|
cursor.execute(" DELETE FROM tbl_equipments WHERE id = %s ", (id_,)) |
353
|
|
|
cnx.commit() |
354
|
|
|
|
355
|
|
|
cursor.close() |
356
|
|
|
cnx.close() |
357
|
|
|
|
358
|
|
|
resp.status = falcon.HTTP_204 |
359
|
|
|
|
360
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
361
|
|
|
@user_logger |
362
|
|
|
def on_put(req, resp, id_): |
363
|
|
|
"""Handles PUT requests""" |
364
|
|
|
admin_control(req) |
365
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
366
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
367
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
368
|
|
|
try: |
369
|
|
|
raw_json = req.stream.read().decode('utf-8') |
370
|
|
|
except Exception as ex: |
371
|
|
|
print(str(ex)) |
372
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
373
|
|
|
title='API.BAD_REQUEST', |
374
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
375
|
|
|
|
376
|
|
|
new_values = json.loads(raw_json) |
377
|
|
|
|
378
|
|
|
if 'name' not in new_values['data'].keys() or \ |
379
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
380
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
381
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
382
|
|
|
description='API.INVALID_EQUIPMENT_NAME') |
383
|
|
|
name = str.strip(new_values['data']['name']) |
384
|
|
|
|
385
|
|
|
if 'is_input_counted' not in new_values['data'].keys() or \ |
386
|
|
|
not isinstance(new_values['data']['is_input_counted'], bool): |
387
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
388
|
|
|
description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
389
|
|
|
is_input_counted = new_values['data']['is_input_counted'] |
390
|
|
|
|
391
|
|
|
if 'is_output_counted' not in new_values['data'].keys() or \ |
392
|
|
|
not isinstance(new_values['data']['is_output_counted'], bool): |
393
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
394
|
|
|
description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
395
|
|
|
is_output_counted = new_values['data']['is_output_counted'] |
396
|
|
|
|
397
|
|
|
if 'cost_center_id' not in new_values['data'].keys() or \ |
398
|
|
|
not isinstance(new_values['data']['cost_center_id'], int) or \ |
399
|
|
|
new_values['data']['cost_center_id'] <= 0: |
400
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
401
|
|
|
description='API.INVALID_COST_CENTER_ID') |
402
|
|
|
cost_center_id = new_values['data']['cost_center_id'] |
403
|
|
|
|
404
|
|
|
if 'svg_id' in new_values['data'].keys() and \ |
405
|
|
|
isinstance(new_values['data']['svg_id'], int) and \ |
406
|
|
|
new_values['data']['svg_id'] > 0: |
407
|
|
|
svg_id = new_values['data']['svg_id'] |
408
|
|
|
else: |
409
|
|
|
svg_id = None |
410
|
|
|
|
411
|
|
|
if 'description' in new_values['data'].keys() and \ |
412
|
|
|
new_values['data']['description'] is not None and \ |
413
|
|
|
len(str(new_values['data']['description'])) > 0: |
414
|
|
|
description = str.strip(new_values['data']['description']) |
415
|
|
|
else: |
416
|
|
|
description = None |
417
|
|
|
|
418
|
|
|
if 'camera_url' in new_values['data'].keys() and \ |
419
|
|
|
new_values['data']['camera_url'] is not None and \ |
420
|
|
|
len(str(new_values['data']['camera_url'])) > 0: |
421
|
|
|
camera_url = str.strip(new_values['data']['camera_url']) |
422
|
|
|
else: |
423
|
|
|
camera_url = None |
424
|
|
|
|
425
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
426
|
|
|
cursor = cnx.cursor() |
427
|
|
|
|
428
|
|
|
cursor.execute(" SELECT name " |
429
|
|
|
" FROM tbl_equipments " |
430
|
|
|
" WHERE id = %s ", (id_,)) |
431
|
|
|
if cursor.fetchone() is None: |
432
|
|
|
cursor.close() |
433
|
|
|
cnx.close() |
434
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
435
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
436
|
|
|
|
437
|
|
|
cursor.execute(" SELECT name " |
438
|
|
|
" FROM tbl_equipments " |
439
|
|
|
" WHERE name = %s AND id != %s ", (name, id_)) |
440
|
|
|
if cursor.fetchone() is not None: |
441
|
|
|
cursor.close() |
442
|
|
|
cnx.close() |
443
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
444
|
|
|
description='API.EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
445
|
|
|
|
446
|
|
|
cursor.execute(" SELECT name " |
447
|
|
|
" FROM tbl_cost_centers " |
448
|
|
|
" WHERE id = %s ", |
449
|
|
|
(new_values['data']['cost_center_id'],)) |
450
|
|
|
row = cursor.fetchone() |
451
|
|
|
if row is None: |
452
|
|
|
cursor.close() |
453
|
|
|
cnx.close() |
454
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
455
|
|
|
description='API.COST_CENTER_NOT_FOUND') |
456
|
|
|
|
457
|
|
|
if svg_id is not None: |
458
|
|
|
cursor.execute(" SELECT name " |
459
|
|
|
" FROM tbl_svgs " |
460
|
|
|
" WHERE id = %s ", |
461
|
|
|
(new_values['data']['svg_id'],)) |
462
|
|
|
row = cursor.fetchone() |
463
|
|
|
if row is None: |
464
|
|
|
cursor.close() |
465
|
|
|
cnx.close() |
466
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
467
|
|
|
description='API.SVG_NOT_FOUND') |
468
|
|
|
|
469
|
|
|
update_row = (" UPDATE tbl_equipments " |
470
|
|
|
" SET name = %s, is_input_counted = %s, is_output_counted = %s, " |
471
|
|
|
" cost_center_id = %s, svg_id = %s, camera_url = %s, description = %s " |
472
|
|
|
" WHERE id = %s ") |
473
|
|
|
cursor.execute(update_row, (name, |
474
|
|
|
is_input_counted, |
475
|
|
|
is_output_counted, |
476
|
|
|
cost_center_id, |
477
|
|
|
svg_id, |
478
|
|
|
camera_url, |
479
|
|
|
description, |
480
|
|
|
id_)) |
481
|
|
|
cnx.commit() |
482
|
|
|
|
483
|
|
|
cursor.close() |
484
|
|
|
cnx.close() |
485
|
|
|
|
486
|
|
|
resp.status = falcon.HTTP_200 |
487
|
|
|
|
488
|
|
|
# Clone an Equipment |
489
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
490
|
|
|
@user_logger |
491
|
|
|
def on_post(req, resp, id_): |
492
|
|
|
admin_control(req) |
493
|
|
|
"""Handles POST requests""" |
494
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
495
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
496
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
497
|
|
|
|
498
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
499
|
|
|
cursor = cnx.cursor() |
500
|
|
|
cursor.execute(" SELECT name " |
501
|
|
|
" FROM tbl_equipments " |
502
|
|
|
" WHERE id = %s ", (id_,)) |
503
|
|
|
if cursor.fetchone() is None: |
504
|
|
|
cursor.close() |
505
|
|
|
cnx.close() |
506
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
507
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
508
|
|
|
|
509
|
|
|
query = (" SELECT name, is_input_counted, is_output_counted, " |
510
|
|
|
" cost_center_id, svg_id, camera_url, description " |
511
|
|
|
" FROM tbl_equipments " |
512
|
|
|
" WHERE id = %s ") |
513
|
|
|
cursor.execute(query, (id_,)) |
514
|
|
|
row = cursor.fetchone() |
515
|
|
|
|
516
|
|
|
if row is None: |
517
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
518
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
519
|
|
|
else: |
520
|
|
|
add_values = (" INSERT INTO tbl_equipments " |
521
|
|
|
" (name, uuid, is_input_counted, is_output_counted, " |
522
|
|
|
" cost_center_id, svg_id, camera_url, description) " |
523
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
524
|
|
|
cursor.execute(add_values, (row[0] + ' Copy', |
525
|
|
|
str(uuid.uuid4()), |
526
|
|
|
row[1], |
527
|
|
|
row[2], |
528
|
|
|
row[3], |
529
|
|
|
row[4], |
530
|
|
|
row[5], |
531
|
|
|
row[6])) |
532
|
|
|
new_id = cursor.lastrowid |
533
|
|
|
cnx.commit() |
534
|
|
|
|
535
|
|
|
# clone relation with meter |
536
|
|
|
cursor.execute(" SELECT meter_id, is_output " |
537
|
|
|
" FROM tbl_equipments_meters " |
538
|
|
|
" WHERE equipment_id = %s ", |
539
|
|
|
(id_,)) |
540
|
|
|
rows_meters = cursor.fetchall() |
541
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
542
|
|
|
add_values = (" INSERT INTO tbl_equipments_meters (equipment_id, meter_id, is_output) " |
543
|
|
|
" VALUES ") |
544
|
|
|
for row in rows_meters: |
545
|
|
|
add_values += " (" + str(new_id) + "," |
546
|
|
|
add_values += str(row[0]) + "," |
547
|
|
|
add_values += str(bool(row[1])) + "), " |
548
|
|
|
# trim ", " at the end of string and then execute |
549
|
|
|
cursor.execute(add_values[:-2]) |
550
|
|
|
cnx.commit() |
551
|
|
|
|
552
|
|
|
# clone relation with offline meter |
553
|
|
|
cursor.execute(" SELECT offline_meter_id, is_output " |
554
|
|
|
" FROM tbl_equipments_offline_meters " |
555
|
|
|
" WHERE equipment_id = %s ", |
556
|
|
|
(id_,)) |
557
|
|
|
rows_offline_meters = cursor.fetchall() |
558
|
|
|
if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
559
|
|
|
add_values = (" INSERT INTO tbl_equipments_offline_meters (equipment_id, offline_meter_id, is_output) " |
560
|
|
|
" VALUES ") |
561
|
|
|
for row in rows_offline_meters: |
562
|
|
|
add_values += " (" + str(new_id) + "," |
563
|
|
|
add_values += "'" + str(row[0]) + "'," |
564
|
|
|
add_values += str(bool(row[1])) + "), " |
565
|
|
|
# trim ", " at the end of string and then execute |
566
|
|
|
cursor.execute(add_values[:-2]) |
567
|
|
|
cnx.commit() |
568
|
|
|
|
569
|
|
|
# clone relation with virtual meter |
570
|
|
|
cursor.execute(" SELECT virtual_meter_id, is_output " |
571
|
|
|
" FROM tbl_equipments_virtual_meters " |
572
|
|
|
" WHERE equipment_id = %s ", |
573
|
|
|
(id_,)) |
574
|
|
|
rows_virtual_meters = cursor.fetchall() |
575
|
|
|
if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
576
|
|
|
add_values = (" INSERT INTO tbl_equipments_virtual_meters (equipment_id, virtual_meter_id, is_output) " |
577
|
|
|
" VALUES ") |
578
|
|
|
for row in rows_virtual_meters: |
579
|
|
|
add_values += " (" + str(new_id) + "," |
580
|
|
|
add_values += str(row[0]) + "," |
581
|
|
|
add_values += str(bool(row[1])) + "), " |
582
|
|
|
# trim ", " at the end of string and then execute |
583
|
|
|
cursor.execute(add_values[:-2]) |
584
|
|
|
cnx.commit() |
585
|
|
|
|
586
|
|
|
# clone parameters |
587
|
|
|
cursor.execute(" SELECT name, parameter_type, constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
588
|
|
|
" FROM tbl_equipments_parameters " |
589
|
|
|
" WHERE equipment_id = %s ", |
590
|
|
|
(id_,)) |
591
|
|
|
rows_parameters = cursor.fetchall() |
592
|
|
|
if rows_parameters is not None and len(rows_parameters) > 0: |
593
|
|
|
add_values = (" INSERT INTO tbl_equipments_parameters" |
594
|
|
|
" (equipment_id, name, parameter_type, constant, point_id, " |
595
|
|
|
" numerator_meter_uuid, denominator_meter_uuid) " |
596
|
|
|
" VALUES ") |
597
|
|
|
for row in rows_parameters: |
598
|
|
|
add_values += " (" + str(new_id) + "," |
599
|
|
|
add_values += "'" + str(row[0]) + "'," |
600
|
|
|
add_values += "'" + str(row[1]) + "'," |
601
|
|
|
if row[2] is not None: |
602
|
|
|
add_values += "'" + str(row[2]) + "'," |
603
|
|
|
else: |
604
|
|
|
add_values += "null, " |
605
|
|
|
|
606
|
|
|
if row[3] is not None: |
607
|
|
|
add_values += str(row[3]) + "," |
608
|
|
|
else: |
609
|
|
|
add_values += "null, " |
610
|
|
|
|
611
|
|
|
if row[4] is not None: |
612
|
|
|
add_values += "'" + row[4] + "'," |
613
|
|
|
else: |
614
|
|
|
add_values += "null, " |
615
|
|
|
if row[5] is not None: |
616
|
|
|
add_values += "'" + row[5] + "'), " |
617
|
|
|
else: |
618
|
|
|
add_values += "null), " |
619
|
|
|
|
620
|
|
|
# trim ", " at the end of string and then execute |
621
|
|
|
cursor.execute(add_values[:-2]) |
622
|
|
|
cnx.commit() |
623
|
|
|
|
624
|
|
|
cursor.close() |
625
|
|
|
cnx.close() |
626
|
|
|
resp.status = falcon.HTTP_201 |
627
|
|
|
resp.location = '/equipments/' + str(new_id) |
628
|
|
|
|
629
|
|
|
|
630
|
|
View Code Duplication |
class EquipmentParameterCollection: |
|
|
|
|
631
|
|
|
def __init__(self): |
632
|
|
|
"""Initializes EquipmentParameterCollection""" |
633
|
|
|
pass |
634
|
|
|
|
635
|
|
|
@staticmethod |
636
|
|
|
def on_options(req, resp, id_): |
637
|
|
|
_ = req |
638
|
|
|
resp.status = falcon.HTTP_200 |
639
|
|
|
_ = id_ |
640
|
|
|
|
641
|
|
|
@staticmethod |
642
|
|
|
def on_get(req, resp, id_): |
643
|
|
|
if 'API-KEY' not in req.headers or \ |
644
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
645
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
646
|
|
|
access_control(req) |
647
|
|
|
else: |
648
|
|
|
api_key_control(req) |
649
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
650
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
651
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
652
|
|
|
|
653
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
654
|
|
|
cursor = cnx.cursor() |
655
|
|
|
|
656
|
|
|
cursor.execute(" SELECT name " |
657
|
|
|
" FROM tbl_equipments " |
658
|
|
|
" WHERE id = %s ", (id_,)) |
659
|
|
|
if cursor.fetchone() is None: |
660
|
|
|
cursor.close() |
661
|
|
|
cnx.close() |
662
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
663
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
664
|
|
|
|
665
|
|
|
query = (" SELECT id, name " |
666
|
|
|
" FROM tbl_points ") |
667
|
|
|
cursor.execute(query) |
668
|
|
|
rows_points = cursor.fetchall() |
669
|
|
|
|
670
|
|
|
point_dict = dict() |
671
|
|
|
if rows_points is not None and len(rows_points) > 0: |
672
|
|
|
for row in rows_points: |
673
|
|
|
point_dict[row[0]] = {"id": row[0], |
674
|
|
|
"name": row[1]} |
675
|
|
|
|
676
|
|
|
query = (" SELECT id, name, uuid " |
677
|
|
|
" FROM tbl_meters ") |
678
|
|
|
cursor.execute(query) |
679
|
|
|
rows_meters = cursor.fetchall() |
680
|
|
|
|
681
|
|
|
meter_dict = dict() |
682
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
683
|
|
|
for row in rows_meters: |
684
|
|
|
meter_dict[row[2]] = {"type": 'meter', |
685
|
|
|
"id": row[0], |
686
|
|
|
"name": row[1], |
687
|
|
|
"uuid": row[2]} |
688
|
|
|
|
689
|
|
|
query = (" SELECT id, name, uuid " |
690
|
|
|
" FROM tbl_offline_meters ") |
691
|
|
|
cursor.execute(query) |
692
|
|
|
rows_offline_meters = cursor.fetchall() |
693
|
|
|
|
694
|
|
|
offline_meter_dict = dict() |
695
|
|
|
if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
696
|
|
|
for row in rows_offline_meters: |
697
|
|
|
offline_meter_dict[row[2]] = {"type": 'offline_meter', |
698
|
|
|
"id": row[0], |
699
|
|
|
"name": row[1], |
700
|
|
|
"uuid": row[2]} |
701
|
|
|
|
702
|
|
|
query = (" SELECT id, name, uuid " |
703
|
|
|
" FROM tbl_virtual_meters ") |
704
|
|
|
cursor.execute(query) |
705
|
|
|
rows_virtual_meters = cursor.fetchall() |
706
|
|
|
|
707
|
|
|
virtual_meter_dict = dict() |
708
|
|
|
if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
709
|
|
|
for row in rows_virtual_meters: |
710
|
|
|
virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
711
|
|
|
"id": row[0], |
712
|
|
|
"name": row[1], |
713
|
|
|
"uuid": row[2]} |
714
|
|
|
|
715
|
|
|
query = (" SELECT id, name, parameter_type, " |
716
|
|
|
" constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
717
|
|
|
" FROM tbl_equipments_parameters " |
718
|
|
|
" WHERE equipment_id = %s " |
719
|
|
|
" ORDER BY id ") |
720
|
|
|
cursor.execute(query, (id_, )) |
721
|
|
|
rows_parameters = cursor.fetchall() |
722
|
|
|
|
723
|
|
|
result = list() |
724
|
|
|
if rows_parameters is not None and len(rows_parameters) > 0: |
725
|
|
|
for row in rows_parameters: |
726
|
|
|
constant = None |
727
|
|
|
point = None |
728
|
|
|
numerator_meter = None |
729
|
|
|
denominator_meter = None |
730
|
|
|
if row[2] == 'point': |
731
|
|
|
point = point_dict.get(row[4], None) |
732
|
|
|
constant = None |
733
|
|
|
numerator_meter = None |
734
|
|
|
denominator_meter = None |
735
|
|
|
elif row[2] == 'constant': |
736
|
|
|
constant = row[3] |
737
|
|
|
point = None |
738
|
|
|
numerator_meter = None |
739
|
|
|
denominator_meter = None |
740
|
|
|
elif row[2] == 'fraction': |
741
|
|
|
constant = None |
742
|
|
|
point = None |
743
|
|
|
# find numerator meter by uuid |
744
|
|
|
numerator_meter = meter_dict.get(row[5], None) |
745
|
|
|
if numerator_meter is None: |
746
|
|
|
numerator_meter = virtual_meter_dict.get(row[5], None) |
747
|
|
|
if numerator_meter is None: |
748
|
|
|
numerator_meter = offline_meter_dict.get(row[5], None) |
749
|
|
|
# find denominator meter by uuid |
750
|
|
|
denominator_meter = meter_dict.get(row[6], None) |
751
|
|
|
if denominator_meter is None: |
752
|
|
|
denominator_meter = virtual_meter_dict.get(row[6], None) |
753
|
|
|
if denominator_meter is None: |
754
|
|
|
denominator_meter = offline_meter_dict.get(row[6], None) |
755
|
|
|
|
756
|
|
|
meta_result = {"id": row[0], |
757
|
|
|
"name": row[1], |
758
|
|
|
"parameter_type": row[2], |
759
|
|
|
"constant": constant, |
760
|
|
|
"point": point, |
761
|
|
|
"numerator_meter": numerator_meter, |
762
|
|
|
"denominator_meter": denominator_meter} |
763
|
|
|
result.append(meta_result) |
764
|
|
|
|
765
|
|
|
cursor.close() |
766
|
|
|
cnx.close() |
767
|
|
|
resp.text = json.dumps(result) |
768
|
|
|
|
769
|
|
|
@staticmethod |
770
|
|
|
@user_logger |
771
|
|
|
def on_post(req, resp, id_): |
772
|
|
|
"""Handles POST requests""" |
773
|
|
|
admin_control(req) |
774
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
775
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
776
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
777
|
|
|
try: |
778
|
|
|
raw_json = req.stream.read().decode('utf-8') |
779
|
|
|
except Exception as ex: |
780
|
|
|
print(str(ex)) |
781
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
782
|
|
|
title='API.BAD_REQUEST', |
783
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
784
|
|
|
|
785
|
|
|
new_values = json.loads(raw_json) |
786
|
|
|
|
787
|
|
|
if 'name' not in new_values['data'].keys() or \ |
788
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
789
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
790
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
791
|
|
|
description='API.INVALID_EQUIPMENT_PARAMETER_NAME') |
792
|
|
|
name = str.strip(new_values['data']['name']) |
793
|
|
|
|
794
|
|
|
if 'parameter_type' not in new_values['data'].keys() or \ |
795
|
|
|
not isinstance(new_values['data']['parameter_type'], str) or \ |
796
|
|
|
len(str.strip(new_values['data']['parameter_type'])) == 0: |
797
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
798
|
|
|
description='API.INVALID_EQUIPMENT_PARAMETER_TYPE') |
799
|
|
|
|
800
|
|
|
parameter_type = str.strip(new_values['data']['parameter_type']) |
801
|
|
|
|
802
|
|
|
if parameter_type not in ('constant', 'point', 'fraction'): |
803
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
804
|
|
|
description='API.INVALID_EQUIPMENT_PARAMETER_TYPE') |
805
|
|
|
|
806
|
|
|
constant = None |
807
|
|
|
if 'constant' in new_values['data'].keys(): |
808
|
|
|
if new_values['data']['constant'] is not None and \ |
809
|
|
|
isinstance(new_values['data']['constant'], str) and \ |
810
|
|
|
len(str.strip(new_values['data']['constant'])) > 0: |
811
|
|
|
constant = str.strip(new_values['data']['constant']) |
812
|
|
|
|
813
|
|
|
point_id = None |
814
|
|
|
if 'point_id' in new_values['data'].keys(): |
815
|
|
|
if new_values['data']['point_id'] is not None and \ |
816
|
|
|
new_values['data']['point_id'] <= 0: |
817
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
818
|
|
|
description='API.INVALID_POINT_ID') |
819
|
|
|
point_id = new_values['data']['point_id'] |
820
|
|
|
|
821
|
|
|
numerator_meter_uuid = None |
822
|
|
|
if 'numerator_meter_uuid' in new_values['data'].keys(): |
823
|
|
|
if new_values['data']['numerator_meter_uuid'] is not None and \ |
824
|
|
|
isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
825
|
|
|
len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
826
|
|
|
numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
827
|
|
|
|
828
|
|
|
denominator_meter_uuid = None |
829
|
|
|
if 'denominator_meter_uuid' in new_values['data'].keys(): |
830
|
|
|
if new_values['data']['denominator_meter_uuid'] is not None and \ |
831
|
|
|
isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
832
|
|
|
len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
833
|
|
|
denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
834
|
|
|
|
835
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
836
|
|
|
cursor = cnx.cursor() |
837
|
|
|
cursor.execute(" SELECT name " |
838
|
|
|
" FROM tbl_equipments " |
839
|
|
|
" WHERE id = %s ", (id_,)) |
840
|
|
|
if cursor.fetchone() is None: |
841
|
|
|
cursor.close() |
842
|
|
|
cnx.close() |
843
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
844
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
845
|
|
|
|
846
|
|
|
cursor.execute(" SELECT name " |
847
|
|
|
" FROM tbl_equipments_parameters " |
848
|
|
|
" WHERE name = %s AND equipment_id = %s ", (name, id_)) |
849
|
|
|
if cursor.fetchone() is not None: |
850
|
|
|
cursor.close() |
851
|
|
|
cnx.close() |
852
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
853
|
|
|
description='API.EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
854
|
|
|
|
855
|
|
|
# validate by parameter type |
856
|
|
|
if parameter_type == 'point': |
857
|
|
|
if point_id is None: |
858
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
859
|
|
|
description='API.INVALID_POINT_ID') |
860
|
|
|
query = (" SELECT id, name " |
861
|
|
|
" FROM tbl_points " |
862
|
|
|
" WHERE id = %s ") |
863
|
|
|
cursor.execute(query, (point_id, )) |
864
|
|
|
if cursor.fetchone() is None: |
865
|
|
|
cursor.close() |
866
|
|
|
cnx.close() |
867
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
868
|
|
|
description='API.POINT_NOT_FOUND') |
869
|
|
|
|
870
|
|
|
elif parameter_type == 'constant': |
871
|
|
|
if constant is None: |
872
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
873
|
|
|
description='API.INVALID_CONSTANT_VALUE') |
874
|
|
|
|
875
|
|
|
elif parameter_type == 'fraction': |
876
|
|
|
query = (" SELECT id, name, uuid " |
877
|
|
|
" FROM tbl_meters ") |
878
|
|
|
cursor.execute(query) |
879
|
|
|
rows_meters = cursor.fetchall() |
880
|
|
|
meter_dict = dict() |
881
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
882
|
|
|
for row in rows_meters: |
883
|
|
|
meter_dict[row[2]] = {"type": 'meter', |
884
|
|
|
"id": row[0], |
885
|
|
|
"name": row[1], |
886
|
|
|
"uuid": row[2]} |
887
|
|
|
|
888
|
|
|
query = (" SELECT id, name, uuid " |
889
|
|
|
" FROM tbl_offline_meters ") |
890
|
|
|
cursor.execute(query) |
891
|
|
|
rows_offline_meters = cursor.fetchall() |
892
|
|
|
|
893
|
|
|
offline_meter_dict = dict() |
894
|
|
|
if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
895
|
|
|
for row in rows_offline_meters: |
896
|
|
|
offline_meter_dict[row[2]] = {"type": 'offline_meter', |
897
|
|
|
"id": row[0], |
898
|
|
|
"name": row[1], |
899
|
|
|
"uuid": row[2]} |
900
|
|
|
|
901
|
|
|
query = (" SELECT id, name, uuid " |
902
|
|
|
" FROM tbl_virtual_meters ") |
903
|
|
|
cursor.execute(query) |
904
|
|
|
rows_virtual_meters = cursor.fetchall() |
905
|
|
|
|
906
|
|
|
virtual_meter_dict = dict() |
907
|
|
|
if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
908
|
|
|
for row in rows_virtual_meters: |
909
|
|
|
virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
910
|
|
|
"id": row[0], |
911
|
|
|
"name": row[1], |
912
|
|
|
"uuid": row[2]} |
913
|
|
|
|
914
|
|
|
# validate numerator meter uuid |
915
|
|
|
if meter_dict.get(numerator_meter_uuid) is None and \ |
916
|
|
|
virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
917
|
|
|
offline_meter_dict.get(numerator_meter_uuid) is None: |
918
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
919
|
|
|
description='API.INVALID_NUMERATOR_METER_UUID') |
920
|
|
|
|
921
|
|
|
# validate denominator meter uuid |
922
|
|
|
if denominator_meter_uuid == numerator_meter_uuid: |
923
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
924
|
|
|
description='API.INVALID_DENOMINATOR_METER_UUID') |
925
|
|
|
|
926
|
|
|
if denominator_meter_uuid not in meter_dict and \ |
927
|
|
|
denominator_meter_uuid not in virtual_meter_dict and \ |
928
|
|
|
denominator_meter_uuid not in offline_meter_dict: |
929
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
930
|
|
|
description='API.INVALID_DENOMINATOR_METER_UUID') |
931
|
|
|
|
932
|
|
|
add_values = (" INSERT INTO tbl_equipments_parameters " |
933
|
|
|
" (equipment_id, name, parameter_type, constant, " |
934
|
|
|
" point_id, numerator_meter_uuid, denominator_meter_uuid) " |
935
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
936
|
|
|
cursor.execute(add_values, (id_, |
937
|
|
|
name, |
938
|
|
|
parameter_type, |
939
|
|
|
constant, |
940
|
|
|
point_id, |
941
|
|
|
numerator_meter_uuid, |
942
|
|
|
denominator_meter_uuid)) |
943
|
|
|
new_id = cursor.lastrowid |
944
|
|
|
cnx.commit() |
945
|
|
|
cursor.close() |
946
|
|
|
cnx.close() |
947
|
|
|
|
948
|
|
|
resp.status = falcon.HTTP_201 |
949
|
|
|
resp.location = '/equipments/' + str(id_) + 'parameters/' + str(new_id) |
950
|
|
|
|
951
|
|
|
|
952
|
|
View Code Duplication |
class EquipmentParameterItem: |
|
|
|
|
953
|
|
|
@staticmethod |
954
|
|
|
@user_logger |
955
|
|
|
def __init__(): |
956
|
|
|
"""Initializes EquipmentParameterItem""" |
957
|
|
|
pass |
958
|
|
|
|
959
|
|
|
@staticmethod |
960
|
|
|
def on_options(req, resp, id_, pid): |
961
|
|
|
_ = req |
962
|
|
|
resp.status = falcon.HTTP_200 |
963
|
|
|
_ = id_ |
964
|
|
|
_ = pid |
965
|
|
|
|
966
|
|
|
@staticmethod |
967
|
|
|
def on_get(req, resp, id_, pid): |
968
|
|
|
if 'API-KEY' not in req.headers or \ |
969
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
970
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
971
|
|
|
access_control(req) |
972
|
|
|
else: |
973
|
|
|
api_key_control(req) |
974
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
975
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
976
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
977
|
|
|
|
978
|
|
|
if not pid.isdigit() or int(pid) <= 0: |
979
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
980
|
|
|
description='API.INVALID_EQUIPMENT_PARAMETER_ID') |
981
|
|
|
|
982
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
983
|
|
|
cursor = cnx.cursor() |
984
|
|
|
|
985
|
|
|
query = (" SELECT id, name " |
986
|
|
|
" FROM tbl_points ") |
987
|
|
|
cursor.execute(query) |
988
|
|
|
rows_points = cursor.fetchall() |
989
|
|
|
|
990
|
|
|
point_dict = dict() |
991
|
|
|
if rows_points is not None and len(rows_points) > 0: |
992
|
|
|
for row in rows_points: |
993
|
|
|
point_dict[row[0]] = {"id": row[0], |
994
|
|
|
"name": row[1]} |
995
|
|
|
|
996
|
|
|
query = (" SELECT id, name, uuid " |
997
|
|
|
" FROM tbl_meters ") |
998
|
|
|
cursor.execute(query) |
999
|
|
|
rows_meters = cursor.fetchall() |
1000
|
|
|
|
1001
|
|
|
meter_dict = dict() |
1002
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
1003
|
|
|
for row in rows_meters: |
1004
|
|
|
meter_dict[row[2]] = {"type": 'meter', |
1005
|
|
|
"id": row[0], |
1006
|
|
|
"name": row[1], |
1007
|
|
|
"uuid": row[2]} |
1008
|
|
|
|
1009
|
|
|
query = (" SELECT id, name, uuid " |
1010
|
|
|
" FROM tbl_offline_meters ") |
1011
|
|
|
cursor.execute(query) |
1012
|
|
|
rows_offline_meters = cursor.fetchall() |
1013
|
|
|
|
1014
|
|
|
offline_meter_dict = dict() |
1015
|
|
|
if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
1016
|
|
|
for row in rows_offline_meters: |
1017
|
|
|
offline_meter_dict[row[2]] = {"type": 'offline_meter', |
1018
|
|
|
"id": row[0], |
1019
|
|
|
"name": row[1], |
1020
|
|
|
"uuid": row[2]} |
1021
|
|
|
|
1022
|
|
|
query = (" SELECT id, name, uuid " |
1023
|
|
|
" FROM tbl_virtual_meters ") |
1024
|
|
|
cursor.execute(query) |
1025
|
|
|
rows_virtual_meters = cursor.fetchall() |
1026
|
|
|
|
1027
|
|
|
virtual_meter_dict = dict() |
1028
|
|
|
if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
1029
|
|
|
for row in rows_virtual_meters: |
1030
|
|
|
virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
1031
|
|
|
"id": row[0], |
1032
|
|
|
"name": row[1], |
1033
|
|
|
"uuid": row[2]} |
1034
|
|
|
|
1035
|
|
|
query = (" SELECT id, name, parameter_type, " |
1036
|
|
|
" constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
1037
|
|
|
" FROM tbl_equipments_parameters " |
1038
|
|
|
" WHERE equipment_id = %s AND id = %s ") |
1039
|
|
|
cursor.execute(query, (id_, pid)) |
1040
|
|
|
row = cursor.fetchone() |
1041
|
|
|
cursor.close() |
1042
|
|
|
cnx.close() |
1043
|
|
|
|
1044
|
|
|
if row is None: |
1045
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1046
|
|
|
description='API.EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
1047
|
|
|
else: |
1048
|
|
|
constant = None |
1049
|
|
|
point = None |
1050
|
|
|
numerator_meter = None |
1051
|
|
|
denominator_meter = None |
1052
|
|
|
if row[2] == 'point': |
1053
|
|
|
point = point_dict.get(row[4], None) |
1054
|
|
|
constant = None |
1055
|
|
|
numerator_meter = None |
1056
|
|
|
denominator_meter = None |
1057
|
|
|
elif row[2] == 'constant': |
1058
|
|
|
constant = row[3] |
1059
|
|
|
point = None |
1060
|
|
|
numerator_meter = None |
1061
|
|
|
denominator_meter = None |
1062
|
|
|
elif row[2] == 'fraction': |
1063
|
|
|
constant = None |
1064
|
|
|
point = None |
1065
|
|
|
# find numerator meter by uuid |
1066
|
|
|
numerator_meter = meter_dict.get(row[5], None) |
1067
|
|
|
if numerator_meter is None: |
1068
|
|
|
numerator_meter = virtual_meter_dict.get(row[5], None) |
1069
|
|
|
if numerator_meter is None: |
1070
|
|
|
numerator_meter = offline_meter_dict.get(row[5], None) |
1071
|
|
|
# find denominator meter by uuid |
1072
|
|
|
denominator_meter = meter_dict.get(row[6], None) |
1073
|
|
|
if denominator_meter is None: |
1074
|
|
|
denominator_meter = virtual_meter_dict.get(row[6], None) |
1075
|
|
|
if denominator_meter is None: |
1076
|
|
|
denominator_meter = offline_meter_dict.get(row[6], None) |
1077
|
|
|
|
1078
|
|
|
meta_result = {"id": row[0], |
1079
|
|
|
"name": row[1], |
1080
|
|
|
"parameter_type": row[2], |
1081
|
|
|
"constant": constant, |
1082
|
|
|
"point": point, |
1083
|
|
|
"numerator_meter": numerator_meter, |
1084
|
|
|
"denominator_meter": denominator_meter} |
1085
|
|
|
|
1086
|
|
|
resp.text = json.dumps(meta_result) |
1087
|
|
|
|
1088
|
|
|
@staticmethod |
1089
|
|
|
@user_logger |
1090
|
|
|
def on_delete(req, resp, id_, pid): |
1091
|
|
|
admin_control(req) |
1092
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1093
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1094
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1095
|
|
|
|
1096
|
|
|
if not pid.isdigit() or int(pid) <= 0: |
1097
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1098
|
|
|
description='API.INVALID_EQUIPMENT_PARAMETER_ID') |
1099
|
|
|
|
1100
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1101
|
|
|
cursor = cnx.cursor() |
1102
|
|
|
|
1103
|
|
|
cursor.execute(" SELECT name " |
1104
|
|
|
" FROM tbl_equipments " |
1105
|
|
|
" WHERE id = %s ", |
1106
|
|
|
(id_,)) |
1107
|
|
|
row = cursor.fetchone() |
1108
|
|
|
if row is None: |
1109
|
|
|
cursor.close() |
1110
|
|
|
cnx.close() |
1111
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
1112
|
|
|
title='API.NOT_FOUND', |
1113
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1114
|
|
|
|
1115
|
|
|
cursor.execute(" SELECT name " |
1116
|
|
|
" FROM tbl_equipments_parameters " |
1117
|
|
|
" WHERE equipment_id = %s AND id = %s ", |
1118
|
|
|
(id_, pid,)) |
1119
|
|
|
row = cursor.fetchone() |
1120
|
|
|
if row is None: |
1121
|
|
|
cursor.close() |
1122
|
|
|
cnx.close() |
1123
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
1124
|
|
|
title='API.NOT_FOUND', |
1125
|
|
|
description='API.EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
1126
|
|
|
|
1127
|
|
|
cursor.execute(" DELETE FROM tbl_equipments_parameters " |
1128
|
|
|
" WHERE id = %s ", (pid, )) |
1129
|
|
|
cnx.commit() |
1130
|
|
|
|
1131
|
|
|
cursor.close() |
1132
|
|
|
cnx.close() |
1133
|
|
|
|
1134
|
|
|
resp.status = falcon.HTTP_204 |
1135
|
|
|
|
1136
|
|
|
@staticmethod |
1137
|
|
|
@user_logger |
1138
|
|
|
def on_put(req, resp, id_, pid): |
1139
|
|
|
"""Handles PUT requests""" |
1140
|
|
|
admin_control(req) |
1141
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1142
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1143
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1144
|
|
|
|
1145
|
|
|
if not pid.isdigit() or int(pid) <= 0: |
1146
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1147
|
|
|
description='API.INVALID_EQUIPMENT_PARAMETER_ID') |
1148
|
|
|
|
1149
|
|
|
try: |
1150
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1151
|
|
|
except Exception as ex: |
1152
|
|
|
print(str(ex)) |
1153
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
1154
|
|
|
title='API.BAD_REQUEST', |
1155
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
1156
|
|
|
|
1157
|
|
|
new_values = json.loads(raw_json) |
1158
|
|
|
|
1159
|
|
|
if 'name' not in new_values['data'].keys() or \ |
1160
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
1161
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
1162
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1163
|
|
|
description='API.INVALID_EQUIPMENT_PARAMETER_NAME') |
1164
|
|
|
name = str.strip(new_values['data']['name']) |
1165
|
|
|
|
1166
|
|
|
if 'parameter_type' not in new_values['data'].keys() or \ |
1167
|
|
|
not isinstance(new_values['data']['parameter_type'], str) or \ |
1168
|
|
|
len(str.strip(new_values['data']['parameter_type'])) == 0: |
1169
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1170
|
|
|
description='API.INVALID_EQUIPMENT_PARAMETER_TYPE') |
1171
|
|
|
|
1172
|
|
|
parameter_type = str.strip(new_values['data']['parameter_type']) |
1173
|
|
|
|
1174
|
|
|
if parameter_type not in ('constant', 'point', 'fraction'): |
1175
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1176
|
|
|
description='API.INVALID_EQUIPMENT_PARAMETER_TYPE') |
1177
|
|
|
|
1178
|
|
|
constant = None |
1179
|
|
|
if 'constant' in new_values['data'].keys(): |
1180
|
|
|
if new_values['data']['constant'] is not None and \ |
1181
|
|
|
isinstance(new_values['data']['constant'], str) and \ |
1182
|
|
|
len(str.strip(new_values['data']['constant'])) > 0: |
1183
|
|
|
constant = str.strip(new_values['data']['constant']) |
1184
|
|
|
|
1185
|
|
|
point_id = None |
1186
|
|
|
if 'point_id' in new_values['data'].keys(): |
1187
|
|
|
if new_values['data']['point_id'] is not None and \ |
1188
|
|
|
new_values['data']['point_id'] <= 0: |
1189
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1190
|
|
|
description='API.INVALID_POINT_ID') |
1191
|
|
|
point_id = new_values['data']['point_id'] |
1192
|
|
|
|
1193
|
|
|
numerator_meter_uuid = None |
1194
|
|
|
if 'numerator_meter_uuid' in new_values['data'].keys(): |
1195
|
|
|
if new_values['data']['numerator_meter_uuid'] is not None and \ |
1196
|
|
|
isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
1197
|
|
|
len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
1198
|
|
|
numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
1199
|
|
|
|
1200
|
|
|
denominator_meter_uuid = None |
1201
|
|
|
if 'denominator_meter_uuid' in new_values['data'].keys(): |
1202
|
|
|
if new_values['data']['denominator_meter_uuid'] is not None and \ |
1203
|
|
|
isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
1204
|
|
|
len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
1205
|
|
|
denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
1206
|
|
|
|
1207
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1208
|
|
|
cursor = cnx.cursor() |
1209
|
|
|
|
1210
|
|
|
cursor.execute(" SELECT name " |
1211
|
|
|
" FROM tbl_equipments " |
1212
|
|
|
" WHERE id = %s ", (id_,)) |
1213
|
|
|
if cursor.fetchone() is None: |
1214
|
|
|
cursor.close() |
1215
|
|
|
cnx.close() |
1216
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
1217
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1218
|
|
|
|
1219
|
|
|
cursor.execute(" SELECT name " |
1220
|
|
|
" FROM tbl_equipments_parameters " |
1221
|
|
|
" WHERE equipment_id = %s AND id = %s ", |
1222
|
|
|
(id_, pid,)) |
1223
|
|
|
row = cursor.fetchone() |
1224
|
|
|
if row is None: |
1225
|
|
|
cursor.close() |
1226
|
|
|
cnx.close() |
1227
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
1228
|
|
|
title='API.NOT_FOUND', |
1229
|
|
|
description='API.EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
1230
|
|
|
|
1231
|
|
|
cursor.execute(" SELECT name " |
1232
|
|
|
" FROM tbl_equipments_parameters " |
1233
|
|
|
" WHERE name = %s AND equipment_id = %s AND id != %s ", (name, id_, pid)) |
1234
|
|
|
row = cursor.fetchone() |
1235
|
|
|
if row is not None: |
1236
|
|
|
cursor.close() |
1237
|
|
|
cnx.close() |
1238
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1239
|
|
|
description='API.EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
1240
|
|
|
|
1241
|
|
|
# validate by parameter type |
1242
|
|
|
if parameter_type == 'point': |
1243
|
|
|
if point_id is None: |
1244
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1245
|
|
|
description='API.INVALID_POINT_ID') |
1246
|
|
|
|
1247
|
|
|
query = (" SELECT id, name " |
1248
|
|
|
" FROM tbl_points " |
1249
|
|
|
" WHERE id = %s ") |
1250
|
|
|
cursor.execute(query, (point_id, )) |
1251
|
|
|
row = cursor.fetchone() |
1252
|
|
|
if row is None: |
1253
|
|
|
cursor.close() |
1254
|
|
|
cnx.close() |
1255
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1256
|
|
|
description='API.POINT_NOT_FOUND') |
1257
|
|
|
|
1258
|
|
|
elif parameter_type == 'constant': |
1259
|
|
|
if constant is None: |
1260
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1261
|
|
|
description='API.INVALID_CONSTANT_VALUE') |
1262
|
|
|
|
1263
|
|
|
elif parameter_type == 'fraction': |
1264
|
|
|
|
1265
|
|
|
query = (" SELECT id, name, uuid " |
1266
|
|
|
" FROM tbl_meters ") |
1267
|
|
|
cursor.execute(query) |
1268
|
|
|
rows_meters = cursor.fetchall() |
1269
|
|
|
|
1270
|
|
|
meter_dict = dict() |
1271
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
1272
|
|
|
for row in rows_meters: |
1273
|
|
|
meter_dict[row[2]] = {"type": 'meter', |
1274
|
|
|
"id": row[0], |
1275
|
|
|
"name": row[1], |
1276
|
|
|
"uuid": row[2]} |
1277
|
|
|
|
1278
|
|
|
query = (" SELECT id, name, uuid " |
1279
|
|
|
" FROM tbl_offline_meters ") |
1280
|
|
|
cursor.execute(query) |
1281
|
|
|
rows_offline_meters = cursor.fetchall() |
1282
|
|
|
|
1283
|
|
|
offline_meter_dict = dict() |
1284
|
|
|
if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
1285
|
|
|
for row in rows_offline_meters: |
1286
|
|
|
offline_meter_dict[row[2]] = {"type": 'offline_meter', |
1287
|
|
|
"id": row[0], |
1288
|
|
|
"name": row[1], |
1289
|
|
|
"uuid": row[2]} |
1290
|
|
|
|
1291
|
|
|
query = (" SELECT id, name, uuid " |
1292
|
|
|
" FROM tbl_virtual_meters ") |
1293
|
|
|
cursor.execute(query) |
1294
|
|
|
rows_virtual_meters = cursor.fetchall() |
1295
|
|
|
|
1296
|
|
|
virtual_meter_dict = dict() |
1297
|
|
|
if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
1298
|
|
|
for row in rows_virtual_meters: |
1299
|
|
|
virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
1300
|
|
|
"id": row[0], |
1301
|
|
|
"name": row[1], |
1302
|
|
|
"uuid": row[2]} |
1303
|
|
|
|
1304
|
|
|
# validate numerator meter uuid |
1305
|
|
|
if meter_dict.get(numerator_meter_uuid) is None and \ |
1306
|
|
|
virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
1307
|
|
|
offline_meter_dict.get(numerator_meter_uuid) is None: |
1308
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1309
|
|
|
description='API.INVALID_NUMERATOR_METER_UUID') |
1310
|
|
|
|
1311
|
|
|
# validate denominator meter uuid |
1312
|
|
|
if denominator_meter_uuid == numerator_meter_uuid: |
1313
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1314
|
|
|
description='API.INVALID_DENOMINATOR_METER_UUID') |
1315
|
|
|
|
1316
|
|
|
if denominator_meter_uuid not in meter_dict and \ |
1317
|
|
|
denominator_meter_uuid not in virtual_meter_dict and \ |
1318
|
|
|
denominator_meter_uuid not in offline_meter_dict: |
1319
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1320
|
|
|
description='API.INVALID_DENOMINATOR_METER_UUID') |
1321
|
|
|
|
1322
|
|
|
add_values = (" UPDATE tbl_equipments_parameters " |
1323
|
|
|
" SET name = %s , parameter_type = %s, constant = %s, " |
1324
|
|
|
" point_id = %s, numerator_meter_uuid = %s, denominator_meter_uuid = %s " |
1325
|
|
|
" WHERE id = %s ") |
1326
|
|
|
cursor.execute(add_values, (name, |
1327
|
|
|
parameter_type, |
1328
|
|
|
constant, |
1329
|
|
|
point_id, |
1330
|
|
|
numerator_meter_uuid, |
1331
|
|
|
denominator_meter_uuid, |
1332
|
|
|
pid)) |
1333
|
|
|
cnx.commit() |
1334
|
|
|
|
1335
|
|
|
cursor.close() |
1336
|
|
|
cnx.close() |
1337
|
|
|
|
1338
|
|
|
resp.status = falcon.HTTP_200 |
1339
|
|
|
|
1340
|
|
|
|
1341
|
|
View Code Duplication |
class EquipmentMeterCollection: |
|
|
|
|
1342
|
|
|
def __init__(self): |
1343
|
|
|
"""Initializes EquipmentMeterCollection""" |
1344
|
|
|
pass |
1345
|
|
|
|
1346
|
|
|
@staticmethod |
1347
|
|
|
def on_options(req, resp, id_): |
1348
|
|
|
_ = req |
1349
|
|
|
resp.status = falcon.HTTP_200 |
1350
|
|
|
_ = id_ |
1351
|
|
|
|
1352
|
|
|
@staticmethod |
1353
|
|
|
def on_get(req, resp, id_): |
1354
|
|
|
if 'API-KEY' not in req.headers or \ |
1355
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
1356
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
1357
|
|
|
access_control(req) |
1358
|
|
|
else: |
1359
|
|
|
api_key_control(req) |
1360
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1361
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1362
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1363
|
|
|
|
1364
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1365
|
|
|
cursor = cnx.cursor() |
1366
|
|
|
|
1367
|
|
|
cursor.execute(" SELECT name " |
1368
|
|
|
" FROM tbl_equipments " |
1369
|
|
|
" WHERE id = %s ", (id_,)) |
1370
|
|
|
if cursor.fetchone() is None: |
1371
|
|
|
cursor.close() |
1372
|
|
|
cnx.close() |
1373
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1374
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1375
|
|
|
|
1376
|
|
|
query = (" SELECT id, name, uuid " |
1377
|
|
|
" FROM tbl_energy_categories ") |
1378
|
|
|
cursor.execute(query) |
1379
|
|
|
rows_energy_categories = cursor.fetchall() |
1380
|
|
|
|
1381
|
|
|
energy_category_dict = dict() |
1382
|
|
|
if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
1383
|
|
|
for row in rows_energy_categories: |
1384
|
|
|
energy_category_dict[row[0]] = {"id": row[0], |
1385
|
|
|
"name": row[1], |
1386
|
|
|
"uuid": row[2]} |
1387
|
|
|
|
1388
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
1389
|
|
|
" FROM tbl_equipments e, tbl_equipments_meters em, tbl_meters m " |
1390
|
|
|
" WHERE em.equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
1391
|
|
|
" ORDER BY m.id ") |
1392
|
|
|
cursor.execute(query, (id_,)) |
1393
|
|
|
rows = cursor.fetchall() |
1394
|
|
|
|
1395
|
|
|
result = list() |
1396
|
|
|
if rows is not None and len(rows) > 0: |
1397
|
|
|
for row in rows: |
1398
|
|
|
meta_result = {"id": row[0], |
1399
|
|
|
"name": row[1], |
1400
|
|
|
"uuid": row[2], |
1401
|
|
|
"energy_category": energy_category_dict.get(row[3], None), |
1402
|
|
|
"is_output": bool(row[4])} |
1403
|
|
|
result.append(meta_result) |
1404
|
|
|
|
1405
|
|
|
resp.text = json.dumps(result) |
1406
|
|
|
|
1407
|
|
|
@staticmethod |
1408
|
|
|
@user_logger |
1409
|
|
|
def on_post(req, resp, id_): |
1410
|
|
|
"""Handles POST requests""" |
1411
|
|
|
admin_control(req) |
1412
|
|
|
try: |
1413
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1414
|
|
|
except Exception as ex: |
1415
|
|
|
print(str(ex)) |
1416
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
1417
|
|
|
title='API.BAD_REQUEST', |
1418
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
1419
|
|
|
|
1420
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1421
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1422
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1423
|
|
|
|
1424
|
|
|
new_values = json.loads(raw_json) |
1425
|
|
|
|
1426
|
|
|
if 'meter_id' not in new_values['data'].keys() or \ |
1427
|
|
|
not isinstance(new_values['data']['meter_id'], int) or \ |
1428
|
|
|
new_values['data']['meter_id'] <= 0: |
1429
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1430
|
|
|
description='API.INVALID_METER_ID') |
1431
|
|
|
meter_id = new_values['data']['meter_id'] |
1432
|
|
|
|
1433
|
|
|
if 'is_output' not in new_values['data'].keys() or \ |
1434
|
|
|
not isinstance(new_values['data']['is_output'], bool): |
1435
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1436
|
|
|
description='API.INVALID_IS_OUTPUT_VALUE') |
1437
|
|
|
is_output = new_values['data']['is_output'] |
1438
|
|
|
|
1439
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1440
|
|
|
cursor = cnx.cursor() |
1441
|
|
|
|
1442
|
|
|
cursor.execute(" SELECT name " |
1443
|
|
|
" from tbl_equipments " |
1444
|
|
|
" WHERE id = %s ", (id_,)) |
1445
|
|
|
if cursor.fetchone() is None: |
1446
|
|
|
cursor.close() |
1447
|
|
|
cnx.close() |
1448
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1449
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1450
|
|
|
|
1451
|
|
|
cursor.execute(" SELECT name " |
1452
|
|
|
" FROM tbl_meters " |
1453
|
|
|
" WHERE id = %s ", (meter_id,)) |
1454
|
|
|
if cursor.fetchone() is None: |
1455
|
|
|
cursor.close() |
1456
|
|
|
cnx.close() |
1457
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1458
|
|
|
description='API.METER_NOT_FOUND') |
1459
|
|
|
|
1460
|
|
|
query = (" SELECT id " |
1461
|
|
|
" FROM tbl_equipments_meters " |
1462
|
|
|
" WHERE equipment_id = %s AND meter_id = %s") |
1463
|
|
|
cursor.execute(query, (id_, meter_id,)) |
1464
|
|
|
if cursor.fetchone() is not None: |
1465
|
|
|
cursor.close() |
1466
|
|
|
cnx.close() |
1467
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
1468
|
|
|
description='API.EQUIPMENT_METER_RELATION_EXISTS') |
1469
|
|
|
|
1470
|
|
|
add_row = (" INSERT INTO tbl_equipments_meters (equipment_id, meter_id, is_output ) " |
1471
|
|
|
" VALUES (%s, %s, %s) ") |
1472
|
|
|
cursor.execute(add_row, (id_, meter_id, is_output)) |
1473
|
|
|
cnx.commit() |
1474
|
|
|
cursor.close() |
1475
|
|
|
cnx.close() |
1476
|
|
|
|
1477
|
|
|
resp.status = falcon.HTTP_201 |
1478
|
|
|
resp.location = '/equipments/' + str(id_) + '/meters/' + str(meter_id) |
1479
|
|
|
|
1480
|
|
|
|
1481
|
|
View Code Duplication |
class EquipmentMeterItem: |
|
|
|
|
1482
|
|
|
def __init__(self): |
1483
|
|
|
"""Initializes EquipmentMeterItem""" |
1484
|
|
|
pass |
1485
|
|
|
|
1486
|
|
|
@staticmethod |
1487
|
|
|
def on_options(req, resp, id_, mid): |
1488
|
|
|
_ = req |
1489
|
|
|
resp.status = falcon.HTTP_200 |
1490
|
|
|
_ = id_ |
1491
|
|
|
_ = mid |
1492
|
|
|
|
1493
|
|
|
@staticmethod |
1494
|
|
|
@user_logger |
1495
|
|
|
def on_delete(req, resp, id_, mid): |
1496
|
|
|
admin_control(req) |
1497
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1498
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1499
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1500
|
|
|
|
1501
|
|
|
if not mid.isdigit() or int(mid) <= 0: |
1502
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1503
|
|
|
description='API.INVALID_METER_ID') |
1504
|
|
|
|
1505
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1506
|
|
|
cursor = cnx.cursor() |
1507
|
|
|
|
1508
|
|
|
cursor.execute(" SELECT name " |
1509
|
|
|
" FROM tbl_equipments " |
1510
|
|
|
" WHERE id = %s ", (id_,)) |
1511
|
|
|
if cursor.fetchone() is None: |
1512
|
|
|
cursor.close() |
1513
|
|
|
cnx.close() |
1514
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1515
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1516
|
|
|
|
1517
|
|
|
cursor.execute(" SELECT name " |
1518
|
|
|
" FROM tbl_meters " |
1519
|
|
|
" WHERE id = %s ", (mid,)) |
1520
|
|
|
if cursor.fetchone() is None: |
1521
|
|
|
cursor.close() |
1522
|
|
|
cnx.close() |
1523
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1524
|
|
|
description='API.METER_NOT_FOUND') |
1525
|
|
|
|
1526
|
|
|
cursor.execute(" SELECT id " |
1527
|
|
|
" FROM tbl_equipments_meters " |
1528
|
|
|
" WHERE equipment_id = %s AND meter_id = %s ", (id_, mid)) |
1529
|
|
|
if cursor.fetchone() is None: |
1530
|
|
|
cursor.close() |
1531
|
|
|
cnx.close() |
1532
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1533
|
|
|
description='API.EQUIPMENT_METER_RELATION_NOT_FOUND') |
1534
|
|
|
|
1535
|
|
|
cursor.execute(" DELETE FROM tbl_equipments_meters WHERE equipment_id = %s AND meter_id = %s ", (id_, mid)) |
1536
|
|
|
cnx.commit() |
1537
|
|
|
|
1538
|
|
|
cursor.close() |
1539
|
|
|
cnx.close() |
1540
|
|
|
|
1541
|
|
|
resp.status = falcon.HTTP_204 |
1542
|
|
|
|
1543
|
|
|
|
1544
|
|
View Code Duplication |
class EquipmentOfflineMeterCollection: |
|
|
|
|
1545
|
|
|
def __init__(self): |
1546
|
|
|
"""Initializes EquipmentOfflineMeterCollection""" |
1547
|
|
|
pass |
1548
|
|
|
|
1549
|
|
|
@staticmethod |
1550
|
|
|
def on_options(req, resp, id_): |
1551
|
|
|
_ = req |
1552
|
|
|
resp.status = falcon.HTTP_200 |
1553
|
|
|
_ = id_ |
1554
|
|
|
|
1555
|
|
|
@staticmethod |
1556
|
|
|
def on_get(req, resp, id_): |
1557
|
|
|
if 'API-KEY' not in req.headers or \ |
1558
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
1559
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
1560
|
|
|
access_control(req) |
1561
|
|
|
else: |
1562
|
|
|
api_key_control(req) |
1563
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1564
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1565
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1566
|
|
|
|
1567
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1568
|
|
|
cursor = cnx.cursor() |
1569
|
|
|
|
1570
|
|
|
cursor.execute(" SELECT name " |
1571
|
|
|
" FROM tbl_equipments " |
1572
|
|
|
" WHERE id = %s ", (id_,)) |
1573
|
|
|
if cursor.fetchone() is None: |
1574
|
|
|
cursor.close() |
1575
|
|
|
cnx.close() |
1576
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1577
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1578
|
|
|
|
1579
|
|
|
query = (" SELECT id, name, uuid " |
1580
|
|
|
" FROM tbl_energy_categories ") |
1581
|
|
|
cursor.execute(query) |
1582
|
|
|
rows_energy_categories = cursor.fetchall() |
1583
|
|
|
|
1584
|
|
|
energy_category_dict = dict() |
1585
|
|
|
if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
1586
|
|
|
for row in rows_energy_categories: |
1587
|
|
|
energy_category_dict[row[0]] = {"id": row[0], |
1588
|
|
|
"name": row[1], |
1589
|
|
|
"uuid": row[2]} |
1590
|
|
|
|
1591
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
1592
|
|
|
" FROM tbl_equipments e, tbl_equipments_offline_meters em, tbl_offline_meters m " |
1593
|
|
|
" WHERE em.equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
1594
|
|
|
" ORDER BY m.id ") |
1595
|
|
|
cursor.execute(query, (id_,)) |
1596
|
|
|
rows = cursor.fetchall() |
1597
|
|
|
|
1598
|
|
|
result = list() |
1599
|
|
|
if rows is not None and len(rows) > 0: |
1600
|
|
|
for row in rows: |
1601
|
|
|
meta_result = {"id": row[0], |
1602
|
|
|
"name": row[1], |
1603
|
|
|
"uuid": row[2], |
1604
|
|
|
"energy_category": energy_category_dict.get(row[3], None), |
1605
|
|
|
"is_output": bool(row[4])} |
1606
|
|
|
result.append(meta_result) |
1607
|
|
|
|
1608
|
|
|
resp.text = json.dumps(result) |
1609
|
|
|
|
1610
|
|
|
@staticmethod |
1611
|
|
|
@user_logger |
1612
|
|
|
def on_post(req, resp, id_): |
1613
|
|
|
"""Handles POST requests""" |
1614
|
|
|
admin_control(req) |
1615
|
|
|
try: |
1616
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1617
|
|
|
except Exception as ex: |
1618
|
|
|
print(str(ex)) |
1619
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
1620
|
|
|
title='API.BAD_REQUEST', |
1621
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
1622
|
|
|
|
1623
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1624
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1625
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1626
|
|
|
|
1627
|
|
|
new_values = json.loads(raw_json) |
1628
|
|
|
|
1629
|
|
|
if 'offline_meter_id' not in new_values['data'].keys() or \ |
1630
|
|
|
not isinstance(new_values['data']['offline_meter_id'], int) or \ |
1631
|
|
|
new_values['data']['offline_meter_id'] <= 0: |
1632
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1633
|
|
|
description='API.INVALID_OFFLINE_METER_ID') |
1634
|
|
|
offline_meter_id = new_values['data']['offline_meter_id'] |
1635
|
|
|
|
1636
|
|
|
if 'is_output' not in new_values['data'].keys() or \ |
1637
|
|
|
not isinstance(new_values['data']['is_output'], bool): |
1638
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1639
|
|
|
description='API.INVALID_IS_OUTPUT_VALUE') |
1640
|
|
|
is_output = new_values['data']['is_output'] |
1641
|
|
|
|
1642
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1643
|
|
|
cursor = cnx.cursor() |
1644
|
|
|
|
1645
|
|
|
cursor.execute(" SELECT name " |
1646
|
|
|
" from tbl_equipments " |
1647
|
|
|
" WHERE id = %s ", (id_,)) |
1648
|
|
|
if cursor.fetchone() is None: |
1649
|
|
|
cursor.close() |
1650
|
|
|
cnx.close() |
1651
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1652
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1653
|
|
|
|
1654
|
|
|
cursor.execute(" SELECT name " |
1655
|
|
|
" FROM tbl_offline_meters " |
1656
|
|
|
" WHERE id = %s ", (offline_meter_id,)) |
1657
|
|
|
if cursor.fetchone() is None: |
1658
|
|
|
cursor.close() |
1659
|
|
|
cnx.close() |
1660
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1661
|
|
|
description='API.OFFLINE_METER_NOT_FOUND') |
1662
|
|
|
|
1663
|
|
|
query = (" SELECT id " |
1664
|
|
|
" FROM tbl_equipments_offline_meters " |
1665
|
|
|
" WHERE equipment_id = %s AND offline_meter_id = %s") |
1666
|
|
|
cursor.execute(query, (id_, offline_meter_id,)) |
1667
|
|
|
if cursor.fetchone() is not None: |
1668
|
|
|
cursor.close() |
1669
|
|
|
cnx.close() |
1670
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
1671
|
|
|
description='API.EQUIPMENT_OFFLINE_METER_RELATION_EXISTS') |
1672
|
|
|
|
1673
|
|
|
add_row = (" INSERT INTO tbl_equipments_offline_meters (equipment_id, offline_meter_id, is_output ) " |
1674
|
|
|
" VALUES (%s, %s, %s) ") |
1675
|
|
|
cursor.execute(add_row, (id_, offline_meter_id, is_output)) |
1676
|
|
|
cnx.commit() |
1677
|
|
|
cursor.close() |
1678
|
|
|
cnx.close() |
1679
|
|
|
|
1680
|
|
|
resp.status = falcon.HTTP_201 |
1681
|
|
|
resp.location = '/equipments/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
1682
|
|
|
|
1683
|
|
|
|
1684
|
|
View Code Duplication |
class EquipmentOfflineMeterItem: |
|
|
|
|
1685
|
|
|
def __init__(self): |
1686
|
|
|
"""Initializes EquipmentOfflineMeterItem""" |
1687
|
|
|
pass |
1688
|
|
|
|
1689
|
|
|
@staticmethod |
1690
|
|
|
def on_options(req, resp, id_, mid): |
1691
|
|
|
_ = req |
1692
|
|
|
resp.status = falcon.HTTP_200 |
1693
|
|
|
_ = id_ |
1694
|
|
|
_ = mid |
1695
|
|
|
|
1696
|
|
|
@staticmethod |
1697
|
|
|
@user_logger |
1698
|
|
|
def on_delete(req, resp, id_, mid): |
1699
|
|
|
admin_control(req) |
1700
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1701
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1702
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1703
|
|
|
|
1704
|
|
|
if not mid.isdigit() or int(mid) <= 0: |
1705
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1706
|
|
|
description='API.INVALID_OFFLINE_METER_ID') |
1707
|
|
|
|
1708
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1709
|
|
|
cursor = cnx.cursor() |
1710
|
|
|
|
1711
|
|
|
cursor.execute(" SELECT name " |
1712
|
|
|
" FROM tbl_equipments " |
1713
|
|
|
" WHERE id = %s ", (id_,)) |
1714
|
|
|
if cursor.fetchone() is None: |
1715
|
|
|
cursor.close() |
1716
|
|
|
cnx.close() |
1717
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1718
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1719
|
|
|
|
1720
|
|
|
cursor.execute(" SELECT name " |
1721
|
|
|
" FROM tbl_offline_meters " |
1722
|
|
|
" WHERE id = %s ", (mid,)) |
1723
|
|
|
if cursor.fetchone() is None: |
1724
|
|
|
cursor.close() |
1725
|
|
|
cnx.close() |
1726
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1727
|
|
|
description='API.OFFLINE_METER_NOT_FOUND') |
1728
|
|
|
|
1729
|
|
|
cursor.execute(" SELECT id " |
1730
|
|
|
" FROM tbl_equipments_offline_meters " |
1731
|
|
|
" WHERE equipment_id = %s AND offline_meter_id = %s ", (id_, mid)) |
1732
|
|
|
if cursor.fetchone() is None: |
1733
|
|
|
cursor.close() |
1734
|
|
|
cnx.close() |
1735
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1736
|
|
|
description='API.EQUIPMENT_OFFLINE_METER_RELATION_NOT_FOUND') |
1737
|
|
|
|
1738
|
|
|
cursor.execute(" DELETE FROM tbl_equipments_offline_meters " |
1739
|
|
|
" WHERE equipment_id = %s AND offline_meter_id = %s ", (id_, mid)) |
1740
|
|
|
cnx.commit() |
1741
|
|
|
|
1742
|
|
|
cursor.close() |
1743
|
|
|
cnx.close() |
1744
|
|
|
|
1745
|
|
|
resp.status = falcon.HTTP_204 |
1746
|
|
|
|
1747
|
|
|
|
1748
|
|
View Code Duplication |
class EquipmentVirtualMeterCollection: |
|
|
|
|
1749
|
|
|
def __init__(self): |
1750
|
|
|
"""Initializes EquipmentVirtualMeterCollection""" |
1751
|
|
|
pass |
1752
|
|
|
|
1753
|
|
|
@staticmethod |
1754
|
|
|
def on_options(req, resp, id_): |
1755
|
|
|
_ = req |
1756
|
|
|
resp.status = falcon.HTTP_200 |
1757
|
|
|
_ = id_ |
1758
|
|
|
|
1759
|
|
|
@staticmethod |
1760
|
|
|
def on_get(req, resp, id_): |
1761
|
|
|
if 'API-KEY' not in req.headers or \ |
1762
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
1763
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
1764
|
|
|
access_control(req) |
1765
|
|
|
else: |
1766
|
|
|
api_key_control(req) |
1767
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1768
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1769
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1770
|
|
|
|
1771
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1772
|
|
|
cursor = cnx.cursor() |
1773
|
|
|
|
1774
|
|
|
cursor.execute(" SELECT name " |
1775
|
|
|
" FROM tbl_equipments " |
1776
|
|
|
" WHERE id = %s ", (id_,)) |
1777
|
|
|
if cursor.fetchone() is None: |
1778
|
|
|
cursor.close() |
1779
|
|
|
cnx.close() |
1780
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1781
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1782
|
|
|
|
1783
|
|
|
query = (" SELECT id, name, uuid " |
1784
|
|
|
" FROM tbl_energy_categories ") |
1785
|
|
|
cursor.execute(query) |
1786
|
|
|
rows_energy_categories = cursor.fetchall() |
1787
|
|
|
|
1788
|
|
|
energy_category_dict = dict() |
1789
|
|
|
if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
1790
|
|
|
for row in rows_energy_categories: |
1791
|
|
|
energy_category_dict[row[0]] = {"id": row[0], |
1792
|
|
|
"name": row[1], |
1793
|
|
|
"uuid": row[2]} |
1794
|
|
|
|
1795
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
1796
|
|
|
" FROM tbl_equipments e, tbl_equipments_virtual_meters em, tbl_virtual_meters m " |
1797
|
|
|
" WHERE em.equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
1798
|
|
|
" ORDER BY m.id ") |
1799
|
|
|
cursor.execute(query, (id_,)) |
1800
|
|
|
rows = cursor.fetchall() |
1801
|
|
|
|
1802
|
|
|
result = list() |
1803
|
|
|
if rows is not None and len(rows) > 0: |
1804
|
|
|
for row in rows: |
1805
|
|
|
meta_result = {"id": row[0], |
1806
|
|
|
"name": row[1], |
1807
|
|
|
"uuid": row[2], |
1808
|
|
|
"energy_category": energy_category_dict.get(row[3], None), |
1809
|
|
|
"is_output": bool(row[4])} |
1810
|
|
|
result.append(meta_result) |
1811
|
|
|
|
1812
|
|
|
resp.text = json.dumps(result) |
1813
|
|
|
|
1814
|
|
|
@staticmethod |
1815
|
|
|
@user_logger |
1816
|
|
|
def on_post(req, resp, id_): |
1817
|
|
|
"""Handles POST requests""" |
1818
|
|
|
admin_control(req) |
1819
|
|
|
try: |
1820
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1821
|
|
|
except Exception as ex: |
1822
|
|
|
print(str(ex)) |
1823
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
1824
|
|
|
title='API.BAD_REQUEST', |
1825
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
1826
|
|
|
|
1827
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1828
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1829
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1830
|
|
|
|
1831
|
|
|
new_values = json.loads(raw_json) |
1832
|
|
|
|
1833
|
|
|
if 'virtual_meter_id' not in new_values['data'].keys() or \ |
1834
|
|
|
not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
1835
|
|
|
new_values['data']['virtual_meter_id'] <= 0: |
1836
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1837
|
|
|
description='API.INVALID_VIRTUAL_METER_ID') |
1838
|
|
|
virtual_meter_id = new_values['data']['virtual_meter_id'] |
1839
|
|
|
|
1840
|
|
|
if 'is_output' not in new_values['data'].keys() or \ |
1841
|
|
|
not isinstance(new_values['data']['is_output'], bool): |
1842
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1843
|
|
|
description='API.INVALID_IS_OUTPUT_VALUE') |
1844
|
|
|
is_output = new_values['data']['is_output'] |
1845
|
|
|
|
1846
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1847
|
|
|
cursor = cnx.cursor() |
1848
|
|
|
|
1849
|
|
|
cursor.execute(" SELECT name " |
1850
|
|
|
" from tbl_equipments " |
1851
|
|
|
" WHERE id = %s ", (id_,)) |
1852
|
|
|
if cursor.fetchone() is None: |
1853
|
|
|
cursor.close() |
1854
|
|
|
cnx.close() |
1855
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1856
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1857
|
|
|
|
1858
|
|
|
cursor.execute(" SELECT name " |
1859
|
|
|
" FROM tbl_virtual_meters " |
1860
|
|
|
" WHERE id = %s ", (virtual_meter_id,)) |
1861
|
|
|
if cursor.fetchone() is None: |
1862
|
|
|
cursor.close() |
1863
|
|
|
cnx.close() |
1864
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1865
|
|
|
description='API.VIRTUAL_METER_NOT_FOUND') |
1866
|
|
|
|
1867
|
|
|
query = (" SELECT id " |
1868
|
|
|
" FROM tbl_equipments_virtual_meters " |
1869
|
|
|
" WHERE equipment_id = %s AND virtual_meter_id = %s") |
1870
|
|
|
cursor.execute(query, (id_, virtual_meter_id,)) |
1871
|
|
|
if cursor.fetchone() is not None: |
1872
|
|
|
cursor.close() |
1873
|
|
|
cnx.close() |
1874
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
1875
|
|
|
description='API.EQUIPMENT_VIRTUAL_METER_RELATION_EXISTS') |
1876
|
|
|
|
1877
|
|
|
add_row = (" INSERT INTO tbl_equipments_virtual_meters (equipment_id, virtual_meter_id, is_output ) " |
1878
|
|
|
" VALUES (%s, %s, %s) ") |
1879
|
|
|
cursor.execute(add_row, (id_, virtual_meter_id, is_output)) |
1880
|
|
|
cnx.commit() |
1881
|
|
|
cursor.close() |
1882
|
|
|
cnx.close() |
1883
|
|
|
|
1884
|
|
|
resp.status = falcon.HTTP_201 |
1885
|
|
|
resp.location = '/equipments/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
1886
|
|
|
|
1887
|
|
|
|
1888
|
|
View Code Duplication |
class EquipmentVirtualMeterItem: |
|
|
|
|
1889
|
|
|
def __init__(self): |
1890
|
|
|
"""Initializes EquipmentVirtualMeterItem""" |
1891
|
|
|
pass |
1892
|
|
|
|
1893
|
|
|
@staticmethod |
1894
|
|
|
def on_options(req, resp, id_, mid): |
1895
|
|
|
_ = req |
1896
|
|
|
resp.status = falcon.HTTP_200 |
1897
|
|
|
_ = id_ |
1898
|
|
|
_ = mid |
1899
|
|
|
|
1900
|
|
|
@staticmethod |
1901
|
|
|
@user_logger |
1902
|
|
|
def on_delete(req, resp, id_, mid): |
1903
|
|
|
admin_control(req) |
1904
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1905
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1906
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1907
|
|
|
|
1908
|
|
|
if not mid.isdigit() or int(mid) <= 0: |
1909
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1910
|
|
|
description='API.INVALID_VIRTUAL_METER_ID') |
1911
|
|
|
|
1912
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1913
|
|
|
cursor = cnx.cursor() |
1914
|
|
|
|
1915
|
|
|
cursor.execute(" SELECT name " |
1916
|
|
|
" FROM tbl_equipments " |
1917
|
|
|
" WHERE id = %s ", (id_,)) |
1918
|
|
|
if cursor.fetchone() is None: |
1919
|
|
|
cursor.close() |
1920
|
|
|
cnx.close() |
1921
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1922
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1923
|
|
|
|
1924
|
|
|
cursor.execute(" SELECT name " |
1925
|
|
|
" FROM tbl_virtual_meters " |
1926
|
|
|
" WHERE id = %s ", (mid,)) |
1927
|
|
|
if cursor.fetchone() is None: |
1928
|
|
|
cursor.close() |
1929
|
|
|
cnx.close() |
1930
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1931
|
|
|
description='API.VIRTUAL_METER_NOT_FOUND') |
1932
|
|
|
|
1933
|
|
|
cursor.execute(" SELECT id " |
1934
|
|
|
" FROM tbl_equipments_virtual_meters " |
1935
|
|
|
" WHERE equipment_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
1936
|
|
|
if cursor.fetchone() is None: |
1937
|
|
|
cursor.close() |
1938
|
|
|
cnx.close() |
1939
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1940
|
|
|
description='API.EQUIPMENT_VIRTUAL_METER_RELATION_NOT_FOUND') |
1941
|
|
|
|
1942
|
|
|
cursor.execute(" DELETE FROM tbl_equipments_virtual_meters " |
1943
|
|
|
" WHERE equipment_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
1944
|
|
|
cnx.commit() |
1945
|
|
|
|
1946
|
|
|
cursor.close() |
1947
|
|
|
cnx.close() |
1948
|
|
|
|
1949
|
|
|
resp.status = falcon.HTTP_204 |
1950
|
|
|
|
1951
|
|
|
|
1952
|
|
View Code Duplication |
class EquipmentCommandCollection: |
|
|
|
|
1953
|
|
|
def __init__(self): |
1954
|
|
|
"""Initializes Class""" |
1955
|
|
|
pass |
1956
|
|
|
|
1957
|
|
|
@staticmethod |
1958
|
|
|
def on_options(req, resp, id_): |
1959
|
|
|
_ = req |
1960
|
|
|
resp.status = falcon.HTTP_200 |
1961
|
|
|
_ = id_ |
1962
|
|
|
|
1963
|
|
|
@staticmethod |
1964
|
|
|
def on_get(req, resp, id_): |
1965
|
|
|
if 'API-KEY' not in req.headers or \ |
1966
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
1967
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
1968
|
|
|
access_control(req) |
1969
|
|
|
else: |
1970
|
|
|
api_key_control(req) |
1971
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1972
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1973
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1974
|
|
|
|
1975
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1976
|
|
|
cursor = cnx.cursor() |
1977
|
|
|
|
1978
|
|
|
cursor.execute(" SELECT name " |
1979
|
|
|
" FROM tbl_equipments " |
1980
|
|
|
" WHERE id = %s ", (id_,)) |
1981
|
|
|
if cursor.fetchone() is None: |
1982
|
|
|
cursor.close() |
1983
|
|
|
cnx.close() |
1984
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1985
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1986
|
|
|
|
1987
|
|
|
query = (" SELECT c.id, c.name, c.uuid " |
1988
|
|
|
" FROM tbl_equipments e, tbl_equipments_commands ec, tbl_commands c " |
1989
|
|
|
" WHERE ec.equipment_id = e.id AND c.id = ec.command_id AND e.id = %s " |
1990
|
|
|
" ORDER BY c.id ") |
1991
|
|
|
cursor.execute(query, (id_,)) |
1992
|
|
|
rows = cursor.fetchall() |
1993
|
|
|
|
1994
|
|
|
result = list() |
1995
|
|
|
if rows is not None and len(rows) > 0: |
1996
|
|
|
for row in rows: |
1997
|
|
|
meta_result = {"id": row[0], |
1998
|
|
|
"name": row[1], |
1999
|
|
|
"uuid": row[2]} |
2000
|
|
|
result.append(meta_result) |
2001
|
|
|
|
2002
|
|
|
resp.text = json.dumps(result) |
2003
|
|
|
|
2004
|
|
|
@staticmethod |
2005
|
|
|
@user_logger |
2006
|
|
|
def on_post(req, resp, id_): |
2007
|
|
|
"""Handles POST requests""" |
2008
|
|
|
admin_control(req) |
2009
|
|
|
try: |
2010
|
|
|
raw_json = req.stream.read().decode('utf-8') |
2011
|
|
|
except Exception as ex: |
2012
|
|
|
print(str(ex)) |
2013
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
2014
|
|
|
title='API.BAD_REQUEST', |
2015
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
2016
|
|
|
|
2017
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
2018
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2019
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
2020
|
|
|
|
2021
|
|
|
new_values = json.loads(raw_json) |
2022
|
|
|
|
2023
|
|
|
if 'command_id' not in new_values['data'].keys() or \ |
2024
|
|
|
not isinstance(new_values['data']['command_id'], int) or \ |
2025
|
|
|
new_values['data']['command_id'] <= 0: |
2026
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2027
|
|
|
description='API.INVALID_COMMAND_ID') |
2028
|
|
|
command_id = new_values['data']['command_id'] |
2029
|
|
|
|
2030
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2031
|
|
|
cursor = cnx.cursor() |
2032
|
|
|
|
2033
|
|
|
cursor.execute(" SELECT name " |
2034
|
|
|
" from tbl_equipments " |
2035
|
|
|
" WHERE id = %s ", (id_,)) |
2036
|
|
|
if cursor.fetchone() is None: |
2037
|
|
|
cursor.close() |
2038
|
|
|
cnx.close() |
2039
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2040
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
2041
|
|
|
|
2042
|
|
|
cursor.execute(" SELECT name " |
2043
|
|
|
" FROM tbl_commands " |
2044
|
|
|
" WHERE id = %s ", (command_id,)) |
2045
|
|
|
if cursor.fetchone() is None: |
2046
|
|
|
cursor.close() |
2047
|
|
|
cnx.close() |
2048
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2049
|
|
|
description='API.COMMAND_NOT_FOUND') |
2050
|
|
|
|
2051
|
|
|
query = (" SELECT id " |
2052
|
|
|
" FROM tbl_equipments_commands " |
2053
|
|
|
" WHERE equipment_id = %s AND command_id = %s") |
2054
|
|
|
cursor.execute(query, (id_, command_id,)) |
2055
|
|
|
if cursor.fetchone() is not None: |
2056
|
|
|
cursor.close() |
2057
|
|
|
cnx.close() |
2058
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2059
|
|
|
description='API.EQUIPMENT_COMMAND_RELATION_EXISTS') |
2060
|
|
|
|
2061
|
|
|
add_row = (" INSERT INTO tbl_equipments_commands (equipment_id, command_id) " |
2062
|
|
|
" VALUES (%s, %s) ") |
2063
|
|
|
cursor.execute(add_row, (id_, command_id,)) |
2064
|
|
|
cnx.commit() |
2065
|
|
|
cursor.close() |
2066
|
|
|
cnx.close() |
2067
|
|
|
|
2068
|
|
|
resp.status = falcon.HTTP_201 |
2069
|
|
|
resp.location = '/equipments/' + str(id_) + '/commands/' + str(command_id) |
2070
|
|
|
|
2071
|
|
|
|
2072
|
|
View Code Duplication |
class EquipmentCommandItem: |
|
|
|
|
2073
|
|
|
def __init__(self): |
2074
|
|
|
"""Initializes Class""" |
2075
|
|
|
pass |
2076
|
|
|
|
2077
|
|
|
@staticmethod |
2078
|
|
|
def on_options(req, resp, id_, cid): |
2079
|
|
|
_ = req |
2080
|
|
|
resp.status = falcon.HTTP_200 |
2081
|
|
|
_ = id_ |
2082
|
|
|
_ = cid |
2083
|
|
|
|
2084
|
|
|
@staticmethod |
2085
|
|
|
@user_logger |
2086
|
|
|
def on_delete(req, resp, id_, cid): |
2087
|
|
|
admin_control(req) |
2088
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
2089
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2090
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
2091
|
|
|
|
2092
|
|
|
if not cid.isdigit() or int(cid) <= 0: |
2093
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2094
|
|
|
description='API.INVALID_COMMAND_ID') |
2095
|
|
|
|
2096
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2097
|
|
|
cursor = cnx.cursor() |
2098
|
|
|
|
2099
|
|
|
cursor.execute(" SELECT name " |
2100
|
|
|
" FROM tbl_equipments " |
2101
|
|
|
" WHERE id = %s ", (id_,)) |
2102
|
|
|
if cursor.fetchone() is None: |
2103
|
|
|
cursor.close() |
2104
|
|
|
cnx.close() |
2105
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2106
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
2107
|
|
|
|
2108
|
|
|
cursor.execute(" SELECT name " |
2109
|
|
|
" FROM tbl_commands " |
2110
|
|
|
" WHERE id = %s ", (cid,)) |
2111
|
|
|
if cursor.fetchone() is None: |
2112
|
|
|
cursor.close() |
2113
|
|
|
cnx.close() |
2114
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2115
|
|
|
description='API.COMMAND_NOT_FOUND') |
2116
|
|
|
|
2117
|
|
|
cursor.execute(" SELECT id " |
2118
|
|
|
" FROM tbl_equipments_commands " |
2119
|
|
|
" WHERE equipment_id = %s AND command_id = %s ", (id_, cid)) |
2120
|
|
|
if cursor.fetchone() is None: |
2121
|
|
|
cursor.close() |
2122
|
|
|
cnx.close() |
2123
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2124
|
|
|
description='API.EQUIPMENT_COMMAND_RELATION_NOT_FOUND') |
2125
|
|
|
|
2126
|
|
|
cursor.execute(" DELETE FROM tbl_equipments_commands WHERE equipment_id = %s AND command_id = %s ", (id_, cid)) |
2127
|
|
|
cnx.commit() |
2128
|
|
|
|
2129
|
|
|
cursor.close() |
2130
|
|
|
cnx.close() |
2131
|
|
|
|
2132
|
|
|
resp.status = falcon.HTTP_204 |
2133
|
|
|
|
2134
|
|
|
|
2135
|
|
|
class EquipmentExport: |
2136
|
|
|
def __init__(self): |
2137
|
|
|
"""Initializes EquipmentExport""" |
2138
|
|
|
pass |
2139
|
|
|
|
2140
|
|
|
@staticmethod |
2141
|
|
|
def on_options(req, resp, id_): |
2142
|
|
|
_ = req |
2143
|
|
|
resp.status = falcon.HTTP_200 |
2144
|
|
|
_ = id_ |
2145
|
|
|
|
2146
|
|
|
@staticmethod |
2147
|
|
|
def on_get(req, resp, id_): |
2148
|
|
|
if 'API-KEY' not in req.headers or \ |
2149
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
2150
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
2151
|
|
|
access_control(req) |
2152
|
|
|
else: |
2153
|
|
|
api_key_control(req) |
2154
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
2155
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2156
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
2157
|
|
|
|
2158
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2159
|
|
|
cursor = cnx.cursor() |
2160
|
|
|
|
2161
|
|
|
query = (" SELECT id, name, uuid " |
2162
|
|
|
" FROM tbl_cost_centers ") |
2163
|
|
|
cursor.execute(query) |
2164
|
|
|
rows_cost_centers = cursor.fetchall() |
2165
|
|
|
|
2166
|
|
|
cost_center_dict = dict() |
2167
|
|
|
if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
2168
|
|
|
for row in rows_cost_centers: |
2169
|
|
|
cost_center_dict[row[0]] = {"id": row[0], |
2170
|
|
|
"name": row[1], |
2171
|
|
|
"uuid": row[2]} |
2172
|
|
|
|
2173
|
|
|
query = (" SELECT id, name, uuid " |
2174
|
|
|
" FROM tbl_svgs ") |
2175
|
|
|
cursor.execute(query) |
2176
|
|
|
rows_svgs = cursor.fetchall() |
2177
|
|
|
|
2178
|
|
|
svg_dict = dict() |
2179
|
|
|
if rows_svgs is not None and len(rows_svgs) > 0: |
2180
|
|
|
for row in rows_svgs: |
2181
|
|
|
svg_dict[row[0]] = {"id": row[0], |
2182
|
|
|
"name": row[1], |
2183
|
|
|
"uuid": row[2]} |
2184
|
|
|
|
2185
|
|
|
query = (" SELECT id, name, uuid, " |
2186
|
|
|
" is_input_counted, is_output_counted, " |
2187
|
|
|
" cost_center_id, svg_id, camera_url, description " |
2188
|
|
|
" FROM tbl_equipments " |
2189
|
|
|
" WHERE id = %s ") |
2190
|
|
|
cursor.execute(query, (id_,)) |
2191
|
|
|
row = cursor.fetchone() |
2192
|
|
|
|
2193
|
|
|
if row is None: |
2194
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2195
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
2196
|
|
|
else: |
2197
|
|
|
meta_result = {"id": row[0], |
2198
|
|
|
"name": row[1], |
2199
|
|
|
"uuid": row[2], |
2200
|
|
|
"is_input_counted": bool(row[3]), |
2201
|
|
|
"is_output_counted": bool(row[4]), |
2202
|
|
|
"cost_center": cost_center_dict.get(row[5], None), |
2203
|
|
|
"svg": svg_dict.get(row[6], None), |
2204
|
|
|
"camera_url": row[7], |
2205
|
|
|
"description": row[8], |
2206
|
|
|
"commands": None, |
2207
|
|
|
"meters": None, |
2208
|
|
|
"offline_meters": None, |
2209
|
|
|
"virtual_meters": None, |
2210
|
|
|
"parameters": None} |
2211
|
|
|
|
2212
|
|
|
query = (" SELECT c.id, c.name, c.uuid " |
2213
|
|
|
" FROM tbl_equipments e, tbl_equipments_commands ec, tbl_commands c " |
2214
|
|
|
" WHERE ec.equipment_id = e.id AND c.id = ec.command_id AND e.id = %s " |
2215
|
|
|
" ORDER BY c.id ") |
2216
|
|
|
cursor.execute(query, (id_,)) |
2217
|
|
|
rows = cursor.fetchall() |
2218
|
|
|
|
2219
|
|
|
command_result = list() |
2220
|
|
|
if rows is not None and len(rows) > 0: |
2221
|
|
|
for row in rows: |
2222
|
|
|
result = {"id": row[0], |
2223
|
|
|
"name": row[1], |
2224
|
|
|
"uuid": row[2]} |
2225
|
|
|
command_result.append(result) |
2226
|
|
|
meta_result['commands'] = command_result |
2227
|
|
|
|
2228
|
|
|
query = (" SELECT id, name, uuid " |
2229
|
|
|
" FROM tbl_energy_categories ") |
2230
|
|
|
cursor.execute(query) |
2231
|
|
|
rows_energy_categories = cursor.fetchall() |
2232
|
|
|
|
2233
|
|
|
energy_category_dict = dict() |
2234
|
|
|
if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
2235
|
|
|
for row in rows_energy_categories: |
2236
|
|
|
energy_category_dict[row[0]] = {"id": row[0], |
2237
|
|
|
"name": row[1], |
2238
|
|
|
"uuid": row[2]} |
2239
|
|
|
|
2240
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
2241
|
|
|
" FROM tbl_equipments e, tbl_equipments_meters em, tbl_meters m " |
2242
|
|
|
" WHERE em.equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
2243
|
|
|
" ORDER BY m.id ") |
2244
|
|
|
cursor.execute(query, (id_,)) |
2245
|
|
|
rows = cursor.fetchall() |
2246
|
|
|
|
2247
|
|
|
meter_result = list() |
2248
|
|
|
if rows is not None and len(rows) > 0: |
2249
|
|
|
for row in rows: |
2250
|
|
|
result = {"id": row[0], |
2251
|
|
|
"name": row[1], |
2252
|
|
|
"uuid": row[2], |
2253
|
|
|
"energy_category": energy_category_dict.get(row[3], None), |
2254
|
|
|
"is_output": bool(row[4])} |
2255
|
|
|
meter_result.append(result) |
2256
|
|
|
meta_result['meters'] = meter_result |
2257
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
2258
|
|
|
" FROM tbl_equipments e, tbl_equipments_offline_meters em, tbl_offline_meters m " |
2259
|
|
|
" WHERE em.equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
2260
|
|
|
" ORDER BY m.id ") |
2261
|
|
|
cursor.execute(query, (id_,)) |
2262
|
|
|
rows = cursor.fetchall() |
2263
|
|
|
|
2264
|
|
|
offlinemeter_result = list() |
2265
|
|
|
if rows is not None and len(rows) > 0: |
2266
|
|
|
for row in rows: |
2267
|
|
|
result = {"id": row[0], |
2268
|
|
|
"name": row[1], |
2269
|
|
|
"uuid": row[2], |
2270
|
|
|
"energy_category": energy_category_dict.get(row[3], None), |
2271
|
|
|
"is_output": bool(row[4])} |
2272
|
|
|
offlinemeter_result.append(result) |
2273
|
|
|
meta_result['offline_meters'] = offlinemeter_result |
2274
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
2275
|
|
|
" FROM tbl_equipments e, tbl_equipments_virtual_meters em, tbl_virtual_meters m " |
2276
|
|
|
" WHERE em.equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
2277
|
|
|
" ORDER BY m.id ") |
2278
|
|
|
cursor.execute(query, (id_,)) |
2279
|
|
|
rows = cursor.fetchall() |
2280
|
|
|
|
2281
|
|
|
virtualmeter_result = list() |
2282
|
|
|
if rows is not None and len(rows) > 0: |
2283
|
|
|
for row in rows: |
2284
|
|
|
result = {"id": row[0], |
2285
|
|
|
"name": row[1], |
2286
|
|
|
"uuid": row[2], |
2287
|
|
|
"energy_category": energy_category_dict.get(row[3], None), |
2288
|
|
|
"is_output": bool(row[4])} |
2289
|
|
|
virtualmeter_result.append(result) |
2290
|
|
|
meta_result['virtual_meters'] = virtualmeter_result |
2291
|
|
|
query = (" SELECT id, name " |
2292
|
|
|
" FROM tbl_points ") |
2293
|
|
|
cursor.execute(query) |
2294
|
|
|
rows_points = cursor.fetchall() |
2295
|
|
|
|
2296
|
|
|
point_dict = dict() |
2297
|
|
|
if rows_points is not None and len(rows_points) > 0: |
2298
|
|
|
for row in rows_points: |
2299
|
|
|
point_dict[row[0]] = {"id": row[0], |
2300
|
|
|
"name": row[1]} |
2301
|
|
|
|
2302
|
|
|
query = (" SELECT id, name, uuid " |
2303
|
|
|
" FROM tbl_meters ") |
2304
|
|
|
cursor.execute(query) |
2305
|
|
|
rows_meters = cursor.fetchall() |
2306
|
|
|
|
2307
|
|
|
meter_dict = dict() |
2308
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
2309
|
|
|
for row in rows_meters: |
2310
|
|
|
meter_dict[row[2]] = {"type": 'meter', |
2311
|
|
|
"id": row[0], |
2312
|
|
|
"name": row[1], |
2313
|
|
|
"uuid": row[2]} |
2314
|
|
|
|
2315
|
|
|
query = (" SELECT id, name, uuid " |
2316
|
|
|
" FROM tbl_offline_meters ") |
2317
|
|
|
cursor.execute(query) |
2318
|
|
|
rows_offline_meters = cursor.fetchall() |
2319
|
|
|
|
2320
|
|
|
offline_meter_dict = dict() |
2321
|
|
|
if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
2322
|
|
|
for row in rows_offline_meters: |
2323
|
|
|
offline_meter_dict[row[2]] = {"type": 'offline_meter', |
2324
|
|
|
"id": row[0], |
2325
|
|
|
"name": row[1], |
2326
|
|
|
"uuid": row[2]} |
2327
|
|
|
|
2328
|
|
|
query = (" SELECT id, name, uuid " |
2329
|
|
|
" FROM tbl_virtual_meters ") |
2330
|
|
|
cursor.execute(query) |
2331
|
|
|
rows_virtual_meters = cursor.fetchall() |
2332
|
|
|
|
2333
|
|
|
virtual_meter_dict = dict() |
2334
|
|
|
if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
2335
|
|
|
for row in rows_virtual_meters: |
2336
|
|
|
virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
2337
|
|
|
"id": row[0], |
2338
|
|
|
"name": row[1], |
2339
|
|
|
"uuid": row[2]} |
2340
|
|
|
|
2341
|
|
|
query = (" SELECT id, name, parameter_type, " |
2342
|
|
|
" constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
2343
|
|
|
" FROM tbl_equipments_parameters " |
2344
|
|
|
" WHERE equipment_id = %s " |
2345
|
|
|
" ORDER BY id ") |
2346
|
|
|
cursor.execute(query, (id_,)) |
2347
|
|
|
rows_parameters = cursor.fetchall() |
2348
|
|
|
|
2349
|
|
|
parameters_result = list() |
2350
|
|
|
if rows_parameters is not None and len(rows_parameters) > 0: |
2351
|
|
|
for row in rows_parameters: |
2352
|
|
|
constant = None |
2353
|
|
|
point = None |
2354
|
|
|
numerator_meter = None |
2355
|
|
|
denominator_meter = None |
2356
|
|
|
if row[2] == 'point': |
2357
|
|
|
point = point_dict.get(row[4], None) |
2358
|
|
|
constant = None |
2359
|
|
|
numerator_meter = None |
2360
|
|
|
denominator_meter = None |
2361
|
|
|
elif row[2] == 'constant': |
2362
|
|
|
constant = row[3] |
2363
|
|
|
point = None |
2364
|
|
|
numerator_meter = None |
2365
|
|
|
denominator_meter = None |
2366
|
|
|
elif row[2] == 'fraction': |
2367
|
|
|
constant = None |
2368
|
|
|
point = None |
2369
|
|
|
# find numerator meter by uuid |
2370
|
|
|
numerator_meter = meter_dict.get(row[5], None) |
2371
|
|
|
if numerator_meter is None: |
2372
|
|
|
numerator_meter = virtual_meter_dict.get(row[5], None) |
2373
|
|
|
if numerator_meter is None: |
2374
|
|
|
numerator_meter = offline_meter_dict.get(row[5], None) |
2375
|
|
|
# find denominator meter by uuid |
2376
|
|
|
denominator_meter = meter_dict.get(row[6], None) |
2377
|
|
|
if denominator_meter is None: |
2378
|
|
|
denominator_meter = virtual_meter_dict.get(row[6], None) |
2379
|
|
|
if denominator_meter is None: |
2380
|
|
|
denominator_meter = offline_meter_dict.get(row[6], None) |
2381
|
|
|
|
2382
|
|
|
result = {"id": row[0], |
2383
|
|
|
"name": row[1], |
2384
|
|
|
"parameter_type": row[2], |
2385
|
|
|
"constant": constant, |
2386
|
|
|
"point": point, |
2387
|
|
|
"numerator_meter": numerator_meter, |
2388
|
|
|
"denominator_meter": denominator_meter} |
2389
|
|
|
parameters_result.append(result) |
2390
|
|
|
meta_result['parameters'] = parameters_result |
2391
|
|
|
|
2392
|
|
|
cursor.close() |
2393
|
|
|
cnx.close() |
2394
|
|
|
resp.text = json.dumps(meta_result) |
2395
|
|
|
|
2396
|
|
|
|
2397
|
|
|
class EquipmentImport: |
2398
|
|
|
def __init__(self): |
2399
|
|
|
"""Initializes EquipmentImport""" |
2400
|
|
|
pass |
2401
|
|
|
|
2402
|
|
|
@staticmethod |
2403
|
|
|
def on_options(req, resp): |
2404
|
|
|
_ = req |
2405
|
|
|
resp.status = falcon.HTTP_200 |
2406
|
|
|
|
2407
|
|
|
@staticmethod |
2408
|
|
|
def on_post(req, resp): |
2409
|
|
|
"""Handles POST requests""" |
2410
|
|
|
admin_control(req) |
2411
|
|
|
try: |
2412
|
|
|
raw_json = req.stream.read().decode('utf-8') |
2413
|
|
|
except Exception as ex: |
2414
|
|
|
print(str(ex)) |
2415
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
2416
|
|
|
title='API.BAD_REQUEST', |
2417
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
2418
|
|
|
|
2419
|
|
|
new_values = json.loads(raw_json) |
2420
|
|
|
|
2421
|
|
|
if 'name' not in new_values.keys() or \ |
2422
|
|
|
not isinstance(new_values['name'], str) or \ |
2423
|
|
|
len(str.strip(new_values['name'])) == 0: |
2424
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2425
|
|
|
description='API.INVALID_EQUIPMENT_NAME') |
2426
|
|
|
name = str.strip(new_values['name']) |
2427
|
|
|
|
2428
|
|
|
if 'is_input_counted' not in new_values.keys() or \ |
2429
|
|
|
not isinstance(new_values['is_input_counted'], bool): |
2430
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2431
|
|
|
description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
2432
|
|
|
is_input_counted = new_values['is_input_counted'] |
2433
|
|
|
|
2434
|
|
|
if 'is_output_counted' not in new_values.keys() or \ |
2435
|
|
|
not isinstance(new_values['is_output_counted'], bool): |
2436
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2437
|
|
|
description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
2438
|
|
|
is_output_counted = new_values['is_output_counted'] |
2439
|
|
|
|
2440
|
|
|
if 'id' not in new_values['cost_center'].keys() or \ |
2441
|
|
|
not isinstance(new_values['cost_center']['id'], int) or \ |
2442
|
|
|
new_values['cost_center']['id'] <= 0: |
2443
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2444
|
|
|
description='API.INVALID_COST_CENTER_ID') |
2445
|
|
|
cost_center_id = new_values['cost_center']['id'] |
2446
|
|
|
|
2447
|
|
|
svg = new_values.get('svg', None) |
2448
|
|
|
svg_id = None |
2449
|
|
|
if isinstance(svg, dict): |
2450
|
|
|
svg_id = svg.get('id', None) |
2451
|
|
|
if not (isinstance(svg_id, int) and svg_id > 0): |
2452
|
|
|
svg_id = None |
2453
|
|
|
|
2454
|
|
|
if 'camera_url' in new_values.keys() and \ |
2455
|
|
|
new_values['camera_url'] is not None and \ |
2456
|
|
|
len(str(new_values['camera_url'])) > 0: |
2457
|
|
|
camera_url = str.strip(new_values['camera_url']) |
2458
|
|
|
else: |
2459
|
|
|
camera_url = None |
2460
|
|
|
|
2461
|
|
|
if 'description' in new_values.keys() and \ |
2462
|
|
|
new_values['description'] is not None and \ |
2463
|
|
|
len(str(new_values['description'])) > 0: |
2464
|
|
|
description = str.strip(new_values['description']) |
2465
|
|
|
else: |
2466
|
|
|
description = None |
2467
|
|
|
|
2468
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2469
|
|
|
cursor = cnx.cursor() |
2470
|
|
|
|
2471
|
|
|
cursor.execute(" SELECT name " |
2472
|
|
|
" FROM tbl_equipments " |
2473
|
|
|
" WHERE name = %s ", (name,)) |
2474
|
|
|
if cursor.fetchone() is not None: |
2475
|
|
|
cursor.close() |
2476
|
|
|
cnx.close() |
2477
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2478
|
|
|
description='API.EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
2479
|
|
|
|
2480
|
|
|
if cost_center_id is not None: |
2481
|
|
|
cursor.execute(" SELECT name " |
2482
|
|
|
" FROM tbl_cost_centers " |
2483
|
|
|
" WHERE id = %s ", |
2484
|
|
|
(new_values['cost_center']['id'],)) |
2485
|
|
|
row = cursor.fetchone() |
2486
|
|
|
if row is None: |
2487
|
|
|
cursor.close() |
2488
|
|
|
cnx.close() |
2489
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2490
|
|
|
description='API.COST_CENTER_NOT_FOUND') |
2491
|
|
|
if svg_id is not None: |
2492
|
|
|
cursor.execute(" SELECT name " |
2493
|
|
|
" FROM tbl_svgs " |
2494
|
|
|
" WHERE id = %s ", |
2495
|
|
|
(svg_id,)) |
2496
|
|
|
row = cursor.fetchone() |
2497
|
|
|
if row is None: |
2498
|
|
|
cursor.close() |
2499
|
|
|
cnx.close() |
2500
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2501
|
|
|
description='API.SVG_NOT_FOUND') |
2502
|
|
|
|
2503
|
|
|
add_values = (" INSERT INTO tbl_equipments " |
2504
|
|
|
" (name, uuid, is_input_counted, is_output_counted, " |
2505
|
|
|
" cost_center_id, svg_id, camera_url, description) " |
2506
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
2507
|
|
|
cursor.execute(add_values, (name, |
2508
|
|
|
str(uuid.uuid4()), |
2509
|
|
|
is_input_counted, |
2510
|
|
|
is_output_counted, |
2511
|
|
|
cost_center_id, |
2512
|
|
|
svg_id, |
2513
|
|
|
camera_url, |
2514
|
|
|
description)) |
2515
|
|
|
new_id = cursor.lastrowid |
2516
|
|
|
if new_values['commands'] is not None and len(new_values['commands']) > 0: |
2517
|
|
|
for command in new_values['commands']: |
2518
|
|
|
cursor.execute(" SELECT name " |
2519
|
|
|
" FROM tbl_commands " |
2520
|
|
|
" WHERE id = %s ", (command['id'],)) |
2521
|
|
|
if cursor.fetchone() is None: |
2522
|
|
|
cursor.close() |
2523
|
|
|
cnx.close() |
2524
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2525
|
|
|
description='API.COMMAND_NOT_FOUND') |
2526
|
|
|
|
2527
|
|
|
query = (" SELECT id " |
2528
|
|
|
" FROM tbl_equipments_commands " |
2529
|
|
|
" WHERE equipment_id = %s AND command_id = %s") |
2530
|
|
|
cursor.execute(query, (new_id, command['id'],)) |
2531
|
|
|
if cursor.fetchone() is not None: |
2532
|
|
|
cursor.close() |
2533
|
|
|
cnx.close() |
2534
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2535
|
|
|
description='API.EQUIPMENT_COMMAND_RELATION_EXISTS') |
2536
|
|
|
|
2537
|
|
|
add_row = (" INSERT INTO tbl_equipments_commands (equipment_id, command_id) " |
2538
|
|
|
" VALUES (%s, %s) ") |
2539
|
|
|
cursor.execute(add_row, (new_id, command['id'],)) |
2540
|
|
|
if new_values['meters'] is not None and len(new_values['meters']) > 0: |
2541
|
|
|
for meter in new_values['meters']: |
2542
|
|
|
cursor.execute(" SELECT name " |
2543
|
|
|
" FROM tbl_meters " |
2544
|
|
|
" WHERE id = %s ", (meter['id'],)) |
2545
|
|
|
if cursor.fetchone() is None: |
2546
|
|
|
cursor.close() |
2547
|
|
|
cnx.close() |
2548
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2549
|
|
|
description='API.METER_NOT_FOUND') |
2550
|
|
|
|
2551
|
|
|
query = (" SELECT id " |
2552
|
|
|
" FROM tbl_equipments_meters " |
2553
|
|
|
" WHERE equipment_id = %s AND meter_id = %s") |
2554
|
|
|
cursor.execute(query, (new_id, meter['id'],)) |
2555
|
|
|
if cursor.fetchone() is not None: |
2556
|
|
|
cursor.close() |
2557
|
|
|
cnx.close() |
2558
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2559
|
|
|
description='API.EQUIPMENT_METER_RELATION_EXISTS') |
2560
|
|
|
|
2561
|
|
|
add_row = (" INSERT INTO tbl_equipments_meters (equipment_id, meter_id, is_output ) " |
2562
|
|
|
" VALUES (%s, %s, %s) ") |
2563
|
|
|
cursor.execute(add_row, (new_id, meter['id'], meter['is_output'])) |
2564
|
|
|
if new_values['offline_meters'] is not None and len(new_values['offline_meters']) > 0: |
2565
|
|
|
for offline_meter in new_values['offline_meters']: |
2566
|
|
|
cursor.execute(" SELECT name " |
2567
|
|
|
" FROM tbl_offline_meters " |
2568
|
|
|
" WHERE id = %s ", (offline_meter['id'],)) |
2569
|
|
|
if cursor.fetchone() is None: |
2570
|
|
|
cursor.close() |
2571
|
|
|
cnx.close() |
2572
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2573
|
|
|
description='API.OFFLINE_METER_NOT_FOUND') |
2574
|
|
|
|
2575
|
|
|
query = (" SELECT id " |
2576
|
|
|
" FROM tbl_equipments_offline_meters " |
2577
|
|
|
" WHERE equipment_id = %s AND offline_meter_id = %s") |
2578
|
|
|
cursor.execute(query, (new_id, offline_meter['id'],)) |
2579
|
|
|
if cursor.fetchone() is not None: |
2580
|
|
|
cursor.close() |
2581
|
|
|
cnx.close() |
2582
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2583
|
|
|
description='API.EQUIPMENT_OFFLINE_METER_RELATION_EXISTS') |
2584
|
|
|
|
2585
|
|
|
add_row = (" INSERT INTO tbl_equipments_offline_meters (equipment_id, offline_meter_id, is_output ) " |
2586
|
|
|
" VALUES (%s, %s, %s) ") |
2587
|
|
|
cursor.execute(add_row, (new_id, offline_meter['id'], offline_meter['is_output'])) |
2588
|
|
|
if new_values['virtual_meters'] is not None and len(new_values['virtual_meters']) > 0: |
2589
|
|
|
for virtual_meter in new_values['virtual_meters']: |
2590
|
|
|
cursor.execute(" SELECT name " |
2591
|
|
|
" FROM tbl_virtual_meters " |
2592
|
|
|
" WHERE id = %s ", (virtual_meter['id'],)) |
2593
|
|
|
if cursor.fetchone() is None: |
2594
|
|
|
cursor.close() |
2595
|
|
|
cnx.close() |
2596
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2597
|
|
|
description='API.VIRTUAL_METER_NOT_FOUND') |
2598
|
|
|
|
2599
|
|
|
query = (" SELECT id " |
2600
|
|
|
" FROM tbl_equipments_virtual_meters " |
2601
|
|
|
" WHERE equipment_id = %s AND virtual_meter_id = %s") |
2602
|
|
|
cursor.execute(query, (new_id, virtual_meter['id'],)) |
2603
|
|
|
if cursor.fetchone() is not None: |
2604
|
|
|
cursor.close() |
2605
|
|
|
cnx.close() |
2606
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2607
|
|
|
description='API.EQUIPMENT_VIRTUAL_METER_RELATION_EXISTS') |
2608
|
|
|
|
2609
|
|
|
add_row = (" INSERT INTO tbl_equipments_virtual_meters (equipment_id, virtual_meter_id, is_output ) " |
2610
|
|
|
" VALUES (%s, %s, %s) ") |
2611
|
|
|
cursor.execute(add_row, (new_id, virtual_meter['id'], virtual_meter['is_output'])) |
2612
|
|
|
if new_values['parameters'] is not None and len(new_values['parameters']) > 0: |
2613
|
|
|
for parameters in new_values['parameters']: |
2614
|
|
|
cursor.execute(" SELECT name " |
2615
|
|
|
" FROM tbl_equipments_parameters " |
2616
|
|
|
" WHERE name = %s AND equipment_id = %s ", (parameters['name'], new_id)) |
2617
|
|
|
if cursor.fetchone() is not None: |
2618
|
|
|
cursor.close() |
2619
|
|
|
cnx.close() |
2620
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2621
|
|
|
description='API.EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
2622
|
|
|
if 'point' in parameters: |
2623
|
|
|
if parameters['point'] is None: |
2624
|
|
|
point_id = None |
2625
|
|
|
elif parameters['point']['id'] is not None and \ |
2626
|
|
|
parameters['point']['id'] <= 0: |
2627
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2628
|
|
|
description='API.INVALID_POINT_ID') |
2629
|
|
|
else: |
2630
|
|
|
point_id = parameters['point']['id'] |
2631
|
|
|
else: |
2632
|
|
|
point_id = None |
2633
|
|
|
numerator_meter_uuid = None |
2634
|
|
|
if 'numerator_meter' in parameters: |
2635
|
|
|
if parameters['numerator_meter'] is not None and \ |
2636
|
|
|
isinstance(parameters['numerator_meter']['uuid'], str) and \ |
2637
|
|
|
len(str.strip(parameters['numerator_meter']['uuid'])) > 0: |
2638
|
|
|
numerator_meter_uuid = str.strip(parameters['numerator_meter']['uuid']) |
2639
|
|
|
|
2640
|
|
|
denominator_meter_uuid = None |
2641
|
|
|
if 'denominator_meter' in parameters: |
2642
|
|
|
if parameters['denominator_meter'] is not None and \ |
2643
|
|
|
isinstance(parameters['denominator_meter']['uuid'], str) and \ |
2644
|
|
|
len(str.strip(parameters['denominator_meter']['uuid'])) > 0: |
2645
|
|
|
denominator_meter_uuid = str.strip(parameters['denominator_meter']['uuid']) |
2646
|
|
|
|
2647
|
|
|
# validate by parameter type |
2648
|
|
|
if parameters['parameter_type'] == 'point': |
2649
|
|
|
if point_id is None: |
2650
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2651
|
|
|
description='API.INVALID_POINT_ID') |
2652
|
|
|
query = (" SELECT id, name " |
2653
|
|
|
" FROM tbl_points " |
2654
|
|
|
" WHERE id = %s ") |
2655
|
|
|
cursor.execute(query, (point_id,)) |
2656
|
|
|
if cursor.fetchone() is None: |
2657
|
|
|
cursor.close() |
2658
|
|
|
cnx.close() |
2659
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2660
|
|
|
description='API.POINT_NOT_FOUND') |
2661
|
|
|
|
2662
|
|
|
elif parameters['parameter_type'] == 'constant': |
2663
|
|
|
if parameters['constant'] is None: |
2664
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2665
|
|
|
description='API.INVALID_CONSTANT_VALUE') |
2666
|
|
|
|
2667
|
|
|
elif parameters['parameter_type'] == 'fraction': |
2668
|
|
|
query = (" SELECT id, name, uuid " |
2669
|
|
|
" FROM tbl_meters ") |
2670
|
|
|
cursor.execute(query) |
2671
|
|
|
rows_meters = cursor.fetchall() |
2672
|
|
|
meter_dict = dict() |
2673
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
2674
|
|
|
for row in rows_meters: |
2675
|
|
|
meter_dict[row[2]] = {"type": 'meter', |
2676
|
|
|
"id": row[0], |
2677
|
|
|
"name": row[1], |
2678
|
|
|
"uuid": row[2]} |
2679
|
|
|
|
2680
|
|
|
query = (" SELECT id, name, uuid " |
2681
|
|
|
" FROM tbl_offline_meters ") |
2682
|
|
|
cursor.execute(query) |
2683
|
|
|
rows_offline_meters = cursor.fetchall() |
2684
|
|
|
|
2685
|
|
|
offline_meter_dict = dict() |
2686
|
|
|
if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
2687
|
|
|
for row in rows_offline_meters: |
2688
|
|
|
offline_meter_dict[row[2]] = {"type": 'offline_meter', |
2689
|
|
|
"id": row[0], |
2690
|
|
|
"name": row[1], |
2691
|
|
|
"uuid": row[2]} |
2692
|
|
|
|
2693
|
|
|
query = (" SELECT id, name, uuid " |
2694
|
|
|
" FROM tbl_virtual_meters ") |
2695
|
|
|
cursor.execute(query) |
2696
|
|
|
rows_virtual_meters = cursor.fetchall() |
2697
|
|
|
|
2698
|
|
|
virtual_meter_dict = dict() |
2699
|
|
|
if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
2700
|
|
|
for row in rows_virtual_meters: |
2701
|
|
|
virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
2702
|
|
|
"id": row[0], |
2703
|
|
|
"name": row[1], |
2704
|
|
|
"uuid": row[2]} |
2705
|
|
|
|
2706
|
|
|
# validate numerator meter uuid |
2707
|
|
|
if numerator_meter_uuid is None: |
2708
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2709
|
|
|
description='API.INVALID_NUMERATOR_METER_UUID') |
2710
|
|
|
|
2711
|
|
|
if meter_dict.get(numerator_meter_uuid) is None and \ |
2712
|
|
|
virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
2713
|
|
|
offline_meter_dict.get(numerator_meter_uuid) is None: |
2714
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2715
|
|
|
description='API.INVALID_NUMERATOR_METER_UUID') |
2716
|
|
|
|
2717
|
|
|
# validate denominator meter uuid |
2718
|
|
|
if denominator_meter_uuid is None: |
2719
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2720
|
|
|
description='API.INVALID_DENOMINATOR_METER_UUID') |
2721
|
|
|
|
2722
|
|
|
if denominator_meter_uuid == numerator_meter_uuid: |
2723
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2724
|
|
|
description='API.INVALID_DENOMINATOR_METER_UUID') |
2725
|
|
|
|
2726
|
|
|
if denominator_meter_uuid not in meter_dict and \ |
2727
|
|
|
denominator_meter_uuid not in virtual_meter_dict and \ |
2728
|
|
|
denominator_meter_uuid not in offline_meter_dict: |
2729
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2730
|
|
|
description='API.INVALID_DENOMINATOR_METER_UUID') |
2731
|
|
|
|
2732
|
|
|
add_values = (" INSERT INTO tbl_equipments_parameters " |
2733
|
|
|
" (equipment_id, name, parameter_type, constant, " |
2734
|
|
|
" point_id, numerator_meter_uuid, denominator_meter_uuid) " |
2735
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
2736
|
|
|
cursor.execute(add_values, (new_id, |
2737
|
|
|
parameters['name'], |
2738
|
|
|
parameters['parameter_type'], |
2739
|
|
|
parameters['constant'], |
2740
|
|
|
point_id, |
2741
|
|
|
numerator_meter_uuid, |
2742
|
|
|
denominator_meter_uuid)) |
2743
|
|
|
cnx.commit() |
2744
|
|
|
cursor.close() |
2745
|
|
|
cnx.close() |
2746
|
|
|
|
2747
|
|
|
resp.status = falcon.HTTP_201 |
2748
|
|
|
resp.location = '/equipments/' + str(new_id) |
2749
|
|
|
|
2750
|
|
|
|
2751
|
|
|
class EquipmentClone: |
2752
|
|
|
def __init__(self): |
2753
|
|
|
"""Initializes EquipmentClone""" |
2754
|
|
|
pass |
2755
|
|
|
|
2756
|
|
|
@staticmethod |
2757
|
|
|
def on_options(req, resp, id_): |
2758
|
|
|
_ = req |
2759
|
|
|
resp.status = falcon.HTTP_200 |
2760
|
|
|
_ = id_ |
2761
|
|
|
|
2762
|
|
|
@staticmethod |
2763
|
|
|
@user_logger |
2764
|
|
|
def on_post(req, resp, id_): |
2765
|
|
|
admin_control(req) |
2766
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
2767
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2768
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
2769
|
|
|
|
2770
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2771
|
|
|
cursor = cnx.cursor() |
2772
|
|
|
|
2773
|
|
|
query = (" SELECT id, name, uuid " |
2774
|
|
|
" FROM tbl_cost_centers ") |
2775
|
|
|
cursor.execute(query) |
2776
|
|
|
rows_cost_centers = cursor.fetchall() |
2777
|
|
|
|
2778
|
|
|
cost_center_dict = dict() |
2779
|
|
|
if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
2780
|
|
|
for row in rows_cost_centers: |
2781
|
|
|
cost_center_dict[row[0]] = {"id": row[0], |
2782
|
|
|
"name": row[1], |
2783
|
|
|
"uuid": row[2]} |
2784
|
|
|
|
2785
|
|
|
query = (" SELECT id, name, uuid, " |
2786
|
|
|
" is_input_counted, is_output_counted, " |
2787
|
|
|
" cost_center_id, svg_id, camera_url, description " |
2788
|
|
|
" FROM tbl_equipments " |
2789
|
|
|
" WHERE id = %s ") |
2790
|
|
|
cursor.execute(query, (id_,)) |
2791
|
|
|
row = cursor.fetchone() |
2792
|
|
|
|
2793
|
|
|
if row is None: |
2794
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2795
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
2796
|
|
|
else: |
2797
|
|
|
meta_result = {"id": row[0], |
2798
|
|
|
"name": row[1], |
2799
|
|
|
"uuid": row[2], |
2800
|
|
|
"is_input_counted": bool(row[3]), |
2801
|
|
|
"is_output_counted": bool(row[4]), |
2802
|
|
|
"cost_center": cost_center_dict.get(row[5], None), |
2803
|
|
|
"svg_id": row[6], |
2804
|
|
|
"camera_url": row[7], |
2805
|
|
|
"description": row[8], |
2806
|
|
|
"commands": None, |
2807
|
|
|
"meters": None, |
2808
|
|
|
"offline_meters": None, |
2809
|
|
|
"virtual_meters": None, |
2810
|
|
|
"parameters": None |
2811
|
|
|
} |
2812
|
|
|
query = (" SELECT c.id, c.name, c.uuid " |
2813
|
|
|
" FROM tbl_equipments e, tbl_equipments_commands ec, tbl_commands c " |
2814
|
|
|
" WHERE ec.equipment_id = e.id AND c.id = ec.command_id AND e.id = %s " |
2815
|
|
|
" ORDER BY c.id ") |
2816
|
|
|
cursor.execute(query, (id_,)) |
2817
|
|
|
rows = cursor.fetchall() |
2818
|
|
|
|
2819
|
|
|
command_result = list() |
2820
|
|
|
if rows is not None and len(rows) > 0: |
2821
|
|
|
for row in rows: |
2822
|
|
|
result = {"id": row[0], |
2823
|
|
|
"name": row[1], |
2824
|
|
|
"uuid": row[2]} |
2825
|
|
|
command_result.append(result) |
2826
|
|
|
meta_result['commands'] = command_result |
2827
|
|
|
|
2828
|
|
|
query = (" SELECT id, name, uuid " |
2829
|
|
|
" FROM tbl_energy_categories ") |
2830
|
|
|
cursor.execute(query) |
2831
|
|
|
rows_energy_categories = cursor.fetchall() |
2832
|
|
|
|
2833
|
|
|
energy_category_dict = dict() |
2834
|
|
|
if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
2835
|
|
|
for row in rows_energy_categories: |
2836
|
|
|
energy_category_dict[row[0]] = {"id": row[0], |
2837
|
|
|
"name": row[1], |
2838
|
|
|
"uuid": row[2]} |
2839
|
|
|
|
2840
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
2841
|
|
|
" FROM tbl_equipments e, tbl_equipments_meters em, tbl_meters m " |
2842
|
|
|
" WHERE em.equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
2843
|
|
|
" ORDER BY m.id ") |
2844
|
|
|
cursor.execute(query, (id_,)) |
2845
|
|
|
rows = cursor.fetchall() |
2846
|
|
|
|
2847
|
|
|
meter_result = list() |
2848
|
|
|
if rows is not None and len(rows) > 0: |
2849
|
|
|
for row in rows: |
2850
|
|
|
result = {"id": row[0], |
2851
|
|
|
"name": row[1], |
2852
|
|
|
"uuid": row[2], |
2853
|
|
|
"energy_category": energy_category_dict.get(row[3], None), |
2854
|
|
|
"is_output": bool(row[4])} |
2855
|
|
|
meter_result.append(result) |
2856
|
|
|
meta_result['meters'] = meter_result |
2857
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
2858
|
|
|
" FROM tbl_equipments e, tbl_equipments_offline_meters em, tbl_offline_meters m " |
2859
|
|
|
" WHERE em.equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
2860
|
|
|
" ORDER BY m.id ") |
2861
|
|
|
cursor.execute(query, (id_,)) |
2862
|
|
|
rows = cursor.fetchall() |
2863
|
|
|
|
2864
|
|
|
offlinemeter_result = list() |
2865
|
|
|
if rows is not None and len(rows) > 0: |
2866
|
|
|
for row in rows: |
2867
|
|
|
result = {"id": row[0], |
2868
|
|
|
"name": row[1], |
2869
|
|
|
"uuid": row[2], |
2870
|
|
|
"energy_category": energy_category_dict.get(row[3], None), |
2871
|
|
|
"is_output": bool(row[4])} |
2872
|
|
|
offlinemeter_result.append(result) |
2873
|
|
|
meta_result['offline_meters'] = offlinemeter_result |
2874
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
2875
|
|
|
" FROM tbl_equipments e, tbl_equipments_virtual_meters em, tbl_virtual_meters m " |
2876
|
|
|
" WHERE em.equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
2877
|
|
|
" ORDER BY m.id ") |
2878
|
|
|
cursor.execute(query, (id_,)) |
2879
|
|
|
rows = cursor.fetchall() |
2880
|
|
|
|
2881
|
|
|
virtualmeter_result = list() |
2882
|
|
|
if rows is not None and len(rows) > 0: |
2883
|
|
|
for row in rows: |
2884
|
|
|
result = {"id": row[0], |
2885
|
|
|
"name": row[1], |
2886
|
|
|
"uuid": row[2], |
2887
|
|
|
"energy_category": energy_category_dict.get(row[3], None), |
2888
|
|
|
"is_output": bool(row[4])} |
2889
|
|
|
virtualmeter_result.append(result) |
2890
|
|
|
meta_result['virtual_meters'] = virtualmeter_result |
2891
|
|
|
query = (" SELECT id, name " |
2892
|
|
|
" FROM tbl_points ") |
2893
|
|
|
cursor.execute(query) |
2894
|
|
|
rows_points = cursor.fetchall() |
2895
|
|
|
|
2896
|
|
|
point_dict = dict() |
2897
|
|
|
if rows_points is not None and len(rows_points) > 0: |
2898
|
|
|
for row in rows_points: |
2899
|
|
|
point_dict[row[0]] = {"id": row[0], |
2900
|
|
|
"name": row[1]} |
2901
|
|
|
|
2902
|
|
|
query = (" SELECT id, name, uuid " |
2903
|
|
|
" FROM tbl_meters ") |
2904
|
|
|
cursor.execute(query) |
2905
|
|
|
rows_meters = cursor.fetchall() |
2906
|
|
|
|
2907
|
|
|
meter_dict = dict() |
2908
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
2909
|
|
|
for row in rows_meters: |
2910
|
|
|
meter_dict[row[2]] = {"type": 'meter', |
2911
|
|
|
"id": row[0], |
2912
|
|
|
"name": row[1], |
2913
|
|
|
"uuid": row[2]} |
2914
|
|
|
|
2915
|
|
|
query = (" SELECT id, name, uuid " |
2916
|
|
|
" FROM tbl_offline_meters ") |
2917
|
|
|
cursor.execute(query) |
2918
|
|
|
rows_offline_meters = cursor.fetchall() |
2919
|
|
|
|
2920
|
|
|
offline_meter_dict = dict() |
2921
|
|
|
if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
2922
|
|
|
for row in rows_offline_meters: |
2923
|
|
|
offline_meter_dict[row[2]] = {"type": 'offline_meter', |
2924
|
|
|
"id": row[0], |
2925
|
|
|
"name": row[1], |
2926
|
|
|
"uuid": row[2]} |
2927
|
|
|
|
2928
|
|
|
query = (" SELECT id, name, uuid " |
2929
|
|
|
" FROM tbl_virtual_meters ") |
2930
|
|
|
cursor.execute(query) |
2931
|
|
|
rows_virtual_meters = cursor.fetchall() |
2932
|
|
|
|
2933
|
|
|
virtual_meter_dict = dict() |
2934
|
|
|
if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
2935
|
|
|
for row in rows_virtual_meters: |
2936
|
|
|
virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
2937
|
|
|
"id": row[0], |
2938
|
|
|
"name": row[1], |
2939
|
|
|
"uuid": row[2]} |
2940
|
|
|
|
2941
|
|
|
query = (" SELECT id, name, parameter_type, " |
2942
|
|
|
" constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
2943
|
|
|
" FROM tbl_equipments_parameters " |
2944
|
|
|
" WHERE equipment_id = %s " |
2945
|
|
|
" ORDER BY id ") |
2946
|
|
|
cursor.execute(query, (id_,)) |
2947
|
|
|
rows_parameters = cursor.fetchall() |
2948
|
|
|
|
2949
|
|
|
parameters_result = list() |
2950
|
|
|
if rows_parameters is not None and len(rows_parameters) > 0: |
2951
|
|
|
for row in rows_parameters: |
2952
|
|
|
constant = None |
2953
|
|
|
point = None |
2954
|
|
|
numerator_meter = None |
2955
|
|
|
denominator_meter = None |
2956
|
|
|
if row[2] == 'point': |
2957
|
|
|
point = point_dict.get(row[4], None) |
2958
|
|
|
constant = None |
2959
|
|
|
numerator_meter = None |
2960
|
|
|
denominator_meter = None |
2961
|
|
|
elif row[2] == 'constant': |
2962
|
|
|
constant = row[3] |
2963
|
|
|
point = None |
2964
|
|
|
numerator_meter = None |
2965
|
|
|
denominator_meter = None |
2966
|
|
|
elif row[2] == 'fraction': |
2967
|
|
|
constant = None |
2968
|
|
|
point = None |
2969
|
|
|
# find numerator meter by uuid |
2970
|
|
|
numerator_meter = meter_dict.get(row[5], None) |
2971
|
|
|
if numerator_meter is None: |
2972
|
|
|
numerator_meter = virtual_meter_dict.get(row[5], None) |
2973
|
|
|
if numerator_meter is None: |
2974
|
|
|
numerator_meter = offline_meter_dict.get(row[5], None) |
2975
|
|
|
# find denominator meter by uuid |
2976
|
|
|
denominator_meter = meter_dict.get(row[6], None) |
2977
|
|
|
if denominator_meter is None: |
2978
|
|
|
denominator_meter = virtual_meter_dict.get(row[6], None) |
2979
|
|
|
if denominator_meter is None: |
2980
|
|
|
denominator_meter = offline_meter_dict.get(row[6], None) |
2981
|
|
|
|
2982
|
|
|
result = {"id": row[0], |
2983
|
|
|
"name": row[1], |
2984
|
|
|
"parameter_type": row[2], |
2985
|
|
|
"constant": constant, |
2986
|
|
|
"point": point, |
2987
|
|
|
"numerator_meter": numerator_meter, |
2988
|
|
|
"denominator_meter": denominator_meter} |
2989
|
|
|
parameters_result.append(result) |
2990
|
|
|
meta_result['parameters'] = parameters_result |
2991
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
2992
|
|
|
if config.utc_offset[0] == '-': |
2993
|
|
|
timezone_offset = -timezone_offset |
2994
|
|
|
new_name = (str.strip(meta_result['name']) + |
2995
|
|
|
(datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')) |
2996
|
|
|
add_values = (" INSERT INTO tbl_equipments " |
2997
|
|
|
" (name, uuid, is_input_counted, is_output_counted, " |
2998
|
|
|
" cost_center_id, svg_id, camera_url, description) " |
2999
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
3000
|
|
|
cursor.execute(add_values, (new_name, |
3001
|
|
|
str(uuid.uuid4()), |
3002
|
|
|
meta_result['is_input_counted'], |
3003
|
|
|
meta_result['is_output_counted'], |
3004
|
|
|
meta_result['cost_center']['id'], |
3005
|
|
|
meta_result['svg_id'], |
3006
|
|
|
meta_result['camera_url'], |
3007
|
|
|
meta_result['description'])) |
3008
|
|
|
new_id = cursor.lastrowid |
3009
|
|
|
if meta_result['commands'] is not None and len(meta_result['commands']) > 0: |
3010
|
|
|
for command in meta_result['commands']: |
3011
|
|
|
cursor.execute(" SELECT name " |
3012
|
|
|
" FROM tbl_commands " |
3013
|
|
|
" WHERE id = %s ", (command['id'],)) |
3014
|
|
|
if cursor.fetchone() is None: |
3015
|
|
|
cursor.close() |
3016
|
|
|
cnx.close() |
3017
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3018
|
|
|
description='API.COMMAND_NOT_FOUND') |
3019
|
|
|
|
3020
|
|
|
query = (" SELECT id " |
3021
|
|
|
" FROM tbl_equipments_commands " |
3022
|
|
|
" WHERE equipment_id = %s AND command_id = %s") |
3023
|
|
|
cursor.execute(query, (new_id, command['id'],)) |
3024
|
|
|
if cursor.fetchone() is not None: |
3025
|
|
|
cursor.close() |
3026
|
|
|
cnx.close() |
3027
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
3028
|
|
|
description='API.EQUIPMENT_COMMAND_RELATION_EXISTS') |
3029
|
|
|
|
3030
|
|
|
add_row = (" INSERT INTO tbl_equipments_commands (equipment_id, command_id) " |
3031
|
|
|
" VALUES (%s, %s) ") |
3032
|
|
|
cursor.execute(add_row, (new_id, command['id'],)) |
3033
|
|
|
if meta_result['meters'] is not None and len(meta_result['meters']) > 0: |
3034
|
|
|
for meter in meta_result['meters']: |
3035
|
|
|
cursor.execute(" SELECT name " |
3036
|
|
|
" FROM tbl_meters " |
3037
|
|
|
" WHERE id = %s ", (meter['id'],)) |
3038
|
|
|
if cursor.fetchone() is None: |
3039
|
|
|
cursor.close() |
3040
|
|
|
cnx.close() |
3041
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3042
|
|
|
description='API.METER_NOT_FOUND') |
3043
|
|
|
|
3044
|
|
|
query = (" SELECT id " |
3045
|
|
|
" FROM tbl_equipments_meters " |
3046
|
|
|
" WHERE equipment_id = %s AND meter_id = %s") |
3047
|
|
|
cursor.execute(query, (new_id, meter['id'],)) |
3048
|
|
|
if cursor.fetchone() is not None: |
3049
|
|
|
cursor.close() |
3050
|
|
|
cnx.close() |
3051
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
3052
|
|
|
description='API.EQUIPMENT_METER_RELATION_EXISTS') |
3053
|
|
|
|
3054
|
|
|
add_row = (" INSERT INTO tbl_equipments_meters (equipment_id, meter_id, is_output ) " |
3055
|
|
|
" VALUES (%s, %s, %s) ") |
3056
|
|
|
cursor.execute(add_row, (new_id, meter['id'], meter['is_output'])) |
3057
|
|
|
if meta_result['offline_meters'] is not None and len(meta_result['offline_meters']) > 0: |
3058
|
|
|
for offline_meter in meta_result['offline_meters']: |
3059
|
|
|
cursor.execute(" SELECT name " |
3060
|
|
|
" FROM tbl_offline_meters " |
3061
|
|
|
" WHERE id = %s ", (offline_meter['id'],)) |
3062
|
|
|
if cursor.fetchone() is None: |
3063
|
|
|
cursor.close() |
3064
|
|
|
cnx.close() |
3065
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3066
|
|
|
description='API.OFFLINE_METER_NOT_FOUND') |
3067
|
|
|
|
3068
|
|
|
query = (" SELECT id " |
3069
|
|
|
" FROM tbl_equipments_offline_meters " |
3070
|
|
|
" WHERE equipment_id = %s AND offline_meter_id = %s") |
3071
|
|
|
cursor.execute(query, (new_id, offline_meter['id'],)) |
3072
|
|
|
if cursor.fetchone() is not None: |
3073
|
|
|
cursor.close() |
3074
|
|
|
cnx.close() |
3075
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
3076
|
|
|
description='API.EQUIPMENT_OFFLINE_METER_RELATION_EXISTS') |
3077
|
|
|
|
3078
|
|
|
add_row = ( |
3079
|
|
|
" INSERT INTO tbl_equipments_offline_meters (equipment_id, offline_meter_id, is_output ) " |
3080
|
|
|
" VALUES (%s, %s, %s) ") |
3081
|
|
|
cursor.execute(add_row, (new_id, offline_meter['id'], offline_meter['is_output'])) |
3082
|
|
|
if meta_result['virtual_meters'] is not None and len(meta_result['virtual_meters']) > 0: |
3083
|
|
|
for virtual_meter in meta_result['virtual_meters']: |
3084
|
|
|
cursor.execute(" SELECT name " |
3085
|
|
|
" FROM tbl_virtual_meters " |
3086
|
|
|
" WHERE id = %s ", (virtual_meter['id'],)) |
3087
|
|
|
if cursor.fetchone() is None: |
3088
|
|
|
cursor.close() |
3089
|
|
|
cnx.close() |
3090
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3091
|
|
|
description='API.VIRTUAL_METER_NOT_FOUND') |
3092
|
|
|
|
3093
|
|
|
query = (" SELECT id " |
3094
|
|
|
" FROM tbl_equipments_virtual_meters " |
3095
|
|
|
" WHERE equipment_id = %s AND virtual_meter_id = %s") |
3096
|
|
|
cursor.execute(query, (new_id, virtual_meter['id'],)) |
3097
|
|
|
if cursor.fetchone() is not None: |
3098
|
|
|
cursor.close() |
3099
|
|
|
cnx.close() |
3100
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
3101
|
|
|
description='API.EQUIPMENT_VIRTUAL_METER_RELATION_EXISTS') |
3102
|
|
|
|
3103
|
|
|
add_row = ( |
3104
|
|
|
" INSERT INTO tbl_equipments_virtual_meters (equipment_id, virtual_meter_id, is_output ) " |
3105
|
|
|
" VALUES (%s, %s, %s) ") |
3106
|
|
|
cursor.execute(add_row, (new_id, virtual_meter['id'], virtual_meter['is_output'])) |
3107
|
|
View Code Duplication |
if meta_result['parameters'] is not None and len(meta_result['parameters']) > 0: |
|
|
|
|
3108
|
|
|
for parameters in meta_result['parameters']: |
3109
|
|
|
cursor.execute(" SELECT name " |
3110
|
|
|
" FROM tbl_equipments_parameters " |
3111
|
|
|
" WHERE name = %s AND equipment_id = %s ", (parameters['name'], new_id)) |
3112
|
|
|
if cursor.fetchone() is not None: |
3113
|
|
|
cursor.close() |
3114
|
|
|
cnx.close() |
3115
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3116
|
|
|
description='API.EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
3117
|
|
|
if 'point' in parameters: |
3118
|
|
|
if parameters['point'] is None: |
3119
|
|
|
point_id = None |
3120
|
|
|
elif parameters['point']['id'] is not None and \ |
3121
|
|
|
parameters['point']['id'] <= 0: |
3122
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3123
|
|
|
description='API.INVALID_POINT_ID') |
3124
|
|
|
else: |
3125
|
|
|
point_id = parameters['point']['id'] |
3126
|
|
|
else: |
3127
|
|
|
point_id = None |
3128
|
|
|
numerator_meter_uuid = None |
3129
|
|
|
if 'numerator_meter' in parameters: |
3130
|
|
|
if parameters['numerator_meter'] is not None and \ |
3131
|
|
|
isinstance(parameters['numerator_meter']['uuid'], str) and \ |
3132
|
|
|
len(str.strip(parameters['numerator_meter']['uuid'])) > 0: |
3133
|
|
|
numerator_meter_uuid = str.strip(parameters['numerator_meter']['uuid']) |
3134
|
|
|
|
3135
|
|
|
denominator_meter_uuid = None |
3136
|
|
|
if 'denominator_meter' in parameters: |
3137
|
|
|
if parameters['denominator_meter'] is not None and \ |
3138
|
|
|
isinstance(parameters['denominator_meter']['uuid'], str) and \ |
3139
|
|
|
len(str.strip(parameters['denominator_meter']['uuid'])) > 0: |
3140
|
|
|
denominator_meter_uuid = str.strip(parameters['denominator_meter']['uuid']) |
3141
|
|
|
|
3142
|
|
|
# validate by parameter type |
3143
|
|
|
if parameters['parameter_type'] == 'point': |
3144
|
|
|
if point_id is None: |
3145
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3146
|
|
|
description='API.INVALID_POINT_ID') |
3147
|
|
|
query = (" SELECT id, name " |
3148
|
|
|
" FROM tbl_points " |
3149
|
|
|
" WHERE id = %s ") |
3150
|
|
|
cursor.execute(query, (point_id,)) |
3151
|
|
|
if cursor.fetchone() is None: |
3152
|
|
|
cursor.close() |
3153
|
|
|
cnx.close() |
3154
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3155
|
|
|
description='API.POINT_NOT_FOUND') |
3156
|
|
|
|
3157
|
|
|
elif parameters['parameter_type'] == 'constant': |
3158
|
|
|
if parameters['constant'] is None: |
3159
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3160
|
|
|
description='API.INVALID_CONSTANT_VALUE') |
3161
|
|
|
|
3162
|
|
|
elif parameters['parameter_type'] == 'fraction': |
3163
|
|
|
query = (" SELECT id, name, uuid " |
3164
|
|
|
" FROM tbl_meters ") |
3165
|
|
|
cursor.execute(query) |
3166
|
|
|
rows_meters = cursor.fetchall() |
3167
|
|
|
meter_dict = dict() |
3168
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
3169
|
|
|
for row in rows_meters: |
3170
|
|
|
meter_dict[row[2]] = {"type": 'meter', |
3171
|
|
|
"id": row[0], |
3172
|
|
|
"name": row[1], |
3173
|
|
|
"uuid": row[2]} |
3174
|
|
|
|
3175
|
|
|
query = (" SELECT id, name, uuid " |
3176
|
|
|
" FROM tbl_offline_meters ") |
3177
|
|
|
cursor.execute(query) |
3178
|
|
|
rows_offline_meters = cursor.fetchall() |
3179
|
|
|
|
3180
|
|
|
offline_meter_dict = dict() |
3181
|
|
|
if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
3182
|
|
|
for row in rows_offline_meters: |
3183
|
|
|
offline_meter_dict[row[2]] = {"type": 'offline_meter', |
3184
|
|
|
"id": row[0], |
3185
|
|
|
"name": row[1], |
3186
|
|
|
"uuid": row[2]} |
3187
|
|
|
|
3188
|
|
|
query = (" SELECT id, name, uuid " |
3189
|
|
|
" FROM tbl_virtual_meters ") |
3190
|
|
|
cursor.execute(query) |
3191
|
|
|
rows_virtual_meters = cursor.fetchall() |
3192
|
|
|
|
3193
|
|
|
virtual_meter_dict = dict() |
3194
|
|
|
if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
3195
|
|
|
for row in rows_virtual_meters: |
3196
|
|
|
virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
3197
|
|
|
"id": row[0], |
3198
|
|
|
"name": row[1], |
3199
|
|
|
"uuid": row[2]} |
3200
|
|
|
|
3201
|
|
|
# validate numerator meter uuid |
3202
|
|
|
if numerator_meter_uuid is None: |
3203
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3204
|
|
|
description='API.INVALID_NUMERATOR_METER_UUID') |
3205
|
|
|
|
3206
|
|
|
if meter_dict.get(numerator_meter_uuid) is None and \ |
3207
|
|
|
virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
3208
|
|
|
offline_meter_dict.get(numerator_meter_uuid) is None: |
3209
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3210
|
|
|
description='API.INVALID_NUMERATOR_METER_UUID') |
3211
|
|
|
|
3212
|
|
|
# validate denominator meter uuid |
3213
|
|
|
if denominator_meter_uuid == numerator_meter_uuid: |
3214
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3215
|
|
|
description='API.INVALID_DENOMINATOR_METER_UUID') |
3216
|
|
|
|
3217
|
|
|
if denominator_meter_uuid not in meter_dict and \ |
3218
|
|
|
denominator_meter_uuid not in virtual_meter_dict and \ |
3219
|
|
|
denominator_meter_uuid not in offline_meter_dict: |
3220
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3221
|
|
|
description='API.INVALID_DENOMINATOR_METER_UUID') |
3222
|
|
|
|
3223
|
|
|
add_values = (" INSERT INTO tbl_equipments_parameters " |
3224
|
|
|
" (equipment_id, name, parameter_type, constant, " |
3225
|
|
|
" point_id, numerator_meter_uuid, denominator_meter_uuid) " |
3226
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
3227
|
|
|
cursor.execute(add_values, (new_id, |
3228
|
|
|
parameters['name'], |
3229
|
|
|
parameters['parameter_type'], |
3230
|
|
|
parameters['constant'], |
3231
|
|
|
point_id, |
3232
|
|
|
numerator_meter_uuid, |
3233
|
|
|
denominator_meter_uuid)) |
3234
|
|
|
cnx.commit() |
3235
|
|
|
cursor.close() |
3236
|
|
|
cnx.close() |
3237
|
|
|
|
3238
|
|
|
resp.status = falcon.HTTP_201 |
3239
|
|
|
resp.location = '/equipments/' + str(new_id) |
3240
|
|
|
|