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