@@ 891-1017 (lines=127) @@ | ||
888 | resp.status = falcon.HTTP_200 |
|
889 | _ = id_ |
|
890 | ||
891 | @staticmethod |
|
892 | def on_get(req, resp, id_): |
|
893 | if 'API-KEY' not in req.headers or \ |
|
894 | not isinstance(req.headers['API-KEY'], str) or \ |
|
895 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
896 | access_control(req) |
|
897 | else: |
|
898 | api_key_control(req) |
|
899 | if not id_.isdigit() or int(id_) <= 0: |
|
900 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
901 | description='API.INVALID_VIRTUAL_METER_ID') |
|
902 | ||
903 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
904 | cursor = cnx.cursor() |
|
905 | ||
906 | query = (" SELECT id, name, uuid " |
|
907 | " FROM tbl_energy_categories ") |
|
908 | cursor.execute(query) |
|
909 | rows_energy_categories = cursor.fetchall() |
|
910 | ||
911 | energy_category_dict = dict() |
|
912 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
913 | for row in rows_energy_categories: |
|
914 | energy_category_dict[row[0]] = {"id": row[0], |
|
915 | "name": row[1], |
|
916 | "uuid": row[2]} |
|
917 | ||
918 | query = (" SELECT id, name, uuid, energy_category_id " |
|
919 | " FROM tbl_energy_items ") |
|
920 | cursor.execute(query) |
|
921 | rows_energy_items = cursor.fetchall() |
|
922 | ||
923 | energy_item_dict = dict() |
|
924 | if rows_energy_items is not None and len(rows_energy_items) > 0: |
|
925 | for row in rows_energy_items: |
|
926 | energy_item_dict[row[0]] = {"id": row[0], |
|
927 | "name": row[1], |
|
928 | "uuid": row[2]} |
|
929 | ||
930 | query = (" SELECT id, name, uuid " |
|
931 | " FROM tbl_cost_centers ") |
|
932 | cursor.execute(query) |
|
933 | rows_cost_centers = cursor.fetchall() |
|
934 | ||
935 | cost_center_dict = dict() |
|
936 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
|
937 | for row in rows_cost_centers: |
|
938 | cost_center_dict[row[0]] = {"id": row[0], |
|
939 | "name": row[1], |
|
940 | "uuid": row[2]} |
|
941 | ||
942 | query = (" SELECT id, name, uuid, equation, energy_category_id, is_counted, cost_center_id, " |
|
943 | " energy_item_id, description " |
|
944 | " FROM tbl_virtual_meters " |
|
945 | " WHERE id = %s ") |
|
946 | cursor.execute(query, (id_,)) |
|
947 | row = cursor.fetchone() |
|
948 | if row is None: |
|
949 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
950 | description='API.VIRTUAL_METER_NOT_FOUND') |
|
951 | else: |
|
952 | meta_result = {"id": row[0], |
|
953 | "name": row[1], |
|
954 | "uuid": row[2], |
|
955 | "equation": row[3], |
|
956 | "energy_category": energy_category_dict.get(row[4], None), |
|
957 | "is_counted": True if row[5] else False, |
|
958 | "cost_center": cost_center_dict.get(row[6], None), |
|
959 | "energy_item": energy_item_dict.get(row[7], None), |
|
960 | "description": row[8], |
|
961 | "expression": {}} |
|
962 | ||
963 | expression = dict() |
|
964 | ||
965 | if meta_result["equation"] is not None: |
|
966 | expression = {'equation': meta_result["equation"], 'variables': []} |
|
967 | ||
968 | query_variables = (" SELECT v.id, v.name, v.meter_type, v.meter_id " |
|
969 | " FROM tbl_virtual_meters vm, tbl_variables v " |
|
970 | " WHERE vm.id = %s AND v.virtual_meter_id = vm.id " |
|
971 | " ORDER BY v.name ") |
|
972 | cursor.execute(query_variables, (meta_result["id"],)) |
|
973 | rows_variables = cursor.fetchall() |
|
974 | if rows_variables is not None: |
|
975 | for row_variable in rows_variables: |
|
976 | if row_variable[2].lower() == 'meter': |
|
977 | query_meter = (" SELECT m.name " |
|
978 | " FROM tbl_meters m " |
|
979 | " WHERE m.id = %s ") |
|
980 | cursor.execute(query_meter, (row_variable[3],)) |
|
981 | row_meter = cursor.fetchone() |
|
982 | if row_meter is not None: |
|
983 | expression['variables'].append({'id': row_variable[0], |
|
984 | 'name': row_variable[1], |
|
985 | 'meter_type': row_variable[2], |
|
986 | 'meter_id': row_variable[3], |
|
987 | 'meter_name': row_meter[0]}) |
|
988 | elif row_variable[2].lower() == 'offline_meter': |
|
989 | query_meter = (" SELECT m.name " |
|
990 | " FROM tbl_offline_meters m " |
|
991 | " WHERE m.id = %s ") |
|
992 | cursor.execute(query_meter, (row_variable[3],)) |
|
993 | row_meter = cursor.fetchone() |
|
994 | if row_meter is not None: |
|
995 | expression['variables'].append({'id': row_variable[0], |
|
996 | 'name': row_variable[1], |
|
997 | 'meter_type': row_variable[2], |
|
998 | 'meter_id': row_variable[3], |
|
999 | 'meter_name': row_meter[0]}) |
|
1000 | elif row_variable[2].lower() == 'virtual_meter': |
|
1001 | query_meter = (" SELECT m.name " |
|
1002 | " FROM tbl_virtual_meters m " |
|
1003 | " WHERE m.id = %s ") |
|
1004 | cursor.execute(query_meter, (row_variable[3],)) |
|
1005 | row_meter = cursor.fetchone() |
|
1006 | if row_meter is not None: |
|
1007 | expression['variables'].append({'id': row_variable[0], |
|
1008 | 'name': row_variable[1], |
|
1009 | 'meter_type': row_variable[2], |
|
1010 | 'meter_id': row_variable[3], |
|
1011 | 'meter_name': row_meter[0]}) |
|
1012 | ||
1013 | meta_result['expression'] = expression |
|
1014 | ||
1015 | cursor.close() |
|
1016 | cnx.close() |
|
1017 | resp.text = json.dumps(meta_result) |
|
1018 | ||
1019 | ||
1020 | class VirtualMeterImport: |
|
@@ 358-484 (lines=127) @@ | ||
355 | resp.status = falcon.HTTP_200 |
|
356 | _ = id_ |
|
357 | ||
358 | @staticmethod |
|
359 | def on_get(req, resp, id_): |
|
360 | if 'API-KEY' not in req.headers or \ |
|
361 | not isinstance(req.headers['API-KEY'], str) or \ |
|
362 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
363 | access_control(req) |
|
364 | else: |
|
365 | api_key_control(req) |
|
366 | if not id_.isdigit() or int(id_) <= 0: |
|
367 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
368 | description='API.INVALID_VIRTUAL_METER_ID') |
|
369 | ||
370 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
371 | cursor = cnx.cursor() |
|
372 | ||
373 | query = (" SELECT id, name, uuid " |
|
374 | " FROM tbl_energy_categories ") |
|
375 | cursor.execute(query) |
|
376 | rows_energy_categories = cursor.fetchall() |
|
377 | ||
378 | energy_category_dict = dict() |
|
379 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
380 | for row in rows_energy_categories: |
|
381 | energy_category_dict[row[0]] = {"id": row[0], |
|
382 | "name": row[1], |
|
383 | "uuid": row[2]} |
|
384 | ||
385 | query = (" SELECT id, name, uuid, energy_category_id " |
|
386 | " FROM tbl_energy_items ") |
|
387 | cursor.execute(query) |
|
388 | rows_energy_items = cursor.fetchall() |
|
389 | ||
390 | energy_item_dict = dict() |
|
391 | if rows_energy_items is not None and len(rows_energy_items) > 0: |
|
392 | for row in rows_energy_items: |
|
393 | energy_item_dict[row[0]] = {"id": row[0], |
|
394 | "name": row[1], |
|
395 | "uuid": row[2]} |
|
396 | ||
397 | query = (" SELECT id, name, uuid " |
|
398 | " FROM tbl_cost_centers ") |
|
399 | cursor.execute(query) |
|
400 | rows_cost_centers = cursor.fetchall() |
|
401 | ||
402 | cost_center_dict = dict() |
|
403 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
|
404 | for row in rows_cost_centers: |
|
405 | cost_center_dict[row[0]] = {"id": row[0], |
|
406 | "name": row[1], |
|
407 | "uuid": row[2]} |
|
408 | ||
409 | query = (" SELECT id, name, uuid, equation, energy_category_id, is_counted, cost_center_id, " |
|
410 | " energy_item_id, description " |
|
411 | " FROM tbl_virtual_meters " |
|
412 | " WHERE id = %s ") |
|
413 | cursor.execute(query, (id_,)) |
|
414 | row = cursor.fetchone() |
|
415 | if row is None: |
|
416 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
417 | description='API.VIRTUAL_METER_NOT_FOUND') |
|
418 | else: |
|
419 | meta_result = {"id": row[0], |
|
420 | "name": row[1], |
|
421 | "uuid": row[2], |
|
422 | "equation": row[3], |
|
423 | "energy_category": energy_category_dict.get(row[4], None), |
|
424 | "is_counted": True if row[5] else False, |
|
425 | "cost_center": cost_center_dict.get(row[6], None), |
|
426 | "energy_item": energy_item_dict.get(row[7], None), |
|
427 | "description": row[8], |
|
428 | "expression": {}} |
|
429 | ||
430 | expression = dict() |
|
431 | ||
432 | if meta_result["equation"] is not None: |
|
433 | expression = {'equation': meta_result["equation"], 'variables': []} |
|
434 | ||
435 | query_variables = (" SELECT v.id, v.name, v.meter_type, v.meter_id " |
|
436 | " FROM tbl_virtual_meters vm, tbl_variables v " |
|
437 | " WHERE vm.id = %s AND v.virtual_meter_id = vm.id " |
|
438 | " ORDER BY v.name ") |
|
439 | cursor.execute(query_variables, (meta_result["id"],)) |
|
440 | rows_variables = cursor.fetchall() |
|
441 | if rows_variables is not None: |
|
442 | for row_variable in rows_variables: |
|
443 | if row_variable[2].lower() == 'meter': |
|
444 | query_meter = (" SELECT m.name " |
|
445 | " FROM tbl_meters m " |
|
446 | " WHERE m.id = %s ") |
|
447 | cursor.execute(query_meter, (row_variable[3],)) |
|
448 | row_meter = cursor.fetchone() |
|
449 | if row_meter is not None: |
|
450 | expression['variables'].append({'id': row_variable[0], |
|
451 | 'name': row_variable[1], |
|
452 | 'meter_type': row_variable[2], |
|
453 | 'meter_id': row_variable[3], |
|
454 | 'meter_name': row_meter[0]}) |
|
455 | elif row_variable[2].lower() == 'offline_meter': |
|
456 | query_meter = (" SELECT m.name " |
|
457 | " FROM tbl_offline_meters m " |
|
458 | " WHERE m.id = %s ") |
|
459 | cursor.execute(query_meter, (row_variable[3],)) |
|
460 | row_meter = cursor.fetchone() |
|
461 | if row_meter is not None: |
|
462 | expression['variables'].append({'id': row_variable[0], |
|
463 | 'name': row_variable[1], |
|
464 | 'meter_type': row_variable[2], |
|
465 | 'meter_id': row_variable[3], |
|
466 | 'meter_name': row_meter[0]}) |
|
467 | elif row_variable[2].lower() == 'virtual_meter': |
|
468 | query_meter = (" SELECT m.name " |
|
469 | " FROM tbl_virtual_meters m " |
|
470 | " WHERE m.id = %s ") |
|
471 | cursor.execute(query_meter, (row_variable[3],)) |
|
472 | row_meter = cursor.fetchone() |
|
473 | if row_meter is not None: |
|
474 | expression['variables'].append({'id': row_variable[0], |
|
475 | 'name': row_variable[1], |
|
476 | 'meter_type': row_variable[2], |
|
477 | 'meter_id': row_variable[3], |
|
478 | 'meter_name': row_meter[0]}) |
|
479 | ||
480 | meta_result['expression'] = expression |
|
481 | ||
482 | cursor.close() |
|
483 | cnx.close() |
|
484 | resp.text = json.dumps(meta_result) |
|
485 | ||
486 | @staticmethod |
|
487 | @user_logger |