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