import pandas as pd from data_initialize_standard.constant import * class DataStandardization(object): def __init__(self, data_dict): self.data_dict = data_dict def null_judge(self): Num = 0 for k in self.data_dict: if self.data_dict[k] == ' ' or not self.data_dict[k]: Num += 1 return Num def check_data(self): # 定义预期的数据结构 missing_fields = [] # 检查顶层字段缺失 for top_key in ExpectedStructure: if top_key not in self.data_dict: missing_fields.append(top_key) # 检查子字段缺失 if top_key in self.data_dict: current_data = self.data_dict[top_key] if isinstance(current_data, dict): expected_subkeys = ExpectedStructure[top_key] for sub_key in expected_subkeys: if sub_key not in current_data: missing_fields.append(f"{top_key}.{sub_key}") # 检查空值的递归函数 def find_empty_fields(data, parent_path='', empty_fields=None): if empty_fields is None: empty_fields = set() if isinstance(data, dict): for key, value in data.items(): new_path = f"{parent_path}.{key}" if parent_path else key # 检查当前值是否为空 if value is None: empty_fields.add(new_path) elif isinstance(value, str) and value.strip() == '': empty_fields.add(new_path) elif isinstance(value, (list, dict)) and not value: empty_fields.add(new_path) # 递归检查子元素 find_empty_fields(value, new_path, empty_fields) elif isinstance(data, list): if not data: # 空列表 empty_fields.add(parent_path) else: # 检查字符串是否为空 if isinstance(data, str) and data.strip() == '': empty_fields.add(parent_path) return empty_fields # 查找所有空值路径 empty_paths = find_empty_fields(self.data_dict) return missing_fields, empty_paths def dict_standard(self): dict_chiller_cooling = self.data_dict['chillerConfigInfo'] dict_chiller_outer = {} dict_code = self.data_dict['controlConfigInfo'] all_chillers_info = self.data_dict['stationInfo']['allChillersInfo'] all_chiller_water_temp_set = [] all_water_temp_control_mode = [] for i in range(len(all_chillers_info['chillerId'])): if all_chillers_info['runStatus'][i] == 1 and all_chillers_info['isInControl'][i] == 1: all_chiller_water_temp_set.append(all_chillers_info['chillerWaterTempSet'][i]) if all_chillers_info['isInControl'][i] == 1: all_water_temp_control_mode.append(all_chillers_info['waterTempControlMode'][i]) dict_chiller_cooling.update({ 'triggerTime': self.data_dict['stationInfo']['triggerTime'], 'runMode': self.data_dict['stationInfo']['runMode'], 'runStatus': self.data_dict['chillerInfo']['runStatus'], 'chillerOffTimeLatest': self.data_dict['chillerInfo']['chillerOffTimeLatest'], 'offSetTempLatestCooling': self.data_dict['chillerInfo']['offSetTempLatestCooling'], # 'offSetTempInLatestCooling': 12, 'allChillerWaterTempSetOn': all_chiller_water_temp_set, # 'allChillerWaterTempInSetOn': [], 'allChillerControlSelect': all_water_temp_control_mode, # 'allChillerPowerRatio': [], 'chillerPowerRatio': self.data_dict['chillerInfo']['chillerPowerRatio'], 'chillerWaterTempOut': self.data_dict['chillerInfo']['chillerWaterTempOut'], 'chillerWaterTempIn': self.data_dict['chillerInfo']['chillerWaterTempIn'], 'chillerWaterTempSet': self.data_dict['chillerInfo']['chillerWaterTempSet'], 'chillerWaterTempSetInitial': self.data_dict['controlConfigInfo']['chillerWaterTempSetInitial'], 'chillerWaterTempSetUpper': self.data_dict['controlConfigInfo']['chillerWaterTempSetUpper'], # 'chillerWaterTempInSetUpper': self.data_dict, 'chillerWaterTempSetDown': self.data_dict['controlConfigInfo']['chillerWaterTempSetLower'], }) dict_chiller_outer.update({ 'triggerTime': self.data_dict['stationInfo']['triggerTime'], 'userName': self.data_dict['stationInfo']['userName'], 'coolingStationName': self.data_dict['stationInfo']['coolingStationName'], 'orgId': self.data_dict['stationInfo']['orgId'], 'chillerName': self.data_dict['chillerInfo']['chillerName'], 'chillerId': self.data_dict['chillerInfo']['chillerId'], 'stationId': self.data_dict['stationInfo']['stationId'], 'controllerId': self.data_dict['stationInfo']['controllerId'], 'allChillersInfo': self.data_dict['stationInfo']['allChillersInfo']}) dict_code.update({'upControlTimeLatest': self.data_dict['chillerInfo']['upControlTimeLatest'], 'downControlTimeLatest': self.data_dict['chillerInfo']['downControlTimeLatest'], 'triggerTime': self.data_dict['stationInfo']['triggerTime'], 'runMode': self.data_dict['stationInfo']['runMode'], 'calMode': 0, # 监测点计算模式默认值为0 'allChillerControlSelect': all_water_temp_control_mode}) return dict_chiller_cooling, dict_chiller_outer, dict_code def dict_safety(self): dict_upper_correct = {'refriType': self.data_dict['chillerInfo']['refriType'], 'chillerParameter': self.data_dict['chillerSafetyPara']} # dict_upper_correct.update({'refriType': self.data_dict['chillerInfo']['refriType'], # 'chillerParameter': self.data_dict['chillerSafetyPara']}) return dict_upper_correct def data_multi_comp(self): dict_chiller_inner, dict_chiller_outer, dict_code = self.dict_standard() dict_upper_correct = self.dict_safety() data_temp_humi = pd.DataFrame(self.data_dict['terminalInfo']) return dict_chiller_inner, dict_chiller_outer, dict_code, dict_upper_correct, data_temp_humi