本期解读:

空间能耗分类数据汇总算法:myems/myems-aggregation/space_energy_input_category.py

代码见底部

这段代码是一个用于能源数据处理的Python脚本,主要功能是计算和汇总一个建筑空间内不同能源消耗分类的小时能耗数据。它通过连接到MySQL数据库来获取空间、表、虚拟表、离线表、组合设备、设备、车间、门店、租户和子空间的相关信息,并计算这些实体在特定时间段内的能耗。以下是对代码的详细解析:

总体流程

主循环:脚本持续运行,定期(每5分钟)执行能耗数据的汇总计算。

获取空间列表:从系统数据库中获取所有空间的列表。

多进程处理:为每个空间创建一个进程,并行处理能耗数据的汇总。

数据汇总:对于每个空间,汇总其下所有相关实体的能耗数据。

详细步骤

连接数据库:连接到系统数据库和能源数据库。

获取空间列表:查询系统数据库,获取所有空间的ID和名称。

随机化空间列表:为了避免每次处理相同顺序的空间,将空间列表随机化。

多进程汇总:使用Python的multiprocessing库,为每个空间创建一个进程进行数据汇总。

数据汇总流程(在每个空间上执行):

获取与空间关联的所有表、虚拟表、离线表、组合设备、设备、商铺、车间、租户和子空间。

确定汇总的起始和结束时间。

从能源数据库中获取每个实体在指定时间段内的能耗数据。

确定所有实体能耗数据的公共时间范围。

在公共时间范围内,按能源类别和小时汇总能耗数据。

将汇总后的数据保存回能源数据库。

关键点

错误处理:在每个关键步骤中,都有异常处理来确保数据库连接的稳定性和错误的记录。

日志记录:使用logger对象记录错误和重要的信息。

配置管理:通过config模块管理数据库连接和其他配置信息。

性能优化:通过多进程处理和数据汇总优化性能。

结论

这个脚本是一个复杂的数据处理工具,用于管理和汇总建筑空间内的能源消耗数据。它展示了数据库操作、多进程编程和数据处理的高级应用。

import randomimport timefrom datetime import datetime, timedeltafrom decimal import Decimalfrom multiprocessing import Pool

import mysql.connector

import config

######################################################################################################################### PROCEDURES# Step 1: get all spaces# Step 2: Create multiprocessing pool to call worker in parallel########################################################################################################################

defmain(logger):

whileTrue:# the outermost while loop################################################################################################################# Step 1: get all spaces################################################################################################################ cnx_system_db = None cursor_system_db = Nonetry: cnx_system_db = mysql.connector.connect(**config.myems_system_db) cursor_system_db = cnx_system_db.cursor()except Exception as e: logger.error("Error in step 1.1 of space_energy_input_category.main " + str(e))if cursor_system_db: cursor_system_db.close()if cnx_system_db: cnx_system_db.close()# sleep and continue the outer loop to reconnect the database time.sleep(60)continue print("Connected to MyEMS System Database")

space_list = list()try: cursor_system_db.execute(" SELECT id, name "" FROM tbl_spaces "" ORDER BY id ") rows_spaces = cursor_system_db.fetchall()

if rows_spaces isNoneor len(rows_spaces) == 0: print("There isn't any spaces ")# sleep and continue the outer loop to reconnect the database time.sleep(60)continue

for row in rows_spaces: space_list.append({"id": row[0], "name": row[1]})

except Exception as e: logger.error("Error in step 1.2 of space_energy_input_category.main " + str(e))# sleep and continue the outer loop to reconnect the database time.sleep(60)continuefinally:if cursor_system_db: cursor_system_db.close()if cnx_system_db: cnx_system_db.close()

print("Got all spaces in MyEMS System Database")

shuffle the space list for randomly calculating the meter hourly value random.shuffle(space_list)

################################################################################################################# Step 2: Create multiprocessing pool to call worker in parallel################################################################################################################ p = Pool(processes=config.pool_size) error_list = p.map(worker, space_list) p.close() p.join()

for error in error_list:if error isnotNoneand len(error) > 0: logger.error(error)

print("go to sleep 300 seconds...") time.sleep(300) print("wake from sleep, and continue to work...")# end of outer while

######################################################################################################################### PROCEDURES:# Step 1: get all input meters associated with the space# Step 2: get all input virtual meters associated with the space# Step 3: get all input offline meters associated with the space# Step 4: get all combined equipments associated with the space# Step 5: get all equipments associated with the space# Step 6: get all shopfloors associated with the space# Step 7: get all stores associated with the space# Step 8: get all tenants associated with the space# Step 9: get all child spaces associated with the space# Step 10: determine start datetime and end datetime to aggregate# Step 11: for each meter in list, get energy input data from energy database# Step 12: for each virtual meter in list, get energy input data from energy database# Step 13: for each offline meter in list, get energy input data from energy database# Step 14: for each combined equipment in list, get energy input data from energy database# Step 15: for each equipment in list, get energy input data from energy database# Step 16: for each shopfloor in list, get energy input data from energy database# Step 17: for each store in list, get energy input data from energy database# Step 18: for each tenant in list, get energy input data from energy database# Step 19: for each child space in list, get energy input data from energy database# Step 20: determine common time slot to aggregate# Step 21: aggregate energy data in the common time slot by energy categories and hourly# Step 22: save energy data to energy database## NOTE: returns None or the error string because that the logger object cannot be passed in as parameter########################################################################################################################

defworker(space):##################################################################################################################### Step 1: get all input meters associated with the space#################################################################################################################### print("Step 1: get all input meters associated with the space " + str(space['name']))

cnx_system_db = None cursor_system_db = Nonetry: cnx_system_db = mysql.connector.connect(config.myems_system_db) cursor_system_db = cnx_system_db.cursor()except Exception as e: error_string = "Error in step 1.1 of space_energy_input_category.worker " + str(e)if cursor_system_db: cursor_system_db.close()if cnx_system_db: cnx_system_db.close() print(error_string)return error_string

meter_list = list()try: cursor_system_db.execute(" SELECT m.id, m.name, m.energy_category_id "" FROM tbl_meters m, tbl_spaces_meters sm "" WHERE m.id = sm.meter_id "" AND m.is_counted = 1 "" AND sm.space_id = %s ", (space['id'],)) rows_meters = cursor_system_db.fetchall()

if rows_meters isnotNoneand len(rows_meters) > 0:for row in rows_meters: meter_list.append({"id": row[0],"name": row[1],"energy_category_id": row[2]})

except Exception as e: error_string = "Error in step 1.2 of space_energy_input_category.worker " + str(e)if cursor_system_db: cursor_system_db.close()if cnx_system_db: cnx_system_db.close() print(error_string)return error_string

##################################################################################################################### Step 2: get all input virtual meters associated with the space#################################################################################################################### print("Step 2: get all input virtual meters associated with the space") virtual_meter_list = list()

try: cursor_system_db.execute(" SELECT m.id, m.name, m.energy_category_id "" FROM tbl_virtual_meters m, tbl_spaces_virtual_meters sm "" WHERE m.id = sm.virtual_meter_id "" AND m.is_counted = 1 "" AND sm.space_id = %s ", (space['id'],)) rows_virtual_meters = cursor_system_db.fetchall()

if rows_virtual_meters isnotNoneand len(rows_virtual_meters) > 0:for row in rows_virtual_meters: virtual_meter_list.append({"id": row[0],"name": row[1],"energy_category_id": row[2]})

except Exception as e: error_string = "Error in step 2 of space_energy_input_category.worker " + str(e)if cursor_system_db: cursor_system_db.close()if cnx_system_db: cnx_system_db.close() print(error_string)return error_string

##################################################################################################################### Step 3: get all input offline meters associated with the space#################################################################################################################### print("Step 3: get all input offline meters associated with the space")

offline_meter_list = list()

try: cursor_system_db.execute(" SELECT m.id, m.name, m.energy_category_id "" FROM tbl_offline_meters m, tbl_spaces_offline_meters sm "" WHERE m.id = sm.offline_meter_id "" AND m.is_counted = 1 "" AND sm.space_id = %s ", (space['id'],)) rows_offline_meters = cursor_system_db.fetchall()

if rows_offline_meters isnotNoneand len(rows_offline_meters) > 0:for row in rows_offline_meters: offline_meter_list.append({"id": row[0],"name": row[1],"energy_category_id": row[2]})

except Exception as e: error_string = "Error in step 3 of space_energy_input_category.worker " + str(e)if cursor_system_db: cursor_system_db.close()if cnx_system_db: cnx_system_db.close() print(error_string)return error_string

##################################################################################################################### Step 4: get all combined equipments associated with the space#################################################################################################################### print("Step 4: get all combined equipments associated with the space")

combined_equipment_list = list()

try: cursor_system_db.execute(" SELECT e.id, e.name "" FROM tbl_combined_equipments e, tbl_spaces_combined_equipments se "" WHERE e.id = se.combined_equipment_id "" AND e.is_input_counted = 1 "" AND se.space_id = %s ", (space['id'],)) rows_combined_equipments = cursor_system_db.fetchall()

if rows_combined_equipments isnotNoneand len(rows_combined_equipments) > 0:for row in rows_combined_equipments: combined_equipment_list.append({"id": row[0],"name": row[1]})

except Exception as e: error_string = "Error in step 4 of space_energy_input_category.worker " + str(e)if cursor_system_db: cursor_system_db.close()if cnx_system_db: cnx_system_db.close() print(error_string)return error_string

##################################################################################################################### Step 5: get all equipments associated with the space#################################################################################################################### print("Step 5: get all equipments associated with the space")

equipment_list = list()

try: cursor_system_db.execute(" SELECT e.id, e.name "" FROM tbl_equipments e, tbl_spaces_equipments se "" WHERE e.id = se.equipment_id "" AND e.is_input_counted = 1 "" AND se.space_id = %s ", (space['id'],)) rows_equipments = cursor_system_db.fetchall()

if rows_equipments isnotNoneand len(rows_equipments) > 0:for row in rows_equipments: equipment_list.append({"id": row[0],"name": row[1]})

except Exception as e: error_string = "Error in step 5 of space_energy_input_category.worker " + str(e)if cursor_system_db: cursor_system_db.close()if cnx_system_db: cnx_system_db.close() print(error_string)return error_string

##################################################################################################################### Step 6: get all shopfloors associated with the space#################################################################################################################### print("Step 6: get all shopfloors associated with the space")

shopfloor_list = list()

try: cursor_system_db.execute(" SELECT s.id, s.name "" FROM tbl_shopfloors s, tbl_spaces_shopfloors ss "" WHERE s.id = ss.shopfloor_id "" AND s.is_input_counted = 1 "" AND ss.space_id = %s ", (space['id'],)) rows_shopfloors = cursor_system_db.fetchall()

if rows_shopfloors isnotNoneand len(rows_shopfloors) > 0:for row in rows_shopfloors: shopfloor_list.append({"id": row[0],"name": row[1]})

except Exception as e: error_string = "Error in step 6 of space_energy_input_category.worker " + str(e)if cursor_system_db: cursor_system_db.close()if cnx_system_db: cnx_system_db.close() print(error_string)return error_string

##################################################################################################################### Step 7: get all stores associated with the space#################################################################################################################### print("Step 7: get all stores associated with the space")

store_list = list()

try: cursor_system_db.execute(" SELECT s.id, s.name "" FROM tbl_stores s, tbl_spaces_stores ss "" WHERE s.id = ss.store_id "" AND s.is_input_counted = 1 "" AND ss.space_id = %s ", (space['id'],)) rows_stores = cursor_system_db.fetchall()

if rows_stores isnotNoneand len(rows_stores) > 0:for row in rows_stores: store_list.append({"id": row[0],"name": row[1]})

except Exception as e: error_string = "Error in step 7 of space_energy_input_category.worker " + str(e)if cursor_system_db: cursor_system_db.close()if cnx_system_db: cnx_system_db.close() print(error_string)return error_string

##################################################################################################################### Step 8: get all tenants associated with the space#################################################################################################################### print("Step 8: get all tenants associated with the space")

tenant_list = list()

try: cursor_system_db.execute(" SELECT t.id, t.name "" FROM tbl_tenants t, tbl_spaces_tenants st "" WHERE t.id = st.tenant_id "" AND t.is_input_counted = 1 "" AND st.space_id = %s ", (space['id'],)) rows_tenants = cursor_system_db.fetchall()

if rows_tenants isnotNoneand len(rows_tenants) > 0:for row in rows_tenants: tenant_list.append({"id": row[0],"name": row[1]})

except Exception as e: error_string = "Error in step 8 of space_energy_input_category.worker " + str(e)if cursor_system_db: cursor_system_db.close()if cnx_system_db: cnx_system_db.close() print(error_string)return error_string

##################################################################################################################### Step 9: get all child spaces associated with the space#################################################################################################################### print("Step 9: get all child spaces associated with the space")

child_space_list = list()

try: cursor_system_db.execute(" SELECT id, name "" FROM tbl_spaces "" WHERE is_input_counted = 1 "" AND parent_space_id = %s ", (space['id'],)) rows_child_spaces = cursor_system_db.fetchall()

if rows_child_spaces isnotNoneand len(rows_child_spaces) > 0:for row in rows_child_spaces: child_space_list.append({"id": row[0],"name": row[1]})

except Exception as e: error_string = "Error in step 9 of space_energy_input_category.worker " + str(e) print(error_string)return error_stringfinally:if cursor_system_db: cursor_system_db.close()if cnx_system_db: cnx_system_db.close()

if (meter_list isNoneor len(meter_list) == 0) and \ (virtual_meter_list isNoneor len(virtual_meter_list) == 0) and \ (offline_meter_list isNoneor len(offline_meter_list) == 0) and \ (combined_equipment_list isNoneor len(combined_equipment_list) == 0) and \ (equipment_list isNoneor len(equipment_list) == 0) and \ (shopfloor_list isNoneor len(shopfloor_list) == 0) and \ (store_list isNoneor len(store_list) == 0) and \ (tenant_list isNoneor len(tenant_list) == 0) and \ (child_space_list isNoneor len(child_space_list) == 0): print("This is an empty space ")returnNone

##################################################################################################################### Step 10: determine start datetime and end datetime to aggregate#################################################################################################################### print("Step 10: determine start datetime and end datetime to aggregate") cnx_energy_db = None cursor_energy_db = Nonetry: cnx_energy_db = mysql.connector.connect(
config.myems_energy_db) cursor_energy_db = cnx_energy_db.cursor()except Exception as e: error_string = "Error in step 10.1 of space_energy_input_category.worker " + str(e)if cursor_energy_db: cursor_energy_db.close()if cnx_energy_db: cnx_energy_db.close() print(error_string)return error_string

try: query = (" SELECT MAX(start_datetime_utc) "" FROM tbl_space_input_category_hourly "" WHERE space_id = %s ") cursor_energy_db.execute(query, (space['id'],)) row_datetime = cursor_energy_db.fetchone() start_datetime_utc = datetime.strptime(config.start_datetime_utc, '%Y-%m-%d %H:%M:%S') start_datetime_utc = start_datetime_utc.replace(minute=0, second=0, microsecond=0, tzinfo=None)

if row_datetime isnotNoneand len(row_datetime) > 0and isinstance(row_datetime[0], datetime):# replace second and microsecond with 0# note: do not replace minute in case of calculating in half hourly start_datetime_utc = row_datetime[0].replace(second=0, microsecond=0, tzinfo=None)# start from the next time slot start_datetime_utc += timedelta(minutes=config.minutes_to_count)

end_datetime_utc = datetime.utcnow().replace(second=0, microsecond=0, tzinfo=None)

print("start_datetime_utc: " + start_datetime_utc.isoformat()[0:19] + "end_datetime_utc: " + end_datetime_utc.isoformat()[0:19])

except Exception as e: error_string = "Error in step 10.2 of space_energy_input_category.worker " + str(e)if cursor_energy_db: cursor_energy_db.close()if cnx_energy_db: cnx_energy_db.close() print(error_string)return error_string

##################################################################################################################### Step 11: for each meter in list, get energy input data from energy database#################################################################################################################### energy_meter_hourly = dict()try:if meter_list isnotNoneand len(meter_list) > 0:for meter in meter_list: meter_id = str(meter['id'])

query = (" SELECT start_datetime_utc, actual_value "" FROM tbl_meter_hourly "" WHERE meter_id = %s "" AND start_datetime_utc >= %s "" AND start_datetime_utc < %s "" ORDER BY start_datetime_utc ") cursor_energy_db.execute(query, (meter_id, start_datetime_utc, end_datetime_utc,)) rows_energy_values = cursor_energy_db.fetchall()if rows_energy_values isNoneor len(rows_energy_values) == 0: energy_meter_hourly[meter_id] = Noneelse: energy_meter_hourly[meter_id] = dict()for row_energy_value in rows_energy_values: energy_meter_hourly[meter_id][row_energy_value[0]] = row_energy_value[1]except Exception as e: error_string = "Error in step 11 of space_energy_input_category.worker " + str(e)if cursor_energy_db: cursor_energy_db.close()if cnx_energy_db: cnx_energy_db.close() print(error_string)return error_string

##################################################################################################################### Step 12: for each virtual meter in list, get energy input data from energy database#################################################################################################################### energy_virtual_meter_hourly = dict()if virtual_meter_list isnotNoneand len(virtual_meter_list) > 0:try:for virtual_meter in virtual_meter_list: virtual_meter_id = str(virtual_meter['id'])

query = (" SELECT start_datetime_utc, actual_value "" FROM tbl_virtual_meter_hourly "" WHERE virtual_meter_id = %s "" AND start_datetime_utc >= %s "" AND start_datetime_utc < %s "" ORDER BY start_datetime_utc ") cursor_energy_db.execute(query, (virtual_meter_id, start_datetime_utc, end_datetime_utc,)) rows_energy_values = cursor_energy_db.fetchall()if rows_energy_values isNoneor len(rows_energy_values) == 0: energy_virtual_meter_hourly[virtual_meter_id] = Noneelse: energy_virtual_meter_hourly[virtual_meter_id] = dict()for row_energy_value in rows_energy_values: energy_virtual_meter_hourly[virtual_meter_id][row_energy_value[0]] = row_energy_value[1]except Exception as e: error_string = "Error in step 12 of space_energy_input_category.worker " + str(e)if cursor_energy_db: cursor_energy_db.close()if cnx_energy_db: cnx_energy_db.close() print(error_string)return error_string

##################################################################################################################### Step 13: for each offline meter in list, get energy input data from energy database#################################################################################################################### energy_offline_meter_hourly = dict()if offline_meter_list isnotNoneand len(offline_meter_list) > 0:try:for offline_meter in offline_meter_list: offline_meter_id = str(offline_meter['id'])

query = (" SELECT start_datetime_utc, actual_value "" FROM tbl_offline_meter_hourly "" WHERE offline_meter_id = %s "" AND start_datetime_utc >= %s "" AND start_datetime_utc < %s "" ORDER BY start_datetime_utc ") cursor_energy_db.execute(query, (offline_meter_id, start_datetime_utc, end_datetime_utc,)) rows_energy_values = cursor_energy_db.fetchall()if rows_energy_values isNoneor len(rows_energy_values) == 0: energy_offline_meter_hourly[offline_meter_id] = Noneelse: energy_offline_meter_hourly[offline_meter_id] = dict()for row_energy_value in rows_energy_values: energy_offline_meter_hourly[offline_meter_id][row_energy_value[0]] = row_energy_value[1]

except Exception as e: error_string = "Error in step 13 of space_energy_input_category.worker " + str(e)if cursor_energy_db: cursor_energy_db.close()if cnx_energy_db: cnx_energy_db.close() print(error_string)return error_string

##################################################################################################################### Step 14: for each combined equipment in list, get energy input data from energy database#################################################################################################################### energy_combined_equipment_hourly = dict()if combined_equipment_list isnotNoneand len(combined_equipment_list) > 0:try:for combined_equipment in combined_equipment_list: combined_equipment_id = str(combined_equipment['id']) query = (" SELECT start_datetime_utc, energy_category_id, actual_value "" FROM tbl_combined_equipment_input_category_hourly "" WHERE combined_equipment_id = %s "" AND start_datetime_utc >= %s "" AND start_datetime_utc < %s "" ORDER BY start_datetime_utc ") cursor_energy_db.execute(query, (combined_equipment_id, start_datetime_utc, end_datetime_utc,)) rows_energy_values = cursor_energy_db.fetchall()if rows_energy_values isNoneor len(rows_energy_values) == 0: energy_combined_equipment_hourly[combined_equipment_id] = Noneelse: energy_combined_equipment_hourly[combined_equipment_id] = dict()for row_value in rows_energy_values: current_datetime_utc = row_value[0]if current_datetime_utc notin energy_combined_equipment_hourly[combined_equipment_id]: energy_combined_equipment_hourly[combined_equipment_id][current_datetime_utc] = dict() energy_category_id = row_value[1] actual_value = row_value[2] energy_combined_equipment_hourly[combined_equipment_id][current_datetime_utc][ energy_category_id] = actual_valueexcept Exception as e: error_string = "Error in step 14 of space_energy_input_category.worker " + str(e)if cursor_energy_db: cursor_energy_db.close()if cnx_energy_db: cnx_energy_db.close() print(error_string)return error_string

##################################################################################################################### Step 15: for each equipment in list, get energy input data from energy database#################################################################################################################### energy_equipment_hourly = dict()if equipment_list isnotNoneand len(equipment_list) > 0:try:for equipment in equipment_list: equipment_id = str(equipment['id']) query = (" SELECT start_datetime_utc, energy_category_id, actual_value "" FROM tbl_equipment_input_category_hourly "" WHERE equipment_id = %s "" AND start_datetime_utc >= %s "" AND start_datetime_utc < %s "" ORDER BY start_datetime_utc ") cursor_energy_db.execute(query, (equipment_id, start_datetime_utc, end_datetime_utc,)) rows_energy_values = cursor_energy_db.fetchall()if rows_energy_values isNoneor len(rows_energy_values) == 0: energy_equipment_hourly[equipment_id] = Noneelse: energy_equipment_hourly[equipment_id] = dict()for row_value in rows_energy_values: current_datetime_utc = row_value[0]if current_datetime_utc notin energy_equipment_hourly[equipment_id]: energy_equipment_hourly[equipment_id][current_datetime_utc] = dict() energy_category_id = row_value[1] actual_value = row_value[2] energy_equipment_hourly[equipment_id][current_datetime_utc][energy_category_id] = \ actual_valueexcept Exception as e: error_string = "Error in step 15 of space_energy_input_category.worker " + str(e)if cursor_energy_db: cursor_energy_db.close()if cnx_energy_db: cnx_energy_db.close() print(error_string)return error_string

##################################################################################################################### Step 16: for each shopfloor in list, get energy input data from energy database#################################################################################################################### energy_shopfloor_hourly = dict()if shopfloor_list isnotNoneand len(shopfloor_list) > 0:try:for shopfloor in shopfloor_list: shopfloor_id = str(shopfloor['id'])

query = (" SELECT start_datetime_utc, energy_category_id, actual_value "" FROM tbl_shopfloor_input_category_hourly "" WHERE shopfloor_id = %s "" AND start_datetime_utc >= %s "" AND start_datetime_utc < %s "" ORDER BY start_datetime_utc ") cursor_energy_db.execute(query, (shopfloor_id, start_datetime_utc, end_datetime_utc,)) rows_energy_values = cursor_energy_db.fetchall()if rows_energy_values isNoneor len(rows_energy_values) == 0: energy_shopfloor_hourly[shopfloor_id] = Noneelse: energy_shopfloor_hourly[shopfloor_id] = dict()for row_energy_value in rows_energy_values: current_datetime_utc = row_energy_value[0]if current_datetime_utc notin energy_shopfloor_hourly[shopfloor_id]: energy_shopfloor_hourly[shopfloor_id][current_datetime_utc] = dict() energy_category_id = row_energy_value[1] actual_value = row_energy_value[2] energy_shopfloor_hourly[shopfloor_id][current_datetime_utc][energy_category_id] = actual_valueexcept Exception as e: error_string = "Error in step 16 of space_energy_input_category.worker " + str(e)if cursor_energy_db: cursor_energy_db.close()if cnx_energy_db: cnx_energy_db.close() print(error_string)return error_string

##################################################################################################################### Step 17: for each store in list, get energy input data from energy database#################################################################################################################### energy_store_hourly = dict()if store_list isnotNoneand len(store_list) > 0:try:for store in store_list: store_id = str(store['id'])

query = (" SELECT start_datetime_utc, energy_category_id, actual_value "" FROM tbl_store_input_category_hourly "" WHERE store_id = %s "" AND start_datetime_utc >= %s "" AND start_datetime_utc < %s "" ORDER BY start_datetime_utc ") cursor_energy_db.execute(query, (store_id, start_datetime_utc, end_datetime_utc,)) rows_energy_values = cursor_energy_db.fetchall()if rows_energy_values isNoneor len(rows_energy_values) == 0: energy_store_hourly[store_id] = Noneelse: energy_store_hourly[store_id] = dict()for row_energy_value in rows_energy_values: current_datetime_utc = row_energy_value[0]if current_datetime_utc notin energy_store_hourly[store_id]: energy_store_hourly[store_id][current_datetime_utc] = dict() energy_category_id = row_energy_value[1] actual_value = row_energy_value[2] energy_store_hourly[store_id][current_datetime_utc][energy_category_id] = actual_valueexcept Exception as e: error_string = "Error in step 17 of space_energy_input_category.worker " + str(e)if cursor_energy_db: cursor_energy_db.close()if cnx_energy_db: cnx_energy_db.close() print(error_string)return error_string

由于字数限制,详细脚本将于下半篇展示。

MyEMS开源能源管理系统核心代码解读003(上)的更多相关文章

  1. 优秀开源代码解读之JS与iOS Native Code互调的优雅实现方案

    简介 本篇为大家介绍一个优秀的开源小项目:WebViewJavascriptBridge. 它优雅地实现了在使用UIWebView时JS与ios 的ObjC nativecode之间的互调,支持消息发 ...

  2. Hybrid----优秀开源代码解读之JS与iOS Native Code互调的优雅实现方案-备

    本篇为大家介绍一个优秀的开源小项目:WebViewJavascriptBridge. 它优雅地实现了在使用UIWebView时JS与ios 的ObjC nativecode之间的互调,支持消息发送.接 ...

  3. itest 开源测试管理项目中封装的下拉列表小组件:实现下拉列表使用者前后端0行代码

    导读: 主要从4个方面来阐述,1:背景:2:思路:3:代码实现:4:使用 一:封装背景       像easy ui 之类的纯前端组件,也有下拉列表组件,但是使用的时候,每个下拉列表,要配一个URL ...

  4. 腾讯开源的 Paxos库 PhxPaxos 代码解读---Accept阶段(一)

    腾讯开源的 Paxos库 PhxPaxos 代码解读---Accept阶段(一) 在看Accept阶段代码之前, 我们再回想一下 Basic Paxos算法; 1.  Basic Paxos 算法是为 ...

  5. Android MVP模式 谷歌官方代码解读

    Google官方MVP Sample代码解读 关于Android程序的构架, 当前(2016.10)最流行的模式即为MVP模式, Google官方提供了Sample代码来展示这种模式的用法. Repo ...

  6. msysGit管理GitHub代码

    msysGit管理GitHub代码   代码的管理,在日常开发中是很重要的环节,程序员的修炼三部曲——版本控制,单元测试,项目自动化. 本篇就简单的说说通过msysGit来管理GitHub中的代码,实 ...

  7. C/C++ 开源库及示例代码

    C/C++ 开源库及示例代码 Table of Contents 说明 1 综合性的库 2 数据结构 & 算法 2.1 容器 2.1.1 标准容器 2.1.2 Lockfree 的容器 2.1 ...

  8. Google免费的SVN服务器管理VS2010代码

    原文:Google免费的SVN服务器管理VS2010代码 前言 Google免费为我们提供了代码管理的SVN服务器.首先我这里用的Win7 64的电脑系统,用VS2010进行的代码开发.这里管理代码需 ...

  9. spring JdbcTemplate 在itest 开源测试管理项目中的浅层(5个使用场景)封装

    导读: 主要从4个方面来阐述,1:背景:2:思路:3:代码实现:4:使用 一:封装背景, 在做项目的时候,用的JPA ,有些复杂查询,比如报表用原生的JdbcTemplate ,很不方便;传参也不方便 ...

  10. weex官方demo weex-hackernews代码解读(上)

    一.介绍 weex 是阿里出品的一个类似RN的框架,可以使用前端技术来开发移动应用,实现一份代码支持H5,IOS和Android.最新版本的weex已默认将vue.js作为前端框架,而weex-hac ...

随机推荐

  1. 爬取西刺代理的IP与端口(一)

    0x01 简陋代码是,获取(.*?)的字符串 #coding:utf-8 from requests import * import re headers = { "accept" ...

  2. vivo Pulsar万亿级消息处理实践(1)-数据发送原理解析和性能调优

    作者:vivo 互联网大数据团队- Quan Limin 本文是vivo互联网大数据团队<vivo Pulsar万亿级消息处理实践>系列文章第1篇. 文章以Pulsar client模块中 ...

  3. 使用Ant Desigen在vue里面实现分页以及表头的模糊查询

    一:分页 这次使用的是后端查来的数据在前端进行分页操作: 1.1: data里面的定义 tab1pagination: { defaultPageSize: 10, defaultCurrent:1, ...

  4. ArkUI-X平台差异化

    跨平台使用场景是一套ArkTS代码运行在多个终端设备上,如Android.iOS.OpenHarmony(含基于OpenHarmony发行的商业版,如HarmonyOS Next).当不同平台业务逻辑 ...

  5. hot100之贪心

    买卖股票的最佳时期(121) class Solution { public int maxProfit(int[] prices) { int res = 0; int min = Integer. ...

  6. AGC021E ball Eat chamelemons

    E - Ball Eat Chameleons 设颜色序列中有\(R\)个红球,\(B\)个蓝球,且有\(B+R=k\) 然后分类讨论: \(R<B\) 无解 \(R>B\) 这时有一种合 ...

  7. Golang基础笔记五之结构体

    本文首发于公众号:Hunter后端 原文链接:Golang基础笔记五之结构体 本篇笔记介绍 Golang 中的结构体. 在 Go 中,结构体是一种用户自定义的数据类型,可以将不同类型的数据组合在一起. ...

  8. 探索 Vue.js 组件的最新特性

    引言: Vue.js 作为一款流行的前端框架,始终在不断发展和演进,为开发者带来新的特性和功能,以提升开发效率和用户体验.Vue.js 组件是构建 Vue 应用的基础,其最新特性为开发者提供了更强大的 ...

  9. 学习spring cloud记录9-nacos深一步学习

    前言 统一配置管理将各个服务的配置管理起来,如果发生变化,可以主动向服务发送信息.配置管理服务也在nacos中. 配置热更新 在配置列表新建配置 发布后,这个配置已经进入统一配置管理 服务配置拉去 项 ...

  10. Word邮件合并

    Word邮件合并功能可以解决在Word中批量填写内容的需求,当需要大量格式相同,只修改少数相关内容时,例如利用Word制作工资条,通知函,奖状等等,同时操作也非常简单灵活.下面通过例子来说明邮件合并的 ...