Total Complexity | 563 |
Total Lines | 2930 |
Duplicated Lines | 54.06 % |
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.shopfloor 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 core.useractivity import user_logger, admin_control, access_control, api_key_control |
||
7 | import config |
||
8 | |||
9 | |||
10 | class ShopfloorCollection: |
||
11 | def __init__(self): |
||
12 | """Initializes ShopfloorCollection""" |
||
13 | pass |
||
14 | |||
15 | @staticmethod |
||
16 | def on_options(req, resp): |
||
17 | resp.status = falcon.HTTP_200 |
||
18 | |||
19 | @staticmethod |
||
20 | def on_get(req, resp): |
||
21 | if 'API-KEY' not in req.headers or \ |
||
22 | not isinstance(req.headers['API-KEY'], str) or \ |
||
23 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
24 | access_control(req) |
||
25 | else: |
||
26 | api_key_control(req) |
||
27 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
28 | cursor = cnx.cursor() |
||
29 | |||
30 | query = (" SELECT id, name, uuid " |
||
31 | " FROM tbl_contacts ") |
||
32 | cursor.execute(query) |
||
33 | rows_contacts = cursor.fetchall() |
||
34 | |||
35 | contact_dict = dict() |
||
36 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
37 | for row in rows_contacts: |
||
38 | contact_dict[row[0]] = {"id": row[0], |
||
39 | "name": row[1], |
||
40 | "uuid": row[2]} |
||
41 | |||
42 | query = (" SELECT id, name, uuid " |
||
43 | " FROM tbl_cost_centers ") |
||
44 | cursor.execute(query) |
||
45 | rows_cost_centers = cursor.fetchall() |
||
46 | |||
47 | cost_center_dict = dict() |
||
48 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
49 | for row in rows_cost_centers: |
||
50 | cost_center_dict[row[0]] = {"id": row[0], |
||
51 | "name": row[1], |
||
52 | "uuid": row[2]} |
||
53 | |||
54 | query = (" SELECT id, name, uuid, " |
||
55 | " area, is_input_counted, " |
||
56 | " contact_id, cost_center_id, description " |
||
57 | " FROM tbl_shopfloors " |
||
58 | " ORDER BY id ") |
||
59 | cursor.execute(query) |
||
60 | rows_shopfloors = cursor.fetchall() |
||
61 | |||
62 | result = list() |
||
63 | if rows_shopfloors is not None and len(rows_shopfloors) > 0: |
||
64 | for row in rows_shopfloors: |
||
65 | meta_result = {"id": row[0], |
||
66 | "name": row[1], |
||
67 | "uuid": row[2], |
||
68 | "area": row[3], |
||
69 | "is_input_counted": bool(row[4]), |
||
70 | "contact": contact_dict.get(row[5], None), |
||
71 | "cost_center": cost_center_dict.get(row[6], None), |
||
72 | "description": row[7], |
||
73 | "qrcode": "shopfloor:" + row[2]} |
||
74 | result.append(meta_result) |
||
75 | |||
76 | cursor.close() |
||
77 | cnx.close() |
||
78 | resp.text = json.dumps(result) |
||
79 | |||
80 | @staticmethod |
||
81 | @user_logger |
||
82 | def on_post(req, resp): |
||
83 | """Handles POST requests""" |
||
84 | admin_control(req) |
||
85 | try: |
||
86 | raw_json = req.stream.read().decode('utf-8') |
||
87 | except Exception as ex: |
||
88 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
89 | title='API.BAD_REQUEST', |
||
90 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
91 | |||
92 | new_values = json.loads(raw_json) |
||
93 | |||
94 | if 'name' not in new_values['data'].keys() or \ |
||
95 | not isinstance(new_values['data']['name'], str) or \ |
||
96 | len(str.strip(new_values['data']['name'])) == 0: |
||
97 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
98 | description='API.INVALID_SHOPFLOOR_NAME') |
||
99 | name = str.strip(new_values['data']['name']) |
||
100 | |||
101 | if 'area' not in new_values['data'].keys() or \ |
||
102 | not (isinstance(new_values['data']['area'], float) or |
||
103 | isinstance(new_values['data']['area'], int)) or \ |
||
104 | new_values['data']['area'] <= 0.0: |
||
105 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
106 | description='API.INVALID_AREA_VALUE') |
||
107 | area = new_values['data']['area'] |
||
108 | |||
109 | if 'is_input_counted' not in new_values['data'].keys() or \ |
||
110 | not isinstance(new_values['data']['is_input_counted'], bool): |
||
111 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
112 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
113 | is_input_counted = new_values['data']['is_input_counted'] |
||
114 | |||
115 | if 'contact_id' in new_values['data'].keys(): |
||
116 | if new_values['data']['contact_id'] <= 0: |
||
117 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
118 | description='API.INVALID_CONTACT_ID') |
||
119 | contact_id = new_values['data']['contact_id'] |
||
120 | else: |
||
121 | contact_id = None |
||
122 | |||
123 | if 'cost_center_id' in new_values['data'].keys(): |
||
124 | if new_values['data']['cost_center_id'] <= 0: |
||
125 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
126 | description='API.INVALID_COST_CENTER_ID') |
||
127 | cost_center_id = new_values['data']['cost_center_id'] |
||
128 | else: |
||
129 | cost_center_id = None |
||
130 | |||
131 | if 'description' in new_values['data'].keys() and \ |
||
132 | new_values['data']['description'] is not None and \ |
||
133 | len(str(new_values['data']['description'])) > 0: |
||
134 | description = str.strip(new_values['data']['description']) |
||
135 | else: |
||
136 | description = None |
||
137 | |||
138 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
139 | cursor = cnx.cursor() |
||
140 | |||
141 | cursor.execute(" SELECT name " |
||
142 | " FROM tbl_shopfloors " |
||
143 | " WHERE name = %s ", (name,)) |
||
144 | if cursor.fetchone() is not None: |
||
145 | cursor.close() |
||
146 | cnx.close() |
||
147 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
148 | description='API.SHOPFLOOR_NAME_IS_ALREADY_IN_USE') |
||
149 | |||
150 | View Code Duplication | if contact_id is not None: |
|
|
|||
151 | cursor.execute(" SELECT name " |
||
152 | " FROM tbl_contacts " |
||
153 | " WHERE id = %s ", |
||
154 | (new_values['data']['contact_id'],)) |
||
155 | row = cursor.fetchone() |
||
156 | if row is None: |
||
157 | cursor.close() |
||
158 | cnx.close() |
||
159 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
160 | description='API.CONTACT_NOT_FOUND') |
||
161 | |||
162 | if cost_center_id is not None: |
||
163 | cursor.execute(" SELECT name " |
||
164 | " FROM tbl_cost_centers " |
||
165 | " WHERE id = %s ", |
||
166 | (new_values['data']['cost_center_id'],)) |
||
167 | row = cursor.fetchone() |
||
168 | if row is None: |
||
169 | cursor.close() |
||
170 | cnx.close() |
||
171 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
172 | description='API.COST_CENTER_NOT_FOUND') |
||
173 | |||
174 | add_values = (" INSERT INTO tbl_shopfloors " |
||
175 | " (name, uuid, area, is_input_counted, " |
||
176 | " contact_id, cost_center_id, description) " |
||
177 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
178 | cursor.execute(add_values, (name, |
||
179 | str(uuid.uuid4()), |
||
180 | area, |
||
181 | is_input_counted, |
||
182 | contact_id, |
||
183 | cost_center_id, |
||
184 | description)) |
||
185 | new_id = cursor.lastrowid |
||
186 | cnx.commit() |
||
187 | cursor.close() |
||
188 | cnx.close() |
||
189 | |||
190 | resp.status = falcon.HTTP_201 |
||
191 | resp.location = '/shopfloors/' + str(new_id) |
||
192 | |||
193 | |||
194 | class ShopfloorItem: |
||
195 | def __init__(self): |
||
196 | """Initializes ShopfloorItem""" |
||
197 | pass |
||
198 | |||
199 | @staticmethod |
||
200 | def on_options(req, resp, id_): |
||
201 | resp.status = falcon.HTTP_200 |
||
202 | |||
203 | View Code Duplication | @staticmethod |
|
204 | def on_get(req, resp, id_): |
||
205 | if 'API-KEY' not in req.headers or \ |
||
206 | not isinstance(req.headers['API-KEY'], str) or \ |
||
207 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
208 | access_control(req) |
||
209 | else: |
||
210 | api_key_control(req) |
||
211 | if not id_.isdigit() or int(id_) <= 0: |
||
212 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
213 | description='API.INVALID_SHOPFLOOR_ID') |
||
214 | |||
215 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
216 | cursor = cnx.cursor() |
||
217 | |||
218 | query = (" SELECT id, name, uuid " |
||
219 | " FROM tbl_contacts ") |
||
220 | cursor.execute(query) |
||
221 | rows_contacts = cursor.fetchall() |
||
222 | |||
223 | contact_dict = dict() |
||
224 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
225 | for row in rows_contacts: |
||
226 | contact_dict[row[0]] = {"id": row[0], |
||
227 | "name": row[1], |
||
228 | "uuid": row[2]} |
||
229 | |||
230 | query = (" SELECT id, name, uuid " |
||
231 | " FROM tbl_cost_centers ") |
||
232 | cursor.execute(query) |
||
233 | rows_cost_centers = cursor.fetchall() |
||
234 | |||
235 | cost_center_dict = dict() |
||
236 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
237 | for row in rows_cost_centers: |
||
238 | cost_center_dict[row[0]] = {"id": row[0], |
||
239 | "name": row[1], |
||
240 | "uuid": row[2]} |
||
241 | |||
242 | query = (" SELECT id, name, uuid, " |
||
243 | " area, is_input_counted, contact_id, cost_center_id, description " |
||
244 | " FROM tbl_shopfloors " |
||
245 | " WHERE id = %s ") |
||
246 | cursor.execute(query, (id_,)) |
||
247 | row = cursor.fetchone() |
||
248 | cursor.close() |
||
249 | cnx.close() |
||
250 | |||
251 | if row is None: |
||
252 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
253 | description='API.SHOPFLOOR_NOT_FOUND') |
||
254 | else: |
||
255 | meta_result = {"id": row[0], |
||
256 | "name": row[1], |
||
257 | "uuid": row[2], |
||
258 | "area": row[3], |
||
259 | "is_input_counted": bool(row[4]), |
||
260 | "contact": contact_dict.get(row[5], None), |
||
261 | "cost_center": cost_center_dict.get(row[6], None), |
||
262 | "description": row[7], |
||
263 | "qrcode": "shopfloor:" + row[2]} |
||
264 | |||
265 | resp.text = json.dumps(meta_result) |
||
266 | |||
267 | View Code Duplication | @staticmethod |
|
268 | @user_logger |
||
269 | def on_delete(req, resp, id_): |
||
270 | admin_control(req) |
||
271 | if not id_.isdigit() or int(id_) <= 0: |
||
272 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
273 | description='API.INVALID_SHOPFLOOR_ID') |
||
274 | |||
275 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
276 | cursor = cnx.cursor() |
||
277 | |||
278 | cursor.execute(" SELECT name " |
||
279 | " FROM tbl_shopfloors " |
||
280 | " WHERE id = %s ", (id_,)) |
||
281 | if cursor.fetchone() is None: |
||
282 | cursor.close() |
||
283 | cnx.close() |
||
284 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
285 | description='API.SHOPFLOOR_NOT_FOUND') |
||
286 | |||
287 | # check relation with spaces |
||
288 | cursor.execute(" SELECT space_id " |
||
289 | " FROM tbl_spaces_shopfloors " |
||
290 | " WHERE shopfloor_id = %s ", |
||
291 | (id_,)) |
||
292 | rows_spaces = cursor.fetchall() |
||
293 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
294 | cursor.close() |
||
295 | cnx.close() |
||
296 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
297 | title='API.BAD_REQUEST', |
||
298 | description='API.THERE_IS_RELATION_WITH_SPACES') |
||
299 | |||
300 | # delete relation with equipments |
||
301 | cursor.execute(" DELETE FROM tbl_shopfloors_equipments WHERE shopfloor_id = %s ", (id_,)) |
||
302 | |||
303 | # delete relation with meters |
||
304 | cursor.execute(" DELETE FROM tbl_shopfloors_meters WHERE shopfloor_id = %s ", (id_,)) |
||
305 | |||
306 | # delete relation with offline meters |
||
307 | cursor.execute(" DELETE FROM tbl_shopfloors_offline_meters WHERE shopfloor_id = %s ", (id_,)) |
||
308 | |||
309 | # delete relation with points |
||
310 | cursor.execute(" DELETE FROM tbl_shopfloors_points WHERE shopfloor_id = %s ", (id_,)) |
||
311 | |||
312 | # delete relation with sensor |
||
313 | cursor.execute(" DELETE FROM tbl_shopfloors_sensors WHERE shopfloor_id = %s ", (id_,)) |
||
314 | |||
315 | # delete relation with virtual meter |
||
316 | cursor.execute(" DELETE FROM tbl_shopfloors_virtual_meters WHERE shopfloor_id = %s ", (id_,)) |
||
317 | |||
318 | # delete relation with command |
||
319 | cursor.execute(" DELETE FROM tbl_shopfloors_commands WHERE shopfloor_id = %s ", (id_,)) |
||
320 | |||
321 | # delete relation with working calendar |
||
322 | cursor.execute(" DELETE FROM tbl_shopfloors_working_calendars WHERE shopfloor_id = %s ", (id_,)) |
||
323 | |||
324 | cursor.execute(" DELETE FROM tbl_shopfloors WHERE id = %s ", (id_,)) |
||
325 | cnx.commit() |
||
326 | |||
327 | cursor.close() |
||
328 | cnx.close() |
||
329 | |||
330 | resp.status = falcon.HTTP_204 |
||
331 | |||
332 | @staticmethod |
||
333 | @user_logger |
||
334 | def on_put(req, resp, id_): |
||
335 | """Handles PUT requests""" |
||
336 | admin_control(req) |
||
337 | try: |
||
338 | raw_json = req.stream.read().decode('utf-8') |
||
339 | except Exception as ex: |
||
340 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
341 | title='API.BAD_REQUEST', |
||
342 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
343 | |||
344 | if not id_.isdigit() or int(id_) <= 0: |
||
345 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
346 | description='API.INVALID_SHOPFLOOR_ID') |
||
347 | |||
348 | new_values = json.loads(raw_json) |
||
349 | |||
350 | if 'name' not in new_values['data'].keys() or \ |
||
351 | not isinstance(new_values['data']['name'], str) or \ |
||
352 | len(str.strip(new_values['data']['name'])) == 0: |
||
353 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
354 | description='API.INVALID_SHOPFLOOR_NAME') |
||
355 | name = str.strip(new_values['data']['name']) |
||
356 | |||
357 | if 'area' not in new_values['data'].keys() or \ |
||
358 | not (isinstance(new_values['data']['area'], float) or |
||
359 | isinstance(new_values['data']['area'], int)) or \ |
||
360 | new_values['data']['area'] <= 0.0: |
||
361 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
362 | description='API.INVALID_AREA_VALUE') |
||
363 | area = new_values['data']['area'] |
||
364 | |||
365 | if 'is_input_counted' not in new_values['data'].keys() or \ |
||
366 | not isinstance(new_values['data']['is_input_counted'], bool): |
||
367 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
368 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
369 | is_input_counted = new_values['data']['is_input_counted'] |
||
370 | |||
371 | if 'contact_id' in new_values['data'].keys(): |
||
372 | if new_values['data']['contact_id'] <= 0: |
||
373 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
374 | description='API.INVALID_CONTACT_ID') |
||
375 | contact_id = new_values['data']['contact_id'] |
||
376 | else: |
||
377 | contact_id = None |
||
378 | |||
379 | if 'cost_center_id' in new_values['data'].keys(): |
||
380 | if new_values['data']['cost_center_id'] <= 0: |
||
381 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
382 | description='API.INVALID_COST_CENTER_ID') |
||
383 | cost_center_id = new_values['data']['cost_center_id'] |
||
384 | else: |
||
385 | cost_center_id = None |
||
386 | |||
387 | if 'description' in new_values['data'].keys() and \ |
||
388 | new_values['data']['description'] is not None and \ |
||
389 | len(str(new_values['data']['description'])) > 0: |
||
390 | description = str.strip(new_values['data']['description']) |
||
391 | else: |
||
392 | description = None |
||
393 | |||
394 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
395 | cursor = cnx.cursor() |
||
396 | |||
397 | cursor.execute(" SELECT name " |
||
398 | " FROM tbl_shopfloors " |
||
399 | " WHERE id = %s ", (id_,)) |
||
400 | if cursor.fetchone() is None: |
||
401 | cursor.close() |
||
402 | cnx.close() |
||
403 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
404 | description='API.SHOPFLOOR_NOT_FOUND') |
||
405 | |||
406 | cursor.execute(" SELECT name " |
||
407 | " FROM tbl_shopfloors " |
||
408 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
409 | if cursor.fetchone() is not None: |
||
410 | cursor.close() |
||
411 | cnx.close() |
||
412 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
413 | description='API.SHOPFLOOR_NAME_IS_ALREADY_IN_USE') |
||
414 | |||
415 | View Code Duplication | if contact_id is not None: |
|
416 | cursor.execute(" SELECT name " |
||
417 | " FROM tbl_contacts " |
||
418 | " WHERE id = %s ", |
||
419 | (new_values['data']['contact_id'],)) |
||
420 | row = cursor.fetchone() |
||
421 | if row is None: |
||
422 | cursor.close() |
||
423 | cnx.close() |
||
424 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
425 | description='API.CONTACT_NOT_FOUND') |
||
426 | |||
427 | if cost_center_id is not None: |
||
428 | cursor.execute(" SELECT name " |
||
429 | " FROM tbl_cost_centers " |
||
430 | " WHERE id = %s ", |
||
431 | (new_values['data']['cost_center_id'],)) |
||
432 | row = cursor.fetchone() |
||
433 | if row is None: |
||
434 | cursor.close() |
||
435 | cnx.close() |
||
436 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
437 | description='API.COST_CENTER_NOT_FOUND') |
||
438 | |||
439 | update_row = (" UPDATE tbl_shopfloors " |
||
440 | " SET name = %s, area = %s, is_input_counted = %s, contact_id = %s, cost_center_id = %s, " |
||
441 | " description = %s " |
||
442 | " WHERE id = %s ") |
||
443 | cursor.execute(update_row, (name, |
||
444 | area, |
||
445 | is_input_counted, |
||
446 | contact_id, |
||
447 | cost_center_id, |
||
448 | description, |
||
449 | id_)) |
||
450 | cnx.commit() |
||
451 | |||
452 | cursor.close() |
||
453 | cnx.close() |
||
454 | |||
455 | resp.status = falcon.HTTP_200 |
||
456 | |||
457 | |||
458 | View Code Duplication | class ShopfloorEquipmentCollection: |
|
459 | def __init__(self): |
||
460 | """Initializes ShopfloorEquipmentCollection""" |
||
461 | pass |
||
462 | |||
463 | @staticmethod |
||
464 | def on_options(req, resp, id_): |
||
465 | resp.status = falcon.HTTP_200 |
||
466 | |||
467 | @staticmethod |
||
468 | def on_get(req, resp, id_): |
||
469 | if 'API-KEY' not in req.headers or \ |
||
470 | not isinstance(req.headers['API-KEY'], str) or \ |
||
471 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
472 | access_control(req) |
||
473 | else: |
||
474 | api_key_control(req) |
||
475 | if not id_.isdigit() or int(id_) <= 0: |
||
476 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
477 | description='API.INVALID_SHOPFLOOR_ID') |
||
478 | |||
479 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
480 | cursor = cnx.cursor() |
||
481 | |||
482 | cursor.execute(" SELECT name " |
||
483 | " FROM tbl_shopfloors " |
||
484 | " WHERE id = %s ", (id_,)) |
||
485 | if cursor.fetchone() is None: |
||
486 | cursor.close() |
||
487 | cnx.close() |
||
488 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
489 | description='API.SHOPFLOOR_NOT_FOUND') |
||
490 | |||
491 | query = (" SELECT e.id, e.name, e.uuid " |
||
492 | " FROM tbl_shopfloors s, tbl_shopfloors_equipments se, tbl_equipments e " |
||
493 | " WHERE se.shopfloor_id = s.id AND e.id = se.equipment_id AND s.id = %s " |
||
494 | " ORDER BY e.id ") |
||
495 | cursor.execute(query, (id_,)) |
||
496 | rows = cursor.fetchall() |
||
497 | |||
498 | result = list() |
||
499 | if rows is not None and len(rows) > 0: |
||
500 | for row in rows: |
||
501 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
502 | result.append(meta_result) |
||
503 | |||
504 | resp.text = json.dumps(result) |
||
505 | |||
506 | @staticmethod |
||
507 | @user_logger |
||
508 | def on_post(req, resp, id_): |
||
509 | """Handles POST requests""" |
||
510 | admin_control(req) |
||
511 | try: |
||
512 | raw_json = req.stream.read().decode('utf-8') |
||
513 | except Exception as ex: |
||
514 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
515 | title='API.BAD_REQUEST', |
||
516 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
517 | |||
518 | if not id_.isdigit() or int(id_) <= 0: |
||
519 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
520 | description='API.INVALID_SHOPFLOOR_ID') |
||
521 | |||
522 | new_values = json.loads(raw_json) |
||
523 | |||
524 | if 'equipment_id' not in new_values['data'].keys() or \ |
||
525 | not isinstance(new_values['data']['equipment_id'], int) or \ |
||
526 | new_values['data']['equipment_id'] <= 0: |
||
527 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
528 | description='API.INVALID_EQUIPMENT_ID') |
||
529 | equipment_id = new_values['data']['equipment_id'] |
||
530 | |||
531 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
532 | cursor = cnx.cursor() |
||
533 | |||
534 | cursor.execute(" SELECT name " |
||
535 | " from tbl_shopfloors " |
||
536 | " WHERE id = %s ", (id_,)) |
||
537 | if cursor.fetchone() is None: |
||
538 | cursor.close() |
||
539 | cnx.close() |
||
540 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
541 | description='API.SHOPFLOOR_NOT_FOUND') |
||
542 | |||
543 | cursor.execute(" SELECT name " |
||
544 | " FROM tbl_equipments " |
||
545 | " WHERE id = %s ", (equipment_id,)) |
||
546 | if cursor.fetchone() is None: |
||
547 | cursor.close() |
||
548 | cnx.close() |
||
549 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
550 | description='API.EQUIPMENT_NOT_FOUND') |
||
551 | |||
552 | query = (" SELECT id " |
||
553 | " FROM tbl_shopfloors_equipments " |
||
554 | " WHERE shopfloor_id = %s AND equipment_id = %s") |
||
555 | cursor.execute(query, (id_, equipment_id,)) |
||
556 | if cursor.fetchone() is not None: |
||
557 | cursor.close() |
||
558 | cnx.close() |
||
559 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
560 | description='API.SHOPFLOOR_EQUIPMENT_RELATION_EXISTS') |
||
561 | |||
562 | add_row = (" INSERT INTO tbl_shopfloors_equipments (shopfloor_id, equipment_id) " |
||
563 | " VALUES (%s, %s) ") |
||
564 | cursor.execute(add_row, (id_, equipment_id,)) |
||
565 | cnx.commit() |
||
566 | cursor.close() |
||
567 | cnx.close() |
||
568 | |||
569 | resp.status = falcon.HTTP_201 |
||
570 | resp.location = '/shopfloors/' + str(id_) + '/equipments/' + str(equipment_id) |
||
571 | |||
572 | |||
573 | View Code Duplication | class ShopfloorEquipmentItem: |
|
574 | def __init__(self): |
||
575 | """Initializes ShopfloorEquipmentItem""" |
||
576 | pass |
||
577 | |||
578 | @staticmethod |
||
579 | def on_options(req, resp, id_, eid): |
||
580 | resp.status = falcon.HTTP_200 |
||
581 | |||
582 | @staticmethod |
||
583 | @user_logger |
||
584 | def on_delete(req, resp, id_, eid): |
||
585 | admin_control(req) |
||
586 | if not id_.isdigit() or int(id_) <= 0: |
||
587 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
588 | description='API.INVALID_SHOPFLOOR_ID') |
||
589 | |||
590 | if not eid.isdigit() or int(eid) <= 0: |
||
591 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
592 | description='API.INVALID_EQUIPMENT_ID') |
||
593 | |||
594 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
595 | cursor = cnx.cursor() |
||
596 | |||
597 | cursor.execute(" SELECT name " |
||
598 | " FROM tbl_shopfloors " |
||
599 | " WHERE id = %s ", (id_,)) |
||
600 | if cursor.fetchone() is None: |
||
601 | cursor.close() |
||
602 | cnx.close() |
||
603 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
604 | description='API.SHOPFLOOR_NOT_FOUND') |
||
605 | |||
606 | cursor.execute(" SELECT name " |
||
607 | " FROM tbl_equipments " |
||
608 | " WHERE id = %s ", (eid,)) |
||
609 | if cursor.fetchone() is None: |
||
610 | cursor.close() |
||
611 | cnx.close() |
||
612 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
613 | description='API.EQUIPMENT_NOT_FOUND') |
||
614 | |||
615 | cursor.execute(" SELECT id " |
||
616 | " FROM tbl_shopfloors_equipments " |
||
617 | " WHERE shopfloor_id = %s AND equipment_id = %s ", (id_, eid)) |
||
618 | if cursor.fetchone() is None: |
||
619 | cursor.close() |
||
620 | cnx.close() |
||
621 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
622 | description='API.SHOPFLOOR_EQUIPMENT_RELATION_NOT_FOUND') |
||
623 | |||
624 | cursor.execute(" DELETE FROM tbl_shopfloors_equipments " |
||
625 | " WHERE shopfloor_id = %s AND equipment_id = %s ", (id_, eid)) |
||
626 | cnx.commit() |
||
627 | |||
628 | cursor.close() |
||
629 | cnx.close() |
||
630 | |||
631 | resp.status = falcon.HTTP_204 |
||
632 | |||
633 | |||
634 | View Code Duplication | class ShopfloorMeterCollection: |
|
635 | def __init__(self): |
||
636 | """Initializes ShopfloorMeterCollection""" |
||
637 | pass |
||
638 | |||
639 | @staticmethod |
||
640 | def on_options(req, resp, id_): |
||
641 | resp.status = falcon.HTTP_200 |
||
642 | |||
643 | @staticmethod |
||
644 | def on_get(req, resp, id_): |
||
645 | if 'API-KEY' not in req.headers or \ |
||
646 | not isinstance(req.headers['API-KEY'], str) or \ |
||
647 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
648 | access_control(req) |
||
649 | else: |
||
650 | api_key_control(req) |
||
651 | if not id_.isdigit() or int(id_) <= 0: |
||
652 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
653 | description='API.INVALID_SHOPFLOOR_ID') |
||
654 | |||
655 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
656 | cursor = cnx.cursor() |
||
657 | |||
658 | cursor.execute(" SELECT name " |
||
659 | " FROM tbl_shopfloors " |
||
660 | " WHERE id = %s ", (id_,)) |
||
661 | if cursor.fetchone() is None: |
||
662 | cursor.close() |
||
663 | cnx.close() |
||
664 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
665 | description='API.SHOPFLOOR_NOT_FOUND') |
||
666 | |||
667 | query = (" SELECT id, name, uuid " |
||
668 | " FROM tbl_energy_categories ") |
||
669 | cursor.execute(query) |
||
670 | rows_energy_categories = cursor.fetchall() |
||
671 | |||
672 | energy_category_dict = dict() |
||
673 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
674 | for row in rows_energy_categories: |
||
675 | energy_category_dict[row[0]] = {"id": row[0], |
||
676 | "name": row[1], |
||
677 | "uuid": row[2]} |
||
678 | |||
679 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
680 | " FROM tbl_shopfloors s, tbl_shopfloors_meters sm, tbl_meters m " |
||
681 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.meter_id AND s.id = %s " |
||
682 | " ORDER BY m.id ") |
||
683 | cursor.execute(query, (id_,)) |
||
684 | rows = cursor.fetchall() |
||
685 | |||
686 | result = list() |
||
687 | if rows is not None and len(rows) > 0: |
||
688 | for row in rows: |
||
689 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
690 | "energy_category": energy_category_dict.get(row[3], None)} |
||
691 | result.append(meta_result) |
||
692 | |||
693 | resp.text = json.dumps(result) |
||
694 | |||
695 | @staticmethod |
||
696 | @user_logger |
||
697 | def on_post(req, resp, id_): |
||
698 | """Handles POST requests""" |
||
699 | admin_control(req) |
||
700 | try: |
||
701 | raw_json = req.stream.read().decode('utf-8') |
||
702 | except Exception as ex: |
||
703 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
704 | title='API.BAD_REQUEST', |
||
705 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
706 | |||
707 | if not id_.isdigit() or int(id_) <= 0: |
||
708 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
709 | description='API.INVALID_SHOPFLOOR_ID') |
||
710 | |||
711 | new_values = json.loads(raw_json) |
||
712 | |||
713 | if 'meter_id' not in new_values['data'].keys() or \ |
||
714 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
715 | new_values['data']['meter_id'] <= 0: |
||
716 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
717 | description='API.INVALID_METER_ID') |
||
718 | meter_id = new_values['data']['meter_id'] |
||
719 | |||
720 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
721 | cursor = cnx.cursor() |
||
722 | |||
723 | cursor.execute(" SELECT name " |
||
724 | " from tbl_shopfloors " |
||
725 | " WHERE id = %s ", (id_,)) |
||
726 | if cursor.fetchone() is None: |
||
727 | cursor.close() |
||
728 | cnx.close() |
||
729 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
730 | description='API.SHOPFLOOR_NOT_FOUND') |
||
731 | |||
732 | cursor.execute(" SELECT name " |
||
733 | " FROM tbl_meters " |
||
734 | " WHERE id = %s ", (meter_id,)) |
||
735 | if cursor.fetchone() is None: |
||
736 | cursor.close() |
||
737 | cnx.close() |
||
738 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
739 | description='API.METER_NOT_FOUND') |
||
740 | |||
741 | query = (" SELECT id " |
||
742 | " FROM tbl_shopfloors_meters " |
||
743 | " WHERE shopfloor_id = %s AND meter_id = %s") |
||
744 | cursor.execute(query, (id_, meter_id,)) |
||
745 | if cursor.fetchone() is not None: |
||
746 | cursor.close() |
||
747 | cnx.close() |
||
748 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
749 | description='API.SHOPFLOOR_METER_RELATION_EXISTS') |
||
750 | |||
751 | add_row = (" INSERT INTO tbl_shopfloors_meters (shopfloor_id, meter_id) " |
||
752 | " VALUES (%s, %s) ") |
||
753 | cursor.execute(add_row, (id_, meter_id,)) |
||
754 | cnx.commit() |
||
755 | cursor.close() |
||
756 | cnx.close() |
||
757 | |||
758 | resp.status = falcon.HTTP_201 |
||
759 | resp.location = '/shopfloors/' + str(id_) + '/meters/' + str(meter_id) |
||
760 | |||
761 | |||
762 | View Code Duplication | class ShopfloorMeterItem: |
|
763 | def __init__(self): |
||
764 | """Initializes ShopfloorMeterItem""" |
||
765 | pass |
||
766 | |||
767 | @staticmethod |
||
768 | def on_options(req, resp, id_, mid): |
||
769 | resp.status = falcon.HTTP_200 |
||
770 | |||
771 | @staticmethod |
||
772 | @user_logger |
||
773 | def on_delete(req, resp, id_, mid): |
||
774 | admin_control(req) |
||
775 | if not id_.isdigit() or int(id_) <= 0: |
||
776 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
777 | description='API.INVALID_SHOPFLOOR_ID') |
||
778 | |||
779 | if not mid.isdigit() or int(mid) <= 0: |
||
780 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
781 | description='API.INVALID_METER_ID') |
||
782 | |||
783 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
784 | cursor = cnx.cursor() |
||
785 | |||
786 | cursor.execute(" SELECT name " |
||
787 | " FROM tbl_shopfloors " |
||
788 | " WHERE id = %s ", (id_,)) |
||
789 | if cursor.fetchone() is None: |
||
790 | cursor.close() |
||
791 | cnx.close() |
||
792 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
793 | description='API.SHOPFLOOR_NOT_FOUND') |
||
794 | |||
795 | cursor.execute(" SELECT name " |
||
796 | " FROM tbl_meters " |
||
797 | " WHERE id = %s ", (mid,)) |
||
798 | if cursor.fetchone() is None: |
||
799 | cursor.close() |
||
800 | cnx.close() |
||
801 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
802 | description='API.METER_NOT_FOUND') |
||
803 | |||
804 | cursor.execute(" SELECT id " |
||
805 | " FROM tbl_shopfloors_meters " |
||
806 | " WHERE shopfloor_id = %s AND meter_id = %s ", (id_, mid)) |
||
807 | if cursor.fetchone() is None: |
||
808 | cursor.close() |
||
809 | cnx.close() |
||
810 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
811 | description='API.SHOPFLOOR_METER_RELATION_NOT_FOUND') |
||
812 | |||
813 | cursor.execute(" DELETE FROM tbl_shopfloors_meters WHERE shopfloor_id = %s AND meter_id = %s ", (id_, mid)) |
||
814 | cnx.commit() |
||
815 | |||
816 | cursor.close() |
||
817 | cnx.close() |
||
818 | |||
819 | resp.status = falcon.HTTP_204 |
||
820 | |||
821 | |||
822 | View Code Duplication | class ShopfloorOfflineMeterCollection: |
|
823 | def __init__(self): |
||
824 | """Initializes ShopfloorOfflineMeterCollection""" |
||
825 | pass |
||
826 | |||
827 | @staticmethod |
||
828 | def on_options(req, resp, id_): |
||
829 | resp.status = falcon.HTTP_200 |
||
830 | |||
831 | @staticmethod |
||
832 | def on_get(req, resp, id_): |
||
833 | if 'API-KEY' not in req.headers or \ |
||
834 | not isinstance(req.headers['API-KEY'], str) or \ |
||
835 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
836 | access_control(req) |
||
837 | else: |
||
838 | api_key_control(req) |
||
839 | if not id_.isdigit() or int(id_) <= 0: |
||
840 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
841 | description='API.INVALID_SHOPFLOOR_ID') |
||
842 | |||
843 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
844 | cursor = cnx.cursor() |
||
845 | |||
846 | cursor.execute(" SELECT name " |
||
847 | " FROM tbl_shopfloors " |
||
848 | " WHERE id = %s ", (id_,)) |
||
849 | if cursor.fetchone() is None: |
||
850 | cursor.close() |
||
851 | cnx.close() |
||
852 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
853 | description='API.SHOPFLOOR_NOT_FOUND') |
||
854 | |||
855 | query = (" SELECT id, name, uuid " |
||
856 | " FROM tbl_energy_categories ") |
||
857 | cursor.execute(query) |
||
858 | rows_energy_categories = cursor.fetchall() |
||
859 | |||
860 | energy_category_dict = dict() |
||
861 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
862 | for row in rows_energy_categories: |
||
863 | energy_category_dict[row[0]] = {"id": row[0], |
||
864 | "name": row[1], |
||
865 | "uuid": row[2]} |
||
866 | |||
867 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
868 | " FROM tbl_shopfloors s, tbl_shopfloors_offline_meters sm, tbl_offline_meters m " |
||
869 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
||
870 | " ORDER BY m.id ") |
||
871 | cursor.execute(query, (id_,)) |
||
872 | rows = cursor.fetchall() |
||
873 | |||
874 | result = list() |
||
875 | if rows is not None and len(rows) > 0: |
||
876 | for row in rows: |
||
877 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
878 | "energy_category": energy_category_dict.get(row[3], None)} |
||
879 | result.append(meta_result) |
||
880 | |||
881 | resp.text = json.dumps(result) |
||
882 | |||
883 | @staticmethod |
||
884 | @user_logger |
||
885 | def on_post(req, resp, id_): |
||
886 | """Handles POST requests""" |
||
887 | admin_control(req) |
||
888 | try: |
||
889 | raw_json = req.stream.read().decode('utf-8') |
||
890 | except Exception as ex: |
||
891 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
892 | title='API.BAD_REQUEST', |
||
893 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
894 | |||
895 | if not id_.isdigit() or int(id_) <= 0: |
||
896 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
897 | description='API.INVALID_SHOPFLOOR_ID') |
||
898 | |||
899 | new_values = json.loads(raw_json) |
||
900 | |||
901 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
||
902 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
||
903 | new_values['data']['offline_meter_id'] <= 0: |
||
904 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
905 | description='API.INVALID_OFFLINE_METER_ID') |
||
906 | offline_meter_id = new_values['data']['offline_meter_id'] |
||
907 | |||
908 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
909 | cursor = cnx.cursor() |
||
910 | |||
911 | cursor.execute(" SELECT name " |
||
912 | " from tbl_shopfloors " |
||
913 | " WHERE id = %s ", (id_,)) |
||
914 | if cursor.fetchone() is None: |
||
915 | cursor.close() |
||
916 | cnx.close() |
||
917 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
918 | description='API.SHOPFLOOR_NOT_FOUND') |
||
919 | |||
920 | cursor.execute(" SELECT name " |
||
921 | " FROM tbl_offline_meters " |
||
922 | " WHERE id = %s ", (offline_meter_id,)) |
||
923 | if cursor.fetchone() is None: |
||
924 | cursor.close() |
||
925 | cnx.close() |
||
926 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
927 | description='API.OFFLINE_METER_NOT_FOUND') |
||
928 | |||
929 | query = (" SELECT id " |
||
930 | " FROM tbl_shopfloors_offline_meters " |
||
931 | " WHERE shopfloor_id = %s AND offline_meter_id = %s") |
||
932 | cursor.execute(query, (id_, offline_meter_id,)) |
||
933 | if cursor.fetchone() is not None: |
||
934 | cursor.close() |
||
935 | cnx.close() |
||
936 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
937 | description='API.SHOPFLOOR_OFFLINE_METER_RELATION_EXISTS') |
||
938 | |||
939 | add_row = (" INSERT INTO tbl_shopfloors_offline_meters (shopfloor_id, offline_meter_id) " |
||
940 | " VALUES (%s, %s) ") |
||
941 | cursor.execute(add_row, (id_, offline_meter_id,)) |
||
942 | cnx.commit() |
||
943 | cursor.close() |
||
944 | cnx.close() |
||
945 | |||
946 | resp.status = falcon.HTTP_201 |
||
947 | resp.location = '/shopfloors/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
||
948 | |||
949 | |||
950 | View Code Duplication | class ShopfloorOfflineMeterItem: |
|
951 | def __init__(self): |
||
952 | """Initializes ShopfloorOfflineMeterItem""" |
||
953 | pass |
||
954 | |||
955 | @staticmethod |
||
956 | def on_options(req, resp, id_, mid): |
||
957 | resp.status = falcon.HTTP_200 |
||
958 | |||
959 | @staticmethod |
||
960 | @user_logger |
||
961 | def on_delete(req, resp, id_, mid): |
||
962 | admin_control(req) |
||
963 | if not id_.isdigit() or int(id_) <= 0: |
||
964 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
965 | description='API.INVALID_SHOPFLOOR_ID') |
||
966 | |||
967 | if not mid.isdigit() or int(mid) <= 0: |
||
968 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
969 | description='API.INVALID_OFFLINE_METER_ID') |
||
970 | |||
971 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
972 | cursor = cnx.cursor() |
||
973 | |||
974 | cursor.execute(" SELECT name " |
||
975 | " FROM tbl_shopfloors " |
||
976 | " WHERE id = %s ", (id_,)) |
||
977 | if cursor.fetchone() is None: |
||
978 | cursor.close() |
||
979 | cnx.close() |
||
980 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
981 | description='API.SHOPFLOOR_NOT_FOUND') |
||
982 | |||
983 | cursor.execute(" SELECT name " |
||
984 | " FROM tbl_offline_meters " |
||
985 | " WHERE id = %s ", (mid,)) |
||
986 | if cursor.fetchone() is None: |
||
987 | cursor.close() |
||
988 | cnx.close() |
||
989 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
990 | description='API.OFFLINE_METER_NOT_FOUND') |
||
991 | |||
992 | cursor.execute(" SELECT id " |
||
993 | " FROM tbl_shopfloors_offline_meters " |
||
994 | " WHERE shopfloor_id = %s AND offline_meter_id = %s ", (id_, mid)) |
||
995 | if cursor.fetchone() is None: |
||
996 | cursor.close() |
||
997 | cnx.close() |
||
998 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
999 | description='API.SHOPFLOOR_OFFLINE_METER_RELATION_NOT_FOUND') |
||
1000 | |||
1001 | cursor.execute(" DELETE FROM tbl_shopfloors_offline_meters " |
||
1002 | " WHERE shopfloor_id = %s AND offline_meter_id = %s ", (id_, mid)) |
||
1003 | cnx.commit() |
||
1004 | |||
1005 | cursor.close() |
||
1006 | cnx.close() |
||
1007 | |||
1008 | resp.status = falcon.HTTP_204 |
||
1009 | |||
1010 | |||
1011 | View Code Duplication | class ShopfloorPointCollection: |
|
1012 | def __init__(self): |
||
1013 | """Initializes ShopfloorPointCollection""" |
||
1014 | pass |
||
1015 | |||
1016 | @staticmethod |
||
1017 | def on_options(req, resp, id_): |
||
1018 | resp.status = falcon.HTTP_200 |
||
1019 | |||
1020 | @staticmethod |
||
1021 | def on_get(req, resp, id_): |
||
1022 | if 'API-KEY' not in req.headers or \ |
||
1023 | not isinstance(req.headers['API-KEY'], str) or \ |
||
1024 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
1025 | access_control(req) |
||
1026 | else: |
||
1027 | api_key_control(req) |
||
1028 | if not id_.isdigit() or int(id_) <= 0: |
||
1029 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1030 | description='API.INVALID_SHOPFLOOR_ID') |
||
1031 | |||
1032 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1033 | cursor = cnx.cursor() |
||
1034 | |||
1035 | cursor.execute(" SELECT name " |
||
1036 | " FROM tbl_shopfloors " |
||
1037 | " WHERE id = %s ", (id_,)) |
||
1038 | if cursor.fetchone() is None: |
||
1039 | cursor.close() |
||
1040 | cnx.close() |
||
1041 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1042 | description='API.SHOPFLOOR_NOT_FOUND') |
||
1043 | |||
1044 | query = (" SELECT id, name, uuid " |
||
1045 | " FROM tbl_data_sources ") |
||
1046 | cursor.execute(query) |
||
1047 | rows_data_sources = cursor.fetchall() |
||
1048 | |||
1049 | data_source_dict = dict() |
||
1050 | if rows_data_sources is not None and len(rows_data_sources) > 0: |
||
1051 | for row in rows_data_sources: |
||
1052 | data_source_dict[row[0]] = {"id": row[0], |
||
1053 | "name": row[1], |
||
1054 | "uuid": row[2]} |
||
1055 | |||
1056 | query = (" SELECT p.id, p.name, p.data_source_id " |
||
1057 | " FROM tbl_shopfloors s, tbl_shopfloors_points sp, tbl_points p " |
||
1058 | " WHERE sp.shopfloor_id = s.id AND p.id = sp.point_id AND s.id = %s " |
||
1059 | " ORDER BY p.id ") |
||
1060 | cursor.execute(query, (id_,)) |
||
1061 | rows = cursor.fetchall() |
||
1062 | |||
1063 | result = list() |
||
1064 | if rows is not None and len(rows) > 0: |
||
1065 | for row in rows: |
||
1066 | meta_result = {"id": row[0], "name": row[1], "data_source": data_source_dict.get(row[2], None)} |
||
1067 | result.append(meta_result) |
||
1068 | |||
1069 | resp.text = json.dumps(result) |
||
1070 | |||
1071 | @staticmethod |
||
1072 | @user_logger |
||
1073 | def on_post(req, resp, id_): |
||
1074 | """Handles POST requests""" |
||
1075 | admin_control(req) |
||
1076 | try: |
||
1077 | raw_json = req.stream.read().decode('utf-8') |
||
1078 | except Exception as ex: |
||
1079 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1080 | title='API.BAD_REQUEST', |
||
1081 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
1082 | |||
1083 | if not id_.isdigit() or int(id_) <= 0: |
||
1084 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1085 | description='API.INVALID_SHOPFLOOR_ID') |
||
1086 | |||
1087 | new_values = json.loads(raw_json) |
||
1088 | |||
1089 | if 'point_id' not in new_values['data'].keys() or \ |
||
1090 | not isinstance(new_values['data']['point_id'], int) or \ |
||
1091 | new_values['data']['point_id'] <= 0: |
||
1092 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1093 | description='API.INVALID_POINT_ID') |
||
1094 | point_id = new_values['data']['point_id'] |
||
1095 | |||
1096 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1097 | cursor = cnx.cursor() |
||
1098 | |||
1099 | cursor.execute(" SELECT name " |
||
1100 | " from tbl_shopfloors " |
||
1101 | " WHERE id = %s ", (id_,)) |
||
1102 | if cursor.fetchone() is None: |
||
1103 | cursor.close() |
||
1104 | cnx.close() |
||
1105 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1106 | description='API.SHOPFLOOR_NOT_FOUND') |
||
1107 | |||
1108 | cursor.execute(" SELECT name " |
||
1109 | " FROM tbl_points " |
||
1110 | " WHERE id = %s ", (point_id,)) |
||
1111 | if cursor.fetchone() is None: |
||
1112 | cursor.close() |
||
1113 | cnx.close() |
||
1114 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1115 | description='API.POINT_NOT_FOUND') |
||
1116 | |||
1117 | query = (" SELECT id " |
||
1118 | " FROM tbl_shopfloors_points " |
||
1119 | " WHERE shopfloor_id = %s AND point_id = %s") |
||
1120 | cursor.execute(query, (id_, point_id,)) |
||
1121 | if cursor.fetchone() is not None: |
||
1122 | cursor.close() |
||
1123 | cnx.close() |
||
1124 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
1125 | description='API.SHOPFLOOR_POINT_RELATION_EXISTS') |
||
1126 | |||
1127 | add_row = (" INSERT INTO tbl_shopfloors_points (shopfloor_id, point_id) " |
||
1128 | " VALUES (%s, %s) ") |
||
1129 | cursor.execute(add_row, (id_, point_id,)) |
||
1130 | cnx.commit() |
||
1131 | cursor.close() |
||
1132 | cnx.close() |
||
1133 | |||
1134 | resp.status = falcon.HTTP_201 |
||
1135 | resp.location = '/shopfloors/' + str(id_) + '/points/' + str(point_id) |
||
1136 | |||
1137 | |||
1138 | View Code Duplication | class ShopfloorPointItem: |
|
1139 | def __init__(self): |
||
1140 | """Initializes ShopfloorPointItem""" |
||
1141 | pass |
||
1142 | |||
1143 | @staticmethod |
||
1144 | def on_options(req, resp, id_, pid): |
||
1145 | resp.status = falcon.HTTP_200 |
||
1146 | |||
1147 | @staticmethod |
||
1148 | @user_logger |
||
1149 | def on_delete(req, resp, id_, pid): |
||
1150 | admin_control(req) |
||
1151 | if not id_.isdigit() or int(id_) <= 0: |
||
1152 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1153 | description='API.INVALID_SHOPFLOOR_ID') |
||
1154 | |||
1155 | if not pid.isdigit() or int(pid) <= 0: |
||
1156 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1157 | description='API.INVALID_POINT_ID') |
||
1158 | |||
1159 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1160 | cursor = cnx.cursor() |
||
1161 | |||
1162 | cursor.execute(" SELECT name " |
||
1163 | " FROM tbl_shopfloors " |
||
1164 | " WHERE id = %s ", (id_,)) |
||
1165 | if cursor.fetchone() is None: |
||
1166 | cursor.close() |
||
1167 | cnx.close() |
||
1168 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1169 | description='API.SHOPFLOOR_NOT_FOUND') |
||
1170 | |||
1171 | cursor.execute(" SELECT name " |
||
1172 | " FROM tbl_points " |
||
1173 | " WHERE id = %s ", (pid,)) |
||
1174 | if cursor.fetchone() is None: |
||
1175 | cursor.close() |
||
1176 | cnx.close() |
||
1177 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1178 | description='API.POINT_NOT_FOUND') |
||
1179 | |||
1180 | cursor.execute(" SELECT id " |
||
1181 | " FROM tbl_shopfloors_points " |
||
1182 | " WHERE shopfloor_id = %s AND point_id = %s ", (id_, pid)) |
||
1183 | if cursor.fetchone() is None: |
||
1184 | cursor.close() |
||
1185 | cnx.close() |
||
1186 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1187 | description='API.SHOPFLOOR_POINT_RELATION_NOT_FOUND') |
||
1188 | |||
1189 | cursor.execute(" DELETE FROM tbl_shopfloors_points " |
||
1190 | " WHERE shopfloor_id = %s AND point_id = %s ", (id_, pid)) |
||
1191 | cnx.commit() |
||
1192 | |||
1193 | cursor.close() |
||
1194 | cnx.close() |
||
1195 | |||
1196 | resp.status = falcon.HTTP_204 |
||
1197 | |||
1198 | |||
1199 | View Code Duplication | class ShopfloorSensorCollection: |
|
1200 | def __init__(self): |
||
1201 | """Initializes ShopfloorSensorCollection""" |
||
1202 | pass |
||
1203 | |||
1204 | @staticmethod |
||
1205 | def on_options(req, resp, id_): |
||
1206 | resp.status = falcon.HTTP_200 |
||
1207 | |||
1208 | @staticmethod |
||
1209 | def on_get(req, resp, id_): |
||
1210 | if 'API-KEY' not in req.headers or \ |
||
1211 | not isinstance(req.headers['API-KEY'], str) or \ |
||
1212 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
1213 | access_control(req) |
||
1214 | else: |
||
1215 | api_key_control(req) |
||
1216 | if not id_.isdigit() or int(id_) <= 0: |
||
1217 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1218 | description='API.INVALID_SHOPFLOOR_ID') |
||
1219 | |||
1220 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1221 | cursor = cnx.cursor() |
||
1222 | |||
1223 | cursor.execute(" SELECT name " |
||
1224 | " FROM tbl_shopfloors " |
||
1225 | " WHERE id = %s ", (id_,)) |
||
1226 | if cursor.fetchone() is None: |
||
1227 | cursor.close() |
||
1228 | cnx.close() |
||
1229 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1230 | description='API.SHOPFLOOR_NOT_FOUND') |
||
1231 | |||
1232 | query = (" SELECT se.id, se.name, se.uuid " |
||
1233 | " FROM tbl_shopfloors sp, tbl_shopfloors_sensors ss, tbl_sensors se " |
||
1234 | " WHERE ss.shopfloor_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s " |
||
1235 | " ORDER BY se.id ") |
||
1236 | cursor.execute(query, (id_,)) |
||
1237 | rows = cursor.fetchall() |
||
1238 | |||
1239 | result = list() |
||
1240 | if rows is not None and len(rows) > 0: |
||
1241 | for row in rows: |
||
1242 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
1243 | result.append(meta_result) |
||
1244 | |||
1245 | resp.text = json.dumps(result) |
||
1246 | |||
1247 | @staticmethod |
||
1248 | @user_logger |
||
1249 | def on_post(req, resp, id_): |
||
1250 | """Handles POST requests""" |
||
1251 | admin_control(req) |
||
1252 | try: |
||
1253 | raw_json = req.stream.read().decode('utf-8') |
||
1254 | except Exception as ex: |
||
1255 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1256 | title='API.BAD_REQUEST', |
||
1257 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
1258 | |||
1259 | if not id_.isdigit() or int(id_) <= 0: |
||
1260 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1261 | description='API.INVALID_SHOPFLOOR_ID') |
||
1262 | |||
1263 | new_values = json.loads(raw_json) |
||
1264 | |||
1265 | if 'sensor_id' not in new_values['data'].keys() or \ |
||
1266 | not isinstance(new_values['data']['sensor_id'], int) or \ |
||
1267 | new_values['data']['sensor_id'] <= 0: |
||
1268 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1269 | description='API.INVALID_SENSOR_ID') |
||
1270 | sensor_id = new_values['data']['sensor_id'] |
||
1271 | |||
1272 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1273 | cursor = cnx.cursor() |
||
1274 | |||
1275 | cursor.execute(" SELECT name " |
||
1276 | " from tbl_shopfloors " |
||
1277 | " WHERE id = %s ", (id_,)) |
||
1278 | if cursor.fetchone() is None: |
||
1279 | cursor.close() |
||
1280 | cnx.close() |
||
1281 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1282 | description='API.SHOPFLOOR_NOT_FOUND') |
||
1283 | |||
1284 | cursor.execute(" SELECT name " |
||
1285 | " FROM tbl_sensors " |
||
1286 | " WHERE id = %s ", (sensor_id,)) |
||
1287 | if cursor.fetchone() is None: |
||
1288 | cursor.close() |
||
1289 | cnx.close() |
||
1290 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1291 | description='API.SENSOR_NOT_FOUND') |
||
1292 | |||
1293 | query = (" SELECT id " |
||
1294 | " FROM tbl_shopfloors_sensors " |
||
1295 | " WHERE shopfloor_id = %s AND sensor_id = %s") |
||
1296 | cursor.execute(query, (id_, sensor_id,)) |
||
1297 | if cursor.fetchone() is not None: |
||
1298 | cursor.close() |
||
1299 | cnx.close() |
||
1300 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
1301 | description='API.SHOPFLOOR_SENSOR_RELATION_EXISTS') |
||
1302 | |||
1303 | add_row = (" INSERT INTO tbl_shopfloors_sensors (shopfloor_id, sensor_id) " |
||
1304 | " VALUES (%s, %s) ") |
||
1305 | cursor.execute(add_row, (id_, sensor_id,)) |
||
1306 | cnx.commit() |
||
1307 | cursor.close() |
||
1308 | cnx.close() |
||
1309 | |||
1310 | resp.status = falcon.HTTP_201 |
||
1311 | resp.location = '/shopfloors/' + str(id_) + '/sensors/' + str(sensor_id) |
||
1312 | |||
1313 | |||
1314 | View Code Duplication | class ShopfloorSensorItem: |
|
1315 | def __init__(self): |
||
1316 | """Initializes ShopfloorSensorItem""" |
||
1317 | pass |
||
1318 | |||
1319 | @staticmethod |
||
1320 | def on_options(req, resp, id_, sid): |
||
1321 | resp.status = falcon.HTTP_200 |
||
1322 | |||
1323 | @staticmethod |
||
1324 | @user_logger |
||
1325 | def on_delete(req, resp, id_, sid): |
||
1326 | admin_control(req) |
||
1327 | if not id_.isdigit() or int(id_) <= 0: |
||
1328 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1329 | description='API.INVALID_SHOPFLOOR_ID') |
||
1330 | |||
1331 | if not sid.isdigit() or int(sid) <= 0: |
||
1332 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1333 | description='API.INVALID_SENSOR_ID') |
||
1334 | |||
1335 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1336 | cursor = cnx.cursor() |
||
1337 | |||
1338 | cursor.execute(" SELECT name " |
||
1339 | " FROM tbl_shopfloors " |
||
1340 | " WHERE id = %s ", (id_,)) |
||
1341 | if cursor.fetchone() is None: |
||
1342 | cursor.close() |
||
1343 | cnx.close() |
||
1344 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1345 | description='API.SHOPFLOOR_NOT_FOUND') |
||
1346 | |||
1347 | cursor.execute(" SELECT name " |
||
1348 | " FROM tbl_sensors " |
||
1349 | " WHERE id = %s ", (sid,)) |
||
1350 | if cursor.fetchone() is None: |
||
1351 | cursor.close() |
||
1352 | cnx.close() |
||
1353 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1354 | description='API.SENSOR_NOT_FOUND') |
||
1355 | |||
1356 | cursor.execute(" SELECT id " |
||
1357 | " FROM tbl_shopfloors_sensors " |
||
1358 | " WHERE shopfloor_id = %s AND sensor_id = %s ", (id_, sid)) |
||
1359 | if cursor.fetchone() is None: |
||
1360 | cursor.close() |
||
1361 | cnx.close() |
||
1362 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1363 | description='API.SHOPFLOOR_SENSOR_RELATION_NOT_FOUND') |
||
1364 | |||
1365 | cursor.execute(" DELETE FROM tbl_shopfloors_sensors WHERE shopfloor_id = %s AND sensor_id = %s ", (id_, sid)) |
||
1366 | cnx.commit() |
||
1367 | |||
1368 | cursor.close() |
||
1369 | cnx.close() |
||
1370 | |||
1371 | resp.status = falcon.HTTP_204 |
||
1372 | |||
1373 | |||
1374 | View Code Duplication | class ShopfloorVirtualMeterCollection: |
|
1375 | def __init__(self): |
||
1376 | """Initializes ShopfloorVirtualMeterCollection""" |
||
1377 | pass |
||
1378 | |||
1379 | @staticmethod |
||
1380 | def on_options(req, resp, id_): |
||
1381 | resp.status = falcon.HTTP_200 |
||
1382 | |||
1383 | @staticmethod |
||
1384 | def on_get(req, resp, id_): |
||
1385 | if 'API-KEY' not in req.headers or \ |
||
1386 | not isinstance(req.headers['API-KEY'], str) or \ |
||
1387 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
1388 | access_control(req) |
||
1389 | else: |
||
1390 | api_key_control(req) |
||
1391 | if not id_.isdigit() or int(id_) <= 0: |
||
1392 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1393 | description='API.INVALID_SHOPFLOOR_ID') |
||
1394 | |||
1395 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1396 | cursor = cnx.cursor() |
||
1397 | |||
1398 | cursor.execute(" SELECT name " |
||
1399 | " FROM tbl_shopfloors " |
||
1400 | " WHERE id = %s ", (id_,)) |
||
1401 | if cursor.fetchone() is None: |
||
1402 | cursor.close() |
||
1403 | cnx.close() |
||
1404 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1405 | description='API.SHOPFLOOR_NOT_FOUND') |
||
1406 | |||
1407 | query = (" SELECT id, name, uuid " |
||
1408 | " FROM tbl_energy_categories ") |
||
1409 | cursor.execute(query) |
||
1410 | rows_energy_categories = cursor.fetchall() |
||
1411 | |||
1412 | energy_category_dict = dict() |
||
1413 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
1414 | for row in rows_energy_categories: |
||
1415 | energy_category_dict[row[0]] = {"id": row[0], |
||
1416 | "name": row[1], |
||
1417 | "uuid": row[2]} |
||
1418 | |||
1419 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
1420 | " FROM tbl_shopfloors s, tbl_shopfloors_virtual_meters sm, tbl_virtual_meters m " |
||
1421 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.virtual_meter_id AND s.id = %s " |
||
1422 | " ORDER BY m.id ") |
||
1423 | cursor.execute(query, (id_,)) |
||
1424 | rows = cursor.fetchall() |
||
1425 | |||
1426 | result = list() |
||
1427 | if rows is not None and len(rows) > 0: |
||
1428 | for row in rows: |
||
1429 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
1430 | "energy_category": energy_category_dict.get(row[3], None)} |
||
1431 | result.append(meta_result) |
||
1432 | |||
1433 | resp.text = json.dumps(result) |
||
1434 | |||
1435 | @staticmethod |
||
1436 | @user_logger |
||
1437 | def on_post(req, resp, id_): |
||
1438 | """Handles POST requests""" |
||
1439 | admin_control(req) |
||
1440 | try: |
||
1441 | raw_json = req.stream.read().decode('utf-8') |
||
1442 | except Exception as ex: |
||
1443 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1444 | title='API.BAD_REQUEST', |
||
1445 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
1446 | |||
1447 | if not id_.isdigit() or int(id_) <= 0: |
||
1448 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1449 | description='API.INVALID_SHOPFLOOR_ID') |
||
1450 | |||
1451 | new_values = json.loads(raw_json) |
||
1452 | |||
1453 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
||
1454 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
||
1455 | new_values['data']['virtual_meter_id'] <= 0: |
||
1456 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1457 | description='API.INVALID_VIRTUAL_METER_ID') |
||
1458 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
||
1459 | |||
1460 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1461 | cursor = cnx.cursor() |
||
1462 | |||
1463 | cursor.execute(" SELECT name " |
||
1464 | " from tbl_shopfloors " |
||
1465 | " WHERE id = %s ", (id_,)) |
||
1466 | if cursor.fetchone() is None: |
||
1467 | cursor.close() |
||
1468 | cnx.close() |
||
1469 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1470 | description='API.SHOPFLOOR_NOT_FOUND') |
||
1471 | |||
1472 | cursor.execute(" SELECT name " |
||
1473 | " FROM tbl_virtual_meters " |
||
1474 | " WHERE id = %s ", (virtual_meter_id,)) |
||
1475 | if cursor.fetchone() is None: |
||
1476 | cursor.close() |
||
1477 | cnx.close() |
||
1478 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1479 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
1480 | |||
1481 | query = (" SELECT id " |
||
1482 | " FROM tbl_shopfloors_virtual_meters " |
||
1483 | " WHERE shopfloor_id = %s AND virtual_meter_id = %s") |
||
1484 | cursor.execute(query, (id_, virtual_meter_id,)) |
||
1485 | if cursor.fetchone() is not None: |
||
1486 | cursor.close() |
||
1487 | cnx.close() |
||
1488 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
1489 | description='API.SHOPFLOOR_VIRTUAL_METER_RELATION_EXISTS') |
||
1490 | |||
1491 | add_row = (" INSERT INTO tbl_shopfloors_virtual_meters (shopfloor_id, virtual_meter_id) " |
||
1492 | " VALUES (%s, %s) ") |
||
1493 | cursor.execute(add_row, (id_, virtual_meter_id,)) |
||
1494 | cnx.commit() |
||
1495 | cursor.close() |
||
1496 | cnx.close() |
||
1497 | |||
1498 | resp.status = falcon.HTTP_201 |
||
1499 | resp.location = '/shopfloors/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
||
1500 | |||
1501 | |||
1502 | View Code Duplication | class ShopfloorVirtualMeterItem: |
|
1503 | def __init__(self): |
||
1504 | """Initializes ShopfloorVirtualMeterItem""" |
||
1505 | pass |
||
1506 | |||
1507 | @staticmethod |
||
1508 | def on_options(req, resp, id_, mid): |
||
1509 | resp.status = falcon.HTTP_200 |
||
1510 | |||
1511 | @staticmethod |
||
1512 | @user_logger |
||
1513 | def on_delete(req, resp, id_, mid): |
||
1514 | admin_control(req) |
||
1515 | if not id_.isdigit() or int(id_) <= 0: |
||
1516 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1517 | description='API.INVALID_SHOPFLOOR_ID') |
||
1518 | |||
1519 | if not mid.isdigit() or int(mid) <= 0: |
||
1520 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1521 | description='API.INVALID_VIRTUAL_METER_ID') |
||
1522 | |||
1523 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1524 | cursor = cnx.cursor() |
||
1525 | |||
1526 | cursor.execute(" SELECT name " |
||
1527 | " FROM tbl_shopfloors " |
||
1528 | " WHERE id = %s ", (id_,)) |
||
1529 | if cursor.fetchone() is None: |
||
1530 | cursor.close() |
||
1531 | cnx.close() |
||
1532 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1533 | description='API.SHOPFLOOR_NOT_FOUND') |
||
1534 | |||
1535 | cursor.execute(" SELECT name " |
||
1536 | " FROM tbl_virtual_meters " |
||
1537 | " WHERE id = %s ", (mid,)) |
||
1538 | if cursor.fetchone() is None: |
||
1539 | cursor.close() |
||
1540 | cnx.close() |
||
1541 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1542 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
1543 | |||
1544 | cursor.execute(" SELECT id " |
||
1545 | " FROM tbl_shopfloors_virtual_meters " |
||
1546 | " WHERE shopfloor_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
||
1547 | if cursor.fetchone() is None: |
||
1548 | cursor.close() |
||
1549 | cnx.close() |
||
1550 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1551 | description='API.SHOPFLOOR_VIRTUAL_METER_RELATION_NOT_FOUND') |
||
1552 | |||
1553 | cursor.execute(" DELETE FROM tbl_shopfloors_virtual_meters " |
||
1554 | " WHERE shopfloor_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
||
1555 | cnx.commit() |
||
1556 | |||
1557 | cursor.close() |
||
1558 | cnx.close() |
||
1559 | |||
1560 | resp.status = falcon.HTTP_204 |
||
1561 | |||
1562 | |||
1563 | View Code Duplication | class ShopfloorWorkingCalendarCollection: |
|
1564 | def __init__(self): |
||
1565 | """Initializes ShopfloorWorkingCalendarCollection Class""" |
||
1566 | pass |
||
1567 | |||
1568 | @staticmethod |
||
1569 | def on_options(req, resp, id_): |
||
1570 | resp.status = falcon.HTTP_200 |
||
1571 | |||
1572 | @staticmethod |
||
1573 | def on_get(req, resp, id_): |
||
1574 | if 'API-KEY' not in req.headers or \ |
||
1575 | not isinstance(req.headers['API-KEY'], str) or \ |
||
1576 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
1577 | access_control(req) |
||
1578 | else: |
||
1579 | api_key_control(req) |
||
1580 | if not id_.isdigit() or int(id_) <= 0: |
||
1581 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1582 | description='API.INVALID_SHOPFLOOR_ID') |
||
1583 | |||
1584 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1585 | cursor = cnx.cursor() |
||
1586 | |||
1587 | cursor.execute(" SELECT name " |
||
1588 | " FROM tbl_shopfloors " |
||
1589 | " WHERE id = %s ", (id_,)) |
||
1590 | if cursor.fetchone() is None: |
||
1591 | cursor.close() |
||
1592 | cnx.close() |
||
1593 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1594 | description='API.SHOPFLOOR_NOT_FOUND') |
||
1595 | |||
1596 | query = (" SELECT wc.id, wc.name, wc.description " |
||
1597 | " FROM tbl_shopfloors s, tbl_shopfloors_working_calendars swc, tbl_working_calendars wc " |
||
1598 | " WHERE swc.shopfloor_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
||
1599 | " ORDER BY wc.id ") |
||
1600 | cursor.execute(query, (id_,)) |
||
1601 | rows = cursor.fetchall() |
||
1602 | |||
1603 | result = list() |
||
1604 | if rows is not None and len(rows) > 0: |
||
1605 | for row in rows: |
||
1606 | meta_result = {"id": row[0], "name": row[1], "description": row[2]} |
||
1607 | result.append(meta_result) |
||
1608 | |||
1609 | resp.text = json.dumps(result) |
||
1610 | |||
1611 | @staticmethod |
||
1612 | @user_logger |
||
1613 | def on_post(req, resp, id_): |
||
1614 | """Handles POST requests""" |
||
1615 | admin_control(req) |
||
1616 | try: |
||
1617 | raw_json = req.stream.read().decode('utf-8') |
||
1618 | except Exception as ex: |
||
1619 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1620 | title='API.BAD_REQUEST', |
||
1621 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
1622 | |||
1623 | if not id_.isdigit() or int(id_) <= 0: |
||
1624 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1625 | description='API.INVALID_SHOPFLOOR_ID') |
||
1626 | |||
1627 | new_values = json.loads(raw_json) |
||
1628 | |||
1629 | if 'working_calendar_id' not in new_values['data'].keys() or \ |
||
1630 | not isinstance(new_values['data']['working_calendar_id'], int) or \ |
||
1631 | new_values['data']['working_calendar_id'] <= 0: |
||
1632 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1633 | description='API.INVALID_WORKING_CALENDAR_ID') |
||
1634 | working_calendar_id = new_values['data']['working_calendar_id'] |
||
1635 | |||
1636 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1637 | cursor = cnx.cursor() |
||
1638 | |||
1639 | cursor.execute(" SELECT name " |
||
1640 | " from tbl_shopfloors " |
||
1641 | " WHERE id = %s ", (id_,)) |
||
1642 | if cursor.fetchone() is None: |
||
1643 | cursor.close() |
||
1644 | cnx.close() |
||
1645 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1646 | description='API.SHOPFLOOR_NOT_FOUND') |
||
1647 | |||
1648 | cursor.execute(" SELECT name " |
||
1649 | " FROM tbl_working_calendars " |
||
1650 | " WHERE id = %s ", (working_calendar_id,)) |
||
1651 | if cursor.fetchone() is None: |
||
1652 | cursor.close() |
||
1653 | cnx.close() |
||
1654 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1655 | description='API.WORKING_CALENDAR_NOT_FOUND') |
||
1656 | |||
1657 | query = (" SELECT id " |
||
1658 | " FROM tbl_shopfloors_working_calendars " |
||
1659 | " WHERE shopfloor_id = %s AND working_calendar_id = %s") |
||
1660 | cursor.execute(query, (id_, working_calendar_id,)) |
||
1661 | if cursor.fetchone() is not None: |
||
1662 | cursor.close() |
||
1663 | cnx.close() |
||
1664 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
1665 | description='API.SHOPFLOOR_WORKING_CALENDAR_RELATION_EXISTS') |
||
1666 | |||
1667 | add_row = (" INSERT INTO tbl_shopfloors_working_calendars (shopfloor_id, working_calendar_id) " |
||
1668 | " VALUES (%s, %s) ") |
||
1669 | cursor.execute(add_row, (id_, working_calendar_id,)) |
||
1670 | cnx.commit() |
||
1671 | cursor.close() |
||
1672 | cnx.close() |
||
1673 | |||
1674 | resp.status = falcon.HTTP_201 |
||
1675 | resp.location = '/shopfloors/' + str(id_) + '/workingcalendars/' + str(working_calendar_id) |
||
1676 | |||
1677 | |||
1678 | View Code Duplication | class ShopfloorWorkingCalendarItem: |
|
1679 | def __init__(self): |
||
1680 | """Initializes ShopfloorWorkingCalendarItem Class""" |
||
1681 | pass |
||
1682 | |||
1683 | @staticmethod |
||
1684 | def on_options(req, resp, id_, wcid): |
||
1685 | resp.status = falcon.HTTP_200 |
||
1686 | |||
1687 | @staticmethod |
||
1688 | @user_logger |
||
1689 | def on_delete(req, resp, id_, wcid): |
||
1690 | admin_control(req) |
||
1691 | if not id_.isdigit() or int(id_) <= 0: |
||
1692 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1693 | description='API.INVALID_SHOPFLOOR_ID') |
||
1694 | |||
1695 | if not wcid.isdigit() or int(wcid) <= 0: |
||
1696 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1697 | description='API.INVALID_WORKING_CALENDAR_ID') |
||
1698 | |||
1699 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1700 | cursor = cnx.cursor() |
||
1701 | |||
1702 | cursor.execute(" SELECT name " |
||
1703 | " FROM tbl_shopfloors " |
||
1704 | " WHERE id = %s ", (id_,)) |
||
1705 | if cursor.fetchone() is None: |
||
1706 | cursor.close() |
||
1707 | cnx.close() |
||
1708 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1709 | description='API.SHOPFLOOR_NOT_FOUND') |
||
1710 | |||
1711 | cursor.execute(" SELECT name " |
||
1712 | " FROM tbl_working_calendars " |
||
1713 | " WHERE id = %s ", (wcid,)) |
||
1714 | if cursor.fetchone() is None: |
||
1715 | cursor.close() |
||
1716 | cnx.close() |
||
1717 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1718 | description='API.WORKING_CALENDAR_NOT_FOUND') |
||
1719 | |||
1720 | cursor.execute(" SELECT id " |
||
1721 | " FROM tbl_shopfloors_working_calendars " |
||
1722 | " WHERE shopfloor_id = %s AND working_calendar_id = %s ", (id_, wcid)) |
||
1723 | if cursor.fetchone() is None: |
||
1724 | cursor.close() |
||
1725 | cnx.close() |
||
1726 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1727 | description='API.SHOPFLOOR_WORKING_CALENDAR_RELATION_NOT_FOUND') |
||
1728 | |||
1729 | cursor.execute(" DELETE FROM tbl_shopfloors_working_calendars " |
||
1730 | " WHERE shopfloor_id = %s AND working_calendar_id = %s ", (id_, wcid)) |
||
1731 | cnx.commit() |
||
1732 | |||
1733 | cursor.close() |
||
1734 | cnx.close() |
||
1735 | |||
1736 | resp.status = falcon.HTTP_204 |
||
1737 | |||
1738 | |||
1739 | View Code Duplication | class ShopfloorCommandCollection: |
|
1740 | def __init__(self): |
||
1741 | """Initializes Class""" |
||
1742 | pass |
||
1743 | |||
1744 | @staticmethod |
||
1745 | def on_options(req, resp, id_): |
||
1746 | resp.status = falcon.HTTP_200 |
||
1747 | |||
1748 | @staticmethod |
||
1749 | def on_get(req, resp, id_): |
||
1750 | if 'API-KEY' not in req.headers or \ |
||
1751 | not isinstance(req.headers['API-KEY'], str) or \ |
||
1752 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
1753 | access_control(req) |
||
1754 | else: |
||
1755 | api_key_control(req) |
||
1756 | if not id_.isdigit() or int(id_) <= 0: |
||
1757 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1758 | description='API.INVALID_STORE_ID') |
||
1759 | |||
1760 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1761 | cursor = cnx.cursor() |
||
1762 | |||
1763 | cursor.execute(" SELECT name " |
||
1764 | " FROM tbl_shopfloors " |
||
1765 | " WHERE id = %s ", (id_,)) |
||
1766 | if cursor.fetchone() is None: |
||
1767 | cursor.close() |
||
1768 | cnx.close() |
||
1769 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1770 | description='API.SHOPFLOOR_NOT_FOUND') |
||
1771 | |||
1772 | query = (" SELECT c.id, c.name, c.uuid " |
||
1773 | " FROM tbl_shopfloors s, tbl_shopfloors_commands sc, tbl_commands c " |
||
1774 | " WHERE sc.shopfloor_id = s.id AND c.id = sc.command_id AND s.id = %s " |
||
1775 | " ORDER BY c.id ") |
||
1776 | cursor.execute(query, (id_,)) |
||
1777 | rows = cursor.fetchall() |
||
1778 | |||
1779 | result = list() |
||
1780 | if rows is not None and len(rows) > 0: |
||
1781 | for row in rows: |
||
1782 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
1783 | result.append(meta_result) |
||
1784 | |||
1785 | resp.text = json.dumps(result) |
||
1786 | |||
1787 | @staticmethod |
||
1788 | @user_logger |
||
1789 | def on_post(req, resp, id_): |
||
1790 | """Handles POST requests""" |
||
1791 | admin_control(req) |
||
1792 | try: |
||
1793 | raw_json = req.stream.read().decode('utf-8') |
||
1794 | except Exception as ex: |
||
1795 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1796 | title='API.BAD_REQUEST', |
||
1797 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
1798 | |||
1799 | if not id_.isdigit() or int(id_) <= 0: |
||
1800 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1801 | description='API.INVALID_SHOPFLOOR_ID') |
||
1802 | |||
1803 | new_values = json.loads(raw_json) |
||
1804 | |||
1805 | if 'command_id' not in new_values['data'].keys() or \ |
||
1806 | not isinstance(new_values['data']['command_id'], int) or \ |
||
1807 | new_values['data']['command_id'] <= 0: |
||
1808 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1809 | description='API.INVALID_COMMAND_ID') |
||
1810 | command_id = new_values['data']['command_id'] |
||
1811 | |||
1812 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1813 | cursor = cnx.cursor() |
||
1814 | |||
1815 | cursor.execute(" SELECT name " |
||
1816 | " from tbl_shopfloors " |
||
1817 | " WHERE id = %s ", (id_,)) |
||
1818 | if cursor.fetchone() is None: |
||
1819 | cursor.close() |
||
1820 | cnx.close() |
||
1821 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1822 | description='API.SHOPFLOOR_NOT_FOUND') |
||
1823 | |||
1824 | cursor.execute(" SELECT name " |
||
1825 | " FROM tbl_commands " |
||
1826 | " WHERE id = %s ", (command_id,)) |
||
1827 | if cursor.fetchone() is None: |
||
1828 | cursor.close() |
||
1829 | cnx.close() |
||
1830 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1831 | description='API.COMMAND_NOT_FOUND') |
||
1832 | |||
1833 | query = (" SELECT id " |
||
1834 | " FROM tbl_shopfloors_commands " |
||
1835 | " WHERE shopfloor_id = %s AND command_id = %s") |
||
1836 | cursor.execute(query, (id_, command_id,)) |
||
1837 | if cursor.fetchone() is not None: |
||
1838 | cursor.close() |
||
1839 | cnx.close() |
||
1840 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
1841 | description='API.SHOPFLOOR_COMMAND_RELATION_EXISTS') |
||
1842 | |||
1843 | add_row = (" INSERT INTO tbl_shopfloors_commands (shopfloor_id, command_id) " |
||
1844 | " VALUES (%s, %s) ") |
||
1845 | cursor.execute(add_row, (id_, command_id,)) |
||
1846 | cnx.commit() |
||
1847 | cursor.close() |
||
1848 | cnx.close() |
||
1849 | |||
1850 | resp.status = falcon.HTTP_201 |
||
1851 | resp.location = '/shopfloors/' + str(id_) + '/commands/' + str(command_id) |
||
1852 | |||
1853 | |||
1854 | View Code Duplication | class ShopfloorCommandItem: |
|
1855 | def __init__(self): |
||
1856 | """Initializes Class""" |
||
1857 | pass |
||
1858 | |||
1859 | @staticmethod |
||
1860 | def on_options(req, resp, id_, cid): |
||
1861 | resp.status = falcon.HTTP_200 |
||
1862 | |||
1863 | @staticmethod |
||
1864 | @user_logger |
||
1865 | def on_delete(req, resp, id_, cid): |
||
1866 | admin_control(req) |
||
1867 | if not id_.isdigit() or int(id_) <= 0: |
||
1868 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1869 | description='API.INVALID_SHOPFLOOR_ID') |
||
1870 | |||
1871 | if not cid.isdigit() or int(cid) <= 0: |
||
1872 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1873 | description='API.INVALID_COMMAND_ID') |
||
1874 | |||
1875 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1876 | cursor = cnx.cursor() |
||
1877 | |||
1878 | cursor.execute(" SELECT name " |
||
1879 | " FROM tbl_shopfloors " |
||
1880 | " WHERE id = %s ", (id_,)) |
||
1881 | if cursor.fetchone() is None: |
||
1882 | cursor.close() |
||
1883 | cnx.close() |
||
1884 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1885 | description='API.SHOPFLOOR_NOT_FOUND') |
||
1886 | |||
1887 | cursor.execute(" SELECT name " |
||
1888 | " FROM tbl_commands " |
||
1889 | " WHERE id = %s ", (cid,)) |
||
1890 | if cursor.fetchone() is None: |
||
1891 | cursor.close() |
||
1892 | cnx.close() |
||
1893 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1894 | description='API.COMMAND_NOT_FOUND') |
||
1895 | |||
1896 | cursor.execute(" SELECT id " |
||
1897 | " FROM tbl_shopfloors_commands " |
||
1898 | " WHERE shopfloor_id = %s AND command_id = %s ", (id_, cid)) |
||
1899 | if cursor.fetchone() is None: |
||
1900 | cursor.close() |
||
1901 | cnx.close() |
||
1902 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1903 | description='API.SHOPFLOOR_COMMAND_RELATION_NOT_FOUND') |
||
1904 | |||
1905 | cursor.execute(" DELETE FROM tbl_shopfloors_commands WHERE shopfloor_id = %s AND command_id = %s ", (id_, cid)) |
||
1906 | cnx.commit() |
||
1907 | |||
1908 | cursor.close() |
||
1909 | cnx.close() |
||
1910 | |||
1911 | resp.status = falcon.HTTP_204 |
||
1912 | |||
1913 | |||
1914 | class ShopfloorExport: |
||
1915 | def __init__(self): |
||
1916 | """Initializes ShopfloorExport""" |
||
1917 | pass |
||
1918 | |||
1919 | @staticmethod |
||
1920 | def on_options(req, resp, id_): |
||
1921 | resp.status = falcon.HTTP_200 |
||
1922 | |||
1923 | @staticmethod |
||
1924 | def on_get(req, resp, id_): |
||
1925 | if 'API-KEY' not in req.headers or \ |
||
1926 | not isinstance(req.headers['API-KEY'], str) or \ |
||
1927 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
1928 | access_control(req) |
||
1929 | else: |
||
1930 | api_key_control(req) |
||
1931 | if not id_.isdigit() or int(id_) <= 0: |
||
1932 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1933 | description='API.INVALID_SHOPFLOOR_ID') |
||
1934 | |||
1935 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1936 | cursor = cnx.cursor() |
||
1937 | |||
1938 | query = (" SELECT id, name, uuid " |
||
1939 | " FROM tbl_contacts ") |
||
1940 | cursor.execute(query) |
||
1941 | rows_contacts = cursor.fetchall() |
||
1942 | |||
1943 | contact_dict = dict() |
||
1944 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
1945 | for row in rows_contacts: |
||
1946 | contact_dict[row[0]] = {"id": row[0], |
||
1947 | "name": row[1], |
||
1948 | "uuid": row[2]} |
||
1949 | |||
1950 | query = (" SELECT id, name, uuid " |
||
1951 | " FROM tbl_cost_centers ") |
||
1952 | cursor.execute(query) |
||
1953 | rows_cost_centers = cursor.fetchall() |
||
1954 | |||
1955 | cost_center_dict = dict() |
||
1956 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
1957 | for row in rows_cost_centers: |
||
1958 | cost_center_dict[row[0]] = {"id": row[0], |
||
1959 | "name": row[1], |
||
1960 | "uuid": row[2]} |
||
1961 | |||
1962 | query = (" SELECT id, name, uuid, " |
||
1963 | " area, is_input_counted, contact_id, cost_center_id, description " |
||
1964 | " FROM tbl_shopfloors " |
||
1965 | " WHERE id = %s ") |
||
1966 | cursor.execute(query, (id_,)) |
||
1967 | row = cursor.fetchone() |
||
1968 | |||
1969 | if row is None: |
||
1970 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1971 | description='API.SHOPFLOOR_NOT_FOUND') |
||
1972 | else: |
||
1973 | meta_result = {"id": row[0], |
||
1974 | "name": row[1], |
||
1975 | "uuid": row[2], |
||
1976 | "area": row[3], |
||
1977 | "is_input_counted": bool(row[4]), |
||
1978 | "contact": contact_dict.get(row[5], None), |
||
1979 | "cost_center": cost_center_dict.get(row[6], None), |
||
1980 | "description": row[7], |
||
1981 | "equipments": None, |
||
1982 | "commands": None, |
||
1983 | "meters": None, |
||
1984 | "offline_meters": None, |
||
1985 | "virtual_meters": None, |
||
1986 | "points": None, |
||
1987 | "sensors": None, |
||
1988 | "working_calendars": None |
||
1989 | } |
||
1990 | query = (" SELECT e.id, e.name, e.uuid " |
||
1991 | " FROM tbl_shopfloors s, tbl_shopfloors_equipments se, tbl_equipments e " |
||
1992 | " WHERE se.shopfloor_id = s.id AND e.id = se.equipment_id AND s.id = %s " |
||
1993 | " ORDER BY e.id ") |
||
1994 | cursor.execute(query, (id_,)) |
||
1995 | rows = cursor.fetchall() |
||
1996 | |||
1997 | equipment_result = list() |
||
1998 | if rows is not None and len(rows) > 0: |
||
1999 | for row in rows: |
||
2000 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
2001 | equipment_result.append(result) |
||
2002 | meta_result['equipments'] = equipment_result |
||
2003 | query = (" SELECT c.id, c.name, c.uuid " |
||
2004 | " FROM tbl_shopfloors s, tbl_shopfloors_commands sc, tbl_commands c " |
||
2005 | " WHERE sc.shopfloor_id = s.id AND c.id = sc.command_id AND s.id = %s " |
||
2006 | " ORDER BY c.id ") |
||
2007 | cursor.execute(query, (id_,)) |
||
2008 | rows = cursor.fetchall() |
||
2009 | |||
2010 | command_result = list() |
||
2011 | if rows is not None and len(rows) > 0: |
||
2012 | for row in rows: |
||
2013 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
2014 | command_result.append(result) |
||
2015 | meta_result['commands'] = command_result |
||
2016 | query = (" SELECT id, name, uuid " |
||
2017 | " FROM tbl_energy_categories ") |
||
2018 | cursor.execute(query) |
||
2019 | rows_energy_categories = cursor.fetchall() |
||
2020 | |||
2021 | energy_category_dict = dict() |
||
2022 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
2023 | for row in rows_energy_categories: |
||
2024 | energy_category_dict[row[0]] = {"id": row[0], |
||
2025 | "name": row[1], |
||
2026 | "uuid": row[2]} |
||
2027 | |||
2028 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
2029 | " FROM tbl_shopfloors s, tbl_shopfloors_meters sm, tbl_meters m " |
||
2030 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.meter_id AND s.id = %s " |
||
2031 | " ORDER BY m.id ") |
||
2032 | cursor.execute(query, (id_,)) |
||
2033 | rows = cursor.fetchall() |
||
2034 | |||
2035 | meter_result = list() |
||
2036 | if rows is not None and len(rows) > 0: |
||
2037 | for row in rows: |
||
2038 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
2039 | "energy_category": energy_category_dict.get(row[3], None)} |
||
2040 | meter_result.append(result) |
||
2041 | meta_result['meters'] = meter_result |
||
2042 | cursor.execute(" SELECT name " |
||
2043 | " FROM tbl_shopfloors " |
||
2044 | " WHERE id = %s ", (id_,)) |
||
2045 | if cursor.fetchone() is None: |
||
2046 | cursor.close() |
||
2047 | cnx.close() |
||
2048 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2049 | description='API.SHOPFLOOR_NOT_FOUND') |
||
2050 | |||
2051 | query = (" SELECT id, name, uuid " |
||
2052 | " FROM tbl_energy_categories ") |
||
2053 | cursor.execute(query) |
||
2054 | rows_energy_categories = cursor.fetchall() |
||
2055 | |||
2056 | energy_category_dict = dict() |
||
2057 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
2058 | for row in rows_energy_categories: |
||
2059 | energy_category_dict[row[0]] = {"id": row[0], |
||
2060 | "name": row[1], |
||
2061 | "uuid": row[2]} |
||
2062 | |||
2063 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
2064 | " FROM tbl_shopfloors s, tbl_shopfloors_offline_meters sm, tbl_offline_meters m " |
||
2065 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
||
2066 | " ORDER BY m.id ") |
||
2067 | cursor.execute(query, (id_,)) |
||
2068 | rows = cursor.fetchall() |
||
2069 | |||
2070 | offlinemeter_result = list() |
||
2071 | if rows is not None and len(rows) > 0: |
||
2072 | for row in rows: |
||
2073 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
2074 | "energy_category": energy_category_dict.get(row[3], None)} |
||
2075 | offlinemeter_result.append(result) |
||
2076 | meta_result['offline_meters'] = offlinemeter_result |
||
2077 | query = (" SELECT id, name, uuid " |
||
2078 | " FROM tbl_energy_categories ") |
||
2079 | cursor.execute(query) |
||
2080 | rows_energy_categories = cursor.fetchall() |
||
2081 | |||
2082 | energy_category_dict = dict() |
||
2083 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
2084 | for row in rows_energy_categories: |
||
2085 | energy_category_dict[row[0]] = {"id": row[0], |
||
2086 | "name": row[1], |
||
2087 | "uuid": row[2]} |
||
2088 | |||
2089 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
2090 | " FROM tbl_shopfloors s, tbl_shopfloors_virtual_meters sm, tbl_virtual_meters m " |
||
2091 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.virtual_meter_id AND s.id = %s " |
||
2092 | " ORDER BY m.id ") |
||
2093 | cursor.execute(query, (id_,)) |
||
2094 | rows = cursor.fetchall() |
||
2095 | |||
2096 | virtualmeter_result = list() |
||
2097 | if rows is not None and len(rows) > 0: |
||
2098 | for row in rows: |
||
2099 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
2100 | "energy_category": energy_category_dict.get(row[3], None)} |
||
2101 | virtualmeter_result.append(result) |
||
2102 | meta_result['virtual_meters'] = virtualmeter_result |
||
2103 | query = (" SELECT id, name, uuid " |
||
2104 | " FROM tbl_data_sources ") |
||
2105 | cursor.execute(query) |
||
2106 | rows_data_sources = cursor.fetchall() |
||
2107 | |||
2108 | data_source_dict = dict() |
||
2109 | if rows_data_sources is not None and len(rows_data_sources) > 0: |
||
2110 | for row in rows_data_sources: |
||
2111 | data_source_dict[row[0]] = {"id": row[0], |
||
2112 | "name": row[1], |
||
2113 | "uuid": row[2]} |
||
2114 | |||
2115 | query = (" SELECT p.id, p.name, p.data_source_id " |
||
2116 | " FROM tbl_shopfloors s, tbl_shopfloors_points sp, tbl_points p " |
||
2117 | " WHERE sp.shopfloor_id = s.id AND p.id = sp.point_id AND s.id = %s " |
||
2118 | " ORDER BY p.id ") |
||
2119 | cursor.execute(query, (id_,)) |
||
2120 | rows = cursor.fetchall() |
||
2121 | |||
2122 | point_result = list() |
||
2123 | if rows is not None and len(rows) > 0: |
||
2124 | for row in rows: |
||
2125 | result = {"id": row[0], "name": row[1], "data_source": data_source_dict.get(row[2], None)} |
||
2126 | point_result.append(result) |
||
2127 | meta_result['points'] = point_result |
||
2128 | query = (" SELECT se.id, se.name, se.uuid " |
||
2129 | " FROM tbl_shopfloors sp, tbl_shopfloors_sensors ss, tbl_sensors se " |
||
2130 | " WHERE ss.shopfloor_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s " |
||
2131 | " ORDER BY se.id ") |
||
2132 | cursor.execute(query, (id_,)) |
||
2133 | rows = cursor.fetchall() |
||
2134 | |||
2135 | sensor_result = list() |
||
2136 | if rows is not None and len(rows) > 0: |
||
2137 | for row in rows: |
||
2138 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
2139 | sensor_result.append(result) |
||
2140 | meta_result['sensors'] = sensor_result |
||
2141 | query = (" SELECT wc.id, wc.name, wc.description " |
||
2142 | " FROM tbl_shopfloors s, tbl_shopfloors_working_calendars swc, tbl_working_calendars wc " |
||
2143 | " WHERE swc.shopfloor_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
||
2144 | " ORDER BY wc.id ") |
||
2145 | cursor.execute(query, (id_,)) |
||
2146 | rows = cursor.fetchall() |
||
2147 | |||
2148 | workingcalendar_result = list() |
||
2149 | if rows is not None and len(rows) > 0: |
||
2150 | for row in rows: |
||
2151 | result = {"id": row[0], "name": row[1], "description": row[2]} |
||
2152 | workingcalendar_result.append(result) |
||
2153 | meta_result['working_calendars'] = workingcalendar_result |
||
2154 | |||
2155 | cursor.close() |
||
2156 | cnx.close() |
||
2157 | resp.text = json.dumps(meta_result) |
||
2158 | |||
2159 | |||
2160 | class ShopfloorImport: |
||
2161 | def __init__(self): |
||
2162 | """Initializes ShopfloorImport""" |
||
2163 | pass |
||
2164 | |||
2165 | @staticmethod |
||
2166 | def on_options(req, resp): |
||
2167 | resp.status = falcon.HTTP_200 |
||
2168 | |||
2169 | @staticmethod |
||
2170 | @user_logger |
||
2171 | def on_post(req, resp): |
||
2172 | """Handles POST requests""" |
||
2173 | admin_control(req) |
||
2174 | try: |
||
2175 | raw_json = req.stream.read().decode('utf-8') |
||
2176 | except Exception as ex: |
||
2177 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
2178 | title='API.BAD_REQUEST', |
||
2179 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
2180 | |||
2181 | new_values = json.loads(raw_json) |
||
2182 | |||
2183 | if 'name' not in new_values.keys() or \ |
||
2184 | not isinstance(new_values['name'], str) or \ |
||
2185 | len(str.strip(new_values['name'])) == 0: |
||
2186 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2187 | description='API.INVALID_SHOPFLOOR_NAME') |
||
2188 | name = str.strip(new_values['name']) |
||
2189 | |||
2190 | if 'area' not in new_values.keys() or \ |
||
2191 | not (isinstance(new_values['area'], float) or |
||
2192 | isinstance(new_values['area'], int)) or \ |
||
2193 | new_values['area'] <= 0.0: |
||
2194 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2195 | description='API.INVALID_AREA_VALUE') |
||
2196 | area = new_values['area'] |
||
2197 | |||
2198 | if 'is_input_counted' not in new_values.keys() or \ |
||
2199 | not isinstance(new_values['is_input_counted'], bool): |
||
2200 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2201 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
2202 | is_input_counted = new_values['is_input_counted'] |
||
2203 | |||
2204 | if 'id' in new_values['contact'].keys(): |
||
2205 | if new_values['contact']['id'] <= 0: |
||
2206 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2207 | description='API.INVALID_CONTACT_ID') |
||
2208 | contact_id = new_values['contact']['id'] |
||
2209 | else: |
||
2210 | contact_id = None |
||
2211 | |||
2212 | if 'id' in new_values['cost_center'].keys(): |
||
2213 | if new_values['cost_center']['id'] <= 0: |
||
2214 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2215 | description='API.INVALID_COST_CENTER_ID') |
||
2216 | cost_center_id = new_values['cost_center']['id'] |
||
2217 | else: |
||
2218 | cost_center_id = None |
||
2219 | |||
2220 | if 'description' in new_values.keys() and \ |
||
2221 | new_values['description'] is not None and \ |
||
2222 | len(str(new_values['description'])) > 0: |
||
2223 | description = str.strip(new_values['description']) |
||
2224 | else: |
||
2225 | description = None |
||
2226 | |||
2227 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
2228 | cursor = cnx.cursor() |
||
2229 | |||
2230 | cursor.execute(" SELECT name " |
||
2231 | " FROM tbl_shopfloors " |
||
2232 | " WHERE name = %s ", (name,)) |
||
2233 | if cursor.fetchone() is not None: |
||
2234 | cursor.close() |
||
2235 | cnx.close() |
||
2236 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2237 | description='API.SHOPFLOOR_NAME_IS_ALREADY_IN_USE') |
||
2238 | |||
2239 | View Code Duplication | if contact_id is not None: |
|
2240 | cursor.execute(" SELECT name " |
||
2241 | " FROM tbl_contacts " |
||
2242 | " WHERE id = %s ", |
||
2243 | (new_values['contact']['id'],)) |
||
2244 | row = cursor.fetchone() |
||
2245 | if row is None: |
||
2246 | cursor.close() |
||
2247 | cnx.close() |
||
2248 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2249 | description='API.CONTACT_NOT_FOUND') |
||
2250 | |||
2251 | if cost_center_id is not None: |
||
2252 | cursor.execute(" SELECT name " |
||
2253 | " FROM tbl_cost_centers " |
||
2254 | " WHERE id = %s ", |
||
2255 | (new_values['cost_center']['id'],)) |
||
2256 | row = cursor.fetchone() |
||
2257 | if row is None: |
||
2258 | cursor.close() |
||
2259 | cnx.close() |
||
2260 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2261 | description='API.COST_CENTER_NOT_FOUND') |
||
2262 | |||
2263 | add_values = (" INSERT INTO tbl_shopfloors " |
||
2264 | " (name, uuid, area, is_input_counted, " |
||
2265 | " contact_id, cost_center_id, description) " |
||
2266 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
2267 | cursor.execute(add_values, (name, |
||
2268 | str(uuid.uuid4()), |
||
2269 | area, |
||
2270 | is_input_counted, |
||
2271 | contact_id, |
||
2272 | cost_center_id, |
||
2273 | description)) |
||
2274 | new_id = cursor.lastrowid |
||
2275 | if new_values['equipments'] is not None and len(new_values['equipments']) > 0: |
||
2276 | for equipment in new_values['equipments']: |
||
2277 | cursor.execute(" SELECT name " |
||
2278 | " FROM tbl_equipments " |
||
2279 | " WHERE id = %s ", (equipment['id'],)) |
||
2280 | if cursor.fetchone() is None: |
||
2281 | cursor.close() |
||
2282 | cnx.close() |
||
2283 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2284 | description='API.EQUIPMENT_NOT_FOUND') |
||
2285 | |||
2286 | query = (" SELECT id " |
||
2287 | " FROM tbl_shopfloors_equipments " |
||
2288 | " WHERE shopfloor_id = %s AND equipment_id = %s") |
||
2289 | cursor.execute(query, (new_id, equipment['id'],)) |
||
2290 | if cursor.fetchone() is not None: |
||
2291 | cursor.close() |
||
2292 | cnx.close() |
||
2293 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2294 | description='API.SHOPFLOOR_EQUIPMENT_RELATION_EXISTS') |
||
2295 | |||
2296 | add_row = (" INSERT INTO tbl_shopfloors_equipments (shopfloor_id, equipment_id) " |
||
2297 | " VALUES (%s, %s) ") |
||
2298 | cursor.execute(add_row, (new_id, equipment['id'],)) |
||
2299 | if new_values['commands'] is not None and len(new_values['commands']) > 0: |
||
2300 | for command in new_values['commands']: |
||
2301 | cursor.execute(" SELECT name " |
||
2302 | " FROM tbl_commands " |
||
2303 | " WHERE id = %s ", (command['id'],)) |
||
2304 | if cursor.fetchone() is None: |
||
2305 | cursor.close() |
||
2306 | cnx.close() |
||
2307 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2308 | description='API.COMMAND_NOT_FOUND') |
||
2309 | |||
2310 | query = (" SELECT id " |
||
2311 | " FROM tbl_shopfloors_commands " |
||
2312 | " WHERE shopfloor_id = %s AND command_id = %s") |
||
2313 | cursor.execute(query, (new_id, command['id'],)) |
||
2314 | if cursor.fetchone() is not None: |
||
2315 | cursor.close() |
||
2316 | cnx.close() |
||
2317 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2318 | description='API.SHOPFLOOR_COMMAND_RELATION_EXISTS') |
||
2319 | |||
2320 | add_row = (" INSERT INTO tbl_shopfloors_commands (shopfloor_id, command_id) " |
||
2321 | " VALUES (%s, %s) ") |
||
2322 | cursor.execute(add_row, (new_id, command['id'],)) |
||
2323 | if new_values['meters'] is not None and len(new_values['meters']) > 0: |
||
2324 | for meter in new_values['meters']: |
||
2325 | cursor.execute(" SELECT name " |
||
2326 | " FROM tbl_meters " |
||
2327 | " WHERE id = %s ", (meter['id'],)) |
||
2328 | if cursor.fetchone() is None: |
||
2329 | cursor.close() |
||
2330 | cnx.close() |
||
2331 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2332 | description='API.METER_NOT_FOUND') |
||
2333 | |||
2334 | query = (" SELECT id " |
||
2335 | " FROM tbl_shopfloors_meters " |
||
2336 | " WHERE shopfloor_id = %s AND meter_id = %s") |
||
2337 | cursor.execute(query, (new_id, meter['id'],)) |
||
2338 | if cursor.fetchone() is not None: |
||
2339 | cursor.close() |
||
2340 | cnx.close() |
||
2341 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2342 | description='API.SHOPFLOOR_METER_RELATION_EXISTS') |
||
2343 | |||
2344 | add_row = (" INSERT INTO tbl_shopfloors_meters (shopfloor_id, meter_id) " |
||
2345 | " VALUES (%s, %s) ") |
||
2346 | cursor.execute(add_row, (new_id, meter['id'],)) |
||
2347 | if new_values['offline_meters'] is not None and len(new_values['offline_meters']) > 0: |
||
2348 | for offline_meter in new_values['offline_meters']: |
||
2349 | cursor.execute(" SELECT name " |
||
2350 | " FROM tbl_offline_meters " |
||
2351 | " WHERE id = %s ", (offline_meter['id'],)) |
||
2352 | if cursor.fetchone() is None: |
||
2353 | cursor.close() |
||
2354 | cnx.close() |
||
2355 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2356 | description='API.OFFLINE_METER_NOT_FOUND') |
||
2357 | |||
2358 | query = (" SELECT id " |
||
2359 | " FROM tbl_shopfloors_offline_meters " |
||
2360 | " WHERE shopfloor_id = %s AND offline_meter_id = %s") |
||
2361 | cursor.execute(query, (new_id, offline_meter['id'],)) |
||
2362 | if cursor.fetchone() is not None: |
||
2363 | cursor.close() |
||
2364 | cnx.close() |
||
2365 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2366 | description='API.SHOPFLOOR_OFFLINE_METER_RELATION_EXISTS') |
||
2367 | |||
2368 | add_row = (" INSERT INTO tbl_shopfloors_offline_meters (shopfloor_id, offline_meter_id) " |
||
2369 | " VALUES (%s, %s) ") |
||
2370 | cursor.execute(add_row, (new_id, offline_meter['id'],)) |
||
2371 | if new_values['virtual_meters'] is not None and len(new_values['virtual_meters']) > 0: |
||
2372 | for virtual_meter in new_values['virtual_meters']: |
||
2373 | cursor.execute(" SELECT name " |
||
2374 | " FROM tbl_virtual_meters " |
||
2375 | " WHERE id = %s ", (virtual_meter['id'],)) |
||
2376 | if cursor.fetchone() is None: |
||
2377 | cursor.close() |
||
2378 | cnx.close() |
||
2379 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2380 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
2381 | |||
2382 | query = (" SELECT id " |
||
2383 | " FROM tbl_shopfloors_virtual_meters " |
||
2384 | " WHERE shopfloor_id = %s AND virtual_meter_id = %s") |
||
2385 | cursor.execute(query, (new_id, virtual_meter['id'],)) |
||
2386 | if cursor.fetchone() is not None: |
||
2387 | cursor.close() |
||
2388 | cnx.close() |
||
2389 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2390 | description='API.SHOPFLOOR_VIRTUAL_METER_RELATION_EXISTS') |
||
2391 | |||
2392 | add_row = (" INSERT INTO tbl_shopfloors_virtual_meters (shopfloor_id, virtual_meter_id) " |
||
2393 | " VALUES (%s, %s) ") |
||
2394 | cursor.execute(add_row, (new_id, virtual_meter['id'],)) |
||
2395 | if new_values['points'] is not None and len(new_values['points']) > 0: |
||
2396 | for point in new_values['points']: |
||
2397 | cursor.execute(" SELECT name " |
||
2398 | " FROM tbl_points " |
||
2399 | " WHERE id = %s ", (point['id'],)) |
||
2400 | if cursor.fetchone() is None: |
||
2401 | cursor.close() |
||
2402 | cnx.close() |
||
2403 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2404 | description='API.POINT_NOT_FOUND') |
||
2405 | |||
2406 | query = (" SELECT id " |
||
2407 | " FROM tbl_shopfloors_points " |
||
2408 | " WHERE shopfloor_id = %s AND point_id = %s") |
||
2409 | cursor.execute(query, (new_id, point['id'],)) |
||
2410 | if cursor.fetchone() is not None: |
||
2411 | cursor.close() |
||
2412 | cnx.close() |
||
2413 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2414 | description='API.SHOPFLOOR_POINT_RELATION_EXISTS') |
||
2415 | |||
2416 | add_row = (" INSERT INTO tbl_shopfloors_points (shopfloor_id, point_id) " |
||
2417 | " VALUES (%s, %s) ") |
||
2418 | cursor.execute(add_row, (new_id, point['id'],)) |
||
2419 | if new_values['sensors'] is not None and len(new_values['sensors']) > 0: |
||
2420 | for sensor in new_values['sensors']: |
||
2421 | cursor.execute(" SELECT name " |
||
2422 | " FROM tbl_sensors " |
||
2423 | " WHERE id = %s ", (sensor['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.SENSOR_NOT_FOUND') |
||
2429 | |||
2430 | query = (" SELECT id " |
||
2431 | " FROM tbl_shopfloors_sensors " |
||
2432 | " WHERE shopfloor_id = %s AND sensor_id = %s") |
||
2433 | cursor.execute(query, (new_id, sensor['id'],)) |
||
2434 | if cursor.fetchone() is not None: |
||
2435 | cursor.close() |
||
2436 | cnx.close() |
||
2437 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2438 | description='API.SHOPFLOOR_SENSOR_RELATION_EXISTS') |
||
2439 | |||
2440 | add_row = (" INSERT INTO tbl_shopfloors_sensors (shopfloor_id, sensor_id) " |
||
2441 | " VALUES (%s, %s) ") |
||
2442 | cursor.execute(add_row, (new_id, sensor['id'],)) |
||
2443 | if new_values['working_calendars'] is not None and len(new_values['workingcalendars']) > 0: |
||
2444 | for working_calendar in new_values['working_calendars']: |
||
2445 | cursor.execute(" SELECT name " |
||
2446 | " FROM tbl_working_calendars " |
||
2447 | " WHERE id = %s ", (working_calendar['id'],)) |
||
2448 | if cursor.fetchone() is None: |
||
2449 | cursor.close() |
||
2450 | cnx.close() |
||
2451 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2452 | description='API.WORKING_CALENDAR_NOT_FOUND') |
||
2453 | |||
2454 | query = (" SELECT id " |
||
2455 | " FROM tbl_shopfloors_working_calendars " |
||
2456 | " WHERE shopfloor_id = %s AND working_calendar_id = %s") |
||
2457 | cursor.execute(query, (new_id, working_calendar['id'],)) |
||
2458 | if cursor.fetchone() is not None: |
||
2459 | cursor.close() |
||
2460 | cnx.close() |
||
2461 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2462 | description='API.SHOPFLOOR_WORKING_CALENDAR_RELATION_EXISTS') |
||
2463 | |||
2464 | add_row = (" INSERT INTO tbl_shopfloors_working_calendars (shopfloor_id, working_calendar_id) " |
||
2465 | " VALUES (%s, %s) ") |
||
2466 | cursor.execute(add_row, (new_id, working_calendar['id'],)) |
||
2467 | cnx.commit() |
||
2468 | cursor.close() |
||
2469 | cnx.close() |
||
2470 | |||
2471 | resp.status = falcon.HTTP_201 |
||
2472 | resp.location = '/shopfloors/' + str(new_id) |
||
2473 | |||
2474 | |||
2475 | class ShopfloorClone: |
||
2476 | def __init__(self): |
||
2477 | """Initializes Shopfloorclone""" |
||
2478 | pass |
||
2479 | |||
2480 | @staticmethod |
||
2481 | def on_options(req, resp, id_): |
||
2482 | resp.status = falcon.HTTP_200 |
||
2483 | |||
2484 | @staticmethod |
||
2485 | def on_post(req, resp, id_): |
||
2486 | if 'API-KEY' not in req.headers or \ |
||
2487 | not isinstance(req.headers['API-KEY'], str) or \ |
||
2488 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
2489 | access_control(req) |
||
2490 | else: |
||
2491 | api_key_control(req) |
||
2492 | if not id_.isdigit() or int(id_) <= 0: |
||
2493 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2494 | description='API.INVALID_SHOPFLOOR_ID') |
||
2495 | |||
2496 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
2497 | cursor = cnx.cursor() |
||
2498 | |||
2499 | query = (" SELECT id, name, uuid " |
||
2500 | " FROM tbl_contacts ") |
||
2501 | cursor.execute(query) |
||
2502 | rows_contacts = cursor.fetchall() |
||
2503 | |||
2504 | contact_dict = dict() |
||
2505 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
2506 | for row in rows_contacts: |
||
2507 | contact_dict[row[0]] = {"id": row[0], |
||
2508 | "name": row[1], |
||
2509 | "uuid": row[2]} |
||
2510 | |||
2511 | query = (" SELECT id, name, uuid " |
||
2512 | " FROM tbl_cost_centers ") |
||
2513 | cursor.execute(query) |
||
2514 | rows_cost_centers = cursor.fetchall() |
||
2515 | |||
2516 | cost_center_dict = dict() |
||
2517 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
2518 | for row in rows_cost_centers: |
||
2519 | cost_center_dict[row[0]] = {"id": row[0], |
||
2520 | "name": row[1], |
||
2521 | "uuid": row[2]} |
||
2522 | |||
2523 | query = (" SELECT id, name, uuid, " |
||
2524 | " area, is_input_counted, contact_id, cost_center_id, description " |
||
2525 | " FROM tbl_shopfloors " |
||
2526 | " WHERE id = %s ") |
||
2527 | cursor.execute(query, (id_,)) |
||
2528 | row = cursor.fetchone() |
||
2529 | |||
2530 | if row is None: |
||
2531 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2532 | description='API.SHOPFLOOR_NOT_FOUND') |
||
2533 | else: |
||
2534 | meta_result = {"id": row[0], |
||
2535 | "name": row[1], |
||
2536 | "uuid": row[2], |
||
2537 | "area": row[3], |
||
2538 | "is_input_counted": bool(row[4]), |
||
2539 | "contact": contact_dict.get(row[5], None), |
||
2540 | "cost_center": cost_center_dict.get(row[6], None), |
||
2541 | "description": row[7], |
||
2542 | "equipments": None, |
||
2543 | "commands": None, |
||
2544 | "meters": None, |
||
2545 | "offline_meters": None, |
||
2546 | "virtual_meters": None, |
||
2547 | "points": None, |
||
2548 | "sensors": None, |
||
2549 | "working_calendars": None |
||
2550 | } |
||
2551 | query = (" SELECT e.id, e.name, e.uuid " |
||
2552 | " FROM tbl_shopfloors s, tbl_shopfloors_equipments se, tbl_equipments e " |
||
2553 | " WHERE se.shopfloor_id = s.id AND e.id = se.equipment_id AND s.id = %s " |
||
2554 | " ORDER BY e.id ") |
||
2555 | cursor.execute(query, (id_,)) |
||
2556 | rows = cursor.fetchall() |
||
2557 | |||
2558 | equipment_result = list() |
||
2559 | if rows is not None and len(rows) > 0: |
||
2560 | for row in rows: |
||
2561 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
2562 | equipment_result.append(result) |
||
2563 | meta_result['equipments'] = equipment_result |
||
2564 | query = (" SELECT c.id, c.name, c.uuid " |
||
2565 | " FROM tbl_shopfloors s, tbl_shopfloors_commands sc, tbl_commands c " |
||
2566 | " WHERE sc.shopfloor_id = s.id AND c.id = sc.command_id AND s.id = %s " |
||
2567 | " ORDER BY c.id ") |
||
2568 | cursor.execute(query, (id_,)) |
||
2569 | rows = cursor.fetchall() |
||
2570 | |||
2571 | command_result = list() |
||
2572 | if rows is not None and len(rows) > 0: |
||
2573 | for row in rows: |
||
2574 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
2575 | command_result.append(result) |
||
2576 | meta_result['commands'] = command_result |
||
2577 | query = (" SELECT id, name, uuid " |
||
2578 | " FROM tbl_energy_categories ") |
||
2579 | cursor.execute(query) |
||
2580 | rows_energy_categories = cursor.fetchall() |
||
2581 | |||
2582 | energy_category_dict = dict() |
||
2583 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
2584 | for row in rows_energy_categories: |
||
2585 | energy_category_dict[row[0]] = {"id": row[0], |
||
2586 | "name": row[1], |
||
2587 | "uuid": row[2]} |
||
2588 | |||
2589 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
2590 | " FROM tbl_shopfloors s, tbl_shopfloors_meters sm, tbl_meters m " |
||
2591 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.meter_id AND s.id = %s " |
||
2592 | " ORDER BY m.id ") |
||
2593 | cursor.execute(query, (id_,)) |
||
2594 | rows = cursor.fetchall() |
||
2595 | |||
2596 | meter_result = list() |
||
2597 | if rows is not None and len(rows) > 0: |
||
2598 | for row in rows: |
||
2599 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
2600 | "energy_category": energy_category_dict.get(row[3], None)} |
||
2601 | meter_result.append(result) |
||
2602 | meta_result['meters'] = meter_result |
||
2603 | cursor.execute(" SELECT name " |
||
2604 | " FROM tbl_shopfloors " |
||
2605 | " WHERE id = %s ", (id_,)) |
||
2606 | if cursor.fetchone() is None: |
||
2607 | cursor.close() |
||
2608 | cnx.close() |
||
2609 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2610 | description='API.SHOPFLOOR_NOT_FOUND') |
||
2611 | |||
2612 | query = (" SELECT id, name, uuid " |
||
2613 | " FROM tbl_energy_categories ") |
||
2614 | cursor.execute(query) |
||
2615 | rows_energy_categories = cursor.fetchall() |
||
2616 | |||
2617 | energy_category_dict = dict() |
||
2618 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
2619 | for row in rows_energy_categories: |
||
2620 | energy_category_dict[row[0]] = {"id": row[0], |
||
2621 | "name": row[1], |
||
2622 | "uuid": row[2]} |
||
2623 | |||
2624 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
2625 | " FROM tbl_shopfloors s, tbl_shopfloors_offline_meters sm, tbl_offline_meters m " |
||
2626 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
||
2627 | " ORDER BY m.id ") |
||
2628 | cursor.execute(query, (id_,)) |
||
2629 | rows = cursor.fetchall() |
||
2630 | |||
2631 | offlinemeter_result = list() |
||
2632 | if rows is not None and len(rows) > 0: |
||
2633 | for row in rows: |
||
2634 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
2635 | "energy_category": energy_category_dict.get(row[3], None)} |
||
2636 | offlinemeter_result.append(result) |
||
2637 | meta_result['offline_meters'] = offlinemeter_result |
||
2638 | query = (" SELECT id, name, uuid " |
||
2639 | " FROM tbl_energy_categories ") |
||
2640 | cursor.execute(query) |
||
2641 | rows_energy_categories = cursor.fetchall() |
||
2642 | |||
2643 | energy_category_dict = dict() |
||
2644 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
2645 | for row in rows_energy_categories: |
||
2646 | energy_category_dict[row[0]] = {"id": row[0], |
||
2647 | "name": row[1], |
||
2648 | "uuid": row[2]} |
||
2649 | |||
2650 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
2651 | " FROM tbl_shopfloors s, tbl_shopfloors_virtual_meters sm, tbl_virtual_meters m " |
||
2652 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.virtual_meter_id AND s.id = %s " |
||
2653 | " ORDER BY m.id ") |
||
2654 | cursor.execute(query, (id_,)) |
||
2655 | rows = cursor.fetchall() |
||
2656 | |||
2657 | virtualmeter_result = list() |
||
2658 | if rows is not None and len(rows) > 0: |
||
2659 | for row in rows: |
||
2660 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
2661 | "energy_category": energy_category_dict.get(row[3], None)} |
||
2662 | virtualmeter_result.append(result) |
||
2663 | meta_result['virtual_meters'] = virtualmeter_result |
||
2664 | query = (" SELECT id, name, uuid " |
||
2665 | " FROM tbl_data_sources ") |
||
2666 | cursor.execute(query) |
||
2667 | rows_data_sources = cursor.fetchall() |
||
2668 | |||
2669 | data_source_dict = dict() |
||
2670 | if rows_data_sources is not None and len(rows_data_sources) > 0: |
||
2671 | for row in rows_data_sources: |
||
2672 | data_source_dict[row[0]] = {"id": row[0], |
||
2673 | "name": row[1], |
||
2674 | "uuid": row[2]} |
||
2675 | |||
2676 | query = (" SELECT p.id, p.name, p.data_source_id " |
||
2677 | " FROM tbl_shopfloors s, tbl_shopfloors_points sp, tbl_points p " |
||
2678 | " WHERE sp.shopfloor_id = s.id AND p.id = sp.point_id AND s.id = %s " |
||
2679 | " ORDER BY p.id ") |
||
2680 | cursor.execute(query, (id_,)) |
||
2681 | rows = cursor.fetchall() |
||
2682 | |||
2683 | point_result = list() |
||
2684 | if rows is not None and len(rows) > 0: |
||
2685 | for row in rows: |
||
2686 | result = {"id": row[0], "name": row[1], "data_source": data_source_dict.get(row[2], None)} |
||
2687 | point_result.append(result) |
||
2688 | meta_result['points'] = point_result |
||
2689 | query = (" SELECT se.id, se.name, se.uuid " |
||
2690 | " FROM tbl_shopfloors sp, tbl_shopfloors_sensors ss, tbl_sensors se " |
||
2691 | " WHERE ss.shopfloor_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s " |
||
2692 | " ORDER BY se.id ") |
||
2693 | cursor.execute(query, (id_,)) |
||
2694 | rows = cursor.fetchall() |
||
2695 | |||
2696 | sensor_result = list() |
||
2697 | if rows is not None and len(rows) > 0: |
||
2698 | for row in rows: |
||
2699 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
2700 | sensor_result.append(result) |
||
2701 | meta_result['sensors'] = sensor_result |
||
2702 | query = (" SELECT wc.id, wc.name, wc.description " |
||
2703 | " FROM tbl_shopfloors s, tbl_shopfloors_working_calendars swc, tbl_working_calendars wc " |
||
2704 | " WHERE swc.shopfloor_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
||
2705 | " ORDER BY wc.id ") |
||
2706 | cursor.execute(query, (id_,)) |
||
2707 | rows = cursor.fetchall() |
||
2708 | |||
2709 | workingcalendar_result = list() |
||
2710 | if rows is not None and len(rows) > 0: |
||
2711 | for row in rows: |
||
2712 | result = {"id": row[0], "name": row[1], "description": row[2]} |
||
2713 | workingcalendar_result.append(result) |
||
2714 | meta_result['working_calendars'] = workingcalendar_result |
||
2715 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
2716 | if config.utc_offset[0] == '-': |
||
2717 | timezone_offset = -timezone_offset |
||
2718 | new_name = (str.strip(meta_result['name']) + |
||
2719 | (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')) |
||
2720 | add_values = (" INSERT INTO tbl_shopfloors " |
||
2721 | " (name, uuid, area, is_input_counted, " |
||
2722 | " contact_id, cost_center_id, description) " |
||
2723 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
2724 | cursor.execute(add_values, (new_name, |
||
2725 | str(uuid.uuid4()), |
||
2726 | meta_result['area'], |
||
2727 | meta_result['is_input_counted'], |
||
2728 | meta_result['contact']['id'], |
||
2729 | meta_result['cost_center']['id'], |
||
2730 | meta_result['description'])) |
||
2731 | new_id = cursor.lastrowid |
||
2732 | if meta_result['equipments'] is not None and len(meta_result['equipments']) > 0: |
||
2733 | for equipment in meta_result['equipments']: |
||
2734 | cursor.execute(" SELECT name " |
||
2735 | " FROM tbl_equipments " |
||
2736 | " WHERE id = %s ", (equipment['id'],)) |
||
2737 | if cursor.fetchone() is None: |
||
2738 | cursor.close() |
||
2739 | cnx.close() |
||
2740 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2741 | description='API.EQUIPMENT_NOT_FOUND') |
||
2742 | |||
2743 | query = (" SELECT id " |
||
2744 | " FROM tbl_shopfloors_equipments " |
||
2745 | " WHERE shopfloor_id = %s AND equipment_id = %s") |
||
2746 | cursor.execute(query, (new_id, equipment['id'],)) |
||
2747 | if cursor.fetchone() is not None: |
||
2748 | cursor.close() |
||
2749 | cnx.close() |
||
2750 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2751 | description='API.SHOPFLOOR_EQUIPMENT_RELATION_EXISTS') |
||
2752 | |||
2753 | add_row = (" INSERT INTO tbl_shopfloors_equipments (shopfloor_id, equipment_id) " |
||
2754 | " VALUES (%s, %s) ") |
||
2755 | cursor.execute(add_row, (new_id, equipment['id'],)) |
||
2756 | if meta_result['commands'] is not None and len(meta_result['commands']) > 0: |
||
2757 | for command in meta_result['commands']: |
||
2758 | cursor.execute(" SELECT name " |
||
2759 | " FROM tbl_commands " |
||
2760 | " WHERE id = %s ", (command['id'],)) |
||
2761 | if cursor.fetchone() is None: |
||
2762 | cursor.close() |
||
2763 | cnx.close() |
||
2764 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2765 | description='API.COMMAND_NOT_FOUND') |
||
2766 | |||
2767 | query = (" SELECT id " |
||
2768 | " FROM tbl_shopfloors_commands " |
||
2769 | " WHERE shopfloor_id = %s AND command_id = %s") |
||
2770 | cursor.execute(query, (new_id, command['id'],)) |
||
2771 | if cursor.fetchone() is not None: |
||
2772 | cursor.close() |
||
2773 | cnx.close() |
||
2774 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2775 | description='API.SHOPFLOOR_COMMAND_RELATION_EXISTS') |
||
2776 | |||
2777 | add_row = (" INSERT INTO tbl_shopfloors_commands (shopfloor_id, command_id) " |
||
2778 | " VALUES (%s, %s) ") |
||
2779 | cursor.execute(add_row, (new_id, command['id'],)) |
||
2780 | if meta_result['meters'] is not None and len(meta_result['meters']) > 0: |
||
2781 | for meter in meta_result['meters']: |
||
2782 | cursor.execute(" SELECT name " |
||
2783 | " FROM tbl_meters " |
||
2784 | " WHERE id = %s ", (meter['id'],)) |
||
2785 | if cursor.fetchone() is None: |
||
2786 | cursor.close() |
||
2787 | cnx.close() |
||
2788 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2789 | description='API.METER_NOT_FOUND') |
||
2790 | |||
2791 | query = (" SELECT id " |
||
2792 | " FROM tbl_shopfloors_meters " |
||
2793 | " WHERE shopfloor_id = %s AND meter_id = %s") |
||
2794 | cursor.execute(query, (new_id, meter['id'],)) |
||
2795 | if cursor.fetchone() is not None: |
||
2796 | cursor.close() |
||
2797 | cnx.close() |
||
2798 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2799 | description='API.SHOPFLOOR_METER_RELATION_EXISTS') |
||
2800 | |||
2801 | add_row = (" INSERT INTO tbl_shopfloors_meters (shopfloor_id, meter_id) " |
||
2802 | " VALUES (%s, %s) ") |
||
2803 | cursor.execute(add_row, (new_id, meter['id'],)) |
||
2804 | if meta_result['offline_meters'] is not None and len(meta_result['offline_meters']) > 0: |
||
2805 | for offline_meter in meta_result['offline_meters']: |
||
2806 | cursor.execute(" SELECT name " |
||
2807 | " FROM tbl_offline_meters " |
||
2808 | " WHERE id = %s ", (offline_meter['id'],)) |
||
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.OFFLINE_METER_NOT_FOUND') |
||
2814 | |||
2815 | query = (" SELECT id " |
||
2816 | " FROM tbl_shopfloors_offline_meters " |
||
2817 | " WHERE shopfloor_id = %s AND offline_meter_id = %s") |
||
2818 | cursor.execute(query, (new_id, offline_meter['id'],)) |
||
2819 | if cursor.fetchone() is not None: |
||
2820 | cursor.close() |
||
2821 | cnx.close() |
||
2822 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2823 | description='API.SHOPFLOOR_OFFLINE_METER_RELATION_EXISTS') |
||
2824 | |||
2825 | add_row = (" INSERT INTO tbl_shopfloors_offline_meters (shopfloor_id, offline_meter_id) " |
||
2826 | " VALUES (%s, %s) ") |
||
2827 | cursor.execute(add_row, (new_id, offline_meter['id'],)) |
||
2828 | if meta_result['virtual_meters'] is not None and len(meta_result['virtual_meters']) > 0: |
||
2829 | for virtual_meter in meta_result['virtual_meters']: |
||
2830 | cursor.execute(" SELECT name " |
||
2831 | " FROM tbl_virtual_meters " |
||
2832 | " WHERE id = %s ", (virtual_meter['id'],)) |
||
2833 | if cursor.fetchone() is None: |
||
2834 | cursor.close() |
||
2835 | cnx.close() |
||
2836 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2837 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
2838 | |||
2839 | query = (" SELECT id " |
||
2840 | " FROM tbl_shopfloors_virtual_meters " |
||
2841 | " WHERE shopfloor_id = %s AND virtual_meter_id = %s") |
||
2842 | cursor.execute(query, (new_id, virtual_meter['id'],)) |
||
2843 | if cursor.fetchone() is not None: |
||
2844 | cursor.close() |
||
2845 | cnx.close() |
||
2846 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2847 | description='API.SHOPFLOOR_VIRTUAL_METER_RELATION_EXISTS') |
||
2848 | |||
2849 | add_row = (" INSERT INTO tbl_shopfloors_virtual_meters (shopfloor_id, virtual_meter_id) " |
||
2850 | " VALUES (%s, %s) ") |
||
2851 | cursor.execute(add_row, (new_id, virtual_meter['id'],)) |
||
2852 | if meta_result['points'] is not None and len(meta_result['points']) > 0: |
||
2853 | for point in meta_result['points']: |
||
2854 | cursor.execute(" SELECT name " |
||
2855 | " FROM tbl_points " |
||
2856 | " WHERE id = %s ", (point['id'],)) |
||
2857 | if cursor.fetchone() is None: |
||
2858 | cursor.close() |
||
2859 | cnx.close() |
||
2860 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2861 | description='API.POINT_NOT_FOUND') |
||
2862 | |||
2863 | query = (" SELECT id " |
||
2864 | " FROM tbl_shopfloors_points " |
||
2865 | " WHERE shopfloor_id = %s AND point_id = %s") |
||
2866 | cursor.execute(query, (new_id, point['id'],)) |
||
2867 | if cursor.fetchone() is not None: |
||
2868 | cursor.close() |
||
2869 | cnx.close() |
||
2870 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2871 | description='API.SHOPFLOOR_POINT_RELATION_EXISTS') |
||
2872 | |||
2873 | add_row = (" INSERT INTO tbl_shopfloors_points (shopfloor_id, point_id) " |
||
2874 | " VALUES (%s, %s) ") |
||
2875 | cursor.execute(add_row, (new_id, point['id'],)) |
||
2876 | if meta_result['sensors'] is not None and len(meta_result['sensors']) > 0: |
||
2877 | for sensor in meta_result['sensors']: |
||
2878 | cursor.execute(" SELECT name " |
||
2879 | " FROM tbl_sensors " |
||
2880 | " WHERE id = %s ", (sensor['id'],)) |
||
2881 | if cursor.fetchone() is None: |
||
2882 | cursor.close() |
||
2883 | cnx.close() |
||
2884 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2885 | description='API.SENSOR_NOT_FOUND') |
||
2886 | |||
2887 | query = (" SELECT id " |
||
2888 | " FROM tbl_shopfloors_sensors " |
||
2889 | " WHERE shopfloor_id = %s AND sensor_id = %s") |
||
2890 | cursor.execute(query, (new_id, sensor['id'],)) |
||
2891 | if cursor.fetchone() is not None: |
||
2892 | cursor.close() |
||
2893 | cnx.close() |
||
2894 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2895 | description='API.SHOPFLOOR_SENSOR_RELATION_EXISTS') |
||
2896 | |||
2897 | add_row = (" INSERT INTO tbl_shopfloors_sensors (shopfloor_id, sensor_id) " |
||
2898 | " VALUES (%s, %s) ") |
||
2899 | cursor.execute(add_row, (new_id, sensor['id'],)) |
||
2900 | if meta_result['working_calendars'] is not None and len(meta_result['workingcalendars']) > 0: |
||
2901 | for working_calendar in meta_result['working_calendars']: |
||
2902 | cursor.execute(" SELECT name " |
||
2903 | " FROM tbl_working_calendars " |
||
2904 | " WHERE id = %s ", (working_calendar['id'],)) |
||
2905 | if cursor.fetchone() is None: |
||
2906 | cursor.close() |
||
2907 | cnx.close() |
||
2908 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2909 | description='API.WORKING_CALENDAR_NOT_FOUND') |
||
2910 | |||
2911 | query = (" SELECT id " |
||
2912 | " FROM tbl_shopfloors_working_calendars " |
||
2913 | " WHERE shopfloor_id = %s AND working_calendar_id = %s") |
||
2914 | cursor.execute(query, (new_id, working_calendar['id'],)) |
||
2915 | if cursor.fetchone() is not None: |
||
2916 | cursor.close() |
||
2917 | cnx.close() |
||
2918 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2919 | description='API.SHOPFLOOR_WORKING_CALENDAR_RELATION_EXISTS') |
||
2920 | |||
2921 | add_row = (" INSERT INTO tbl_shopfloors_working_calendars (shopfloor_id, working_calendar_id) " |
||
2922 | " VALUES (%s, %s) ") |
||
2923 | cursor.execute(add_row, (new_id, working_calendar['id'],)) |
||
2924 | cnx.commit() |
||
2925 | cursor.close() |
||
2926 | cnx.close() |
||
2927 | |||
2928 | resp.status = falcon.HTTP_201 |
||
2929 | resp.location = '/shopfloors/' + str(new_id) |
||
2930 |