/// <summary> /// 处理分时数据 /// </summary> /// <param name="period">时间间隔</param> public static async Task ProcessStatistics(DeviceDto dev, int period = 15) { var deviceExceptionSet = await _deviceExceptionSetService.GetDeviceExceptionSetByDeviceIdAsny(dev.Serialnum); var timeRange = GetTimeRange(dev.UpdateTime, period); var timeSharingDatas = await _deviceTimeSharingStatisticsService.GetDeviceTimeSharingStatisticsByArgsAsny(dev.Serialnum, period, timeRange.Item1, timeRange.Item2);//获取设备分时统计数据 DeviceTimeSharingStatisticsDto statistics; if (timeSharingDatas == null || timeSharingDatas.Count() == 0) { statistics = new DeviceTimeSharingStatisticsDto { DeviceSerialnum = dev.Serialnum, TimeSharing = period, Count = 1, StartValue = dev.ProcessedValue, EndValue = dev.ProcessedValue, MaxValue = dev.ProcessedValue, MinValue = dev.ProcessedValue, AvgValue = dev.ProcessedValue, CreateTime = dev.UpdateTime, UpdateTime = dev.UpdateTime }; if (deviceExceptionSet != null) { if (dev.ProcessedValue > deviceExceptionSet.Max || dev.ProcessedValue < deviceExceptionSet.Min) { statistics.ExceptionCount = 1; } } } else { statistics = timeSharingDatas.ToList()[timeSharingDatas.Count() - 1]; statistics.Count++; statistics.EndValue = dev.ProcessedValue; if (dev.ProcessedValue > statistics.MaxValue) { statistics.MaxValue = dev.ProcessedValue; } if (dev.ProcessedValue < statistics.MinValue) { statistics.MinValue = dev.ProcessedValue; } statistics.AvgValue = (statistics.AvgValue + dev.ProcessedValue) / 2; statistics.UpdateTime = dev.UpdateTime; if (deviceExceptionSet != null) { if (dev.ProcessedValue > deviceExceptionSet.Max || dev.ProcessedValue < deviceExceptionSet.Min) { statistics.ExceptionCount++; } } } await _deviceTimeSharingStatisticsService.AddDeviceTimeSharingStatisticsAsny(statistics);//保存 }
/// <summary> /// 处理并计算统计数据 /// </summary> /// <param name="sd"></param> /// <param name="period"></param> /// <returns></returns> private async Task<bool> ProcessStatistics(SensorData sd, int period) { var key = StatisticsProcessor.GetStatisticsKey(sd.Time, period, sd.DeviceCode); var sts = _redisClient.HashGet<DeviceTimeSharingStatisticsDto>(RedisKeyString.DeviceTimeSharingStatistics, key, RedisSerializeType.DataType); if (sts == null) { sts = new DeviceTimeSharingStatisticsDto { DeviceSerialnum = sd.DeviceCode, Serialnum = key, TimeSharing = period }; sts.MaxValue = sts.MinValue = sts.StartValue = sts.EndValue = sts.AvgValue = sd.Value; sts.CreateTime = DateTime.Now; } else { if (sd.Value > sts.MaxValue) sts.MaxValue = sd.Value; if (sd.Value < sts.MinValue) sts.MinValue = sd.Value; sts.EndValue = sd.Value; sts.AvgValue = (sts.AvgValue + sd.Value) / 2; } sts.Count++; sts.UpdateTime = sd.Time; //取上下限 var deviceExceptionSetService = AhnqIotContainer.Container.Resolve<IDeviceExceptionSetService>(); var set = await deviceExceptionSetService.GetDeviceExceptionSetByDeviceIdAsny(sd.DeviceCode); if (set == null) return _redisClient.HashSetFieldValue(RedisKeyString.DeviceTimeSharingStatistics, key, sts, RedisSerializeType.DataType) > 0; sts.Max = set.Max; sts.Min = set.Min; if (sd.Value > set.Max || sd.Value < set.Min) { sts.ExceptionCount++; } return _redisClient.HashSetFieldValue(RedisKeyString.DeviceTimeSharingStatistics, key, sts, RedisSerializeType.DataType) > 0; }