internal static void UpdateAlarmToday(ref Alarm_TodayModel alarmTodayModel, List <Alarm_TodayModel> alarmTodayModels, RealDataModel realDataModel, AnalogPointModel analogPointModel, Func <PointState, bool> isAlarmState) { if (alarmTodayModel == null) { NewAlarmAndSaveDatabase(out alarmTodayModel, alarmTodayModels, realDataModel, analogPointModel, isAlarmState); } else { alarmTodayModel.EndTime = realDataModel.RealDate; if (alarmTodayModel.IsRequireNew(realDataModel)) { // 有新的记录, 把老记录状态置1 alarmTodayModel.State = 1; } var realValue = realDataModel.RealValue.Value <float>(); if (realValue > alarmTodayModel.AlarmValue && !alarmTodayModel.IsRequireNew(realDataModel)) { // 没有新记录情况下, 把报警值设置为最大值. alarmTodayModel.AlarmValue = realValue; } if (isAlarmState((PointState)alarmTodayModel.AlarmState) && (alarmTodayModel.IsTimeToSave(realDataModel) || alarmTodayModel.IsRequireNew(realDataModel))) { // 报警情况下, 写入数据库. var newM = alarmTodayModel.DeepClone(); Alarm_TodayModel existM; if ((existM = alarmTodayModels.FirstOrDefault(o => o.PointID == newM.PointID && o.StartTime == newM.StartTime && o.AlarmState == newM.AlarmState)) != null) { existM.State = newM.State; existM.AlarmValue = newM.AlarmValue; existM.EndTime = newM.EndTime; existM.SpanTime = (int)newM.EndTime.Subtract(newM.StartTime).TotalSeconds; } else { alarmTodayModels.Add(alarmTodayModel.DeepClone()); } } if (alarmTodayModel.IsRequireNew(realDataModel)) { // 有新记录. NewAlarmAndSaveDatabase(out alarmTodayModel, alarmTodayModels, realDataModel, analogPointModel, isAlarmState); } } }
private static void NewAlarmAndSaveDatabase(out Alarm_TodayModel alarmTodayModel, List <Alarm_TodayModel> alarmTodayModels, RealDataModel realDataModel, AnalogPointModel analogPointModel, Func <PointState, bool> isAlarmState) { // 需要新增加一条数据. alarmTodayModel = NewAlarmTodayModel(realDataModel, analogPointModel); var newStartTime = alarmTodayModel.StartTime; var valueState = alarmTodayModel.AlarmState; var existSameStartTime = alarmTodayModels.Find(o => newStartTime.Subtract(o.StartTime).TotalSeconds < 1 && o.AlarmState == valueState); if (existSameStartTime != null) { // 只有当开始时间和结束时间不一致的时候, 排除初始化出现主键冲突问题. alarmTodayModels.Remove(existSameStartTime); LogD.Warn($"分站{alarmTodayModel.PointID} 状态{existSameStartTime.AlarmState}出现一秒内多次状态报警状态改变, 会发生主键冲突."); } if (isAlarmState((PointState)alarmTodayModel.AlarmState)) { alarmTodayModels.Add(alarmTodayModel.DeepClone()); // 直接写入数据库 } }