1
|
|
|
from datetime import timedelta |
2
|
|
|
import mysql.connector |
3
|
|
|
import config |
4
|
|
|
import collections |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
######################################################################################################################## |
8
|
|
|
# Get carbon dioxide emission factor by energy category |
9
|
|
|
######################################################################################################################## |
10
|
|
View Code Duplication |
def get_energy_category_factor(energy_category_id, start_datetime_utc, end_datetime_utc): |
|
|
|
|
11
|
|
|
# todo: verify parameters |
12
|
|
|
# todo: add start_datetime_utc and end_datetime_utc to factor |
13
|
|
|
# get timezone offset in minutes, this value will be returned to client |
14
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
15
|
|
|
if config.utc_offset[0] == '-': |
16
|
|
|
timezone_offset = -timezone_offset |
17
|
|
|
|
18
|
|
|
cnx = None |
19
|
|
|
cursor = None |
20
|
|
|
try: |
21
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
22
|
|
|
cursor = cnx.cursor() |
23
|
|
|
query_factors = (" SELECT kgco2e " |
24
|
|
|
" FROM tbl_energy_categories " |
25
|
|
|
" WHERE energy_category_id = %s ") |
26
|
|
|
cursor.execute(query_factors, (energy_category_id,)) |
27
|
|
|
rows_factor = cursor.fetchone() |
28
|
|
|
except Exception as e: |
29
|
|
|
print(str(e)) |
30
|
|
|
return None |
31
|
|
|
finally: |
32
|
|
|
if cursor: |
33
|
|
|
cursor.close() |
34
|
|
|
if cnx: |
35
|
|
|
cnx.disconnect() |
36
|
|
|
|
37
|
|
|
if rows_factor is None: |
38
|
|
|
if cursor: |
39
|
|
|
cursor.close() |
40
|
|
|
if cnx: |
41
|
|
|
cnx.disconnect() |
42
|
|
|
return None |
43
|
|
|
else: |
44
|
|
|
return rows_factor[0] |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
######################################################################################################################## |
48
|
|
|
# Get carbon dioxide emission factor by energy item |
49
|
|
|
######################################################################################################################## |
50
|
|
View Code Duplication |
def get_energy_item_tariffs(energy_item_id, start_datetime_utc, end_datetime_utc): |
|
|
|
|
51
|
|
|
# todo: verify parameters |
52
|
|
|
# todo: add start_datetime_utc and end_datetime_utc to factor |
53
|
|
|
# get timezone offset in minutes, this value will be returned to client |
54
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
55
|
|
|
if config.utc_offset[0] == '-': |
56
|
|
|
timezone_offset = -timezone_offset |
57
|
|
|
|
58
|
|
|
cnx = None |
59
|
|
|
cursor = None |
60
|
|
|
try: |
61
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
62
|
|
|
cursor = cnx.cursor() |
63
|
|
|
query_factors = (" SELECT ec.kgco2e " |
64
|
|
|
" FROM tbl_energy_categories ec, tbl_energy_items ei " |
65
|
|
|
" WHERE ei.id = %s AND ei.energy_category_id = ec.id ") |
66
|
|
|
cursor.execute(query_factors, (energy_item_id,)) |
67
|
|
|
rows_factor = cursor.fetchone() |
68
|
|
|
except Exception as e: |
69
|
|
|
print(str(e)) |
70
|
|
|
return None |
71
|
|
|
finally: |
72
|
|
|
if cursor: |
73
|
|
|
cursor.close() |
74
|
|
|
if cnx: |
75
|
|
|
cnx.disconnect() |
76
|
|
|
|
77
|
|
|
if rows_factor is None: |
78
|
|
|
if cursor: |
79
|
|
|
cursor.close() |
80
|
|
|
if cnx: |
81
|
|
|
cnx.disconnect() |
82
|
|
|
return None |
83
|
|
|
else: |
84
|
|
|
return rows_factor[0] |
85
|
|
|
|