/// <summary> /// 添加设备 /// </summary> /// <param name="deviceInsert">设备信息</param> /// <param name="userName">用户名</param> /// <returns>添加结果</returns> public ObjectResult Add(FlowDeviceInsert deviceInsert, string userName = null) { FlowDevice device = new FlowDevice { DeviceId = 0, DeviceName = deviceInsert.DeviceName, DeviceModel = deviceInsert.DeviceModel, DeviceStatus = (int)DeviceStatus.异常, Ip = deviceInsert.Ip, Port = deviceInsert.Port }; try { UpdateChannels(device, deviceInsert.Channels); _context.Devices.Add(device); _context.SaveChanges(); _logger.LogInformation(new EventId((int)LogEvent.编辑设备, userName), $"添加设备 {device}"); return(new OkObjectResult(device)); } catch (Exception) { ModelStateDictionary modelState = CheckError(device, deviceInsert.Channels); if (modelState.IsValid) { throw; } else { return(new BadRequestObjectResult(modelState)); } } }
/// <summary> /// 对设备包含的子项排序 /// </summary> /// <param name="device">设备</param> /// <param name="order">排序方式</param> /// <returns>设备</returns> protected FlowDevice OrderInclude(FlowDevice device, string order) { if (order == "status") { device.FlowDevice_FlowChannels = device.FlowDevice_FlowChannels .OrderBy(r => r.Channel.ChannelStatus) .ThenBy(r => r.Channel.ChannelIndex) .ToList(); } else { device.FlowDevice_FlowChannels = device.FlowDevice_FlowChannels .OrderBy(c => c.Channel.ChannelIndex) .ToList(); } foreach (var relation in device.FlowDevice_FlowChannels) { _memoryCache.FillChannel(relation.Channel); if (relation.Channel.RoadSection != null) { _memoryCache.FillSection(relation.Channel.RoadSection); } relation.Channel.Lanes = relation.Channel.Lanes .OrderBy(l => l.LaneIndex) .Select(l => _memoryCache.FillLane(l)) .ToList(); } return(device); }
/// <summary> /// 填充设备缓存 /// </summary> /// <param name="memoryCache">缓存</param> /// <param name="device">设备</param> /// <returns>设备</returns> public static FlowDevice FillDevice(this IMemoryCache memoryCache, FlowDevice device) { if (device != null) { device.DeviceStatus_Desc = memoryCache.GetCode(typeof(DeviceStatus), device.DeviceStatus); device.DeviceModel_Desc = memoryCache.GetCode(typeof(DeviceModel), device.DeviceModel); } return(device); }
/// <summary> /// 删除流量设备 /// </summary> /// <param name="deviceId">设备编号</param> /// <param name="userName">用户名</param> /// <returns>删除结果</returns> public IStatusCodeActionResult Remove(int deviceId, string userName = null) { FlowDevice device = _context.Devices.SingleOrDefault(d => d.DeviceId == deviceId); if (device == null) { return(new NotFoundResult()); } _context.Devices.Remove(device); _context.SaveChanges(); _logger.LogInformation(new EventId((int)LogEvent.编辑设备, userName), $"删除设备 {device}"); return(new OkResult()); }
/// <summary> /// 更新设备标注状态 /// </summary> /// <param name="deviceUpdateLocation">设备标注状态</param> /// <returns>更新结果</returns> public IStatusCodeActionResult UpdateLocation(FlowDeviceUpdateLocation deviceUpdateLocation) { FlowDevice device = _context.Devices.SingleOrDefault(d => d.DeviceId == deviceUpdateLocation.DeviceId); if (device == null) { return(new NotFoundResult()); } device.Location = deviceUpdateLocation.Location; device.Marked = true; _context.Devices.Update(device); _context.SaveChanges(); return(new OkResult()); }
/// <summary> /// 查询流量设备 /// </summary> /// <param name="deviceId">设备编号</param> /// <returns>查询结果</returns> public IStatusCodeActionResult Get(int deviceId) { FlowDevice device = Include(_context.Devices) .SingleOrDefault(c => c.DeviceId == deviceId); if (device == null) { return(new NotFoundResult()); } else { _memoryCache.FillDevice(device); OrderInclude(device, null); return(new OkObjectResult(device)); } }
/// <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); }
/// <summary> /// 更新设备状态 /// </summary> /// <param name="deviceUpdateStatus">设备状态</param> /// <returns>更新结果</returns> public IStatusCodeActionResult UpdateStatus(FlowDeviceUpdateStatus deviceUpdateStatus) { FlowDevice device = _context.Devices.SingleOrDefault(d => d.DeviceId == deviceUpdateStatus.DeviceId); if (device == null) { return(new NotFoundResult()); } device.DeviceStatus = deviceUpdateStatus.DeviceStatus; device.License = deviceUpdateStatus.License; device.Runtime = deviceUpdateStatus.Runtime; device.Systime = deviceUpdateStatus.Systime; device.Space = deviceUpdateStatus.Space; _context.Devices.Update(device); _context.SaveChanges(); return(new OkResult()); }
/// <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); } } } }
/// <summary> /// 更新设备 /// </summary> /// <param name="deviceUpdate">设备信息</param> /// <param name="userName">用户名</param> /// <returns>更新结果</returns> public IStatusCodeActionResult Update(FlowDeviceUpdate deviceUpdate, string userName = null) { FlowDevice device = _context.Devices .Include(d => d.FlowDevice_FlowChannels) .SingleOrDefault(d => d.DeviceId == deviceUpdate.DeviceId); if (device == null) { return(new NotFoundResult()); } device.DeviceName = deviceUpdate.DeviceName; device.DeviceModel = deviceUpdate.DeviceModel; device.Ip = deviceUpdate.Ip; device.Port = deviceUpdate.Port; try { UpdateChannels(device, deviceUpdate.Channels); _context.Devices.Update(device); _context.SaveChanges(); _logger.LogInformation(new EventId((int)LogEvent.编辑设备, userName), $"更新设备 {device}"); return(new OkResult()); } catch (Exception) { ModelStateDictionary modelState = CheckError(device, deviceUpdate.Channels); if (modelState.IsValid) { throw; } else { return(new BadRequestObjectResult(modelState)); } } }
public static List <FlowDevice> CreateFlowDevice(IServiceProvider serviceProvider, int deviceCount, int channelCount, int laneCount, bool initDatabase = false, string ip1 = "127.0.0.", int ip2 = 1, int id = 100) { List <FlowDevice> devices = new List <FlowDevice>(); using (FlowContext context = serviceProvider.CreateScope().ServiceProvider.GetRequiredService <FlowContext>()) { if (initDatabase) { ResetDatabase(serviceProvider); } for (int i = 0; i < deviceCount; ++i) { FlowDevice device = new FlowDevice { DeviceId = id, DeviceName = $"流量测试设备_{id}", DeviceModel = (int)DeviceModel.MO_AF_A11_04_4X, Ip = $"{ip1}{ip2++}", Port = 17000, FlowDevice_FlowChannels = new List <FlowDevice_FlowChannel>() }; for (int j = 0; j < channelCount; ++j) { RoadCrossing roadCrossing = new RoadCrossing { CrossingId = id, CrossingName = $"流量测试路口_{id}" }; RoadSection roadSection = new RoadSection { SectionId = id, SectionName = $"流量测试通路段_{id}", SectionType = (int)SectionType.主干路, SpeedLimit = 10, Length = 10, Direction = (int)LaneDirection.由东向西 }; FlowChannel channel = new FlowChannel { ChannelId = $"channel_{id}", ChannelName = $"流量测试通道_{id}", ChannelIndex = j + 1, CrossingId = id, SectionId = id, ChannelType = (int)ChannelType.GB28181, Lanes = new List <Lane>(), RoadCrossing = roadCrossing, RoadSection = roadSection }; FlowDevice_FlowChannel relation = new FlowDevice_FlowChannel { DeviceId = id, ChannelId = channel.ChannelId, Channel = channel }; id++; device.FlowDevice_FlowChannels.Add(relation); for (int k = 0; k < laneCount; ++k) { LaneDirection direction; if (k >= 0 && k < 3) { direction = LaneDirection.由南向北; } else if (k >= 3 && k < 6) { direction = LaneDirection.由北向南; } else if (k >= 6 && k < 9) { direction = LaneDirection.由东向西; } else { direction = LaneDirection.由西向东; } FlowDirection flowDirection; if (k % 3 == 0) { flowDirection = FlowDirection.直行; } else if (k % 3 == 1) { flowDirection = FlowDirection.左转; } else { flowDirection = FlowDirection.右转; } channel.Lanes.Add(new Lane { ChannelId = channel.ChannelId, LaneId = $"{k + 1:D2}", LaneName = $"流量测试车道_{k + 1:D2}", Channel = channel, Direction = (int)direction, FlowDirection = (int)flowDirection, LaneIndex = k + 1, Region = "[]", Length = 10 }); } } context.Devices.Add(device); devices.Add(device); context.SaveChanges(); } } return(devices); }
public FlowDevicePayload(FlowDevice payload) { data = payload; type = FlowNetworkManager.clientType; }
/// <summary> /// 获取设备在客户端集合中的url /// </summary> /// <param name="device">设备</param> /// <param name="url">相对路径</param> /// <returns>url</returns> private Uri GetDeviceUrl(FlowDevice device, string url) { return(new Uri($"ws://{device.Ip}:{device.Port}/{url}")); }
/// <summary> /// 获取设备在客户端集合中的key /// </summary> /// <param name="device">设备</param> /// <returns>key</returns> private string GetDeviceKey(FlowDevice device) { return(GetDeviceUrl(device, string.Empty).Authority); }
public FlowDeviceCommand(FlowDevice devicePayload) { value = new FlowDevicePayload(devicePayload); }