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 |
7
|
|
|
import config |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class MicrogridCollection: |
11
|
|
|
def __init__(self): |
12
|
|
|
""""Initializes MicrogridCollection""" |
13
|
|
|
pass |
14
|
|
|
|
15
|
|
|
@staticmethod |
16
|
|
|
def on_options(req, resp): |
17
|
|
|
_ = req |
18
|
|
|
resp.status = falcon.HTTP_200 |
19
|
|
|
|
20
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
21
|
|
|
def on_get(req, resp): |
22
|
|
|
access_control(req) |
23
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
24
|
|
|
cursor = cnx.cursor() |
25
|
|
|
|
26
|
|
|
query = (" SELECT id, name, uuid " |
27
|
|
|
" FROM tbl_contacts ") |
28
|
|
|
cursor.execute(query) |
29
|
|
|
rows_contacts = cursor.fetchall() |
30
|
|
|
|
31
|
|
|
contact_dict = dict() |
32
|
|
|
if rows_contacts is not None and len(rows_contacts) > 0: |
33
|
|
|
for row in rows_contacts: |
34
|
|
|
contact_dict[row[0]] = {"id": row[0], |
35
|
|
|
"name": row[1], |
36
|
|
|
"uuid": row[2]} |
37
|
|
|
|
38
|
|
|
query = (" SELECT id, name, uuid " |
39
|
|
|
" FROM tbl_cost_centers ") |
40
|
|
|
cursor.execute(query) |
41
|
|
|
rows_cost_centers = cursor.fetchall() |
42
|
|
|
|
43
|
|
|
cost_center_dict = dict() |
44
|
|
|
if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
45
|
|
|
for row in rows_cost_centers: |
46
|
|
|
cost_center_dict[row[0]] = {"id": row[0], |
47
|
|
|
"name": row[1], |
48
|
|
|
"uuid": row[2]} |
49
|
|
|
svg_dict = dict() |
50
|
|
|
|
51
|
|
|
query = (" SELECT id, name, uuid " |
52
|
|
|
" FROM tbl_svgs ") |
53
|
|
|
cursor.execute(query) |
54
|
|
|
rows_svgs = cursor.fetchall() |
55
|
|
|
if rows_svgs is not None and len(rows_svgs) > 0: |
56
|
|
|
for row in rows_svgs: |
57
|
|
|
svg_dict[row[0]] = {"id": row[0], |
58
|
|
|
"name": row[1], |
59
|
|
|
"uuid": row[2]} |
60
|
|
|
|
61
|
|
|
query = (" SELECT id, name, uuid, " |
62
|
|
|
" address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
63
|
|
|
" contact_id, cost_center_id, serial_number, svg_id, is_cost_data_displayed, " |
64
|
|
|
" phase_of_lifecycle, description " |
65
|
|
|
" FROM tbl_microgrids " |
66
|
|
|
" ORDER BY id ") |
67
|
|
|
cursor.execute(query) |
68
|
|
|
rows_microgrids = cursor.fetchall() |
69
|
|
|
|
70
|
|
|
result = list() |
71
|
|
|
if rows_microgrids is not None and len(rows_microgrids) > 0: |
72
|
|
|
for row in rows_microgrids: |
73
|
|
|
meta_result = {"id": row[0], |
74
|
|
|
"name": row[1], |
75
|
|
|
"uuid": row[2], |
76
|
|
|
"address": row[3], |
77
|
|
|
"postal_code": row[4], |
78
|
|
|
"latitude": row[5], |
79
|
|
|
"longitude": row[6], |
80
|
|
|
"rated_capacity": row[7], |
81
|
|
|
"rated_power": row[8], |
82
|
|
|
"contact": contact_dict.get(row[9], None), |
83
|
|
|
"cost_center": cost_center_dict.get(row[10], None), |
84
|
|
|
"serial_number": row[11], |
85
|
|
|
"svg": svg_dict.get(row[12], None), |
86
|
|
|
"is_cost_data_displayed": bool(row[13]), |
87
|
|
|
"phase_of_lifecycle": row[14], |
88
|
|
|
"description": row[15], |
89
|
|
|
"qrcode": 'microgrid:' + row[2]} |
90
|
|
|
result.append(meta_result) |
91
|
|
|
|
92
|
|
|
cursor.close() |
93
|
|
|
cnx.close() |
94
|
|
|
resp.text = json.dumps(result) |
95
|
|
|
|
96
|
|
|
@staticmethod |
97
|
|
|
@user_logger |
98
|
|
|
def on_post(req, resp): |
99
|
|
|
"""Handles POST requests""" |
100
|
|
|
admin_control(req) |
101
|
|
|
try: |
102
|
|
|
raw_json = req.stream.read().decode('utf-8') |
103
|
|
|
except Exception as ex: |
104
|
|
|
print(str(ex)) |
105
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
106
|
|
|
title='API.BAD_REQUEST', |
107
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
108
|
|
|
|
109
|
|
|
new_values = json.loads(raw_json) |
110
|
|
|
|
111
|
|
|
if 'name' not in new_values['data'].keys() or \ |
112
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
113
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
114
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
115
|
|
|
description='API.INVALID_MICROGRID_NAME') |
116
|
|
|
name = str.strip(new_values['data']['name']) |
117
|
|
|
|
118
|
|
|
if 'address' not in new_values['data'].keys() or \ |
119
|
|
|
not isinstance(new_values['data']['address'], str) or \ |
120
|
|
|
len(str.strip(new_values['data']['address'])) == 0: |
121
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
122
|
|
|
description='API.INVALID_ADDRESS_VALUE') |
123
|
|
|
address = str.strip(new_values['data']['address']) |
124
|
|
|
|
125
|
|
|
if 'postal_code' not in new_values['data'].keys() or \ |
126
|
|
|
not isinstance(new_values['data']['postal_code'], str) or \ |
127
|
|
|
len(str.strip(new_values['data']['postal_code'])) == 0: |
128
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
129
|
|
|
description='API.INVALID_POSTAL_CODE_VALUE') |
130
|
|
|
postal_code = str.strip(new_values['data']['postal_code']) |
131
|
|
|
|
132
|
|
|
if 'latitude' not in new_values['data'].keys() or \ |
133
|
|
|
not (isinstance(new_values['data']['latitude'], float) or |
134
|
|
|
isinstance(new_values['data']['latitude'], int)) or \ |
135
|
|
|
new_values['data']['latitude'] < -90.0 or \ |
136
|
|
|
new_values['data']['latitude'] > 90.0: |
137
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
138
|
|
|
description='API.INVALID_LATITUDE_VALUE') |
139
|
|
|
latitude = new_values['data']['latitude'] |
140
|
|
|
|
141
|
|
|
if 'longitude' not in new_values['data'].keys() or \ |
142
|
|
|
not (isinstance(new_values['data']['longitude'], float) or |
143
|
|
|
isinstance(new_values['data']['longitude'], int)) or \ |
144
|
|
|
new_values['data']['longitude'] < -180.0 or \ |
145
|
|
|
new_values['data']['longitude'] > 180.0: |
146
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
147
|
|
|
description='API.INVALID_LONGITUDE_VALUE') |
148
|
|
|
longitude = new_values['data']['longitude'] |
149
|
|
|
|
150
|
|
|
if 'rated_capacity' not in new_values['data'].keys() or \ |
151
|
|
|
not (isinstance(new_values['data']['rated_capacity'], float) or |
152
|
|
|
isinstance(new_values['data']['rated_capacity'], int)) or \ |
153
|
|
|
new_values['data']['rated_capacity'] <= 0.0: |
154
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
155
|
|
|
description='API.INVALID_RATED_CAPACITY') |
156
|
|
|
rated_capacity = new_values['data']['rated_capacity'] |
157
|
|
|
|
158
|
|
|
if 'rated_power' not in new_values['data'].keys() or \ |
159
|
|
|
not (isinstance(new_values['data']['rated_power'], float) or |
160
|
|
|
isinstance(new_values['data']['rated_power'], int)) or \ |
161
|
|
|
new_values['data']['rated_power'] <= 0.0: |
162
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
163
|
|
|
description='API.INVALID_RATED_POWER_VALUE') |
164
|
|
|
rated_power = new_values['data']['rated_power'] |
165
|
|
|
|
166
|
|
|
if 'contact_id' not in new_values['data'].keys() or \ |
167
|
|
|
not isinstance(new_values['data']['contact_id'], int) or \ |
168
|
|
|
new_values['data']['contact_id'] <= 0: |
169
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
170
|
|
|
description='API.INVALID_CONTACT_ID') |
171
|
|
|
contact_id = new_values['data']['contact_id'] |
172
|
|
|
|
173
|
|
|
if 'cost_center_id' not in new_values['data'].keys() or \ |
174
|
|
|
not isinstance(new_values['data']['cost_center_id'], int) or \ |
175
|
|
|
new_values['data']['cost_center_id'] <= 0: |
176
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
177
|
|
|
description='API.INVALID_COST_CENTER_ID') |
178
|
|
|
cost_center_id = new_values['data']['cost_center_id'] |
179
|
|
|
|
180
|
|
|
if 'serial_number' not in new_values['data'].keys() or \ |
181
|
|
|
not isinstance(new_values['data']['serial_number'], str) or \ |
182
|
|
|
len(str.strip(new_values['data']['serial_number'])) == 0: |
183
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
184
|
|
|
description='API.INVALID_SERIAL_NUMBER') |
185
|
|
|
serial_number = str.strip(new_values['data']['serial_number']) |
186
|
|
|
|
187
|
|
|
if 'svg_id' not in new_values['data'].keys() or \ |
188
|
|
|
not isinstance(new_values['data']['svg_id'], int) or \ |
189
|
|
|
new_values['data']['svg_id'] <= 0: |
190
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
191
|
|
|
description='API.INVALID_SVG_ID') |
192
|
|
|
svg_id = new_values['data']['svg_id'] |
193
|
|
|
|
194
|
|
|
if 'is_cost_data_displayed' not in new_values['data'].keys() or \ |
195
|
|
|
not isinstance(new_values['data']['is_cost_data_displayed'], bool): |
196
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
197
|
|
|
description='API.INVALID_IS_COST_DATA_DISPLAYED') |
198
|
|
|
is_cost_data_displayed = new_values['data']['is_cost_data_displayed'] |
199
|
|
|
|
200
|
|
|
if 'phase_of_lifecycle' not in new_values['data'].keys() or \ |
201
|
|
|
not isinstance(new_values['data']['phase_of_lifecycle'], str) or \ |
202
|
|
|
len(str.strip(new_values['data']['phase_of_lifecycle'])) == 0: |
203
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
204
|
|
|
description='API.INVALID_PHASE_OF_LIFECYCLE') |
205
|
|
|
phase_of_lifecycle = str.strip(new_values['data']['phase_of_lifecycle']) |
206
|
|
|
|
207
|
|
|
if 'description' in new_values['data'].keys() and \ |
208
|
|
|
new_values['data']['description'] is not None and \ |
209
|
|
|
len(str(new_values['data']['description'])) > 0: |
210
|
|
|
description = str.strip(new_values['data']['description']) |
211
|
|
|
else: |
212
|
|
|
description = None |
213
|
|
|
|
214
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
215
|
|
|
cursor = cnx.cursor() |
216
|
|
|
|
217
|
|
|
cursor.execute(" SELECT name " |
218
|
|
|
" FROM tbl_microgrids " |
219
|
|
|
" WHERE name = %s ", (name,)) |
220
|
|
|
if cursor.fetchone() is not None: |
221
|
|
|
cursor.close() |
222
|
|
|
cnx.close() |
223
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
224
|
|
|
description='API.MICROGRID_NAME_IS_ALREADY_IN_USE') |
225
|
|
|
|
226
|
|
|
cursor.execute(" SELECT name " |
227
|
|
|
" FROM tbl_contacts " |
228
|
|
|
" WHERE id = %s ", |
229
|
|
|
(new_values['data']['contact_id'],)) |
230
|
|
|
row = cursor.fetchone() |
231
|
|
|
if row is None: |
232
|
|
|
cursor.close() |
233
|
|
|
cnx.close() |
234
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
235
|
|
|
description='API.CONTACT_NOT_FOUND') |
236
|
|
|
|
237
|
|
|
cursor.execute(" SELECT name " |
238
|
|
|
" FROM tbl_cost_centers " |
239
|
|
|
" WHERE id = %s ", |
240
|
|
|
(new_values['data']['cost_center_id'],)) |
241
|
|
|
row = cursor.fetchone() |
242
|
|
|
if row is None: |
243
|
|
|
cursor.close() |
244
|
|
|
cnx.close() |
245
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
246
|
|
|
description='API.COST_CENTER_NOT_FOUND') |
247
|
|
|
|
248
|
|
|
cursor.execute(" SELECT name " |
249
|
|
|
" FROM tbl_svgs " |
250
|
|
|
" WHERE id = %s ", |
251
|
|
|
(svg_id,)) |
252
|
|
|
row = cursor.fetchone() |
253
|
|
|
if row is None: |
254
|
|
|
cursor.close() |
255
|
|
|
cnx.close() |
256
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
257
|
|
|
description='API.SVG_NOT_FOUND') |
258
|
|
|
|
259
|
|
|
add_values = (" INSERT INTO tbl_microgrids " |
260
|
|
|
" (name, uuid, address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
261
|
|
|
" contact_id, cost_center_id, serial_number, svg_id, is_cost_data_displayed, " |
262
|
|
|
" phase_of_lifecycle, description) " |
263
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
264
|
|
|
cursor.execute(add_values, (name, |
265
|
|
|
str(uuid.uuid4()), |
266
|
|
|
address, |
267
|
|
|
postal_code, |
268
|
|
|
latitude, |
269
|
|
|
longitude, |
270
|
|
|
rated_capacity, |
271
|
|
|
rated_power, |
272
|
|
|
contact_id, |
273
|
|
|
cost_center_id, |
274
|
|
|
serial_number, |
275
|
|
|
svg_id, |
276
|
|
|
is_cost_data_displayed, |
277
|
|
|
phase_of_lifecycle, |
278
|
|
|
description)) |
279
|
|
|
new_id = cursor.lastrowid |
280
|
|
|
cnx.commit() |
281
|
|
|
cursor.close() |
282
|
|
|
cnx.close() |
283
|
|
|
|
284
|
|
|
resp.status = falcon.HTTP_201 |
285
|
|
|
resp.location = '/microgrids/' + str(new_id) |
286
|
|
|
|
287
|
|
|
|
288
|
|
|
class MicrogridItem: |
289
|
|
|
def __init__(self): |
290
|
|
|
""""Initializes MicrogridItem""" |
291
|
|
|
pass |
292
|
|
|
|
293
|
|
|
@staticmethod |
294
|
|
|
def on_options(req, resp, id_): |
295
|
|
|
_ = req |
296
|
|
|
resp.status = falcon.HTTP_200 |
297
|
|
|
_ = id_ |
298
|
|
|
|
299
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
300
|
|
|
def on_get(req, resp, id_): |
301
|
|
|
access_control(req) |
302
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
303
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
304
|
|
|
description='API.INVALID_MICROGRID_ID') |
305
|
|
|
|
306
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
307
|
|
|
cursor = cnx.cursor() |
308
|
|
|
|
309
|
|
|
query = (" SELECT id, name, uuid " |
310
|
|
|
" FROM tbl_contacts ") |
311
|
|
|
cursor.execute(query) |
312
|
|
|
rows_contacts = cursor.fetchall() |
313
|
|
|
|
314
|
|
|
contact_dict = dict() |
315
|
|
|
if rows_contacts is not None and len(rows_contacts) > 0: |
316
|
|
|
for row in rows_contacts: |
317
|
|
|
contact_dict[row[0]] = {"id": row[0], |
318
|
|
|
"name": row[1], |
319
|
|
|
"uuid": row[2]} |
320
|
|
|
|
321
|
|
|
query = (" SELECT id, name, uuid " |
322
|
|
|
" FROM tbl_cost_centers ") |
323
|
|
|
cursor.execute(query) |
324
|
|
|
rows_cost_centers = cursor.fetchall() |
325
|
|
|
|
326
|
|
|
cost_center_dict = dict() |
327
|
|
|
if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
328
|
|
|
for row in rows_cost_centers: |
329
|
|
|
cost_center_dict[row[0]] = {"id": row[0], |
330
|
|
|
"name": row[1], |
331
|
|
|
"uuid": row[2]} |
332
|
|
|
svg_dict = dict() |
333
|
|
|
|
334
|
|
|
query = (" SELECT id, name, uuid " |
335
|
|
|
" FROM tbl_svgs ") |
336
|
|
|
cursor.execute(query) |
337
|
|
|
rows_svgs = cursor.fetchall() |
338
|
|
|
if rows_svgs is not None and len(rows_svgs) > 0: |
339
|
|
|
for row in rows_svgs: |
340
|
|
|
svg_dict[row[0]] = {"id": row[0], |
341
|
|
|
"name": row[1], |
342
|
|
|
"uuid": row[2]} |
343
|
|
|
|
344
|
|
|
query = (" SELECT id, name, uuid, " |
345
|
|
|
" address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
346
|
|
|
" contact_id, cost_center_id, serial_number, svg_id, is_cost_data_displayed, " |
347
|
|
|
" phase_of_lifecycle, description " |
348
|
|
|
" FROM tbl_microgrids " |
349
|
|
|
" WHERE id = %s ") |
350
|
|
|
cursor.execute(query, (id_,)) |
351
|
|
|
row = cursor.fetchone() |
352
|
|
|
cursor.close() |
353
|
|
|
cnx.close() |
354
|
|
|
|
355
|
|
|
if row is None: |
356
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
357
|
|
|
description='API.MICROGRID_NOT_FOUND') |
358
|
|
|
else: |
359
|
|
|
meta_result = {"id": row[0], |
360
|
|
|
"name": row[1], |
361
|
|
|
"uuid": row[2], |
362
|
|
|
"address": row[3], |
363
|
|
|
"postal_code": row[4], |
364
|
|
|
"latitude": row[5], |
365
|
|
|
"longitude": row[6], |
366
|
|
|
"rated_capacity": row[7], |
367
|
|
|
"rated_power": row[8], |
368
|
|
|
"contact": contact_dict.get(row[9], None), |
369
|
|
|
"cost_center": cost_center_dict.get(row[10], None), |
370
|
|
|
"serial_number": row[11], |
371
|
|
|
"svg": svg_dict.get(row[12], None), |
372
|
|
|
"is_cost_data_displayed": bool(row[13]), |
373
|
|
|
"phase_of_lifecycle": row[14], |
374
|
|
|
"description": row[15], |
375
|
|
|
"qrcode": 'microgrid:' + row[2]} |
376
|
|
|
|
377
|
|
|
resp.text = json.dumps(meta_result) |
378
|
|
|
|
379
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
380
|
|
|
@user_logger |
381
|
|
|
def on_delete(req, resp, id_): |
382
|
|
|
admin_control(req) |
383
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
384
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
385
|
|
|
description='API.INVALID_MICROGRID_ID') |
386
|
|
|
|
387
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
388
|
|
|
cursor = cnx.cursor() |
389
|
|
|
|
390
|
|
|
cursor.execute(" SELECT name " |
391
|
|
|
" FROM tbl_microgrids " |
392
|
|
|
" WHERE id = %s ", (id_,)) |
393
|
|
|
if cursor.fetchone() is None: |
394
|
|
|
cursor.close() |
395
|
|
|
cnx.close() |
396
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
397
|
|
|
description='API.MICROGRID_NOT_FOUND') |
398
|
|
|
|
399
|
|
|
# check relation with spaces |
400
|
|
|
cursor.execute(" SELECT id " |
401
|
|
|
" FROM tbl_spaces_microgrids " |
402
|
|
|
" WHERE microgrid_id = %s ", (id_,)) |
403
|
|
|
rows_spaces = cursor.fetchall() |
404
|
|
|
if rows_spaces is not None and len(rows_spaces) > 0: |
405
|
|
|
cursor.close() |
406
|
|
|
cnx.close() |
407
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
408
|
|
|
title='API.BAD_REQUEST', |
409
|
|
|
description='API.THERE_IS_RELATION_WITH_SPACES') |
410
|
|
|
|
411
|
|
|
# check relation with power plants |
412
|
|
|
cursor.execute(" SELECT id " |
413
|
|
|
" FROM tbl_virtual_power_plants_microgrids " |
414
|
|
|
" WHERE microgrid_id = %s ", |
415
|
|
|
(id_,)) |
416
|
|
|
rows_power_plants = cursor.fetchall() |
417
|
|
|
if rows_power_plants is not None and len(rows_power_plants) > 0: |
418
|
|
|
cursor.close() |
419
|
|
|
cnx.close() |
420
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
421
|
|
|
title='API.BAD_REQUEST', |
422
|
|
|
description='API.THERE_IS_RELATION_WITH_VIRTUAL_POWER_PLANTS') |
423
|
|
|
|
424
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_batteries WHERE microgrid_id = %s ", (id_,)) |
425
|
|
|
cnx.commit() |
426
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_commands WHERE microgrid_id = %s ", (id_,)) |
427
|
|
|
cnx.commit() |
428
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_power_conversion_systems WHERE microgrid_id = %s ", (id_,)) |
429
|
|
|
cnx.commit() |
430
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_evchargers WHERE microgrid_id = %s ", (id_,)) |
431
|
|
|
cnx.commit() |
432
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_generators WHERE microgrid_id = %s ", (id_,)) |
433
|
|
|
cnx.commit() |
434
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_grids WHERE microgrid_id = %s ", (id_,)) |
435
|
|
|
cnx.commit() |
436
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_heatpumps WHERE microgrid_id = %s ", (id_,)) |
437
|
|
|
cnx.commit() |
438
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_loads WHERE microgrid_id = %s ", (id_,)) |
439
|
|
|
cnx.commit() |
440
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_photovoltaics WHERE microgrid_id = %s ", (id_,)) |
441
|
|
|
cnx.commit() |
442
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_sensors WHERE microgrid_id = %s ", (id_,)) |
443
|
|
|
cnx.commit() |
444
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_users WHERE microgrid_id = %s ", (id_,)) |
445
|
|
|
cnx.commit() |
446
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids WHERE id = %s ", (id_,)) |
447
|
|
|
cnx.commit() |
448
|
|
|
|
449
|
|
|
cursor.close() |
450
|
|
|
cnx.close() |
451
|
|
|
|
452
|
|
|
resp.status = falcon.HTTP_204 |
453
|
|
|
|
454
|
|
|
@staticmethod |
455
|
|
|
@user_logger |
456
|
|
|
def on_put(req, resp, id_): |
457
|
|
|
"""Handles PUT requests""" |
458
|
|
|
admin_control(req) |
459
|
|
|
try: |
460
|
|
|
raw_json = req.stream.read().decode('utf-8') |
461
|
|
|
except Exception as ex: |
462
|
|
|
print(str(ex)) |
463
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
464
|
|
|
title='API.BAD_REQUEST', |
465
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
466
|
|
|
|
467
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
468
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
469
|
|
|
description='API.INVALID_MICROGRID_ID') |
470
|
|
|
|
471
|
|
|
new_values = json.loads(raw_json) |
472
|
|
|
|
473
|
|
|
if 'name' not in new_values['data'].keys() or \ |
474
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
475
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
476
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
477
|
|
|
description='API.INVALID_MICROGRID_NAME') |
478
|
|
|
name = str.strip(new_values['data']['name']) |
479
|
|
|
|
480
|
|
|
if 'address' not in new_values['data'].keys() or \ |
481
|
|
|
not isinstance(new_values['data']['address'], str) or \ |
482
|
|
|
len(str.strip(new_values['data']['address'])) == 0: |
483
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
484
|
|
|
description='API.INVALID_ADDRESS_VALUE') |
485
|
|
|
address = str.strip(new_values['data']['address']) |
486
|
|
|
|
487
|
|
|
if 'postal_code' not in new_values['data'].keys() or \ |
488
|
|
|
not isinstance(new_values['data']['postal_code'], str) or \ |
489
|
|
|
len(str.strip(new_values['data']['postal_code'])) == 0: |
490
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
491
|
|
|
description='API.INVALID_POSTAL_CODE_VALUE') |
492
|
|
|
postal_code = str.strip(new_values['data']['postal_code']) |
493
|
|
|
|
494
|
|
|
if 'latitude' not in new_values['data'].keys() or \ |
495
|
|
|
not (isinstance(new_values['data']['latitude'], float) or |
496
|
|
|
isinstance(new_values['data']['latitude'], int)) or \ |
497
|
|
|
new_values['data']['latitude'] < -90.0 or \ |
498
|
|
|
new_values['data']['latitude'] > 90.0: |
499
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
500
|
|
|
description='API.INVALID_LATITUDE_VALUE') |
501
|
|
|
latitude = new_values['data']['latitude'] |
502
|
|
|
|
503
|
|
|
if 'longitude' not in new_values['data'].keys() or \ |
504
|
|
|
not (isinstance(new_values['data']['longitude'], float) or |
505
|
|
|
isinstance(new_values['data']['longitude'], int)) or \ |
506
|
|
|
new_values['data']['longitude'] < -180.0 or \ |
507
|
|
|
new_values['data']['longitude'] > 180.0: |
508
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
509
|
|
|
description='API.INVALID_LONGITUDE_VALUE') |
510
|
|
|
longitude = new_values['data']['longitude'] |
511
|
|
|
|
512
|
|
|
if 'rated_capacity' not in new_values['data'].keys() or \ |
513
|
|
|
not (isinstance(new_values['data']['rated_capacity'], float) or |
514
|
|
|
isinstance(new_values['data']['rated_capacity'], int)) or \ |
515
|
|
|
new_values['data']['rated_capacity'] <= 0.0: |
516
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
517
|
|
|
description='API.INVALID_RATED_CAPACITY') |
518
|
|
|
rated_capacity = new_values['data']['rated_capacity'] |
519
|
|
|
|
520
|
|
|
if 'rated_power' not in new_values['data'].keys() or \ |
521
|
|
|
not (isinstance(new_values['data']['rated_power'], float) or |
522
|
|
|
isinstance(new_values['data']['rated_power'], int)) or \ |
523
|
|
|
new_values['data']['rated_power'] <= 0.0: |
524
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
525
|
|
|
description='API.INVALID_RATED_POWER_VALUE') |
526
|
|
|
rated_power = new_values['data']['rated_power'] |
527
|
|
|
|
528
|
|
|
if 'contact_id' not in new_values['data'].keys() or \ |
529
|
|
|
not isinstance(new_values['data']['contact_id'], int) or \ |
530
|
|
|
new_values['data']['contact_id'] <= 0: |
531
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
532
|
|
|
description='API.INVALID_CONTACT_ID') |
533
|
|
|
contact_id = new_values['data']['contact_id'] |
534
|
|
|
|
535
|
|
|
if 'cost_center_id' not in new_values['data'].keys() or \ |
536
|
|
|
not isinstance(new_values['data']['cost_center_id'], int) or \ |
537
|
|
|
new_values['data']['cost_center_id'] <= 0: |
538
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
539
|
|
|
description='API.INVALID_COST_CENTER_ID') |
540
|
|
|
cost_center_id = new_values['data']['cost_center_id'] |
541
|
|
|
|
542
|
|
|
if 'serial_number' not in new_values['data'].keys() or \ |
543
|
|
|
not isinstance(new_values['data']['serial_number'], str) or \ |
544
|
|
|
len(str.strip(new_values['data']['serial_number'])) == 0: |
545
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
546
|
|
|
description='API.INVALID_SERIAL_NUMBER') |
547
|
|
|
serial_number = str.strip(new_values['data']['serial_number']) |
548
|
|
|
|
549
|
|
|
if 'svg_id' not in new_values['data'].keys() or \ |
550
|
|
|
not isinstance(new_values['data']['svg_id'], int) or \ |
551
|
|
|
new_values['data']['svg_id'] <= 0: |
552
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
553
|
|
|
description='API.INVALID_SVG_ID') |
554
|
|
|
svg_id = new_values['data']['svg_id'] |
555
|
|
|
|
556
|
|
|
if 'is_cost_data_displayed' not in new_values['data'].keys() or \ |
557
|
|
|
not isinstance(new_values['data']['is_cost_data_displayed'], bool): |
558
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
559
|
|
|
description='API.INVALID_IS_COST_DATA_DISPLAYED') |
560
|
|
|
is_cost_data_displayed = new_values['data']['is_cost_data_displayed'] |
561
|
|
|
|
562
|
|
|
if 'phase_of_lifecycle' not in new_values['data'].keys() or \ |
563
|
|
|
not isinstance(new_values['data']['phase_of_lifecycle'], str) or \ |
564
|
|
|
len(str.strip(new_values['data']['phase_of_lifecycle'])) == 0: |
565
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
566
|
|
|
description='API.INVALID_MICROGRID_PHASE_OF_LIFECYCLE') |
567
|
|
|
phase_of_lifecycle = str.strip(new_values['data']['phase_of_lifecycle']) |
568
|
|
|
|
569
|
|
|
if 'description' in new_values['data'].keys() and \ |
570
|
|
|
new_values['data']['description'] is not None and \ |
571
|
|
|
len(str(new_values['data']['description'])) > 0: |
572
|
|
|
description = str.strip(new_values['data']['description']) |
573
|
|
|
else: |
574
|
|
|
description = None |
575
|
|
|
|
576
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
577
|
|
|
cursor = cnx.cursor() |
578
|
|
|
|
579
|
|
|
cursor.execute(" SELECT name " |
580
|
|
|
" FROM tbl_microgrids " |
581
|
|
|
" WHERE id = %s ", (id_,)) |
582
|
|
|
if cursor.fetchone() is None: |
583
|
|
|
cursor.close() |
584
|
|
|
cnx.close() |
585
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
586
|
|
|
description='API.MICROGRID_NOT_FOUND') |
587
|
|
|
|
588
|
|
|
cursor.execute(" SELECT name " |
589
|
|
|
" FROM tbl_microgrids " |
590
|
|
|
" WHERE name = %s AND id != %s ", (name, id_)) |
591
|
|
|
if cursor.fetchone() is not None: |
592
|
|
|
cursor.close() |
593
|
|
|
cnx.close() |
594
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
595
|
|
|
description='API.MICROGRID_NAME_IS_ALREADY_IN_USE') |
596
|
|
|
|
597
|
|
|
cursor.execute(" SELECT name " |
598
|
|
|
" FROM tbl_contacts " |
599
|
|
|
" WHERE id = %s ", |
600
|
|
|
(new_values['data']['contact_id'],)) |
601
|
|
|
row = cursor.fetchone() |
602
|
|
|
if row is None: |
603
|
|
|
cursor.close() |
604
|
|
|
cnx.close() |
605
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
606
|
|
|
description='API.CONTACT_NOT_FOUND') |
607
|
|
|
|
608
|
|
|
cursor.execute(" SELECT name " |
609
|
|
|
" FROM tbl_cost_centers " |
610
|
|
|
" WHERE id = %s ", |
611
|
|
|
(new_values['data']['cost_center_id'],)) |
612
|
|
|
row = cursor.fetchone() |
613
|
|
|
if row is None: |
614
|
|
|
cursor.close() |
615
|
|
|
cnx.close() |
616
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
617
|
|
|
description='API.COST_CENTER_NOT_FOUND') |
618
|
|
|
|
619
|
|
|
cursor.execute(" SELECT name " |
620
|
|
|
" FROM tbl_svgs " |
621
|
|
|
" WHERE id = %s ", |
622
|
|
|
(new_values['data']['svg_id'],)) |
623
|
|
|
row = cursor.fetchone() |
624
|
|
|
if row is None: |
625
|
|
|
cursor.close() |
626
|
|
|
cnx.close() |
627
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
628
|
|
|
description='API.SVG_NOT_FOUND') |
629
|
|
|
|
630
|
|
|
update_row = (" UPDATE tbl_microgrids " |
631
|
|
|
" SET name = %s, address = %s, postal_code = %s, latitude = %s, longitude = %s, " |
632
|
|
|
" rated_capacity = %s, rated_power = %s, " |
633
|
|
|
" contact_id = %s, cost_center_id = %s, " |
634
|
|
|
" serial_number = %s, svg_id = %s, is_cost_data_displayed = %s, phase_of_lifecycle = %s, " |
635
|
|
|
" description = %s " |
636
|
|
|
" WHERE id = %s ") |
637
|
|
|
cursor.execute(update_row, (name, |
638
|
|
|
address, |
639
|
|
|
postal_code, |
640
|
|
|
latitude, |
641
|
|
|
longitude, |
642
|
|
|
rated_capacity, |
643
|
|
|
rated_power, |
644
|
|
|
contact_id, |
645
|
|
|
cost_center_id, |
646
|
|
|
serial_number, |
647
|
|
|
svg_id, |
648
|
|
|
is_cost_data_displayed, |
649
|
|
|
phase_of_lifecycle, |
650
|
|
|
description, |
651
|
|
|
id_)) |
652
|
|
|
cnx.commit() |
653
|
|
|
|
654
|
|
|
cursor.close() |
655
|
|
|
cnx.close() |
656
|
|
|
|
657
|
|
|
resp.status = falcon.HTTP_200 |
658
|
|
|
|
659
|
|
|
|
660
|
|
|
class MicrogridBatteryCollection: |
661
|
|
|
def __init__(self): |
662
|
|
|
"""Initializes MicrogridBatteryCollection""" |
663
|
|
|
pass |
664
|
|
|
|
665
|
|
|
@staticmethod |
666
|
|
|
def on_options(req, resp, id_): |
667
|
|
|
_ = req |
668
|
|
|
resp.status = falcon.HTTP_200 |
669
|
|
|
_ = id_ |
670
|
|
|
|
671
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
672
|
|
|
def on_get(req, resp, id_): |
673
|
|
|
access_control(req) |
674
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
675
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
676
|
|
|
description='API.INVALID_MICROGRID_ID') |
677
|
|
|
|
678
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
679
|
|
|
cursor = cnx.cursor() |
680
|
|
|
|
681
|
|
|
cursor.execute(" SELECT name " |
682
|
|
|
" FROM tbl_microgrids " |
683
|
|
|
" WHERE id = %s ", (id_,)) |
684
|
|
|
if cursor.fetchone() is None: |
685
|
|
|
cursor.close() |
686
|
|
|
cnx.close() |
687
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
688
|
|
|
description='API.MICROGRID_NOT_FOUND') |
689
|
|
|
|
690
|
|
|
# query meter dict |
691
|
|
|
query = (" SELECT id, name, uuid " |
692
|
|
|
" FROM tbl_meters ") |
693
|
|
|
cursor.execute(query) |
694
|
|
|
rows_meters = cursor.fetchall() |
695
|
|
|
|
696
|
|
|
meter_dict = dict() |
697
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
698
|
|
|
for row in rows_meters: |
699
|
|
|
meter_dict[row[0]] = {"id": row[0], |
700
|
|
|
"name": row[1], |
701
|
|
|
"uuid": row[2]} |
702
|
|
|
# query point dict |
703
|
|
|
query = (" SELECT id, name " |
704
|
|
|
" FROM tbl_points ") |
705
|
|
|
cursor.execute(query) |
706
|
|
|
rows_points = cursor.fetchall() |
707
|
|
|
|
708
|
|
|
point_dict = dict() |
709
|
|
|
if rows_points is not None and len(rows_points) > 0: |
710
|
|
|
for row in rows_points: |
711
|
|
|
point_dict[row[0]] = {"id": row[0], |
712
|
|
|
"name": row[1]} |
713
|
|
|
|
714
|
|
|
query = (" SELECT id, name, uuid, " |
715
|
|
|
" battery_state_point_id, soc_point_id, power_point_id, " |
716
|
|
|
" charge_meter_id, discharge_meter_id, rated_capacity, rated_power, nominal_voltage " |
717
|
|
|
" FROM tbl_microgrids_batteries " |
718
|
|
|
" WHERE microgrid_id = %s " |
719
|
|
|
" ORDER BY name ") |
720
|
|
|
cursor.execute(query, (id_,)) |
721
|
|
|
rows = cursor.fetchall() |
722
|
|
|
|
723
|
|
|
result = list() |
724
|
|
|
if rows is not None and len(rows) > 0: |
725
|
|
|
for row in rows: |
726
|
|
|
meta_result = {"id": row[0], |
727
|
|
|
"name": row[1], |
728
|
|
|
"uuid": row[2], |
729
|
|
|
"battery_state_point": point_dict.get(row[3]), |
730
|
|
|
"soc_point": point_dict.get(row[4]), |
731
|
|
|
"power_point": point_dict.get(row[5]), |
732
|
|
|
"charge_meter": meter_dict.get(row[6]), |
733
|
|
|
"discharge_meter": meter_dict.get(row[7]), |
734
|
|
|
"rated_capacity": row[8], |
735
|
|
|
"rated_power": row[9], |
736
|
|
|
"nominal_voltage": row[10]} |
737
|
|
|
result.append(meta_result) |
738
|
|
|
|
739
|
|
|
resp.text = json.dumps(result) |
740
|
|
|
|
741
|
|
|
@staticmethod |
742
|
|
|
@user_logger |
743
|
|
|
def on_post(req, resp, id_): |
744
|
|
|
"""Handles POST requests""" |
745
|
|
|
admin_control(req) |
746
|
|
|
try: |
747
|
|
|
raw_json = req.stream.read().decode('utf-8') |
748
|
|
|
except Exception as ex: |
749
|
|
|
print(str(ex)) |
750
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
751
|
|
|
title='API.BAD_REQUEST', |
752
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
753
|
|
|
|
754
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
755
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
756
|
|
|
description='API.INVALID_MICROGRID_ID') |
757
|
|
|
|
758
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
759
|
|
|
cursor = cnx.cursor() |
760
|
|
|
|
761
|
|
|
cursor.execute(" SELECT name " |
762
|
|
|
" FROM tbl_microgrids " |
763
|
|
|
" WHERE id = %s ", (id_,)) |
764
|
|
|
if cursor.fetchone() is None: |
765
|
|
|
cursor.close() |
766
|
|
|
cnx.close() |
767
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
768
|
|
|
description='API.MICROGRID_NOT_FOUND') |
769
|
|
|
|
770
|
|
|
new_values = json.loads(raw_json) |
771
|
|
|
|
772
|
|
|
if 'name' not in new_values['data'].keys() or \ |
773
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
774
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
775
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
776
|
|
|
description='API.INVALID_MICROGRID_BATTERY_NAME') |
777
|
|
|
name = str.strip(new_values['data']['name']) |
778
|
|
|
|
779
|
|
|
if 'battery_state_point_id' not in new_values['data'].keys() or \ |
780
|
|
|
not isinstance(new_values['data']['battery_state_point_id'], int) or \ |
781
|
|
|
new_values['data']['battery_state_point_id'] <= 0: |
782
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
783
|
|
|
description='API.INVALID_BATTERY_STATE_POINT_ID') |
784
|
|
|
battery_state_point_id = new_values['data']['battery_state_point_id'] |
785
|
|
|
|
786
|
|
|
if 'soc_point_id' not in new_values['data'].keys() or \ |
787
|
|
|
not isinstance(new_values['data']['soc_point_id'], int) or \ |
788
|
|
|
new_values['data']['soc_point_id'] <= 0: |
789
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
790
|
|
|
description='API.INVALID_SOC_POINT_ID') |
791
|
|
|
soc_point_id = new_values['data']['soc_point_id'] |
792
|
|
|
|
793
|
|
|
if 'power_point_id' not in new_values['data'].keys() or \ |
794
|
|
|
not isinstance(new_values['data']['power_point_id'], int) or \ |
795
|
|
|
new_values['data']['power_point_id'] <= 0: |
796
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
797
|
|
|
description='API.INVALID_POWER_POINT_ID') |
798
|
|
|
power_point_id = new_values['data']['power_point_id'] |
799
|
|
|
|
800
|
|
|
if 'charge_meter_id' not in new_values['data'].keys() or \ |
801
|
|
|
not isinstance(new_values['data']['charge_meter_id'], int) or \ |
802
|
|
|
new_values['data']['charge_meter_id'] <= 0: |
803
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
804
|
|
|
description='API.INVALID_CHARGE_METER_ID') |
805
|
|
|
charge_meter_id = new_values['data']['charge_meter_id'] |
806
|
|
|
|
807
|
|
|
if 'discharge_meter_id' not in new_values['data'].keys() or \ |
808
|
|
|
not isinstance(new_values['data']['discharge_meter_id'], int) or \ |
809
|
|
|
new_values['data']['discharge_meter_id'] <= 0: |
810
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
811
|
|
|
description='API.INVALID_DISCHARGE_METER_ID') |
812
|
|
|
discharge_meter_id = new_values['data']['discharge_meter_id'] |
813
|
|
|
|
814
|
|
|
if 'rated_capacity' not in new_values['data'].keys() or \ |
815
|
|
|
not (isinstance(new_values['data']['rated_capacity'], float) or |
816
|
|
|
isinstance(new_values['data']['rated_capacity'], int)): |
817
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
818
|
|
|
description='API.INVALID_RATED_CAPACITY') |
819
|
|
|
rated_capacity = float(new_values['data']['rated_capacity']) |
820
|
|
|
|
821
|
|
|
if 'rated_power' not in new_values['data'].keys() or \ |
822
|
|
|
not (isinstance(new_values['data']['rated_power'], float) or |
823
|
|
|
isinstance(new_values['data']['rated_power'], int)): |
824
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
825
|
|
|
description='API.INVALID_RATED_POWER') |
826
|
|
|
rated_power = float(new_values['data']['rated_power']) |
827
|
|
|
|
828
|
|
|
if 'nominal_voltage' not in new_values['data'].keys() or \ |
829
|
|
|
not (isinstance(new_values['data']['nominal_voltage'], float) or |
830
|
|
|
isinstance(new_values['data']['nominal_voltage'], int)): |
831
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
832
|
|
|
description='API.INVALID_NOMINAL_VOLTAGE') |
833
|
|
|
nominal_voltage = float(new_values['data']['nominal_voltage']) |
834
|
|
|
|
835
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
836
|
|
|
cursor = cnx.cursor() |
837
|
|
|
|
838
|
|
|
cursor.execute(" SELECT name " |
839
|
|
|
" FROM tbl_microgrids_batteries " |
840
|
|
|
" WHERE microgrid_id = %s AND name = %s ", |
841
|
|
|
(id_, name,)) |
842
|
|
|
if cursor.fetchone() is not None: |
843
|
|
|
cursor.close() |
844
|
|
|
cnx.close() |
845
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
846
|
|
|
description='API.MICROGRID_BATTERY_NAME_IS_ALREADY_IN_USE') |
847
|
|
|
|
848
|
|
|
cursor.execute(" SELECT name " |
849
|
|
|
" FROM tbl_points " |
850
|
|
|
" WHERE id = %s ", |
851
|
|
|
(battery_state_point_id,)) |
852
|
|
|
if cursor.fetchone() is None: |
853
|
|
|
cursor.close() |
854
|
|
|
cnx.close() |
855
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
856
|
|
|
description='API.BATTERY_STATE_POINT_NOT_FOUND') |
857
|
|
|
|
858
|
|
|
cursor.execute(" SELECT name " |
859
|
|
|
" FROM tbl_points " |
860
|
|
|
" WHERE id = %s ", |
861
|
|
|
(soc_point_id,)) |
862
|
|
|
if cursor.fetchone() is None: |
863
|
|
|
cursor.close() |
864
|
|
|
cnx.close() |
865
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
866
|
|
|
description='API.SOC_POINT_NOT_FOUND') |
867
|
|
|
|
868
|
|
|
cursor.execute(" SELECT name " |
869
|
|
|
" FROM tbl_points " |
870
|
|
|
" WHERE id = %s ", |
871
|
|
|
(power_point_id,)) |
872
|
|
|
if cursor.fetchone() is None: |
873
|
|
|
cursor.close() |
874
|
|
|
cnx.close() |
875
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
876
|
|
|
description='API.POWER_POINT_NOT_FOUND') |
877
|
|
|
|
878
|
|
|
cursor.execute(" SELECT name " |
879
|
|
|
" FROM tbl_meters " |
880
|
|
|
" WHERE id = %s ", |
881
|
|
|
(charge_meter_id,)) |
882
|
|
|
if cursor.fetchone() is None: |
883
|
|
|
cursor.close() |
884
|
|
|
cnx.close() |
885
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
886
|
|
|
description='API.CHARGE_METER_NOT_FOUND') |
887
|
|
|
|
888
|
|
|
cursor.execute(" SELECT name " |
889
|
|
|
" FROM tbl_meters " |
890
|
|
|
" WHERE id = %s ", |
891
|
|
|
(discharge_meter_id,)) |
892
|
|
|
if cursor.fetchone() is None: |
893
|
|
|
cursor.close() |
894
|
|
|
cnx.close() |
895
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
896
|
|
|
description='API.DISCHARGE_METER_NOT_FOUND') |
897
|
|
|
|
898
|
|
|
add_values = (" INSERT INTO tbl_microgrids_batteries " |
899
|
|
|
" (name, uuid, microgrid_id, " |
900
|
|
|
" battery_state_point_id, soc_point_id, power_point_id, " |
901
|
|
|
" charge_meter_id, discharge_meter_id, rated_capacity, rated_power, nominal_voltage) " |
902
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
903
|
|
|
cursor.execute(add_values, (name, |
904
|
|
|
str(uuid.uuid4()), |
905
|
|
|
id_, |
906
|
|
|
battery_state_point_id, |
907
|
|
|
soc_point_id, |
908
|
|
|
power_point_id, |
909
|
|
|
charge_meter_id, |
910
|
|
|
discharge_meter_id, |
911
|
|
|
rated_capacity, |
912
|
|
|
rated_power, |
913
|
|
|
nominal_voltage)) |
914
|
|
|
new_id = cursor.lastrowid |
915
|
|
|
cnx.commit() |
916
|
|
|
cursor.close() |
917
|
|
|
cnx.close() |
918
|
|
|
|
919
|
|
|
resp.status = falcon.HTTP_201 |
920
|
|
|
resp.location = '/microgrids/' + str(id_) + '/batteries/' + str(new_id) |
921
|
|
|
|
922
|
|
|
|
923
|
|
|
class MicrogridBatteryItem: |
924
|
|
|
def __init__(self): |
925
|
|
|
"""Initializes MicrogridBatteryItem""" |
926
|
|
|
pass |
927
|
|
|
|
928
|
|
|
@staticmethod |
929
|
|
|
def on_options(req, resp, id_, bid): |
930
|
|
|
_ = req |
931
|
|
|
resp.status = falcon.HTTP_200 |
932
|
|
|
_ = id_ |
933
|
|
|
|
934
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
935
|
|
|
def on_get(req, resp, id_, bid): |
936
|
|
|
access_control(req) |
937
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
938
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
939
|
|
|
description='API.INVALID_MICROGRID_ID') |
940
|
|
|
if not bid.isdigit() or int(bid) <= 0: |
941
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
942
|
|
|
description='API.INVALID_MICROGRID_BATTERY_ID') |
943
|
|
|
|
944
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
945
|
|
|
cursor = cnx.cursor() |
946
|
|
|
|
947
|
|
|
cursor.execute(" SELECT name " |
948
|
|
|
" FROM tbl_microgrids " |
949
|
|
|
" WHERE id = %s ", (id_,)) |
950
|
|
|
if cursor.fetchone() is None: |
951
|
|
|
cursor.close() |
952
|
|
|
cnx.close() |
953
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
954
|
|
|
description='API.MICROGRID_NOT_FOUND') |
955
|
|
|
|
956
|
|
|
# query microgrid dict |
957
|
|
|
query = (" SELECT id, name, uuid " |
958
|
|
|
" FROM tbl_microgrids ") |
959
|
|
|
cursor.execute(query) |
960
|
|
|
rows_microgrids = cursor.fetchall() |
961
|
|
|
|
962
|
|
|
microgrid_dict = dict() |
963
|
|
|
if rows_microgrids is not None and len(rows_microgrids) > 0: |
964
|
|
|
for row in rows_microgrids: |
965
|
|
|
microgrid_dict[row[0]] = {"id": row[0], |
966
|
|
|
"name": row[1], |
967
|
|
|
"uuid": row[2]} |
968
|
|
|
# query meter dict |
969
|
|
|
query = (" SELECT id, name, uuid " |
970
|
|
|
" FROM tbl_meters ") |
971
|
|
|
cursor.execute(query) |
972
|
|
|
rows_meters = cursor.fetchall() |
973
|
|
|
|
974
|
|
|
meter_dict = dict() |
975
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
976
|
|
|
for row in rows_meters: |
977
|
|
|
meter_dict[row[0]] = {"id": row[0], |
978
|
|
|
"name": row[1], |
979
|
|
|
"uuid": row[2]} |
980
|
|
|
# query point dict |
981
|
|
|
query = (" SELECT id, name " |
982
|
|
|
" FROM tbl_points ") |
983
|
|
|
cursor.execute(query) |
984
|
|
|
rows_points = cursor.fetchall() |
985
|
|
|
|
986
|
|
|
point_dict = dict() |
987
|
|
|
if rows_points is not None and len(rows_points) > 0: |
988
|
|
|
for row in rows_points: |
989
|
|
|
point_dict[row[0]] = {"id": row[0], |
990
|
|
|
"name": row[1]} |
991
|
|
|
|
992
|
|
|
query = (" SELECT id, name, uuid, microgrid_id, " |
993
|
|
|
" battery_state_point_id, soc_point_id, power_point_id, " |
994
|
|
|
" charge_meter_id, discharge_meter_id, rated_capacity, rated_power, nominal_voltage " |
995
|
|
|
" FROM tbl_microgrids_batteries " |
996
|
|
|
" WHERE id = %s ") |
997
|
|
|
cursor.execute(query, (bid,)) |
998
|
|
|
row = cursor.fetchone() |
999
|
|
|
cursor.close() |
1000
|
|
|
cnx.close() |
1001
|
|
|
|
1002
|
|
|
if row is None: |
1003
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1004
|
|
|
description='API.MICROGRID_BATTERY_NOT_FOUND') |
1005
|
|
|
else: |
1006
|
|
|
meta_result = {"id": row[0], |
1007
|
|
|
"name": row[1], |
1008
|
|
|
"uuid": row[2], |
1009
|
|
|
"microgrid": microgrid_dict.get(row[3]), |
1010
|
|
|
"battery_state_point": point_dict.get(row[4]), |
1011
|
|
|
"soc_point": point_dict.get(row[5]), |
1012
|
|
|
"power_point": point_dict.get(row[6]), |
1013
|
|
|
"charge_meter": meter_dict.get(row[7]), |
1014
|
|
|
"discharge_meter": meter_dict.get(row[8]), |
1015
|
|
|
"rated_capacity": row[9], |
1016
|
|
|
"rated_power": row[10], |
1017
|
|
|
"nominal_voltage": row[11]} |
1018
|
|
|
|
1019
|
|
|
resp.text = json.dumps(meta_result) |
1020
|
|
|
|
1021
|
|
|
@staticmethod |
1022
|
|
|
@user_logger |
1023
|
|
|
def on_delete(req, resp, id_, bid): |
1024
|
|
|
admin_control(req) |
1025
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1026
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1027
|
|
|
description='API.INVALID_MICROGRID_ID') |
1028
|
|
|
if not bid.isdigit() or int(bid) <= 0: |
1029
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1030
|
|
|
description='API.INVALID_MICROGRID_BATTERY_ID') |
1031
|
|
|
|
1032
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1033
|
|
|
cursor = cnx.cursor() |
1034
|
|
|
|
1035
|
|
|
cursor.execute(" SELECT name " |
1036
|
|
|
" FROM tbl_microgrids " |
1037
|
|
|
" WHERE id = %s ", (id_,)) |
1038
|
|
|
if cursor.fetchone() is None: |
1039
|
|
|
cursor.close() |
1040
|
|
|
cnx.close() |
1041
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1042
|
|
|
description='API.MICROGRID_NOT_FOUND') |
1043
|
|
|
|
1044
|
|
|
cursor.execute(" SELECT name " |
1045
|
|
|
" FROM tbl_microgrids_batteries " |
1046
|
|
|
" WHERE id = %s ", (bid,)) |
1047
|
|
|
if cursor.fetchone() is None: |
1048
|
|
|
cursor.close() |
1049
|
|
|
cnx.close() |
1050
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1051
|
|
|
description='API.MICROGRID_BATTERY_NOT_FOUND') |
1052
|
|
|
|
1053
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_batteries " |
1054
|
|
|
" WHERE id = %s ", (bid,)) |
1055
|
|
|
cnx.commit() |
1056
|
|
|
|
1057
|
|
|
cursor.close() |
1058
|
|
|
cnx.close() |
1059
|
|
|
|
1060
|
|
|
resp.status = falcon.HTTP_204 |
1061
|
|
|
|
1062
|
|
|
@staticmethod |
1063
|
|
|
@user_logger |
1064
|
|
|
def on_put(req, resp, id_, bid): |
1065
|
|
|
"""Handles PUT requests""" |
1066
|
|
|
admin_control(req) |
1067
|
|
|
try: |
1068
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1069
|
|
|
except Exception as ex: |
1070
|
|
|
print(str(ex)) |
1071
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
1072
|
|
|
title='API.BAD_REQUEST', |
1073
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
1074
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1075
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1076
|
|
|
description='API.INVALID_MICROGRID_ID') |
1077
|
|
|
if not bid.isdigit() or int(bid) <= 0: |
1078
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1079
|
|
|
description='API.INVALID_MICROGRID_BATTERY_ID') |
1080
|
|
|
|
1081
|
|
|
new_values = json.loads(raw_json) |
1082
|
|
|
|
1083
|
|
|
if 'name' not in new_values['data'].keys() or \ |
1084
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
1085
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
1086
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1087
|
|
|
description='API.INVALID_MICROGRID_BATTERY_NAME') |
1088
|
|
|
name = str.strip(new_values['data']['name']) |
1089
|
|
|
|
1090
|
|
|
if 'battery_state_point_id' not in new_values['data'].keys() or \ |
1091
|
|
|
not isinstance(new_values['data']['battery_state_point_id'], int) or \ |
1092
|
|
|
new_values['data']['battery_state_point_id'] <= 0: |
1093
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1094
|
|
|
description='API.INVALID_BATTERY_STATE_POINT_ID') |
1095
|
|
|
battery_state_point_id = new_values['data']['battery_state_point_id'] |
1096
|
|
|
|
1097
|
|
|
if 'soc_point_id' not in new_values['data'].keys() or \ |
1098
|
|
|
not isinstance(new_values['data']['soc_point_id'], int) or \ |
1099
|
|
|
new_values['data']['soc_point_id'] <= 0: |
1100
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1101
|
|
|
description='API.INVALID_SOC_POINT_ID') |
1102
|
|
|
soc_point_id = new_values['data']['soc_point_id'] |
1103
|
|
|
|
1104
|
|
|
if 'power_point_id' not in new_values['data'].keys() or \ |
1105
|
|
|
not isinstance(new_values['data']['power_point_id'], int) or \ |
1106
|
|
|
new_values['data']['power_point_id'] <= 0: |
1107
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1108
|
|
|
description='API.INVALID_POWER_POINT_ID') |
1109
|
|
|
power_point_id = new_values['data']['power_point_id'] |
1110
|
|
|
|
1111
|
|
|
if 'charge_meter_id' not in new_values['data'].keys() or \ |
1112
|
|
|
not isinstance(new_values['data']['charge_meter_id'], int) or \ |
1113
|
|
|
new_values['data']['charge_meter_id'] <= 0: |
1114
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1115
|
|
|
description='API.INVALID_CHARGE_METER_ID') |
1116
|
|
|
charge_meter_id = new_values['data']['charge_meter_id'] |
1117
|
|
|
|
1118
|
|
|
if 'discharge_meter_id' not in new_values['data'].keys() or \ |
1119
|
|
|
not isinstance(new_values['data']['discharge_meter_id'], int) or \ |
1120
|
|
|
new_values['data']['discharge_meter_id'] <= 0: |
1121
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1122
|
|
|
description='API.INVALID_DISCHARGE_METER_ID') |
1123
|
|
|
discharge_meter_id = new_values['data']['discharge_meter_id'] |
1124
|
|
|
|
1125
|
|
|
if 'rated_capacity' not in new_values['data'].keys() or \ |
1126
|
|
|
not (isinstance(new_values['data']['rated_capacity'], float) or |
1127
|
|
|
isinstance(new_values['data']['rated_capacity'], int)): |
1128
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1129
|
|
|
description='API.INVALID_RATED_CAPACITY') |
1130
|
|
|
rated_capacity = float(new_values['data']['rated_capacity']) |
1131
|
|
|
|
1132
|
|
|
if 'rated_power' not in new_values['data'].keys() or \ |
1133
|
|
|
not (isinstance(new_values['data']['rated_power'], float) or |
1134
|
|
|
isinstance(new_values['data']['rated_power'], int)): |
1135
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1136
|
|
|
description='API.INVALID_RATED_POWER') |
1137
|
|
|
rated_power = float(new_values['data']['rated_power']) |
1138
|
|
|
|
1139
|
|
|
if 'nominal_voltage' not in new_values['data'].keys() or \ |
1140
|
|
|
not (isinstance(new_values['data']['nominal_voltage'], float) or |
1141
|
|
|
isinstance(new_values['data']['nominal_voltage'], int)): |
1142
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1143
|
|
|
description='API.INVALID_NOMINAL_VOLTAGE') |
1144
|
|
|
nominal_voltage = float(new_values['data']['nominal_voltage']) |
1145
|
|
|
|
1146
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1147
|
|
|
cursor = cnx.cursor() |
1148
|
|
|
|
1149
|
|
|
cursor.execute(" SELECT name " |
1150
|
|
|
" FROM tbl_microgrids " |
1151
|
|
|
" WHERE id = %s ", |
1152
|
|
|
(id_,)) |
1153
|
|
|
if cursor.fetchone() is None: |
1154
|
|
|
cursor.close() |
1155
|
|
|
cnx.close() |
1156
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1157
|
|
|
description='API.MICROGRID_NOT_FOUND') |
1158
|
|
|
|
1159
|
|
|
cursor.execute(" SELECT name " |
1160
|
|
|
" FROM tbl_microgrids_batteries " |
1161
|
|
|
" WHERE id = %s ", (bid,)) |
1162
|
|
|
if cursor.fetchone() is None: |
1163
|
|
|
cursor.close() |
1164
|
|
|
cnx.close() |
1165
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1166
|
|
|
description='API.MICROGRID_BATTERY_NOT_FOUND') |
1167
|
|
|
|
1168
|
|
|
cursor.execute(" SELECT name " |
1169
|
|
|
" FROM tbl_microgrids_batteries " |
1170
|
|
|
" WHERE microgrid_id = %s AND name = %s AND id != %s ", |
1171
|
|
|
(id_, name, bid)) |
1172
|
|
|
if cursor.fetchone() is not None: |
1173
|
|
|
cursor.close() |
1174
|
|
|
cnx.close() |
1175
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1176
|
|
|
description='API.MICROGRID_BATTERY_NAME_IS_ALREADY_IN_USE') |
1177
|
|
|
|
1178
|
|
|
cursor.execute(" SELECT name " |
1179
|
|
|
" FROM tbl_points " |
1180
|
|
|
" WHERE id = %s ", |
1181
|
|
|
(battery_state_point_id,)) |
1182
|
|
|
if cursor.fetchone() is None: |
1183
|
|
|
cursor.close() |
1184
|
|
|
cnx.close() |
1185
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1186
|
|
|
description='API.BATTERY_STATE_POINT_NOT_FOUND') |
1187
|
|
|
|
1188
|
|
|
cursor.execute(" SELECT name " |
1189
|
|
|
" FROM tbl_points " |
1190
|
|
|
" WHERE id = %s ", |
1191
|
|
|
(soc_point_id,)) |
1192
|
|
|
if cursor.fetchone() is None: |
1193
|
|
|
cursor.close() |
1194
|
|
|
cnx.close() |
1195
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1196
|
|
|
description='API.SOC_POINT_NOT_FOUND') |
1197
|
|
|
|
1198
|
|
|
cursor.execute(" SELECT name " |
1199
|
|
|
" FROM tbl_points " |
1200
|
|
|
" WHERE id = %s ", |
1201
|
|
|
(power_point_id,)) |
1202
|
|
|
if cursor.fetchone() is None: |
1203
|
|
|
cursor.close() |
1204
|
|
|
cnx.close() |
1205
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1206
|
|
|
description='API.POWER_POINT_NOT_FOUND') |
1207
|
|
|
|
1208
|
|
|
cursor.execute(" SELECT name " |
1209
|
|
|
" FROM tbl_meters " |
1210
|
|
|
" WHERE id = %s ", |
1211
|
|
|
(charge_meter_id,)) |
1212
|
|
|
if cursor.fetchone() is None: |
1213
|
|
|
cursor.close() |
1214
|
|
|
cnx.close() |
1215
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1216
|
|
|
description='API.CHARGE_METER_NOT_FOUND') |
1217
|
|
|
|
1218
|
|
|
cursor.execute(" SELECT name " |
1219
|
|
|
" FROM tbl_meters " |
1220
|
|
|
" WHERE id = %s ", |
1221
|
|
|
(discharge_meter_id,)) |
1222
|
|
|
if cursor.fetchone() is None: |
1223
|
|
|
cursor.close() |
1224
|
|
|
cnx.close() |
1225
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1226
|
|
|
description='API.DISCHARGE_METER_NOT_FOUND') |
1227
|
|
|
|
1228
|
|
|
update_row = (" UPDATE tbl_microgrids_batteries " |
1229
|
|
|
" SET name = %s, microgrid_id = %s, " |
1230
|
|
|
" battery_state_point_id = %s, soc_point_id = %s, power_point_id = %s, " |
1231
|
|
|
" charge_meter_id = %s, discharge_meter_id = %s, rated_capacity = %s, rated_power = %s," |
1232
|
|
|
" nominal_voltage = %s " |
1233
|
|
|
" WHERE id = %s ") |
1234
|
|
|
cursor.execute(update_row, (name, |
1235
|
|
|
id_, |
1236
|
|
|
battery_state_point_id, |
1237
|
|
|
soc_point_id, |
1238
|
|
|
power_point_id, |
1239
|
|
|
charge_meter_id, |
1240
|
|
|
discharge_meter_id, |
1241
|
|
|
rated_capacity, |
1242
|
|
|
rated_power, |
1243
|
|
|
nominal_voltage, |
1244
|
|
|
bid)) |
1245
|
|
|
cnx.commit() |
1246
|
|
|
|
1247
|
|
|
cursor.close() |
1248
|
|
|
cnx.close() |
1249
|
|
|
|
1250
|
|
|
resp.status = falcon.HTTP_200 |
1251
|
|
|
|
1252
|
|
|
|
1253
|
|
|
class MicrogridCommandCollection: |
1254
|
|
|
def __init__(self): |
1255
|
|
|
"""Initializes Class""" |
1256
|
|
|
pass |
1257
|
|
|
|
1258
|
|
|
@staticmethod |
1259
|
|
|
def on_options(req, resp, id_): |
1260
|
|
|
_ = req |
1261
|
|
|
resp.status = falcon.HTTP_200 |
1262
|
|
|
_ = id_ |
1263
|
|
|
|
1264
|
|
|
@staticmethod |
1265
|
|
|
def on_get(req, resp, id_): |
1266
|
|
|
access_control(req) |
1267
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1268
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1269
|
|
|
description='API.INVALID_MICROGRID_ID') |
1270
|
|
|
|
1271
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1272
|
|
|
cursor = cnx.cursor() |
1273
|
|
|
|
1274
|
|
|
cursor.execute(" SELECT name " |
1275
|
|
|
" FROM tbl_microgrids " |
1276
|
|
|
" WHERE id = %s ", (id_,)) |
1277
|
|
|
if cursor.fetchone() is None: |
1278
|
|
|
cursor.close() |
1279
|
|
|
cnx.close() |
1280
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1281
|
|
|
description='API.MICROGRID_NOT_FOUND') |
1282
|
|
|
|
1283
|
|
|
query = (" SELECT c.id, c.name, c.uuid " |
1284
|
|
|
" FROM tbl_microgrids m, tbl_microgrids_commands mc, tbl_commands c " |
1285
|
|
|
" WHERE mc.microgrid_id = m.id AND c.id = mc.command_id AND m.id = %s " |
1286
|
|
|
" ORDER BY c.id ") |
1287
|
|
|
cursor.execute(query, (id_,)) |
1288
|
|
|
rows = cursor.fetchall() |
1289
|
|
|
|
1290
|
|
|
result = list() |
1291
|
|
|
if rows is not None and len(rows) > 0: |
1292
|
|
|
for row in rows: |
1293
|
|
|
meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
1294
|
|
|
result.append(meta_result) |
1295
|
|
|
|
1296
|
|
|
resp.text = json.dumps(result) |
1297
|
|
|
|
1298
|
|
|
|
1299
|
|
View Code Duplication |
class MicrogridEVChargerCollection: |
|
|
|
|
1300
|
|
|
def __init__(self): |
1301
|
|
|
"""Initializes MicrogridEVChargerCollection""" |
1302
|
|
|
pass |
1303
|
|
|
|
1304
|
|
|
@staticmethod |
1305
|
|
|
def on_options(req, resp, id_): |
1306
|
|
|
_ = req |
1307
|
|
|
resp.status = falcon.HTTP_200 |
1308
|
|
|
_ = id_ |
1309
|
|
|
|
1310
|
|
|
@staticmethod |
1311
|
|
|
def on_get(req, resp, id_): |
1312
|
|
|
access_control(req) |
1313
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1314
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1315
|
|
|
description='API.INVALID_MICROGRID_ID') |
1316
|
|
|
|
1317
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1318
|
|
|
cursor = cnx.cursor() |
1319
|
|
|
|
1320
|
|
|
cursor.execute(" SELECT name " |
1321
|
|
|
" FROM tbl_microgrids " |
1322
|
|
|
" WHERE id = %s ", (id_,)) |
1323
|
|
|
if cursor.fetchone() is None: |
1324
|
|
|
cursor.close() |
1325
|
|
|
cnx.close() |
1326
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1327
|
|
|
description='API.MICROGRID_NOT_FOUND') |
1328
|
|
|
|
1329
|
|
|
# query meter dict |
1330
|
|
|
query = (" SELECT id, name, uuid " |
1331
|
|
|
" FROM tbl_meters ") |
1332
|
|
|
cursor.execute(query) |
1333
|
|
|
rows_meters = cursor.fetchall() |
1334
|
|
|
|
1335
|
|
|
meter_dict = dict() |
1336
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
1337
|
|
|
for row in rows_meters: |
1338
|
|
|
meter_dict[row[0]] = {"id": row[0], |
1339
|
|
|
"name": row[1], |
1340
|
|
|
"uuid": row[2]} |
1341
|
|
|
# query point dict |
1342
|
|
|
query = (" SELECT id, name " |
1343
|
|
|
" FROM tbl_points ") |
1344
|
|
|
cursor.execute(query) |
1345
|
|
|
rows_points = cursor.fetchall() |
1346
|
|
|
|
1347
|
|
|
point_dict = dict() |
1348
|
|
|
if rows_points is not None and len(rows_points) > 0: |
1349
|
|
|
for row in rows_points: |
1350
|
|
|
point_dict[row[0]] = {"id": row[0], |
1351
|
|
|
"name": row[1]} |
1352
|
|
|
|
1353
|
|
|
query = (" SELECT id, name, uuid, " |
1354
|
|
|
" power_point_id, meter_id, rated_output_power " |
1355
|
|
|
" FROM tbl_microgrids_evchargers " |
1356
|
|
|
" WHERE microgrid_id = %s " |
1357
|
|
|
" ORDER BY name ") |
1358
|
|
|
cursor.execute(query, (id_,)) |
1359
|
|
|
rows = cursor.fetchall() |
1360
|
|
|
|
1361
|
|
|
result = list() |
1362
|
|
|
if rows is not None and len(rows) > 0: |
1363
|
|
|
for row in rows: |
1364
|
|
|
meta_result = {"id": row[0], |
1365
|
|
|
"name": row[1], |
1366
|
|
|
"uuid": row[2], |
1367
|
|
|
"power_point": point_dict.get(row[3]), |
1368
|
|
|
"meter": meter_dict.get(row[4]), |
1369
|
|
|
"rated_output_power": row[5]} |
1370
|
|
|
result.append(meta_result) |
1371
|
|
|
|
1372
|
|
|
resp.text = json.dumps(result) |
1373
|
|
|
|
1374
|
|
|
@staticmethod |
1375
|
|
|
@user_logger |
1376
|
|
|
def on_post(req, resp, id_): |
1377
|
|
|
"""Handles POST requests""" |
1378
|
|
|
admin_control(req) |
1379
|
|
|
try: |
1380
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1381
|
|
|
except Exception as ex: |
1382
|
|
|
print(str(ex)) |
1383
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
1384
|
|
|
title='API.BAD_REQUEST', |
1385
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
1386
|
|
|
|
1387
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1388
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1389
|
|
|
description='API.INVALID_MICROGRID_ID') |
1390
|
|
|
|
1391
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1392
|
|
|
cursor = cnx.cursor() |
1393
|
|
|
|
1394
|
|
|
cursor.execute(" SELECT name " |
1395
|
|
|
" FROM tbl_microgrids " |
1396
|
|
|
" WHERE id = %s ", (id_,)) |
1397
|
|
|
if cursor.fetchone() is None: |
1398
|
|
|
cursor.close() |
1399
|
|
|
cnx.close() |
1400
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1401
|
|
|
description='API.MICROGRID_NOT_FOUND') |
1402
|
|
|
|
1403
|
|
|
new_values = json.loads(raw_json) |
1404
|
|
|
|
1405
|
|
|
if 'name' not in new_values['data'].keys() or \ |
1406
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
1407
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
1408
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1409
|
|
|
description='API.INVALID_MICROGRID_EVCHARGER_NAME') |
1410
|
|
|
name = str.strip(new_values['data']['name']) |
1411
|
|
|
|
1412
|
|
|
if 'power_point_id' not in new_values['data'].keys() or \ |
1413
|
|
|
not isinstance(new_values['data']['power_point_id'], int) or \ |
1414
|
|
|
new_values['data']['power_point_id'] <= 0: |
1415
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1416
|
|
|
description='API.INVALID_POWER_POINT_ID') |
1417
|
|
|
power_point_id = new_values['data']['power_point_id'] |
1418
|
|
|
|
1419
|
|
|
if 'meter_id' not in new_values['data'].keys() or \ |
1420
|
|
|
not isinstance(new_values['data']['meter_id'], int) or \ |
1421
|
|
|
new_values['data']['meter_id'] <= 0: |
1422
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1423
|
|
|
description='API.INVALID_METER_ID') |
1424
|
|
|
meter_id = new_values['data']['meter_id'] |
1425
|
|
|
|
1426
|
|
|
if 'rated_output_power' not in new_values['data'].keys() or \ |
1427
|
|
|
not (isinstance(new_values['data']['rated_output_power'], float) or |
1428
|
|
|
isinstance(new_values['data']['rated_output_power'], int)): |
1429
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1430
|
|
|
description='API.INVALID_RATED_OUTPUT_POWER') |
1431
|
|
|
rated_output_power = float(new_values['data']['rated_output_power']) |
1432
|
|
|
|
1433
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1434
|
|
|
cursor = cnx.cursor() |
1435
|
|
|
|
1436
|
|
|
cursor.execute(" SELECT name " |
1437
|
|
|
" FROM tbl_microgrids " |
1438
|
|
|
" WHERE id = %s ", |
1439
|
|
|
(id_,)) |
1440
|
|
|
if cursor.fetchone() is None: |
1441
|
|
|
cursor.close() |
1442
|
|
|
cnx.close() |
1443
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1444
|
|
|
description='API.MICROGRID_NOT_FOUND') |
1445
|
|
|
|
1446
|
|
|
cursor.execute(" SELECT name " |
1447
|
|
|
" FROM tbl_microgrids_evchargers " |
1448
|
|
|
" WHERE microgrid_id = %s AND name = %s ", |
1449
|
|
|
(id_, name,)) |
1450
|
|
|
if cursor.fetchone() is not None: |
1451
|
|
|
cursor.close() |
1452
|
|
|
cnx.close() |
1453
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1454
|
|
|
description='API.MICROGRID_EVCHARGER_NAME_IS_ALREADY_IN_USE') |
1455
|
|
|
|
1456
|
|
|
cursor.execute(" SELECT name " |
1457
|
|
|
" FROM tbl_points " |
1458
|
|
|
" WHERE id = %s ", |
1459
|
|
|
(power_point_id,)) |
1460
|
|
|
if cursor.fetchone() is None: |
1461
|
|
|
cursor.close() |
1462
|
|
|
cnx.close() |
1463
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1464
|
|
|
description='API.POWER_POINT_NOT_FOUND') |
1465
|
|
|
|
1466
|
|
|
cursor.execute(" SELECT name " |
1467
|
|
|
" FROM tbl_meters " |
1468
|
|
|
" WHERE id = %s ", |
1469
|
|
|
(meter_id,)) |
1470
|
|
|
if cursor.fetchone() is None: |
1471
|
|
|
cursor.close() |
1472
|
|
|
cnx.close() |
1473
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1474
|
|
|
description='API.METER_NOT_FOUND') |
1475
|
|
|
|
1476
|
|
|
add_values = (" INSERT INTO tbl_microgrids_evchargers " |
1477
|
|
|
" (name, uuid, microgrid_id, power_point_id, meter_id, rated_output_power) " |
1478
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s) ") |
1479
|
|
|
cursor.execute(add_values, (name, |
1480
|
|
|
str(uuid.uuid4()), |
1481
|
|
|
id_, |
1482
|
|
|
power_point_id, |
1483
|
|
|
meter_id, |
1484
|
|
|
rated_output_power)) |
1485
|
|
|
new_id = cursor.lastrowid |
1486
|
|
|
cnx.commit() |
1487
|
|
|
cursor.close() |
1488
|
|
|
cnx.close() |
1489
|
|
|
|
1490
|
|
|
resp.status = falcon.HTTP_201 |
1491
|
|
|
resp.location = '/microgrids/' + id_ + '/evchargers/' + str(new_id) |
1492
|
|
|
|
1493
|
|
|
|
1494
|
|
View Code Duplication |
class MicrogridEVChargerItem: |
|
|
|
|
1495
|
|
|
def __init__(self): |
1496
|
|
|
"""Initializes MicrogridEVChargerItem""" |
1497
|
|
|
pass |
1498
|
|
|
|
1499
|
|
|
@staticmethod |
1500
|
|
|
def on_options(req, resp, id_, eid): |
1501
|
|
|
_ = req |
1502
|
|
|
resp.status = falcon.HTTP_200 |
1503
|
|
|
_ = id_ |
1504
|
|
|
|
1505
|
|
|
@staticmethod |
1506
|
|
|
def on_get(req, resp, id_, eid): |
1507
|
|
|
access_control(req) |
1508
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1509
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1510
|
|
|
description='API.INVALID_MICROGRID_ID') |
1511
|
|
|
if not eid.isdigit() or int(eid) <= 0: |
1512
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1513
|
|
|
description='API.INVALID_MICROGRID_EVCHARGER_ID') |
1514
|
|
|
|
1515
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1516
|
|
|
cursor = cnx.cursor() |
1517
|
|
|
|
1518
|
|
|
cursor.execute(" SELECT name " |
1519
|
|
|
" FROM tbl_microgrids " |
1520
|
|
|
" WHERE id = %s ", (id_,)) |
1521
|
|
|
if cursor.fetchone() is None: |
1522
|
|
|
cursor.close() |
1523
|
|
|
cnx.close() |
1524
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1525
|
|
|
description='API.MICROGRID_NOT_FOUND') |
1526
|
|
|
|
1527
|
|
|
# query microgrid dict |
1528
|
|
|
query = (" SELECT id, name, uuid " |
1529
|
|
|
" FROM tbl_microgrids ") |
1530
|
|
|
cursor.execute(query) |
1531
|
|
|
rows_microgrids = cursor.fetchall() |
1532
|
|
|
|
1533
|
|
|
microgrid_dict = dict() |
1534
|
|
|
if rows_microgrids is not None and len(rows_microgrids) > 0: |
1535
|
|
|
for row in rows_microgrids: |
1536
|
|
|
microgrid_dict[row[0]] = {"id": row[0], |
1537
|
|
|
"name": row[1], |
1538
|
|
|
"uuid": row[2]} |
1539
|
|
|
# query meter dict |
1540
|
|
|
query = (" SELECT id, name, uuid " |
1541
|
|
|
" FROM tbl_meters ") |
1542
|
|
|
cursor.execute(query) |
1543
|
|
|
rows_meters = cursor.fetchall() |
1544
|
|
|
|
1545
|
|
|
meter_dict = dict() |
1546
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
1547
|
|
|
for row in rows_meters: |
1548
|
|
|
meter_dict[row[0]] = {"id": row[0], |
1549
|
|
|
"name": row[1], |
1550
|
|
|
"uuid": row[2]} |
1551
|
|
|
# query point dict |
1552
|
|
|
query = (" SELECT id, name " |
1553
|
|
|
" FROM tbl_points ") |
1554
|
|
|
cursor.execute(query) |
1555
|
|
|
rows_points = cursor.fetchall() |
1556
|
|
|
|
1557
|
|
|
point_dict = dict() |
1558
|
|
|
if rows_points is not None and len(rows_points) > 0: |
1559
|
|
|
for row in rows_points: |
1560
|
|
|
point_dict[row[0]] = {"id": row[0], |
1561
|
|
|
"name": row[1]} |
1562
|
|
|
|
1563
|
|
|
query = (" SELECT id, name, uuid, microgrid_id, power_point_id, meter_id, rated_output_power " |
1564
|
|
|
" FROM tbl_microgrids_evchargers " |
1565
|
|
|
" WHERE id = %s ") |
1566
|
|
|
cursor.execute(query, (eid,)) |
1567
|
|
|
row = cursor.fetchone() |
1568
|
|
|
cursor.close() |
1569
|
|
|
cnx.close() |
1570
|
|
|
|
1571
|
|
|
if row is None: |
1572
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1573
|
|
|
description='API.MICROGRID_EVCHARGER_NOT_FOUND') |
1574
|
|
|
else: |
1575
|
|
|
meta_result = {"id": row[0], |
1576
|
|
|
"name": row[1], |
1577
|
|
|
"uuid": row[2], |
1578
|
|
|
"microgrid": microgrid_dict.get(row[3]), |
1579
|
|
|
"power_point": point_dict.get(row[4]), |
1580
|
|
|
"meter": meter_dict.get(row[5]), |
1581
|
|
|
"rated_output_power": row[6]} |
1582
|
|
|
|
1583
|
|
|
resp.text = json.dumps(meta_result) |
1584
|
|
|
|
1585
|
|
|
@staticmethod |
1586
|
|
|
@user_logger |
1587
|
|
|
def on_delete(req, resp, id_, eid): |
1588
|
|
|
admin_control(req) |
1589
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1590
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1591
|
|
|
description='API.INVALID_MICROGRID_ID') |
1592
|
|
|
if not eid.isdigit() or int(eid) <= 0: |
1593
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1594
|
|
|
description='API.INVALID_MICROGRID_EVCHARGER_ID') |
1595
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1596
|
|
|
cursor = cnx.cursor() |
1597
|
|
|
|
1598
|
|
|
cursor.execute(" SELECT name " |
1599
|
|
|
" FROM tbl_microgrids " |
1600
|
|
|
" WHERE id = %s ", (id_,)) |
1601
|
|
|
if cursor.fetchone() is None: |
1602
|
|
|
cursor.close() |
1603
|
|
|
cnx.close() |
1604
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1605
|
|
|
description='API.MICROGRID_NOT_FOUND') |
1606
|
|
|
|
1607
|
|
|
cursor.execute(" SELECT name " |
1608
|
|
|
" FROM tbl_microgrids_evchargers " |
1609
|
|
|
" WHERE id = %s ", (eid,)) |
1610
|
|
|
if cursor.fetchone() is None: |
1611
|
|
|
cursor.close() |
1612
|
|
|
cnx.close() |
1613
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1614
|
|
|
description='API.MICROGRID_EVCHARGER_NOT_FOUND') |
1615
|
|
|
|
1616
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_evchargers " |
1617
|
|
|
" WHERE id = %s ", (eid,)) |
1618
|
|
|
cnx.commit() |
1619
|
|
|
|
1620
|
|
|
cursor.close() |
1621
|
|
|
cnx.close() |
1622
|
|
|
|
1623
|
|
|
resp.status = falcon.HTTP_204 |
1624
|
|
|
|
1625
|
|
|
@staticmethod |
1626
|
|
|
@user_logger |
1627
|
|
|
def on_put(req, resp, id_, eid): |
1628
|
|
|
"""Handles PUT requests""" |
1629
|
|
|
admin_control(req) |
1630
|
|
|
try: |
1631
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1632
|
|
|
except Exception as ex: |
1633
|
|
|
print(str(ex)) |
1634
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
1635
|
|
|
title='API.BAD_REQUEST', |
1636
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
1637
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1638
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1639
|
|
|
description='API.INVALID_MICROGRID_ID') |
1640
|
|
|
if not eid.isdigit() or int(eid) <= 0: |
1641
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1642
|
|
|
description='API.INVALID_MICROGRID_EVCHARGER_ID') |
1643
|
|
|
|
1644
|
|
|
new_values = json.loads(raw_json) |
1645
|
|
|
|
1646
|
|
|
if 'name' not in new_values['data'].keys() or \ |
1647
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
1648
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
1649
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1650
|
|
|
description='API.INVALID_MICROGRID_EVCHARGER_NAME') |
1651
|
|
|
name = str.strip(new_values['data']['name']) |
1652
|
|
|
|
1653
|
|
|
if 'power_point_id' not in new_values['data'].keys() or \ |
1654
|
|
|
not isinstance(new_values['data']['power_point_id'], int) or \ |
1655
|
|
|
new_values['data']['power_point_id'] <= 0: |
1656
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1657
|
|
|
description='API.INVALID_POWER_POINT_ID') |
1658
|
|
|
power_point_id = new_values['data']['power_point_id'] |
1659
|
|
|
|
1660
|
|
|
if 'meter_id' not in new_values['data'].keys() or \ |
1661
|
|
|
not isinstance(new_values['data']['meter_id'], int) or \ |
1662
|
|
|
new_values['data']['meter_id'] <= 0: |
1663
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1664
|
|
|
description='API.INVALID_METER_ID') |
1665
|
|
|
meter_id = new_values['data']['meter_id'] |
1666
|
|
|
|
1667
|
|
|
if 'rated_output_power' not in new_values['data'].keys() or \ |
1668
|
|
|
not (isinstance(new_values['data']['rated_output_power'], float) or |
1669
|
|
|
isinstance(new_values['data']['rated_output_power'], int)): |
1670
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1671
|
|
|
description='API.INVALID_RATED_OUTPUT_POWER') |
1672
|
|
|
rated_output_power = float(new_values['data']['rated_output_power']) |
1673
|
|
|
|
1674
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1675
|
|
|
cursor = cnx.cursor() |
1676
|
|
|
|
1677
|
|
|
cursor.execute(" SELECT name " |
1678
|
|
|
" FROM tbl_microgrids " |
1679
|
|
|
" WHERE id = %s ", (id_,)) |
1680
|
|
|
if cursor.fetchone() is None: |
1681
|
|
|
cursor.close() |
1682
|
|
|
cnx.close() |
1683
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1684
|
|
|
description='API.MICROGRID_NOT_FOUND') |
1685
|
|
|
|
1686
|
|
|
cursor.execute(" SELECT name " |
1687
|
|
|
" FROM tbl_microgrids_evchargers " |
1688
|
|
|
" WHERE id = %s ", (eid,)) |
1689
|
|
|
if cursor.fetchone() is None: |
1690
|
|
|
cursor.close() |
1691
|
|
|
cnx.close() |
1692
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1693
|
|
|
description='API.MICROGRID_EVCHARGER_NOT_FOUND') |
1694
|
|
|
|
1695
|
|
|
cursor.execute(" SELECT name " |
1696
|
|
|
" FROM tbl_microgrids_evchargers " |
1697
|
|
|
" WHERE microgrid_id = %s AND name = %s AND id != %s ", |
1698
|
|
|
(id_, name, eid)) |
1699
|
|
|
if cursor.fetchone() is not None: |
1700
|
|
|
cursor.close() |
1701
|
|
|
cnx.close() |
1702
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1703
|
|
|
description='API.MICROGRID_EVCHARGER_NAME_IS_ALREADY_IN_USE') |
1704
|
|
|
|
1705
|
|
|
cursor.execute(" SELECT name " |
1706
|
|
|
" FROM tbl_points " |
1707
|
|
|
" WHERE id = %s ", |
1708
|
|
|
(power_point_id,)) |
1709
|
|
|
if cursor.fetchone() is None: |
1710
|
|
|
cursor.close() |
1711
|
|
|
cnx.close() |
1712
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1713
|
|
|
description='API.POWER_POINT_NOT_FOUND') |
1714
|
|
|
|
1715
|
|
|
cursor.execute(" SELECT name " |
1716
|
|
|
" FROM tbl_meters " |
1717
|
|
|
" WHERE id = %s ", |
1718
|
|
|
(meter_id,)) |
1719
|
|
|
if cursor.fetchone() is None: |
1720
|
|
|
cursor.close() |
1721
|
|
|
cnx.close() |
1722
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1723
|
|
|
description='API.METER_NOT_FOUND') |
1724
|
|
|
|
1725
|
|
|
update_row = (" UPDATE tbl_microgrids_evchargers " |
1726
|
|
|
" SET name = %s, microgrid_id = %s, power_point_id = %s, meter_id = %s, rated_output_power = %s " |
1727
|
|
|
" WHERE id = %s ") |
1728
|
|
|
cursor.execute(update_row, (name, |
1729
|
|
|
id_, |
1730
|
|
|
power_point_id, |
1731
|
|
|
meter_id, |
1732
|
|
|
rated_output_power, |
1733
|
|
|
eid)) |
1734
|
|
|
cnx.commit() |
1735
|
|
|
|
1736
|
|
|
cursor.close() |
1737
|
|
|
cnx.close() |
1738
|
|
|
|
1739
|
|
|
resp.status = falcon.HTTP_200 |
1740
|
|
|
|
1741
|
|
|
|
1742
|
|
View Code Duplication |
class MicrogridGeneratorCollection: |
|
|
|
|
1743
|
|
|
def __init__(self): |
1744
|
|
|
"""Initializes MicrogridGeneratorCollection""" |
1745
|
|
|
pass |
1746
|
|
|
|
1747
|
|
|
@staticmethod |
1748
|
|
|
def on_options(req, resp, id_): |
1749
|
|
|
_ = req |
1750
|
|
|
resp.status = falcon.HTTP_200 |
1751
|
|
|
_ = id_ |
1752
|
|
|
|
1753
|
|
|
@staticmethod |
1754
|
|
|
def on_get(req, resp, id_): |
1755
|
|
|
access_control(req) |
1756
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1757
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1758
|
|
|
description='API.INVALID_MICROGRID_ID') |
1759
|
|
|
|
1760
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1761
|
|
|
cursor = cnx.cursor() |
1762
|
|
|
|
1763
|
|
|
cursor.execute(" SELECT name " |
1764
|
|
|
" FROM tbl_microgrids " |
1765
|
|
|
" WHERE id = %s ", (id_,)) |
1766
|
|
|
if cursor.fetchone() is None: |
1767
|
|
|
cursor.close() |
1768
|
|
|
cnx.close() |
1769
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1770
|
|
|
description='API.MICROGRID_NOT_FOUND') |
1771
|
|
|
|
1772
|
|
|
# query meter dict |
1773
|
|
|
query = (" SELECT id, name, uuid " |
1774
|
|
|
" FROM tbl_meters ") |
1775
|
|
|
cursor.execute(query) |
1776
|
|
|
rows_meters = cursor.fetchall() |
1777
|
|
|
|
1778
|
|
|
meter_dict = dict() |
1779
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
1780
|
|
|
for row in rows_meters: |
1781
|
|
|
meter_dict[row[0]] = {"id": row[0], |
1782
|
|
|
"name": row[1], |
1783
|
|
|
"uuid": row[2]} |
1784
|
|
|
# query point dict |
1785
|
|
|
query = (" SELECT id, name " |
1786
|
|
|
" FROM tbl_points ") |
1787
|
|
|
cursor.execute(query) |
1788
|
|
|
rows_points = cursor.fetchall() |
1789
|
|
|
|
1790
|
|
|
point_dict = dict() |
1791
|
|
|
if rows_points is not None and len(rows_points) > 0: |
1792
|
|
|
for row in rows_points: |
1793
|
|
|
point_dict[row[0]] = {"id": row[0], |
1794
|
|
|
"name": row[1]} |
1795
|
|
|
|
1796
|
|
|
query = (" SELECT id, name, uuid, " |
1797
|
|
|
" power_point_id, meter_id, rated_output_power " |
1798
|
|
|
" FROM tbl_microgrids_generators " |
1799
|
|
|
" WHERE microgrid_id = %s " |
1800
|
|
|
" ORDER BY name ") |
1801
|
|
|
cursor.execute(query, (id_,)) |
1802
|
|
|
rows = cursor.fetchall() |
1803
|
|
|
|
1804
|
|
|
result = list() |
1805
|
|
|
if rows is not None and len(rows) > 0: |
1806
|
|
|
for row in rows: |
1807
|
|
|
meta_result = {"id": row[0], |
1808
|
|
|
"name": row[1], |
1809
|
|
|
"uuid": row[2], |
1810
|
|
|
"power_point": point_dict.get(row[3]), |
1811
|
|
|
"meter": meter_dict.get(row[4]), |
1812
|
|
|
"rated_output_power": row[5]} |
1813
|
|
|
result.append(meta_result) |
1814
|
|
|
|
1815
|
|
|
resp.text = json.dumps(result) |
1816
|
|
|
|
1817
|
|
|
@staticmethod |
1818
|
|
|
@user_logger |
1819
|
|
|
def on_post(req, resp, id_): |
1820
|
|
|
"""Handles POST requests""" |
1821
|
|
|
admin_control(req) |
1822
|
|
|
try: |
1823
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1824
|
|
|
except Exception as ex: |
1825
|
|
|
print(str(ex)) |
1826
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
1827
|
|
|
title='API.BAD_REQUEST', |
1828
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
1829
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1830
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1831
|
|
|
description='API.INVALID_MICROGRID_ID') |
1832
|
|
|
|
1833
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1834
|
|
|
cursor = cnx.cursor() |
1835
|
|
|
|
1836
|
|
|
cursor.execute(" SELECT name " |
1837
|
|
|
" FROM tbl_microgrids " |
1838
|
|
|
" WHERE id = %s ", (id_,)) |
1839
|
|
|
if cursor.fetchone() is None: |
1840
|
|
|
cursor.close() |
1841
|
|
|
cnx.close() |
1842
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1843
|
|
|
description='API.MICROGRID_NOT_FOUND') |
1844
|
|
|
|
1845
|
|
|
new_values = json.loads(raw_json) |
1846
|
|
|
|
1847
|
|
|
if 'name' not in new_values['data'].keys() or \ |
1848
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
1849
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
1850
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1851
|
|
|
description='API.INVALID_MICROGRID_GENERATOR_NAME') |
1852
|
|
|
name = str.strip(new_values['data']['name']) |
1853
|
|
|
|
1854
|
|
|
if 'power_point_id' not in new_values['data'].keys() or \ |
1855
|
|
|
not isinstance(new_values['data']['power_point_id'], int) or \ |
1856
|
|
|
new_values['data']['power_point_id'] <= 0: |
1857
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1858
|
|
|
description='API.INVALID_POWER_POINT_ID') |
1859
|
|
|
power_point_id = new_values['data']['power_point_id'] |
1860
|
|
|
|
1861
|
|
|
if 'meter_id' not in new_values['data'].keys() or \ |
1862
|
|
|
not isinstance(new_values['data']['meter_id'], int) or \ |
1863
|
|
|
new_values['data']['meter_id'] <= 0: |
1864
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1865
|
|
|
description='API.INVALID_METER_ID') |
1866
|
|
|
meter_id = new_values['data']['meter_id'] |
1867
|
|
|
|
1868
|
|
|
if 'rated_output_power' not in new_values['data'].keys() or \ |
1869
|
|
|
not (isinstance(new_values['data']['rated_output_power'], float) or |
1870
|
|
|
isinstance(new_values['data']['rated_output_power'], int)): |
1871
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1872
|
|
|
description='API.INVALID_RATED_OUTPUT_POWER') |
1873
|
|
|
rated_output_power = float(new_values['data']['rated_output_power']) |
1874
|
|
|
|
1875
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1876
|
|
|
cursor = cnx.cursor() |
1877
|
|
|
|
1878
|
|
|
cursor.execute(" SELECT name " |
1879
|
|
|
" FROM tbl_microgrids " |
1880
|
|
|
" WHERE id = %s ", |
1881
|
|
|
(id_,)) |
1882
|
|
|
if cursor.fetchone() is None: |
1883
|
|
|
cursor.close() |
1884
|
|
|
cnx.close() |
1885
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1886
|
|
|
description='API.MICROGRID_NOT_FOUND') |
1887
|
|
|
|
1888
|
|
|
cursor.execute(" SELECT name " |
1889
|
|
|
" FROM tbl_microgrids_generators " |
1890
|
|
|
" WHERE microgrid_id = %s AND name = %s ", |
1891
|
|
|
(id_, name,)) |
1892
|
|
|
if cursor.fetchone() is not None: |
1893
|
|
|
cursor.close() |
1894
|
|
|
cnx.close() |
1895
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1896
|
|
|
description='API.MICROGRID_GENERATOR_NAME_IS_ALREADY_IN_USE') |
1897
|
|
|
|
1898
|
|
|
cursor.execute(" SELECT name " |
1899
|
|
|
" FROM tbl_points " |
1900
|
|
|
" WHERE id = %s ", |
1901
|
|
|
(power_point_id,)) |
1902
|
|
|
if cursor.fetchone() is None: |
1903
|
|
|
cursor.close() |
1904
|
|
|
cnx.close() |
1905
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1906
|
|
|
description='API.POWER_POINT_NOT_FOUND') |
1907
|
|
|
|
1908
|
|
|
cursor.execute(" SELECT name " |
1909
|
|
|
" FROM tbl_meters " |
1910
|
|
|
" WHERE id = %s ", |
1911
|
|
|
(meter_id,)) |
1912
|
|
|
if cursor.fetchone() is None: |
1913
|
|
|
cursor.close() |
1914
|
|
|
cnx.close() |
1915
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1916
|
|
|
description='API.METER_NOT_FOUND') |
1917
|
|
|
|
1918
|
|
|
add_values = (" INSERT INTO tbl_microgrids_generators " |
1919
|
|
|
" (name, uuid, microgrid_id, power_point_id, meter_id, rated_output_power) " |
1920
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s) ") |
1921
|
|
|
cursor.execute(add_values, (name, |
1922
|
|
|
str(uuid.uuid4()), |
1923
|
|
|
id_, |
1924
|
|
|
power_point_id, |
1925
|
|
|
meter_id, |
1926
|
|
|
rated_output_power)) |
1927
|
|
|
new_id = cursor.lastrowid |
1928
|
|
|
cnx.commit() |
1929
|
|
|
cursor.close() |
1930
|
|
|
cnx.close() |
1931
|
|
|
|
1932
|
|
|
resp.status = falcon.HTTP_201 |
1933
|
|
|
resp.location = '/microgrids/' + str(id_) + '/generators/' + str(new_id) |
1934
|
|
|
|
1935
|
|
|
|
1936
|
|
View Code Duplication |
class MicrogridGeneratorItem: |
|
|
|
|
1937
|
|
|
def __init__(self): |
1938
|
|
|
"""Initializes MicrogridGeneratorItem""" |
1939
|
|
|
pass |
1940
|
|
|
|
1941
|
|
|
@staticmethod |
1942
|
|
|
def on_options(req, resp, id_, gid): |
1943
|
|
|
_ = req |
1944
|
|
|
resp.status = falcon.HTTP_200 |
1945
|
|
|
_ = id_ |
1946
|
|
|
|
1947
|
|
|
@staticmethod |
1948
|
|
|
def on_get(req, resp, id_, gid): |
1949
|
|
|
access_control(req) |
1950
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1951
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1952
|
|
|
description='API.INVALID_MICROGRID_ID') |
1953
|
|
|
if not gid.isdigit() or int(gid) <= 0: |
1954
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1955
|
|
|
description='API.INVALID_MICROGRID_GENERATOR_ID') |
1956
|
|
|
|
1957
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1958
|
|
|
cursor = cnx.cursor() |
1959
|
|
|
|
1960
|
|
|
cursor.execute(" SELECT name " |
1961
|
|
|
" FROM tbl_microgrids " |
1962
|
|
|
" WHERE id = %s ", (id_,)) |
1963
|
|
|
if cursor.fetchone() is None: |
1964
|
|
|
cursor.close() |
1965
|
|
|
cnx.close() |
1966
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1967
|
|
|
description='API.MICROGRID_NOT_FOUND') |
1968
|
|
|
|
1969
|
|
|
# query microgrid dict |
1970
|
|
|
query = (" SELECT id, name, uuid " |
1971
|
|
|
" FROM tbl_microgrids ") |
1972
|
|
|
cursor.execute(query) |
1973
|
|
|
rows_microgrids = cursor.fetchall() |
1974
|
|
|
|
1975
|
|
|
microgrid_dict = dict() |
1976
|
|
|
if rows_microgrids is not None and len(rows_microgrids) > 0: |
1977
|
|
|
for row in rows_microgrids: |
1978
|
|
|
microgrid_dict[row[0]] = {"id": row[0], |
1979
|
|
|
"name": row[1], |
1980
|
|
|
"uuid": row[2]} |
1981
|
|
|
# query meter dict |
1982
|
|
|
query = (" SELECT id, name, uuid " |
1983
|
|
|
" FROM tbl_meters ") |
1984
|
|
|
cursor.execute(query) |
1985
|
|
|
rows_meters = cursor.fetchall() |
1986
|
|
|
|
1987
|
|
|
meter_dict = dict() |
1988
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
1989
|
|
|
for row in rows_meters: |
1990
|
|
|
meter_dict[row[0]] = {"id": row[0], |
1991
|
|
|
"name": row[1], |
1992
|
|
|
"uuid": row[2]} |
1993
|
|
|
# query point dict |
1994
|
|
|
query = (" SELECT id, name " |
1995
|
|
|
" FROM tbl_points ") |
1996
|
|
|
cursor.execute(query) |
1997
|
|
|
rows_points = cursor.fetchall() |
1998
|
|
|
|
1999
|
|
|
point_dict = dict() |
2000
|
|
|
if rows_points is not None and len(rows_points) > 0: |
2001
|
|
|
for row in rows_points: |
2002
|
|
|
point_dict[row[0]] = {"id": row[0], |
2003
|
|
|
"name": row[1]} |
2004
|
|
|
|
2005
|
|
|
query = (" SELECT id, name, uuid, microgrid_id, power_point_id, meter_id, rated_output_power " |
2006
|
|
|
" FROM tbl_microgrids_generators " |
2007
|
|
|
" WHERE id = %s ") |
2008
|
|
|
cursor.execute(query, (gid,)) |
2009
|
|
|
row = cursor.fetchone() |
2010
|
|
|
cursor.close() |
2011
|
|
|
cnx.close() |
2012
|
|
|
|
2013
|
|
|
if row is None: |
2014
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2015
|
|
|
description='API.MICROGRID_GENERATOR_NOT_FOUND') |
2016
|
|
|
else: |
2017
|
|
|
meta_result = {"id": row[0], |
2018
|
|
|
"name": row[1], |
2019
|
|
|
"uuid": row[2], |
2020
|
|
|
"microgrid": microgrid_dict.get(row[3]), |
2021
|
|
|
"power_point": point_dict.get(row[4]), |
2022
|
|
|
"meter": meter_dict.get(row[5]), |
2023
|
|
|
"rated_output_power": row[6]} |
2024
|
|
|
|
2025
|
|
|
resp.text = json.dumps(meta_result) |
2026
|
|
|
|
2027
|
|
|
@staticmethod |
2028
|
|
|
@user_logger |
2029
|
|
|
def on_delete(req, resp, id_, gid): |
2030
|
|
|
admin_control(req) |
2031
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
2032
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2033
|
|
|
description='API.INVALID_MICROGRID_ID') |
2034
|
|
|
if not gid.isdigit() or int(gid) <= 0: |
2035
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2036
|
|
|
description='API.INVALID_MICROGRID_GENERATOR_ID') |
2037
|
|
|
|
2038
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2039
|
|
|
cursor = cnx.cursor() |
2040
|
|
|
|
2041
|
|
|
cursor.execute(" SELECT name " |
2042
|
|
|
" FROM tbl_microgrids " |
2043
|
|
|
" WHERE id = %s ", (id_,)) |
2044
|
|
|
if cursor.fetchone() is None: |
2045
|
|
|
cursor.close() |
2046
|
|
|
cnx.close() |
2047
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2048
|
|
|
description='API.MICROGRID_NOT_FOUND') |
2049
|
|
|
|
2050
|
|
|
cursor.execute(" SELECT name " |
2051
|
|
|
" FROM tbl_microgrids_generators " |
2052
|
|
|
" WHERE id = %s ", (gid,)) |
2053
|
|
|
if cursor.fetchone() is None: |
2054
|
|
|
cursor.close() |
2055
|
|
|
cnx.close() |
2056
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2057
|
|
|
description='API.MICROGRID_GENERATOR_NOT_FOUND') |
2058
|
|
|
|
2059
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_generators " |
2060
|
|
|
" WHERE id = %s ", (gid,)) |
2061
|
|
|
cnx.commit() |
2062
|
|
|
|
2063
|
|
|
cursor.close() |
2064
|
|
|
cnx.close() |
2065
|
|
|
|
2066
|
|
|
resp.status = falcon.HTTP_204 |
2067
|
|
|
|
2068
|
|
|
@staticmethod |
2069
|
|
|
@user_logger |
2070
|
|
|
def on_put(req, resp, id_, gid): |
2071
|
|
|
"""Handles PUT requests""" |
2072
|
|
|
admin_control(req) |
2073
|
|
|
try: |
2074
|
|
|
raw_json = req.stream.read().decode('utf-8') |
2075
|
|
|
except Exception as ex: |
2076
|
|
|
print(str(ex)) |
2077
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
2078
|
|
|
title='API.BAD_REQUEST', |
2079
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
2080
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
2081
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2082
|
|
|
description='API.INVALID_MICROGRID_ID') |
2083
|
|
|
if not gid.isdigit() or int(gid) <= 0: |
2084
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2085
|
|
|
description='API.INVALID_MICROGRID_GENERATOR_ID') |
2086
|
|
|
|
2087
|
|
|
new_values = json.loads(raw_json) |
2088
|
|
|
|
2089
|
|
|
if 'name' not in new_values['data'].keys() or \ |
2090
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
2091
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
2092
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2093
|
|
|
description='API.INVALID_MICROGRID_GENERATOR_NAME') |
2094
|
|
|
name = str.strip(new_values['data']['name']) |
2095
|
|
|
|
2096
|
|
|
if 'power_point_id' not in new_values['data'].keys() or \ |
2097
|
|
|
not isinstance(new_values['data']['power_point_id'], int) or \ |
2098
|
|
|
new_values['data']['power_point_id'] <= 0: |
2099
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2100
|
|
|
description='API.INVALID_POWER_POINT_ID') |
2101
|
|
|
power_point_id = new_values['data']['power_point_id'] |
2102
|
|
|
|
2103
|
|
|
if 'meter_id' not in new_values['data'].keys() or \ |
2104
|
|
|
not isinstance(new_values['data']['meter_id'], int) or \ |
2105
|
|
|
new_values['data']['meter_id'] <= 0: |
2106
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2107
|
|
|
description='API.INVALID_METER_ID') |
2108
|
|
|
meter_id = new_values['data']['meter_id'] |
2109
|
|
|
|
2110
|
|
|
if 'rated_output_power' not in new_values['data'].keys() or \ |
2111
|
|
|
not (isinstance(new_values['data']['rated_output_power'], float) or |
2112
|
|
|
isinstance(new_values['data']['rated_output_power'], int)): |
2113
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2114
|
|
|
description='API.INVALID_RATED_OUTPUT_POWER') |
2115
|
|
|
rated_output_power = float(new_values['data']['rated_output_power']) |
2116
|
|
|
|
2117
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2118
|
|
|
cursor = cnx.cursor() |
2119
|
|
|
|
2120
|
|
|
cursor.execute(" SELECT name " |
2121
|
|
|
" FROM tbl_microgrids " |
2122
|
|
|
" WHERE id = %s ", (id_,)) |
2123
|
|
|
if cursor.fetchone() is None: |
2124
|
|
|
cursor.close() |
2125
|
|
|
cnx.close() |
2126
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2127
|
|
|
description='API.MICROGRID_NOT_FOUND') |
2128
|
|
|
|
2129
|
|
|
cursor.execute(" SELECT name " |
2130
|
|
|
" FROM tbl_microgrids_generators " |
2131
|
|
|
" WHERE id = %s ", (gid,)) |
2132
|
|
|
if cursor.fetchone() is None: |
2133
|
|
|
cursor.close() |
2134
|
|
|
cnx.close() |
2135
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2136
|
|
|
description='API.MICROGRID_GENERATOR_NOT_FOUND') |
2137
|
|
|
|
2138
|
|
|
cursor.execute(" SELECT name " |
2139
|
|
|
" FROM tbl_microgrids_generators " |
2140
|
|
|
" WHERE microgrid_id = %s AND name = %s AND id != %s ", |
2141
|
|
|
(id_, name, gid)) |
2142
|
|
|
if cursor.fetchone() is not None: |
2143
|
|
|
cursor.close() |
2144
|
|
|
cnx.close() |
2145
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2146
|
|
|
description='API.MICROGRID_GENERATOR_NAME_IS_ALREADY_IN_USE') |
2147
|
|
|
|
2148
|
|
|
cursor.execute(" SELECT name " |
2149
|
|
|
" FROM tbl_points " |
2150
|
|
|
" WHERE id = %s ", |
2151
|
|
|
(power_point_id,)) |
2152
|
|
|
if cursor.fetchone() is None: |
2153
|
|
|
cursor.close() |
2154
|
|
|
cnx.close() |
2155
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2156
|
|
|
description='API.POWER_POINT_NOT_FOUND') |
2157
|
|
|
|
2158
|
|
|
cursor.execute(" SELECT name " |
2159
|
|
|
" FROM tbl_meters " |
2160
|
|
|
" WHERE id = %s ", |
2161
|
|
|
(meter_id,)) |
2162
|
|
|
if cursor.fetchone() is None: |
2163
|
|
|
cursor.close() |
2164
|
|
|
cnx.close() |
2165
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2166
|
|
|
description='API.METER_NOT_FOUND') |
2167
|
|
|
|
2168
|
|
|
update_row = (" UPDATE tbl_microgrids_generators " |
2169
|
|
|
" SET name = %s, microgrid_id = %s, power_point_id = %s, meter_id = %s, rated_output_power = %s " |
2170
|
|
|
" WHERE id = %s ") |
2171
|
|
|
cursor.execute(update_row, (name, |
2172
|
|
|
id_, |
2173
|
|
|
power_point_id, |
2174
|
|
|
meter_id, |
2175
|
|
|
rated_output_power, |
2176
|
|
|
gid)) |
2177
|
|
|
cnx.commit() |
2178
|
|
|
|
2179
|
|
|
cursor.close() |
2180
|
|
|
cnx.close() |
2181
|
|
|
|
2182
|
|
|
resp.status = falcon.HTTP_200 |
2183
|
|
|
|
2184
|
|
|
|
2185
|
|
View Code Duplication |
class MicrogridGridCollection: |
|
|
|
|
2186
|
|
|
def __init__(self): |
2187
|
|
|
"""Initializes MicrogridGridCollection""" |
2188
|
|
|
pass |
2189
|
|
|
|
2190
|
|
|
@staticmethod |
2191
|
|
|
def on_options(req, resp, id_): |
2192
|
|
|
_ = req |
2193
|
|
|
resp.status = falcon.HTTP_200 |
2194
|
|
|
_ = id_ |
2195
|
|
|
|
2196
|
|
|
@staticmethod |
2197
|
|
|
def on_get(req, resp, id_): |
2198
|
|
|
access_control(req) |
2199
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
2200
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2201
|
|
|
description='API.INVALID_MICROGRID_ID') |
2202
|
|
|
|
2203
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2204
|
|
|
cursor = cnx.cursor() |
2205
|
|
|
|
2206
|
|
|
cursor.execute(" SELECT name " |
2207
|
|
|
" FROM tbl_microgrids " |
2208
|
|
|
" WHERE id = %s ", (id_,)) |
2209
|
|
|
if cursor.fetchone() is None: |
2210
|
|
|
cursor.close() |
2211
|
|
|
cnx.close() |
2212
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2213
|
|
|
description='API.MICROGRID_NOT_FOUND') |
2214
|
|
|
|
2215
|
|
|
# query meter dict |
2216
|
|
|
query = (" SELECT id, name, uuid " |
2217
|
|
|
" FROM tbl_meters ") |
2218
|
|
|
cursor.execute(query) |
2219
|
|
|
rows_meters = cursor.fetchall() |
2220
|
|
|
|
2221
|
|
|
meter_dict = dict() |
2222
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
2223
|
|
|
for row in rows_meters: |
2224
|
|
|
meter_dict[row[0]] = {"id": row[0], |
2225
|
|
|
"name": row[1], |
2226
|
|
|
"uuid": row[2]} |
2227
|
|
|
# query point dict |
2228
|
|
|
query = (" SELECT id, name " |
2229
|
|
|
" FROM tbl_points ") |
2230
|
|
|
cursor.execute(query) |
2231
|
|
|
rows_points = cursor.fetchall() |
2232
|
|
|
|
2233
|
|
|
point_dict = dict() |
2234
|
|
|
if rows_points is not None and len(rows_points) > 0: |
2235
|
|
|
for row in rows_points: |
2236
|
|
|
point_dict[row[0]] = {"id": row[0], |
2237
|
|
|
"name": row[1]} |
2238
|
|
|
|
2239
|
|
|
query = (" SELECT id, name, uuid, " |
2240
|
|
|
" power_point_id, buy_meter_id, sell_meter_id, capacity " |
2241
|
|
|
" FROM tbl_microgrids_grids " |
2242
|
|
|
" WHERE microgrid_id = %s " |
2243
|
|
|
" ORDER BY name ") |
2244
|
|
|
cursor.execute(query, (id_,)) |
2245
|
|
|
rows = cursor.fetchall() |
2246
|
|
|
|
2247
|
|
|
result = list() |
2248
|
|
|
if rows is not None and len(rows) > 0: |
2249
|
|
|
for row in rows: |
2250
|
|
|
meta_result = {"id": row[0], |
2251
|
|
|
"name": row[1], |
2252
|
|
|
"uuid": row[2], |
2253
|
|
|
"power_point": point_dict.get(row[3]), |
2254
|
|
|
"buy_meter": meter_dict.get(row[4]), |
2255
|
|
|
"sell_meter": meter_dict.get(row[5]), |
2256
|
|
|
"capacity": row[6]} |
2257
|
|
|
result.append(meta_result) |
2258
|
|
|
|
2259
|
|
|
resp.text = json.dumps(result) |
2260
|
|
|
|
2261
|
|
|
@staticmethod |
2262
|
|
|
@user_logger |
2263
|
|
|
def on_post(req, resp, id_): |
2264
|
|
|
"""Handles POST requests""" |
2265
|
|
|
admin_control(req) |
2266
|
|
|
try: |
2267
|
|
|
raw_json = req.stream.read().decode('utf-8') |
2268
|
|
|
except Exception as ex: |
2269
|
|
|
print(str(ex)) |
2270
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
2271
|
|
|
title='API.BAD_REQUEST', |
2272
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
2273
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
2274
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2275
|
|
|
description='API.INVALID_MICROGRID_ID') |
2276
|
|
|
|
2277
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2278
|
|
|
cursor = cnx.cursor() |
2279
|
|
|
|
2280
|
|
|
cursor.execute(" SELECT name " |
2281
|
|
|
" FROM tbl_microgrids " |
2282
|
|
|
" WHERE id = %s ", (id_,)) |
2283
|
|
|
if cursor.fetchone() is None: |
2284
|
|
|
cursor.close() |
2285
|
|
|
cnx.close() |
2286
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2287
|
|
|
description='API.MICROGRID_NOT_FOUND') |
2288
|
|
|
|
2289
|
|
|
new_values = json.loads(raw_json) |
2290
|
|
|
|
2291
|
|
|
if 'name' not in new_values['data'].keys() or \ |
2292
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
2293
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
2294
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2295
|
|
|
description='API.INVALID_MICROGRID_GRID_NAME') |
2296
|
|
|
name = str.strip(new_values['data']['name']) |
2297
|
|
|
|
2298
|
|
|
if 'power_point_id' not in new_values['data'].keys() or \ |
2299
|
|
|
not isinstance(new_values['data']['power_point_id'], int) or \ |
2300
|
|
|
new_values['data']['power_point_id'] <= 0: |
2301
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2302
|
|
|
description='API.INVALID_POWER_POINT_ID') |
2303
|
|
|
power_point_id = new_values['data']['power_point_id'] |
2304
|
|
|
|
2305
|
|
|
if 'buy_meter_id' not in new_values['data'].keys() or \ |
2306
|
|
|
not isinstance(new_values['data']['buy_meter_id'], int) or \ |
2307
|
|
|
new_values['data']['buy_meter_id'] <= 0: |
2308
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2309
|
|
|
description='API.INVALID_BUY_METER_ID') |
2310
|
|
|
buy_meter_id = new_values['data']['buy_meter_id'] |
2311
|
|
|
|
2312
|
|
|
if 'sell_meter_id' not in new_values['data'].keys() or \ |
2313
|
|
|
not isinstance(new_values['data']['sell_meter_id'], int) or \ |
2314
|
|
|
new_values['data']['sell_meter_id'] <= 0: |
2315
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2316
|
|
|
description='API.INVALID_SELL_METER_ID') |
2317
|
|
|
sell_meter_id = new_values['data']['sell_meter_id'] |
2318
|
|
|
|
2319
|
|
|
if 'capacity' not in new_values['data'].keys() or \ |
2320
|
|
|
not (isinstance(new_values['data']['capacity'], float) or |
2321
|
|
|
isinstance(new_values['data']['capacity'], int)): |
2322
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2323
|
|
|
description='API.INVALID_CAPACITY') |
2324
|
|
|
capacity = float(new_values['data']['capacity']) |
2325
|
|
|
|
2326
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2327
|
|
|
cursor = cnx.cursor() |
2328
|
|
|
|
2329
|
|
|
cursor.execute(" SELECT name " |
2330
|
|
|
" FROM tbl_microgrids " |
2331
|
|
|
" WHERE id = %s ", |
2332
|
|
|
(id_,)) |
2333
|
|
|
if cursor.fetchone() is None: |
2334
|
|
|
cursor.close() |
2335
|
|
|
cnx.close() |
2336
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2337
|
|
|
description='API.MICROGRID_NOT_FOUND') |
2338
|
|
|
|
2339
|
|
|
cursor.execute(" SELECT name " |
2340
|
|
|
" FROM tbl_microgrids_grids " |
2341
|
|
|
" WHERE microgrid_id = %s AND name = %s ", |
2342
|
|
|
(id_, name,)) |
2343
|
|
|
if cursor.fetchone() is not None: |
2344
|
|
|
cursor.close() |
2345
|
|
|
cnx.close() |
2346
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2347
|
|
|
description='API.MICROGRID_GRID_NAME_IS_ALREADY_IN_USE') |
2348
|
|
|
|
2349
|
|
|
cursor.execute(" SELECT name " |
2350
|
|
|
" FROM tbl_points " |
2351
|
|
|
" WHERE id = %s ", |
2352
|
|
|
(power_point_id,)) |
2353
|
|
|
if cursor.fetchone() is None: |
2354
|
|
|
cursor.close() |
2355
|
|
|
cnx.close() |
2356
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2357
|
|
|
description='API.POWER_POINT_NOT_FOUND') |
2358
|
|
|
|
2359
|
|
|
cursor.execute(" SELECT name " |
2360
|
|
|
" FROM tbl_meters " |
2361
|
|
|
" WHERE id = %s ", |
2362
|
|
|
(buy_meter_id,)) |
2363
|
|
|
if cursor.fetchone() is None: |
2364
|
|
|
cursor.close() |
2365
|
|
|
cnx.close() |
2366
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2367
|
|
|
description='API.BUY_METER_NOT_FOUND') |
2368
|
|
|
|
2369
|
|
|
cursor.execute(" SELECT name " |
2370
|
|
|
" FROM tbl_meters " |
2371
|
|
|
" WHERE id = %s ", |
2372
|
|
|
(sell_meter_id,)) |
2373
|
|
|
if cursor.fetchone() is None: |
2374
|
|
|
cursor.close() |
2375
|
|
|
cnx.close() |
2376
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2377
|
|
|
description='API.SELL_METER_NOT_FOUND') |
2378
|
|
|
|
2379
|
|
|
add_values = (" INSERT INTO tbl_microgrids_grids " |
2380
|
|
|
" (name, uuid, microgrid_id, power_point_id, buy_meter_id, sell_meter_id, capacity) " |
2381
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
2382
|
|
|
cursor.execute(add_values, (name, |
2383
|
|
|
str(uuid.uuid4()), |
2384
|
|
|
id_, |
2385
|
|
|
power_point_id, |
2386
|
|
|
buy_meter_id, |
2387
|
|
|
sell_meter_id, |
2388
|
|
|
capacity)) |
2389
|
|
|
new_id = cursor.lastrowid |
2390
|
|
|
cnx.commit() |
2391
|
|
|
cursor.close() |
2392
|
|
|
cnx.close() |
2393
|
|
|
|
2394
|
|
|
resp.status = falcon.HTTP_201 |
2395
|
|
|
resp.location = '/microgrids/' + str(id_) + '/grids/' + str(new_id) |
2396
|
|
|
|
2397
|
|
|
|
2398
|
|
View Code Duplication |
class MicrogridGridItem: |
|
|
|
|
2399
|
|
|
def __init__(self): |
2400
|
|
|
"""Initializes MicrogridGridItem""" |
2401
|
|
|
pass |
2402
|
|
|
|
2403
|
|
|
@staticmethod |
2404
|
|
|
def on_options(req, resp, id_, gid): |
2405
|
|
|
_ = req |
2406
|
|
|
resp.status = falcon.HTTP_200 |
2407
|
|
|
_ = id_ |
2408
|
|
|
|
2409
|
|
|
@staticmethod |
2410
|
|
|
def on_get(req, resp, id_, gid): |
2411
|
|
|
access_control(req) |
2412
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
2413
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2414
|
|
|
description='API.INVALID_MICROGRID_ID') |
2415
|
|
|
if not gid.isdigit() or int(gid) <= 0: |
2416
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2417
|
|
|
description='API.INVALID_MICROGRID_GRID_ID') |
2418
|
|
|
|
2419
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2420
|
|
|
cursor = cnx.cursor() |
2421
|
|
|
|
2422
|
|
|
cursor.execute(" SELECT name " |
2423
|
|
|
" FROM tbl_microgrids " |
2424
|
|
|
" WHERE id = %s ", (id_,)) |
2425
|
|
|
if cursor.fetchone() is None: |
2426
|
|
|
cursor.close() |
2427
|
|
|
cnx.close() |
2428
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2429
|
|
|
description='API.MICROGRID_NOT_FOUND') |
2430
|
|
|
|
2431
|
|
|
# query microgrid dict |
2432
|
|
|
query = (" SELECT id, name, uuid " |
2433
|
|
|
" FROM tbl_microgrids ") |
2434
|
|
|
cursor.execute(query) |
2435
|
|
|
rows_microgrids = cursor.fetchall() |
2436
|
|
|
|
2437
|
|
|
microgrid_dict = dict() |
2438
|
|
|
if rows_microgrids is not None and len(rows_microgrids) > 0: |
2439
|
|
|
for row in rows_microgrids: |
2440
|
|
|
microgrid_dict[row[0]] = {"id": row[0], |
2441
|
|
|
"name": row[1], |
2442
|
|
|
"uuid": row[2]} |
2443
|
|
|
# query meter dict |
2444
|
|
|
query = (" SELECT id, name, uuid " |
2445
|
|
|
" FROM tbl_meters ") |
2446
|
|
|
cursor.execute(query) |
2447
|
|
|
rows_meters = cursor.fetchall() |
2448
|
|
|
|
2449
|
|
|
meter_dict = dict() |
2450
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
2451
|
|
|
for row in rows_meters: |
2452
|
|
|
meter_dict[row[0]] = {"id": row[0], |
2453
|
|
|
"name": row[1], |
2454
|
|
|
"uuid": row[2]} |
2455
|
|
|
# query point dict |
2456
|
|
|
query = (" SELECT id, name " |
2457
|
|
|
" FROM tbl_points ") |
2458
|
|
|
cursor.execute(query) |
2459
|
|
|
rows_points = cursor.fetchall() |
2460
|
|
|
|
2461
|
|
|
point_dict = dict() |
2462
|
|
|
if rows_points is not None and len(rows_points) > 0: |
2463
|
|
|
for row in rows_points: |
2464
|
|
|
point_dict[row[0]] = {"id": row[0], |
2465
|
|
|
"name": row[1]} |
2466
|
|
|
|
2467
|
|
|
query = (" SELECT id, name, uuid, microgrid_id, power_point_id, buy_meter_id, sell_meter_id, capacity " |
2468
|
|
|
" FROM tbl_microgrids_grids " |
2469
|
|
|
" WHERE id = %s ") |
2470
|
|
|
cursor.execute(query, (gid,)) |
2471
|
|
|
row = cursor.fetchone() |
2472
|
|
|
cursor.close() |
2473
|
|
|
cnx.close() |
2474
|
|
|
|
2475
|
|
|
if row is None: |
2476
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2477
|
|
|
description='API.MICROGRID_GRID_NOT_FOUND') |
2478
|
|
|
else: |
2479
|
|
|
meta_result = {"id": row[0], |
2480
|
|
|
"name": row[1], |
2481
|
|
|
"uuid": row[2], |
2482
|
|
|
"microgrid": microgrid_dict.get(row[3]), |
2483
|
|
|
"power_point": point_dict.get(row[4]), |
2484
|
|
|
"buy_meter": meter_dict.get(row[5]), |
2485
|
|
|
"sell_meter": meter_dict.get(row[6]), |
2486
|
|
|
"capacity": row[7]} |
2487
|
|
|
|
2488
|
|
|
resp.text = json.dumps(meta_result) |
2489
|
|
|
|
2490
|
|
|
@staticmethod |
2491
|
|
|
@user_logger |
2492
|
|
|
def on_delete(req, resp, id_, gid): |
2493
|
|
|
admin_control(req) |
2494
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
2495
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2496
|
|
|
description='API.INVALID_MICROGRID_ID') |
2497
|
|
|
if not gid.isdigit() or int(gid) <= 0: |
2498
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2499
|
|
|
description='API.INVALID_MICROGRID_GRID_ID') |
2500
|
|
|
|
2501
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2502
|
|
|
cursor = cnx.cursor() |
2503
|
|
|
|
2504
|
|
|
cursor.execute(" SELECT name " |
2505
|
|
|
" FROM tbl_microgrids " |
2506
|
|
|
" WHERE id = %s ", (id_,)) |
2507
|
|
|
if cursor.fetchone() is None: |
2508
|
|
|
cursor.close() |
2509
|
|
|
cnx.close() |
2510
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2511
|
|
|
description='API.MICROGRID_NOT_FOUND') |
2512
|
|
|
|
2513
|
|
|
cursor.execute(" SELECT name " |
2514
|
|
|
" FROM tbl_microgrids_grids " |
2515
|
|
|
" WHERE id = %s ", (gid,)) |
2516
|
|
|
if cursor.fetchone() is None: |
2517
|
|
|
cursor.close() |
2518
|
|
|
cnx.close() |
2519
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2520
|
|
|
description='API.MICROGRID_GRID_NOT_FOUND') |
2521
|
|
|
|
2522
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_grids " |
2523
|
|
|
" WHERE id = %s ", (gid,)) |
2524
|
|
|
cnx.commit() |
2525
|
|
|
|
2526
|
|
|
cursor.close() |
2527
|
|
|
cnx.close() |
2528
|
|
|
|
2529
|
|
|
resp.status = falcon.HTTP_204 |
2530
|
|
|
|
2531
|
|
|
@staticmethod |
2532
|
|
|
@user_logger |
2533
|
|
|
def on_put(req, resp, id_, gid): |
2534
|
|
|
"""Handles PUT requests""" |
2535
|
|
|
admin_control(req) |
2536
|
|
|
try: |
2537
|
|
|
raw_json = req.stream.read().decode('utf-8') |
2538
|
|
|
except Exception as ex: |
2539
|
|
|
print(str(ex)) |
2540
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
2541
|
|
|
title='API.BAD_REQUEST', |
2542
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
2543
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
2544
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2545
|
|
|
description='API.INVALID_MICROGRID_ID') |
2546
|
|
|
|
2547
|
|
|
if not gid.isdigit() or int(gid) <= 0: |
2548
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2549
|
|
|
description='API.INVALID_MICROGRID_GRID_ID') |
2550
|
|
|
|
2551
|
|
|
new_values = json.loads(raw_json) |
2552
|
|
|
|
2553
|
|
|
if 'name' not in new_values['data'].keys() or \ |
2554
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
2555
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
2556
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2557
|
|
|
description='API.INVALID_MICROGRID_GRID_NAME') |
2558
|
|
|
name = str.strip(new_values['data']['name']) |
2559
|
|
|
|
2560
|
|
|
if 'power_point_id' not in new_values['data'].keys() or \ |
2561
|
|
|
not isinstance(new_values['data']['power_point_id'], int) or \ |
2562
|
|
|
new_values['data']['power_point_id'] <= 0: |
2563
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2564
|
|
|
description='API.INVALID_POWER_POINT_ID') |
2565
|
|
|
power_point_id = new_values['data']['power_point_id'] |
2566
|
|
|
|
2567
|
|
|
if 'buy_meter_id' not in new_values['data'].keys() or \ |
2568
|
|
|
not isinstance(new_values['data']['buy_meter_id'], int) or \ |
2569
|
|
|
new_values['data']['buy_meter_id'] <= 0: |
2570
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2571
|
|
|
description='API.INVALID_BUY_METER_ID') |
2572
|
|
|
buy_meter_id = new_values['data']['buy_meter_id'] |
2573
|
|
|
|
2574
|
|
|
if 'sell_meter_id' not in new_values['data'].keys() or \ |
2575
|
|
|
not isinstance(new_values['data']['sell_meter_id'], int) or \ |
2576
|
|
|
new_values['data']['sell_meter_id'] <= 0: |
2577
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2578
|
|
|
description='API.INVALID_METER_ID') |
2579
|
|
|
sell_meter_id = new_values['data']['sell_meter_id'] |
2580
|
|
|
|
2581
|
|
|
if 'capacity' not in new_values['data'].keys() or \ |
2582
|
|
|
not (isinstance(new_values['data']['capacity'], float) or |
2583
|
|
|
isinstance(new_values['data']['capacity'], int)): |
2584
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2585
|
|
|
description='API.INVALID_CAPACITY') |
2586
|
|
|
capacity = float(new_values['data']['capacity']) |
2587
|
|
|
|
2588
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2589
|
|
|
cursor = cnx.cursor() |
2590
|
|
|
|
2591
|
|
|
cursor.execute(" SELECT name " |
2592
|
|
|
" FROM tbl_microgrids " |
2593
|
|
|
" WHERE id = %s ", (id_,)) |
2594
|
|
|
if cursor.fetchone() is None: |
2595
|
|
|
cursor.close() |
2596
|
|
|
cnx.close() |
2597
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2598
|
|
|
description='API.MICROGRID_NOT_FOUND') |
2599
|
|
|
|
2600
|
|
|
cursor.execute(" SELECT name " |
2601
|
|
|
" FROM tbl_microgrids_grids " |
2602
|
|
|
" WHERE id = %s ", (gid,)) |
2603
|
|
|
if cursor.fetchone() is None: |
2604
|
|
|
cursor.close() |
2605
|
|
|
cnx.close() |
2606
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2607
|
|
|
description='API.MICROGRID_GRID_NOT_FOUND') |
2608
|
|
|
|
2609
|
|
|
cursor.execute(" SELECT name " |
2610
|
|
|
" FROM tbl_microgrids_grids " |
2611
|
|
|
" WHERE microgrid_id = %s AND name = %s AND id != %s ", |
2612
|
|
|
(id_, name, gid)) |
2613
|
|
|
if cursor.fetchone() is not None: |
2614
|
|
|
cursor.close() |
2615
|
|
|
cnx.close() |
2616
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2617
|
|
|
description='API.MICROGRID_GRID_NAME_IS_ALREADY_IN_USE') |
2618
|
|
|
|
2619
|
|
|
cursor.execute(" SELECT name " |
2620
|
|
|
" FROM tbl_points " |
2621
|
|
|
" WHERE id = %s ", |
2622
|
|
|
(power_point_id,)) |
2623
|
|
|
if cursor.fetchone() is None: |
2624
|
|
|
cursor.close() |
2625
|
|
|
cnx.close() |
2626
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2627
|
|
|
description='API.POWER_POINT_NOT_FOUND') |
2628
|
|
|
|
2629
|
|
|
cursor.execute(" SELECT name " |
2630
|
|
|
" FROM tbl_meters " |
2631
|
|
|
" WHERE id = %s ", |
2632
|
|
|
(buy_meter_id,)) |
2633
|
|
|
if cursor.fetchone() is None: |
2634
|
|
|
cursor.close() |
2635
|
|
|
cnx.close() |
2636
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2637
|
|
|
description='API.BUY_METER_NOT_FOUND') |
2638
|
|
|
|
2639
|
|
|
cursor.execute(" SELECT name " |
2640
|
|
|
" FROM tbl_meters " |
2641
|
|
|
" WHERE id = %s ", |
2642
|
|
|
(sell_meter_id,)) |
2643
|
|
|
if cursor.fetchone() is None: |
2644
|
|
|
cursor.close() |
2645
|
|
|
cnx.close() |
2646
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2647
|
|
|
description='API.SELL_METER_NOT_FOUND') |
2648
|
|
|
|
2649
|
|
|
update_row = (" UPDATE tbl_microgrids_grids " |
2650
|
|
|
" SET name = %s, microgrid_id = %s, " |
2651
|
|
|
" power_point_id = %s, buy_meter_id = %s, sell_meter_id = %s, capacity = %s " |
2652
|
|
|
" WHERE id = %s ") |
2653
|
|
|
cursor.execute(update_row, (name, |
2654
|
|
|
id_, |
2655
|
|
|
power_point_id, |
2656
|
|
|
buy_meter_id, |
2657
|
|
|
sell_meter_id, |
2658
|
|
|
capacity, |
2659
|
|
|
gid)) |
2660
|
|
|
cnx.commit() |
2661
|
|
|
|
2662
|
|
|
cursor.close() |
2663
|
|
|
cnx.close() |
2664
|
|
|
|
2665
|
|
|
resp.status = falcon.HTTP_200 |
2666
|
|
|
|
2667
|
|
|
|
2668
|
|
|
class MicrogridHeatpumpCollection: |
2669
|
|
|
def __init__(self): |
2670
|
|
|
"""Initializes MicrogridHeatpumpCollection""" |
2671
|
|
|
pass |
2672
|
|
|
|
2673
|
|
|
@staticmethod |
2674
|
|
|
def on_options(req, resp, id_): |
2675
|
|
|
_ = req |
2676
|
|
|
resp.status = falcon.HTTP_200 |
2677
|
|
|
_ = id_ |
2678
|
|
|
|
2679
|
|
|
@staticmethod |
2680
|
|
|
def on_get(req, resp, id_): |
2681
|
|
|
access_control(req) |
2682
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
2683
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2684
|
|
|
description='API.INVALID_MICROGRID_ID') |
2685
|
|
|
|
2686
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2687
|
|
|
cursor = cnx.cursor() |
2688
|
|
|
|
2689
|
|
|
cursor.execute(" SELECT name " |
2690
|
|
|
" FROM tbl_microgrids " |
2691
|
|
|
" WHERE id = %s ", (id_,)) |
2692
|
|
|
if cursor.fetchone() is None: |
2693
|
|
|
cursor.close() |
2694
|
|
|
cnx.close() |
2695
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2696
|
|
|
description='API.MICROGRID_NOT_FOUND') |
2697
|
|
|
|
2698
|
|
|
# query meter dict |
2699
|
|
|
query = (" SELECT id, name, uuid " |
2700
|
|
|
" FROM tbl_meters ") |
2701
|
|
|
cursor.execute(query) |
2702
|
|
|
rows_meters = cursor.fetchall() |
2703
|
|
|
|
2704
|
|
|
meter_dict = dict() |
2705
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
2706
|
|
|
for row in rows_meters: |
2707
|
|
|
meter_dict[row[0]] = {"id": row[0], |
2708
|
|
|
"name": row[1], |
2709
|
|
|
"uuid": row[2]} |
2710
|
|
|
# query point dict |
2711
|
|
|
query = (" SELECT id, name " |
2712
|
|
|
" FROM tbl_points ") |
2713
|
|
|
cursor.execute(query) |
2714
|
|
|
rows_points = cursor.fetchall() |
2715
|
|
|
|
2716
|
|
|
point_dict = dict() |
2717
|
|
|
if rows_points is not None and len(rows_points) > 0: |
2718
|
|
|
for row in rows_points: |
2719
|
|
|
point_dict[row[0]] = {"id": row[0], |
2720
|
|
|
"name": row[1]} |
2721
|
|
|
|
2722
|
|
|
query = (" SELECT id, name, uuid, " |
2723
|
|
|
" power_point_id, electricity_meter_id, heat_meter_id, cooling_meter_id, rated_input_power " |
2724
|
|
|
" FROM tbl_microgrids_heatpumps " |
2725
|
|
|
" WHERE microgrid_id = %s " |
2726
|
|
|
" ORDER BY name ") |
2727
|
|
|
cursor.execute(query, (id_,)) |
2728
|
|
|
rows = cursor.fetchall() |
2729
|
|
|
|
2730
|
|
|
result = list() |
2731
|
|
|
if rows is not None and len(rows) > 0: |
2732
|
|
|
for row in rows: |
2733
|
|
|
meta_result = {"id": row[0], |
2734
|
|
|
"name": row[1], |
2735
|
|
|
"uuid": row[2], |
2736
|
|
|
"power_point": point_dict.get(row[3], None), |
2737
|
|
|
"electricity_meter": meter_dict.get(row[4], None), |
2738
|
|
|
"heat_meter": meter_dict.get(row[5], None), |
2739
|
|
|
"cooling_meter": meter_dict.get(row[6], None), |
2740
|
|
|
"rated_input_power": row[7]} |
2741
|
|
|
result.append(meta_result) |
2742
|
|
|
|
2743
|
|
|
resp.text = json.dumps(result) |
2744
|
|
|
|
2745
|
|
|
@staticmethod |
2746
|
|
|
@user_logger |
2747
|
|
|
def on_post(req, resp, id_): |
2748
|
|
|
"""Handles POST requests""" |
2749
|
|
|
admin_control(req) |
2750
|
|
|
try: |
2751
|
|
|
raw_json = req.stream.read().decode('utf-8') |
2752
|
|
|
except Exception as ex: |
2753
|
|
|
print(str(ex)) |
2754
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
2755
|
|
|
title='API.BAD_REQUEST', |
2756
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
2757
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
2758
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2759
|
|
|
description='API.INVALID_MICROGRID_ID') |
2760
|
|
|
|
2761
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2762
|
|
|
cursor = cnx.cursor() |
2763
|
|
|
|
2764
|
|
|
cursor.execute(" SELECT name " |
2765
|
|
|
" FROM tbl_microgrids " |
2766
|
|
|
" WHERE id = %s ", (id_,)) |
2767
|
|
|
if cursor.fetchone() is None: |
2768
|
|
|
cursor.close() |
2769
|
|
|
cnx.close() |
2770
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2771
|
|
|
description='API.MICROGRID_NOT_FOUND') |
2772
|
|
|
|
2773
|
|
|
new_values = json.loads(raw_json) |
2774
|
|
|
|
2775
|
|
|
if 'name' not in new_values['data'].keys() or \ |
2776
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
2777
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
2778
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2779
|
|
|
description='API.INVALID_MICROGRID_HEATPUMP_NAME') |
2780
|
|
|
name = str.strip(new_values['data']['name']) |
2781
|
|
|
|
2782
|
|
|
if 'power_point_id' not in new_values['data'].keys() or \ |
2783
|
|
|
not isinstance(new_values['data']['power_point_id'], int) or \ |
2784
|
|
|
new_values['data']['power_point_id'] <= 0: |
2785
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2786
|
|
|
description='API.INVALID_POWER_POINT_ID') |
2787
|
|
|
power_point_id = new_values['data']['power_point_id'] |
2788
|
|
|
|
2789
|
|
|
if 'electricity_meter_id' not in new_values['data'].keys() or \ |
2790
|
|
|
not isinstance(new_values['data']['electricity_meter_id'], int) or \ |
2791
|
|
|
new_values['data']['electricity_meter_id'] <= 0: |
2792
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2793
|
|
|
description='API.INVALID_ELECTRICITY_METER_ID') |
2794
|
|
|
electricity_meter_id = new_values['data']['electricity_meter_id'] |
2795
|
|
|
|
2796
|
|
|
if 'heat_meter_id' not in new_values['data'].keys() or \ |
2797
|
|
|
not isinstance(new_values['data']['heat_meter_id'], int) or \ |
2798
|
|
|
new_values['data']['heat_meter_id'] <= 0: |
2799
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2800
|
|
|
description='API.INVALID_SELL_METER_ID') |
2801
|
|
|
heat_meter_id = new_values['data']['heat_meter_id'] |
2802
|
|
|
|
2803
|
|
|
if 'cooling_meter_id' not in new_values['data'].keys() or \ |
2804
|
|
|
not isinstance(new_values['data']['cooling_meter_id'], int) or \ |
2805
|
|
|
new_values['data']['cooling_meter_id'] <= 0: |
2806
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2807
|
|
|
description='API.INVALID_COOLING_METER_ID') |
2808
|
|
|
cooling_meter_id = new_values['data']['cooling_meter_id'] |
2809
|
|
|
|
2810
|
|
|
if 'rated_input_power' not in new_values['data'].keys() or \ |
2811
|
|
|
not (isinstance(new_values['data']['rated_input_power'], float) or |
2812
|
|
|
isinstance(new_values['data']['rated_input_power'], int)): |
2813
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2814
|
|
|
description='API.INVALID_RATED_INPUT_POWER') |
2815
|
|
|
rated_input_power = float(new_values['data']['rated_input_power']) |
2816
|
|
|
|
2817
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2818
|
|
|
cursor = cnx.cursor() |
2819
|
|
|
|
2820
|
|
|
cursor.execute(" SELECT name " |
2821
|
|
|
" FROM tbl_microgrids " |
2822
|
|
|
" WHERE id = %s ", |
2823
|
|
|
(id_,)) |
2824
|
|
|
if cursor.fetchone() is None: |
2825
|
|
|
cursor.close() |
2826
|
|
|
cnx.close() |
2827
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2828
|
|
|
description='API.MICROGRID_NOT_FOUND') |
2829
|
|
|
|
2830
|
|
|
cursor.execute(" SELECT name " |
2831
|
|
|
" FROM tbl_microgrids_heatpumps " |
2832
|
|
|
" WHERE microgrid_id = %s AND name = %s ", |
2833
|
|
|
(id_, name,)) |
2834
|
|
|
if cursor.fetchone() is not None: |
2835
|
|
|
cursor.close() |
2836
|
|
|
cnx.close() |
2837
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2838
|
|
|
description='API.MICROGRID_HEATPUMP_NAME_IS_ALREADY_IN_USE') |
2839
|
|
|
|
2840
|
|
|
cursor.execute(" SELECT name " |
2841
|
|
|
" FROM tbl_points " |
2842
|
|
|
" WHERE id = %s ", |
2843
|
|
|
(power_point_id,)) |
2844
|
|
|
if cursor.fetchone() is None: |
2845
|
|
|
cursor.close() |
2846
|
|
|
cnx.close() |
2847
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2848
|
|
|
description='API.POWER_POINT_NOT_FOUND') |
2849
|
|
|
|
2850
|
|
|
cursor.execute(" SELECT name " |
2851
|
|
|
" FROM tbl_meters " |
2852
|
|
|
" WHERE id = %s ", |
2853
|
|
|
(electricity_meter_id,)) |
2854
|
|
|
if cursor.fetchone() is None: |
2855
|
|
|
cursor.close() |
2856
|
|
|
cnx.close() |
2857
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2858
|
|
|
description='API.ELECTRICITY_METER_NOT_FOUND') |
2859
|
|
|
|
2860
|
|
|
cursor.execute(" SELECT name " |
2861
|
|
|
" FROM tbl_meters " |
2862
|
|
|
" WHERE id = %s ", |
2863
|
|
|
(heat_meter_id,)) |
2864
|
|
|
if cursor.fetchone() is None: |
2865
|
|
|
cursor.close() |
2866
|
|
|
cnx.close() |
2867
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2868
|
|
|
description='API.HEAT_METER_NOT_FOUND') |
2869
|
|
|
|
2870
|
|
|
cursor.execute(" SELECT name " |
2871
|
|
|
" FROM tbl_meters " |
2872
|
|
|
" WHERE id = %s ", |
2873
|
|
|
(cooling_meter_id,)) |
2874
|
|
|
if cursor.fetchone() is None: |
2875
|
|
|
cursor.close() |
2876
|
|
|
cnx.close() |
2877
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2878
|
|
|
description='API.COOLING_METER_NOT_FOUND') |
2879
|
|
|
|
2880
|
|
|
add_values = (" INSERT INTO tbl_microgrids_heatpumps " |
2881
|
|
|
" (name, uuid, microgrid_id, " |
2882
|
|
|
" power_point_id, electricity_meter_id, heat_meter_id, cooling_meter_id, rated_input_power) " |
2883
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
2884
|
|
|
cursor.execute(add_values, (name, |
2885
|
|
|
str(uuid.uuid4()), |
2886
|
|
|
id_, |
2887
|
|
|
power_point_id, |
2888
|
|
|
electricity_meter_id, |
2889
|
|
|
heat_meter_id, |
2890
|
|
|
cooling_meter_id, |
2891
|
|
|
rated_input_power)) |
2892
|
|
|
new_id = cursor.lastrowid |
2893
|
|
|
cnx.commit() |
2894
|
|
|
cursor.close() |
2895
|
|
|
cnx.close() |
2896
|
|
|
|
2897
|
|
|
resp.status = falcon.HTTP_201 |
2898
|
|
|
resp.location = '/microgrids/' + str(id_) + '/heatpumps/' + str(new_id) |
2899
|
|
|
|
2900
|
|
|
|
2901
|
|
|
class MicrogridHeatpumpItem: |
2902
|
|
|
def __init__(self): |
2903
|
|
|
"""Initializes MicrogridHeatpumpItem""" |
2904
|
|
|
pass |
2905
|
|
|
|
2906
|
|
|
@staticmethod |
2907
|
|
|
def on_options(req, resp, id_, hid): |
2908
|
|
|
_ = req |
2909
|
|
|
resp.status = falcon.HTTP_200 |
2910
|
|
|
_ = id_ |
2911
|
|
|
|
2912
|
|
|
@staticmethod |
2913
|
|
|
def on_get(req, resp, id_, hid): |
2914
|
|
|
access_control(req) |
2915
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
2916
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2917
|
|
|
description='API.INVALID_MICROGRID_ID') |
2918
|
|
|
if not hid.isdigit() or int(hid) <= 0: |
2919
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2920
|
|
|
description='API.INVALID_MICROGRID_HEATPUMP_ID') |
2921
|
|
|
|
2922
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2923
|
|
|
cursor = cnx.cursor() |
2924
|
|
|
|
2925
|
|
|
cursor.execute(" SELECT name " |
2926
|
|
|
" FROM tbl_microgrids " |
2927
|
|
|
" WHERE id = %s ", (id_,)) |
2928
|
|
|
if cursor.fetchone() is None: |
2929
|
|
|
cursor.close() |
2930
|
|
|
cnx.close() |
2931
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2932
|
|
|
description='API.MICROGRID_NOT_FOUND') |
2933
|
|
|
|
2934
|
|
|
# query microgrid dict |
2935
|
|
|
query = (" SELECT id, name, uuid " |
2936
|
|
|
" FROM tbl_microgrids ") |
2937
|
|
|
cursor.execute(query) |
2938
|
|
|
rows_microgrids = cursor.fetchall() |
2939
|
|
|
|
2940
|
|
|
microgrid_dict = dict() |
2941
|
|
|
if rows_microgrids is not None and len(rows_microgrids) > 0: |
2942
|
|
|
for row in rows_microgrids: |
2943
|
|
|
microgrid_dict[row[0]] = {"id": row[0], |
2944
|
|
|
"name": row[1], |
2945
|
|
|
"uuid": row[2]} |
2946
|
|
|
# query meter dict |
2947
|
|
|
query = (" SELECT id, name, uuid " |
2948
|
|
|
" FROM tbl_meters ") |
2949
|
|
|
cursor.execute(query) |
2950
|
|
|
rows_meters = cursor.fetchall() |
2951
|
|
|
|
2952
|
|
|
meter_dict = dict() |
2953
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
2954
|
|
|
for row in rows_meters: |
2955
|
|
|
meter_dict[row[0]] = {"id": row[0], |
2956
|
|
|
"name": row[1], |
2957
|
|
|
"uuid": row[2]} |
2958
|
|
|
# query point dict |
2959
|
|
|
query = (" SELECT id, name " |
2960
|
|
|
" FROM tbl_points ") |
2961
|
|
|
cursor.execute(query) |
2962
|
|
|
rows_points = cursor.fetchall() |
2963
|
|
|
|
2964
|
|
|
point_dict = dict() |
2965
|
|
|
if rows_points is not None and len(rows_points) > 0: |
2966
|
|
|
for row in rows_points: |
2967
|
|
|
point_dict[row[0]] = {"id": row[0], |
2968
|
|
|
"name": row[1]} |
2969
|
|
|
|
2970
|
|
|
query = (" SELECT id, name, uuid, microgrid_id, " |
2971
|
|
|
" power_point_id, electricity_meter_id, heat_meter_id, cooling_meter_id, rated_input_power " |
2972
|
|
|
" FROM tbl_microgrids_heatpumps " |
2973
|
|
|
" WHERE id = %s ") |
2974
|
|
|
cursor.execute(query, (hid,)) |
2975
|
|
|
row = cursor.fetchone() |
2976
|
|
|
cursor.close() |
2977
|
|
|
cnx.close() |
2978
|
|
|
|
2979
|
|
|
if row is None: |
2980
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2981
|
|
|
description='API.MICROGRID_HEATPUMP_NOT_FOUND') |
2982
|
|
|
else: |
2983
|
|
|
meta_result = {"id": row[0], |
2984
|
|
|
"name": row[1], |
2985
|
|
|
"uuid": row[2], |
2986
|
|
|
"microgrid": microgrid_dict.get(row[3], None), |
2987
|
|
|
"power_point": point_dict.get(row[4], None), |
2988
|
|
|
"electricity_meter": meter_dict.get(row[5], None), |
2989
|
|
|
"heat_meter": meter_dict.get(row[6], None), |
2990
|
|
|
"cooling_meter": meter_dict.get(row[7], None), |
2991
|
|
|
"rated_input_power": row[8]} |
2992
|
|
|
|
2993
|
|
|
resp.text = json.dumps(meta_result) |
2994
|
|
|
|
2995
|
|
|
@staticmethod |
2996
|
|
|
@user_logger |
2997
|
|
|
def on_delete(req, resp, id_, hid): |
2998
|
|
|
admin_control(req) |
2999
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
3000
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3001
|
|
|
description='API.INVALID_MICROGRID_ID') |
3002
|
|
|
if not hid.isdigit() or int(hid) <= 0: |
3003
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3004
|
|
|
description='API.INVALID_MICROGRID_HEATPUMP_ID') |
3005
|
|
|
|
3006
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
3007
|
|
|
cursor = cnx.cursor() |
3008
|
|
|
|
3009
|
|
|
cursor.execute(" SELECT name " |
3010
|
|
|
" FROM tbl_microgrids " |
3011
|
|
|
" WHERE id = %s ", (id_,)) |
3012
|
|
|
if cursor.fetchone() is None: |
3013
|
|
|
cursor.close() |
3014
|
|
|
cnx.close() |
3015
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3016
|
|
|
description='API.MICROGRID_NOT_FOUND') |
3017
|
|
|
|
3018
|
|
|
cursor.execute(" SELECT name " |
3019
|
|
|
" FROM tbl_microgrids_heatpumps " |
3020
|
|
|
" WHERE id = %s ", (hid,)) |
3021
|
|
|
if cursor.fetchone() is None: |
3022
|
|
|
cursor.close() |
3023
|
|
|
cnx.close() |
3024
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3025
|
|
|
description='API.MICROGRID_HEATPUMP_NOT_FOUND') |
3026
|
|
|
|
3027
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_heatpumps " |
3028
|
|
|
" WHERE id = %s ", (hid,)) |
3029
|
|
|
cnx.commit() |
3030
|
|
|
|
3031
|
|
|
cursor.close() |
3032
|
|
|
cnx.close() |
3033
|
|
|
|
3034
|
|
|
resp.status = falcon.HTTP_204 |
3035
|
|
|
|
3036
|
|
|
@staticmethod |
3037
|
|
|
@user_logger |
3038
|
|
|
def on_put(req, resp, id_, hid): |
3039
|
|
|
"""Handles PUT requests""" |
3040
|
|
|
admin_control(req) |
3041
|
|
|
try: |
3042
|
|
|
raw_json = req.stream.read().decode('utf-8') |
3043
|
|
|
except Exception as ex: |
3044
|
|
|
print(str(ex)) |
3045
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
3046
|
|
|
title='API.BAD_REQUEST', |
3047
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
3048
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
3049
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3050
|
|
|
description='API.INVALID_MICROGRID_ID') |
3051
|
|
|
if not hid.isdigit() or int(hid) <= 0: |
3052
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3053
|
|
|
description='API.INVALID_MICROGRID_HEATPUMP_ID') |
3054
|
|
|
|
3055
|
|
|
new_values = json.loads(raw_json) |
3056
|
|
|
|
3057
|
|
|
if 'name' not in new_values['data'].keys() or \ |
3058
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
3059
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
3060
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3061
|
|
|
description='API.INVALID_MICROGRID_HEATPUMP_NAME') |
3062
|
|
|
name = str.strip(new_values['data']['name']) |
3063
|
|
|
|
3064
|
|
|
if 'power_point_id' not in new_values['data'].keys() or \ |
3065
|
|
|
not isinstance(new_values['data']['power_point_id'], int) or \ |
3066
|
|
|
new_values['data']['power_point_id'] <= 0: |
3067
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3068
|
|
|
description='API.INVALID_POWER_POINT_ID') |
3069
|
|
|
power_point_id = new_values['data']['power_point_id'] |
3070
|
|
|
|
3071
|
|
|
if 'electricity_meter_id' not in new_values['data'].keys() or \ |
3072
|
|
|
not isinstance(new_values['data']['electricity_meter_id'], int) or \ |
3073
|
|
|
new_values['data']['electricity_meter_id'] <= 0: |
3074
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3075
|
|
|
description='API.INVALID_ELECTRICITY_METER_ID') |
3076
|
|
|
electricity_meter_id = new_values['data']['electricity_meter_id'] |
3077
|
|
|
|
3078
|
|
|
if 'heat_meter_id' not in new_values['data'].keys() or \ |
3079
|
|
|
not isinstance(new_values['data']['heat_meter_id'], int) or \ |
3080
|
|
|
new_values['data']['heat_meter_id'] <= 0: |
3081
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3082
|
|
|
description='API.INVALID_HEAT_METER_ID') |
3083
|
|
|
heat_meter_id = new_values['data']['heat_meter_id'] |
3084
|
|
|
|
3085
|
|
|
if 'cooling_meter_id' not in new_values['data'].keys() or \ |
3086
|
|
|
not isinstance(new_values['data']['cooling_meter_id'], int) or \ |
3087
|
|
|
new_values['data']['cooling_meter_id'] <= 0: |
3088
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3089
|
|
|
description='API.INVALID_COOLING_METER_ID') |
3090
|
|
|
cooling_meter_id = new_values['data']['cooling_meter_id'] |
3091
|
|
|
|
3092
|
|
|
if 'rated_input_power' not in new_values['data'].keys() or \ |
3093
|
|
|
not (isinstance(new_values['data']['rated_input_power'], float) or |
3094
|
|
|
isinstance(new_values['data']['rated_input_power'], int)): |
3095
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3096
|
|
|
description='API.INVALID_RATED_INPUT_POWER') |
3097
|
|
|
rated_input_power = float(new_values['data']['rated_input_power']) |
3098
|
|
|
|
3099
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
3100
|
|
|
cursor = cnx.cursor() |
3101
|
|
|
|
3102
|
|
|
cursor.execute(" SELECT name " |
3103
|
|
|
" FROM tbl_microgrids " |
3104
|
|
|
" WHERE id = %s ", (id_,)) |
3105
|
|
|
if cursor.fetchone() is None: |
3106
|
|
|
cursor.close() |
3107
|
|
|
cnx.close() |
3108
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3109
|
|
|
description='API.MICROGRID_NOT_FOUND') |
3110
|
|
|
|
3111
|
|
|
cursor.execute(" SELECT name " |
3112
|
|
|
" FROM tbl_microgrids_heatpumps " |
3113
|
|
|
" WHERE id = %s ", (hid,)) |
3114
|
|
|
if cursor.fetchone() is None: |
3115
|
|
|
cursor.close() |
3116
|
|
|
cnx.close() |
3117
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3118
|
|
|
description='API.MICROGRID_HEATPUMP_NOT_FOUND') |
3119
|
|
|
|
3120
|
|
|
cursor.execute(" SELECT name " |
3121
|
|
|
" FROM tbl_microgrids_heatpumps " |
3122
|
|
|
" WHERE microgrid_id = %s AND name = %s AND id != %s ", |
3123
|
|
|
(id_, name, hid)) |
3124
|
|
|
if cursor.fetchone() is not None: |
3125
|
|
|
cursor.close() |
3126
|
|
|
cnx.close() |
3127
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3128
|
|
|
description='API.MICROGRID_HEATPUMP_NAME_IS_ALREADY_IN_USE') |
3129
|
|
|
|
3130
|
|
|
cursor.execute(" SELECT name " |
3131
|
|
|
" FROM tbl_points " |
3132
|
|
|
" WHERE id = %s ", |
3133
|
|
|
(power_point_id,)) |
3134
|
|
|
if cursor.fetchone() is None: |
3135
|
|
|
cursor.close() |
3136
|
|
|
cnx.close() |
3137
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3138
|
|
|
description='API.POWER_POINT_NOT_FOUND') |
3139
|
|
|
|
3140
|
|
|
cursor.execute(" SELECT name " |
3141
|
|
|
" FROM tbl_meters " |
3142
|
|
|
" WHERE id = %s ", |
3143
|
|
|
(electricity_meter_id,)) |
3144
|
|
|
if cursor.fetchone() is None: |
3145
|
|
|
cursor.close() |
3146
|
|
|
cnx.close() |
3147
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3148
|
|
|
description='API.ELECTRICITY_METER_NOT_FOUND') |
3149
|
|
|
|
3150
|
|
|
cursor.execute(" SELECT name " |
3151
|
|
|
" FROM tbl_meters " |
3152
|
|
|
" WHERE id = %s ", |
3153
|
|
|
(heat_meter_id,)) |
3154
|
|
|
if cursor.fetchone() is None: |
3155
|
|
|
cursor.close() |
3156
|
|
|
cnx.close() |
3157
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3158
|
|
|
description='API.HEAT_METER_NOT_FOUND') |
3159
|
|
|
|
3160
|
|
|
cursor.execute(" SELECT name " |
3161
|
|
|
" FROM tbl_meters " |
3162
|
|
|
" WHERE id = %s ", |
3163
|
|
|
(cooling_meter_id,)) |
3164
|
|
|
if cursor.fetchone() is None: |
3165
|
|
|
cursor.close() |
3166
|
|
|
cnx.close() |
3167
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3168
|
|
|
description='API.COOLING_METER_NOT_FOUND') |
3169
|
|
|
|
3170
|
|
|
update_row = (" UPDATE tbl_microgrids_heatpumps " |
3171
|
|
|
" SET name = %s, microgrid_id = %s, " |
3172
|
|
|
" power_point_id = %s, electricity_meter_id = %s, heat_meter_id = %s, cooling_meter_id = %s, " |
3173
|
|
|
" rated_input_power = %s " |
3174
|
|
|
" WHERE id = %s ") |
3175
|
|
|
cursor.execute(update_row, (name, |
3176
|
|
|
id_, |
3177
|
|
|
power_point_id, |
3178
|
|
|
electricity_meter_id, |
3179
|
|
|
heat_meter_id, |
3180
|
|
|
heat_meter_id, |
3181
|
|
|
rated_input_power, |
3182
|
|
|
hid)) |
3183
|
|
|
cnx.commit() |
3184
|
|
|
|
3185
|
|
|
cursor.close() |
3186
|
|
|
cnx.close() |
3187
|
|
|
|
3188
|
|
|
resp.status = falcon.HTTP_200 |
3189
|
|
|
|
3190
|
|
|
|
3191
|
|
View Code Duplication |
class MicrogridLoadCollection: |
|
|
|
|
3192
|
|
|
def __init__(self): |
3193
|
|
|
"""Initializes MicrogridLoadCollection""" |
3194
|
|
|
pass |
3195
|
|
|
|
3196
|
|
|
@staticmethod |
3197
|
|
|
def on_options(req, resp, id_): |
3198
|
|
|
_ = req |
3199
|
|
|
resp.status = falcon.HTTP_200 |
3200
|
|
|
_ = id_ |
3201
|
|
|
|
3202
|
|
|
@staticmethod |
3203
|
|
|
def on_get(req, resp, id_): |
3204
|
|
|
access_control(req) |
3205
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
3206
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3207
|
|
|
description='API.INVALID_MICROGRID_ID') |
3208
|
|
|
|
3209
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
3210
|
|
|
cursor = cnx.cursor() |
3211
|
|
|
|
3212
|
|
|
cursor.execute(" SELECT name " |
3213
|
|
|
" FROM tbl_microgrids " |
3214
|
|
|
" WHERE id = %s ", (id_,)) |
3215
|
|
|
if cursor.fetchone() is None: |
3216
|
|
|
cursor.close() |
3217
|
|
|
cnx.close() |
3218
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3219
|
|
|
description='API.MICROGRID_NOT_FOUND') |
3220
|
|
|
|
3221
|
|
|
# query meter dict |
3222
|
|
|
query = (" SELECT id, name, uuid " |
3223
|
|
|
" FROM tbl_meters ") |
3224
|
|
|
cursor.execute(query) |
3225
|
|
|
rows_meters = cursor.fetchall() |
3226
|
|
|
|
3227
|
|
|
meter_dict = dict() |
3228
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
3229
|
|
|
for row in rows_meters: |
3230
|
|
|
meter_dict[row[0]] = {"id": row[0], |
3231
|
|
|
"name": row[1], |
3232
|
|
|
"uuid": row[2]} |
3233
|
|
|
# query point dict |
3234
|
|
|
query = (" SELECT id, name " |
3235
|
|
|
" FROM tbl_points ") |
3236
|
|
|
cursor.execute(query) |
3237
|
|
|
rows_points = cursor.fetchall() |
3238
|
|
|
|
3239
|
|
|
point_dict = dict() |
3240
|
|
|
if rows_points is not None and len(rows_points) > 0: |
3241
|
|
|
for row in rows_points: |
3242
|
|
|
point_dict[row[0]] = {"id": row[0], |
3243
|
|
|
"name": row[1]} |
3244
|
|
|
|
3245
|
|
|
query = (" SELECT id, name, uuid, " |
3246
|
|
|
" power_point_id, meter_id, rated_input_power " |
3247
|
|
|
" FROM tbl_microgrids_loads " |
3248
|
|
|
" WHERE microgrid_id = %s " |
3249
|
|
|
" ORDER BY name ") |
3250
|
|
|
cursor.execute(query, (id_,)) |
3251
|
|
|
rows = cursor.fetchall() |
3252
|
|
|
|
3253
|
|
|
result = list() |
3254
|
|
|
if rows is not None and len(rows) > 0: |
3255
|
|
|
for row in rows: |
3256
|
|
|
meta_result = {"id": row[0], |
3257
|
|
|
"name": row[1], |
3258
|
|
|
"uuid": row[2], |
3259
|
|
|
"power_point": point_dict.get(row[3], None), |
3260
|
|
|
"meter": meter_dict.get(row[4], None), |
3261
|
|
|
"rated_input_power": row[5]} |
3262
|
|
|
result.append(meta_result) |
3263
|
|
|
|
3264
|
|
|
resp.text = json.dumps(result) |
3265
|
|
|
|
3266
|
|
|
@staticmethod |
3267
|
|
|
@user_logger |
3268
|
|
|
def on_post(req, resp, id_): |
3269
|
|
|
"""Handles POST requests""" |
3270
|
|
|
admin_control(req) |
3271
|
|
|
try: |
3272
|
|
|
raw_json = req.stream.read().decode('utf-8') |
3273
|
|
|
except Exception as ex: |
3274
|
|
|
print(str(ex)) |
3275
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
3276
|
|
|
title='API.BAD_REQUEST', |
3277
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
3278
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
3279
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3280
|
|
|
description='API.INVALID_MICROGRID_ID') |
3281
|
|
|
|
3282
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
3283
|
|
|
cursor = cnx.cursor() |
3284
|
|
|
|
3285
|
|
|
cursor.execute(" SELECT name " |
3286
|
|
|
" FROM tbl_microgrids " |
3287
|
|
|
" WHERE id = %s ", (id_,)) |
3288
|
|
|
if cursor.fetchone() is None: |
3289
|
|
|
cursor.close() |
3290
|
|
|
cnx.close() |
3291
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3292
|
|
|
description='API.MICROGRID_NOT_FOUND') |
3293
|
|
|
|
3294
|
|
|
new_values = json.loads(raw_json) |
3295
|
|
|
|
3296
|
|
|
if 'name' not in new_values['data'].keys() or \ |
3297
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
3298
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
3299
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3300
|
|
|
description='API.INVALID_MICROGRID_LOAD_NAME') |
3301
|
|
|
name = str.strip(new_values['data']['name']) |
3302
|
|
|
|
3303
|
|
|
if 'power_point_id' not in new_values['data'].keys() or \ |
3304
|
|
|
not isinstance(new_values['data']['power_point_id'], int) or \ |
3305
|
|
|
new_values['data']['power_point_id'] <= 0: |
3306
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3307
|
|
|
description='API.INVALID_POWER_POINT_ID') |
3308
|
|
|
power_point_id = new_values['data']['power_point_id'] |
3309
|
|
|
|
3310
|
|
|
if 'meter_id' not in new_values['data'].keys() or \ |
3311
|
|
|
not isinstance(new_values['data']['meter_id'], int) or \ |
3312
|
|
|
new_values['data']['meter_id'] <= 0: |
3313
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3314
|
|
|
description='API.INVALID_METER_ID') |
3315
|
|
|
meter_id = new_values['data']['meter_id'] |
3316
|
|
|
|
3317
|
|
|
if 'rated_input_power' not in new_values['data'].keys() or \ |
3318
|
|
|
not (isinstance(new_values['data']['rated_input_power'], float) or |
3319
|
|
|
isinstance(new_values['data']['rated_input_power'], int)): |
3320
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3321
|
|
|
description='API.INVALID_RATED_INPUT_POWER') |
3322
|
|
|
rated_input_power = float(new_values['data']['rated_input_power']) |
3323
|
|
|
|
3324
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
3325
|
|
|
cursor = cnx.cursor() |
3326
|
|
|
|
3327
|
|
|
cursor.execute(" SELECT name " |
3328
|
|
|
" FROM tbl_microgrids " |
3329
|
|
|
" WHERE id = %s ", |
3330
|
|
|
(id_,)) |
3331
|
|
|
if cursor.fetchone() is None: |
3332
|
|
|
cursor.close() |
3333
|
|
|
cnx.close() |
3334
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3335
|
|
|
description='API.MICROGRID_NOT_FOUND') |
3336
|
|
|
|
3337
|
|
|
cursor.execute(" SELECT name " |
3338
|
|
|
" FROM tbl_microgrids_loads " |
3339
|
|
|
" WHERE microgrid_id = %s AND name = %s ", |
3340
|
|
|
(id_, name,)) |
3341
|
|
|
if cursor.fetchone() is not None: |
3342
|
|
|
cursor.close() |
3343
|
|
|
cnx.close() |
3344
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3345
|
|
|
description='API.MICROGRID_LOAD_NAME_IS_ALREADY_IN_USE') |
3346
|
|
|
|
3347
|
|
|
cursor.execute(" SELECT name " |
3348
|
|
|
" FROM tbl_points " |
3349
|
|
|
" WHERE id = %s ", |
3350
|
|
|
(power_point_id,)) |
3351
|
|
|
if cursor.fetchone() is None: |
3352
|
|
|
cursor.close() |
3353
|
|
|
cnx.close() |
3354
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3355
|
|
|
description='API.POWER_POINT_NOT_FOUND') |
3356
|
|
|
|
3357
|
|
|
cursor.execute(" SELECT name " |
3358
|
|
|
" FROM tbl_meters " |
3359
|
|
|
" WHERE id = %s ", |
3360
|
|
|
(meter_id,)) |
3361
|
|
|
if cursor.fetchone() is None: |
3362
|
|
|
cursor.close() |
3363
|
|
|
cnx.close() |
3364
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3365
|
|
|
description='API.METER_NOT_FOUND') |
3366
|
|
|
|
3367
|
|
|
add_values = (" INSERT INTO tbl_microgrids_loads " |
3368
|
|
|
" (name, uuid, microgrid_id, power_point_id, meter_id, rated_input_power) " |
3369
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s) ") |
3370
|
|
|
cursor.execute(add_values, (name, |
3371
|
|
|
str(uuid.uuid4()), |
3372
|
|
|
id_, |
3373
|
|
|
power_point_id, |
3374
|
|
|
meter_id, |
3375
|
|
|
rated_input_power)) |
3376
|
|
|
new_id = cursor.lastrowid |
3377
|
|
|
cnx.commit() |
3378
|
|
|
cursor.close() |
3379
|
|
|
cnx.close() |
3380
|
|
|
|
3381
|
|
|
resp.status = falcon.HTTP_201 |
3382
|
|
|
resp.location = '/microgrids/' + str(id_) + '/loads/' + str(new_id) |
3383
|
|
|
|
3384
|
|
|
|
3385
|
|
|
class MicrogridLoadItem: |
3386
|
|
|
def __init__(self): |
3387
|
|
|
"""Initializes MicrogridLoadItem""" |
3388
|
|
|
pass |
3389
|
|
|
|
3390
|
|
|
@staticmethod |
3391
|
|
|
def on_options(req, resp, id_, lid): |
3392
|
|
|
_ = req |
3393
|
|
|
resp.status = falcon.HTTP_200 |
3394
|
|
|
_ = id_ |
3395
|
|
|
|
3396
|
|
|
@staticmethod |
3397
|
|
|
def on_get(req, resp, id_, lid): |
3398
|
|
|
access_control(req) |
3399
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
3400
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3401
|
|
|
description='API.INVALID_MICROGRID_ID') |
3402
|
|
|
if not lid.isdigit() or int(lid) <= 0: |
3403
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3404
|
|
|
description='API.INVALID_MICROGRID_LOAD_ID') |
3405
|
|
|
|
3406
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
3407
|
|
|
cursor = cnx.cursor() |
3408
|
|
|
|
3409
|
|
|
cursor.execute(" SELECT name " |
3410
|
|
|
" FROM tbl_microgrids " |
3411
|
|
|
" WHERE id = %s ", (id_,)) |
3412
|
|
|
if cursor.fetchone() is None: |
3413
|
|
|
cursor.close() |
3414
|
|
|
cnx.close() |
3415
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3416
|
|
|
description='API.MICROGRID_NOT_FOUND') |
3417
|
|
|
|
3418
|
|
|
# query microgrid dict |
3419
|
|
|
query = (" SELECT id, name, uuid " |
3420
|
|
|
" FROM tbl_microgrids ") |
3421
|
|
|
cursor.execute(query) |
3422
|
|
|
rows_microgrids = cursor.fetchall() |
3423
|
|
|
|
3424
|
|
|
microgrid_dict = dict() |
3425
|
|
|
if rows_microgrids is not None and len(rows_microgrids) > 0: |
3426
|
|
|
for row in rows_microgrids: |
3427
|
|
|
microgrid_dict[row[0]] = {"id": row[0], |
3428
|
|
|
"name": row[1], |
3429
|
|
|
"uuid": row[2]} |
3430
|
|
|
# query meter dict |
3431
|
|
|
query = (" SELECT id, name, uuid " |
3432
|
|
|
" FROM tbl_meters ") |
3433
|
|
|
cursor.execute(query) |
3434
|
|
|
rows_meters = cursor.fetchall() |
3435
|
|
|
|
3436
|
|
|
meter_dict = dict() |
3437
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
3438
|
|
|
for row in rows_meters: |
3439
|
|
|
meter_dict[row[0]] = {"id": row[0], |
3440
|
|
|
"name": row[1], |
3441
|
|
|
"uuid": row[2]} |
3442
|
|
|
# query point dict |
3443
|
|
|
query = (" SELECT id, name " |
3444
|
|
|
" FROM tbl_points ") |
3445
|
|
|
cursor.execute(query) |
3446
|
|
|
rows_points = cursor.fetchall() |
3447
|
|
|
|
3448
|
|
|
point_dict = dict() |
3449
|
|
|
if rows_points is not None and len(rows_points) > 0: |
3450
|
|
|
for row in rows_points: |
3451
|
|
|
point_dict[row[0]] = {"id": row[0], |
3452
|
|
|
"name": row[1]} |
3453
|
|
|
|
3454
|
|
|
query = (" SELECT id, name, uuid, microgrid_id, power_point_id, meter_id, rated_input_power " |
3455
|
|
|
" FROM tbl_microgrids_loads " |
3456
|
|
|
" WHERE id = %s ") |
3457
|
|
|
cursor.execute(query, (lid,)) |
3458
|
|
|
row = cursor.fetchone() |
3459
|
|
|
cursor.close() |
3460
|
|
|
cnx.close() |
3461
|
|
|
|
3462
|
|
|
if row is None: |
3463
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3464
|
|
|
description='API.MICROGRID_LOAD_NOT_FOUND') |
3465
|
|
|
else: |
3466
|
|
|
meta_result = {"id": row[0], |
3467
|
|
|
"name": row[1], |
3468
|
|
|
"uuid": row[2], |
3469
|
|
|
"microgrid": microgrid_dict.get(row[3], None), |
3470
|
|
|
"power_point": point_dict.get(row[4], None), |
3471
|
|
|
"meter": meter_dict.get(row[5], None), |
3472
|
|
|
"rated_input_power": row[6]} |
3473
|
|
|
|
3474
|
|
|
resp.text = json.dumps(meta_result) |
3475
|
|
|
|
3476
|
|
|
@staticmethod |
3477
|
|
|
@user_logger |
3478
|
|
|
def on_delete(req, resp, id_, lid): |
3479
|
|
|
admin_control(req) |
3480
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
3481
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3482
|
|
|
description='API.INVALID_MICROGRID_ID') |
3483
|
|
|
if not lid.isdigit() or int(lid) <= 0: |
3484
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3485
|
|
|
description='API.INVALID_MICROGRID_LOAD_ID') |
3486
|
|
|
|
3487
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
3488
|
|
|
cursor = cnx.cursor() |
3489
|
|
|
|
3490
|
|
|
cursor.execute(" SELECT name " |
3491
|
|
|
" FROM tbl_microgrids " |
3492
|
|
|
" WHERE id = %s ", (id_,)) |
3493
|
|
|
if cursor.fetchone() is None: |
3494
|
|
|
cursor.close() |
3495
|
|
|
cnx.close() |
3496
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3497
|
|
|
description='API.MICROGRID_NOT_FOUND') |
3498
|
|
|
|
3499
|
|
|
cursor.execute(" SELECT name " |
3500
|
|
|
" FROM tbl_microgrids_loads " |
3501
|
|
|
" WHERE id = %s ", (lid,)) |
3502
|
|
|
if cursor.fetchone() is None: |
3503
|
|
|
cursor.close() |
3504
|
|
|
cnx.close() |
3505
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3506
|
|
|
description='API.MICROGRID_LOAD_NOT_FOUND') |
3507
|
|
|
|
3508
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_loads " |
3509
|
|
|
" WHERE id = %s ", (lid,)) |
3510
|
|
|
cnx.commit() |
3511
|
|
|
|
3512
|
|
|
cursor.close() |
3513
|
|
|
cnx.close() |
3514
|
|
|
|
3515
|
|
|
resp.status = falcon.HTTP_204 |
3516
|
|
|
|
3517
|
|
|
@staticmethod |
3518
|
|
|
@user_logger |
3519
|
|
|
def on_put(req, resp, id_, lid): |
3520
|
|
|
"""Handles PUT requests""" |
3521
|
|
|
admin_control(req) |
3522
|
|
|
try: |
3523
|
|
|
raw_json = req.stream.read().decode('utf-8') |
3524
|
|
|
except Exception as ex: |
3525
|
|
|
print(str(ex)) |
3526
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
3527
|
|
|
title='API.BAD_REQUEST', |
3528
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
3529
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
3530
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3531
|
|
|
description='API.INVALID_MICROGRID_ID') |
3532
|
|
|
if not lid.isdigit() or int(lid) <= 0: |
3533
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3534
|
|
|
description='API.INVALID_MICROGRID_LOAD_ID') |
3535
|
|
|
|
3536
|
|
|
new_values = json.loads(raw_json) |
3537
|
|
|
|
3538
|
|
|
if 'name' not in new_values['data'].keys() or \ |
3539
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
3540
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
3541
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3542
|
|
|
description='API.INVALID_MICROGRID_LOAD_NAME') |
3543
|
|
|
name = str.strip(new_values['data']['name']) |
3544
|
|
|
|
3545
|
|
|
if 'power_point_id' not in new_values['data'].keys() or \ |
3546
|
|
|
not isinstance(new_values['data']['power_point_id'], int) or \ |
3547
|
|
|
new_values['data']['power_point_id'] <= 0: |
3548
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3549
|
|
|
description='API.INVALID_POWER_POINT_ID') |
3550
|
|
|
power_point_id = new_values['data']['power_point_id'] |
3551
|
|
|
|
3552
|
|
|
if 'meter_id' not in new_values['data'].keys() or \ |
3553
|
|
|
not isinstance(new_values['data']['meter_id'], int) or \ |
3554
|
|
|
new_values['data']['meter_id'] <= 0: |
3555
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3556
|
|
|
description='API.INVALID_METER_ID') |
3557
|
|
|
meter_id = new_values['data']['meter_id'] |
3558
|
|
|
|
3559
|
|
|
if 'rated_input_power' not in new_values['data'].keys() or \ |
3560
|
|
|
not (isinstance(new_values['data']['rated_input_power'], float) or |
3561
|
|
|
isinstance(new_values['data']['rated_input_power'], int)): |
3562
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3563
|
|
|
description='API.INVALID_RATED_INPUT_POWER') |
3564
|
|
|
rated_input_power = float(new_values['data']['rated_input_power']) |
3565
|
|
|
|
3566
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
3567
|
|
|
cursor = cnx.cursor() |
3568
|
|
|
|
3569
|
|
|
cursor.execute(" SELECT name " |
3570
|
|
|
" FROM tbl_microgrids " |
3571
|
|
|
" WHERE id = %s ", (id_,)) |
3572
|
|
|
if cursor.fetchone() is None: |
3573
|
|
|
cursor.close() |
3574
|
|
|
cnx.close() |
3575
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3576
|
|
|
description='API.MICROGRID_NOT_FOUND') |
3577
|
|
|
|
3578
|
|
|
cursor.execute(" SELECT name " |
3579
|
|
|
" FROM tbl_microgrids_loads " |
3580
|
|
|
" WHERE id = %s ", (lid,)) |
3581
|
|
|
if cursor.fetchone() is None: |
3582
|
|
|
cursor.close() |
3583
|
|
|
cnx.close() |
3584
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3585
|
|
|
description='API.MICROGRID_LOAD_NOT_FOUND') |
3586
|
|
|
|
3587
|
|
|
cursor.execute(" SELECT name " |
3588
|
|
|
" FROM tbl_microgrids_loads " |
3589
|
|
|
" WHERE microgrid_id = %s AND name = %s AND id != %s ", |
3590
|
|
|
(id_, name, lid)) |
3591
|
|
|
if cursor.fetchone() is not None: |
3592
|
|
|
cursor.close() |
3593
|
|
|
cnx.close() |
3594
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3595
|
|
|
description='API.MICROGRID_LOAD_NAME_IS_ALREADY_IN_USE') |
3596
|
|
|
|
3597
|
|
|
cursor.execute(" SELECT name " |
3598
|
|
|
" FROM tbl_points " |
3599
|
|
|
" WHERE id = %s ", |
3600
|
|
|
(power_point_id,)) |
3601
|
|
|
if cursor.fetchone() is None: |
3602
|
|
|
cursor.close() |
3603
|
|
|
cnx.close() |
3604
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3605
|
|
|
description='API.POWER_POINT_NOT_FOUND') |
3606
|
|
|
|
3607
|
|
|
cursor.execute(" SELECT name " |
3608
|
|
|
" FROM tbl_meters " |
3609
|
|
|
" WHERE id = %s ", |
3610
|
|
|
(meter_id,)) |
3611
|
|
|
if cursor.fetchone() is None: |
3612
|
|
|
cursor.close() |
3613
|
|
|
cnx.close() |
3614
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3615
|
|
|
description='API.METER_NOT_FOUND') |
3616
|
|
|
|
3617
|
|
|
update_row = (" UPDATE tbl_microgrids_loads " |
3618
|
|
|
" SET name = %s, microgrid_id = %s, power_point_id = %s, meter_id = %s, rated_input_power = %s " |
3619
|
|
|
" WHERE id = %s ") |
3620
|
|
|
cursor.execute(update_row, (name, |
3621
|
|
|
id_, |
3622
|
|
|
power_point_id, |
3623
|
|
|
meter_id, |
3624
|
|
|
rated_input_power, |
3625
|
|
|
lid)) |
3626
|
|
|
cnx.commit() |
3627
|
|
|
|
3628
|
|
|
cursor.close() |
3629
|
|
|
cnx.close() |
3630
|
|
|
|
3631
|
|
|
resp.status = falcon.HTTP_200 |
3632
|
|
|
|
3633
|
|
|
|
3634
|
|
View Code Duplication |
class MicrogridPhotovoltaicCollection: |
|
|
|
|
3635
|
|
|
def __init__(self): |
3636
|
|
|
"""Initializes MicrogridPhotovoltaicCollection""" |
3637
|
|
|
pass |
3638
|
|
|
|
3639
|
|
|
@staticmethod |
3640
|
|
|
def on_options(req, resp, id_): |
3641
|
|
|
_ = req |
3642
|
|
|
resp.status = falcon.HTTP_200 |
3643
|
|
|
_ = id_ |
3644
|
|
|
|
3645
|
|
|
@staticmethod |
3646
|
|
|
def on_get(req, resp, id_): |
3647
|
|
|
access_control(req) |
3648
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
3649
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3650
|
|
|
description='API.INVALID_MICROGRID_ID') |
3651
|
|
|
|
3652
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
3653
|
|
|
cursor = cnx.cursor() |
3654
|
|
|
|
3655
|
|
|
cursor.execute(" SELECT name " |
3656
|
|
|
" FROM tbl_microgrids " |
3657
|
|
|
" WHERE id = %s ", (id_,)) |
3658
|
|
|
if cursor.fetchone() is None: |
3659
|
|
|
cursor.close() |
3660
|
|
|
cnx.close() |
3661
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3662
|
|
|
description='API.MICROGRID_NOT_FOUND') |
3663
|
|
|
|
3664
|
|
|
# query meter dict |
3665
|
|
|
query = (" SELECT id, name, uuid " |
3666
|
|
|
" FROM tbl_meters ") |
3667
|
|
|
cursor.execute(query) |
3668
|
|
|
rows_meters = cursor.fetchall() |
3669
|
|
|
|
3670
|
|
|
meter_dict = dict() |
3671
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
3672
|
|
|
for row in rows_meters: |
3673
|
|
|
meter_dict[row[0]] = {"id": row[0], |
3674
|
|
|
"name": row[1], |
3675
|
|
|
"uuid": row[2]} |
3676
|
|
|
# query point dict |
3677
|
|
|
query = (" SELECT id, name " |
3678
|
|
|
" FROM tbl_points ") |
3679
|
|
|
cursor.execute(query) |
3680
|
|
|
rows_points = cursor.fetchall() |
3681
|
|
|
|
3682
|
|
|
point_dict = dict() |
3683
|
|
|
if rows_points is not None and len(rows_points) > 0: |
3684
|
|
|
for row in rows_points: |
3685
|
|
|
point_dict[row[0]] = {"id": row[0], |
3686
|
|
|
"name": row[1]} |
3687
|
|
|
|
3688
|
|
|
query = (" SELECT id, name, uuid, " |
3689
|
|
|
" power_point_id, meter_id, rated_power " |
3690
|
|
|
" FROM tbl_microgrids_photovoltaics " |
3691
|
|
|
" WHERE microgrid_id = %s " |
3692
|
|
|
" ORDER BY name ") |
3693
|
|
|
cursor.execute(query, (id_,)) |
3694
|
|
|
rows = cursor.fetchall() |
3695
|
|
|
|
3696
|
|
|
result = list() |
3697
|
|
|
if rows is not None and len(rows) > 0: |
3698
|
|
|
for row in rows: |
3699
|
|
|
meta_result = {"id": row[0], |
3700
|
|
|
"name": row[1], |
3701
|
|
|
"uuid": row[2], |
3702
|
|
|
"power_point": point_dict.get(row[3], None), |
3703
|
|
|
"meter": meter_dict.get(row[4], None), |
3704
|
|
|
"rated_power": row[5], |
3705
|
|
|
} |
3706
|
|
|
result.append(meta_result) |
3707
|
|
|
|
3708
|
|
|
resp.text = json.dumps(result) |
3709
|
|
|
|
3710
|
|
|
@staticmethod |
3711
|
|
|
@user_logger |
3712
|
|
|
def on_post(req, resp, id_): |
3713
|
|
|
"""Handles POST requests""" |
3714
|
|
|
admin_control(req) |
3715
|
|
|
try: |
3716
|
|
|
raw_json = req.stream.read().decode('utf-8') |
3717
|
|
|
except Exception as ex: |
3718
|
|
|
print(str(ex)) |
3719
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
3720
|
|
|
title='API.BAD_REQUEST', |
3721
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
3722
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
3723
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3724
|
|
|
description='API.INVALID_MICROGRID_ID') |
3725
|
|
|
|
3726
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
3727
|
|
|
cursor = cnx.cursor() |
3728
|
|
|
|
3729
|
|
|
cursor.execute(" SELECT name " |
3730
|
|
|
" FROM tbl_microgrids " |
3731
|
|
|
" WHERE id = %s ", (id_,)) |
3732
|
|
|
if cursor.fetchone() is None: |
3733
|
|
|
cursor.close() |
3734
|
|
|
cnx.close() |
3735
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3736
|
|
|
description='API.MICROGRID_NOT_FOUND') |
3737
|
|
|
|
3738
|
|
|
new_values = json.loads(raw_json) |
3739
|
|
|
|
3740
|
|
|
if 'name' not in new_values['data'].keys() or \ |
3741
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
3742
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
3743
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3744
|
|
|
description='API.INVALID_MICROGRID_PHOTOVOLTAIC_NAME') |
3745
|
|
|
name = str.strip(new_values['data']['name']) |
3746
|
|
|
|
3747
|
|
|
if 'power_point_id' not in new_values['data'].keys() or \ |
3748
|
|
|
not isinstance(new_values['data']['power_point_id'], int) or \ |
3749
|
|
|
new_values['data']['power_point_id'] <= 0: |
3750
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3751
|
|
|
description='API.INVALID_POWER_POINT_ID') |
3752
|
|
|
power_point_id = new_values['data']['power_point_id'] |
3753
|
|
|
|
3754
|
|
|
if 'meter_id' not in new_values['data'].keys() or \ |
3755
|
|
|
not isinstance(new_values['data']['meter_id'], int) or \ |
3756
|
|
|
new_values['data']['meter_id'] <= 0: |
3757
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3758
|
|
|
description='API.INVALID_METER_ID') |
3759
|
|
|
meter_id = new_values['data']['meter_id'] |
3760
|
|
|
|
3761
|
|
|
if 'rated_power' not in new_values['data'].keys() or \ |
3762
|
|
|
not (isinstance(new_values['data']['rated_power'], float) or |
3763
|
|
|
isinstance(new_values['data']['rated_power'], int)): |
3764
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3765
|
|
|
description='API.INVALID_RATED_POWER') |
3766
|
|
|
rated_power = float(new_values['data']['rated_power']) |
3767
|
|
|
|
3768
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
3769
|
|
|
cursor = cnx.cursor() |
3770
|
|
|
|
3771
|
|
|
cursor.execute(" SELECT name " |
3772
|
|
|
" FROM tbl_microgrids " |
3773
|
|
|
" WHERE id = %s ", |
3774
|
|
|
(id_,)) |
3775
|
|
|
if cursor.fetchone() is None: |
3776
|
|
|
cursor.close() |
3777
|
|
|
cnx.close() |
3778
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3779
|
|
|
description='API.MICROGRID_NOT_FOUND') |
3780
|
|
|
|
3781
|
|
|
cursor.execute(" SELECT name " |
3782
|
|
|
" FROM tbl_microgrids_photovoltaics " |
3783
|
|
|
" WHERE microgrid_id = %s AND name = %s ", |
3784
|
|
|
(id_, name,)) |
3785
|
|
|
if cursor.fetchone() is not None: |
3786
|
|
|
cursor.close() |
3787
|
|
|
cnx.close() |
3788
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3789
|
|
|
description='API.MICROGRID_PHOTOVOLTAIC_NAME_IS_ALREADY_IN_USE') |
3790
|
|
|
|
3791
|
|
|
cursor.execute(" SELECT name " |
3792
|
|
|
" FROM tbl_points " |
3793
|
|
|
" WHERE id = %s ", |
3794
|
|
|
(power_point_id,)) |
3795
|
|
|
if cursor.fetchone() is None: |
3796
|
|
|
cursor.close() |
3797
|
|
|
cnx.close() |
3798
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3799
|
|
|
description='API.POWER_POINT_NOT_FOUND') |
3800
|
|
|
|
3801
|
|
|
cursor.execute(" SELECT name " |
3802
|
|
|
" FROM tbl_meters " |
3803
|
|
|
" WHERE id = %s ", |
3804
|
|
|
(meter_id,)) |
3805
|
|
|
if cursor.fetchone() is None: |
3806
|
|
|
cursor.close() |
3807
|
|
|
cnx.close() |
3808
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3809
|
|
|
description='API.METER_NOT_FOUND') |
3810
|
|
|
|
3811
|
|
|
add_values = (" INSERT INTO tbl_microgrids_photovoltaics " |
3812
|
|
|
" (name, uuid, microgrid_id, power_point_id, meter_id, rated_power) " |
3813
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s) ") |
3814
|
|
|
cursor.execute(add_values, (name, |
3815
|
|
|
str(uuid.uuid4()), |
3816
|
|
|
id_, |
3817
|
|
|
power_point_id, |
3818
|
|
|
meter_id, |
3819
|
|
|
rated_power)) |
3820
|
|
|
new_id = cursor.lastrowid |
3821
|
|
|
cnx.commit() |
3822
|
|
|
cursor.close() |
3823
|
|
|
cnx.close() |
3824
|
|
|
|
3825
|
|
|
resp.status = falcon.HTTP_201 |
3826
|
|
|
resp.location = '/microgrids' + str(id_) + '/photovoltaics/' + str(new_id) |
3827
|
|
|
|
3828
|
|
|
|
3829
|
|
|
class MicrogridPhotovoltaicItem: |
3830
|
|
|
def __init__(self): |
3831
|
|
|
"""Initializes MicrogridPhotovoltaicItem""" |
3832
|
|
|
pass |
3833
|
|
|
|
3834
|
|
|
@staticmethod |
3835
|
|
|
def on_options(req, resp, id_, pid): |
3836
|
|
|
_ = req |
3837
|
|
|
resp.status = falcon.HTTP_200 |
3838
|
|
|
_ = id_ |
3839
|
|
|
|
3840
|
|
|
@staticmethod |
3841
|
|
|
def on_get(req, resp, id_, pid): |
3842
|
|
|
access_control(req) |
3843
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
3844
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3845
|
|
|
description='API.INVALID_MICROGRID_ID') |
3846
|
|
|
if not pid.isdigit() or int(pid) <= 0: |
3847
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3848
|
|
|
description='API.INVALID_MICROGRID_PHOTOVOLTAIC_ID') |
3849
|
|
|
|
3850
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
3851
|
|
|
cursor = cnx.cursor() |
3852
|
|
|
|
3853
|
|
|
cursor.execute(" SELECT name " |
3854
|
|
|
" FROM tbl_microgrids " |
3855
|
|
|
" WHERE id = %s ", (id_,)) |
3856
|
|
|
if cursor.fetchone() is None: |
3857
|
|
|
cursor.close() |
3858
|
|
|
cnx.close() |
3859
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3860
|
|
|
description='API.MICROGRID_NOT_FOUND') |
3861
|
|
|
|
3862
|
|
|
# query microgrid dict |
3863
|
|
|
query = (" SELECT id, name, uuid " |
3864
|
|
|
" FROM tbl_microgrids ") |
3865
|
|
|
cursor.execute(query) |
3866
|
|
|
rows_microgrids = cursor.fetchall() |
3867
|
|
|
|
3868
|
|
|
microgrid_dict = dict() |
3869
|
|
|
if rows_microgrids is not None and len(rows_microgrids) > 0: |
3870
|
|
|
for row in rows_microgrids: |
3871
|
|
|
microgrid_dict[row[0]] = {"id": row[0], |
3872
|
|
|
"name": row[1], |
3873
|
|
|
"uuid": row[2]} |
3874
|
|
|
# query meter dict |
3875
|
|
|
query = (" SELECT id, name, uuid " |
3876
|
|
|
" FROM tbl_meters ") |
3877
|
|
|
cursor.execute(query) |
3878
|
|
|
rows_meters = cursor.fetchall() |
3879
|
|
|
|
3880
|
|
|
meter_dict = dict() |
3881
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
3882
|
|
|
for row in rows_meters: |
3883
|
|
|
meter_dict[row[0]] = {"id": row[0], |
3884
|
|
|
"name": row[1], |
3885
|
|
|
"uuid": row[2]} |
3886
|
|
|
# query point dict |
3887
|
|
|
query = (" SELECT id, name " |
3888
|
|
|
" FROM tbl_points ") |
3889
|
|
|
cursor.execute(query) |
3890
|
|
|
rows_points = cursor.fetchall() |
3891
|
|
|
|
3892
|
|
|
point_dict = dict() |
3893
|
|
|
if rows_points is not None and len(rows_points) > 0: |
3894
|
|
|
for row in rows_points: |
3895
|
|
|
point_dict[row[0]] = {"id": row[0], |
3896
|
|
|
"name": row[1]} |
3897
|
|
|
|
3898
|
|
|
query = (" SELECT id, name, uuid, microgrid_id, power_point_id, meter_id, rated_power " |
3899
|
|
|
" FROM tbl_microgrids_photovoltaics " |
3900
|
|
|
" WHERE id = %s ") |
3901
|
|
|
cursor.execute(query, (pid,)) |
3902
|
|
|
row = cursor.fetchone() |
3903
|
|
|
cursor.close() |
3904
|
|
|
cnx.close() |
3905
|
|
|
|
3906
|
|
|
if row is None: |
3907
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3908
|
|
|
description='API.MICROGRID_PHOTOVOLTAIC_NOT_FOUND') |
3909
|
|
|
else: |
3910
|
|
|
meta_result = {"id": row[0], |
3911
|
|
|
"name": row[1], |
3912
|
|
|
"uuid": row[2], |
3913
|
|
|
"microgrid": microgrid_dict.get(row[3], None), |
3914
|
|
|
"power_point": point_dict.get(row[4], None), |
3915
|
|
|
"meter": meter_dict.get(row[5], None), |
3916
|
|
|
"rated_power": row[6]} |
3917
|
|
|
|
3918
|
|
|
resp.text = json.dumps(meta_result) |
3919
|
|
|
|
3920
|
|
|
@staticmethod |
3921
|
|
|
@user_logger |
3922
|
|
|
def on_delete(req, resp, id_, pid): |
3923
|
|
|
admin_control(req) |
3924
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
3925
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3926
|
|
|
description='API.INVALID_MICROGRID_ID') |
3927
|
|
|
if not pid.isdigit() or int(pid) <= 0: |
3928
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3929
|
|
|
description='API.INVALID_MICROGRID_PHOTOVOLTAIC_ID') |
3930
|
|
|
|
3931
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
3932
|
|
|
cursor = cnx.cursor() |
3933
|
|
|
|
3934
|
|
|
cursor.execute(" SELECT name " |
3935
|
|
|
" FROM tbl_microgrids " |
3936
|
|
|
" WHERE id = %s ", (id_,)) |
3937
|
|
|
if cursor.fetchone() is None: |
3938
|
|
|
cursor.close() |
3939
|
|
|
cnx.close() |
3940
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3941
|
|
|
description='API.MICROGRID_NOT_FOUND') |
3942
|
|
|
|
3943
|
|
|
cursor.execute(" SELECT name " |
3944
|
|
|
" FROM tbl_microgrids_photovoltaics " |
3945
|
|
|
" WHERE id = %s ", (pid,)) |
3946
|
|
|
if cursor.fetchone() is None: |
3947
|
|
|
cursor.close() |
3948
|
|
|
cnx.close() |
3949
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
3950
|
|
|
description='API.MICROGRID_PHOTOVOLTAIC_NOT_FOUND') |
3951
|
|
|
|
3952
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_photovoltaics " |
3953
|
|
|
" WHERE id = %s ", (pid,)) |
3954
|
|
|
cnx.commit() |
3955
|
|
|
|
3956
|
|
|
cursor.close() |
3957
|
|
|
cnx.close() |
3958
|
|
|
|
3959
|
|
|
resp.status = falcon.HTTP_204 |
3960
|
|
|
|
3961
|
|
|
@staticmethod |
3962
|
|
|
@user_logger |
3963
|
|
|
def on_put(req, resp, id_, pid): |
3964
|
|
|
"""Handles PUT requests""" |
3965
|
|
|
admin_control(req) |
3966
|
|
|
try: |
3967
|
|
|
raw_json = req.stream.read().decode('utf-8') |
3968
|
|
|
except Exception as ex: |
3969
|
|
|
print(str(ex)) |
3970
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
3971
|
|
|
title='API.BAD_REQUEST', |
3972
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
3973
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
3974
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3975
|
|
|
description='API.INVALID_MICROGRID_ID') |
3976
|
|
|
if not pid.isdigit() or int(pid) <= 0: |
3977
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3978
|
|
|
description='API.INVALID_MICROGRID_PHOTOVOLTAIC_ID') |
3979
|
|
|
|
3980
|
|
|
new_values = json.loads(raw_json) |
3981
|
|
|
|
3982
|
|
|
if 'name' not in new_values['data'].keys() or \ |
3983
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
3984
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
3985
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3986
|
|
|
description='API.INVALID_MICROGRID_PHOTOVOLTAIC_NAME') |
3987
|
|
|
name = str.strip(new_values['data']['name']) |
3988
|
|
|
|
3989
|
|
|
if 'power_point_id' not in new_values['data'].keys() or \ |
3990
|
|
|
not isinstance(new_values['data']['power_point_id'], int) or \ |
3991
|
|
|
new_values['data']['power_point_id'] <= 0: |
3992
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
3993
|
|
|
description='API.INVALID_POWER_POINT_ID') |
3994
|
|
|
power_point_id = new_values['data']['power_point_id'] |
3995
|
|
|
|
3996
|
|
|
if 'meter_id' not in new_values['data'].keys() or \ |
3997
|
|
|
not isinstance(new_values['data']['meter_id'], int) or \ |
3998
|
|
|
new_values['data']['meter_id'] <= 0: |
3999
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4000
|
|
|
description='API.INVALID_METER_ID') |
4001
|
|
|
meter_id = new_values['data']['meter_id'] |
4002
|
|
|
|
4003
|
|
|
if 'rated_power' not in new_values['data'].keys() or \ |
4004
|
|
|
not (isinstance(new_values['data']['rated_power'], float) or |
4005
|
|
|
isinstance(new_values['data']['rated_power'], int)): |
4006
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4007
|
|
|
description='API.INVALID_RATED_POWER') |
4008
|
|
|
rated_power = float(new_values['data']['rated_power']) |
4009
|
|
|
|
4010
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
4011
|
|
|
cursor = cnx.cursor() |
4012
|
|
|
|
4013
|
|
|
cursor.execute(" SELECT name " |
4014
|
|
|
" FROM tbl_microgrids " |
4015
|
|
|
" WHERE id = %s ", (id_,)) |
4016
|
|
|
if cursor.fetchone() is None: |
4017
|
|
|
cursor.close() |
4018
|
|
|
cnx.close() |
4019
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4020
|
|
|
description='API.MICROGRID_NOT_FOUND') |
4021
|
|
|
|
4022
|
|
|
cursor.execute(" SELECT name " |
4023
|
|
|
" FROM tbl_microgrids_photovoltaics " |
4024
|
|
|
" WHERE id = %s ", (pid,)) |
4025
|
|
|
if cursor.fetchone() is None: |
4026
|
|
|
cursor.close() |
4027
|
|
|
cnx.close() |
4028
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4029
|
|
|
description='API.MICROGRID_PHOTOVOLTAIC_NOT_FOUND') |
4030
|
|
|
|
4031
|
|
|
cursor.execute(" SELECT name " |
4032
|
|
|
" FROM tbl_microgrids_photovoltaics " |
4033
|
|
|
" WHERE microgrid_id = %s AND name = %s AND id != %s ", |
4034
|
|
|
(id_, name, pid)) |
4035
|
|
|
if cursor.fetchone() is not None: |
4036
|
|
|
cursor.close() |
4037
|
|
|
cnx.close() |
4038
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4039
|
|
|
description='API.MICROGRID_PHOTOVOLTAIC_NAME_IS_ALREADY_IN_USE') |
4040
|
|
|
|
4041
|
|
|
cursor.execute(" SELECT name " |
4042
|
|
|
" FROM tbl_points " |
4043
|
|
|
" WHERE id = %s ", |
4044
|
|
|
(power_point_id,)) |
4045
|
|
|
if cursor.fetchone() is None: |
4046
|
|
|
cursor.close() |
4047
|
|
|
cnx.close() |
4048
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4049
|
|
|
description='API.POWER_POINT_NOT_FOUND') |
4050
|
|
|
|
4051
|
|
|
cursor.execute(" SELECT name " |
4052
|
|
|
" FROM tbl_meters " |
4053
|
|
|
" WHERE id = %s ", |
4054
|
|
|
(meter_id,)) |
4055
|
|
|
if cursor.fetchone() is None: |
4056
|
|
|
cursor.close() |
4057
|
|
|
cnx.close() |
4058
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4059
|
|
|
description='API.METER_NOT_FOUND') |
4060
|
|
|
|
4061
|
|
|
update_row = (" UPDATE tbl_microgrids_photovoltaics " |
4062
|
|
|
" SET name = %s, microgrid_id = %s, power_point_id = %s, meter_id = %s, rated_power = %s " |
4063
|
|
|
" WHERE id = %s ") |
4064
|
|
|
cursor.execute(update_row, (name, |
4065
|
|
|
id_, |
4066
|
|
|
power_point_id, |
4067
|
|
|
meter_id, |
4068
|
|
|
rated_power, |
4069
|
|
|
pid)) |
4070
|
|
|
cnx.commit() |
4071
|
|
|
|
4072
|
|
|
cursor.close() |
4073
|
|
|
cnx.close() |
4074
|
|
|
|
4075
|
|
|
resp.status = falcon.HTTP_200 |
4076
|
|
|
|
4077
|
|
|
|
4078
|
|
|
class MicrogridPowerconversionsystemCollection: |
4079
|
|
|
def __init__(self): |
4080
|
|
|
"""Initializes MicrogridPowerconversionsystemCollection""" |
4081
|
|
|
pass |
4082
|
|
|
|
4083
|
|
|
@staticmethod |
4084
|
|
|
def on_options(req, resp, id_): |
4085
|
|
|
_ = req |
4086
|
|
|
resp.status = falcon.HTTP_200 |
4087
|
|
|
_ = id_ |
4088
|
|
|
|
4089
|
|
|
@staticmethod |
4090
|
|
|
def on_get(req, resp, id_): |
4091
|
|
|
access_control(req) |
4092
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
4093
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4094
|
|
|
description='API.INVALID_MICROGRID_ID') |
4095
|
|
|
|
4096
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
4097
|
|
|
cursor = cnx.cursor() |
4098
|
|
|
|
4099
|
|
|
cursor.execute(" SELECT name " |
4100
|
|
|
" FROM tbl_microgrids " |
4101
|
|
|
" WHERE id = %s ", (id_,)) |
4102
|
|
|
if cursor.fetchone() is None: |
4103
|
|
|
cursor.close() |
4104
|
|
|
cnx.close() |
4105
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4106
|
|
|
description='API.MICROGRID_NOT_FOUND') |
4107
|
|
|
|
4108
|
|
|
# query point dict |
4109
|
|
|
query = (" SELECT id, name " |
4110
|
|
|
" FROM tbl_points ") |
4111
|
|
|
cursor.execute(query) |
4112
|
|
|
rows_points = cursor.fetchall() |
4113
|
|
|
|
4114
|
|
|
point_dict = dict() |
4115
|
|
|
if rows_points is not None and len(rows_points) > 0: |
4116
|
|
|
for row in rows_points: |
4117
|
|
|
point_dict[row[0]] = {"id": row[0], |
4118
|
|
|
"name": row[1]} |
4119
|
|
|
# query command dict |
4120
|
|
|
query = (" SELECT id, name " |
4121
|
|
|
" FROM tbl_commands ") |
4122
|
|
|
cursor.execute(query) |
4123
|
|
|
rows_commands = cursor.fetchall() |
4124
|
|
|
|
4125
|
|
|
command_dict = dict() |
4126
|
|
|
if rows_commands is not None and len(rows_commands) > 0: |
4127
|
|
|
for row in rows_commands: |
4128
|
|
|
command_dict[row[0]] = {"id": row[0], |
4129
|
|
|
"name": row[1]} |
4130
|
|
|
|
4131
|
|
|
query = (" SELECT id, name, uuid, run_state_point_id, rated_output_power, " |
4132
|
|
|
" today_charge_energy_point_id, today_discharge_energy_point_id, " |
4133
|
|
|
" total_charge_energy_point_id, total_discharge_energy_point_id " |
4134
|
|
|
" FROM tbl_microgrids_power_conversion_systems " |
4135
|
|
|
" WHERE microgrid_id = %s " |
4136
|
|
|
" ORDER BY name ") |
4137
|
|
|
cursor.execute(query, (id_,)) |
4138
|
|
|
rows = cursor.fetchall() |
4139
|
|
|
|
4140
|
|
|
result = list() |
4141
|
|
|
if rows is not None and len(rows) > 0: |
4142
|
|
|
for row in rows: |
4143
|
|
|
meta_result = {"id": row[0], |
4144
|
|
|
"name": row[1], |
4145
|
|
|
"uuid": row[2], |
4146
|
|
|
"run_state_point": point_dict.get(row[3]), |
4147
|
|
|
"rated_output_power": row[4], |
4148
|
|
|
"today_charge_energy_point": point_dict.get(row[5]), |
4149
|
|
|
"today_discharge_energy_point": point_dict.get(row[6]), |
4150
|
|
|
"total_charge_energy_point": point_dict.get(row[7]), |
4151
|
|
|
"total_discharge_energy_point": point_dict.get(row[8])} |
4152
|
|
|
result.append(meta_result) |
4153
|
|
|
|
4154
|
|
|
resp.text = json.dumps(result) |
4155
|
|
|
|
4156
|
|
|
@staticmethod |
4157
|
|
|
@user_logger |
4158
|
|
|
def on_post(req, resp, id_): |
4159
|
|
|
"""Handles POST requests""" |
4160
|
|
|
admin_control(req) |
4161
|
|
|
try: |
4162
|
|
|
raw_json = req.stream.read().decode('utf-8') |
4163
|
|
|
except Exception as ex: |
4164
|
|
|
print(str(ex)) |
4165
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
4166
|
|
|
title='API.BAD_REQUEST', |
4167
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
4168
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
4169
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4170
|
|
|
description='API.INVALID_MICROGRID_ID') |
4171
|
|
|
|
4172
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
4173
|
|
|
cursor = cnx.cursor() |
4174
|
|
|
|
4175
|
|
|
cursor.execute(" SELECT name " |
4176
|
|
|
" FROM tbl_microgrids " |
4177
|
|
|
" WHERE id = %s ", (id_,)) |
4178
|
|
|
if cursor.fetchone() is None: |
4179
|
|
|
cursor.close() |
4180
|
|
|
cnx.close() |
4181
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4182
|
|
|
description='API.MICROGRID_NOT_FOUND') |
4183
|
|
|
|
4184
|
|
|
new_values = json.loads(raw_json) |
4185
|
|
|
|
4186
|
|
|
if 'name' not in new_values['data'].keys() or \ |
4187
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
4188
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
4189
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4190
|
|
|
description='API.INVALID_MICROGRID_POWER_CONVERSION_SYSTEM_NAME') |
4191
|
|
|
name = str.strip(new_values['data']['name']) |
4192
|
|
|
|
4193
|
|
|
if 'run_state_point_id' not in new_values['data'].keys() or \ |
4194
|
|
|
not isinstance(new_values['data']['run_state_point_id'], int) or \ |
4195
|
|
|
new_values['data']['run_state_point_id'] <= 0: |
4196
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4197
|
|
|
description='API.INVALID_RUN_STATE_POINT_ID') |
4198
|
|
|
run_state_point_id = new_values['data']['run_state_point_id'] |
4199
|
|
|
|
4200
|
|
|
if 'rated_output_power' not in new_values['data'].keys() or \ |
4201
|
|
|
not (isinstance(new_values['data']['rated_output_power'], float) or |
4202
|
|
|
isinstance(new_values['data']['rated_output_power'], int)): |
4203
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4204
|
|
|
description='API.INVALID_RATED_OUTPUT_POWER') |
4205
|
|
|
rated_output_power = float(new_values['data']['rated_output_power']) |
4206
|
|
|
|
4207
|
|
|
if 'today_charge_energy_point_id' not in new_values['data'].keys() or \ |
4208
|
|
|
not isinstance(new_values['data']['today_charge_energy_point_id'], int) or \ |
4209
|
|
|
new_values['data']['today_charge_energy_point_id'] <= 0: |
4210
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4211
|
|
|
description='API.INVALID_TODAY_CHARGE_ENERGY_POINT_ID') |
4212
|
|
|
today_charge_energy_point_id = new_values['data']['today_charge_energy_point_id'] |
4213
|
|
|
|
4214
|
|
|
if 'today_discharge_energy_point_id' not in new_values['data'].keys() or \ |
4215
|
|
|
not isinstance(new_values['data']['today_discharge_energy_point_id'], int) or \ |
4216
|
|
|
new_values['data']['today_discharge_energy_point_id'] <= 0: |
4217
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4218
|
|
|
description='API.INVALID_TODAY_DISCHARGE_ENERGY_POINT_ID') |
4219
|
|
|
today_discharge_energy_point_id = new_values['data']['today_discharge_energy_point_id'] |
4220
|
|
|
|
4221
|
|
|
if 'total_charge_energy_point_id' not in new_values['data'].keys() or \ |
4222
|
|
|
not isinstance(new_values['data']['total_charge_energy_point_id'], int) or \ |
4223
|
|
|
new_values['data']['total_charge_energy_point_id'] <= 0: |
4224
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4225
|
|
|
description='API.INVALID_TOTAL_CHARGE_POINT_ID') |
4226
|
|
|
total_charge_energy_point_id = new_values['data']['total_charge_energy_point_id'] |
4227
|
|
|
|
4228
|
|
|
if 'total_discharge_energy_point_id' not in new_values['data'].keys() or \ |
4229
|
|
|
not isinstance(new_values['data']['total_discharge_energy_point_id'], int) or \ |
4230
|
|
|
new_values['data']['total_discharge_energy_point_id'] <= 0: |
4231
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4232
|
|
|
description='API.INVALID_TOTAL_DISCHARGE_POINT_ID') |
4233
|
|
|
total_discharge_energy_point_id = new_values['data']['total_discharge_energy_point_id'] |
4234
|
|
|
|
4235
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
4236
|
|
|
cursor = cnx.cursor() |
4237
|
|
|
|
4238
|
|
|
cursor.execute(" SELECT name " |
4239
|
|
|
" FROM tbl_microgrids " |
4240
|
|
|
" WHERE id = %s ", |
4241
|
|
|
(id_,)) |
4242
|
|
|
if cursor.fetchone() is None: |
4243
|
|
|
cursor.close() |
4244
|
|
|
cnx.close() |
4245
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4246
|
|
|
description='API.MICROGRID_NOT_FOUND') |
4247
|
|
|
|
4248
|
|
|
cursor.execute(" SELECT name " |
4249
|
|
|
" FROM tbl_microgrids_power_conversion_systems " |
4250
|
|
|
" WHERE microgrid_id = %s AND name = %s ", |
4251
|
|
|
(id_, name,)) |
4252
|
|
|
if cursor.fetchone() is not None: |
4253
|
|
|
cursor.close() |
4254
|
|
|
cnx.close() |
4255
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4256
|
|
|
description='API.MICROGRID_POWER_CONVERSION_SYSTEM_NAME_IS_ALREADY_IN_USE') |
4257
|
|
|
|
4258
|
|
|
add_values = (" INSERT INTO tbl_microgrids_power_conversion_systems " |
4259
|
|
|
" (name, uuid, microgrid_id, run_state_point_id, rated_output_power, " |
4260
|
|
|
" today_charge_energy_point_id, today_discharge_energy_point_id, " |
4261
|
|
|
" total_charge_energy_point_id, total_discharge_energy_point_id) " |
4262
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
4263
|
|
|
cursor.execute(add_values, (name, |
4264
|
|
|
str(uuid.uuid4()), |
4265
|
|
|
id_, |
4266
|
|
|
run_state_point_id, |
4267
|
|
|
rated_output_power, |
4268
|
|
|
today_charge_energy_point_id, |
4269
|
|
|
today_discharge_energy_point_id, |
4270
|
|
|
total_charge_energy_point_id, |
4271
|
|
|
total_discharge_energy_point_id |
4272
|
|
|
)) |
4273
|
|
|
new_id = cursor.lastrowid |
4274
|
|
|
cnx.commit() |
4275
|
|
|
cursor.close() |
4276
|
|
|
cnx.close() |
4277
|
|
|
|
4278
|
|
|
resp.status = falcon.HTTP_201 |
4279
|
|
|
resp.location = '/microgridpowerconversionsystems/' + str(new_id) |
4280
|
|
|
|
4281
|
|
|
|
4282
|
|
|
class MicrogridPowerconversionsystemItem: |
4283
|
|
|
def __init__(self): |
4284
|
|
|
"""Initializes MicrogridPowerconversionsystemItem""" |
4285
|
|
|
pass |
4286
|
|
|
|
4287
|
|
|
@staticmethod |
4288
|
|
|
def on_options(req, resp, id_, pid): |
4289
|
|
|
_ = req |
4290
|
|
|
resp.status = falcon.HTTP_200 |
4291
|
|
|
_ = id_ |
4292
|
|
|
|
4293
|
|
|
@staticmethod |
4294
|
|
|
def on_get(req, resp, id_, pid): |
4295
|
|
|
access_control(req) |
4296
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
4297
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4298
|
|
|
description='API.INVALID_MICROGRID_ID') |
4299
|
|
|
if not pid.isdigit() or int(pid) <= 0: |
4300
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4301
|
|
|
description='API.INVALID_MICROGRID_POWER_CONVERSION_SYSTEM_ID') |
4302
|
|
|
|
4303
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
4304
|
|
|
cursor = cnx.cursor() |
4305
|
|
|
|
4306
|
|
|
cursor.execute(" SELECT name " |
4307
|
|
|
" FROM tbl_microgrids " |
4308
|
|
|
" WHERE id = %s ", (id_,)) |
4309
|
|
|
if cursor.fetchone() is None: |
4310
|
|
|
cursor.close() |
4311
|
|
|
cnx.close() |
4312
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4313
|
|
|
description='API.MICROGRID_NOT_FOUND') |
4314
|
|
|
|
4315
|
|
|
# query microgrid dict |
4316
|
|
|
query = (" SELECT id, name, uuid " |
4317
|
|
|
" FROM tbl_microgrids ") |
4318
|
|
|
cursor.execute(query) |
4319
|
|
|
rows_microgrids = cursor.fetchall() |
4320
|
|
|
|
4321
|
|
|
microgrid_dict = dict() |
4322
|
|
|
if rows_microgrids is not None and len(rows_microgrids) > 0: |
4323
|
|
|
for row in rows_microgrids: |
4324
|
|
|
microgrid_dict[row[0]] = {"id": row[0], |
4325
|
|
|
"name": row[1], |
4326
|
|
|
"uuid": row[2]} |
4327
|
|
|
# query meter dict |
4328
|
|
|
query = (" SELECT id, name, uuid " |
4329
|
|
|
" FROM tbl_meters ") |
4330
|
|
|
cursor.execute(query) |
4331
|
|
|
rows_meters = cursor.fetchall() |
4332
|
|
|
|
4333
|
|
|
meter_dict = dict() |
4334
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
4335
|
|
|
for row in rows_meters: |
4336
|
|
|
meter_dict[row[0]] = {"id": row[0], |
4337
|
|
|
"name": row[1], |
4338
|
|
|
"uuid": row[2]} |
4339
|
|
|
# query point dict |
4340
|
|
|
query = (" SELECT id, name " |
4341
|
|
|
" FROM tbl_points ") |
4342
|
|
|
cursor.execute(query) |
4343
|
|
|
rows_points = cursor.fetchall() |
4344
|
|
|
|
4345
|
|
|
point_dict = dict() |
4346
|
|
|
if rows_points is not None and len(rows_points) > 0: |
4347
|
|
|
for row in rows_points: |
4348
|
|
|
point_dict[row[0]] = {"id": row[0], |
4349
|
|
|
"name": row[1]} |
4350
|
|
|
|
4351
|
|
|
# query command dict |
4352
|
|
|
query = (" SELECT id, name " |
4353
|
|
|
" FROM tbl_commands ") |
4354
|
|
|
cursor.execute(query) |
4355
|
|
|
rows_commands = cursor.fetchall() |
4356
|
|
|
|
4357
|
|
|
command_dict = dict() |
4358
|
|
|
if rows_commands is not None and len(rows_commands) > 0: |
4359
|
|
|
for row in rows_commands: |
4360
|
|
|
command_dict[row[0]] = {"id": row[0], |
4361
|
|
|
"name": row[1]} |
4362
|
|
|
|
4363
|
|
|
query = (" SELECT id, name, uuid, microgrid_id, run_state_point_id, rated_output_power, " |
4364
|
|
|
" today_charge_energy_point_id, today_discharge_energy_point_id, " |
4365
|
|
|
" total_charge_energy_point_id, total_discharge_energy_point_id " |
4366
|
|
|
" FROM tbl_microgrids_power_conversion_systems " |
4367
|
|
|
" WHERE id = %s ") |
4368
|
|
|
cursor.execute(query, (pid,)) |
4369
|
|
|
row = cursor.fetchone() |
4370
|
|
|
cursor.close() |
4371
|
|
|
cnx.close() |
4372
|
|
|
|
4373
|
|
|
if row is None: |
4374
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4375
|
|
|
description='API.MICROGRID_POWER_CONVERSION_SYSTEM_NOT_FOUND') |
4376
|
|
|
else: |
4377
|
|
|
meta_result = {"id": row[0], |
4378
|
|
|
"name": row[1], |
4379
|
|
|
"uuid": row[2], |
4380
|
|
|
"microgrid": microgrid_dict.get(row[3]), |
4381
|
|
|
"run_state_point": point_dict.get(row[4]), |
4382
|
|
|
"rated_output_power": row[5], |
4383
|
|
|
"today_charge_energy_point": point_dict.get(row[6]), |
4384
|
|
|
"today_discharge_energy_point": point_dict.get(row[7]), |
4385
|
|
|
"total_charge_energy_point": point_dict.get(row[8]), |
4386
|
|
|
"total_discharge_energy_point": point_dict.get(row[9])} |
4387
|
|
|
|
4388
|
|
|
resp.text = json.dumps(meta_result) |
4389
|
|
|
|
4390
|
|
|
@staticmethod |
4391
|
|
|
@user_logger |
4392
|
|
|
def on_delete(req, resp, id_, pid): |
4393
|
|
|
admin_control(req) |
4394
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
4395
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4396
|
|
|
description='API.INVALID_MICROGRID_ID') |
4397
|
|
|
if not pid.isdigit() or int(pid) <= 0: |
4398
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4399
|
|
|
description='API.INVALID_MICROGRID_POWER_CONVERSION_SYSTEM_ID') |
4400
|
|
|
|
4401
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
4402
|
|
|
cursor = cnx.cursor() |
4403
|
|
|
|
4404
|
|
|
cursor.execute(" SELECT name " |
4405
|
|
|
" FROM tbl_microgrids " |
4406
|
|
|
" WHERE id = %s ", (id_,)) |
4407
|
|
|
if cursor.fetchone() is None: |
4408
|
|
|
cursor.close() |
4409
|
|
|
cnx.close() |
4410
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4411
|
|
|
description='API.MICROGRID_NOT_FOUND') |
4412
|
|
|
|
4413
|
|
|
cursor.execute(" SELECT name " |
4414
|
|
|
" FROM tbl_microgrids_power_conversion_systems " |
4415
|
|
|
" WHERE id = %s ", (pid,)) |
4416
|
|
|
if cursor.fetchone() is None: |
4417
|
|
|
cursor.close() |
4418
|
|
|
cnx.close() |
4419
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4420
|
|
|
description='API.MICROGRID_POWER_CONVERSION_SYSTEM_NOT_FOUND') |
4421
|
|
|
|
4422
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_power_conversion_systems " |
4423
|
|
|
" WHERE id = %s ", (pid,)) |
4424
|
|
|
cnx.commit() |
4425
|
|
|
|
4426
|
|
|
cursor.close() |
4427
|
|
|
cnx.close() |
4428
|
|
|
|
4429
|
|
|
resp.status = falcon.HTTP_204 |
4430
|
|
|
|
4431
|
|
|
@staticmethod |
4432
|
|
|
@user_logger |
4433
|
|
|
def on_put(req, resp, id_, pid): |
4434
|
|
|
"""Handles PUT requests""" |
4435
|
|
|
admin_control(req) |
4436
|
|
|
try: |
4437
|
|
|
raw_json = req.stream.read().decode('utf-8') |
4438
|
|
|
except Exception as ex: |
4439
|
|
|
print(str(ex)) |
4440
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
4441
|
|
|
title='API.BAD_REQUEST', |
4442
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
4443
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
4444
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4445
|
|
|
description='API.INVALID_MICROGRID_ID') |
4446
|
|
|
if not pid.isdigit() or int(pid) <= 0: |
4447
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4448
|
|
|
description='API.INVALID_MICROGRID_POWER_CONVERSION_SYSTEM_ID') |
4449
|
|
|
|
4450
|
|
|
new_values = json.loads(raw_json) |
4451
|
|
|
|
4452
|
|
|
if 'name' not in new_values['data'].keys() or \ |
4453
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
4454
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
4455
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4456
|
|
|
description='API.INVALID_MICROGRID_POWER_CONVERSION_SYSTEM_NAME') |
4457
|
|
|
name = str.strip(new_values['data']['name']) |
4458
|
|
|
|
4459
|
|
|
if 'run_state_point_id' not in new_values['data'].keys() or \ |
4460
|
|
|
not isinstance(new_values['data']['run_state_point_id'], int) or \ |
4461
|
|
|
new_values['data']['run_state_point_id'] <= 0: |
4462
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4463
|
|
|
description='API.INVALID_RUN_STATE_POINT_ID') |
4464
|
|
|
run_state_point_id = new_values['data']['run_state_point_id'] |
4465
|
|
|
|
4466
|
|
|
if 'rated_output_power' not in new_values['data'].keys() or \ |
4467
|
|
|
not (isinstance(new_values['data']['rated_output_power'], float) or |
4468
|
|
|
isinstance(new_values['data']['rated_output_power'], int)): |
4469
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4470
|
|
|
description='API.INVALID_RATED_OUTPUT_POWER') |
4471
|
|
|
rated_output_power = float(new_values['data']['rated_output_power']) |
4472
|
|
|
|
4473
|
|
|
if 'today_charge_energy_point_id' not in new_values['data'].keys() or \ |
4474
|
|
|
not isinstance(new_values['data']['today_charge_energy_point_id'], int) or \ |
4475
|
|
|
new_values['data']['today_charge_energy_point_id'] <= 0: |
4476
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4477
|
|
|
description='API.INVALID_TODAY_CHARGE_ENERGY_POINT_ID') |
4478
|
|
|
today_charge_energy_point_id = new_values['data']['today_charge_energy_point_id'] |
4479
|
|
|
|
4480
|
|
|
if 'today_discharge_energy_point_id' not in new_values['data'].keys() or \ |
4481
|
|
|
not isinstance(new_values['data']['today_discharge_energy_point_id'], int) or \ |
4482
|
|
|
new_values['data']['today_discharge_energy_point_id'] <= 0: |
4483
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4484
|
|
|
description='API.INVALID_TODAY_DISCHARGE_ENERGY_POINT_ID') |
4485
|
|
|
today_discharge_energy_point_id = new_values['data']['today_discharge_energy_point_id'] |
4486
|
|
|
|
4487
|
|
|
if 'total_charge_energy_point_id' not in new_values['data'].keys() or \ |
4488
|
|
|
not isinstance(new_values['data']['total_charge_energy_point_id'], int) or \ |
4489
|
|
|
new_values['data']['total_charge_energy_point_id'] <= 0: |
4490
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4491
|
|
|
description='API.INVALID_TOTAL_CHARGE_POINT_ID') |
4492
|
|
|
total_charge_energy_point_id = new_values['data']['total_charge_energy_point_id'] |
4493
|
|
|
|
4494
|
|
|
if 'total_discharge_energy_point_id' not in new_values['data'].keys() or \ |
4495
|
|
|
not isinstance(new_values['data']['total_discharge_energy_point_id'], int) or \ |
4496
|
|
|
new_values['data']['total_discharge_energy_point_id'] <= 0: |
4497
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4498
|
|
|
description='API.INVALID_TOTAL_DISCHARGE_POINT_ID') |
4499
|
|
|
total_discharge_energy_point_id = new_values['data']['total_discharge_energy_point_id'] |
4500
|
|
|
|
4501
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
4502
|
|
|
cursor = cnx.cursor() |
4503
|
|
|
|
4504
|
|
|
cursor.execute(" SELECT name " |
4505
|
|
|
" FROM tbl_microgrids " |
4506
|
|
|
" WHERE id = %s ", (id_,)) |
4507
|
|
|
if cursor.fetchone() is None: |
4508
|
|
|
cursor.close() |
4509
|
|
|
cnx.close() |
4510
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4511
|
|
|
description='API.MICROGRID_NOT_FOUND') |
4512
|
|
|
|
4513
|
|
|
cursor.execute(" SELECT name " |
4514
|
|
|
" FROM tbl_microgrids_power_conversion_systems " |
4515
|
|
|
" WHERE id = %s ", (pid,)) |
4516
|
|
|
if cursor.fetchone() is None: |
4517
|
|
|
cursor.close() |
4518
|
|
|
cnx.close() |
4519
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4520
|
|
|
description='API.MICROGRID_POWER_CONVERSION_SYSTEM_NOT_FOUND') |
4521
|
|
|
|
4522
|
|
|
cursor.execute(" SELECT name " |
4523
|
|
|
" FROM tbl_microgrids_power_conversion_systems " |
4524
|
|
|
" WHERE microgrid_id = %s AND name = %s AND id != %s ", |
4525
|
|
|
(id_, name, pid)) |
4526
|
|
|
if cursor.fetchone() is not None: |
4527
|
|
|
cursor.close() |
4528
|
|
|
cnx.close() |
4529
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4530
|
|
|
description='API.MICROGRID_POWER_CONVERSION_SYSTEM_NAME_IS_ALREADY_IN_USE') |
4531
|
|
|
|
4532
|
|
|
update_row = (" UPDATE tbl_microgrids_power_conversion_systems " |
4533
|
|
|
" SET name = %s, microgrid_id = %s, run_state_point_id = %s, rated_output_power = %s, " |
4534
|
|
|
" today_charge_energy_point_id = %s, today_discharge_energy_point_id = %s, " |
4535
|
|
|
" total_charge_energy_point_id = %s, total_discharge_energy_point_id = %s " |
4536
|
|
|
" WHERE id = %s ") |
4537
|
|
|
cursor.execute(update_row, (name, |
4538
|
|
|
id_, |
4539
|
|
|
run_state_point_id, |
4540
|
|
|
rated_output_power, |
4541
|
|
|
today_charge_energy_point_id, |
4542
|
|
|
today_discharge_energy_point_id, |
4543
|
|
|
total_charge_energy_point_id, |
4544
|
|
|
total_discharge_energy_point_id, |
4545
|
|
|
pid)) |
4546
|
|
|
cnx.commit() |
4547
|
|
|
|
4548
|
|
|
cursor.close() |
4549
|
|
|
cnx.close() |
4550
|
|
|
|
4551
|
|
|
resp.status = falcon.HTTP_200 |
4552
|
|
|
|
4553
|
|
|
|
4554
|
|
View Code Duplication |
class MicrogridScheduleCollection: |
|
|
|
|
4555
|
|
|
def __init__(self): |
4556
|
|
|
"""Initializes Class""" |
4557
|
|
|
pass |
4558
|
|
|
|
4559
|
|
|
@staticmethod |
4560
|
|
|
def on_options(req, resp, id_): |
4561
|
|
|
_ = req |
4562
|
|
|
resp.status = falcon.HTTP_200 |
4563
|
|
|
_ = id_ |
4564
|
|
|
|
4565
|
|
|
@staticmethod |
4566
|
|
|
def on_get(req, resp, id_): |
4567
|
|
|
access_control(req) |
4568
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
4569
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4570
|
|
|
description='API.INVALID_MICROGRID_ID') |
4571
|
|
|
|
4572
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
4573
|
|
|
cursor = cnx.cursor() |
4574
|
|
|
|
4575
|
|
|
cursor.execute(" SELECT name " |
4576
|
|
|
" FROM tbl_microgrids " |
4577
|
|
|
" WHERE id = %s ", (id_,)) |
4578
|
|
|
if cursor.fetchone() is None: |
4579
|
|
|
cursor.close() |
4580
|
|
|
cnx.close() |
4581
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4582
|
|
|
description='API.MICROGRID_NOT_FOUND') |
4583
|
|
|
|
4584
|
|
|
query = (" SELECT id, start_time_of_day, end_time_of_day, peak_type, power " |
4585
|
|
|
" FROM tbl_microgrids_schedules " |
4586
|
|
|
" WHERE microgrid_id = %s " |
4587
|
|
|
" ORDER BY start_time_of_day ") |
4588
|
|
|
cursor.execute(query, (id_,)) |
4589
|
|
|
rows = cursor.fetchall() |
4590
|
|
|
|
4591
|
|
|
result = list() |
4592
|
|
|
if rows is not None and len(rows) > 0: |
4593
|
|
|
for row in rows: |
4594
|
|
|
meta_result = {"id": row[0], |
4595
|
|
|
"start_time_of_day": str(row[1]), |
4596
|
|
|
"end_time_of_day": str(row[2]), |
4597
|
|
|
"peak_type": row[3], |
4598
|
|
|
"power": row[4], |
4599
|
|
|
} |
4600
|
|
|
result.append(meta_result) |
4601
|
|
|
|
4602
|
|
|
resp.text = json.dumps(result) |
4603
|
|
|
|
4604
|
|
|
@staticmethod |
4605
|
|
|
@user_logger |
4606
|
|
|
def on_post(req, resp, id_): |
4607
|
|
|
"""Handles POST requests""" |
4608
|
|
|
admin_control(req) |
4609
|
|
|
try: |
4610
|
|
|
raw_json = req.stream.read().decode('utf-8') |
4611
|
|
|
except Exception as ex: |
4612
|
|
|
print(str(ex)) |
4613
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
4614
|
|
|
title='API.BAD_REQUEST', |
4615
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
4616
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
4617
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4618
|
|
|
description='API.INVALID_MICROGRID_ID') |
4619
|
|
|
|
4620
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
4621
|
|
|
cursor = cnx.cursor() |
4622
|
|
|
|
4623
|
|
|
cursor.execute(" SELECT name " |
4624
|
|
|
" FROM tbl_microgrids " |
4625
|
|
|
" WHERE id = %s ", (id_,)) |
4626
|
|
|
if cursor.fetchone() is None: |
4627
|
|
|
cursor.close() |
4628
|
|
|
cnx.close() |
4629
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4630
|
|
|
description='API.MICROGRID_NOT_FOUND') |
4631
|
|
|
|
4632
|
|
|
new_values = json.loads(raw_json) |
4633
|
|
|
|
4634
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
4635
|
|
|
cursor = cnx.cursor() |
4636
|
|
|
|
4637
|
|
|
cursor.execute(" SELECT name " |
4638
|
|
|
" FROM tbl_microgrids " |
4639
|
|
|
" WHERE id = %s ", |
4640
|
|
|
(id_,)) |
4641
|
|
|
if cursor.fetchone() is None: |
4642
|
|
|
cursor.close() |
4643
|
|
|
cnx.close() |
4644
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4645
|
|
|
description='API.MICROGRID_NOT_FOUND') |
4646
|
|
|
|
4647
|
|
|
add_schedule = (" INSERT INTO tbl_microgrids_schedules " |
4648
|
|
|
" (microgrid_id, start_time_of_day, end_time_of_day, peak_type, power) " |
4649
|
|
|
" VALUES (%s, %s, %s, %s, %s) ") |
4650
|
|
|
cursor.execute(add_schedule, (id_, |
4651
|
|
|
new_values['data']['start_time_of_day'], |
4652
|
|
|
new_values['data']['end_time_of_day'], |
4653
|
|
|
new_values['data']['peak_type'], |
4654
|
|
|
new_values['data']['power'])) |
4655
|
|
|
new_id = cursor.lastrowid |
4656
|
|
|
cnx.commit() |
4657
|
|
|
cursor.close() |
4658
|
|
|
cnx.close() |
4659
|
|
|
resp.status = falcon.HTTP_201 |
4660
|
|
|
resp.location = '/energystoragecontainerschedules/' + str(new_id) |
4661
|
|
|
|
4662
|
|
|
|
4663
|
|
View Code Duplication |
class MicrogridScheduleItem: |
|
|
|
|
4664
|
|
|
def __init__(self): |
4665
|
|
|
"""Initializes Class""" |
4666
|
|
|
pass |
4667
|
|
|
|
4668
|
|
|
@staticmethod |
4669
|
|
|
def on_options(req, resp, id_, sid): |
4670
|
|
|
_ = req |
4671
|
|
|
resp.status = falcon.HTTP_200 |
4672
|
|
|
_ = id_ |
4673
|
|
|
|
4674
|
|
|
@staticmethod |
4675
|
|
|
def on_get(req, resp, id_, sid): |
4676
|
|
|
access_control(req) |
4677
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
4678
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4679
|
|
|
description='API.INVALID_MICROGRID_ID') |
4680
|
|
|
if not sid.isdigit() or int(sid) <= 0: |
4681
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4682
|
|
|
description='API.INVALID_MICROGRID_SCHEDULE_ID') |
4683
|
|
|
|
4684
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
4685
|
|
|
cursor = cnx.cursor() |
4686
|
|
|
|
4687
|
|
|
cursor.execute(" SELECT name " |
4688
|
|
|
" FROM tbl_microgrids " |
4689
|
|
|
" WHERE id = %s ", (id_,)) |
4690
|
|
|
if cursor.fetchone() is None: |
4691
|
|
|
cursor.close() |
4692
|
|
|
cnx.close() |
4693
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4694
|
|
|
description='API.MICROGRID_NOT_FOUND') |
4695
|
|
|
|
4696
|
|
|
query = (" SELECT id, name, uuid " |
4697
|
|
|
" FROM tbl_microgrids ") |
4698
|
|
|
cursor.execute(query) |
4699
|
|
|
rows_energystoragecontainers = cursor.fetchall() |
4700
|
|
|
|
4701
|
|
|
microgrid_dict = dict() |
4702
|
|
|
if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
4703
|
|
|
for row in rows_energystoragecontainers: |
4704
|
|
|
microgrid_dict[row[0]] = {"id": row[0], |
4705
|
|
|
"name": row[1], |
4706
|
|
|
"uuid": row[2]} |
4707
|
|
|
|
4708
|
|
|
query = (" SELECT id, start_time_of_day, end_time_of_day, peak_type, power " |
4709
|
|
|
" FROM tbl_microgrids_schedules " |
4710
|
|
|
" WHERE id = %s ") |
4711
|
|
|
cursor.execute(query, (sid,)) |
4712
|
|
|
row = cursor.fetchone() |
4713
|
|
|
cursor.close() |
4714
|
|
|
cnx.close() |
4715
|
|
|
|
4716
|
|
|
if row is None: |
4717
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4718
|
|
|
description='API.MICROGRID_SCHEDULE_NOT_FOUND') |
4719
|
|
|
else: |
4720
|
|
|
meta_result = {"id": row[0], |
4721
|
|
|
"start_time_of_day": str(row[1]), |
4722
|
|
|
"end_time_of_day": str(row[2]), |
4723
|
|
|
"peak_type": row[3], |
4724
|
|
|
"power": row[4]} |
4725
|
|
|
|
4726
|
|
|
resp.text = json.dumps(meta_result) |
4727
|
|
|
|
4728
|
|
|
@staticmethod |
4729
|
|
|
@user_logger |
4730
|
|
|
def on_delete(req, resp, id_, sid): |
4731
|
|
|
admin_control(req) |
4732
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
4733
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4734
|
|
|
description='API.INVALID_MICROGRID_ID') |
4735
|
|
|
if not sid.isdigit() or int(sid) <= 0: |
4736
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4737
|
|
|
description='API.INVALID_MICROGRID_SCHEDULE_ID') |
4738
|
|
|
|
4739
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
4740
|
|
|
cursor = cnx.cursor() |
4741
|
|
|
|
4742
|
|
|
cursor.execute(" SELECT name " |
4743
|
|
|
" FROM tbl_microgrids " |
4744
|
|
|
" WHERE id = %s ", (id_,)) |
4745
|
|
|
if cursor.fetchone() is None: |
4746
|
|
|
cursor.close() |
4747
|
|
|
cnx.close() |
4748
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4749
|
|
|
description='API.MICROGRID_NOT_FOUND') |
4750
|
|
|
|
4751
|
|
|
cursor.execute(" SELECT id " |
4752
|
|
|
" FROM tbl_microgrids_schedules " |
4753
|
|
|
" WHERE id = %s ", (sid,)) |
4754
|
|
|
if cursor.fetchone() is None: |
4755
|
|
|
cursor.close() |
4756
|
|
|
cnx.close() |
4757
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4758
|
|
|
description='API.MICROGRID_SCHEDULE_NOT_FOUND') |
4759
|
|
|
|
4760
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_schedules " |
4761
|
|
|
" WHERE id = %s ", (sid,)) |
4762
|
|
|
cnx.commit() |
4763
|
|
|
|
4764
|
|
|
cursor.close() |
4765
|
|
|
cnx.close() |
4766
|
|
|
|
4767
|
|
|
resp.status = falcon.HTTP_204 |
4768
|
|
|
|
4769
|
|
|
@staticmethod |
4770
|
|
|
@user_logger |
4771
|
|
|
def on_put(req, resp, id_, sid): |
4772
|
|
|
"""Handles PUT requests""" |
4773
|
|
|
admin_control(req) |
4774
|
|
|
try: |
4775
|
|
|
raw_json = req.stream.read().decode('utf-8') |
4776
|
|
|
except Exception as ex: |
4777
|
|
|
print(str(ex)) |
4778
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
4779
|
|
|
title='API.BAD_REQUEST', |
4780
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
4781
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
4782
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4783
|
|
|
description='API.INVALID_MICROGRID_ID') |
4784
|
|
|
if not sid.isdigit() or int(sid) <= 0: |
4785
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4786
|
|
|
description='API.INVALID_MICROGRID_SCHEDULE_ID') |
4787
|
|
|
|
4788
|
|
|
new_values = json.loads(raw_json) |
4789
|
|
|
|
4790
|
|
|
if 'start_time_of_day' not in new_values['data'].keys() or \ |
4791
|
|
|
not isinstance(new_values['data']['start_time_of_day'], str) or \ |
4792
|
|
|
len(str.strip(new_values['data']['start_time_of_day'])) == 0: |
4793
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4794
|
|
|
description='API.INVALID_START_TIME_OF_DAY') |
4795
|
|
|
start_time_of_day = str.strip(new_values['data']['start_time_of_day']) |
4796
|
|
|
|
4797
|
|
|
if 'end_time_of_day' not in new_values['data'].keys() or \ |
4798
|
|
|
not isinstance(new_values['data']['end_time_of_day'], str) or \ |
4799
|
|
|
len(str.strip(new_values['data']['end_time_of_day'])) == 0: |
4800
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4801
|
|
|
description='API.INVALID_END_TIME_OF_DAY') |
4802
|
|
|
end_time_of_day = str.strip(new_values['data']['end_time_of_day']) |
4803
|
|
|
|
4804
|
|
|
if 'peak_type' not in new_values['data'].keys() or \ |
4805
|
|
|
not isinstance(new_values['data']['peak_type'], str) or \ |
4806
|
|
|
len(str.strip(new_values['data']['peak_type'])) == 0: |
4807
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4808
|
|
|
description='API.INVALID_PEAK_TYPE') |
4809
|
|
|
peak_type = str.strip(new_values['data']['peak_type']) |
4810
|
|
|
|
4811
|
|
|
if 'power' not in new_values['data'].keys() or \ |
4812
|
|
|
not (isinstance(new_values['data']['power'], float) or |
4813
|
|
|
isinstance(new_values['data']['power'], int)): |
4814
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4815
|
|
|
description='API.INVALID_POWER') |
4816
|
|
|
power = float(new_values['data']['power']) |
4817
|
|
|
|
4818
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
4819
|
|
|
cursor = cnx.cursor() |
4820
|
|
|
|
4821
|
|
|
cursor.execute(" SELECT name " |
4822
|
|
|
" FROM tbl_microgrids " |
4823
|
|
|
" WHERE id = %s ", (id_,)) |
4824
|
|
|
if cursor.fetchone() is None: |
4825
|
|
|
cursor.close() |
4826
|
|
|
cnx.close() |
4827
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4828
|
|
|
description='API.MICROGRID_NOT_FOUND') |
4829
|
|
|
|
4830
|
|
|
cursor.execute(" SELECT id " |
4831
|
|
|
" FROM tbl_microgrids_schedules " |
4832
|
|
|
" WHERE id = %s ", (sid,)) |
4833
|
|
|
if cursor.fetchone() is None: |
4834
|
|
|
cursor.close() |
4835
|
|
|
cnx.close() |
4836
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4837
|
|
|
description='API.MICROGRID_SCHEDULE_NOT_FOUND') |
4838
|
|
|
|
4839
|
|
|
update_row = (" UPDATE tbl_microgrids_schedules " |
4840
|
|
|
" SET start_time_of_day = %s, end_time_of_day = %s, peak_type = %s, power = %s " |
4841
|
|
|
" WHERE id = %s ") |
4842
|
|
|
cursor.execute(update_row, (start_time_of_day, |
4843
|
|
|
end_time_of_day, |
4844
|
|
|
peak_type, |
4845
|
|
|
power, |
4846
|
|
|
sid)) |
4847
|
|
|
cnx.commit() |
4848
|
|
|
|
4849
|
|
|
cursor.close() |
4850
|
|
|
cnx.close() |
4851
|
|
|
|
4852
|
|
|
resp.status = falcon.HTTP_200 |
4853
|
|
|
|
4854
|
|
|
|
4855
|
|
View Code Duplication |
class MicrogridSensorCollection: |
|
|
|
|
4856
|
|
|
def __init__(self): |
4857
|
|
|
"""Initializes Class""" |
4858
|
|
|
pass |
4859
|
|
|
|
4860
|
|
|
@staticmethod |
4861
|
|
|
def on_options(req, resp, id_): |
4862
|
|
|
_ = req |
4863
|
|
|
resp.status = falcon.HTTP_200 |
4864
|
|
|
_ = id_ |
4865
|
|
|
|
4866
|
|
|
@staticmethod |
4867
|
|
|
def on_get(req, resp, id_): |
4868
|
|
|
access_control(req) |
4869
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
4870
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4871
|
|
|
description='API.INVALID_MICROGRID_ID') |
4872
|
|
|
|
4873
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
4874
|
|
|
cursor = cnx.cursor() |
4875
|
|
|
|
4876
|
|
|
cursor.execute(" SELECT name " |
4877
|
|
|
" FROM tbl_microgrids " |
4878
|
|
|
" WHERE id = %s ", (id_,)) |
4879
|
|
|
if cursor.fetchone() is None: |
4880
|
|
|
cursor.close() |
4881
|
|
|
cnx.close() |
4882
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4883
|
|
|
description='API.MICROGRID_NOT_FOUND') |
4884
|
|
|
|
4885
|
|
|
query = (" SELECT s.id, s.name, s.uuid " |
4886
|
|
|
" FROM tbl_microgrids m, tbl_microgrids_sensors ms, tbl_sensors s " |
4887
|
|
|
" WHERE ms.microgrid_id = m.id AND s.id = ms.sensor_id AND m.id = %s " |
4888
|
|
|
" ORDER BY s.id ") |
4889
|
|
|
cursor.execute(query, (id_,)) |
4890
|
|
|
rows = cursor.fetchall() |
4891
|
|
|
|
4892
|
|
|
result = list() |
4893
|
|
|
if rows is not None and len(rows) > 0: |
4894
|
|
|
for row in rows: |
4895
|
|
|
meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
4896
|
|
|
result.append(meta_result) |
4897
|
|
|
|
4898
|
|
|
resp.text = json.dumps(result) |
4899
|
|
|
|
4900
|
|
|
@staticmethod |
4901
|
|
|
@user_logger |
4902
|
|
|
def on_post(req, resp, id_): |
4903
|
|
|
"""Handles POST requests""" |
4904
|
|
|
admin_control(req) |
4905
|
|
|
try: |
4906
|
|
|
raw_json = req.stream.read().decode('utf-8') |
4907
|
|
|
except Exception as ex: |
4908
|
|
|
print(str(ex)) |
4909
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
4910
|
|
|
title='API.BAD_REQUEST', |
4911
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
4912
|
|
|
|
4913
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
4914
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4915
|
|
|
description='API.INVALID_MICROGRID_ID') |
4916
|
|
|
|
4917
|
|
|
new_values = json.loads(raw_json) |
4918
|
|
|
|
4919
|
|
|
if 'sensor_id' not in new_values['data'].keys() or \ |
4920
|
|
|
not isinstance(new_values['data']['sensor_id'], int) or \ |
4921
|
|
|
new_values['data']['sensor_id'] <= 0: |
4922
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4923
|
|
|
description='API.INVALID_SENSOR_ID') |
4924
|
|
|
sensor_id = new_values['data']['sensor_id'] |
4925
|
|
|
|
4926
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
4927
|
|
|
cursor = cnx.cursor() |
4928
|
|
|
|
4929
|
|
|
cursor.execute(" SELECT name " |
4930
|
|
|
" from tbl_microgrids " |
4931
|
|
|
" WHERE id = %s ", (id_,)) |
4932
|
|
|
if cursor.fetchone() is None: |
4933
|
|
|
cursor.close() |
4934
|
|
|
cnx.close() |
4935
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4936
|
|
|
description='API.MICROGRID_NOT_FOUND') |
4937
|
|
|
|
4938
|
|
|
cursor.execute(" SELECT name " |
4939
|
|
|
" FROM tbl_sensors " |
4940
|
|
|
" WHERE id = %s ", (sensor_id,)) |
4941
|
|
|
if cursor.fetchone() is None: |
4942
|
|
|
cursor.close() |
4943
|
|
|
cnx.close() |
4944
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
4945
|
|
|
description='API.SENSOR_NOT_FOUND') |
4946
|
|
|
|
4947
|
|
|
query = (" SELECT id " |
4948
|
|
|
" FROM tbl_microgrids_sensors " |
4949
|
|
|
" WHERE microgrid_id = %s AND sensor_id = %s") |
4950
|
|
|
cursor.execute(query, (id_, sensor_id,)) |
4951
|
|
|
if cursor.fetchone() is not None: |
4952
|
|
|
cursor.close() |
4953
|
|
|
cnx.close() |
4954
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
4955
|
|
|
description='API.MICROGRID_SENSOR_RELATION_EXISTS') |
4956
|
|
|
|
4957
|
|
|
add_row = (" INSERT INTO tbl_microgrids_sensors (microgrid_id, sensor_id) " |
4958
|
|
|
" VALUES (%s, %s) ") |
4959
|
|
|
cursor.execute(add_row, (id_, sensor_id,)) |
4960
|
|
|
cnx.commit() |
4961
|
|
|
cursor.close() |
4962
|
|
|
cnx.close() |
4963
|
|
|
|
4964
|
|
|
resp.status = falcon.HTTP_201 |
4965
|
|
|
resp.location = '/microgrids/' + str(id_) + '/sensors/' + str(sensor_id) |
4966
|
|
|
|
4967
|
|
|
|
4968
|
|
View Code Duplication |
class MicrogridSensorItem: |
|
|
|
|
4969
|
|
|
def __init__(self): |
4970
|
|
|
"""Initializes Class""" |
4971
|
|
|
pass |
4972
|
|
|
|
4973
|
|
|
@staticmethod |
4974
|
|
|
def on_options(req, resp, id_, sid): |
4975
|
|
|
_ = req |
4976
|
|
|
resp.status = falcon.HTTP_200 |
4977
|
|
|
_ = id_ |
4978
|
|
|
|
4979
|
|
|
@staticmethod |
4980
|
|
|
@user_logger |
4981
|
|
|
def on_delete(req, resp, id_, sid): |
4982
|
|
|
admin_control(req) |
4983
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
4984
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4985
|
|
|
description='API.INVALID_MICROGRID_ID') |
4986
|
|
|
|
4987
|
|
|
if not sid.isdigit() or int(sid) <= 0: |
4988
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
4989
|
|
|
description='API.INVALID_SENSOR_ID') |
4990
|
|
|
|
4991
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
4992
|
|
|
cursor = cnx.cursor() |
4993
|
|
|
|
4994
|
|
|
cursor.execute(" SELECT name " |
4995
|
|
|
" FROM tbl_microgrids " |
4996
|
|
|
" WHERE id = %s ", (id_,)) |
4997
|
|
|
if cursor.fetchone() is None: |
4998
|
|
|
cursor.close() |
4999
|
|
|
cnx.close() |
5000
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5001
|
|
|
description='API.MICROGRID_NOT_FOUND') |
5002
|
|
|
|
5003
|
|
|
cursor.execute(" SELECT name " |
5004
|
|
|
" FROM tbl_sensors " |
5005
|
|
|
" WHERE id = %s ", (sid,)) |
5006
|
|
|
if cursor.fetchone() is None: |
5007
|
|
|
cursor.close() |
5008
|
|
|
cnx.close() |
5009
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5010
|
|
|
description='API.SENSOR_NOT_FOUND') |
5011
|
|
|
|
5012
|
|
|
cursor.execute(" SELECT id " |
5013
|
|
|
" FROM tbl_microgrids_sensors " |
5014
|
|
|
" WHERE microgrid_id = %s AND sensor_id = %s ", (id_, sid)) |
5015
|
|
|
if cursor.fetchone() is None: |
5016
|
|
|
cursor.close() |
5017
|
|
|
cnx.close() |
5018
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5019
|
|
|
description='API.MICROGRID_SENSOR_RELATION_NOT_FOUND') |
5020
|
|
|
|
5021
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_sensors " |
5022
|
|
|
" WHERE microgrid_id = %s AND sensor_id = %s ", (id_, sid)) |
5023
|
|
|
cnx.commit() |
5024
|
|
|
|
5025
|
|
|
cursor.close() |
5026
|
|
|
cnx.close() |
5027
|
|
|
|
5028
|
|
|
resp.status = falcon.HTTP_204 |
5029
|
|
|
|
5030
|
|
|
|
5031
|
|
View Code Duplication |
class MicrogridUserCollection: |
|
|
|
|
5032
|
|
|
def __init__(self): |
5033
|
|
|
"""Initializes Class""" |
5034
|
|
|
pass |
5035
|
|
|
|
5036
|
|
|
@staticmethod |
5037
|
|
|
def on_options(req, resp, id_): |
5038
|
|
|
_ = req |
5039
|
|
|
resp.status = falcon.HTTP_200 |
5040
|
|
|
_ = id_ |
5041
|
|
|
|
5042
|
|
|
@staticmethod |
5043
|
|
|
def on_get(req, resp, id_): |
5044
|
|
|
access_control(req) |
5045
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
5046
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5047
|
|
|
description='API.INVALID_MICROGRID_ID') |
5048
|
|
|
|
5049
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
5050
|
|
|
cursor = cnx.cursor() |
5051
|
|
|
cursor.execute(" SELECT name " |
5052
|
|
|
" FROM tbl_microgrids " |
5053
|
|
|
" WHERE id = %s ", (id_,)) |
5054
|
|
|
if cursor.fetchone() is None: |
5055
|
|
|
cursor.close() |
5056
|
|
|
cnx.close() |
5057
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5058
|
|
|
description='API.MICROGRID_NOT_FOUND') |
5059
|
|
|
|
5060
|
|
|
query = (" SELECT u.id, u.name, u.uuid " |
5061
|
|
|
" FROM tbl_microgrids m, tbl_microgrids_users mu, " |
5062
|
|
|
+ config.myems_user_db['database'] + ".tbl_users u " |
5063
|
|
|
" WHERE mu.microgrid_id = m.id AND u.id = mu.user_id AND m.id = %s " |
5064
|
|
|
" ORDER BY u.id ") |
5065
|
|
|
cursor.execute(query, (id_,)) |
5066
|
|
|
rows = cursor.fetchall() |
5067
|
|
|
result = list() |
5068
|
|
|
if rows is not None and len(rows) > 0: |
5069
|
|
|
for row in rows: |
5070
|
|
|
meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
5071
|
|
|
result.append(meta_result) |
5072
|
|
|
|
5073
|
|
|
cursor.close() |
5074
|
|
|
cnx.close() |
5075
|
|
|
resp.text = json.dumps(result) |
5076
|
|
|
|
5077
|
|
|
@staticmethod |
5078
|
|
|
@user_logger |
5079
|
|
|
def on_post(req, resp, id_): |
5080
|
|
|
"""Handles POST requests""" |
5081
|
|
|
admin_control(req) |
5082
|
|
|
try: |
5083
|
|
|
raw_json = req.stream.read().decode('utf-8') |
5084
|
|
|
except Exception as ex: |
5085
|
|
|
print(str(ex)) |
5086
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
5087
|
|
|
title='API.BAD_REQUEST', |
5088
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
5089
|
|
|
|
5090
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
5091
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5092
|
|
|
description='API.INVALID_MICROGRID_ID') |
5093
|
|
|
|
5094
|
|
|
new_values = json.loads(raw_json) |
5095
|
|
|
if 'user_id' not in new_values['data'].keys() or \ |
5096
|
|
|
not isinstance(new_values['data']['user_id'], int) or \ |
5097
|
|
|
new_values['data']['user_id'] <= 0: |
5098
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5099
|
|
|
description='API.INVALID_USER_ID') |
5100
|
|
|
user_id = new_values['data']['user_id'] |
5101
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
5102
|
|
|
cursor = cnx.cursor() |
5103
|
|
|
cursor.execute(" SELECT name " |
5104
|
|
|
" from tbl_microgrids " |
5105
|
|
|
" WHERE id = %s ", (id_,)) |
5106
|
|
|
if cursor.fetchone() is None: |
5107
|
|
|
cursor.close() |
5108
|
|
|
cnx.close() |
5109
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5110
|
|
|
description='API.MICROGRID_NOT_FOUND') |
5111
|
|
|
|
5112
|
|
|
cnx_user = mysql.connector.connect(**config.myems_user_db) |
5113
|
|
|
cursor_user = cnx_user.cursor() |
5114
|
|
|
cursor_user.execute(" SELECT name" |
5115
|
|
|
" FROM tbl_users " |
5116
|
|
|
" WHERE id = %s ", (user_id,)) |
5117
|
|
|
if cursor_user.fetchone() is None: |
5118
|
|
|
cursor_user.close() |
5119
|
|
|
cnx_user.close() |
5120
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5121
|
|
|
description='API.USER_NOT_FOUND') |
5122
|
|
|
query = (" SELECT id " |
5123
|
|
|
" FROM tbl_microgrids_users " |
5124
|
|
|
" WHERE microgrid_id = %s AND user_id = %s") |
5125
|
|
|
cursor.execute(query, (id_, user_id,)) |
5126
|
|
|
if cursor.fetchone() is not None: |
5127
|
|
|
cursor.close() |
5128
|
|
|
cnx.close() |
5129
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
5130
|
|
|
description='API.MICROGRID_USER_RELATION_EXISTS') |
5131
|
|
|
add_row = (" INSERT INTO tbl_microgrids_users (microgrid_id, user_id) " |
5132
|
|
|
" VALUES (%s, %s) ") |
5133
|
|
|
cursor.execute(add_row, (id_, user_id,)) |
5134
|
|
|
cnx.commit() |
5135
|
|
|
cursor.close() |
5136
|
|
|
cnx.close() |
5137
|
|
|
cursor_user.close() |
5138
|
|
|
cnx_user.close() |
5139
|
|
|
|
5140
|
|
|
resp.status = falcon.HTTP_201 |
5141
|
|
|
resp.location = '/microgrids/' + str(id_) + '/users/' + str(user_id) |
5142
|
|
|
|
5143
|
|
|
|
5144
|
|
View Code Duplication |
class MicrogridUserItem: |
|
|
|
|
5145
|
|
|
def __init__(self): |
5146
|
|
|
"""Initializes Class""" |
5147
|
|
|
pass |
5148
|
|
|
|
5149
|
|
|
@staticmethod |
5150
|
|
|
def on_options(req, resp, id_, uid): |
5151
|
|
|
_ = req |
5152
|
|
|
resp.status = falcon.HTTP_200 |
5153
|
|
|
_ = id_ |
5154
|
|
|
|
5155
|
|
|
@staticmethod |
5156
|
|
|
@user_logger |
5157
|
|
|
def on_delete(req, resp, id_, uid): |
5158
|
|
|
# todo Verify if the user is bound when deleting it |
5159
|
|
|
admin_control(req) |
5160
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
5161
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5162
|
|
|
description='API.INVALID_MICROGRID_ID') |
5163
|
|
|
|
5164
|
|
|
if not uid.isdigit() or int(uid) <= 0: |
5165
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5166
|
|
|
description='API.INVALID_USER_ID') |
5167
|
|
|
|
5168
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
5169
|
|
|
cursor = cnx.cursor() |
5170
|
|
|
cursor.execute(" SELECT name " |
5171
|
|
|
" FROM tbl_microgrids " |
5172
|
|
|
" WHERE id = %s ", (id_,)) |
5173
|
|
|
if cursor.fetchone() is None: |
5174
|
|
|
cursor.close() |
5175
|
|
|
cnx.close() |
5176
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5177
|
|
|
description='API.MICROGRID_NOT_FOUND') |
5178
|
|
|
|
5179
|
|
|
cnx_user = mysql.connector.connect(**config.myems_user_db) |
5180
|
|
|
cursor_user = cnx_user.cursor() |
5181
|
|
|
cursor_user.execute(" SELECT name FROM tbl_users WHERE id = %s ", (uid,)) |
5182
|
|
|
if cursor_user.fetchone() is None: |
5183
|
|
|
cursor_user.close() |
5184
|
|
|
cnx_user.close() |
5185
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5186
|
|
|
description='API.USER_NOT_FOUND') |
5187
|
|
|
|
5188
|
|
|
cursor.execute(" SELECT id " |
5189
|
|
|
" FROM tbl_microgrids_users " |
5190
|
|
|
" WHERE microgrid_id = %s AND user_id = %s ", (id_, uid)) |
5191
|
|
|
if cursor.fetchone() is None: |
5192
|
|
|
cursor.close() |
5193
|
|
|
cnx.close() |
5194
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5195
|
|
|
description='API.MICROGRID_USER_RELATION_NOT_FOUND') |
5196
|
|
|
|
5197
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_users WHERE microgrid_id = %s AND user_id = %s ", (id_, uid)) |
5198
|
|
|
cnx.commit() |
5199
|
|
|
|
5200
|
|
|
cursor.close() |
5201
|
|
|
cnx.close() |
5202
|
|
|
cursor_user.close() |
5203
|
|
|
cnx_user.close() |
5204
|
|
|
|
5205
|
|
|
resp.status = falcon.HTTP_204 |
5206
|
|
|
|
5207
|
|
|
|
5208
|
|
|
class MicrogridExport: |
5209
|
|
|
def __init__(self): |
5210
|
|
|
""""Initializes MicrogridExport""" |
5211
|
|
|
pass |
5212
|
|
|
|
5213
|
|
|
@staticmethod |
5214
|
|
|
def on_options(req, resp, id_): |
5215
|
|
|
_ = req |
5216
|
|
|
resp.status = falcon.HTTP_200 |
5217
|
|
|
_ = id_ |
5218
|
|
|
|
5219
|
|
|
@staticmethod |
5220
|
|
|
def on_get(req, resp, id_): |
5221
|
|
|
access_control(req) |
5222
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
5223
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5224
|
|
|
description='API.INVALID_MICROGRID_ID') |
5225
|
|
|
|
5226
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
5227
|
|
|
cursor = cnx.cursor() |
5228
|
|
|
|
5229
|
|
|
query = (" SELECT id, name, uuid " |
5230
|
|
|
" FROM tbl_contacts ") |
5231
|
|
|
cursor.execute(query) |
5232
|
|
|
rows_contacts = cursor.fetchall() |
5233
|
|
|
|
5234
|
|
|
contact_dict = dict() |
5235
|
|
|
if rows_contacts is not None and len(rows_contacts) > 0: |
5236
|
|
|
for row in rows_contacts: |
5237
|
|
|
contact_dict[row[0]] = {"id": row[0], |
5238
|
|
|
"name": row[1], |
5239
|
|
|
"uuid": row[2]} |
5240
|
|
|
|
5241
|
|
|
query = (" SELECT id, name, uuid " |
5242
|
|
|
" FROM tbl_cost_centers ") |
5243
|
|
|
cursor.execute(query) |
5244
|
|
|
rows_cost_centers = cursor.fetchall() |
5245
|
|
|
|
5246
|
|
|
cost_center_dict = dict() |
5247
|
|
|
if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
5248
|
|
|
for row in rows_cost_centers: |
5249
|
|
|
cost_center_dict[row[0]] = {"id": row[0], |
5250
|
|
|
"name": row[1], |
5251
|
|
|
"uuid": row[2]} |
5252
|
|
|
|
5253
|
|
|
query = (" SELECT id, name, uuid, " |
5254
|
|
|
" address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
5255
|
|
|
" contact_id, cost_center_id, serial_number, svg_id, is_cost_data_displayed, phase_of_lifecycle, description " |
5256
|
|
|
" FROM tbl_microgrids " |
5257
|
|
|
" WHERE id = %s ") |
5258
|
|
|
cursor.execute(query, (id_,)) |
5259
|
|
|
row = cursor.fetchone() |
5260
|
|
|
cursor.close() |
5261
|
|
|
cnx.close() |
5262
|
|
|
|
5263
|
|
|
if row is None: |
5264
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5265
|
|
|
description='API.MICROGRID_NOT_FOUND') |
5266
|
|
|
else: |
5267
|
|
|
meta_result = {"id": row[0], |
5268
|
|
|
"name": row[1], |
5269
|
|
|
"uuid": row[2], |
5270
|
|
|
"address": row[3], |
5271
|
|
|
"postal_code": row[4], |
5272
|
|
|
"latitude": row[5], |
5273
|
|
|
"longitude": row[6], |
5274
|
|
|
"rated_capacity": row[7], |
5275
|
|
|
"rated_power": row[8], |
5276
|
|
|
"contact": contact_dict.get(row[9], None), |
5277
|
|
|
"cost_center": cost_center_dict.get(row[10], None), |
5278
|
|
|
"serial_number": row[11], |
5279
|
|
|
"svg_id": row[12], |
5280
|
|
|
"is_cost_data_displayed": bool(row[13]), |
5281
|
|
|
"phase_of_lifecycle": row[14], |
5282
|
|
|
"description": row[15]} |
5283
|
|
|
|
5284
|
|
|
resp.text = json.dumps(meta_result) |
5285
|
|
|
|
5286
|
|
|
|
5287
|
|
|
class MicrogridImport: |
5288
|
|
|
def __init__(self): |
5289
|
|
|
""""Initializes MicrogridImport""" |
5290
|
|
|
pass |
5291
|
|
|
|
5292
|
|
|
@staticmethod |
5293
|
|
|
def on_options(req, resp): |
5294
|
|
|
_ = req |
5295
|
|
|
resp.status = falcon.HTTP_200 |
5296
|
|
|
|
5297
|
|
|
@staticmethod |
5298
|
|
|
@user_logger |
5299
|
|
|
def on_post(req, resp): |
5300
|
|
|
"""Handles POST requests""" |
5301
|
|
|
admin_control(req) |
5302
|
|
|
try: |
5303
|
|
|
raw_json = req.stream.read().decode('utf-8') |
5304
|
|
|
except Exception as ex: |
5305
|
|
|
print(str(ex)) |
5306
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
5307
|
|
|
title='API.BAD_REQUEST', |
5308
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
5309
|
|
|
|
5310
|
|
|
new_values = json.loads(raw_json) |
5311
|
|
|
|
5312
|
|
|
if 'name' not in new_values.keys() or \ |
5313
|
|
|
not isinstance(new_values['name'], str) or \ |
5314
|
|
|
len(str.strip(new_values['name'])) == 0: |
5315
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5316
|
|
|
description='API.INVALID_MICROGRID_NAME') |
5317
|
|
|
name = str.strip(new_values['name']) |
5318
|
|
|
|
5319
|
|
|
if 'address' not in new_values.keys() or \ |
5320
|
|
|
not isinstance(new_values['address'], str) or \ |
5321
|
|
|
len(str.strip(new_values['address'])) == 0: |
5322
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5323
|
|
|
description='API.INVALID_ADDRESS_VALUE') |
5324
|
|
|
address = str.strip(new_values['address']) |
5325
|
|
|
|
5326
|
|
|
if 'postal_code' not in new_values.keys() or \ |
5327
|
|
|
not isinstance(new_values['postal_code'], str) or \ |
5328
|
|
|
len(str.strip(new_values['postal_code'])) == 0: |
5329
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5330
|
|
|
description='API.INVALID_POSTAL_CODE_VALUE') |
5331
|
|
|
postal_code = str.strip(new_values['postal_code']) |
5332
|
|
|
|
5333
|
|
|
if 'latitude' not in new_values.keys() or \ |
5334
|
|
|
not (isinstance(new_values['latitude'], float) or |
5335
|
|
|
isinstance(new_values['latitude'], int)) or \ |
5336
|
|
|
new_values['latitude'] < -90.0 or \ |
5337
|
|
|
new_values['latitude'] > 90.0: |
5338
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5339
|
|
|
description='API.INVALID_LATITUDE_VALUE') |
5340
|
|
|
latitude = new_values['latitude'] |
5341
|
|
|
|
5342
|
|
|
if 'longitude' not in new_values.keys() or \ |
5343
|
|
|
not (isinstance(new_values['longitude'], float) or |
5344
|
|
|
isinstance(new_values['longitude'], int)) or \ |
5345
|
|
|
new_values['longitude'] < -180.0 or \ |
5346
|
|
|
new_values['longitude'] > 180.0: |
5347
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5348
|
|
|
description='API.INVALID_LONGITUDE_VALUE') |
5349
|
|
|
longitude = new_values['longitude'] |
5350
|
|
|
|
5351
|
|
|
if 'rated_capacity' not in new_values.keys() or \ |
5352
|
|
|
not (isinstance(new_values['rated_capacity'], float) or |
5353
|
|
|
isinstance(new_values['rated_capacity'], int)) or \ |
5354
|
|
|
new_values['rated_capacity'] <= 0.0: |
5355
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5356
|
|
|
description='API.INVALID_RATED_CAPACITY') |
5357
|
|
|
rated_capacity = new_values['rated_capacity'] |
5358
|
|
|
|
5359
|
|
|
if 'rated_power' not in new_values.keys() or \ |
5360
|
|
|
not (isinstance(new_values['rated_power'], float) or |
5361
|
|
|
isinstance(new_values['rated_power'], int)) or \ |
5362
|
|
|
new_values['rated_power'] <= 0.0: |
5363
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5364
|
|
|
description='API.INVALID_RATED_POWER') |
5365
|
|
|
rated_power = new_values['rated_power'] |
5366
|
|
|
|
5367
|
|
|
if 'id' not in new_values['contact'].keys() or \ |
5368
|
|
|
not isinstance(new_values['contact']['id'], int) or \ |
5369
|
|
|
new_values['contact']['id'] <= 0: |
5370
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5371
|
|
|
description='API.INVALID_CONTACT_ID') |
5372
|
|
|
contact_id = new_values['contact']['id'] |
5373
|
|
|
|
5374
|
|
|
if 'id' not in new_values['cost_center'].keys() or \ |
5375
|
|
|
not isinstance(new_values['cost_center']['id'], int) or \ |
5376
|
|
|
new_values['cost_center']['id'] <= 0: |
5377
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5378
|
|
|
description='API.INVALID_COST_CENTER_ID') |
5379
|
|
|
cost_center_id = new_values['cost_center']['id'] |
5380
|
|
|
|
5381
|
|
|
if 'serial_number' not in new_values.keys() or \ |
5382
|
|
|
not isinstance(new_values['serial_number'], str) or \ |
5383
|
|
|
len(str.strip(new_values['serial_number'])) == 0: |
5384
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5385
|
|
|
description='API.INVALID_SERIAL_NUMBER') |
5386
|
|
|
serial_number = str.strip(new_values['serial_number']) |
5387
|
|
|
|
5388
|
|
|
if 'svg_id' not in new_values.keys() or \ |
5389
|
|
|
not isinstance(new_values['svg_id'], int) or \ |
5390
|
|
|
new_values['svg_id'] <= 0: |
5391
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5392
|
|
|
description='API.INVALID_SVG_ID') |
5393
|
|
|
svg_id = new_values['svg_id'] |
5394
|
|
|
|
5395
|
|
|
if 'is_cost_data_displayed' not in new_values.keys() or \ |
5396
|
|
|
not isinstance(new_values['is_cost_data_displayed'], bool): |
5397
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5398
|
|
|
description='API.INVALID_IS_COST_DATA_DISPLAYED') |
5399
|
|
|
is_cost_data_displayed = new_values['is_cost_data_displayed'] |
5400
|
|
|
|
5401
|
|
|
if 'phase_of_lifecycle' not in new_values.keys() or \ |
5402
|
|
|
not isinstance(new_values['phase_of_lifecycle'], str) or \ |
5403
|
|
|
len(str.strip(new_values['phase_of_lifecycle'])) == 0: |
5404
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5405
|
|
|
description='API.INVALID_PHASE_OF_LIFECYCLE') |
5406
|
|
|
phase_of_lifecycle = str.strip(new_values['phase_of_lifecycle']) |
5407
|
|
|
|
5408
|
|
|
if 'description' in new_values.keys() and \ |
5409
|
|
|
new_values['description'] is not None and \ |
5410
|
|
|
len(str(new_values['description'])) > 0: |
5411
|
|
|
description = str.strip(new_values['description']) |
5412
|
|
|
else: |
5413
|
|
|
description = None |
5414
|
|
|
|
5415
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
5416
|
|
|
cursor = cnx.cursor() |
5417
|
|
|
|
5418
|
|
|
original_name = name |
5419
|
|
|
while True: |
5420
|
|
|
cursor.execute(" SELECT name " |
5421
|
|
|
" FROM tbl_microgrids " |
5422
|
|
|
" WHERE name = %s ", (name,)) |
5423
|
|
|
if cursor.fetchone() is None: |
5424
|
|
|
break |
5425
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
5426
|
|
|
if config.utc_offset[0] == '-': |
5427
|
|
|
timezone_offset = -timezone_offset |
5428
|
|
|
name = (str.strip(original_name) + |
5429
|
|
|
(datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')) |
5430
|
|
|
|
5431
|
|
|
cursor.execute(" SELECT name " |
5432
|
|
|
" FROM tbl_contacts " |
5433
|
|
|
" WHERE id = %s ", |
5434
|
|
|
(new_values['contact']['id'],)) |
5435
|
|
|
row = cursor.fetchone() |
5436
|
|
|
if row is None: |
5437
|
|
|
cursor.close() |
5438
|
|
|
cnx.close() |
5439
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5440
|
|
|
description='API.CONTACT_NOT_FOUND') |
5441
|
|
|
|
5442
|
|
|
cursor.execute(" SELECT name " |
5443
|
|
|
" FROM tbl_cost_centers " |
5444
|
|
|
" WHERE id = %s ", |
5445
|
|
|
(new_values['cost_center']['id'],)) |
5446
|
|
|
row = cursor.fetchone() |
5447
|
|
|
if row is None: |
5448
|
|
|
cursor.close() |
5449
|
|
|
cnx.close() |
5450
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5451
|
|
|
description='API.COST_CENTER_NOT_FOUND') |
5452
|
|
|
|
5453
|
|
|
cursor.execute(" SELECT name " |
5454
|
|
|
" FROM tbl_svgs " |
5455
|
|
|
" WHERE id = %s ", |
5456
|
|
|
(svg_id,)) |
5457
|
|
|
row = cursor.fetchone() |
5458
|
|
|
if row is None: |
5459
|
|
|
cursor.close() |
5460
|
|
|
cnx.close() |
5461
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5462
|
|
|
description='API.SVG_NOT_FOUND') |
5463
|
|
|
|
5464
|
|
|
add_values = (" INSERT INTO tbl_microgrids " |
5465
|
|
|
" (name, uuid, address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
5466
|
|
|
" contact_id, cost_center_id, serial_number, svg_id, is_cost_data_displayed, phase_of_lifecycle, description) " |
5467
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
5468
|
|
|
cursor.execute(add_values, (name, |
5469
|
|
|
str(uuid.uuid4()), |
5470
|
|
|
address, |
5471
|
|
|
postal_code, |
5472
|
|
|
latitude, |
5473
|
|
|
longitude, |
5474
|
|
|
rated_capacity, |
5475
|
|
|
rated_power, |
5476
|
|
|
contact_id, |
5477
|
|
|
cost_center_id, |
5478
|
|
|
serial_number, |
5479
|
|
|
svg_id, |
5480
|
|
|
is_cost_data_displayed, |
5481
|
|
|
phase_of_lifecycle, |
5482
|
|
|
description)) |
5483
|
|
|
new_id = cursor.lastrowid |
5484
|
|
|
cnx.commit() |
5485
|
|
|
cursor.close() |
5486
|
|
|
cnx.close() |
5487
|
|
|
|
5488
|
|
|
resp.status = falcon.HTTP_201 |
5489
|
|
|
resp.location = '/microgrids/' + str(new_id) |
5490
|
|
|
|
5491
|
|
|
|
5492
|
|
|
class MicrogridClone: |
5493
|
|
|
def __init__(self): |
5494
|
|
|
""""Initializes MicrogridClone""" |
5495
|
|
|
pass |
5496
|
|
|
|
5497
|
|
|
@staticmethod |
5498
|
|
|
def on_options(req, resp, id_): |
5499
|
|
|
_ = req |
5500
|
|
|
resp.status = falcon.HTTP_200 |
5501
|
|
|
_ = id_ |
5502
|
|
|
|
5503
|
|
|
@staticmethod |
5504
|
|
|
@user_logger |
5505
|
|
|
def on_post(req, resp, id_): |
5506
|
|
|
admin_control(req) |
5507
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
5508
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5509
|
|
|
description='API.INVALID_MICROGRID_ID') |
5510
|
|
|
|
5511
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
5512
|
|
|
cursor = cnx.cursor() |
5513
|
|
|
|
5514
|
|
|
query = (" SELECT id, name, uuid " |
5515
|
|
|
" FROM tbl_contacts ") |
5516
|
|
|
cursor.execute(query) |
5517
|
|
|
rows_contacts = cursor.fetchall() |
5518
|
|
|
|
5519
|
|
|
contact_dict = dict() |
5520
|
|
|
if rows_contacts is not None and len(rows_contacts) > 0: |
5521
|
|
|
for row in rows_contacts: |
5522
|
|
|
contact_dict[row[0]] = {"id": row[0], |
5523
|
|
|
"name": row[1], |
5524
|
|
|
"uuid": row[2]} |
5525
|
|
|
|
5526
|
|
|
query = (" SELECT id, name, uuid " |
5527
|
|
|
" FROM tbl_cost_centers ") |
5528
|
|
|
cursor.execute(query) |
5529
|
|
|
rows_cost_centers = cursor.fetchall() |
5530
|
|
|
|
5531
|
|
|
cost_center_dict = dict() |
5532
|
|
|
if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
5533
|
|
|
for row in rows_cost_centers: |
5534
|
|
|
cost_center_dict[row[0]] = {"id": row[0], |
5535
|
|
|
"name": row[1], |
5536
|
|
|
"uuid": row[2]} |
5537
|
|
|
|
5538
|
|
|
query = (" SELECT id, name, uuid, " |
5539
|
|
|
" address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
5540
|
|
|
" contact_id, cost_center_id, serial_number, svg_id, is_cost_data_displayed, phase_of_lifecycle, description " |
5541
|
|
|
" FROM tbl_microgrids " |
5542
|
|
|
" WHERE id = %s ") |
5543
|
|
|
cursor.execute(query, (id_,)) |
5544
|
|
|
row = cursor.fetchone() |
5545
|
|
|
|
5546
|
|
|
if row is None: |
5547
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5548
|
|
|
description='API.MICROGRID_NOT_FOUND') |
5549
|
|
|
else: |
5550
|
|
|
meta_result = {"id": row[0], |
5551
|
|
|
"name": row[1], |
5552
|
|
|
"uuid": row[2], |
5553
|
|
|
"address": row[3], |
5554
|
|
|
"postal_code": row[4], |
5555
|
|
|
"latitude": row[5], |
5556
|
|
|
"longitude": row[6], |
5557
|
|
|
"rated_capacity": row[7], |
5558
|
|
|
"rated_power": row[8], |
5559
|
|
|
"contact": contact_dict.get(row[9], None), |
5560
|
|
|
"cost_center": cost_center_dict.get(row[10], None), |
5561
|
|
|
"serial_number": row[11], |
5562
|
|
|
"svg_id": row[12], |
5563
|
|
|
"is_cost_data_displayed": row[13], |
5564
|
|
|
"phase_of_lifecycle": row[14], |
5565
|
|
|
"description": row[15]} |
5566
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
5567
|
|
|
if config.utc_offset[0] == '-': |
5568
|
|
|
timezone_offset = -timezone_offset |
5569
|
|
|
new_name = (str.strip(meta_result['name']) + |
5570
|
|
|
(datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')) |
5571
|
|
|
add_values = (" INSERT INTO tbl_microgrids " |
5572
|
|
|
" (name, uuid, address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
5573
|
|
|
" contact_id, cost_center_id, serial_number, svg_id, is_cost_data_displayed, phase_of_lifecycle, description) " |
5574
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
5575
|
|
|
cursor.execute(add_values, (new_name, |
5576
|
|
|
str(uuid.uuid4()), |
5577
|
|
|
meta_result['address'], |
5578
|
|
|
meta_result['postal_code'], |
5579
|
|
|
meta_result['latitude'], |
5580
|
|
|
meta_result['longitude'], |
5581
|
|
|
meta_result['rated_capacity'], |
5582
|
|
|
meta_result['rated_power'], |
5583
|
|
|
meta_result['contact']['id'], |
5584
|
|
|
meta_result['cost_center']['id'], |
5585
|
|
|
meta_result['serial_number'], |
5586
|
|
|
meta_result['svg_id'], |
5587
|
|
|
meta_result['is_cost_data_displayed'], |
5588
|
|
|
meta_result['phase_of_lifecycle'], |
5589
|
|
|
meta_result['description'])) |
5590
|
|
|
new_id = cursor.lastrowid |
5591
|
|
|
cnx.commit() |
5592
|
|
|
cursor.close() |
5593
|
|
|
cnx.close() |
5594
|
|
|
|
5595
|
|
|
resp.status = falcon.HTTP_201 |
5596
|
|
|
resp.location = '/microgrids/' + str(new_id) |
5597
|
|
|
|
5598
|
|
View Code Duplication |
class MicrogridDataSourceCollection: |
|
|
|
|
5599
|
|
|
def __init__(self): |
5600
|
|
|
pass |
5601
|
|
|
|
5602
|
|
|
@staticmethod |
5603
|
|
|
def on_options(req, resp, id_): |
5604
|
|
|
_ = req |
5605
|
|
|
_ = id_ |
5606
|
|
|
resp.status = falcon.HTTP_200 |
5607
|
|
|
|
5608
|
|
|
@staticmethod |
5609
|
|
|
def on_get(req, resp, id_): |
5610
|
|
|
if 'API-KEY' not in req.headers or \ |
5611
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
5612
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
5613
|
|
|
access_control(req) |
5614
|
|
|
else: |
5615
|
|
|
api_key_control(req) |
|
|
|
|
5616
|
|
|
|
5617
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
5618
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5619
|
|
|
description='API.INVALID_MICROGRID_ID') |
5620
|
|
|
|
5621
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
5622
|
|
|
cursor = cnx.cursor() |
5623
|
|
|
|
5624
|
|
|
cursor.execute(" SELECT name FROM tbl_microgrids WHERE id = %s ", (id_,)) |
5625
|
|
|
if cursor.fetchone() is None: |
5626
|
|
|
cursor.close() |
5627
|
|
|
cnx.close() |
5628
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5629
|
|
|
description='API.MICROGRID_NOT_FOUND') |
5630
|
|
|
|
5631
|
|
|
query = (" SELECT ds.id, ds.name, ds.uuid " |
5632
|
|
|
" FROM tbl_microgrids mg, tbl_microgrids_data_sources mgds, tbl_data_sources ds " |
5633
|
|
|
" WHERE mgds.microgrid_id = mg.id AND ds.id = mgds.data_source_id AND mg.id = %s " |
5634
|
|
|
" ORDER BY ds.id ") |
5635
|
|
|
cursor.execute(query, (id_,)) |
5636
|
|
|
rows = cursor.fetchall() |
5637
|
|
|
|
5638
|
|
|
result = list() |
5639
|
|
|
if rows is not None and len(rows) > 0: |
5640
|
|
|
for row in rows: |
5641
|
|
|
meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
5642
|
|
|
result.append(meta_result) |
5643
|
|
|
|
5644
|
|
|
cursor.close() |
5645
|
|
|
cnx.close() |
5646
|
|
|
|
5647
|
|
|
resp.text = json.dumps(result) |
5648
|
|
|
|
5649
|
|
|
@staticmethod |
5650
|
|
|
@user_logger |
5651
|
|
|
def on_post(req, resp, id_): |
5652
|
|
|
admin_control(req) |
5653
|
|
|
try: |
5654
|
|
|
raw_json = req.stream.read().decode('utf-8') |
5655
|
|
|
except Exception as ex: |
5656
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
5657
|
|
|
title='API.BAD_REQUEST', |
5658
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
5659
|
|
|
|
5660
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
5661
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5662
|
|
|
description='API.INVALID_MICROGRID_ID') |
5663
|
|
|
|
5664
|
|
|
new_values = json.loads(raw_json) |
5665
|
|
|
|
5666
|
|
|
if 'data_source_id' not in new_values['data'].keys() or \ |
5667
|
|
|
not isinstance(new_values['data']['data_source_id'], int) or \ |
5668
|
|
|
new_values['data']['data_source_id'] <= 0: |
5669
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5670
|
|
|
description='API.INVALID_DATA_SOURCE_ID') |
5671
|
|
|
|
5672
|
|
|
data_source_id = new_values['data']['data_source_id'] |
5673
|
|
|
|
5674
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
5675
|
|
|
cursor = cnx.cursor() |
5676
|
|
|
|
5677
|
|
|
cursor.execute(" SELECT name FROM tbl_microgrids WHERE id = %s ", (id_,)) |
5678
|
|
|
if cursor.fetchone() is None: |
5679
|
|
|
cursor.close() |
5680
|
|
|
cnx.close() |
5681
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5682
|
|
|
description='API.MICROGRID_NOT_FOUND') |
5683
|
|
|
|
5684
|
|
|
cursor.execute(" SELECT name FROM tbl_data_sources WHERE id = %s ", (data_source_id,)) |
5685
|
|
|
if cursor.fetchone() is None: |
5686
|
|
|
cursor.close() |
5687
|
|
|
cnx.close() |
5688
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5689
|
|
|
description='API.DATA_SOURCE_NOT_FOUND') |
5690
|
|
|
|
5691
|
|
|
cursor.execute(" SELECT id " |
5692
|
|
|
" FROM tbl_microgrids_data_sources " |
5693
|
|
|
" WHERE microgrid_id = %s AND data_source_id = %s", (id_, data_source_id)) |
5694
|
|
|
if cursor.fetchone() is not None: |
5695
|
|
|
cursor.close() |
5696
|
|
|
cnx.close() |
5697
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
5698
|
|
|
description='API.MICROGRID_DATA_SOURCE_RELATION_EXISTS') |
5699
|
|
|
|
5700
|
|
|
cursor.execute(" INSERT INTO tbl_microgrids_data_sources (microgrid_id, data_source_id) " |
5701
|
|
|
" VALUES (%s, %s) ", (id_, data_source_id)) |
5702
|
|
|
cnx.commit() |
5703
|
|
|
cursor.close() |
5704
|
|
|
cnx.close() |
5705
|
|
|
|
5706
|
|
|
resp.status = falcon.HTTP_201 |
5707
|
|
|
resp.location = '/microgrids/' + str(id_) + '/datasources/' + str(data_source_id) |
5708
|
|
|
|
5709
|
|
View Code Duplication |
class MicrogridDataSourceItem: |
|
|
|
|
5710
|
|
|
def __init__(self): |
5711
|
|
|
pass |
5712
|
|
|
|
5713
|
|
|
@staticmethod |
5714
|
|
|
def on_options(req, resp, id_, dsid): |
5715
|
|
|
_ = req |
5716
|
|
|
_ = id_ |
5717
|
|
|
_ = dsid |
5718
|
|
|
resp.status = falcon.HTTP_200 |
5719
|
|
|
|
5720
|
|
|
@staticmethod |
5721
|
|
|
@user_logger |
5722
|
|
|
def on_delete(req, resp, id_, dsid): |
5723
|
|
|
admin_control(req) |
5724
|
|
|
|
5725
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
5726
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5727
|
|
|
description='API.INVALID_MICROGRID_ID') |
5728
|
|
|
|
5729
|
|
|
if not dsid.isdigit() or int(dsid) <= 0: |
5730
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
5731
|
|
|
description='API.INVALID_DATA_SOURCE_ID') |
5732
|
|
|
|
5733
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
5734
|
|
|
cursor = cnx.cursor() |
5735
|
|
|
|
5736
|
|
|
cursor.execute(" SELECT name FROM tbl_microgrids WHERE id = %s ", (id_,)) |
5737
|
|
|
if cursor.fetchone() is None: |
5738
|
|
|
cursor.close() |
5739
|
|
|
cnx.close() |
5740
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5741
|
|
|
description='API.MICROGRID_NOT_FOUND') |
5742
|
|
|
|
5743
|
|
|
cursor.execute(" SELECT name FROM tbl_data_sources WHERE id = %s ", (dsid,)) |
5744
|
|
|
if cursor.fetchone() is None: |
5745
|
|
|
cursor.close() |
5746
|
|
|
cnx.close() |
5747
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5748
|
|
|
description='API.DATA_SOURCE_NOT_FOUND') |
5749
|
|
|
|
5750
|
|
|
cursor.execute(" SELECT id " |
5751
|
|
|
" FROM tbl_microgrids_data_sources " |
5752
|
|
|
" WHERE microgrid_id = %s AND data_source_id = %s ", (id_, dsid)) |
5753
|
|
|
if cursor.fetchone() is None: |
5754
|
|
|
cursor.close() |
5755
|
|
|
cnx.close() |
5756
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
5757
|
|
|
description='API.MICROGRID_DATA_SOURCE_RELATION_NOT_FOUND') |
5758
|
|
|
|
5759
|
|
|
cursor.execute(" DELETE FROM tbl_microgrids_data_sources " |
5760
|
|
|
" WHERE microgrid_id = %s AND data_source_id = %s ", (id_, dsid)) |
5761
|
|
|
cnx.commit() |
5762
|
|
|
cursor.close() |
5763
|
|
|
cnx.close() |
5764
|
|
|
|
5765
|
|
|
resp.status = falcon.HTTP_204 |
5766
|
|
|
|