Пример #1
0
        /// <summary>
        /// 检查设备添加或更新时的错误原因
        /// </summary>
        /// <param name="device">设备</param>
        /// <param name="channels">通道集合</param>
        /// <returns>数据校验结果</returns>
        private ModelStateDictionary CheckError(FlowDevice device, List <FlowChannel> channels)
        {
            ModelStateDictionary modelState = new ModelStateDictionary();

            if (_context.Devices.Count(d => d.Ip == device.Ip) > 0)
            {
                modelState.AddModelError("Ip", "设备IP重复");
            }

            if (channels != null)
            {
                List <int> indexes = channels.Select(c => c.ChannelIndex).Distinct().ToList();
                if (indexes.Count < channels.Count)
                {
                    modelState.AddModelError("ChannelIndex", "通道序号重复");
                }

                if (channels.Any(c => c.ChannelIndex <= 0))
                {
                    modelState.AddModelError("ChannelIndex", "通道序号应该大于0");
                }

                foreach (FlowChannel newChannel in channels)
                {
                    ModelStateDictionary channelModelState = ChannelsManager.CheckUpdateError(_context, newChannel);
                    if (channelModelState.IsValid)
                    {
                        if (_context.Device_Channels.Count(dc =>
                                                           dc.ChannelId == newChannel.ChannelId && dc.DeviceId != device.DeviceId) != 0)
                        {
                            modelState.AddModelError("ChannelId", $"通道 {newChannel.ChannelId} 已经关联在其他设备");
                        }
                    }
                    else
                    {
                        foreach (var(key, value) in channelModelState)
                        {
                            foreach (var error in value.Errors)
                            {
                                modelState.AddModelError(key, error.ErrorMessage);
                            }
                        }
                    }
                }
            }
            return(modelState);
        }
Пример #2
0
 /// <summary>
 /// 更新设备下的通道集合
 /// </summary>
 /// <param name="device">设备</param>
 /// <param name="channels">通道集合</param>
 private void UpdateChannels(FlowDevice device, List <FlowChannel> channels)
 {
     device.FlowDevice_FlowChannels = new List <FlowDevice_FlowChannel>();
     if (channels != null)
     {
         foreach (var channel in channels)
         {
             FlowDevice_FlowChannel relation = new FlowDevice_FlowChannel
             {
                 DeviceId  = device.DeviceId,
                 ChannelId = channel.ChannelId
             };
             device.FlowDevice_FlowChannels.Add(relation);
             if (!ChannelsManager.UpdateChannel(_context, channel))
             {
                 ChannelsManager.AddChannel(_context, channel);
             }
         }
     }
 }