1 | import time |
||
2 | from datetime import datetime, timedelta |
||
3 | from decimal import Decimal |
||
4 | |||
5 | import mysql.connector |
||
6 | |||
7 | import carbon_dioxide_emmision_factor |
||
8 | import config |
||
9 | |||
10 | |||
11 | ######################################################################################################################## |
||
12 | # PROCEDURES |
||
13 | # Step 1: get all virtual meters |
||
14 | # for each virtual meter in list: |
||
15 | # Step 2: get the latest start_datetime_utc |
||
16 | # Step 3: get all energy data since the latest start_datetime_utc |
||
17 | # Step 4: get carbon dioxide emission factor |
||
18 | # Step 5: calculate carbon dioxide emission by multiplying energy with factor |
||
19 | # Step 6: save carbon dioxide emission data to database |
||
20 | ######################################################################################################################## |
||
21 | |||
22 | |||
23 | View Code Duplication | def main(logger): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
24 | |||
25 | while True: |
||
26 | # the outermost while loop |
||
27 | ################################################################################################################ |
||
28 | # Step 1: get all virtual meters |
||
29 | ################################################################################################################ |
||
30 | cnx_system_db = None |
||
31 | cursor_system_db = None |
||
32 | try: |
||
33 | cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
||
34 | cursor_system_db = cnx_system_db.cursor() |
||
35 | except Exception as e: |
||
36 | logger.error("Error in step 1.1 of virtual_meter_carbon " + str(e)) |
||
37 | if cursor_system_db: |
||
38 | cursor_system_db.close() |
||
39 | if cnx_system_db: |
||
40 | cnx_system_db.close() |
||
41 | # sleep and continue the outermost while loop |
||
42 | time.sleep(60) |
||
43 | continue |
||
44 | |||
45 | print("Connected to MyEMS System Database") |
||
46 | |||
47 | virtual_meter_list = list() |
||
48 | try: |
||
49 | cursor_system_db.execute(" SELECT id, name, energy_category_id, cost_center_id " |
||
50 | " FROM tbl_virtual_meters " |
||
51 | " ORDER BY id ") |
||
52 | rows_virtual_meters = cursor_system_db.fetchall() |
||
53 | |||
54 | if rows_virtual_meters is None or len(rows_virtual_meters) == 0: |
||
55 | print("Step 1.2: There isn't any virtual meters. ") |
||
56 | if cursor_system_db: |
||
57 | cursor_system_db.close() |
||
58 | if cnx_system_db: |
||
59 | cnx_system_db.close() |
||
60 | # sleep and continue the outermost while loop |
||
61 | time.sleep(60) |
||
62 | continue |
||
63 | |||
64 | for row in rows_virtual_meters: |
||
65 | virtual_meter_list.append({"id": row[0], |
||
66 | "name": row[1], |
||
67 | "energy_category_id": row[2], |
||
68 | "cost_center_id": row[3]}) |
||
69 | |||
70 | except Exception as e: |
||
71 | logger.error("Error in step 1.2 of virtual_meter_carbon " + str(e)) |
||
72 | if cursor_system_db: |
||
73 | cursor_system_db.close() |
||
74 | if cnx_system_db: |
||
75 | cnx_system_db.close() |
||
76 | # sleep and continue the outermost while loop |
||
77 | time.sleep(60) |
||
78 | continue |
||
79 | |||
80 | print("Step 1.2: Got all virtual meters from MyEMS System Database") |
||
81 | |||
82 | cnx_energy_db = None |
||
83 | cursor_energy_db = None |
||
84 | try: |
||
85 | cnx_energy_db = mysql.connector.connect(**config.myems_energy_db) |
||
86 | cursor_energy_db = cnx_energy_db.cursor() |
||
87 | except Exception as e: |
||
88 | logger.error("Error in step 1.3 of virtual_meter_carbon " + str(e)) |
||
89 | if cursor_energy_db: |
||
90 | cursor_energy_db.close() |
||
91 | if cnx_energy_db: |
||
92 | cnx_energy_db.close() |
||
93 | |||
94 | if cursor_system_db: |
||
95 | cursor_system_db.close() |
||
96 | if cnx_system_db: |
||
97 | cnx_system_db.close() |
||
98 | # sleep and continue the outermost while loop |
||
99 | time.sleep(60) |
||
100 | continue |
||
101 | |||
102 | print("Connected to MyEMS Energy Database") |
||
103 | |||
104 | cnx_carbon_db = None |
||
105 | cursor_carbon_db = None |
||
106 | try: |
||
107 | cnx_carbon_db = mysql.connector.connect(**config.myems_carbon_db) |
||
108 | cursor_carbon_db = cnx_carbon_db.cursor() |
||
109 | except Exception as e: |
||
110 | logger.error("Error in step 1.4 of virtual_meter_carbon " + str(e)) |
||
111 | if cursor_carbon_db: |
||
112 | cursor_carbon_db.close() |
||
113 | if cnx_carbon_db: |
||
114 | cnx_carbon_db.close() |
||
115 | |||
116 | if cursor_energy_db: |
||
117 | cursor_energy_db.close() |
||
118 | if cnx_energy_db: |
||
119 | cnx_energy_db.close() |
||
120 | |||
121 | if cursor_system_db: |
||
122 | cursor_system_db.close() |
||
123 | if cnx_system_db: |
||
124 | cnx_system_db.close() |
||
125 | # sleep and continue the outermost while loop |
||
126 | time.sleep(60) |
||
127 | continue |
||
128 | |||
129 | print("Connected to MyEMS Carbon Database") |
||
130 | |||
131 | for virtual_meter in virtual_meter_list: |
||
132 | |||
133 | ############################################################################################################ |
||
134 | # Step 2: get the latest start_datetime_utc |
||
135 | ############################################################################################################ |
||
136 | print("Step 2: get the latest start_datetime_utc from carbon database for " + virtual_meter['name']) |
||
137 | try: |
||
138 | cursor_carbon_db.execute(" SELECT MAX(start_datetime_utc) " |
||
139 | " FROM tbl_virtual_meter_hourly " |
||
140 | " WHERE virtual_meter_id = %s ", |
||
141 | (virtual_meter['id'], )) |
||
142 | row_datetime = cursor_carbon_db.fetchone() |
||
143 | start_datetime_utc = datetime.strptime(config.start_datetime_utc, '%Y-%m-%d %H:%M:%S') |
||
144 | start_datetime_utc = start_datetime_utc.replace(minute=0, second=0, microsecond=0, tzinfo=None) |
||
145 | |||
146 | if row_datetime is not None and len(row_datetime) > 0 and isinstance(row_datetime[0], datetime): |
||
147 | # replace second and microsecond with 0 |
||
148 | # note: do not replace minute in case of calculating in half hourly |
||
149 | start_datetime_utc = row_datetime[0].replace(second=0, microsecond=0, tzinfo=None) |
||
150 | # start from the next time slot |
||
151 | start_datetime_utc += timedelta(minutes=config.minutes_to_count) |
||
152 | |||
153 | print("start_datetime_utc: " + start_datetime_utc.isoformat()[0:19]) |
||
154 | except Exception as e: |
||
155 | logger.error("Error in step 2 of virtual_meter_carbon " + str(e)) |
||
156 | # break the for virtual_meter loop |
||
157 | break |
||
158 | |||
159 | ############################################################################################################ |
||
160 | # Step 3: get all energy data since the latest start_datetime_utc |
||
161 | ############################################################################################################ |
||
162 | print("Step 3: get all energy data since the latest start_datetime_utc") |
||
163 | try: |
||
164 | query = (" SELECT start_datetime_utc, actual_value " |
||
165 | " FROM tbl_virtual_meter_hourly " |
||
166 | " WHERE virtual_meter_id = %s AND start_datetime_utc >= %s " |
||
167 | " ORDER BY id ") |
||
168 | cursor_energy_db.execute(query, (virtual_meter['id'], start_datetime_utc, )) |
||
169 | rows_hourly = cursor_energy_db.fetchall() |
||
170 | |||
171 | if rows_hourly is None or len(rows_hourly) == 0: |
||
172 | print("Step 3: There isn't any energy input data to calculate. ") |
||
173 | # continue the for virtual meter loop |
||
174 | continue |
||
175 | |||
176 | energy_dict = dict() |
||
177 | end_datetime_utc = start_datetime_utc |
||
178 | for row_hourly in rows_hourly: |
||
179 | current_datetime_utc = row_hourly[0] |
||
180 | actual_value = row_hourly[1] |
||
181 | if energy_dict.get(current_datetime_utc) is None: |
||
182 | energy_dict[current_datetime_utc] = dict() |
||
183 | energy_dict[current_datetime_utc][virtual_meter['energy_category_id']] = actual_value |
||
184 | if current_datetime_utc > end_datetime_utc: |
||
185 | end_datetime_utc = current_datetime_utc |
||
186 | except Exception as e: |
||
187 | logger.error("Error in step 3 of virtual_meter_carbon " + str(e)) |
||
188 | # break the for virtual_meter loop |
||
189 | break |
||
190 | |||
191 | ############################################################################################################ |
||
192 | # Step 4: get carbon dioxide emission factor |
||
193 | ############################################################################################################ |
||
194 | print("Step 4: get carbon dioxide emission factor") |
||
195 | factor_dict = dict() |
||
196 | factor_dict[virtual_meter['energy_category_id']] = \ |
||
197 | carbon_dioxide_emmision_factor.get_energy_category_factor( |
||
198 | virtual_meter['energy_category_id'], |
||
199 | start_datetime_utc, |
||
200 | end_datetime_utc) |
||
201 | ############################################################################################################ |
||
202 | # Step 5: calculate carbon dioxide emission by multiplying energy with factor |
||
203 | ############################################################################################################ |
||
204 | print("Step 5: calculate carbon dioxide emission by multiplying energy with factor") |
||
205 | aggregated_values = list() |
||
206 | if len(energy_dict) > 0: |
||
207 | for current_datetime_utc in energy_dict.keys(): |
||
208 | aggregated_value = dict() |
||
209 | aggregated_value['start_datetime_utc'] = current_datetime_utc |
||
210 | aggregated_value['actual_value'] = None |
||
211 | current_factor = factor_dict[virtual_meter['energy_category_id']] |
||
212 | current_energy = energy_dict[current_datetime_utc].get(virtual_meter['energy_category_id']) |
||
213 | if current_factor is not None \ |
||
214 | and isinstance(current_factor, Decimal) \ |
||
215 | and current_energy is not None \ |
||
216 | and isinstance(current_energy, Decimal): |
||
217 | aggregated_value['actual_value'] = current_energy * current_factor |
||
218 | aggregated_values.append(aggregated_value) |
||
219 | |||
220 | ############################################################################################################ |
||
221 | # Step 6: save carbon dioxide emission data to database |
||
222 | ############################################################################################################ |
||
223 | print("Step 6: save carbon dioxide emission data to database") |
||
224 | |||
225 | while len(aggregated_values) > 0: |
||
226 | insert_100 = aggregated_values[:100] |
||
227 | aggregated_values = aggregated_values[100:] |
||
228 | try: |
||
229 | add_values = (" INSERT INTO tbl_virtual_meter_hourly " |
||
230 | " (virtual_meter_id, " |
||
231 | " start_datetime_utc, " |
||
232 | " actual_value) " |
||
233 | " VALUES ") |
||
234 | for aggregated_value in insert_100: |
||
235 | if aggregated_value['actual_value'] is not None and \ |
||
236 | isinstance(aggregated_value['actual_value'], Decimal): |
||
237 | add_values += " (" + str(virtual_meter['id']) + "," |
||
238 | add_values += "'" + aggregated_value['start_datetime_utc'].isoformat()[0:19] + "'," |
||
239 | add_values += str(aggregated_value['actual_value']) + "), " |
||
240 | # print("add_values:" + add_values) |
||
241 | # trim ", " at the end of string and then execute |
||
242 | cursor_carbon_db.execute(add_values[:-2]) |
||
243 | cnx_carbon_db.commit() |
||
244 | except Exception as e: |
||
245 | logger.error("Error in step 6 of virtual_meter_carbon " + str(e)) |
||
246 | break |
||
247 | |||
248 | # end of for virtual_meter loop |
||
249 | if cursor_system_db: |
||
250 | cursor_system_db.close() |
||
251 | if cnx_system_db: |
||
252 | cnx_system_db.close() |
||
253 | |||
254 | if cursor_energy_db: |
||
255 | cursor_energy_db.close() |
||
256 | if cnx_energy_db: |
||
257 | cnx_energy_db.close() |
||
258 | |||
259 | if cursor_carbon_db: |
||
260 | cursor_carbon_db.close() |
||
261 | if cnx_carbon_db: |
||
262 | cnx_carbon_db.close() |
||
263 | print("go to sleep 300 seconds...") |
||
264 | time.sleep(300) |
||
265 | print("wake from sleep, and continue to work...") |
||
266 | # end of the outermost while loop |
||
267 |