/// <summary> /// 增加异常记录 /// </summary> /// <param name="dtos"></param> /// <returns></returns> private async Task ProcessExceptionLog(List <DeviceDataInfoDto> dtos) { var logs = new List <DeviceDataExceptionLogDto>(); if (dtos.Count == 0 || !dtos.Any()) { return; } dtos.ForEach(dto => { if (!dto.IsException) { return; } var log = new DeviceDataExceptionLogDto { Serialnum = Guid.NewGuid().ToString(), DeviceSerialnum = dto.DeviceSerialnum, Value = dto.ProcessedValue, Max = dto.Max, Min = dto.Min, Sort = 0, CreateTime = dto.UpdateTime, UpdateTime = DateTime.Now }; if (log.Value > log.Max) { log.Status = 1; } else if (log.Value < log.Min) { log.Status = -1; } else { log.Status = 0; } logs.Add(log); }); try { if (logs.Any() && logs.Count > 0) { await Task.Factory.StartNew( () => _redisClient.Sadd(RedisKeyString.DeviceDataExceptionLog, logs, RedisSerializeType.DataType)); } } catch (Exception ex) { throw; } }
/// <summary> /// 处理设备异常数据 /// </summary> private static async Task ProcessDeviceDataExceptionLog() { while (true) { Thread.Sleep(10 * 1000); if (!_deviceDataExceptionLogQueue.IsEmpty) { try { var list = _deviceDataExceptionLogQueue.ToArray(); var logs = list.Select(e => { var dataExceptionLog = new DeviceDataExceptionLogDto { DeviceSerialnum = e.Serialnum, Value = e.ProcessedValue, CreateTime = DateTime.Now }; var set = _deviceExceptionSetService.GetDeviceExceptionSetByDeviceId(e.Serialnum);//查找异常区间 if (set != null) { dataExceptionLog.Status = e.ProcessedValue > set.Max ? 1 : -1; dataExceptionLog.Max = set.Max; dataExceptionLog.Min = set.Min; } return(dataExceptionLog); }); if (logs.Any()) { foreach (var log in logs) { var result = await _deviceDataExceptionLogService.AddDeviceDataExceptionLog(log);//添加设备异常日志 if (!result) { LogHelper.Debug("添加设备异常日志失败,编码:{0}", log.Serialnum); } } } //清除刚取出来的数据 DeviceDto entityDevice = null; for (var i = 0; i < list.Length; i++) { _deviceDataExceptionLogQueue.TryDequeue(out entityDevice); } } catch (Exception ex) { ServiceLogger.Current.WriteException(ex); } } } }