Conditions | 330 |
Total Lines | 967 |
Code Lines | 686 |
Lines | 967 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like space_energy_input_item.worker() 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 |
||
118 | View Code Duplication | def worker(space): |
|
119 | #################################################################################################################### |
||
120 | # Step 1: get all input meters associated with the space |
||
121 | #################################################################################################################### |
||
122 | print("Step 1: get all input meters associated with the space " + str(space['name'])) |
||
123 | |||
124 | cnx_system_db = None |
||
125 | cursor_system_db = None |
||
126 | try: |
||
127 | cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
||
128 | cursor_system_db = cnx_system_db.cursor() |
||
129 | except Exception as e: |
||
130 | error_string = "Error in step 1.1 of space_energy_input_item.worker " + str(e) |
||
131 | if cursor_system_db: |
||
132 | cursor_system_db.close() |
||
133 | if cnx_system_db: |
||
134 | cnx_system_db.close() |
||
135 | print(error_string) |
||
136 | return error_string |
||
137 | |||
138 | meter_list = list() |
||
139 | try: |
||
140 | cursor_system_db.execute(" SELECT m.id, m.name, m.energy_item_id " |
||
141 | " FROM tbl_meters m, tbl_spaces_meters sm " |
||
142 | " WHERE m.id = sm.meter_id " |
||
143 | " AND m.is_counted = true " |
||
144 | " AND m.energy_item_id is NOT NULL " |
||
145 | " AND sm.space_id = %s ", |
||
146 | (space['id'],)) |
||
147 | rows_meters = cursor_system_db.fetchall() |
||
148 | |||
149 | if rows_meters is not None and len(rows_meters) > 0: |
||
150 | for row in rows_meters: |
||
151 | meter_list.append({"id": row[0], |
||
152 | "name": row[1], |
||
153 | "energy_item_id": row[2]}) |
||
154 | |||
155 | except Exception as e: |
||
156 | error_string = "Error in step 1.2 of space_energy_input_item.worker " + str(e) |
||
157 | if cursor_system_db: |
||
158 | cursor_system_db.close() |
||
159 | if cnx_system_db: |
||
160 | cnx_system_db.close() |
||
161 | print(error_string) |
||
162 | return error_string |
||
163 | |||
164 | #################################################################################################################### |
||
165 | # Step 2: get all input virtual meters associated with the space |
||
166 | #################################################################################################################### |
||
167 | print("Step 2: get all input virtual meters associated with the space") |
||
168 | virtual_meter_list = list() |
||
169 | |||
170 | try: |
||
171 | cursor_system_db.execute(" SELECT m.id, m.name, m.energy_item_id " |
||
172 | " FROM tbl_virtual_meters m, tbl_spaces_virtual_meters sm " |
||
173 | " WHERE m.id = sm.virtual_meter_id " |
||
174 | " AND m.is_counted = true " |
||
175 | " AND m.energy_item_id is NOT NULL " |
||
176 | " AND sm.space_id = %s ", |
||
177 | (space['id'],)) |
||
178 | rows_virtual_meters = cursor_system_db.fetchall() |
||
179 | |||
180 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
181 | for row in rows_virtual_meters: |
||
182 | virtual_meter_list.append({"id": row[0], |
||
183 | "name": row[1], |
||
184 | "energy_item_id": row[2]}) |
||
185 | |||
186 | except Exception as e: |
||
187 | error_string = "Error in step 2 of space_energy_input_item.worker " + str(e) |
||
188 | if cursor_system_db: |
||
189 | cursor_system_db.close() |
||
190 | if cnx_system_db: |
||
191 | cnx_system_db.close() |
||
192 | print(error_string) |
||
193 | return error_string |
||
194 | |||
195 | #################################################################################################################### |
||
196 | # Step 3: get all input offline meters associated with the space |
||
197 | #################################################################################################################### |
||
198 | print("Step 3: get all input offline meters associated with the space") |
||
199 | |||
200 | offline_meter_list = list() |
||
201 | |||
202 | try: |
||
203 | cursor_system_db.execute(" SELECT m.id, m.name, m.energy_item_id " |
||
204 | " FROM tbl_offline_meters m, tbl_spaces_offline_meters sm " |
||
205 | " WHERE m.id = sm.offline_meter_id " |
||
206 | " AND m.is_counted = true " |
||
207 | " AND m.energy_item_id is NOT NULL " |
||
208 | " AND sm.space_id = %s ", |
||
209 | (space['id'],)) |
||
210 | rows_offline_meters = cursor_system_db.fetchall() |
||
211 | |||
212 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
213 | for row in rows_offline_meters: |
||
214 | offline_meter_list.append({"id": row[0], |
||
215 | "name": row[1], |
||
216 | "energy_item_id": row[2]}) |
||
217 | |||
218 | except Exception as e: |
||
219 | error_string = "Error in step 3 of space_energy_input_item.worker " + str(e) |
||
220 | if cursor_system_db: |
||
221 | cursor_system_db.close() |
||
222 | if cnx_system_db: |
||
223 | cnx_system_db.close() |
||
224 | print(error_string) |
||
225 | return error_string |
||
226 | |||
227 | #################################################################################################################### |
||
228 | # Step 4: get all combined equipments associated with the space |
||
229 | #################################################################################################################### |
||
230 | print("Step 4: get all combined equipments associated with the space") |
||
231 | |||
232 | combined_equipment_list = list() |
||
233 | |||
234 | try: |
||
235 | cursor_system_db.execute(" SELECT e.id, e.name " |
||
236 | " FROM tbl_combined_equipments e, tbl_spaces_combined_equipments se " |
||
237 | " WHERE e.id = se.combined_equipment_id " |
||
238 | " AND e.is_input_counted = true " |
||
239 | " AND se.space_id = %s ", |
||
240 | (space['id'],)) |
||
241 | rows_combined_equipments = cursor_system_db.fetchall() |
||
242 | |||
243 | if rows_combined_equipments is not None and len(rows_combined_equipments) > 0: |
||
244 | for row in rows_combined_equipments: |
||
245 | combined_equipment_list.append({"id": row[0], |
||
246 | "name": row[1]}) |
||
247 | |||
248 | except Exception as e: |
||
249 | error_string = "Error in step 4 of space_energy_input_item.worker " + str(e) |
||
250 | if cursor_system_db: |
||
251 | cursor_system_db.close() |
||
252 | if cnx_system_db: |
||
253 | cnx_system_db.close() |
||
254 | print(error_string) |
||
255 | return error_string |
||
256 | |||
257 | #################################################################################################################### |
||
258 | # Step 5: get all equipments associated with the space |
||
259 | #################################################################################################################### |
||
260 | print("Step 5: get all equipments associated with the space") |
||
261 | |||
262 | equipment_list = list() |
||
263 | |||
264 | try: |
||
265 | cursor_system_db.execute(" SELECT e.id, e.name " |
||
266 | " FROM tbl_equipments e, tbl_spaces_equipments se " |
||
267 | " WHERE e.id = se.equipment_id " |
||
268 | " AND e.is_input_counted = true " |
||
269 | " AND se.space_id = %s ", |
||
270 | (space['id'],)) |
||
271 | rows_equipments = cursor_system_db.fetchall() |
||
272 | |||
273 | if rows_equipments is not None and len(rows_equipments) > 0: |
||
274 | for row in rows_equipments: |
||
275 | equipment_list.append({"id": row[0], |
||
276 | "name": row[1]}) |
||
277 | |||
278 | except Exception as e: |
||
279 | error_string = "Error in step 5 of space_energy_input_item.worker " + str(e) |
||
280 | if cursor_system_db: |
||
281 | cursor_system_db.close() |
||
282 | if cnx_system_db: |
||
283 | cnx_system_db.close() |
||
284 | print(error_string) |
||
285 | return error_string |
||
286 | |||
287 | #################################################################################################################### |
||
288 | # Step 6: get all shopfloors associated with the space |
||
289 | #################################################################################################################### |
||
290 | print("Step 6: get all shopfloors associated with the space") |
||
291 | |||
292 | shopfloor_list = list() |
||
293 | |||
294 | try: |
||
295 | cursor_system_db.execute(" SELECT s.id, s.name " |
||
296 | " FROM tbl_shopfloors s, tbl_spaces_shopfloors ss " |
||
297 | " WHERE s.id = ss.shopfloor_id " |
||
298 | " AND s.is_input_counted = true " |
||
299 | " AND ss.space_id = %s ", |
||
300 | (space['id'],)) |
||
301 | rows_shopfloors = cursor_system_db.fetchall() |
||
302 | |||
303 | if rows_shopfloors is not None and len(rows_shopfloors) > 0: |
||
304 | for row in rows_shopfloors: |
||
305 | shopfloor_list.append({"id": row[0], |
||
306 | "name": row[1]}) |
||
307 | |||
308 | except Exception as e: |
||
309 | error_string = "Error in step 6 of space_energy_input_item.worker " + str(e) |
||
310 | if cursor_system_db: |
||
311 | cursor_system_db.close() |
||
312 | if cnx_system_db: |
||
313 | cnx_system_db.close() |
||
314 | print(error_string) |
||
315 | return error_string |
||
316 | |||
317 | #################################################################################################################### |
||
318 | # Step 7: get all stores associated with the space |
||
319 | #################################################################################################################### |
||
320 | print("Step 7: get all stores associated with the space") |
||
321 | |||
322 | store_list = list() |
||
323 | |||
324 | try: |
||
325 | cursor_system_db.execute(" SELECT s.id, s.name " |
||
326 | " FROM tbl_stores s, tbl_spaces_stores ss " |
||
327 | " WHERE s.id = ss.store_id " |
||
328 | " AND s.is_input_counted = true " |
||
329 | " AND ss.space_id = %s ", |
||
330 | (space['id'],)) |
||
331 | rows_stores = cursor_system_db.fetchall() |
||
332 | |||
333 | if rows_stores is not None and len(rows_stores) > 0: |
||
334 | for row in rows_stores: |
||
335 | store_list.append({"id": row[0], |
||
336 | "name": row[1]}) |
||
337 | |||
338 | except Exception as e: |
||
339 | error_string = "Error in step 7 of space_energy_input_item.worker " + str(e) |
||
340 | if cursor_system_db: |
||
341 | cursor_system_db.close() |
||
342 | if cnx_system_db: |
||
343 | cnx_system_db.close() |
||
344 | print(error_string) |
||
345 | return error_string |
||
346 | |||
347 | #################################################################################################################### |
||
348 | # Step 8: get all tenants associated with the space |
||
349 | #################################################################################################################### |
||
350 | print("Step 8: get all tenants associated with the space") |
||
351 | |||
352 | tenant_list = list() |
||
353 | |||
354 | try: |
||
355 | cursor_system_db.execute(" SELECT t.id, t.name " |
||
356 | " FROM tbl_tenants t, tbl_spaces_tenants st " |
||
357 | " WHERE t.id = st.tenant_id " |
||
358 | " AND t.is_input_counted = true " |
||
359 | " AND st.space_id = %s ", |
||
360 | (space['id'],)) |
||
361 | rows_tenants = cursor_system_db.fetchall() |
||
362 | |||
363 | if rows_tenants is not None and len(rows_tenants) > 0: |
||
364 | for row in rows_tenants: |
||
365 | tenant_list.append({"id": row[0], |
||
366 | "name": row[1]}) |
||
367 | |||
368 | except Exception as e: |
||
369 | error_string = "Error in step 8 of space_energy_input_item.worker " + str(e) |
||
370 | if cursor_system_db: |
||
371 | cursor_system_db.close() |
||
372 | if cnx_system_db: |
||
373 | cnx_system_db.close() |
||
374 | print(error_string) |
||
375 | return error_string |
||
376 | |||
377 | #################################################################################################################### |
||
378 | # Step 9: get all child spaces associated with the space |
||
379 | #################################################################################################################### |
||
380 | print("Step 9: get all child spaces associated with the space") |
||
381 | |||
382 | child_space_list = list() |
||
383 | |||
384 | try: |
||
385 | cursor_system_db.execute(" SELECT id, name " |
||
386 | " FROM tbl_spaces " |
||
387 | " WHERE is_input_counted = true " |
||
388 | " AND parent_space_id = %s ", |
||
389 | (space['id'],)) |
||
390 | rows_child_spaces = cursor_system_db.fetchall() |
||
391 | |||
392 | if rows_child_spaces is not None and len(rows_child_spaces) > 0: |
||
393 | for row in rows_child_spaces: |
||
394 | child_space_list.append({"id": row[0], |
||
395 | "name": row[1]}) |
||
396 | |||
397 | except Exception as e: |
||
398 | error_string = "Error in step 9 of space_energy_input_item.worker " + str(e) |
||
399 | print(error_string) |
||
400 | return error_string |
||
401 | finally: |
||
402 | if cursor_system_db: |
||
403 | cursor_system_db.close() |
||
404 | if cnx_system_db: |
||
405 | cnx_system_db.close() |
||
406 | |||
407 | if (meter_list is None or len(meter_list) == 0) and \ |
||
408 | (virtual_meter_list is None or len(virtual_meter_list) == 0) and \ |
||
409 | (offline_meter_list is None or len(offline_meter_list) == 0) and \ |
||
410 | (combined_equipment_list is None or len(combined_equipment_list) == 0) and \ |
||
411 | (equipment_list is None or len(equipment_list) == 0) and \ |
||
412 | (shopfloor_list is None or len(shopfloor_list) == 0) and \ |
||
413 | (store_list is None or len(store_list) == 0) and \ |
||
414 | (tenant_list is None or len(tenant_list) == 0) and \ |
||
415 | (child_space_list is None or len(child_space_list) == 0): |
||
416 | print("This is an empty space ") |
||
417 | return None |
||
418 | |||
419 | #################################################################################################################### |
||
420 | # Step 10: determine start datetime and end datetime to aggregate |
||
421 | #################################################################################################################### |
||
422 | print("Step 10: determine start datetime and end datetime to aggregate") |
||
423 | cnx_energy_db = None |
||
424 | cursor_energy_db = None |
||
425 | try: |
||
426 | cnx_energy_db = mysql.connector.connect(**config.myems_energy_db) |
||
427 | cursor_energy_db = cnx_energy_db.cursor() |
||
428 | except Exception as e: |
||
429 | error_string = "Error in step 10.1 of space_energy_input_item.worker " + str(e) |
||
430 | if cursor_energy_db: |
||
431 | cursor_energy_db.close() |
||
432 | if cnx_energy_db: |
||
433 | cnx_energy_db.close() |
||
434 | print(error_string) |
||
435 | return error_string |
||
436 | |||
437 | try: |
||
438 | query = (" SELECT MAX(start_datetime_utc) " |
||
439 | " FROM tbl_space_input_item_hourly " |
||
440 | " WHERE space_id = %s ") |
||
441 | cursor_energy_db.execute(query, (space['id'],)) |
||
442 | row_datetime = cursor_energy_db.fetchone() |
||
443 | start_datetime_utc = datetime.strptime(config.start_datetime_utc, '%Y-%m-%d %H:%M:%S') |
||
444 | start_datetime_utc = start_datetime_utc.replace(minute=0, second=0, microsecond=0, tzinfo=None) |
||
445 | |||
446 | if row_datetime is not None and len(row_datetime) > 0 and isinstance(row_datetime[0], datetime): |
||
447 | # replace second and microsecond with 0 |
||
448 | # note: do not replace minute in case of calculating in half hourly |
||
449 | start_datetime_utc = row_datetime[0].replace(second=0, microsecond=0, tzinfo=None) |
||
450 | # start from the next time slot |
||
451 | start_datetime_utc += timedelta(minutes=config.minutes_to_count) |
||
452 | |||
453 | end_datetime_utc = datetime.utcnow().replace(second=0, microsecond=0, tzinfo=None) |
||
454 | |||
455 | print("start_datetime_utc: " + start_datetime_utc.isoformat()[0:19] |
||
456 | + "end_datetime_utc: " + end_datetime_utc.isoformat()[0:19]) |
||
457 | |||
458 | except Exception as e: |
||
459 | error_string = "Error in step 10.2 of space_energy_input_item.worker " + str(e) |
||
460 | if cursor_energy_db: |
||
461 | cursor_energy_db.close() |
||
462 | if cnx_energy_db: |
||
463 | cnx_energy_db.close() |
||
464 | print(error_string) |
||
465 | return error_string |
||
466 | |||
467 | #################################################################################################################### |
||
468 | # Step 11: for each meter in list, get energy input data from energy database |
||
469 | #################################################################################################################### |
||
470 | energy_meter_hourly = dict() |
||
471 | try: |
||
472 | if meter_list is not None and len(meter_list) > 0: |
||
473 | for meter in meter_list: |
||
474 | meter_id = str(meter['id']) |
||
475 | |||
476 | query = (" SELECT start_datetime_utc, actual_value " |
||
477 | " FROM tbl_meter_hourly " |
||
478 | " WHERE meter_id = %s " |
||
479 | " AND start_datetime_utc >= %s " |
||
480 | " AND start_datetime_utc < %s " |
||
481 | " ORDER BY start_datetime_utc ") |
||
482 | cursor_energy_db.execute(query, (meter_id, start_datetime_utc, end_datetime_utc,)) |
||
483 | rows_energy_values = cursor_energy_db.fetchall() |
||
484 | if rows_energy_values is None or len(rows_energy_values) == 0: |
||
485 | energy_meter_hourly[meter_id] = None |
||
486 | else: |
||
487 | energy_meter_hourly[meter_id] = dict() |
||
488 | for row_energy_value in rows_energy_values: |
||
489 | energy_meter_hourly[meter_id][row_energy_value[0]] = row_energy_value[1] |
||
490 | except Exception as e: |
||
491 | error_string = "Error in step 11 of space_energy_input_item.worker " + str(e) |
||
492 | if cursor_energy_db: |
||
493 | cursor_energy_db.close() |
||
494 | if cnx_energy_db: |
||
495 | cnx_energy_db.close() |
||
496 | print(error_string) |
||
497 | return error_string |
||
498 | |||
499 | #################################################################################################################### |
||
500 | # Step 12: for each virtual meter in list, get energy input data from energy database |
||
501 | #################################################################################################################### |
||
502 | energy_virtual_meter_hourly = dict() |
||
503 | if virtual_meter_list is not None and len(virtual_meter_list) > 0: |
||
504 | try: |
||
505 | for virtual_meter in virtual_meter_list: |
||
506 | virtual_meter_id = str(virtual_meter['id']) |
||
507 | |||
508 | query = (" SELECT start_datetime_utc, actual_value " |
||
509 | " FROM tbl_virtual_meter_hourly " |
||
510 | " WHERE virtual_meter_id = %s " |
||
511 | " AND start_datetime_utc >= %s " |
||
512 | " AND start_datetime_utc < %s " |
||
513 | " ORDER BY start_datetime_utc ") |
||
514 | cursor_energy_db.execute(query, (virtual_meter_id, start_datetime_utc, end_datetime_utc,)) |
||
515 | rows_energy_values = cursor_energy_db.fetchall() |
||
516 | if rows_energy_values is None or len(rows_energy_values) == 0: |
||
517 | energy_virtual_meter_hourly[virtual_meter_id] = None |
||
518 | else: |
||
519 | energy_virtual_meter_hourly[virtual_meter_id] = dict() |
||
520 | for row_energy_value in rows_energy_values: |
||
521 | energy_virtual_meter_hourly[virtual_meter_id][row_energy_value[0]] = row_energy_value[1] |
||
522 | except Exception as e: |
||
523 | error_string = "Error in step 12 of space_energy_input_item.worker " + str(e) |
||
524 | if cursor_energy_db: |
||
525 | cursor_energy_db.close() |
||
526 | if cnx_energy_db: |
||
527 | cnx_energy_db.close() |
||
528 | print(error_string) |
||
529 | return error_string |
||
530 | |||
531 | #################################################################################################################### |
||
532 | # Step 13: for each offline meter in list, get energy input data from energy database |
||
533 | #################################################################################################################### |
||
534 | energy_offline_meter_hourly = dict() |
||
535 | if offline_meter_list is not None and len(offline_meter_list) > 0: |
||
536 | try: |
||
537 | for offline_meter in offline_meter_list: |
||
538 | offline_meter_id = str(offline_meter['id']) |
||
539 | |||
540 | query = (" SELECT start_datetime_utc, actual_value " |
||
541 | " FROM tbl_offline_meter_hourly " |
||
542 | " WHERE offline_meter_id = %s " |
||
543 | " AND start_datetime_utc >= %s " |
||
544 | " AND start_datetime_utc < %s " |
||
545 | " ORDER BY start_datetime_utc ") |
||
546 | cursor_energy_db.execute(query, (offline_meter_id, start_datetime_utc, end_datetime_utc,)) |
||
547 | rows_energy_values = cursor_energy_db.fetchall() |
||
548 | if rows_energy_values is None or len(rows_energy_values) == 0: |
||
549 | energy_offline_meter_hourly[offline_meter_id] = None |
||
550 | else: |
||
551 | energy_offline_meter_hourly[offline_meter_id] = dict() |
||
552 | for row_energy_value in rows_energy_values: |
||
553 | energy_offline_meter_hourly[offline_meter_id][row_energy_value[0]] = row_energy_value[1] |
||
554 | |||
555 | except Exception as e: |
||
556 | error_string = "Error in step 13 of space_energy_input_item.worker " + str(e) |
||
557 | if cursor_energy_db: |
||
558 | cursor_energy_db.close() |
||
559 | if cnx_energy_db: |
||
560 | cnx_energy_db.close() |
||
561 | print(error_string) |
||
562 | return error_string |
||
563 | |||
564 | #################################################################################################################### |
||
565 | # Step 14: for each combined equipment in list, get energy input data from energy database |
||
566 | #################################################################################################################### |
||
567 | energy_combined_equipment_hourly = dict() |
||
568 | if combined_equipment_list is not None and len(combined_equipment_list) > 0: |
||
569 | try: |
||
570 | for combined_equipment in combined_equipment_list: |
||
571 | combined_equipment_id = str(combined_equipment['id']) |
||
572 | query = (" SELECT start_datetime_utc, energy_item_id, actual_value " |
||
573 | " FROM tbl_combined_equipment_input_item_hourly " |
||
574 | " WHERE combined_equipment_id = %s " |
||
575 | " AND start_datetime_utc >= %s " |
||
576 | " AND start_datetime_utc < %s " |
||
577 | " ORDER BY start_datetime_utc ") |
||
578 | cursor_energy_db.execute(query, (combined_equipment_id, start_datetime_utc, end_datetime_utc,)) |
||
579 | rows_energy_values = cursor_energy_db.fetchall() |
||
580 | if rows_energy_values is None or len(rows_energy_values) == 0: |
||
581 | energy_combined_equipment_hourly[combined_equipment_id] = None |
||
582 | else: |
||
583 | energy_combined_equipment_hourly[combined_equipment_id] = dict() |
||
584 | for row_value in rows_energy_values: |
||
585 | current_datetime_utc = row_value[0] |
||
586 | if current_datetime_utc not in energy_combined_equipment_hourly[combined_equipment_id]: |
||
587 | energy_combined_equipment_hourly[combined_equipment_id][current_datetime_utc] = dict() |
||
588 | energy_item_id = row_value[1] |
||
589 | actual_value = row_value[2] |
||
590 | energy_combined_equipment_hourly[combined_equipment_id][current_datetime_utc][energy_item_id] = actual_value |
||
591 | except Exception as e: |
||
592 | error_string = "Error in step 14 of space_energy_input_item.worker " + str(e) |
||
593 | if cursor_energy_db: |
||
594 | cursor_energy_db.close() |
||
595 | if cnx_energy_db: |
||
596 | cnx_energy_db.close() |
||
597 | print(error_string) |
||
598 | return error_string |
||
599 | |||
600 | #################################################################################################################### |
||
601 | # Step 15: for each equipment in list, get energy input data from energy database |
||
602 | #################################################################################################################### |
||
603 | energy_equipment_hourly = dict() |
||
604 | if equipment_list is not None and len(equipment_list) > 0: |
||
605 | try: |
||
606 | for equipment in equipment_list: |
||
607 | equipment_id = str(equipment['id']) |
||
608 | query = (" SELECT start_datetime_utc, energy_item_id, actual_value " |
||
609 | " FROM tbl_equipment_input_item_hourly " |
||
610 | " WHERE equipment_id = %s " |
||
611 | " AND start_datetime_utc >= %s " |
||
612 | " AND start_datetime_utc < %s " |
||
613 | " ORDER BY start_datetime_utc ") |
||
614 | cursor_energy_db.execute(query, (equipment_id, start_datetime_utc, end_datetime_utc,)) |
||
615 | rows_energy_values = cursor_energy_db.fetchall() |
||
616 | if rows_energy_values is None or len(rows_energy_values) == 0: |
||
617 | energy_equipment_hourly[equipment_id] = None |
||
618 | else: |
||
619 | energy_equipment_hourly[equipment_id] = dict() |
||
620 | for row_value in rows_energy_values: |
||
621 | current_datetime_utc = row_value[0] |
||
622 | if current_datetime_utc not in energy_equipment_hourly[equipment_id]: |
||
623 | energy_equipment_hourly[equipment_id][current_datetime_utc] = dict() |
||
624 | energy_item_id = row_value[1] |
||
625 | actual_value = row_value[2] |
||
626 | energy_equipment_hourly[equipment_id][current_datetime_utc][energy_item_id] = \ |
||
627 | actual_value |
||
628 | except Exception as e: |
||
629 | error_string = "Error in step 15 of space_energy_input_item.worker " + str(e) |
||
630 | if cursor_energy_db: |
||
631 | cursor_energy_db.close() |
||
632 | if cnx_energy_db: |
||
633 | cnx_energy_db.close() |
||
634 | print(error_string) |
||
635 | return error_string |
||
636 | |||
637 | #################################################################################################################### |
||
638 | # Step 16: for each shopfloor in list, get energy input data from energy database |
||
639 | #################################################################################################################### |
||
640 | energy_shopfloor_hourly = dict() |
||
641 | if shopfloor_list is not None and len(shopfloor_list) > 0: |
||
642 | try: |
||
643 | for shopfloor in shopfloor_list: |
||
644 | shopfloor_id = str(shopfloor['id']) |
||
645 | |||
646 | query = (" SELECT start_datetime_utc, energy_item_id, actual_value " |
||
647 | " FROM tbl_shopfloor_input_item_hourly " |
||
648 | " WHERE shopfloor_id = %s " |
||
649 | " AND start_datetime_utc >= %s " |
||
650 | " AND start_datetime_utc < %s " |
||
651 | " ORDER BY start_datetime_utc ") |
||
652 | cursor_energy_db.execute(query, (shopfloor_id, start_datetime_utc, end_datetime_utc,)) |
||
653 | rows_energy_values = cursor_energy_db.fetchall() |
||
654 | if rows_energy_values is None or len(rows_energy_values) == 0: |
||
655 | energy_shopfloor_hourly[shopfloor_id] = None |
||
656 | else: |
||
657 | energy_shopfloor_hourly[shopfloor_id] = dict() |
||
658 | for row_energy_value in rows_energy_values: |
||
659 | current_datetime_utc = row_energy_value[0] |
||
660 | if current_datetime_utc not in energy_shopfloor_hourly[shopfloor_id]: |
||
661 | energy_shopfloor_hourly[shopfloor_id][current_datetime_utc] = dict() |
||
662 | energy_item_id = row_energy_value[1] |
||
663 | actual_value = row_energy_value[2] |
||
664 | energy_shopfloor_hourly[shopfloor_id][current_datetime_utc][energy_item_id] = actual_value |
||
665 | except Exception as e: |
||
666 | error_string = "Error in step 16 of space_energy_input_item.worker " + str(e) |
||
667 | if cursor_energy_db: |
||
668 | cursor_energy_db.close() |
||
669 | if cnx_energy_db: |
||
670 | cnx_energy_db.close() |
||
671 | print(error_string) |
||
672 | return error_string |
||
673 | |||
674 | #################################################################################################################### |
||
675 | # Step 17: for each store in list, get energy input data from energy database |
||
676 | #################################################################################################################### |
||
677 | energy_store_hourly = dict() |
||
678 | if store_list is not None and len(store_list) > 0: |
||
679 | try: |
||
680 | for store in store_list: |
||
681 | store_id = str(store['id']) |
||
682 | |||
683 | query = (" SELECT start_datetime_utc, energy_item_id, actual_value " |
||
684 | " FROM tbl_store_input_item_hourly " |
||
685 | " WHERE store_id = %s " |
||
686 | " AND start_datetime_utc >= %s " |
||
687 | " AND start_datetime_utc < %s " |
||
688 | " ORDER BY start_datetime_utc ") |
||
689 | cursor_energy_db.execute(query, (store_id, start_datetime_utc, end_datetime_utc,)) |
||
690 | rows_energy_values = cursor_energy_db.fetchall() |
||
691 | if rows_energy_values is None or len(rows_energy_values) == 0: |
||
692 | energy_store_hourly[store_id] = None |
||
693 | else: |
||
694 | energy_store_hourly[store_id] = dict() |
||
695 | for row_energy_value in rows_energy_values: |
||
696 | current_datetime_utc = row_energy_value[0] |
||
697 | if current_datetime_utc not in energy_store_hourly[store_id]: |
||
698 | energy_store_hourly[store_id][current_datetime_utc] = dict() |
||
699 | energy_item_id = row_energy_value[1] |
||
700 | actual_value = row_energy_value[2] |
||
701 | energy_store_hourly[store_id][current_datetime_utc][energy_item_id] = actual_value |
||
702 | except Exception as e: |
||
703 | error_string = "Error in step 17 of space_energy_input_item.worker " + str(e) |
||
704 | if cursor_energy_db: |
||
705 | cursor_energy_db.close() |
||
706 | if cnx_energy_db: |
||
707 | cnx_energy_db.close() |
||
708 | print(error_string) |
||
709 | return error_string |
||
710 | |||
711 | #################################################################################################################### |
||
712 | # Step 18: for each tenant in list, get energy input data from energy database |
||
713 | #################################################################################################################### |
||
714 | energy_tenant_hourly = dict() |
||
715 | if tenant_list is not None and len(tenant_list) > 0: |
||
716 | try: |
||
717 | for tenant in tenant_list: |
||
718 | tenant_id = str(tenant['id']) |
||
719 | |||
720 | query = (" SELECT start_datetime_utc, energy_item_id, actual_value " |
||
721 | " FROM tbl_tenant_input_item_hourly " |
||
722 | " WHERE tenant_id = %s " |
||
723 | " AND start_datetime_utc >= %s " |
||
724 | " AND start_datetime_utc < %s " |
||
725 | " ORDER BY start_datetime_utc ") |
||
726 | cursor_energy_db.execute(query, (tenant_id, start_datetime_utc, end_datetime_utc,)) |
||
727 | rows_energy_values = cursor_energy_db.fetchall() |
||
728 | if rows_energy_values is None or len(rows_energy_values) == 0: |
||
729 | energy_tenant_hourly[tenant_id] = None |
||
730 | else: |
||
731 | energy_tenant_hourly[tenant_id] = dict() |
||
732 | for row_energy_value in rows_energy_values: |
||
733 | current_datetime_utc = row_energy_value[0] |
||
734 | if current_datetime_utc not in energy_tenant_hourly[tenant_id]: |
||
735 | energy_tenant_hourly[tenant_id][current_datetime_utc] = dict() |
||
736 | energy_item_id = row_energy_value[1] |
||
737 | actual_value = row_energy_value[2] |
||
738 | energy_tenant_hourly[tenant_id][current_datetime_utc][energy_item_id] = actual_value |
||
739 | except Exception as e: |
||
740 | error_string = "Error in step 18 of space_energy_input_item.worker " + str(e) |
||
741 | if cursor_energy_db: |
||
742 | cursor_energy_db.close() |
||
743 | if cnx_energy_db: |
||
744 | cnx_energy_db.close() |
||
745 | print(error_string) |
||
746 | return error_string |
||
747 | |||
748 | #################################################################################################################### |
||
749 | # Step 19: for each child space in list, get energy input data from energy database |
||
750 | #################################################################################################################### |
||
751 | energy_child_space_hourly = dict() |
||
752 | if child_space_list is not None and len(child_space_list) > 0: |
||
753 | try: |
||
754 | for child_space in child_space_list: |
||
755 | child_space_id = str(child_space['id']) |
||
756 | |||
757 | query = (" SELECT start_datetime_utc, energy_item_id, actual_value " |
||
758 | " FROM tbl_space_input_item_hourly " |
||
759 | " WHERE space_id = %s " |
||
760 | " AND start_datetime_utc >= %s " |
||
761 | " AND start_datetime_utc < %s " |
||
762 | " ORDER BY start_datetime_utc ") |
||
763 | cursor_energy_db.execute(query, (child_space_id, start_datetime_utc, end_datetime_utc,)) |
||
764 | rows_energy_values = cursor_energy_db.fetchall() |
||
765 | if rows_energy_values is None or len(rows_energy_values) == 0: |
||
766 | energy_child_space_hourly[child_space_id] = None |
||
767 | else: |
||
768 | energy_child_space_hourly[child_space_id] = dict() |
||
769 | for row_energy_value in rows_energy_values: |
||
770 | current_datetime_utc = row_energy_value[0] |
||
771 | if current_datetime_utc not in energy_child_space_hourly[child_space_id]: |
||
772 | energy_child_space_hourly[child_space_id][current_datetime_utc] = dict() |
||
773 | energy_item_id = row_energy_value[1] |
||
774 | actual_value = row_energy_value[2] |
||
775 | energy_child_space_hourly[child_space_id][current_datetime_utc][energy_item_id] = actual_value |
||
776 | except Exception as e: |
||
777 | error_string = "Error in step 19 of space_energy_input_item.worker " + str(e) |
||
778 | if cursor_energy_db: |
||
779 | cursor_energy_db.close() |
||
780 | if cnx_energy_db: |
||
781 | cnx_energy_db.close() |
||
782 | print(error_string) |
||
783 | return error_string |
||
784 | |||
785 | #################################################################################################################### |
||
786 | # Step 20: determine common time slot to aggregate |
||
787 | #################################################################################################################### |
||
788 | |||
789 | common_start_datetime_utc = start_datetime_utc |
||
790 | common_end_datetime_utc = end_datetime_utc |
||
791 | |||
792 | print("Getting common time slot of energy values for all meters") |
||
793 | if energy_meter_hourly is not None and len(energy_meter_hourly) > 0: |
||
794 | for meter_id, energy_hourly in energy_meter_hourly.items(): |
||
795 | if energy_hourly is None or len(energy_hourly) == 0: |
||
796 | common_start_datetime_utc = None |
||
797 | common_end_datetime_utc = None |
||
798 | break |
||
799 | else: |
||
800 | if common_start_datetime_utc < min(energy_hourly.keys()): |
||
801 | common_start_datetime_utc = min(energy_hourly.keys()) |
||
802 | if common_end_datetime_utc > max(energy_hourly.keys()): |
||
803 | common_end_datetime_utc = max(energy_hourly.keys()) |
||
804 | |||
805 | print("Getting common time slot of energy values for all virtual meters") |
||
806 | if common_start_datetime_utc is not None and common_start_datetime_utc is not None: |
||
807 | if energy_virtual_meter_hourly is not None and len(energy_virtual_meter_hourly) > 0: |
||
808 | for meter_id, energy_hourly in energy_virtual_meter_hourly.items(): |
||
809 | if energy_hourly is None or len(energy_hourly) == 0: |
||
810 | common_start_datetime_utc = None |
||
811 | common_end_datetime_utc = None |
||
812 | break |
||
813 | else: |
||
814 | if common_start_datetime_utc < min(energy_hourly.keys()): |
||
815 | common_start_datetime_utc = min(energy_hourly.keys()) |
||
816 | if common_end_datetime_utc > max(energy_hourly.keys()): |
||
817 | common_end_datetime_utc = max(energy_hourly.keys()) |
||
818 | |||
819 | print("Getting common time slot of energy values for all offline meters") |
||
820 | if common_start_datetime_utc is not None and common_start_datetime_utc is not None: |
||
821 | if energy_offline_meter_hourly is not None and len(energy_offline_meter_hourly) > 0: |
||
822 | for meter_id, energy_hourly in energy_offline_meter_hourly.items(): |
||
823 | if energy_hourly is None or len(energy_hourly) == 0: |
||
824 | common_start_datetime_utc = None |
||
825 | common_end_datetime_utc = None |
||
826 | break |
||
827 | else: |
||
828 | if common_start_datetime_utc < min(energy_hourly.keys()): |
||
829 | common_start_datetime_utc = min(energy_hourly.keys()) |
||
830 | if common_end_datetime_utc > max(energy_hourly.keys()): |
||
831 | common_end_datetime_utc = max(energy_hourly.keys()) |
||
832 | |||
833 | print("Getting common time slot of energy values for all combined equipments") |
||
834 | if common_start_datetime_utc is not None and common_start_datetime_utc is not None: |
||
835 | if energy_combined_equipment_hourly is not None and len(energy_combined_equipment_hourly) > 0: |
||
836 | for combined_equipment_id, energy_hourly in energy_combined_equipment_hourly.items(): |
||
837 | if energy_hourly is None or len(energy_hourly) == 0: |
||
838 | common_start_datetime_utc = None |
||
839 | common_end_datetime_utc = None |
||
840 | break |
||
841 | else: |
||
842 | if common_start_datetime_utc < min(energy_hourly.keys()): |
||
843 | common_start_datetime_utc = min(energy_hourly.keys()) |
||
844 | if common_end_datetime_utc > max(energy_hourly.keys()): |
||
845 | common_end_datetime_utc = max(energy_hourly.keys()) |
||
846 | |||
847 | print("Getting common time slot of energy values for all equipments") |
||
848 | if common_start_datetime_utc is not None and common_start_datetime_utc is not None: |
||
849 | if energy_equipment_hourly is not None and len(energy_equipment_hourly) > 0: |
||
850 | for equipment_id, energy_hourly in energy_equipment_hourly.items(): |
||
851 | if energy_hourly is None or len(energy_hourly) == 0: |
||
852 | common_start_datetime_utc = None |
||
853 | common_end_datetime_utc = None |
||
854 | break |
||
855 | else: |
||
856 | if common_start_datetime_utc < min(energy_hourly.keys()): |
||
857 | common_start_datetime_utc = min(energy_hourly.keys()) |
||
858 | if common_end_datetime_utc > max(energy_hourly.keys()): |
||
859 | common_end_datetime_utc = max(energy_hourly.keys()) |
||
860 | |||
861 | print("Getting common time slot of energy values for all shopfloors") |
||
862 | if common_start_datetime_utc is not None and common_start_datetime_utc is not None: |
||
863 | if energy_shopfloor_hourly is not None and len(energy_shopfloor_hourly) > 0: |
||
864 | for shopfloor_id, energy_hourly in energy_shopfloor_hourly.items(): |
||
865 | if energy_hourly is None or len(energy_hourly) == 0: |
||
866 | common_start_datetime_utc = None |
||
867 | common_end_datetime_utc = None |
||
868 | break |
||
869 | else: |
||
870 | if common_start_datetime_utc < min(energy_hourly.keys()): |
||
871 | common_start_datetime_utc = min(energy_hourly.keys()) |
||
872 | if common_end_datetime_utc > max(energy_hourly.keys()): |
||
873 | common_end_datetime_utc = max(energy_hourly.keys()) |
||
874 | |||
875 | print("Getting common time slot of energy values for all stores") |
||
876 | if common_start_datetime_utc is not None and common_start_datetime_utc is not None: |
||
877 | if energy_store_hourly is not None and len(energy_store_hourly) > 0: |
||
878 | for store_id, energy_hourly in energy_store_hourly.items(): |
||
879 | if energy_hourly is None or len(energy_hourly) == 0: |
||
880 | common_start_datetime_utc = None |
||
881 | common_end_datetime_utc = None |
||
882 | break |
||
883 | else: |
||
884 | if common_start_datetime_utc < min(energy_hourly.keys()): |
||
885 | common_start_datetime_utc = min(energy_hourly.keys()) |
||
886 | if common_end_datetime_utc > max(energy_hourly.keys()): |
||
887 | common_end_datetime_utc = max(energy_hourly.keys()) |
||
888 | |||
889 | print("Getting common time slot of energy values for all tenants") |
||
890 | if common_start_datetime_utc is not None and common_start_datetime_utc is not None: |
||
891 | if energy_tenant_hourly is not None and len(energy_tenant_hourly) > 0: |
||
892 | for tenant_id, energy_hourly in energy_tenant_hourly.items(): |
||
893 | if energy_hourly is None or len(energy_hourly) == 0: |
||
894 | common_start_datetime_utc = None |
||
895 | common_end_datetime_utc = None |
||
896 | break |
||
897 | else: |
||
898 | if common_start_datetime_utc < min(energy_hourly.keys()): |
||
899 | common_start_datetime_utc = min(energy_hourly.keys()) |
||
900 | if common_end_datetime_utc > max(energy_hourly.keys()): |
||
901 | common_end_datetime_utc = max(energy_hourly.keys()) |
||
902 | |||
903 | print("Getting common time slot of energy values for all child spaces") |
||
904 | if common_start_datetime_utc is not None and common_start_datetime_utc is not None: |
||
905 | if energy_child_space_hourly is not None and len(energy_child_space_hourly) > 0: |
||
906 | for child_space_id, energy_hourly in energy_child_space_hourly.items(): |
||
907 | if energy_hourly is None or len(energy_hourly) == 0: |
||
908 | common_start_datetime_utc = None |
||
909 | common_end_datetime_utc = None |
||
910 | break |
||
911 | else: |
||
912 | if common_start_datetime_utc < min(energy_hourly.keys()): |
||
913 | common_start_datetime_utc = min(energy_hourly.keys()) |
||
914 | if common_end_datetime_utc > max(energy_hourly.keys()): |
||
915 | common_end_datetime_utc = max(energy_hourly.keys()) |
||
916 | |||
917 | if (energy_meter_hourly is None or len(energy_meter_hourly) == 0) and \ |
||
918 | (energy_virtual_meter_hourly is None or len(energy_virtual_meter_hourly) == 0) and \ |
||
919 | (energy_offline_meter_hourly is None or len(energy_offline_meter_hourly) == 0) and \ |
||
920 | (energy_combined_equipment_hourly is None or len(energy_combined_equipment_hourly) == 0) and \ |
||
921 | (energy_equipment_hourly is None or len(energy_equipment_hourly) == 0) and \ |
||
922 | (energy_shopfloor_hourly is None or len(energy_shopfloor_hourly) == 0) and \ |
||
923 | (energy_store_hourly is None or len(energy_store_hourly) == 0) and \ |
||
924 | (energy_tenant_hourly is None or len(energy_tenant_hourly) == 0) and \ |
||
925 | (energy_child_space_hourly is None or len(energy_child_space_hourly) == 0): |
||
926 | # There isn't any energy data |
||
927 | print("There isn't any energy data") |
||
928 | # continue the for space loop to the next space |
||
929 | print("continue the for space loop to the next space") |
||
930 | if cursor_energy_db: |
||
931 | cursor_energy_db.close() |
||
932 | if cnx_energy_db: |
||
933 | cnx_energy_db.close() |
||
934 | return None |
||
935 | |||
936 | print("common_start_datetime_utc: " + str(common_start_datetime_utc)) |
||
937 | print("common_end_datetime_utc: " + str(common_end_datetime_utc)) |
||
938 | |||
939 | #################################################################################################################### |
||
940 | # Step 21: aggregate energy data in the common time slot by energy items and hourly |
||
941 | #################################################################################################################### |
||
942 | |||
943 | print("Step 21: aggregate energy data in the common time slot by energy items and hourly") |
||
944 | aggregated_values = list() |
||
945 | try: |
||
946 | current_datetime_utc = common_start_datetime_utc |
||
947 | while common_start_datetime_utc is not None \ |
||
948 | and common_end_datetime_utc is not None \ |
||
949 | and current_datetime_utc <= common_end_datetime_utc: |
||
950 | aggregated_value = dict() |
||
951 | aggregated_value['start_datetime_utc'] = current_datetime_utc |
||
952 | aggregated_value['meta_data'] = dict() |
||
953 | |||
954 | if meter_list is not None and len(meter_list) > 0: |
||
955 | for meter in meter_list: |
||
956 | meter_id = str(meter['id']) |
||
957 | energy_item_id = meter['energy_item_id'] |
||
958 | actual_value = energy_meter_hourly[meter_id].get(current_datetime_utc, Decimal(0.0)) |
||
959 | aggregated_value['meta_data'][energy_item_id] = \ |
||
960 | aggregated_value['meta_data'].get(energy_item_id, Decimal(0.0)) + actual_value |
||
961 | |||
962 | if virtual_meter_list is not None and len(virtual_meter_list) > 0: |
||
963 | for virtual_meter in virtual_meter_list: |
||
964 | virtual_meter_id = str(virtual_meter['id']) |
||
965 | energy_item_id = virtual_meter['energy_item_id'] |
||
966 | actual_value = energy_virtual_meter_hourly[virtual_meter_id].get(current_datetime_utc, Decimal(0.0)) |
||
967 | aggregated_value['meta_data'][energy_item_id] = \ |
||
968 | aggregated_value['meta_data'].get(energy_item_id, Decimal(0.0)) + actual_value |
||
969 | |||
970 | if offline_meter_list is not None and len(offline_meter_list) > 0: |
||
971 | for offline_meter in offline_meter_list: |
||
972 | offline_meter_id = str(offline_meter['id']) |
||
973 | energy_item_id = offline_meter['energy_item_id'] |
||
974 | actual_value = energy_offline_meter_hourly[offline_meter_id].get(current_datetime_utc, Decimal(0.0)) |
||
975 | aggregated_value['meta_data'][energy_item_id] = \ |
||
976 | aggregated_value['meta_data'].get(energy_item_id, Decimal(0.0)) + actual_value |
||
977 | |||
978 | if combined_equipment_list is not None and len(combined_equipment_list) > 0: |
||
979 | for combined_equipment in combined_equipment_list: |
||
980 | combined_equipment_id = str(combined_equipment['id']) |
||
981 | meta_data_dict = \ |
||
982 | energy_combined_equipment_hourly[combined_equipment_id].get(current_datetime_utc, None) |
||
983 | if meta_data_dict is not None and len(meta_data_dict) > 0: |
||
984 | for energy_item_id, actual_value in meta_data_dict.items(): |
||
985 | aggregated_value['meta_data'][energy_item_id] = \ |
||
986 | aggregated_value['meta_data'].get(energy_item_id, Decimal(0.0)) + actual_value |
||
987 | |||
988 | if equipment_list is not None and len(equipment_list) > 0: |
||
989 | for equipment in equipment_list: |
||
990 | equipment_id = str(equipment['id']) |
||
991 | meta_data_dict = energy_equipment_hourly[equipment_id].get(current_datetime_utc, None) |
||
992 | if meta_data_dict is not None and len(meta_data_dict) > 0: |
||
993 | for energy_item_id, actual_value in meta_data_dict.items(): |
||
994 | aggregated_value['meta_data'][energy_item_id] = \ |
||
995 | aggregated_value['meta_data'].get(energy_item_id, Decimal(0.0)) + actual_value |
||
996 | |||
997 | if shopfloor_list is not None and len(shopfloor_list) > 0: |
||
998 | for shopfloor in shopfloor_list: |
||
999 | shopfloor_id = str(shopfloor['id']) |
||
1000 | meta_data_dict = energy_store_hourly[shopfloor_id].get(current_datetime_utc, None) |
||
1001 | if meta_data_dict is not None and len(meta_data_dict) > 0: |
||
1002 | for energy_item_id, actual_value in meta_data_dict.items(): |
||
1003 | aggregated_value['meta_data'][energy_item_id] = \ |
||
1004 | aggregated_value['meta_data'].get(energy_item_id, Decimal(0.0)) + actual_value |
||
1005 | |||
1006 | if store_list is not None and len(store_list) > 0: |
||
1007 | for store in store_list: |
||
1008 | store_id = str(store['id']) |
||
1009 | meta_data_dict = energy_store_hourly[store_id].get(current_datetime_utc, None) |
||
1010 | if meta_data_dict is not None and len(meta_data_dict) > 0: |
||
1011 | for energy_item_id, actual_value in meta_data_dict.items(): |
||
1012 | aggregated_value['meta_data'][energy_item_id] = \ |
||
1013 | aggregated_value['meta_data'].get(energy_item_id, Decimal(0.0)) + actual_value |
||
1014 | |||
1015 | if tenant_list is not None and len(tenant_list) > 0: |
||
1016 | for tenant in tenant_list: |
||
1017 | tenant_id = str(tenant['id']) |
||
1018 | meta_data_dict = energy_tenant_hourly[tenant_id].get(current_datetime_utc, None) |
||
1019 | if meta_data_dict is not None and len(meta_data_dict) > 0: |
||
1020 | for energy_item_id, actual_value in meta_data_dict.items(): |
||
1021 | aggregated_value['meta_data'][energy_item_id] = \ |
||
1022 | aggregated_value['meta_data'].get(energy_item_id, Decimal(0.0)) + actual_value |
||
1023 | |||
1024 | if child_space_list is not None and len(child_space_list) > 0: |
||
1025 | for child_space in child_space_list: |
||
1026 | child_space_id = str(child_space['id']) |
||
1027 | meta_data_dict = energy_child_space_hourly[child_space_id].get(current_datetime_utc, None) |
||
1028 | if meta_data_dict is not None and len(meta_data_dict) > 0: |
||
1029 | for energy_item_id, actual_value in meta_data_dict.items(): |
||
1030 | aggregated_value['meta_data'][energy_item_id] = \ |
||
1031 | aggregated_value['meta_data'].get(energy_item_id, Decimal(0.0)) + actual_value |
||
1032 | |||
1033 | aggregated_values.append(aggregated_value) |
||
1034 | |||
1035 | current_datetime_utc += timedelta(minutes=config.minutes_to_count) |
||
1036 | |||
1037 | except Exception as e: |
||
1038 | error_string = "Error in step 19 of space_energy_input_item.worker " + str(e) |
||
1039 | if cursor_energy_db: |
||
1040 | cursor_energy_db.close() |
||
1041 | if cnx_energy_db: |
||
1042 | cnx_energy_db.close() |
||
1043 | print(error_string) |
||
1044 | return error_string |
||
1045 | |||
1046 | #################################################################################################################### |
||
1047 | # Step 22: save energy data to energy database |
||
1048 | #################################################################################################################### |
||
1049 | print("Step 22: save energy data to energy database") |
||
1050 | |||
1051 | if len(aggregated_values) > 0: |
||
1052 | try: |
||
1053 | add_values = (" INSERT INTO tbl_space_input_item_hourly " |
||
1054 | " (space_id, " |
||
1055 | " energy_item_id, " |
||
1056 | " start_datetime_utc, " |
||
1057 | " actual_value) " |
||
1058 | " VALUES ") |
||
1059 | |||
1060 | for aggregated_value in aggregated_values: |
||
1061 | for energy_item_id, actual_value in aggregated_value['meta_data'].items(): |
||
1062 | add_values += " (" + str(space['id']) + "," |
||
1063 | add_values += " " + str(energy_item_id) + "," |
||
1064 | add_values += "'" + aggregated_value['start_datetime_utc'].isoformat()[0:19] + "'," |
||
1065 | add_values += str(actual_value) + "), " |
||
1066 | print("add_values:" + add_values) |
||
1067 | # trim ", " at the end of string and then execute |
||
1068 | cursor_energy_db.execute(add_values[:-2]) |
||
1069 | cnx_energy_db.commit() |
||
1070 | |||
1071 | except Exception as e: |
||
1072 | error_string = "Error in step 20 of space_energy_input_item.worker " + str(e) |
||
1073 | print(error_string) |
||
1074 | return error_string |
||
1075 | finally: |
||
1076 | if cursor_energy_db: |
||
1077 | cursor_energy_db.close() |
||
1078 | if cnx_energy_db: |
||
1079 | cnx_energy_db.close() |
||
1080 | else: |
||
1081 | if cursor_energy_db: |
||
1082 | cursor_energy_db.close() |
||
1083 | if cnx_energy_db: |
||
1084 | cnx_energy_db.close() |
||
1085 |