/// <summary> /// 查询设施下所有设备 /// </summary> /// <param name="facilitySerialnum"></param> /// <returns></returns> public static async Task <XResponseMessage> ProcessGetFacilityDevices(string facilitySerialnum) { if (facilitySerialnum == null) { throw new ArgumentNullException("facilitySerialnum"); } decimal?nullValueDecimal = null; try { //查询设施下所有传感器采集和控制设备 var devices = _deviceService.GetDevicesByFacilityId(facilitySerialnum).ToList().Select(t => new DeviceModel() { Serialnum = t.Serialnum, Name = t.Name, FacilitySerialnum = facilitySerialnum, DeviceTypeSerialnum = t.DeviceTypeSerialnum, ProcessedValue = t.ProcessedValue, ShowValue = t.ShowValue, Unit = t.Unit, UpdateTime = t.UpdateTime, Max = _deviceExceptionSetService.GetDeviceExceptionSetByDeviceId(t.Serialnum) != null ? _deviceExceptionSetService.GetDeviceExceptionSetByDeviceId(t.Serialnum).Max : nullValueDecimal, Min = _deviceExceptionSetService.GetDeviceExceptionSetByDeviceId(t.Serialnum) != null ? _deviceExceptionSetService.GetDeviceExceptionSetByDeviceId(t.Serialnum).Min : nullValueDecimal, }); //查询设施下所有音视频设备 var cameras = (await _facilityCameraService.GetFacilityCamerasByFacilityIdAsny(facilitySerialnum)).ToList().Select(cam => new MediaData() { DeviceCode = cam.Serialnum, FacilityCode = cam.FacilitySerialnum, Url = cam.IP, MediaPort = cam.HttpPort, ContrPort = cam.DataPort, User = cam.UserID, Pwd = cam.UserPwd, Channel = cam.Channel }); var deviceModels = devices as IList <DeviceModel> ?? devices.ToList(); return(ResultHelper.CreateMessage("", ErrorType.NoError, new { Collect = deviceModels.Where(d => d.DeviceTypeSerialnum.StartsWith("collect")), Control = deviceModels.Where(d => d.DeviceTypeSerialnum.StartsWith("control")), Camera = cameras })); } catch (Exception ex) { return(ResultHelper.CreateMessage("", ErrorType.InternalError, null, ex)); } }
/// <summary> /// 处理设备历史数据 /// </summary> private static async Task ProcessDeviceDataInfo() { while (true) { Thread.Sleep(10 * 1000); if (!_dataInfoQueue.IsEmpty) { try { var list = _dataInfoQueue.ToArray(); var logs = list.Select(e => { var cdi = new DeviceDataInfoDto { DeviceSerialnum = e.Serialnum, ProcessedValue = e.ProcessedValue, CreateTime = e.UpdateTime, UpdateTime = e.UpdateTime, Remark = e.Remark }; var set = _deviceExceptionSetService.GetDeviceExceptionSetByDeviceId(e.Serialnum); //查找异常区间 if (set != null) { cdi.IsException = e.ProcessedValue > set.Max || e.ProcessedValue < set.Min; if (cdi.IsException) { cdi.Max = set.Max; cdi.Min = set.Min; cdi.CreateTime = cdi.UpdateTime = e.UpdateTime; } } return(cdi); }); await _deviceDataInfoService.AddDeviceDataInfo2(logs);//保存历史数据 //清除刚取出来的数据 DeviceDto entityDevice = null; for (var i = 0; i < list.Length; i++) { _dataInfoQueue.TryDequeue(out entityDevice); } } catch (Exception ex) { ServiceLogger.Current.WriteException(ex); } } } }
/// <summary> /// 分析设备是否处理异常状态 /// </summary> /// <param name="dtos"></param> /// <returns></returns> private IEnumerable <DeviceDataInfoDto> ProcessDtoIsException(List <DeviceDataInfoDto> dtos) { var deviceDataInfos = new List <DeviceDataInfoDto>(); dtos.ForEach(dto => { var set = _deviceExceptionSetService.GetDeviceExceptionSetByDeviceId(dto.DeviceSerialnum); if (set != null) { dto.Max = set.Max; dto.Min = set.Min; if (dto.ProcessedValue > set.Max || dto.ProcessedValue < set.Min) { dto.IsException = true; } } deviceDataInfos.Add(dto); }); return(deviceDataInfos); }