/// <summary> /// 更新设备数据设备实时数据 /// </summary> public static async void ProcessDevice(decimal value, string show, DateTime updateTime) { DeviceDto dto = new DeviceDto { ProcessedValue = value, ShowValue = show, UpdateTime = updateTime }; //查询设备异常范围 DeviceExceptionSetDto set = await _deviceExceptionSetService.GetDeviceExceptionSetByDeviceIdAsny(dto.Serialnum); var isException = false; if (set != null) { if (dto.ProcessedValue > set.Max || dto.ProcessedValue < set.Min) { isException = true; } } var dType = _redis.Smember <DeviceTypeDto>("deviceType", DataType.Protobuf).Find(dt => dt.Serialnum.EqualIgnoreCase(dto.DeviceTypeSerialnum)); var deviceType = dType != null ? dType : await _deviceTypeService.GetDeviceTypeByIdAsny(dto.DeviceTypeSerialnum);//根据设备类型编码获取设备类型 if (dType == null && deviceType != null) { _redis.Sadd("deviceType", deviceType, DataType.Protobuf);//加入到redis缓存中去 } // device show value if (dto.ShowValue.IsNullOrWhiteSpace() && dto.Serialnum.Contains("control")) { if (deviceType.ValueCount == 1) { dto.ShowValue = dto.ProcessedValue + ""; } if (deviceType.ValueCount == 2) { if (dto.ProcessedValue == 1) { dto.ShowValue = "开"; } else if (dto.ProcessedValue == 0) { dto.ShowValue = "关"; } } else if (deviceType.ValueCount == 3) { if (dto.ProcessedValue == 0xFF00) { dto.ShowValue = "开"; } else if (dto.ProcessedValue == 0) { dto.ShowValue = "停"; } else if (dto.ProcessedValue == 0x00FF) { dto.ShowValue = "关"; } } } dto.OnlineStatus = true; dto.IsException = isException; DeviceDto dev = null; if (_redis.Exists("device") == 1) { dev = _redis.Smember <DeviceDto>("device", DataType.Protobuf).Find(d => d.Serialnum.EqualIgnoreCase(dto.DeviceTypeSerialnum)); } if (dev != null) { _redis.Srem("device", dev, DataType.Protobuf); } _redis.Sadd("device", dto, DataType.Protobuf);//放入redis缓存 //await _deviceService.UpdateDevice(dto);//更新设备 LogHelper.Debug("更新设备数据 {0}:{1} {2} {3}", dto.Name, dto.ProcessedValue, dto.Unit, dto.UpdateTime); }
/// <summary> /// 处理设备更新数据 /// </summary> /// <param name="device">设备更新数据</param> /// <returns></returns> public async Task <XResponseMessage> ProcessAsync(DeviceModel device) { var d = await _deviceService.GetDeviceByIdAsny(device.Serialnum); if (d == null) { return(ResultHelper.CreateMessage("设备不存在", ErrorType.DeviceNotExists)); } var deviceType = await _deviceTypeService.GetByIdAsync(device.DeviceTypeSerialnum); if (deviceType == null) { return(ResultHelper.CreateMessage("设备类型不存在", ErrorType.DeviceTypeNotExists)); } var item = await _deviceService.GetDeviceByIdAsny(device.Serialnum); //数据库中不存在该设备并且更新时间大于最新的更新时间 if (item == null || device.UpdateTime < item.UpdateTime) { return(null); } item.Serialnum = device.Serialnum; item.Name = device.Name; item.DeviceTypeSerialnum = device.DeviceTypeSerialnum; item.FacilitySerialnum = device.FacilitySerialnum; item.UpdateTime = device.UpdateTime; item.Unit = device.Unit; item.ProcessedValue = device.ProcessedValue; item.ShowValue = device.ShowValue; item.RelayType = device.RelayType; var deviceset = await _deviceExceptionSet.GetDeviceExceptionSetByDeviceIdAsny(device.Serialnum); //创建设备异常区间 var set = new DeviceExceptionSetDto { Max = device.Max, Min = device.Min, DeviceSerialnum = device.Serialnum, Status = true, CreateTime = device.UpdateTime, UpdateTime = device.UpdateTime }; if (deviceset == null) { set.Serialnum = Guid.NewGuid().ToString(); await _deviceExceptionSet.AddDeviceExceptionSet(set); } else { set.Serialnum = deviceset.Serialnum; await _deviceExceptionSet.UpdateAsny(set); } try { var result = await _deviceService.UpdateDevice(item); LogHelper.Trace("[设备]设备{0}{1}更新{2}", device.Name, device.Serialnum, result); return(ResultHelper.CreateMessage($"更新设备{(result ? "成功" : "失败")}", result ? ErrorType.NoError : ErrorType.InternalError)); } catch (AggregateException ex) { LogHelper.Error(ex.ToString()); return(ResultHelper.CreateExceptionMessage(ex, "更新设备失败")); } }