/// <summary> /// 将所有的Entity存到字典当中 /// </summary> /// <param name="entity"></param> public void Save(BaseDataModelEntity entity) { //保存ID=DataEntity到字典当中 if (!AllEntity.ContainsKey(entity.objectID)) { AllEntity.Add(entity.objectID, entity); } }
/// <summary> /// 还原所有的entity /// </summary> /// <param name="entity"></param> public void Load(BaseDataModelEntity entity) { //如果在字典中查找不到,找不到的一定不是动态创建生成的(动态生成会立刻写入ID) if (!AllEntity.ContainsKey(entity.objectID)) { //如果还不包含,则return if (!AllEntity.ContainsKey(entity.objectID)) { #if UNITY_EDITOR Debug.Log("can't find entity with objectID: [" + entity.objectID + "___" + entity.GetType() + "],is not storagedata"); #endif return; } } //深拷贝,进行数据还原 string str = _serviceSerializer.SerializerObject(AllEntity[entity.objectID]); object tmpEntity = _serviceSerializer.DeSerializerObject(str); PropertyInfo[] propertyInfos = tmpEntity.GetType().GetProperties(); FieldInfo[] fieldInfos = tmpEntity.GetType().GetFields(); PropertyInfo[] propertyInfos1 = entity.GetType().GetProperties(); FieldInfo[] fieldInfos1 = entity.GetType().GetFields(); for (int i = 0; i < propertyInfos1.Length; i++) { for (int j = 0; j < propertyInfos1.Length; j++) { if (propertyInfos1[i].Name.Equals(propertyInfos[j].Name)) { propertyInfos1[i].SetValue(entity, propertyInfos[j].GetValue(tmpEntity)); } } } for (int i = 0; i < fieldInfos1.Length; i++) { for (int j = 0; j < fieldInfos1.Length; j++) { if (fieldInfos1[i].Name.Equals(fieldInfos[j].Name)) { fieldInfos1[i].SetValue(entity, fieldInfos[j].GetValue(tmpEntity)); } } } }
public static void WriteAll(AllEntity all, CommonConfig config, Action <string> callback, Action <string> commandCallback) { try { WriteRealTime(all.Address, all.EquipmentDataTime, commandCallback); callback("写入时间成功"); } catch (Exception ex) { log.Error(ex); callback("写入时间失败"); } try { WriteOutDate(all.Address, all.OutDate, commandCallback); callback("写入出厂日期成功"); } catch (Exception ex) { log.Error(ex); callback("写入出厂日期失败"); } try { NormalInstruction.WriteGasCount((short)all.GasList.Count, all.Address, config, commandCallback); callback("写入气体个数成功"); foreach (var gas in all.GasList) { try { GasInstruction.WriteGas(gas, all.Address, commandCallback); callback(string.Format("写入气体{0}成功", gas.GasID)); } catch (Exception ex) { log.Error(ex); callback(string.Format("写入气体{0}失败", gas.GasID)); } } } catch (Exception ex) { log.Error(ex); callback("写入气体个数失败"); } try { NormalInstruction.WriteWeatherCount((short)all.WeatherList.Count, all.Address, config, commandCallback); WeatherInstruction.WriteWeather(all.WeatherList, all.Address, commandCallback); callback("写入气象参数成功"); } catch (Exception ex) { log.Error(ex); callback("写入气象参数失败"); } try { NormalInstruction.WriteNormal(all.Normal, all.Address, commandCallback); callback("写入通用参数成功"); } catch (Exception ex) { log.Error(ex); callback("写入通用参数失败"); } try { SerialInstruction.WriteSerialParam(all.Serial, all.Address, commandCallback); callback("写入串口参数成功"); } catch (Exception ex) { log.Error(ex); callback("写入串口参数失败"); } }
public static AllEntity ReadAll(byte address, CommonConfig config, Action <string> callback, Action <string> commandCallback) { AllEntity all = new AllEntity(); try { all.Normal = NormalInstruction.ReadNormal(address, config, commandCallback); callback("读取通用参数成功"); } catch (Exception ex) { log.Error(ex); callback("读取通用参数失败"); } for (int i = 1; i <= all.Normal.GasCount; i++) { try { all.GasList.Add(GasInstruction.ReadGas(i, address, config, commandCallback)); callback(string.Format("读取气体{0}成功", i)); } catch (Exception ex) { log.Error(ex); callback(string.Format("读取气体{0}失败", i)); } } try { all.WeatherList = WeatherInstruction.ReadWeather(address, config, commandCallback); callback("读取气象成功"); } catch (Exception ex) { log.Error(ex); callback("读取气象失败"); } try { all.Serial = SerialInstruction.ReadSerialParam(address, config, commandCallback); callback("读取串口参数成功"); } catch (Exception ex) { log.Error(ex); callback("读取串口参数失败"); } try { all.EquipmentDataTime = ReadRealTime(address, commandCallback); callback("读取时间成功"); } catch (Exception ex) { log.Error(ex); callback("读取时间失败"); } try { all.OutDate = ReadOutDate(address, commandCallback); callback("读取出厂日期成功"); } catch (Exception ex) { log.Error(ex); callback("读取出厂日期失败"); } return(all); }