/// <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);
                    }
                }
            }
        }
Beispiel #2
0
        public async Task ProcessAsync()
        {
            var logDtos = _redisClient.Smember <DeviceDataExceptionLogDto>(RedisKeyString.DeviceDataExceptionLog,
                                                                           RedisSerializeType.DataType);

            if (logDtos == null || !logDtos.Any())
            {
                return;
            }

            var result = await _deviceDataExceptionLogService.AddDeviceDataExceptionLog(logDtos);

            if (result)
            {
                logDtos.ForEach(
                    log =>
                {
                    _redisClient.Srem(RedisKeyString.DeviceDataExceptionLog, log, RedisSerializeType.DataType);
                });
            }
        }