| Total Complexity | 169 |
| Total Lines | 623 |
| Duplicated Lines | 93.9 % |
| 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 combined_equipment_energy_input_item 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 time |
||
| 2 | from datetime import datetime, timedelta |
||
| 3 | from decimal import Decimal |
||
| 4 | import mysql.connector |
||
| 5 | from multiprocessing import Pool |
||
| 6 | import random |
||
| 7 | import config |
||
| 8 | |||
| 9 | |||
| 10 | ######################################################################################################################## |
||
| 11 | # PROCEDURES |
||
| 12 | # Step 1: get all combined equipments |
||
| 13 | # Step 2: Create multiprocessing pool to call worker in parallel |
||
| 14 | ######################################################################################################################## |
||
| 15 | |||
| 16 | |||
| 17 | View Code Duplication | def main(logger): |
|
|
|
|||
| 18 | |||
| 19 | while True: |
||
| 20 | # the outermost while loop |
||
| 21 | ################################################################################################################ |
||
| 22 | # Step 1: get all combined equipments |
||
| 23 | ################################################################################################################ |
||
| 24 | cnx_system_db = None |
||
| 25 | cursor_system_db = None |
||
| 26 | try: |
||
| 27 | cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
||
| 28 | cursor_system_db = cnx_system_db.cursor() |
||
| 29 | except Exception as e: |
||
| 30 | logger.error("Error in step 1.1 of combined_equipment_energy_input_item.main " + str(e)) |
||
| 31 | if cursor_system_db: |
||
| 32 | cursor_system_db.close() |
||
| 33 | if cnx_system_db: |
||
| 34 | cnx_system_db.close() |
||
| 35 | # sleep and continue the outer loop to reconnect the database |
||
| 36 | time.sleep(60) |
||
| 37 | continue |
||
| 38 | print("Connected to MyEMS System Database") |
||
| 39 | |||
| 40 | try: |
||
| 41 | cursor_system_db.execute(" SELECT id, name " |
||
| 42 | " FROM tbl_combined_equipments " |
||
| 43 | " ORDER BY id ") |
||
| 44 | rows_combined_equipments = cursor_system_db.fetchall() |
||
| 45 | |||
| 46 | if rows_combined_equipments is None or len(rows_combined_equipments) == 0: |
||
| 47 | print("There isn't any combined equipments ") |
||
| 48 | # sleep and continue the outer loop to reconnect the database |
||
| 49 | time.sleep(60) |
||
| 50 | continue |
||
| 51 | |||
| 52 | combined_equipment_list = list() |
||
| 53 | for row in rows_combined_equipments: |
||
| 54 | combined_equipment_list.append({"id": row[0], "name": row[1]}) |
||
| 55 | |||
| 56 | except Exception as e: |
||
| 57 | logger.error("Error in step 1.2 of combined_equipment_energy_input_item.main " + str(e)) |
||
| 58 | # sleep and continue the outer loop to reconnect the database |
||
| 59 | time.sleep(60) |
||
| 60 | continue |
||
| 61 | finally: |
||
| 62 | if cursor_system_db: |
||
| 63 | cursor_system_db.close() |
||
| 64 | if cnx_system_db: |
||
| 65 | cnx_system_db.close() |
||
| 66 | |||
| 67 | print("Got all combined equipments in MyEMS System Database") |
||
| 68 | |||
| 69 | # shuffle the combined equipment list for randomly calculating the meter hourly value |
||
| 70 | random.shuffle(combined_equipment_list) |
||
| 71 | |||
| 72 | ################################################################################################################ |
||
| 73 | # Step 2: Create multiprocessing pool to call worker in parallel |
||
| 74 | ################################################################################################################ |
||
| 75 | p = Pool(processes=config.pool_size) |
||
| 76 | error_list = p.map(worker, combined_equipment_list) |
||
| 77 | p.close() |
||
| 78 | p.join() |
||
| 79 | |||
| 80 | for error in error_list: |
||
| 81 | if error is not None and len(error) > 0: |
||
| 82 | logger.error(error) |
||
| 83 | |||
| 84 | print("go to sleep 300 seconds...") |
||
| 85 | time.sleep(300) |
||
| 86 | print("wake from sleep, and continue to work...") |
||
| 87 | # end of outer while |
||
| 88 | |||
| 89 | |||
| 90 | ######################################################################################################################## |
||
| 91 | # PROCEDURES: |
||
| 92 | # Step 1: get all input meters associated with the combined equipment |
||
| 93 | # Step 2: get all input virtual meters associated with the combined equipment |
||
| 94 | # Step 3: get all input offline meters associated with the combined equipment |
||
| 95 | # Step 4: get all equipments associated with the combined equipment |
||
| 96 | # Step 5: determine start datetime and end datetime to aggregate |
||
| 97 | # Step 6: for each meter in list, get energy input data from energy database |
||
| 98 | # Step 7: for each virtual meter in list, get energy input data from energy database |
||
| 99 | # Step 8: for each offline meter in list, get energy input data from energy database |
||
| 100 | # Step 9: for each equipment in list, get energy input data from energy database |
||
| 101 | # Step 10: determine common time slot to aggregate |
||
| 102 | # Step 11: aggregate energy data in the common time slot by energy items and hourly |
||
| 103 | # Step 12: save energy data to energy database |
||
| 104 | # |
||
| 105 | # NOTE: returns None or the error string because that the logger object cannot be passed in as parameter |
||
| 106 | ######################################################################################################################## |
||
| 107 | |||
| 108 | View Code Duplication | def worker(combined_equipment): |
|
| 109 | #################################################################################################################### |
||
| 110 | # Step 1: get all input meters associated with the combined equipment |
||
| 111 | #################################################################################################################### |
||
| 112 | print("Step 1: get all input meters associated with the combined equipment " + str(combined_equipment['name'])) |
||
| 113 | |||
| 114 | meter_list = list() |
||
| 115 | cnx_system_db = None |
||
| 116 | cursor_system_db = None |
||
| 117 | try: |
||
| 118 | cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
||
| 119 | cursor_system_db = cnx_system_db.cursor() |
||
| 120 | except Exception as e: |
||
| 121 | error_string = "Error in step 1.1 of combined_equipment_energy_input_item.worker " + str(e) |
||
| 122 | if cursor_system_db: |
||
| 123 | cursor_system_db.close() |
||
| 124 | if cnx_system_db: |
||
| 125 | cnx_system_db.close() |
||
| 126 | print(error_string) |
||
| 127 | return error_string |
||
| 128 | |||
| 129 | try: |
||
| 130 | cursor_system_db.execute(" SELECT m.id, m.name, m.energy_item_id " |
||
| 131 | " FROM tbl_meters m, tbl_combined_equipments_meters em " |
||
| 132 | " WHERE m.id = em.meter_id " |
||
| 133 | " AND m.is_counted = true " |
||
| 134 | " AND m.energy_item_id is NOT NULL " |
||
| 135 | " AND em.is_output = false " |
||
| 136 | " AND em.combined_equipment_id = %s ", |
||
| 137 | (combined_equipment['id'],)) |
||
| 138 | rows_meters = cursor_system_db.fetchall() |
||
| 139 | |||
| 140 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 141 | for row in rows_meters: |
||
| 142 | meter_list.append({"id": row[0], |
||
| 143 | "name": row[1], |
||
| 144 | "energy_item_id": row[2]}) |
||
| 145 | |||
| 146 | except Exception as e: |
||
| 147 | error_string = "Error in step 1.2 of combined_equipment_energy_input_item.worker " + str(e) |
||
| 148 | if cursor_system_db: |
||
| 149 | cursor_system_db.close() |
||
| 150 | if cnx_system_db: |
||
| 151 | cnx_system_db.close() |
||
| 152 | print(error_string) |
||
| 153 | return error_string |
||
| 154 | |||
| 155 | #################################################################################################################### |
||
| 156 | # Step 2: get all input virtual meters associated with the combined equipment |
||
| 157 | #################################################################################################################### |
||
| 158 | print("Step 2: get all input virtual meters associated with the combined equipment") |
||
| 159 | virtual_meter_list = list() |
||
| 160 | |||
| 161 | try: |
||
| 162 | cursor_system_db.execute(" SELECT m.id, m.name, m.energy_item_id " |
||
| 163 | " FROM tbl_virtual_meters m, tbl_combined_equipments_virtual_meters em " |
||
| 164 | " WHERE m.id = em.virtual_meter_id " |
||
| 165 | " AND m.energy_item_id is NOT NULL " |
||
| 166 | " AND m.is_counted = true " |
||
| 167 | " AND em.is_output = false " |
||
| 168 | " AND em.combined_equipment_id = %s ", |
||
| 169 | (combined_equipment['id'],)) |
||
| 170 | rows_virtual_meters = cursor_system_db.fetchall() |
||
| 171 | |||
| 172 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 173 | for row in rows_virtual_meters: |
||
| 174 | virtual_meter_list.append({"id": row[0], |
||
| 175 | "name": row[1], |
||
| 176 | "energy_item_id": row[2]}) |
||
| 177 | |||
| 178 | except Exception as e: |
||
| 179 | error_string = "Error in step 2.1 of combined_equipment_energy_input_item.worker " + str(e) |
||
| 180 | if cursor_system_db: |
||
| 181 | cursor_system_db.close() |
||
| 182 | if cnx_system_db: |
||
| 183 | cnx_system_db.close() |
||
| 184 | print(error_string) |
||
| 185 | return error_string |
||
| 186 | |||
| 187 | #################################################################################################################### |
||
| 188 | # Step 3: get all input offline meters associated with the combined equipment |
||
| 189 | #################################################################################################################### |
||
| 190 | print("Step 3: get all input offline meters associated with the combined equipment") |
||
| 191 | |||
| 192 | offline_meter_list = list() |
||
| 193 | |||
| 194 | try: |
||
| 195 | cursor_system_db.execute(" SELECT m.id, m.name, m.energy_item_id " |
||
| 196 | " FROM tbl_offline_meters m, tbl_combined_equipments_offline_meters em " |
||
| 197 | " WHERE m.id = em.offline_meter_id " |
||
| 198 | " AND m.energy_item_id is NOT NULL " |
||
| 199 | " AND m.is_counted = true " |
||
| 200 | " AND em.is_output = false " |
||
| 201 | " AND em.combined_equipment_id = %s ", |
||
| 202 | (combined_equipment['id'],)) |
||
| 203 | rows_offline_meters = cursor_system_db.fetchall() |
||
| 204 | |||
| 205 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 206 | for row in rows_offline_meters: |
||
| 207 | offline_meter_list.append({"id": row[0], |
||
| 208 | "name": row[1], |
||
| 209 | "energy_item_id": row[2]}) |
||
| 210 | |||
| 211 | except Exception as e: |
||
| 212 | error_string = "Error in step 3.1 of combined_equipment_energy_input_item.worker " + str(e) |
||
| 213 | print(error_string) |
||
| 214 | return error_string |
||
| 215 | finally: |
||
| 216 | if cursor_system_db: |
||
| 217 | cursor_system_db.close() |
||
| 218 | if cnx_system_db: |
||
| 219 | cnx_system_db.close() |
||
| 220 | |||
| 221 | #################################################################################################################### |
||
| 222 | # Step 4: get all equipments associated with the combined equipment |
||
| 223 | #################################################################################################################### |
||
| 224 | print("Step 4: get all equipments associated with the combined equipment") |
||
| 225 | |||
| 226 | equipment_list = list() |
||
| 227 | |||
| 228 | try: |
||
| 229 | cursor_system_db.execute(" SELECT e.id, e.name " |
||
| 230 | " FROM tbl_equipments e, tbl_combined_equipments_equipments ce " |
||
| 231 | " WHERE e.id = ce.equipment_id " |
||
| 232 | " AND e.is_input_counted = true " |
||
| 233 | " AND ce.combined_equipment_id = %s ", |
||
| 234 | (combined_equipment['id'],)) |
||
| 235 | rows_equipments = cursor_system_db.fetchall() |
||
| 236 | |||
| 237 | if rows_equipments is not None and len(rows_equipments) > 0: |
||
| 238 | for row in rows_equipments: |
||
| 239 | equipment_list.append({"id": row[0], |
||
| 240 | "name": row[1]}) |
||
| 241 | |||
| 242 | except Exception as e: |
||
| 243 | error_string = "Error in step 4 of combined_equipment_energy_input_item.worker " + str(e) |
||
| 244 | print(error_string) |
||
| 245 | return error_string |
||
| 246 | finally: |
||
| 247 | if cursor_system_db: |
||
| 248 | cursor_system_db.close() |
||
| 249 | if cnx_system_db: |
||
| 250 | cnx_system_db.close() |
||
| 251 | |||
| 252 | #################################################################################################################### |
||
| 253 | # stop to the next combined equipment if this combined equipment is empty |
||
| 254 | #################################################################################################################### |
||
| 255 | if (meter_list is None or len(meter_list) == 0) and \ |
||
| 256 | (virtual_meter_list is None or len(virtual_meter_list) == 0) and \ |
||
| 257 | (offline_meter_list is None or len(offline_meter_list) == 0) and \ |
||
| 258 | (equipment_list is None or len(equipment_list) == 0): |
||
| 259 | print("This is an empty combined equipment ") |
||
| 260 | return None |
||
| 261 | |||
| 262 | #################################################################################################################### |
||
| 263 | # Step 5: determine start datetime and end datetime to aggregate |
||
| 264 | #################################################################################################################### |
||
| 265 | print("Step 5: determine start datetime and end datetime to aggregate") |
||
| 266 | cnx_energy_db = None |
||
| 267 | cursor_energy_db = None |
||
| 268 | try: |
||
| 269 | cnx_energy_db = mysql.connector.connect(**config.myems_energy_db) |
||
| 270 | cursor_energy_db = cnx_energy_db.cursor() |
||
| 271 | except Exception as e: |
||
| 272 | error_string = "Error in step 5.1 of combined_equipment_energy_input_item.worker " + str(e) |
||
| 273 | if cursor_energy_db: |
||
| 274 | cursor_energy_db.close() |
||
| 275 | if cnx_energy_db: |
||
| 276 | cnx_energy_db.close() |
||
| 277 | print(error_string) |
||
| 278 | return error_string |
||
| 279 | |||
| 280 | try: |
||
| 281 | query = (" SELECT MAX(start_datetime_utc) " |
||
| 282 | " FROM tbl_combined_equipment_input_item_hourly " |
||
| 283 | " WHERE combined_equipment_id = %s ") |
||
| 284 | cursor_energy_db.execute(query, (combined_equipment['id'],)) |
||
| 285 | row_datetime = cursor_energy_db.fetchone() |
||
| 286 | start_datetime_utc = datetime.strptime(config.start_datetime_utc, '%Y-%m-%d %H:%M:%S') |
||
| 287 | start_datetime_utc = start_datetime_utc.replace(minute=0, second=0, microsecond=0, tzinfo=None) |
||
| 288 | |||
| 289 | if row_datetime is not None and len(row_datetime) > 0 and isinstance(row_datetime[0], datetime): |
||
| 290 | # replace second and microsecond with 0 |
||
| 291 | # note: do not replace minute in case of calculating in half hourly |
||
| 292 | start_datetime_utc = row_datetime[0].replace(second=0, microsecond=0, tzinfo=None) |
||
| 293 | # start from the next time slot |
||
| 294 | start_datetime_utc += timedelta(minutes=config.minutes_to_count) |
||
| 295 | |||
| 296 | end_datetime_utc = datetime.utcnow().replace(second=0, microsecond=0, tzinfo=None) |
||
| 297 | |||
| 298 | print("start_datetime_utc: " + start_datetime_utc.isoformat()[0:19] |
||
| 299 | + "end_datetime_utc: " + end_datetime_utc.isoformat()[0:19]) |
||
| 300 | |||
| 301 | except Exception as e: |
||
| 302 | error_string = "Error in step 5.2 of combined_equipment_energy_input_item.worker " + str(e) |
||
| 303 | if cursor_energy_db: |
||
| 304 | cursor_energy_db.close() |
||
| 305 | if cnx_energy_db: |
||
| 306 | cnx_energy_db.close() |
||
| 307 | print(error_string) |
||
| 308 | return error_string |
||
| 309 | |||
| 310 | #################################################################################################################### |
||
| 311 | # Step 6: for each meter in list, get energy input data from energy database |
||
| 312 | #################################################################################################################### |
||
| 313 | energy_meter_hourly = dict() |
||
| 314 | try: |
||
| 315 | if meter_list is not None and len(meter_list) > 0: |
||
| 316 | for meter in meter_list: |
||
| 317 | meter_id = str(meter['id']) |
||
| 318 | |||
| 319 | query = (" SELECT start_datetime_utc, actual_value " |
||
| 320 | " FROM tbl_meter_hourly " |
||
| 321 | " WHERE meter_id = %s " |
||
| 322 | " AND start_datetime_utc >= %s " |
||
| 323 | " AND start_datetime_utc < %s " |
||
| 324 | " ORDER BY start_datetime_utc ") |
||
| 325 | cursor_energy_db.execute(query, (meter_id, start_datetime_utc, end_datetime_utc,)) |
||
| 326 | rows_energy_values = cursor_energy_db.fetchall() |
||
| 327 | if rows_energy_values is None or len(rows_energy_values) == 0: |
||
| 328 | energy_meter_hourly[meter_id] = None |
||
| 329 | else: |
||
| 330 | energy_meter_hourly[meter_id] = dict() |
||
| 331 | for row_energy_value in rows_energy_values: |
||
| 332 | energy_meter_hourly[meter_id][row_energy_value[0]] = row_energy_value[1] |
||
| 333 | except Exception as e: |
||
| 334 | error_string = "Error in step 6.1 of combined_equipment_energy_input_item.worker " + str(e) |
||
| 335 | if cursor_energy_db: |
||
| 336 | cursor_energy_db.close() |
||
| 337 | if cnx_energy_db: |
||
| 338 | cnx_energy_db.close() |
||
| 339 | print(error_string) |
||
| 340 | return error_string |
||
| 341 | |||
| 342 | #################################################################################################################### |
||
| 343 | # Step 7: for each virtual meter in list, get energy input data from energy database |
||
| 344 | #################################################################################################################### |
||
| 345 | energy_virtual_meter_hourly = dict() |
||
| 346 | if virtual_meter_list is not None and len(virtual_meter_list) > 0: |
||
| 347 | try: |
||
| 348 | for virtual_meter in virtual_meter_list: |
||
| 349 | virtual_meter_id = str(virtual_meter['id']) |
||
| 350 | |||
| 351 | query = (" SELECT start_datetime_utc, actual_value " |
||
| 352 | " FROM tbl_virtual_meter_hourly " |
||
| 353 | " WHERE virtual_meter_id = %s " |
||
| 354 | " AND start_datetime_utc >= %s " |
||
| 355 | " AND start_datetime_utc < %s " |
||
| 356 | " ORDER BY start_datetime_utc ") |
||
| 357 | cursor_energy_db.execute(query, (virtual_meter_id, start_datetime_utc, end_datetime_utc,)) |
||
| 358 | rows_energy_values = cursor_energy_db.fetchall() |
||
| 359 | if rows_energy_values is None or len(rows_energy_values) == 0: |
||
| 360 | energy_virtual_meter_hourly[virtual_meter_id] = None |
||
| 361 | else: |
||
| 362 | energy_virtual_meter_hourly[virtual_meter_id] = dict() |
||
| 363 | for row_energy_value in rows_energy_values: |
||
| 364 | energy_virtual_meter_hourly[virtual_meter_id][row_energy_value[0]] = row_energy_value[1] |
||
| 365 | except Exception as e: |
||
| 366 | error_string = "Error in step 7.1 of combined_equipment_energy_input_item.worker " + str(e) |
||
| 367 | if cursor_energy_db: |
||
| 368 | cursor_energy_db.close() |
||
| 369 | if cnx_energy_db: |
||
| 370 | cnx_energy_db.close() |
||
| 371 | print(error_string) |
||
| 372 | return error_string |
||
| 373 | |||
| 374 | #################################################################################################################### |
||
| 375 | # Step 8: for each offline meter in list, get energy input data from energy database |
||
| 376 | #################################################################################################################### |
||
| 377 | energy_offline_meter_hourly = dict() |
||
| 378 | if offline_meter_list is not None and len(offline_meter_list) > 0: |
||
| 379 | try: |
||
| 380 | for offline_meter in offline_meter_list: |
||
| 381 | offline_meter_id = str(offline_meter['id']) |
||
| 382 | |||
| 383 | query = (" SELECT start_datetime_utc, actual_value " |
||
| 384 | " FROM tbl_offline_meter_hourly " |
||
| 385 | " WHERE offline_meter_id = %s " |
||
| 386 | " AND start_datetime_utc >= %s " |
||
| 387 | " AND start_datetime_utc < %s " |
||
| 388 | " ORDER BY start_datetime_utc ") |
||
| 389 | cursor_energy_db.execute(query, (offline_meter_id, start_datetime_utc, end_datetime_utc,)) |
||
| 390 | rows_energy_values = cursor_energy_db.fetchall() |
||
| 391 | if rows_energy_values is None or len(rows_energy_values) == 0: |
||
| 392 | energy_offline_meter_hourly[offline_meter_id] = None |
||
| 393 | else: |
||
| 394 | energy_offline_meter_hourly[offline_meter_id] = dict() |
||
| 395 | for row_energy_value in rows_energy_values: |
||
| 396 | energy_offline_meter_hourly[offline_meter_id][row_energy_value[0]] = row_energy_value[1] |
||
| 397 | |||
| 398 | except Exception as e: |
||
| 399 | error_string = "Error in step 8.1 of combined_equipment_energy_input_item.worker " + str(e) |
||
| 400 | if cursor_energy_db: |
||
| 401 | cursor_energy_db.close() |
||
| 402 | if cnx_energy_db: |
||
| 403 | cnx_energy_db.close() |
||
| 404 | print(error_string) |
||
| 405 | return error_string |
||
| 406 | |||
| 407 | #################################################################################################################### |
||
| 408 | # Step 9: for each equipment in list, get energy input data from energy database |
||
| 409 | #################################################################################################################### |
||
| 410 | energy_equipment_hourly = dict() |
||
| 411 | if equipment_list is not None and len(equipment_list) > 0: |
||
| 412 | try: |
||
| 413 | for equipment in equipment_list: |
||
| 414 | equipment_id = str(equipment['id']) |
||
| 415 | query = (" SELECT start_datetime_utc, energy_category_id, actual_value " |
||
| 416 | " FROM tbl_equipment_input_item_hourly " |
||
| 417 | " WHERE equipment_id = %s " |
||
| 418 | " AND start_datetime_utc >= %s " |
||
| 419 | " AND start_datetime_utc < %s " |
||
| 420 | " ORDER BY start_datetime_utc ") |
||
| 421 | cursor_energy_db.execute(query, (equipment_id, start_datetime_utc, end_datetime_utc,)) |
||
| 422 | rows_energy_values = cursor_energy_db.fetchall() |
||
| 423 | if rows_energy_values is None or len(rows_energy_values) == 0: |
||
| 424 | energy_equipment_hourly[equipment_id] = None |
||
| 425 | else: |
||
| 426 | energy_equipment_hourly[equipment_id] = dict() |
||
| 427 | for row_value in rows_energy_values: |
||
| 428 | current_datetime_utc = row_value[0] |
||
| 429 | if current_datetime_utc not in energy_equipment_hourly[equipment_id]: |
||
| 430 | energy_equipment_hourly[equipment_id][current_datetime_utc] = dict() |
||
| 431 | energy_category_id = row_value[1] |
||
| 432 | actual_value = row_value[2] |
||
| 433 | energy_equipment_hourly[equipment_id][current_datetime_utc][energy_category_id] = \ |
||
| 434 | actual_value |
||
| 435 | except Exception as e: |
||
| 436 | error_string = "Error in step 9 of combined_equipment_energy_input_item.worker " + str(e) |
||
| 437 | if cursor_energy_db: |
||
| 438 | cursor_energy_db.close() |
||
| 439 | if cnx_energy_db: |
||
| 440 | cnx_energy_db.close() |
||
| 441 | print(error_string) |
||
| 442 | return error_string |
||
| 443 | |||
| 444 | #################################################################################################################### |
||
| 445 | # Step 10: determine common time slot to aggregate |
||
| 446 | #################################################################################################################### |
||
| 447 | |||
| 448 | common_start_datetime_utc = start_datetime_utc |
||
| 449 | common_end_datetime_utc = end_datetime_utc |
||
| 450 | |||
| 451 | print("Getting common time slot of energy values for all meters") |
||
| 452 | if energy_meter_hourly is not None and len(energy_meter_hourly) > 0: |
||
| 453 | for meter_id, energy_hourly in energy_meter_hourly.items(): |
||
| 454 | if energy_hourly is None or len(energy_hourly) == 0: |
||
| 455 | common_start_datetime_utc = None |
||
| 456 | common_end_datetime_utc = None |
||
| 457 | break |
||
| 458 | else: |
||
| 459 | if common_start_datetime_utc < min(energy_hourly.keys()): |
||
| 460 | common_start_datetime_utc = min(energy_hourly.keys()) |
||
| 461 | if common_end_datetime_utc > max(energy_hourly.keys()): |
||
| 462 | common_end_datetime_utc = max(energy_hourly.keys()) |
||
| 463 | |||
| 464 | print("Getting common time slot of energy values for all virtual meters") |
||
| 465 | if common_start_datetime_utc is not None and common_start_datetime_utc is not None: |
||
| 466 | if energy_virtual_meter_hourly is not None and len(energy_virtual_meter_hourly) > 0: |
||
| 467 | for meter_id, energy_hourly in energy_virtual_meter_hourly.items(): |
||
| 468 | if energy_hourly is None or len(energy_hourly) == 0: |
||
| 469 | common_start_datetime_utc = None |
||
| 470 | common_end_datetime_utc = None |
||
| 471 | break |
||
| 472 | else: |
||
| 473 | if common_start_datetime_utc < min(energy_hourly.keys()): |
||
| 474 | common_start_datetime_utc = min(energy_hourly.keys()) |
||
| 475 | if common_end_datetime_utc > max(energy_hourly.keys()): |
||
| 476 | common_end_datetime_utc = max(energy_hourly.keys()) |
||
| 477 | |||
| 478 | print("Getting common time slot of energy values for all offline meters") |
||
| 479 | if common_start_datetime_utc is not None and common_start_datetime_utc is not None: |
||
| 480 | if energy_offline_meter_hourly is not None and len(energy_offline_meter_hourly) > 0: |
||
| 481 | for meter_id, energy_hourly in energy_offline_meter_hourly.items(): |
||
| 482 | if energy_hourly is None or len(energy_hourly) == 0: |
||
| 483 | common_start_datetime_utc = None |
||
| 484 | common_end_datetime_utc = None |
||
| 485 | break |
||
| 486 | else: |
||
| 487 | if common_start_datetime_utc < min(energy_hourly.keys()): |
||
| 488 | common_start_datetime_utc = min(energy_hourly.keys()) |
||
| 489 | if common_end_datetime_utc > max(energy_hourly.keys()): |
||
| 490 | common_end_datetime_utc = max(energy_hourly.keys()) |
||
| 491 | |||
| 492 | print("Getting common time slot of energy values for all equipments") |
||
| 493 | if common_start_datetime_utc is not None and common_start_datetime_utc is not None: |
||
| 494 | if energy_equipment_hourly is not None and len(energy_equipment_hourly) > 0: |
||
| 495 | for equipment_id, energy_hourly in energy_equipment_hourly.items(): |
||
| 496 | if energy_hourly is None or len(energy_hourly) == 0: |
||
| 497 | common_start_datetime_utc = None |
||
| 498 | common_end_datetime_utc = None |
||
| 499 | break |
||
| 500 | else: |
||
| 501 | if common_start_datetime_utc < min(energy_hourly.keys()): |
||
| 502 | common_start_datetime_utc = min(energy_hourly.keys()) |
||
| 503 | if common_end_datetime_utc > max(energy_hourly.keys()): |
||
| 504 | common_end_datetime_utc = max(energy_hourly.keys()) |
||
| 505 | |||
| 506 | if (energy_meter_hourly is None or len(energy_meter_hourly) == 0) and \ |
||
| 507 | (energy_virtual_meter_hourly is None or len(energy_virtual_meter_hourly) == 0) and \ |
||
| 508 | (energy_offline_meter_hourly is None or len(energy_offline_meter_hourly) == 0) and \ |
||
| 509 | (energy_equipment_hourly is None or len(energy_equipment_hourly) == 0): |
||
| 510 | # There isn't any energy data |
||
| 511 | print("There isn't any energy data") |
||
| 512 | # continue the for combined equipment loop to the next combined equipment |
||
| 513 | print("continue the for combined equipment loop to the next combined equipment") |
||
| 514 | if cursor_energy_db: |
||
| 515 | cursor_energy_db.close() |
||
| 516 | if cnx_energy_db: |
||
| 517 | cnx_energy_db.close() |
||
| 518 | return None |
||
| 519 | |||
| 520 | print("common_start_datetime_utc: " + str(common_start_datetime_utc)) |
||
| 521 | print("common_end_datetime_utc: " + str(common_end_datetime_utc)) |
||
| 522 | |||
| 523 | #################################################################################################################### |
||
| 524 | # Step 11: aggregate energy data in the common time slot by energy items and hourly |
||
| 525 | #################################################################################################################### |
||
| 526 | |||
| 527 | print("Step 11: aggregate energy data in the common time slot by energy items and hourly") |
||
| 528 | aggregated_values = list() |
||
| 529 | try: |
||
| 530 | current_datetime_utc = common_start_datetime_utc |
||
| 531 | while common_start_datetime_utc is not None \ |
||
| 532 | and common_end_datetime_utc is not None \ |
||
| 533 | and current_datetime_utc <= common_end_datetime_utc: |
||
| 534 | aggregated_value = dict() |
||
| 535 | aggregated_value['start_datetime_utc'] = current_datetime_utc |
||
| 536 | aggregated_value['meta_data'] = dict() |
||
| 537 | |||
| 538 | if meter_list is not None and len(meter_list) > 0: |
||
| 539 | for meter in meter_list: |
||
| 540 | meter_id = str(meter['id']) |
||
| 541 | energy_item_id = meter['energy_item_id'] |
||
| 542 | actual_value = energy_meter_hourly[meter_id].get(current_datetime_utc, Decimal(0.0)) |
||
| 543 | aggregated_value['meta_data'][energy_item_id] = \ |
||
| 544 | aggregated_value['meta_data'].get(energy_item_id, Decimal(0.0)) + actual_value |
||
| 545 | |||
| 546 | if virtual_meter_list is not None and len(virtual_meter_list) > 0: |
||
| 547 | for virtual_meter in virtual_meter_list: |
||
| 548 | virtual_meter_id = str(virtual_meter['id']) |
||
| 549 | energy_item_id = virtual_meter['energy_item_id'] |
||
| 550 | actual_value = energy_virtual_meter_hourly[virtual_meter_id].get(current_datetime_utc, Decimal(0.0)) |
||
| 551 | aggregated_value['meta_data'][energy_item_id] = \ |
||
| 552 | aggregated_value['meta_data'].get(energy_item_id, Decimal(0.0)) + actual_value |
||
| 553 | |||
| 554 | if offline_meter_list is not None and len(offline_meter_list) > 0: |
||
| 555 | for offline_meter in offline_meter_list: |
||
| 556 | offline_meter_id = str(offline_meter['id']) |
||
| 557 | energy_item_id = offline_meter['energy_item_id'] |
||
| 558 | actual_value = energy_offline_meter_hourly[offline_meter_id].get(current_datetime_utc, Decimal(0.0)) |
||
| 559 | aggregated_value['meta_data'][energy_item_id] = \ |
||
| 560 | aggregated_value['meta_data'].get(energy_item_id, Decimal(0.0)) + actual_value |
||
| 561 | |||
| 562 | if equipment_list is not None and len(equipment_list) > 0: |
||
| 563 | for equipment in equipment_list: |
||
| 564 | equipment_id = str(equipment['id']) |
||
| 565 | meta_data_dict = energy_equipment_hourly[equipment_id].get(current_datetime_utc, None) |
||
| 566 | if meta_data_dict is not None and len(meta_data_dict) > 0: |
||
| 567 | for energy_category_id, actual_value in meta_data_dict.items(): |
||
| 568 | aggregated_value['meta_data'][energy_category_id] = \ |
||
| 569 | aggregated_value['meta_data'].get(energy_category_id, Decimal(0.0)) + actual_value |
||
| 570 | |||
| 571 | aggregated_values.append(aggregated_value) |
||
| 572 | |||
| 573 | current_datetime_utc += timedelta(minutes=config.minutes_to_count) |
||
| 574 | |||
| 575 | except Exception as e: |
||
| 576 | error_string = "Error in step 11 of combined_equipment_energy_input_item.worker " + str(e) |
||
| 577 | if cursor_energy_db: |
||
| 578 | cursor_energy_db.close() |
||
| 579 | if cnx_energy_db: |
||
| 580 | cnx_energy_db.close() |
||
| 581 | print(error_string) |
||
| 582 | return error_string |
||
| 583 | |||
| 584 | #################################################################################################################### |
||
| 585 | # Step 12: save energy data to energy database |
||
| 586 | #################################################################################################################### |
||
| 587 | print("Step 12: save energy data to energy database") |
||
| 588 | |||
| 589 | if len(aggregated_values) > 0: |
||
| 590 | try: |
||
| 591 | add_values = (" INSERT INTO tbl_combined_equipment_input_item_hourly " |
||
| 592 | " (combined_equipment_id, " |
||
| 593 | " energy_item_id, " |
||
| 594 | " start_datetime_utc, " |
||
| 595 | " actual_value) " |
||
| 596 | " VALUES ") |
||
| 597 | |||
| 598 | for aggregated_value in aggregated_values: |
||
| 599 | for energy_item_id, actual_value in aggregated_value['meta_data'].items(): |
||
| 600 | add_values += " (" + str(combined_equipment['id']) + "," |
||
| 601 | add_values += " " + str(energy_item_id) + "," |
||
| 602 | add_values += "'" + aggregated_value['start_datetime_utc'].isoformat()[0:19] + "'," |
||
| 603 | add_values += str(actual_value) + "), " |
||
| 604 | print("add_values:" + add_values) |
||
| 605 | # trim ", " at the end of string and then execute |
||
| 606 | cursor_energy_db.execute(add_values[:-2]) |
||
| 607 | cnx_energy_db.commit() |
||
| 608 | |||
| 609 | except Exception as e: |
||
| 610 | error_string = "Error in step 12.1 of combined_equipment_energy_input_item.worker " + str(e) |
||
| 611 | print(error_string) |
||
| 612 | return error_string |
||
| 613 | finally: |
||
| 614 | if cursor_energy_db: |
||
| 615 | cursor_energy_db.close() |
||
| 616 | if cnx_energy_db: |
||
| 617 | cnx_energy_db.close() |
||
| 618 | else: |
||
| 619 | if cursor_energy_db: |
||
| 620 | cursor_energy_db.close() |
||
| 621 | if cnx_energy_db: |
||
| 622 | cnx_energy_db.close() |
||
| 623 |