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