1
|
|
|
import falcon |
2
|
|
|
import simplejson as json |
3
|
|
|
import mysql.connector |
4
|
|
|
import config |
5
|
|
|
import uuid |
6
|
|
|
|
7
|
|
|
|
8
|
|
View Code Duplication |
class EquipmentCollection: |
|
|
|
|
9
|
|
|
@staticmethod |
10
|
|
|
def __init__(): |
11
|
|
|
pass |
12
|
|
|
|
13
|
|
|
@staticmethod |
14
|
|
|
def on_options(req, resp): |
15
|
|
|
resp.status = falcon.HTTP_200 |
16
|
|
|
|
17
|
|
|
@staticmethod |
18
|
|
|
def on_get(req, resp): |
19
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
20
|
|
|
cursor = cnx.cursor(dictionary=True) |
21
|
|
|
|
22
|
|
|
query = (" SELECT id, name, uuid " |
23
|
|
|
" FROM tbl_cost_centers ") |
24
|
|
|
cursor.execute(query) |
25
|
|
|
rows_cost_centers = cursor.fetchall() |
26
|
|
|
|
27
|
|
|
cost_center_dict = dict() |
28
|
|
|
if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
29
|
|
|
for row in rows_cost_centers: |
30
|
|
|
cost_center_dict[row['id']] = {"id": row['id'], |
31
|
|
|
"name": row['name'], |
32
|
|
|
"uuid": row['uuid']} |
33
|
|
|
|
34
|
|
|
query = (" SELECT id, name, uuid, " |
35
|
|
|
" is_input_counted, is_output_counted, " |
36
|
|
|
" cost_center_id, description " |
37
|
|
|
" FROM tbl_equipments " |
38
|
|
|
" ORDER BY id ") |
39
|
|
|
cursor.execute(query) |
40
|
|
|
rows_equipments = cursor.fetchall() |
41
|
|
|
|
42
|
|
|
result = list() |
43
|
|
|
if rows_equipments is not None and len(rows_equipments) > 0: |
44
|
|
|
for row in rows_equipments: |
45
|
|
|
cost_center = cost_center_dict.get(row['cost_center_id'], None) |
46
|
|
|
meta_result = {"id": row['id'], |
47
|
|
|
"name": row['name'], |
48
|
|
|
"uuid": row['uuid'], |
49
|
|
|
"is_input_counted": bool(row['is_input_counted']), |
50
|
|
|
"is_output_counted": bool(row['is_output_counted']), |
51
|
|
|
"cost_center": cost_center, |
52
|
|
|
"description": row['description']} |
53
|
|
|
result.append(meta_result) |
54
|
|
|
|
55
|
|
|
cursor.close() |
56
|
|
|
cnx.disconnect() |
57
|
|
|
resp.body = json.dumps(result) |
58
|
|
|
|
59
|
|
|
@staticmethod |
60
|
|
|
def on_post(req, resp): |
61
|
|
|
"""Handles POST requests""" |
62
|
|
|
try: |
63
|
|
|
raw_json = req.stream.read().decode('utf-8') |
64
|
|
|
except Exception as ex: |
65
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
66
|
|
|
|
67
|
|
|
new_values = json.loads(raw_json) |
68
|
|
|
|
69
|
|
|
if 'name' not in new_values['data'].keys() or \ |
70
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
71
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
72
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
73
|
|
|
description='API.INVALID_EQUIPMENT_NAME') |
74
|
|
|
name = str.strip(new_values['data']['name']) |
75
|
|
|
|
76
|
|
|
if 'is_input_counted' not in new_values['data'].keys() or \ |
77
|
|
|
not isinstance(new_values['data']['is_input_counted'], bool): |
78
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
79
|
|
|
description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
80
|
|
|
is_input_counted = new_values['data']['is_input_counted'] |
81
|
|
|
|
82
|
|
|
if 'is_output_counted' not in new_values['data'].keys() or \ |
83
|
|
|
not isinstance(new_values['data']['is_output_counted'], bool): |
84
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
85
|
|
|
description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
86
|
|
|
is_output_counted = new_values['data']['is_output_counted'] |
87
|
|
|
|
88
|
|
|
if 'cost_center_id' not in new_values['data'].keys() or \ |
89
|
|
|
not isinstance(new_values['data']['cost_center_id'], int) or \ |
90
|
|
|
new_values['data']['cost_center_id'] <= 0: |
91
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
92
|
|
|
description='API.INVALID_COST_CENTER_ID') |
93
|
|
|
cost_center_id = new_values['data']['cost_center_id'] |
94
|
|
|
|
95
|
|
|
if 'description' in new_values['data'].keys() and \ |
96
|
|
|
new_values['data']['description'] is not None and \ |
97
|
|
|
len(str(new_values['data']['description'])) > 0: |
98
|
|
|
description = str.strip(new_values['data']['description']) |
99
|
|
|
else: |
100
|
|
|
description = None |
101
|
|
|
|
102
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
103
|
|
|
cursor = cnx.cursor() |
104
|
|
|
|
105
|
|
|
cursor.execute(" SELECT name " |
106
|
|
|
" FROM tbl_equipments " |
107
|
|
|
" WHERE name = %s ", (name,)) |
108
|
|
|
if cursor.fetchone() is not None: |
109
|
|
|
cursor.close() |
110
|
|
|
cnx.disconnect() |
111
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
112
|
|
|
description='API.EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
113
|
|
|
|
114
|
|
|
if cost_center_id is not None: |
115
|
|
|
cursor.execute(" SELECT name " |
116
|
|
|
" FROM tbl_cost_centers " |
117
|
|
|
" WHERE id = %s ", |
118
|
|
|
(new_values['data']['cost_center_id'],)) |
119
|
|
|
row = cursor.fetchone() |
120
|
|
|
if row is None: |
121
|
|
|
cursor.close() |
122
|
|
|
cnx.disconnect() |
123
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
124
|
|
|
description='API.COST_CENTER_NOT_FOUND') |
125
|
|
|
|
126
|
|
|
add_values = (" INSERT INTO tbl_equipments " |
127
|
|
|
" (name, uuid, is_input_counted, is_output_counted, " |
128
|
|
|
" cost_center_id, description) " |
129
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s) ") |
130
|
|
|
cursor.execute(add_values, (name, |
131
|
|
|
str(uuid.uuid4()), |
132
|
|
|
is_input_counted, |
133
|
|
|
is_output_counted, |
134
|
|
|
cost_center_id, |
135
|
|
|
description)) |
136
|
|
|
new_id = cursor.lastrowid |
137
|
|
|
cnx.commit() |
138
|
|
|
cursor.close() |
139
|
|
|
cnx.disconnect() |
140
|
|
|
|
141
|
|
|
resp.status = falcon.HTTP_201 |
142
|
|
|
resp.location = '/equipments/' + str(new_id) |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
class EquipmentItem: |
146
|
|
|
@staticmethod |
147
|
|
|
def __init__(): |
148
|
|
|
pass |
149
|
|
|
|
150
|
|
|
@staticmethod |
151
|
|
|
def on_options(req, resp, id_): |
152
|
|
|
resp.status = falcon.HTTP_200 |
153
|
|
|
|
154
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
155
|
|
|
def on_get(req, resp, id_): |
156
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
157
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
158
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
159
|
|
|
|
160
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
161
|
|
|
cursor = cnx.cursor(dictionary=True) |
162
|
|
|
|
163
|
|
|
query = (" SELECT id, name, uuid " |
164
|
|
|
" FROM tbl_cost_centers ") |
165
|
|
|
cursor.execute(query) |
166
|
|
|
rows_cost_centers = cursor.fetchall() |
167
|
|
|
|
168
|
|
|
cost_center_dict = dict() |
169
|
|
|
if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
170
|
|
|
for row in rows_cost_centers: |
171
|
|
|
cost_center_dict[row['id']] = {"id": row['id'], |
172
|
|
|
"name": row['name'], |
173
|
|
|
"uuid": row['uuid']} |
174
|
|
|
|
175
|
|
|
query = (" SELECT id, name, uuid, " |
176
|
|
|
" is_input_counted, is_output_counted, " |
177
|
|
|
" cost_center_id, description " |
178
|
|
|
" FROM tbl_equipments " |
179
|
|
|
" WHERE id = %s ") |
180
|
|
|
cursor.execute(query, (id_,)) |
181
|
|
|
row = cursor.fetchone() |
182
|
|
|
cursor.close() |
183
|
|
|
cnx.disconnect() |
184
|
|
|
|
185
|
|
|
if row is None: |
186
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
187
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
188
|
|
|
else: |
189
|
|
|
cost_center = cost_center_dict.get(row['cost_center_id'], None) |
190
|
|
|
meta_result = {"id": row['id'], |
191
|
|
|
"name": row['name'], |
192
|
|
|
"uuid": row['uuid'], |
193
|
|
|
"is_input_counted": bool(row['is_input_counted']), |
194
|
|
|
"is_output_counted": bool(row['is_output_counted']), |
195
|
|
|
"cost_center": cost_center, |
196
|
|
|
"description": row['description']} |
197
|
|
|
|
198
|
|
|
resp.body = json.dumps(meta_result) |
199
|
|
|
|
200
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
201
|
|
|
def on_delete(req, resp, id_): |
202
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
203
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
204
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
205
|
|
|
|
206
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
207
|
|
|
cursor = cnx.cursor() |
208
|
|
|
|
209
|
|
|
# check relation with space |
210
|
|
|
cursor.execute(" SELECT space_id " |
211
|
|
|
" FROM tbl_spaces_equipments " |
212
|
|
|
" WHERE equipment_id = %s ", |
213
|
|
|
(id_,)) |
214
|
|
|
rows_equipments = cursor.fetchall() |
215
|
|
|
if rows_equipments is not None and len(rows_equipments) > 0: |
216
|
|
|
cursor.close() |
217
|
|
|
cnx.disconnect() |
218
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, |
219
|
|
|
title='API.BAD_REQUEST', |
220
|
|
|
description='API.THERE_IS_RELATION_WITH_SPACE') |
221
|
|
|
|
222
|
|
|
# check relation with combined equipments |
223
|
|
|
cursor.execute(" SELECT combined_equipment_id " |
224
|
|
|
" FROM tbl_combined_equipments_equipments " |
225
|
|
|
" WHERE equipment_id = %s ", |
226
|
|
|
(id_,)) |
227
|
|
|
rows_combined_equipments = cursor.fetchall() |
228
|
|
|
if rows_combined_equipments is not None and len(rows_combined_equipments) > 0: |
229
|
|
|
cursor.close() |
230
|
|
|
cnx.disconnect() |
231
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, |
232
|
|
|
title='API.BAD_REQUEST', |
233
|
|
|
description='API.THERE_IS_RELATION_WITH_COMBINED_EQUIPMENTS') |
234
|
|
|
|
235
|
|
|
# check relation with meter |
236
|
|
|
cursor.execute(" SELECT meter_id " |
237
|
|
|
" FROM tbl_equipments_meters " |
238
|
|
|
" WHERE equipment_id = %s ", |
239
|
|
|
(id_,)) |
240
|
|
|
rows_meters = cursor.fetchall() |
241
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
242
|
|
|
cursor.close() |
243
|
|
|
cnx.disconnect() |
244
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, |
245
|
|
|
title='API.BAD_REQUEST', |
246
|
|
|
description='API.THERE_IS_RELATION_WITH_METER') |
247
|
|
|
|
248
|
|
|
# check relation with offline meter |
249
|
|
|
cursor.execute(" SELECT offline_meter_id " |
250
|
|
|
" FROM tbl_equipments_offline_meters " |
251
|
|
|
" WHERE equipment_id = %s ", |
252
|
|
|
(id_,)) |
253
|
|
|
rows_offline_meters = cursor.fetchall() |
254
|
|
|
if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
255
|
|
|
cursor.close() |
256
|
|
|
cnx.disconnect() |
257
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, |
258
|
|
|
title='API.BAD_REQUEST', |
259
|
|
|
description='API.THERE_IS_RELATION_WITH_OFFLINE_METER') |
260
|
|
|
|
261
|
|
|
# check relation with virtual meter |
262
|
|
|
cursor.execute(" SELECT virtual_meter_id " |
263
|
|
|
" FROM tbl_equipments_virtual_meters " |
264
|
|
|
" WHERE equipment_id = %s ", |
265
|
|
|
(id_,)) |
266
|
|
|
rows_virtual_meters = cursor.fetchall() |
267
|
|
|
if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
268
|
|
|
cursor.close() |
269
|
|
|
cnx.disconnect() |
270
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, |
271
|
|
|
title='API.BAD_REQUEST', |
272
|
|
|
description='API.THERE_IS_RELATION_WITH_VIRTUAL_METER') |
273
|
|
|
|
274
|
|
|
# delete all associated parameters |
275
|
|
|
cursor.execute(" DELETE FROM tbl_equipments_parameters WHERE equipment_id = %s ", (id_,)) |
276
|
|
|
cnx.commit() |
277
|
|
|
|
278
|
|
|
cursor.execute(" DELETE FROM tbl_equipments WHERE id = %s ", (id_,)) |
279
|
|
|
cnx.commit() |
280
|
|
|
|
281
|
|
|
cursor.close() |
282
|
|
|
cnx.disconnect() |
283
|
|
|
|
284
|
|
|
resp.status = falcon.HTTP_204 |
285
|
|
|
|
286
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
287
|
|
|
def on_put(req, resp, id_): |
288
|
|
|
"""Handles PUT requests""" |
289
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
290
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
291
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
292
|
|
|
try: |
293
|
|
|
raw_json = req.stream.read().decode('utf-8') |
294
|
|
|
except Exception as ex: |
295
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
296
|
|
|
|
297
|
|
|
new_values = json.loads(raw_json) |
298
|
|
|
|
299
|
|
|
if 'name' not in new_values['data'].keys() or \ |
300
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
301
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
302
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
303
|
|
|
description='API.INVALID_EQUIPMENT_NAME') |
304
|
|
|
name = str.strip(new_values['data']['name']) |
305
|
|
|
|
306
|
|
|
if 'is_input_counted' not in new_values['data'].keys() or \ |
307
|
|
|
not isinstance(new_values['data']['is_input_counted'], bool): |
308
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
309
|
|
|
description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
310
|
|
|
is_input_counted = new_values['data']['is_input_counted'] |
311
|
|
|
|
312
|
|
|
if 'is_output_counted' not in new_values['data'].keys() or \ |
313
|
|
|
not isinstance(new_values['data']['is_output_counted'], bool): |
314
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
315
|
|
|
description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
316
|
|
|
is_output_counted = new_values['data']['is_output_counted'] |
317
|
|
|
|
318
|
|
|
if 'cost_center_id' not in new_values['data'].keys() or \ |
319
|
|
|
not isinstance(new_values['data']['cost_center_id'], int) or \ |
320
|
|
|
new_values['data']['cost_center_id'] <= 0: |
321
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
322
|
|
|
description='API.INVALID_COST_CENTER_ID') |
323
|
|
|
cost_center_id = new_values['data']['cost_center_id'] |
324
|
|
|
|
325
|
|
|
if 'description' in new_values['data'].keys() and \ |
326
|
|
|
new_values['data']['description'] is not None and \ |
327
|
|
|
len(str(new_values['data']['description'])) > 0: |
328
|
|
|
description = str.strip(new_values['data']['description']) |
329
|
|
|
else: |
330
|
|
|
description = None |
331
|
|
|
|
332
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
333
|
|
|
cursor = cnx.cursor() |
334
|
|
|
|
335
|
|
|
cursor.execute(" SELECT name " |
336
|
|
|
" FROM tbl_equipments " |
337
|
|
|
" WHERE id = %s ", (id_,)) |
338
|
|
|
if cursor.fetchone() is None: |
339
|
|
|
cursor.close() |
340
|
|
|
cnx.disconnect() |
341
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
342
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
343
|
|
|
|
344
|
|
|
cursor.execute(" SELECT name " |
345
|
|
|
" FROM tbl_equipments " |
346
|
|
|
" WHERE name = %s AND id != %s ", (name, id_)) |
347
|
|
|
if cursor.fetchone() is not None: |
348
|
|
|
cursor.close() |
349
|
|
|
cnx.disconnect() |
350
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
351
|
|
|
description='API.EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
352
|
|
|
|
353
|
|
|
cursor.execute(" SELECT name " |
354
|
|
|
" FROM tbl_cost_centers " |
355
|
|
|
" WHERE id = %s ", |
356
|
|
|
(new_values['data']['cost_center_id'],)) |
357
|
|
|
row = cursor.fetchone() |
358
|
|
|
if row is None: |
359
|
|
|
cursor.close() |
360
|
|
|
cnx.disconnect() |
361
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
362
|
|
|
description='API.COST_CENTER_NOT_FOUND') |
363
|
|
|
|
364
|
|
|
update_row = (" UPDATE tbl_equipments " |
365
|
|
|
" SET name = %s, is_input_counted = %s, is_output_counted = %s, " |
366
|
|
|
" cost_center_id = %s, description = %s " |
367
|
|
|
" WHERE id = %s ") |
368
|
|
|
cursor.execute(update_row, (name, |
369
|
|
|
is_input_counted, |
370
|
|
|
is_output_counted, |
371
|
|
|
cost_center_id, |
372
|
|
|
description, |
373
|
|
|
id_)) |
374
|
|
|
cnx.commit() |
375
|
|
|
|
376
|
|
|
cursor.close() |
377
|
|
|
cnx.disconnect() |
378
|
|
|
|
379
|
|
|
resp.status = falcon.HTTP_200 |
380
|
|
|
|
381
|
|
|
# Clone an Equipment |
382
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
383
|
|
|
def on_post(req, resp, id_): |
384
|
|
|
"""Handles PUT requests""" |
385
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
386
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
387
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
388
|
|
|
try: |
389
|
|
|
raw_json = req.stream.read().decode('utf-8') |
390
|
|
|
except Exception as ex: |
391
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
392
|
|
|
|
393
|
|
|
new_values = json.loads(raw_json) |
394
|
|
|
|
395
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
396
|
|
|
cursor = cnx.cursor(dictionary=True) |
397
|
|
|
cursor.execute(" SELECT name " |
398
|
|
|
" FROM tbl_equipments " |
399
|
|
|
" WHERE id = %s ", (id_,)) |
400
|
|
|
if cursor.fetchone() is None: |
401
|
|
|
cursor.close() |
402
|
|
|
cnx.disconnect() |
403
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
404
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
405
|
|
|
|
406
|
|
|
query = (" SELECT name, is_input_counted, is_output_counted, " |
407
|
|
|
" cost_center_id, description " |
408
|
|
|
" FROM tbl_equipments " |
409
|
|
|
" WHERE id = %s ") |
410
|
|
|
cursor.execute(query, (id_,)) |
411
|
|
|
row = cursor.fetchone() |
412
|
|
|
|
413
|
|
|
if row is None: |
414
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
415
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
416
|
|
|
else: |
417
|
|
|
|
418
|
|
|
add_values = (" INSERT INTO tbl_equipments " |
419
|
|
|
" (name, uuid, is_input_counted, is_output_counted, " |
420
|
|
|
" cost_center_id, description) " |
421
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s) ") |
422
|
|
|
cursor.execute(add_values, (row['name'] + ' Copy', |
423
|
|
|
str(uuid.uuid4()), |
424
|
|
|
row['is_input_counted'], |
425
|
|
|
row['is_output_counted'], |
426
|
|
|
row['cost_center_id'], |
427
|
|
|
row['description'])) |
428
|
|
|
new_id = cursor.lastrowid |
429
|
|
|
cnx.commit() |
430
|
|
|
|
431
|
|
|
# clone relation with meter |
432
|
|
|
cursor.execute(" SELECT meter_id, is_output " |
433
|
|
|
" FROM tbl_equipments_meters " |
434
|
|
|
" WHERE equipment_id = %s ", |
435
|
|
|
(id_,)) |
436
|
|
|
rows_meters = cursor.fetchall() |
437
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
438
|
|
|
add_values = (" INSERT INTO tbl_equipments_meters (equipment_id, meter_id, is_output) " |
439
|
|
|
" VALUES ") |
440
|
|
|
for row in rows_meters: |
441
|
|
|
add_values += " (" + str(new_id) + "," |
442
|
|
|
add_values += str(row['meter_id']) + "," |
443
|
|
|
add_values += str(bool(row['is_output'])) + "), " |
444
|
|
|
# trim ", " at the end of string and then execute |
445
|
|
|
cursor.execute(add_values[:-2]) |
446
|
|
|
cnx.commit() |
447
|
|
|
|
448
|
|
|
# clone relation with offline meter |
449
|
|
|
cursor.execute(" SELECT offline_meter_id, is_output " |
450
|
|
|
" FROM tbl_equipments_offline_meters " |
451
|
|
|
" WHERE equipment_id = %s ", |
452
|
|
|
(id_,)) |
453
|
|
|
rows_offline_meters = cursor.fetchall() |
454
|
|
|
if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
455
|
|
|
add_values = (" INSERT INTO tbl_equipments_offline_meters (equipment_id, offline_meter_id, is_output) " |
456
|
|
|
" VALUES ") |
457
|
|
|
for row in rows_offline_meters: |
458
|
|
|
add_values += " (" + str(new_id) + "," |
459
|
|
|
add_values += "'" + str(row['offline_meter_id']) + "'," |
460
|
|
|
add_values += str(bool(row['is_output'])) + "), " |
461
|
|
|
# trim ", " at the end of string and then execute |
462
|
|
|
cursor.execute(add_values[:-2]) |
463
|
|
|
cnx.commit() |
464
|
|
|
|
465
|
|
|
# clone relation with virtual meter |
466
|
|
|
cursor.execute(" SELECT virtual_meter_id, is_output " |
467
|
|
|
" FROM tbl_equipments_virtual_meters " |
468
|
|
|
" WHERE equipment_id = %s ", |
469
|
|
|
(id_,)) |
470
|
|
|
rows_virtual_meters = cursor.fetchall() |
471
|
|
|
if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
472
|
|
|
add_values = (" INSERT INTO tbl_equipments_virtual_meters (equipment_id, virtual_meter_id, is_output) " |
473
|
|
|
" VALUES ") |
474
|
|
|
for row in rows_virtual_meters: |
475
|
|
|
add_values += " (" + str(new_id) + "," |
476
|
|
|
add_values += str(row['virtual_meter_id']) + "," |
477
|
|
|
add_values += str(bool(row['is_output'])) + "), " |
478
|
|
|
# trim ", " at the end of string and then execute |
479
|
|
|
cursor.execute(add_values[:-2]) |
480
|
|
|
cnx.commit() |
481
|
|
|
|
482
|
|
|
# clone parameters |
483
|
|
|
cursor.execute(" SELECT name, parameter_type, constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
484
|
|
|
" FROM tbl_equipments_parameters " |
485
|
|
|
" WHERE equipment_id = %s ", |
486
|
|
|
(id_,)) |
487
|
|
|
rows_parameters = cursor.fetchall() |
488
|
|
|
if rows_parameters is not None and len(rows_parameters) > 0: |
489
|
|
|
add_values = (" INSERT INTO tbl_equipments_parameters" |
490
|
|
|
" (equipment_id, name, parameter_type, constant, point_id, " |
491
|
|
|
" numerator_meter_uuid, denominator_meter_uuid) " |
492
|
|
|
" VALUES ") |
493
|
|
|
for row in rows_parameters: |
494
|
|
|
add_values += " (" + str(new_id) + "," |
495
|
|
|
add_values += "'" + str(row['name']) + "'," |
496
|
|
|
add_values += "'" + str(row['parameter_type']) + "'," |
497
|
|
|
if row['constant'] is not None: |
498
|
|
|
add_values += "'" + str(row['constant']) + "'," |
499
|
|
|
else: |
500
|
|
|
add_values += "null, " |
501
|
|
|
|
502
|
|
|
if row['point_id'] is not None: |
503
|
|
|
add_values += str(row['point_id']) + "," |
504
|
|
|
else: |
505
|
|
|
add_values += "null, " |
506
|
|
|
|
507
|
|
|
if row['numerator_meter_uuid'] is not None: |
508
|
|
|
add_values += "'" + row['numerator_meter_uuid'] + "'," |
509
|
|
|
else: |
510
|
|
|
add_values += "null, " |
511
|
|
|
if row['denominator_meter_uuid'] is not None: |
512
|
|
|
add_values += "'" + row['denominator_meter_uuid'] + "'), " |
513
|
|
|
else: |
514
|
|
|
add_values += "null), " |
515
|
|
|
|
516
|
|
|
# trim ", " at the end of string and then execute |
517
|
|
|
cursor.execute(add_values[:-2]) |
518
|
|
|
cnx.commit() |
519
|
|
|
|
520
|
|
|
cursor.close() |
521
|
|
|
cnx.disconnect() |
522
|
|
|
resp.status = falcon.HTTP_201 |
523
|
|
|
resp.location = '/equipments/' + str(new_id) |
524
|
|
|
|
525
|
|
|
|
526
|
|
View Code Duplication |
class EquipmentParameterCollection: |
|
|
|
|
527
|
|
|
@staticmethod |
528
|
|
|
def __init__(): |
529
|
|
|
pass |
530
|
|
|
|
531
|
|
|
@staticmethod |
532
|
|
|
def on_options(req, resp, id_): |
533
|
|
|
resp.status = falcon.HTTP_200 |
534
|
|
|
|
535
|
|
|
@staticmethod |
536
|
|
|
def on_get(req, resp, id_): |
537
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
538
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
539
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
540
|
|
|
|
541
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
542
|
|
|
cursor = cnx.cursor(dictionary=True) |
543
|
|
|
|
544
|
|
|
cursor.execute(" SELECT name " |
545
|
|
|
" FROM tbl_equipments " |
546
|
|
|
" WHERE id = %s ", (id_,)) |
547
|
|
|
if cursor.fetchone() is None: |
548
|
|
|
cursor.close() |
549
|
|
|
cnx.disconnect() |
550
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
551
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
552
|
|
|
|
553
|
|
|
query = (" SELECT id, name " |
554
|
|
|
" FROM tbl_points ") |
555
|
|
|
cursor.execute(query) |
556
|
|
|
rows_points = cursor.fetchall() |
557
|
|
|
|
558
|
|
|
point_dict = dict() |
559
|
|
|
if rows_points is not None and len(rows_points) > 0: |
560
|
|
|
for row in rows_points: |
561
|
|
|
point_dict[row['id']] = {"id": row['id'], |
562
|
|
|
"name": row['name']} |
563
|
|
|
|
564
|
|
|
query = (" SELECT id, name, uuid " |
565
|
|
|
" FROM tbl_meters ") |
566
|
|
|
cursor.execute(query) |
567
|
|
|
rows_meters = cursor.fetchall() |
568
|
|
|
|
569
|
|
|
meter_dict = dict() |
570
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
571
|
|
|
for row in rows_meters: |
572
|
|
|
meter_dict[row['uuid']] = {"type": 'meter', |
573
|
|
|
"id": row['id'], |
574
|
|
|
"name": row['name'], |
575
|
|
|
"uuid": row['uuid']} |
576
|
|
|
|
577
|
|
|
query = (" SELECT id, name, uuid " |
578
|
|
|
" FROM tbl_offline_meters ") |
579
|
|
|
cursor.execute(query) |
580
|
|
|
rows_offline_meters = cursor.fetchall() |
581
|
|
|
|
582
|
|
|
offline_meter_dict = dict() |
583
|
|
|
if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
584
|
|
|
for row in rows_offline_meters: |
585
|
|
|
offline_meter_dict[row['uuid']] = {"type": 'offline_meter', |
586
|
|
|
"id": row['id'], |
587
|
|
|
"name": row['name'], |
588
|
|
|
"uuid": row['uuid']} |
589
|
|
|
|
590
|
|
|
query = (" SELECT id, name, uuid " |
591
|
|
|
" FROM tbl_virtual_meters ") |
592
|
|
|
cursor.execute(query) |
593
|
|
|
rows_virtual_meters = cursor.fetchall() |
594
|
|
|
|
595
|
|
|
virtual_meter_dict = dict() |
596
|
|
|
if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
597
|
|
|
for row in rows_virtual_meters: |
598
|
|
|
virtual_meter_dict[row['uuid']] = {"type": 'virtual_meter', |
599
|
|
|
"id": row['id'], |
600
|
|
|
"name": row['name'], |
601
|
|
|
"uuid": row['uuid']} |
602
|
|
|
|
603
|
|
|
query = (" SELECT id, name, parameter_type, " |
604
|
|
|
" constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
605
|
|
|
" FROM tbl_equipments_parameters " |
606
|
|
|
" WHERE equipment_id = %s " |
607
|
|
|
" ORDER BY id ") |
608
|
|
|
cursor.execute(query, (id_, )) |
609
|
|
|
rows_parameters = cursor.fetchall() |
610
|
|
|
|
611
|
|
|
result = list() |
612
|
|
|
if rows_parameters is not None and len(rows_parameters) > 0: |
613
|
|
|
for row in rows_parameters: |
614
|
|
|
constant = None |
615
|
|
|
point = None |
616
|
|
|
numerator_meter = None |
617
|
|
|
denominator_meter = None |
618
|
|
|
if row['parameter_type'] == 'point': |
619
|
|
|
point = point_dict.get(row['point_id'], None) |
620
|
|
|
constant = None |
621
|
|
|
numerator_meter = None |
622
|
|
|
denominator_meter = None |
623
|
|
|
elif row['parameter_type'] == 'constant': |
624
|
|
|
constant = row['constant'] |
625
|
|
|
point = None |
626
|
|
|
numerator_meter = None |
627
|
|
|
denominator_meter = None |
628
|
|
|
elif row['parameter_type'] == 'fraction': |
629
|
|
|
constant = None |
630
|
|
|
point = None |
631
|
|
|
# find numerator meter by uuid |
632
|
|
|
numerator_meter = meter_dict.get(row['numerator_meter_uuid'], None) |
633
|
|
|
if numerator_meter is None: |
634
|
|
|
numerator_meter = virtual_meter_dict.get(row['numerator_meter_uuid'], None) |
635
|
|
|
if numerator_meter is None: |
636
|
|
|
numerator_meter = offline_meter_dict.get(row['numerator_meter_uuid'], None) |
637
|
|
|
# find denominator meter by uuid |
638
|
|
|
denominator_meter = meter_dict.get(row['denominator_meter_uuid'], None) |
639
|
|
|
if denominator_meter is None: |
640
|
|
|
denominator_meter = virtual_meter_dict.get(row['denominator_meter_uuid'], None) |
641
|
|
|
if denominator_meter is None: |
642
|
|
|
denominator_meter = offline_meter_dict.get(row['denominator_meter_uuid'], None) |
643
|
|
|
|
644
|
|
|
meta_result = {"id": row['id'], |
645
|
|
|
"name": row['name'], |
646
|
|
|
"parameter_type": row['parameter_type'], |
647
|
|
|
"constant": constant, |
648
|
|
|
"point": point, |
649
|
|
|
"numerator_meter": numerator_meter, |
650
|
|
|
"denominator_meter": denominator_meter} |
651
|
|
|
result.append(meta_result) |
652
|
|
|
|
653
|
|
|
cursor.close() |
654
|
|
|
cnx.disconnect() |
655
|
|
|
resp.body = json.dumps(result) |
656
|
|
|
|
657
|
|
|
@staticmethod |
658
|
|
|
def on_post(req, resp, id_): |
659
|
|
|
"""Handles POST requests""" |
660
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
661
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
662
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
663
|
|
|
try: |
664
|
|
|
raw_json = req.stream.read().decode('utf-8') |
665
|
|
|
except Exception as ex: |
666
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
667
|
|
|
|
668
|
|
|
new_values = json.loads(raw_json) |
669
|
|
|
|
670
|
|
|
if 'name' not in new_values['data'].keys() or \ |
671
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
672
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
673
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
674
|
|
|
description='API.INVALID_EQUIPMENT_PARAMETER_NAME') |
675
|
|
|
name = str.strip(new_values['data']['name']) |
676
|
|
|
|
677
|
|
|
if 'parameter_type' not in new_values['data'].keys() or \ |
678
|
|
|
not isinstance(new_values['data']['parameter_type'], str) or \ |
679
|
|
|
len(str.strip(new_values['data']['parameter_type'])) == 0: |
680
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
681
|
|
|
description='API.INVALID_EQUIPMENT_PARAMETER_TYPE') |
682
|
|
|
|
683
|
|
|
parameter_type = str.strip(new_values['data']['parameter_type']) |
684
|
|
|
|
685
|
|
|
if parameter_type not in ('constant', 'point', 'fraction'): |
686
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
687
|
|
|
description='API.INVALID_EQUIPMENT_PARAMETER_TYPE') |
688
|
|
|
|
689
|
|
|
constant = None |
690
|
|
|
if 'constant' in new_values['data'].keys(): |
691
|
|
|
if new_values['data']['constant'] is not None and \ |
692
|
|
|
isinstance(new_values['data']['constant'], str) and \ |
693
|
|
|
len(str.strip(new_values['data']['constant'])) > 0: |
694
|
|
|
constant = str.strip(new_values['data']['constant']) |
695
|
|
|
|
696
|
|
|
point_id = None |
697
|
|
|
if 'point_id' in new_values['data'].keys(): |
698
|
|
|
if new_values['data']['point_id'] is not None and \ |
699
|
|
|
new_values['data']['point_id'] <= 0: |
700
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
701
|
|
|
description='API.INVALID_POINT_ID') |
702
|
|
|
point_id = new_values['data']['point_id'] |
703
|
|
|
|
704
|
|
|
numerator_meter_uuid = None |
705
|
|
|
if 'numerator_meter_uuid' in new_values['data'].keys(): |
706
|
|
|
if new_values['data']['numerator_meter_uuid'] is not None and \ |
707
|
|
|
isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
708
|
|
|
len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
709
|
|
|
numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
710
|
|
|
|
711
|
|
|
denominator_meter_uuid = None |
712
|
|
|
if 'denominator_meter_uuid' in new_values['data'].keys(): |
713
|
|
|
if new_values['data']['denominator_meter_uuid'] is not None and \ |
714
|
|
|
isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
715
|
|
|
len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
716
|
|
|
denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
717
|
|
|
|
718
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
719
|
|
|
cursor = cnx.cursor(dictionary=True) |
720
|
|
|
|
721
|
|
|
cursor.execute(" SELECT name " |
722
|
|
|
" FROM tbl_equipments " |
723
|
|
|
" WHERE id = %s ", (id_,)) |
724
|
|
|
if cursor.fetchone() is None: |
725
|
|
|
cursor.close() |
726
|
|
|
cnx.disconnect() |
727
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.NOT_FOUND', |
728
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
729
|
|
|
|
730
|
|
|
cursor.execute(" SELECT name " |
731
|
|
|
" FROM tbl_equipments_parameters " |
732
|
|
|
" WHERE name = %s AND equipment_id = %s ", (name, id_)) |
733
|
|
|
if cursor.fetchone() is not None: |
734
|
|
|
cursor.close() |
735
|
|
|
cnx.disconnect() |
736
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
737
|
|
|
description='API.EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
738
|
|
|
|
739
|
|
|
# validate by parameter type |
740
|
|
|
if parameter_type == 'point': |
741
|
|
|
if point_id is None: |
742
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
743
|
|
|
description='API.INVALID_POINT_ID') |
744
|
|
|
|
745
|
|
|
query = (" SELECT id, name " |
746
|
|
|
" FROM tbl_points " |
747
|
|
|
" WHERE id = %s ") |
748
|
|
|
cursor.execute(query, (point_id, )) |
749
|
|
|
if cursor.fetchone() is None: |
750
|
|
|
cursor.close() |
751
|
|
|
cnx.disconnect() |
752
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
753
|
|
|
description='API.POINT_NOT_FOUND') |
754
|
|
|
|
755
|
|
|
elif parameter_type == 'constant': |
756
|
|
|
if constant is None: |
757
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
758
|
|
|
description='API.INVALID_CONSTANT_VALUE') |
759
|
|
|
|
760
|
|
|
elif parameter_type == 'fraction': |
761
|
|
|
|
762
|
|
|
query = (" SELECT id, name, uuid " |
763
|
|
|
" FROM tbl_meters ") |
764
|
|
|
cursor.execute(query) |
765
|
|
|
rows_meters = cursor.fetchall() |
766
|
|
|
|
767
|
|
|
meter_dict = dict() |
768
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
769
|
|
|
for row in rows_meters: |
770
|
|
|
meter_dict[row['uuid']] = {"type": 'meter', |
771
|
|
|
"id": row['id'], |
772
|
|
|
"name": row['name'], |
773
|
|
|
"uuid": row['uuid']} |
774
|
|
|
|
775
|
|
|
query = (" SELECT id, name, uuid " |
776
|
|
|
" FROM tbl_offline_meters ") |
777
|
|
|
cursor.execute(query) |
778
|
|
|
rows_offline_meters = cursor.fetchall() |
779
|
|
|
|
780
|
|
|
offline_meter_dict = dict() |
781
|
|
|
if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
782
|
|
|
for row in rows_offline_meters: |
783
|
|
|
offline_meter_dict[row['uuid']] = {"type": 'offline_meter', |
784
|
|
|
"id": row['id'], |
785
|
|
|
"name": row['name'], |
786
|
|
|
"uuid": row['uuid']} |
787
|
|
|
|
788
|
|
|
query = (" SELECT id, name, uuid " |
789
|
|
|
" FROM tbl_virtual_meters ") |
790
|
|
|
cursor.execute(query) |
791
|
|
|
rows_virtual_meters = cursor.fetchall() |
792
|
|
|
|
793
|
|
|
virtual_meter_dict = dict() |
794
|
|
|
if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
795
|
|
|
for row in rows_virtual_meters: |
796
|
|
|
virtual_meter_dict[row['uuid']] = {"type": 'virtual_meter', |
797
|
|
|
"id": row['id'], |
798
|
|
|
"name": row['name'], |
799
|
|
|
"uuid": row['uuid']} |
800
|
|
|
|
801
|
|
|
# validate numerator meter uuid |
802
|
|
|
if meter_dict.get(numerator_meter_uuid) is None and \ |
803
|
|
|
virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
804
|
|
|
offline_meter_dict.get(numerator_meter_uuid) is None: |
805
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
806
|
|
|
description='API.INVALID_NUMERATOR_METER_UUID') |
807
|
|
|
|
808
|
|
|
# validate denominator meter uuid |
809
|
|
|
if denominator_meter_uuid == numerator_meter_uuid: |
810
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
811
|
|
|
description='API.INVALID_DENOMINATOR_METER_UUID') |
812
|
|
|
|
813
|
|
|
if denominator_meter_uuid not in meter_dict and \ |
814
|
|
|
denominator_meter_uuid not in virtual_meter_dict and \ |
815
|
|
|
denominator_meter_uuid not in offline_meter_dict: |
816
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
817
|
|
|
description='API.INVALID_DENOMINATOR_METER_UUID') |
818
|
|
|
|
819
|
|
|
add_values = (" INSERT INTO tbl_equipments_parameters " |
820
|
|
|
" (equipment_id, name, parameter_type, constant, " |
821
|
|
|
" point_id, numerator_meter_uuid, denominator_meter_uuid) " |
822
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
823
|
|
|
cursor.execute(add_values, (id_, |
824
|
|
|
name, |
825
|
|
|
parameter_type, |
826
|
|
|
constant, |
827
|
|
|
point_id, |
828
|
|
|
numerator_meter_uuid, |
829
|
|
|
denominator_meter_uuid)) |
830
|
|
|
new_id = cursor.lastrowid |
831
|
|
|
cnx.commit() |
832
|
|
|
cursor.close() |
833
|
|
|
cnx.disconnect() |
834
|
|
|
|
835
|
|
|
resp.status = falcon.HTTP_201 |
836
|
|
|
resp.location = '/equipments/' + str(id_) + 'parameters/' + str(new_id) |
837
|
|
|
|
838
|
|
|
|
839
|
|
View Code Duplication |
class EquipmentParameterItem: |
|
|
|
|
840
|
|
|
@staticmethod |
841
|
|
|
def __init__(): |
842
|
|
|
pass |
843
|
|
|
|
844
|
|
|
@staticmethod |
845
|
|
|
def on_options(req, resp, id_, pid): |
846
|
|
|
resp.status = falcon.HTTP_200 |
847
|
|
|
|
848
|
|
|
@staticmethod |
849
|
|
|
def on_get(req, resp, id_, pid): |
850
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
851
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
852
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
853
|
|
|
|
854
|
|
|
if not pid.isdigit() or int(pid) <= 0: |
855
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
856
|
|
|
description='API.INVALID_EQUIPMENT_PARAMETER_ID') |
857
|
|
|
|
858
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
859
|
|
|
cursor = cnx.cursor(dictionary=True) |
860
|
|
|
|
861
|
|
|
query = (" SELECT id, name " |
862
|
|
|
" FROM tbl_points ") |
863
|
|
|
cursor.execute(query) |
864
|
|
|
rows_points = cursor.fetchall() |
865
|
|
|
|
866
|
|
|
point_dict = dict() |
867
|
|
|
if rows_points is not None and len(rows_points) > 0: |
868
|
|
|
for row in rows_points: |
869
|
|
|
point_dict[row['id']] = {"id": row['id'], |
870
|
|
|
"name": row['name']} |
871
|
|
|
|
872
|
|
|
query = (" SELECT id, name, uuid " |
873
|
|
|
" FROM tbl_meters ") |
874
|
|
|
cursor.execute(query) |
875
|
|
|
rows_meters = cursor.fetchall() |
876
|
|
|
|
877
|
|
|
meter_dict = dict() |
878
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
879
|
|
|
for row in rows_meters: |
880
|
|
|
meter_dict[row['uuid']] = {"type": 'meter', |
881
|
|
|
"id": row['id'], |
882
|
|
|
"name": row['name'], |
883
|
|
|
"uuid": row['uuid']} |
884
|
|
|
|
885
|
|
|
query = (" SELECT id, name, uuid " |
886
|
|
|
" FROM tbl_offline_meters ") |
887
|
|
|
cursor.execute(query) |
888
|
|
|
rows_offline_meters = cursor.fetchall() |
889
|
|
|
|
890
|
|
|
offline_meter_dict = dict() |
891
|
|
|
if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
892
|
|
|
for row in rows_offline_meters: |
893
|
|
|
offline_meter_dict[row['uuid']] = {"type": 'offline_meter', |
894
|
|
|
"id": row['id'], |
895
|
|
|
"name": row['name'], |
896
|
|
|
"uuid": row['uuid']} |
897
|
|
|
|
898
|
|
|
query = (" SELECT id, name, uuid " |
899
|
|
|
" FROM tbl_virtual_meters ") |
900
|
|
|
cursor.execute(query) |
901
|
|
|
rows_virtual_meters = cursor.fetchall() |
902
|
|
|
|
903
|
|
|
virtual_meter_dict = dict() |
904
|
|
|
if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
905
|
|
|
for row in rows_virtual_meters: |
906
|
|
|
virtual_meter_dict[row['uuid']] = {"type": 'virtual_meter', |
907
|
|
|
"id": row['id'], |
908
|
|
|
"name": row['name'], |
909
|
|
|
"uuid": row['uuid']} |
910
|
|
|
|
911
|
|
|
query = (" SELECT id, name, parameter_type, " |
912
|
|
|
" constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
913
|
|
|
" FROM tbl_equipments_parameters " |
914
|
|
|
" WHERE equipment_id = %s AND id = %s ") |
915
|
|
|
cursor.execute(query, (id_, pid)) |
916
|
|
|
row = cursor.fetchone() |
917
|
|
|
cursor.close() |
918
|
|
|
cnx.disconnect() |
919
|
|
|
|
920
|
|
|
if row is None: |
921
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
922
|
|
|
description='API.EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
923
|
|
|
else: |
924
|
|
|
constant = None |
925
|
|
|
point = None |
926
|
|
|
numerator_meter = None |
927
|
|
|
denominator_meter = None |
928
|
|
|
if row['parameter_type'] == 'point': |
929
|
|
|
point = point_dict.get(row['point_id'], None) |
930
|
|
|
constant = None |
931
|
|
|
numerator_meter = None |
932
|
|
|
denominator_meter = None |
933
|
|
|
elif row['parameter_type'] == 'constant': |
934
|
|
|
constant = row['constant'] |
935
|
|
|
point = None |
936
|
|
|
numerator_meter = None |
937
|
|
|
denominator_meter = None |
938
|
|
|
elif row['parameter_type'] == 'fraction': |
939
|
|
|
constant = None |
940
|
|
|
point = None |
941
|
|
|
# find numerator meter by uuid |
942
|
|
|
numerator_meter = meter_dict.get(row['numerator_meter_uuid'], None) |
943
|
|
|
if numerator_meter is None: |
944
|
|
|
numerator_meter = virtual_meter_dict.get(row['numerator_meter_uuid'], None) |
945
|
|
|
if numerator_meter is None: |
946
|
|
|
numerator_meter = offline_meter_dict.get(row['numerator_meter_uuid'], None) |
947
|
|
|
# find denominator meter by uuid |
948
|
|
|
denominator_meter = meter_dict.get(row['denominator_meter_uuid'], None) |
949
|
|
|
if denominator_meter is None: |
950
|
|
|
denominator_meter = virtual_meter_dict.get(row['denominator_meter_uuid'], None) |
951
|
|
|
if denominator_meter is None: |
952
|
|
|
denominator_meter = offline_meter_dict.get(row['denominator_meter_uuid'], None) |
953
|
|
|
|
954
|
|
|
meta_result = {"id": row['id'], |
955
|
|
|
"name": row['name'], |
956
|
|
|
"parameter_type": row['parameter_type'], |
957
|
|
|
"constant": constant, |
958
|
|
|
"point": point, |
959
|
|
|
"numerator_meter": numerator_meter, |
960
|
|
|
"denominator_meter": denominator_meter} |
961
|
|
|
|
962
|
|
|
resp.body = json.dumps(meta_result) |
963
|
|
|
|
964
|
|
|
@staticmethod |
965
|
|
|
def on_delete(req, resp, id_, pid): |
966
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
967
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
968
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
969
|
|
|
|
970
|
|
|
if not pid.isdigit() or int(pid) <= 0: |
971
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
972
|
|
|
description='API.INVALID_EQUIPMENT_PARAMETER_ID') |
973
|
|
|
|
974
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
975
|
|
|
cursor = cnx.cursor() |
976
|
|
|
|
977
|
|
|
cursor.execute(" SELECT name " |
978
|
|
|
" FROM tbl_equipments " |
979
|
|
|
" WHERE id = %s ", |
980
|
|
|
(id_,)) |
981
|
|
|
row = cursor.fetchone() |
982
|
|
|
if row is None: |
983
|
|
|
cursor.close() |
984
|
|
|
cnx.disconnect() |
985
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, |
986
|
|
|
title='API.NOT_FOUND', |
987
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
988
|
|
|
|
989
|
|
|
cursor.execute(" SELECT name " |
990
|
|
|
" FROM tbl_equipments_parameters " |
991
|
|
|
" WHERE equipment_id = %s AND id = %s ", |
992
|
|
|
(id_, pid,)) |
993
|
|
|
row = cursor.fetchone() |
994
|
|
|
if row is None: |
995
|
|
|
cursor.close() |
996
|
|
|
cnx.disconnect() |
997
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, |
998
|
|
|
title='API.NOT_FOUND', |
999
|
|
|
description='API.EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
1000
|
|
|
|
1001
|
|
|
cursor.execute(" DELETE FROM tbl_equipments_parameters " |
1002
|
|
|
" WHERE id = %s ", (pid, )) |
1003
|
|
|
cnx.commit() |
1004
|
|
|
|
1005
|
|
|
cursor.close() |
1006
|
|
|
cnx.disconnect() |
1007
|
|
|
|
1008
|
|
|
resp.status = falcon.HTTP_204 |
1009
|
|
|
|
1010
|
|
|
@staticmethod |
1011
|
|
|
def on_put(req, resp, id_, pid): |
1012
|
|
|
"""Handles POST requests""" |
1013
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1014
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1015
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1016
|
|
|
|
1017
|
|
|
if not pid.isdigit() or int(pid) <= 0: |
1018
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1019
|
|
|
description='API.INVALID_EQUIPMENT_PARAMETER_ID') |
1020
|
|
|
|
1021
|
|
|
try: |
1022
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1023
|
|
|
except Exception as ex: |
1024
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
1025
|
|
|
|
1026
|
|
|
new_values = json.loads(raw_json) |
1027
|
|
|
|
1028
|
|
|
if 'name' not in new_values['data'].keys() or \ |
1029
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
1030
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
1031
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1032
|
|
|
description='API.INVALID_EQUIPMENT_PARAMETER_NAME') |
1033
|
|
|
name = str.strip(new_values['data']['name']) |
1034
|
|
|
|
1035
|
|
|
if 'parameter_type' not in new_values['data'].keys() or \ |
1036
|
|
|
not isinstance(new_values['data']['parameter_type'], str) or \ |
1037
|
|
|
len(str.strip(new_values['data']['parameter_type'])) == 0: |
1038
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1039
|
|
|
description='API.INVALID_EQUIPMENT_PARAMETER_TYPE') |
1040
|
|
|
|
1041
|
|
|
parameter_type = str.strip(new_values['data']['parameter_type']) |
1042
|
|
|
|
1043
|
|
|
if parameter_type not in ('constant', 'point', 'fraction'): |
1044
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1045
|
|
|
description='API.INVALID_EQUIPMENT_PARAMETER_TYPE') |
1046
|
|
|
|
1047
|
|
|
constant = None |
1048
|
|
|
if 'constant' in new_values['data'].keys(): |
1049
|
|
|
if new_values['data']['constant'] is not None and \ |
1050
|
|
|
isinstance(new_values['data']['constant'], str) and \ |
1051
|
|
|
len(str.strip(new_values['data']['constant'])) > 0: |
1052
|
|
|
constant = str.strip(new_values['data']['constant']) |
1053
|
|
|
|
1054
|
|
|
point_id = None |
1055
|
|
|
if 'point_id' in new_values['data'].keys(): |
1056
|
|
|
if new_values['data']['point_id'] is not None and \ |
1057
|
|
|
new_values['data']['point_id'] <= 0: |
1058
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1059
|
|
|
description='API.INVALID_POINT_ID') |
1060
|
|
|
point_id = new_values['data']['point_id'] |
1061
|
|
|
|
1062
|
|
|
numerator_meter_uuid = None |
1063
|
|
|
if 'numerator_meter_uuid' in new_values['data'].keys(): |
1064
|
|
|
if new_values['data']['numerator_meter_uuid'] is not None and \ |
1065
|
|
|
isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
1066
|
|
|
len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
1067
|
|
|
numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
1068
|
|
|
|
1069
|
|
|
denominator_meter_uuid = None |
1070
|
|
|
if 'denominator_meter_uuid' in new_values['data'].keys(): |
1071
|
|
|
if new_values['data']['denominator_meter_uuid'] is not None and \ |
1072
|
|
|
isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
1073
|
|
|
len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
1074
|
|
|
denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
1075
|
|
|
|
1076
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1077
|
|
|
cursor = cnx.cursor(dictionary=True) |
1078
|
|
|
|
1079
|
|
|
cursor.execute(" SELECT name " |
1080
|
|
|
" FROM tbl_equipments " |
1081
|
|
|
" WHERE id = %s ", (id_,)) |
1082
|
|
|
if cursor.fetchone() is None: |
1083
|
|
|
cursor.close() |
1084
|
|
|
cnx.disconnect() |
1085
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.NOT_FOUND', |
1086
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1087
|
|
|
|
1088
|
|
|
cursor.execute(" SELECT name " |
1089
|
|
|
" FROM tbl_equipments_parameters " |
1090
|
|
|
" WHERE equipment_id = %s AND id = %s ", |
1091
|
|
|
(id_, pid,)) |
1092
|
|
|
row = cursor.fetchone() |
1093
|
|
|
if row is None: |
1094
|
|
|
cursor.close() |
1095
|
|
|
cnx.disconnect() |
1096
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, |
1097
|
|
|
title='API.NOT_FOUND', |
1098
|
|
|
description='API.EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
1099
|
|
|
|
1100
|
|
|
cursor.execute(" SELECT name " |
1101
|
|
|
" FROM tbl_equipments_parameters " |
1102
|
|
|
" WHERE name = %s AND equipment_id = %s AND id != %s ", (name, id_, pid)) |
1103
|
|
|
row = cursor.fetchone() |
1104
|
|
|
if row is not None: |
1105
|
|
|
cursor.close() |
1106
|
|
|
cnx.disconnect() |
1107
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1108
|
|
|
description='API.EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
1109
|
|
|
|
1110
|
|
|
# validate by parameter type |
1111
|
|
|
if parameter_type == 'point': |
1112
|
|
|
if point_id is None: |
1113
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1114
|
|
|
description='API.INVALID_POINT_ID') |
1115
|
|
|
|
1116
|
|
|
query = (" SELECT id, name " |
1117
|
|
|
" FROM tbl_points " |
1118
|
|
|
" WHERE id = %s ") |
1119
|
|
|
cursor.execute(query, (point_id, )) |
1120
|
|
|
row = cursor.fetchone() |
1121
|
|
|
if row is None: |
1122
|
|
|
cursor.close() |
1123
|
|
|
cnx.disconnect() |
1124
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1125
|
|
|
description='API.POINT_NOT_FOUND') |
1126
|
|
|
|
1127
|
|
|
elif parameter_type == 'constant': |
1128
|
|
|
if constant is None: |
1129
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1130
|
|
|
description='API.INVALID_CONSTANT_VALUE') |
1131
|
|
|
|
1132
|
|
|
elif parameter_type == 'fraction': |
1133
|
|
|
|
1134
|
|
|
query = (" SELECT id, name, uuid " |
1135
|
|
|
" FROM tbl_meters ") |
1136
|
|
|
cursor.execute(query) |
1137
|
|
|
rows_meters = cursor.fetchall() |
1138
|
|
|
|
1139
|
|
|
meter_dict = dict() |
1140
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
1141
|
|
|
for row in rows_meters: |
1142
|
|
|
meter_dict[row['uuid']] = {"type": 'meter', |
1143
|
|
|
"id": row['id'], |
1144
|
|
|
"name": row['name'], |
1145
|
|
|
"uuid": row['uuid']} |
1146
|
|
|
|
1147
|
|
|
query = (" SELECT id, name, uuid " |
1148
|
|
|
" FROM tbl_offline_meters ") |
1149
|
|
|
cursor.execute(query) |
1150
|
|
|
rows_offline_meters = cursor.fetchall() |
1151
|
|
|
|
1152
|
|
|
offline_meter_dict = dict() |
1153
|
|
|
if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
1154
|
|
|
for row in rows_offline_meters: |
1155
|
|
|
offline_meter_dict[row['uuid']] = {"type": 'offline_meter', |
1156
|
|
|
"id": row['id'], |
1157
|
|
|
"name": row['name'], |
1158
|
|
|
"uuid": row['uuid']} |
1159
|
|
|
|
1160
|
|
|
query = (" SELECT id, name, uuid " |
1161
|
|
|
" FROM tbl_virtual_meters ") |
1162
|
|
|
cursor.execute(query) |
1163
|
|
|
rows_virtual_meters = cursor.fetchall() |
1164
|
|
|
|
1165
|
|
|
virtual_meter_dict = dict() |
1166
|
|
|
if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
1167
|
|
|
for row in rows_virtual_meters: |
1168
|
|
|
virtual_meter_dict[row['uuid']] = {"type": 'virtual_meter', |
1169
|
|
|
"id": row['id'], |
1170
|
|
|
"name": row['name'], |
1171
|
|
|
"uuid": row['uuid']} |
1172
|
|
|
|
1173
|
|
|
# validate numerator meter uuid |
1174
|
|
|
if meter_dict.get(numerator_meter_uuid) is None and \ |
1175
|
|
|
virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
1176
|
|
|
offline_meter_dict.get(numerator_meter_uuid) is None: |
1177
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1178
|
|
|
description='API.INVALID_NUMERATOR_METER_UUID') |
1179
|
|
|
|
1180
|
|
|
# validate denominator meter uuid |
1181
|
|
|
if denominator_meter_uuid == numerator_meter_uuid: |
1182
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1183
|
|
|
description='API.INVALID_DENOMINATOR_METER_UUID') |
1184
|
|
|
|
1185
|
|
|
if denominator_meter_uuid not in meter_dict and \ |
1186
|
|
|
denominator_meter_uuid not in virtual_meter_dict and \ |
1187
|
|
|
denominator_meter_uuid not in offline_meter_dict: |
1188
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1189
|
|
|
description='API.INVALID_DENOMINATOR_METER_UUID') |
1190
|
|
|
|
1191
|
|
|
add_values = (" UPDATE tbl_equipments_parameters " |
1192
|
|
|
" SET name = %s , parameter_type = %s, constant = %s, " |
1193
|
|
|
" point_id = %s, numerator_meter_uuid = %s, denominator_meter_uuid =%s " |
1194
|
|
|
" WHERE id = %s ") |
1195
|
|
|
cursor.execute(add_values, (name, |
1196
|
|
|
parameter_type, |
1197
|
|
|
constant, |
1198
|
|
|
point_id, |
1199
|
|
|
numerator_meter_uuid, |
1200
|
|
|
denominator_meter_uuid, |
1201
|
|
|
pid)) |
1202
|
|
|
cnx.commit() |
1203
|
|
|
|
1204
|
|
|
cursor.close() |
1205
|
|
|
cnx.disconnect() |
1206
|
|
|
|
1207
|
|
|
resp.status = falcon.HTTP_200 |
1208
|
|
|
|
1209
|
|
|
|
1210
|
|
View Code Duplication |
class EquipmentMeterCollection: |
|
|
|
|
1211
|
|
|
@staticmethod |
1212
|
|
|
def __init__(): |
1213
|
|
|
pass |
1214
|
|
|
|
1215
|
|
|
@staticmethod |
1216
|
|
|
def on_options(req, resp, id_): |
1217
|
|
|
resp.status = falcon.HTTP_200 |
1218
|
|
|
|
1219
|
|
|
@staticmethod |
1220
|
|
|
def on_get(req, resp, id_): |
1221
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1222
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1223
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1224
|
|
|
|
1225
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1226
|
|
|
cursor = cnx.cursor(dictionary=True) |
1227
|
|
|
|
1228
|
|
|
cursor.execute(" SELECT name " |
1229
|
|
|
" FROM tbl_equipments " |
1230
|
|
|
" WHERE id = %s ", (id_,)) |
1231
|
|
|
if cursor.fetchone() is None: |
1232
|
|
|
cursor.close() |
1233
|
|
|
cnx.disconnect() |
1234
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
1235
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1236
|
|
|
|
1237
|
|
|
query = (" SELECT id, name, uuid " |
1238
|
|
|
" FROM tbl_energy_categories ") |
1239
|
|
|
cursor.execute(query) |
1240
|
|
|
rows_energy_categories = cursor.fetchall() |
1241
|
|
|
|
1242
|
|
|
energy_category_dict = dict() |
1243
|
|
|
if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
1244
|
|
|
for row in rows_energy_categories: |
1245
|
|
|
energy_category_dict[row['id']] = {"id": row['id'], |
1246
|
|
|
"name": row['name'], |
1247
|
|
|
"uuid": row['uuid']} |
1248
|
|
|
|
1249
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
1250
|
|
|
" FROM tbl_equipments e, tbl_equipments_meters em, tbl_meters m " |
1251
|
|
|
" WHERE em.equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
1252
|
|
|
" ORDER BY m.id ") |
1253
|
|
|
cursor.execute(query, (id_,)) |
1254
|
|
|
rows = cursor.fetchall() |
1255
|
|
|
|
1256
|
|
|
result = list() |
1257
|
|
|
if rows is not None and len(rows) > 0: |
1258
|
|
|
for row in rows: |
1259
|
|
|
energy_category = energy_category_dict.get(row['energy_category_id'], None) |
1260
|
|
|
meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
1261
|
|
|
"energy_category": energy_category, |
1262
|
|
|
"is_output": bool(row['is_output'])} |
1263
|
|
|
result.append(meta_result) |
1264
|
|
|
|
1265
|
|
|
resp.body = json.dumps(result) |
1266
|
|
|
|
1267
|
|
|
@staticmethod |
1268
|
|
|
def on_post(req, resp, id_): |
1269
|
|
|
"""Handles POST requests""" |
1270
|
|
|
try: |
1271
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1272
|
|
|
except Exception as ex: |
1273
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
1274
|
|
|
|
1275
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1276
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1277
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1278
|
|
|
|
1279
|
|
|
new_values = json.loads(raw_json) |
1280
|
|
|
|
1281
|
|
|
if 'meter_id' not in new_values['data'].keys() or \ |
1282
|
|
|
not isinstance(new_values['data']['meter_id'], int) or \ |
1283
|
|
|
new_values['data']['meter_id'] <= 0: |
1284
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1285
|
|
|
description='API.INVALID_METER_ID') |
1286
|
|
|
meter_id = new_values['data']['meter_id'] |
1287
|
|
|
|
1288
|
|
|
if 'is_output' not in new_values['data'].keys() or \ |
1289
|
|
|
not isinstance(new_values['data']['is_output'], bool): |
1290
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1291
|
|
|
description='API.INVALID_IS_OUTPUT_VALUE') |
1292
|
|
|
is_output = new_values['data']['is_output'] |
1293
|
|
|
|
1294
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1295
|
|
|
cursor = cnx.cursor() |
1296
|
|
|
|
1297
|
|
|
cursor.execute(" SELECT name " |
1298
|
|
|
" from tbl_equipments " |
1299
|
|
|
" WHERE id = %s ", (id_,)) |
1300
|
|
|
if cursor.fetchone() is None: |
1301
|
|
|
cursor.close() |
1302
|
|
|
cnx.disconnect() |
1303
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
1304
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1305
|
|
|
|
1306
|
|
|
cursor.execute(" SELECT name " |
1307
|
|
|
" FROM tbl_meters " |
1308
|
|
|
" WHERE id = %s ", (meter_id,)) |
1309
|
|
|
if cursor.fetchone() is None: |
1310
|
|
|
cursor.close() |
1311
|
|
|
cnx.disconnect() |
1312
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
1313
|
|
|
description='API.METER_NOT_FOUND') |
1314
|
|
|
|
1315
|
|
|
query = (" SELECT id " |
1316
|
|
|
" FROM tbl_equipments_meters " |
1317
|
|
|
" WHERE equipment_id = %s AND meter_id = %s") |
1318
|
|
|
cursor.execute(query, (id_, meter_id,)) |
1319
|
|
|
if cursor.fetchone() is not None: |
1320
|
|
|
cursor.close() |
1321
|
|
|
cnx.disconnect() |
1322
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
1323
|
|
|
description='API.EQUIPMENT_METER_RELATION_EXISTED') |
1324
|
|
|
|
1325
|
|
|
add_row = (" INSERT INTO tbl_equipments_meters (equipment_id, meter_id, is_output ) " |
1326
|
|
|
" VALUES (%s, %s, %s) ") |
1327
|
|
|
cursor.execute(add_row, (id_, meter_id, is_output)) |
1328
|
|
|
new_id = cursor.lastrowid |
1329
|
|
|
cnx.commit() |
1330
|
|
|
cursor.close() |
1331
|
|
|
cnx.disconnect() |
1332
|
|
|
|
1333
|
|
|
resp.status = falcon.HTTP_201 |
1334
|
|
|
resp.location = '/equipments/' + str(id_) + '/meters/' + str(meter_id) |
1335
|
|
|
|
1336
|
|
|
|
1337
|
|
View Code Duplication |
class EquipmentMeterItem: |
|
|
|
|
1338
|
|
|
@staticmethod |
1339
|
|
|
def __init__(): |
1340
|
|
|
pass |
1341
|
|
|
|
1342
|
|
|
@staticmethod |
1343
|
|
|
def on_options(req, resp, id_, mid): |
1344
|
|
|
resp.status = falcon.HTTP_200 |
1345
|
|
|
|
1346
|
|
|
@staticmethod |
1347
|
|
|
def on_delete(req, resp, id_, mid): |
1348
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1349
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1350
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1351
|
|
|
|
1352
|
|
|
if not mid.isdigit() or int(mid) <= 0: |
1353
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1354
|
|
|
description='API.INVALID_METER_ID') |
1355
|
|
|
|
1356
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1357
|
|
|
cursor = cnx.cursor() |
1358
|
|
|
|
1359
|
|
|
cursor.execute(" SELECT name " |
1360
|
|
|
" FROM tbl_equipments " |
1361
|
|
|
" WHERE id = %s ", (id_,)) |
1362
|
|
|
if cursor.fetchone() is None: |
1363
|
|
|
cursor.close() |
1364
|
|
|
cnx.disconnect() |
1365
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
1366
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1367
|
|
|
|
1368
|
|
|
cursor.execute(" SELECT name " |
1369
|
|
|
" FROM tbl_meters " |
1370
|
|
|
" WHERE id = %s ", (mid,)) |
1371
|
|
|
if cursor.fetchone() is None: |
1372
|
|
|
cursor.close() |
1373
|
|
|
cnx.disconnect() |
1374
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
1375
|
|
|
description='API.METER_NOT_FOUND') |
1376
|
|
|
|
1377
|
|
|
cursor.execute(" SELECT id " |
1378
|
|
|
" FROM tbl_equipments_meters " |
1379
|
|
|
" WHERE equipment_id = %s AND meter_id = %s ", (id_, mid)) |
1380
|
|
|
if cursor.fetchone() is None: |
1381
|
|
|
cursor.close() |
1382
|
|
|
cnx.disconnect() |
1383
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
1384
|
|
|
description='API.EQUIPMENT_METER_RELATION_NOT_FOUND') |
1385
|
|
|
|
1386
|
|
|
cursor.execute(" DELETE FROM tbl_equipments_meters WHERE equipment_id = %s AND meter_id = %s ", (id_, mid)) |
1387
|
|
|
cnx.commit() |
1388
|
|
|
|
1389
|
|
|
cursor.close() |
1390
|
|
|
cnx.disconnect() |
1391
|
|
|
|
1392
|
|
|
resp.status = falcon.HTTP_204 |
1393
|
|
|
|
1394
|
|
|
|
1395
|
|
View Code Duplication |
class EquipmentOfflineMeterCollection: |
|
|
|
|
1396
|
|
|
@staticmethod |
1397
|
|
|
def __init__(): |
1398
|
|
|
pass |
1399
|
|
|
|
1400
|
|
|
@staticmethod |
1401
|
|
|
def on_options(req, resp, id_): |
1402
|
|
|
resp.status = falcon.HTTP_200 |
1403
|
|
|
|
1404
|
|
|
@staticmethod |
1405
|
|
|
def on_get(req, resp, id_): |
1406
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1407
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1408
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1409
|
|
|
|
1410
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1411
|
|
|
cursor = cnx.cursor(dictionary=True) |
1412
|
|
|
|
1413
|
|
|
cursor.execute(" SELECT name " |
1414
|
|
|
" FROM tbl_equipments " |
1415
|
|
|
" WHERE id = %s ", (id_,)) |
1416
|
|
|
if cursor.fetchone() is None: |
1417
|
|
|
cursor.close() |
1418
|
|
|
cnx.disconnect() |
1419
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
1420
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1421
|
|
|
|
1422
|
|
|
query = (" SELECT id, name, uuid " |
1423
|
|
|
" FROM tbl_energy_categories ") |
1424
|
|
|
cursor.execute(query) |
1425
|
|
|
rows_energy_categories = cursor.fetchall() |
1426
|
|
|
|
1427
|
|
|
energy_category_dict = dict() |
1428
|
|
|
if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
1429
|
|
|
for row in rows_energy_categories: |
1430
|
|
|
energy_category_dict[row['id']] = {"id": row['id'], |
1431
|
|
|
"name": row['name'], |
1432
|
|
|
"uuid": row['uuid']} |
1433
|
|
|
|
1434
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
1435
|
|
|
" FROM tbl_equipments e, tbl_equipments_offline_meters em, tbl_offline_meters m " |
1436
|
|
|
" WHERE em.equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
1437
|
|
|
" ORDER BY m.id ") |
1438
|
|
|
cursor.execute(query, (id_,)) |
1439
|
|
|
rows = cursor.fetchall() |
1440
|
|
|
|
1441
|
|
|
result = list() |
1442
|
|
|
if rows is not None and len(rows) > 0: |
1443
|
|
|
for row in rows: |
1444
|
|
|
energy_category = energy_category_dict.get(row['energy_category_id'], None) |
1445
|
|
|
meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
1446
|
|
|
"energy_category": energy_category, |
1447
|
|
|
"is_output": bool(row['is_output'])} |
1448
|
|
|
result.append(meta_result) |
1449
|
|
|
|
1450
|
|
|
resp.body = json.dumps(result) |
1451
|
|
|
|
1452
|
|
|
@staticmethod |
1453
|
|
|
def on_post(req, resp, id_): |
1454
|
|
|
"""Handles POST requests""" |
1455
|
|
|
try: |
1456
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1457
|
|
|
except Exception as ex: |
1458
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
1459
|
|
|
|
1460
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1461
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1462
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1463
|
|
|
|
1464
|
|
|
new_values = json.loads(raw_json) |
1465
|
|
|
|
1466
|
|
|
if 'offline_meter_id' not in new_values['data'].keys() or \ |
1467
|
|
|
not isinstance(new_values['data']['offline_meter_id'], int) or \ |
1468
|
|
|
new_values['data']['offline_meter_id'] <= 0: |
1469
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1470
|
|
|
description='API.INVALID_OFFLINE_METER_ID') |
1471
|
|
|
offline_meter_id = new_values['data']['offline_meter_id'] |
1472
|
|
|
|
1473
|
|
|
if 'is_output' not in new_values['data'].keys() or \ |
1474
|
|
|
not isinstance(new_values['data']['is_output'], bool): |
1475
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1476
|
|
|
description='API.INVALID_IS_OUTPUT_VALUE') |
1477
|
|
|
is_output = new_values['data']['is_output'] |
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.disconnect() |
1488
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
1489
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1490
|
|
|
|
1491
|
|
|
cursor.execute(" SELECT name " |
1492
|
|
|
" FROM tbl_offline_meters " |
1493
|
|
|
" WHERE id = %s ", (offline_meter_id,)) |
1494
|
|
|
if cursor.fetchone() is None: |
1495
|
|
|
cursor.close() |
1496
|
|
|
cnx.disconnect() |
1497
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
1498
|
|
|
description='API.OFFLINE_METER_NOT_FOUND') |
1499
|
|
|
|
1500
|
|
|
query = (" SELECT id " |
1501
|
|
|
" FROM tbl_equipments_offline_meters " |
1502
|
|
|
" WHERE equipment_id = %s AND offline_meter_id = %s") |
1503
|
|
|
cursor.execute(query, (id_, offline_meter_id,)) |
1504
|
|
|
if cursor.fetchone() is not None: |
1505
|
|
|
cursor.close() |
1506
|
|
|
cnx.disconnect() |
1507
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
1508
|
|
|
description='API.EQUIPMENT_OFFLINE_METER_RELATION_EXISTED') |
1509
|
|
|
|
1510
|
|
|
add_row = (" INSERT INTO tbl_equipments_offline_meters (equipment_id, offline_meter_id, is_output ) " |
1511
|
|
|
" VALUES (%s, %s, %s) ") |
1512
|
|
|
cursor.execute(add_row, (id_, offline_meter_id, is_output)) |
1513
|
|
|
new_id = cursor.lastrowid |
1514
|
|
|
cnx.commit() |
1515
|
|
|
cursor.close() |
1516
|
|
|
cnx.disconnect() |
1517
|
|
|
|
1518
|
|
|
resp.status = falcon.HTTP_201 |
1519
|
|
|
resp.location = '/equipments/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
1520
|
|
|
|
1521
|
|
|
|
1522
|
|
View Code Duplication |
class EquipmentOfflineMeterItem: |
|
|
|
|
1523
|
|
|
@staticmethod |
1524
|
|
|
def __init__(): |
1525
|
|
|
pass |
1526
|
|
|
|
1527
|
|
|
@staticmethod |
1528
|
|
|
def on_options(req, resp, id_, mid): |
1529
|
|
|
resp.status = falcon.HTTP_200 |
1530
|
|
|
|
1531
|
|
|
@staticmethod |
1532
|
|
|
def on_delete(req, resp, id_, mid): |
1533
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1534
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1535
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1536
|
|
|
|
1537
|
|
|
if not mid.isdigit() or int(mid) <= 0: |
1538
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1539
|
|
|
description='API.INVALID_OFFLINE_METER_ID') |
1540
|
|
|
|
1541
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1542
|
|
|
cursor = cnx.cursor() |
1543
|
|
|
|
1544
|
|
|
cursor.execute(" SELECT name " |
1545
|
|
|
" FROM tbl_equipments " |
1546
|
|
|
" WHERE id = %s ", (id_,)) |
1547
|
|
|
if cursor.fetchone() is None: |
1548
|
|
|
cursor.close() |
1549
|
|
|
cnx.disconnect() |
1550
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
1551
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1552
|
|
|
|
1553
|
|
|
cursor.execute(" SELECT name " |
1554
|
|
|
" FROM tbl_offline_meters " |
1555
|
|
|
" WHERE id = %s ", (mid,)) |
1556
|
|
|
if cursor.fetchone() is None: |
1557
|
|
|
cursor.close() |
1558
|
|
|
cnx.disconnect() |
1559
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
1560
|
|
|
description='API.OFFLINE_METER_NOT_FOUND') |
1561
|
|
|
|
1562
|
|
|
cursor.execute(" SELECT id " |
1563
|
|
|
" FROM tbl_equipments_offline_meters " |
1564
|
|
|
" WHERE equipment_id = %s AND offline_meter_id = %s ", (id_, mid)) |
1565
|
|
|
if cursor.fetchone() is None: |
1566
|
|
|
cursor.close() |
1567
|
|
|
cnx.disconnect() |
1568
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
1569
|
|
|
description='API.EQUIPMENT_OFFLINE_METER_RELATION_NOT_FOUND') |
1570
|
|
|
|
1571
|
|
|
cursor.execute(" DELETE FROM tbl_equipments_offline_meters " |
1572
|
|
|
" WHERE equipment_id = %s AND offline_meter_id = %s ", (id_, mid)) |
1573
|
|
|
cnx.commit() |
1574
|
|
|
|
1575
|
|
|
cursor.close() |
1576
|
|
|
cnx.disconnect() |
1577
|
|
|
|
1578
|
|
|
resp.status = falcon.HTTP_204 |
1579
|
|
|
|
1580
|
|
|
|
1581
|
|
View Code Duplication |
class EquipmentVirtualMeterCollection: |
|
|
|
|
1582
|
|
|
@staticmethod |
1583
|
|
|
def __init__(): |
1584
|
|
|
pass |
1585
|
|
|
|
1586
|
|
|
@staticmethod |
1587
|
|
|
def on_options(req, resp, id_): |
1588
|
|
|
resp.status = falcon.HTTP_200 |
1589
|
|
|
|
1590
|
|
|
@staticmethod |
1591
|
|
|
def on_get(req, resp, id_): |
1592
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1593
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1594
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1595
|
|
|
|
1596
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1597
|
|
|
cursor = cnx.cursor(dictionary=True) |
1598
|
|
|
|
1599
|
|
|
cursor.execute(" SELECT name " |
1600
|
|
|
" FROM tbl_equipments " |
1601
|
|
|
" WHERE id = %s ", (id_,)) |
1602
|
|
|
if cursor.fetchone() is None: |
1603
|
|
|
cursor.close() |
1604
|
|
|
cnx.disconnect() |
1605
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
1606
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1607
|
|
|
|
1608
|
|
|
query = (" SELECT id, name, uuid " |
1609
|
|
|
" FROM tbl_energy_categories ") |
1610
|
|
|
cursor.execute(query) |
1611
|
|
|
rows_energy_categories = cursor.fetchall() |
1612
|
|
|
|
1613
|
|
|
energy_category_dict = dict() |
1614
|
|
|
if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
1615
|
|
|
for row in rows_energy_categories: |
1616
|
|
|
energy_category_dict[row['id']] = {"id": row['id'], |
1617
|
|
|
"name": row['name'], |
1618
|
|
|
"uuid": row['uuid']} |
1619
|
|
|
|
1620
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
1621
|
|
|
" FROM tbl_equipments e, tbl_equipments_virtual_meters em, tbl_virtual_meters m " |
1622
|
|
|
" WHERE em.equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
1623
|
|
|
" ORDER BY m.id ") |
1624
|
|
|
cursor.execute(query, (id_,)) |
1625
|
|
|
rows = cursor.fetchall() |
1626
|
|
|
|
1627
|
|
|
result = list() |
1628
|
|
|
if rows is not None and len(rows) > 0: |
1629
|
|
|
for row in rows: |
1630
|
|
|
energy_category = energy_category_dict.get(row['energy_category_id'], None) |
1631
|
|
|
meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
1632
|
|
|
"energy_category": energy_category, |
1633
|
|
|
"is_output": bool(row['is_output'])} |
1634
|
|
|
result.append(meta_result) |
1635
|
|
|
|
1636
|
|
|
resp.body = json.dumps(result) |
1637
|
|
|
|
1638
|
|
|
@staticmethod |
1639
|
|
|
def on_post(req, resp, id_): |
1640
|
|
|
"""Handles POST requests""" |
1641
|
|
|
try: |
1642
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1643
|
|
|
except Exception as ex: |
1644
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
1645
|
|
|
|
1646
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1647
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1648
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1649
|
|
|
|
1650
|
|
|
new_values = json.loads(raw_json) |
1651
|
|
|
|
1652
|
|
|
if 'virtual_meter_id' not in new_values['data'].keys() or \ |
1653
|
|
|
not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
1654
|
|
|
new_values['data']['virtual_meter_id'] <= 0: |
1655
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1656
|
|
|
description='API.INVALID_VIRTUAL_METER_ID') |
1657
|
|
|
virtual_meter_id = new_values['data']['virtual_meter_id'] |
1658
|
|
|
|
1659
|
|
|
if 'is_output' not in new_values['data'].keys() or \ |
1660
|
|
|
not isinstance(new_values['data']['is_output'], bool): |
1661
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1662
|
|
|
description='API.INVALID_IS_OUTPUT_VALUE') |
1663
|
|
|
is_output = new_values['data']['is_output'] |
1664
|
|
|
|
1665
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1666
|
|
|
cursor = cnx.cursor() |
1667
|
|
|
|
1668
|
|
|
cursor.execute(" SELECT name " |
1669
|
|
|
" from tbl_equipments " |
1670
|
|
|
" WHERE id = %s ", (id_,)) |
1671
|
|
|
if cursor.fetchone() is None: |
1672
|
|
|
cursor.close() |
1673
|
|
|
cnx.disconnect() |
1674
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
1675
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1676
|
|
|
|
1677
|
|
|
cursor.execute(" SELECT name " |
1678
|
|
|
" FROM tbl_virtual_meters " |
1679
|
|
|
" WHERE id = %s ", (virtual_meter_id,)) |
1680
|
|
|
if cursor.fetchone() is None: |
1681
|
|
|
cursor.close() |
1682
|
|
|
cnx.disconnect() |
1683
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
1684
|
|
|
description='API.VIRTUAL_METER_NOT_FOUND') |
1685
|
|
|
|
1686
|
|
|
query = (" SELECT id " |
1687
|
|
|
" FROM tbl_equipments_virtual_meters " |
1688
|
|
|
" WHERE equipment_id = %s AND virtual_meter_id = %s") |
1689
|
|
|
cursor.execute(query, (id_, virtual_meter_id,)) |
1690
|
|
|
if cursor.fetchone() is not None: |
1691
|
|
|
cursor.close() |
1692
|
|
|
cnx.disconnect() |
1693
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
1694
|
|
|
description='API.EQUIPMENT_VIRTUAL_METER_RELATION_EXISTED') |
1695
|
|
|
|
1696
|
|
|
add_row = (" INSERT INTO tbl_equipments_virtual_meters (equipment_id, virtual_meter_id, is_output ) " |
1697
|
|
|
" VALUES (%s, %s, %s) ") |
1698
|
|
|
cursor.execute(add_row, (id_, virtual_meter_id, is_output)) |
1699
|
|
|
new_id = cursor.lastrowid |
1700
|
|
|
cnx.commit() |
1701
|
|
|
cursor.close() |
1702
|
|
|
cnx.disconnect() |
1703
|
|
|
|
1704
|
|
|
resp.status = falcon.HTTP_201 |
1705
|
|
|
resp.location = '/equipments/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
1706
|
|
|
|
1707
|
|
|
|
1708
|
|
View Code Duplication |
class EquipmentVirtualMeterItem: |
|
|
|
|
1709
|
|
|
@staticmethod |
1710
|
|
|
def __init__(): |
1711
|
|
|
pass |
1712
|
|
|
|
1713
|
|
|
@staticmethod |
1714
|
|
|
def on_options(req, resp, id_, mid): |
1715
|
|
|
resp.status = falcon.HTTP_200 |
1716
|
|
|
|
1717
|
|
|
@staticmethod |
1718
|
|
|
def on_delete(req, resp, id_, mid): |
1719
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1720
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1721
|
|
|
description='API.INVALID_EQUIPMENT_ID') |
1722
|
|
|
|
1723
|
|
|
if not mid.isdigit() or int(mid) <= 0: |
1724
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
1725
|
|
|
description='API.INVALID_VIRTUAL_METER_ID') |
1726
|
|
|
|
1727
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1728
|
|
|
cursor = cnx.cursor() |
1729
|
|
|
|
1730
|
|
|
cursor.execute(" SELECT name " |
1731
|
|
|
" FROM tbl_equipments " |
1732
|
|
|
" WHERE id = %s ", (id_,)) |
1733
|
|
|
if cursor.fetchone() is None: |
1734
|
|
|
cursor.close() |
1735
|
|
|
cnx.disconnect() |
1736
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
1737
|
|
|
description='API.EQUIPMENT_NOT_FOUND') |
1738
|
|
|
|
1739
|
|
|
cursor.execute(" SELECT name " |
1740
|
|
|
" FROM tbl_virtual_meters " |
1741
|
|
|
" WHERE id = %s ", (mid,)) |
1742
|
|
|
if cursor.fetchone() is None: |
1743
|
|
|
cursor.close() |
1744
|
|
|
cnx.disconnect() |
1745
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
1746
|
|
|
description='API.VIRTUAL_METER_NOT_FOUND') |
1747
|
|
|
|
1748
|
|
|
cursor.execute(" SELECT id " |
1749
|
|
|
" FROM tbl_equipments_virtual_meters " |
1750
|
|
|
" WHERE equipment_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
1751
|
|
|
if cursor.fetchone() is None: |
1752
|
|
|
cursor.close() |
1753
|
|
|
cnx.disconnect() |
1754
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
1755
|
|
|
description='API.EQUIPMENT_VIRTUAL_METER_RELATION_NOT_FOUND') |
1756
|
|
|
|
1757
|
|
|
cursor.execute(" DELETE FROM tbl_equipments_virtual_meters " |
1758
|
|
|
" WHERE equipment_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
1759
|
|
|
cnx.commit() |
1760
|
|
|
|
1761
|
|
|
cursor.close() |
1762
|
|
|
cnx.disconnect() |
1763
|
|
|
|
1764
|
|
|
resp.status = falcon.HTTP_204 |
1765
|
|
|
|