/// <summary> /// 处理设备添加数据 /// </summary> /// <param name="device">设备添加数据</param> /// <returns></returns> public async Task <XResponseMessage> ProcessAsync(DeviceModel device) { var faccility = await _facilityService.GetFacilityByIdAsny(device.FacilitySerialnum); if (faccility == null) { return(ResultHelper.CreateMessage("设施不存在", ErrorType.FacilityNotExists)); } //if (!_deviceService.CheckCode(device.Serialnum)) return; //if (!await _farmService.ExistsAsync(device.FacilitySerialnum)) return ResultHelper.CreateMessage("基地不存在", ErrorType.FarmNotExists); 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) { return(null); } item = new DeviceDto { Serialnum = device.Serialnum, Name = device.Name, DeviceTypeSerialnum = device.DeviceTypeSerialnum, FacilitySerialnum = device.FacilitySerialnum, CreateTime = device.UpdateTime, UpdateTime = device.UpdateTime, Status = 1, Sort = 0, Unit = device.Unit, ProcessedValue = device.ProcessedValue, ShowValue = device.ShowValue, RelayType = device.RelayType, }; try { var result = await _deviceService.AddDevice(item); LogHelper.Info("[设备]设备{0}{1}添加{2}", device.Name, device.Serialnum, result); return(ResultHelper.CreateMessage($"添加设备{(result ? "成功" : "失败")}", result ? ErrorType.NoError : ErrorType.InternalError)); } catch (AggregateException ex) { return(ResultHelper.CreateExceptionMessage(ex, "添加设备失败")); } }
/// <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, "更新设备失败")); } }
public async Task <IActionResult> Get([FromRoute] long id) { var model = await service.GetByIdAsync <DeviceTypeEditModel>(id); return(Ok(model)); }