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