diff --git a/myems-api/core/costfile.py b/myems-api/core/costfile.py index d5840ed6..ff249050 100644 --- a/myems-api/core/costfile.py +++ b/myems-api/core/costfile.py @@ -80,62 +80,60 @@ class CostFileCollection: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.USER_UUID_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') - cnx = mysql.connector.connect(**config.myems_user_db) - cursor = cnx.cursor() + cnx_user_db = mysql.connector.connect(**config.myems_user_db) + cursor_user_db = cnx_user_db.cursor() query = (" SELECT utc_expires " " FROM tbl_sessions " " WHERE user_uuid = %s AND token = %s") - cursor.execute(query, (user_uuid, token,)) - row = cursor.fetchone() + cursor_user_db.execute(query, (user_uuid, token,)) + row = cursor_user_db.fetchone() if row is None: - if cursor: - cursor.close() - if cnx: - cnx.disconnect() + if cursor_user_db: + cursor_user_db.close() + if cnx_user_db: + cnx_user_db.disconnect() raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SESSION_PLEASE_RE_LOGIN') else: utc_expires = row[0] if datetime.utcnow() > utc_expires: - if cursor: - cursor.close() - if cnx: - cnx.disconnect() + if cursor_user_db: + cursor_user_db.close() + if cnx_user_db: + cnx_user_db.disconnect() raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.USER_SESSION_TIMEOUT') - cursor.execute(" SELECT id " - " FROM tbl_users " - " WHERE uuid = %s ", - (user_uuid,)) - row = cursor.fetchone() + cursor_user_db.execute(" SELECT id " + " FROM tbl_users " + " WHERE uuid = %s ", + (user_uuid,)) + row = cursor_user_db.fetchone() if row is None: - if cursor: - cursor.close() - if cnx: - cnx.disconnect() + if cursor_user_db: + cursor_user_db.close() + if cnx_user_db: + cnx_user_db.disconnect() raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_USER_PLEASE_RE_LOGIN') - else: - user_id = row[0] - cnx = mysql.connector.connect(**config.myems_historical_db) - cursor = cnx.cursor() + cnx_historical_db = mysql.connector.connect(**config.myems_historical_db) + cursor_historical_db = cnx_historical_db.cursor() add_values = (" INSERT INTO tbl_cost_files " " (file_name, uuid, upload_datetime_utc, status, file_object ) " " VALUES (%s, %s, %s, %s, %s) ") - cursor.execute(add_values, (filename, - file_uuid, - datetime.utcnow(), - 'new', - raw_blob)) - new_id = cursor.lastrowid - cnx.commit() - cursor.close() - cnx.disconnect() + cursor_historical_db.execute(add_values, (filename, + file_uuid, + datetime.utcnow(), + 'new', + raw_blob)) + new_id = cursor_historical_db.lastrowid + cnx_historical_db.commit() + cursor_historical_db.close() + cnx_historical_db.disconnect() resp.status = falcon.HTTP_201 resp.location = '/costfiles/' + str(new_id) diff --git a/myems-api/excelexporters/equipmentincome.py b/myems-api/excelexporters/equipmentincome.py index 11a7171a..b6d44d1b 100644 --- a/myems-api/excelexporters/equipmentincome.py +++ b/myems-api/excelexporters/equipmentincome.py @@ -2,7 +2,7 @@ import base64 import uuid import os from openpyxl.chart import PieChart, LineChart, Reference -from decimal import * +from decimal import Decimal from openpyxl.styles import PatternFill, Border, Side, Alignment, Font from openpyxl.drawing.image import Image from openpyxl import Workbook diff --git a/myems-api/reports/combinedequipmentbatch.py b/myems-api/reports/combinedequipmentbatch.py index 7239d361..b69f3d6d 100644 --- a/myems-api/reports/combinedequipmentbatch.py +++ b/myems-api/reports/combinedequipmentbatch.py @@ -2,7 +2,7 @@ import falcon import simplejson as json import mysql.connector import config -from anytree import Node, AnyNode, LevelOrderIter +from anytree import AnyNode, LevelOrderIter from datetime import datetime, timedelta, timezone from decimal import Decimal import excelexporters.combinedequipmentbatch diff --git a/myems-api/reports/equipmentbatch.py b/myems-api/reports/equipmentbatch.py index 230b9543..8ab8a622 100644 --- a/myems-api/reports/equipmentbatch.py +++ b/myems-api/reports/equipmentbatch.py @@ -2,7 +2,7 @@ import falcon import simplejson as json import mysql.connector import config -from anytree import Node, AnyNode, LevelOrderIter +from anytree import AnyNode, LevelOrderIter from datetime import datetime, timedelta, timezone from decimal import Decimal import excelexporters.equipmentbatch diff --git a/myems-api/reports/equipmenttracking.py b/myems-api/reports/equipmenttracking.py index 71f1b08b..b790d21d 100644 --- a/myems-api/reports/equipmenttracking.py +++ b/myems-api/reports/equipmenttracking.py @@ -2,7 +2,7 @@ import falcon import simplejson as json import mysql.connector import config -from anytree import Node, AnyNode, LevelOrderIter +from anytree import AnyNode, LevelOrderIter import excelexporters.equipmenttracking diff --git a/myems-api/reports/meterbatch.py b/myems-api/reports/meterbatch.py index ce3e0a6a..2a81959c 100644 --- a/myems-api/reports/meterbatch.py +++ b/myems-api/reports/meterbatch.py @@ -2,9 +2,8 @@ import falcon import simplejson as json import mysql.connector import config -from anytree import Node, AnyNode, LevelOrderIter +from anytree import AnyNode, LevelOrderIter from datetime import datetime, timedelta, timezone -from decimal import Decimal import excelexporters.meterbatch diff --git a/myems-api/reports/metertracking.py b/myems-api/reports/metertracking.py index c392b9da..5023a612 100644 --- a/myems-api/reports/metertracking.py +++ b/myems-api/reports/metertracking.py @@ -2,7 +2,7 @@ import falcon import simplejson as json import mysql.connector import config -from anytree import Node, AnyNode, LevelOrderIter +from anytree import AnyNode, LevelOrderIter import excelexporters.metertracking from datetime import datetime, timedelta, timezone diff --git a/myems-api/reports/shopfloorbatch.py b/myems-api/reports/shopfloorbatch.py index 4c8be4b7..a58a4aaa 100644 --- a/myems-api/reports/shopfloorbatch.py +++ b/myems-api/reports/shopfloorbatch.py @@ -2,7 +2,7 @@ import falcon import simplejson as json import mysql.connector import config -from anytree import Node, AnyNode, LevelOrderIter +from anytree import AnyNode, LevelOrderIter from datetime import datetime, timedelta, timezone from decimal import Decimal import excelexporters.shopfloorbatch diff --git a/myems-api/reports/storebatch.py b/myems-api/reports/storebatch.py index ead5c122..46a18de9 100644 --- a/myems-api/reports/storebatch.py +++ b/myems-api/reports/storebatch.py @@ -2,7 +2,7 @@ import falcon import simplejson as json import mysql.connector import config -from anytree import Node, AnyNode, LevelOrderIter +from anytree import AnyNode, LevelOrderIter from datetime import datetime, timedelta, timezone from decimal import Decimal import excelexporters.storebatch diff --git a/myems-api/reports/tenantbatch.py b/myems-api/reports/tenantbatch.py index 1959d515..d4f5b14a 100644 --- a/myems-api/reports/tenantbatch.py +++ b/myems-api/reports/tenantbatch.py @@ -2,7 +2,7 @@ import falcon import simplejson as json import mysql.connector import config -from anytree import Node, AnyNode, LevelOrderIter +from anytree import AnyNode, LevelOrderIter from datetime import datetime, timedelta, timezone from decimal import Decimal import excelexporters.tenantbatch diff --git a/myems-modbus-tcp/acquisition.py b/myems-modbus-tcp/acquisition.py index 063384a7..a59988bb 100644 --- a/myems-modbus-tcp/acquisition.py +++ b/myems-modbus-tcp/acquisition.py @@ -283,7 +283,6 @@ def process(logger, data_source_id, host, port): except Exception as e: logger.error("Error in step 4.3.1 of acquisition process " + str(e)) # ignore this exception - pass # update tbl_analog_value_latest delete_values = " DELETE FROM tbl_analog_value_latest WHERE point_id IN ( " @@ -306,7 +305,6 @@ def process(logger, data_source_id, host, port): except Exception as e: logger.error("Error in step 4.3.2 of acquisition process " + str(e)) # ignore this exception - pass try: # trim ", " at the end of string and then execute @@ -315,7 +313,6 @@ def process(logger, data_source_id, host, port): except Exception as e: logger.error("Error in step 4.3.3 of acquisition process " + str(e)) # ignore this exception - pass if len(energy_value_list) > 0: add_values = (" INSERT INTO tbl_energy_value (point_id, utc_date_time, actual_value) " @@ -337,7 +334,6 @@ def process(logger, data_source_id, host, port): except Exception as e: logger.error("Error in step 4.4.1 of acquisition process: " + str(e)) # ignore this exception - pass # update tbl_energy_value_latest delete_values = " DELETE FROM tbl_energy_value_latest WHERE point_id IN ( " @@ -361,7 +357,6 @@ def process(logger, data_source_id, host, port): except Exception as e: logger.error("Error in step 4.4.2 of acquisition process " + str(e)) # ignore this exception - pass try: # trim ", " at the end of string and then execute @@ -371,7 +366,6 @@ def process(logger, data_source_id, host, port): except Exception as e: logger.error("Error in step 4.4.3 of acquisition process " + str(e)) # ignore this exception - pass if len(digital_value_list) > 0: add_values = (" INSERT INTO tbl_digital_value (point_id, utc_date_time, actual_value) " @@ -393,7 +387,6 @@ def process(logger, data_source_id, host, port): except Exception as e: logger.error("Error in step 4.5.1 of acquisition process: " + str(e)) # ignore this exception - pass # update tbl_digital_value_latest delete_values = " DELETE FROM tbl_digital_value_latest WHERE point_id IN ( " @@ -415,7 +408,6 @@ def process(logger, data_source_id, host, port): except Exception as e: logger.error("Error in step 4.5.2 of acquisition process " + str(e)) # ignore this exception - pass try: # trim ", " at the end of string and then execute @@ -424,7 +416,6 @@ def process(logger, data_source_id, host, port): except Exception as e: logger.error("Error in step 4.5.3 of acquisition process " + str(e)) # ignore this exception - pass # update data source last seen datetime update_row = (" UPDATE tbl_data_sources " @@ -436,7 +427,6 @@ def process(logger, data_source_id, host, port): except Exception as e: logger.error("Error in step 4.6 of acquisition process " + str(e)) # ignore this exception - pass # sleep and continue the next iteration of the inner while loop time.sleep(config.interval_in_seconds)