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