/// <summary> /// 更新通道 /// </summary> /// <param name="deviceContext">数据库实例</param> /// <param name="updateChannel">更新的通道</param> /// <returns>更新成功时返回true,如果未找到通道返回false</returns> public static bool UpdateChannel(DensityContext deviceContext, DensityChannel updateChannel) { DensityChannel channel = deviceContext.Channels .Include(c => c.Regions) .SingleOrDefault(c => c.ChannelId == updateChannel.ChannelId); if (channel == null) { return(false); } else { channel.ChannelName = updateChannel.ChannelName; channel.ChannelType = updateChannel.ChannelType; channel.ChannelIndex = updateChannel.ChannelIndex; channel.RtspUser = updateChannel.RtspUser; channel.RtspPassword = updateChannel.RtspPassword; channel.RtspProtocol = updateChannel.RtspProtocol; channel.IsLoop = updateChannel.IsLoop; channel.CrossingId = updateChannel.CrossingId; channel.RoadCrossing = null; channel.Regions = updateChannel.Regions; deviceContext.Channels.Update(channel); return(true); } }
/// <summary> /// 更新通道 /// </summary> /// <param name="updateChannel">通道</param> /// <param name="userName">用户名</param> /// <returns>更新结果</returns> public IStatusCodeActionResult Update([FromBody] DensityChannel updateChannel, string userName = null) { try { if (UpdateChannel(_context, updateChannel)) { _context.SaveChanges(); _logger.LogInformation(new EventId((int)LogEvent.编辑通道, userName), $"更新通道 {updateChannel}"); return(new OkResult()); } else { return(new NotFoundResult()); } } catch (Exception) { ModelStateDictionary modelState = CheckUpdateError(_context, updateChannel); if (modelState.IsValid) { throw; } else { return(new BadRequestObjectResult(modelState)); } } }
/// <summary> /// 更新通道状态 /// </summary> /// <param name="channelUpdateStatus">通道状态</param> /// <returns>更新结果</returns> public IStatusCodeActionResult UpdateStatus(DensityChannelUpdateStatus channelUpdateStatus) { DensityChannel channel = _context.Channels.SingleOrDefault(c => c.ChannelId == channelUpdateStatus.ChannelId); if (channel == null) { return(new NotFoundResult()); } channel.ChannelStatus = channelUpdateStatus.ChannelStatus; _context.Channels.Update(channel); _context.SaveChanges(); return(new OkResult()); }
/// <summary> /// 删除通道 /// </summary> /// <param name="channelId">通道编号</param> /// <param name="userName">用户名</param> /// <returns>删除结果</returns> public IStatusCodeActionResult Remove([FromRoute] string channelId, string userName = null) { channelId = Uri.UnescapeDataString(channelId); DensityChannel channel = _context.Channels.SingleOrDefault(c => c.ChannelId == channelId); if (channel == null) { return(new NotFoundResult()); } _context.Channels.Remove(channel); _context.SaveChanges(); _logger.LogInformation(new EventId((int)LogEvent.编辑通道, userName), $"删除通道 {channel}"); return(new OkResult()); }
/// <summary> /// 查询通道 /// </summary> /// <param name="channelId"/>通道编号/param> /// <returns>查询结果</returns> public IStatusCodeActionResult Get(string channelId) { DensityChannel channel = Include(_context.Channels) .SingleOrDefault(c => c.ChannelId == channelId); if (channel == null) { return(new NotFoundObjectResult(null)); } else { _memoryCache.FillChannel(channel); return(new OkObjectResult(channel)); } }
/// <summary> /// 更新通道标注状态 /// </summary> /// <param name="channelUpdateLocation">通道标注状态</param> /// <returns>更新结果</returns> public IStatusCodeActionResult UpdateLocation([FromBody] DensityChannelUpdateLocation channelUpdateLocation) { DensityChannel channel = _context.Channels.SingleOrDefault(d => d.ChannelId == channelUpdateLocation.ChannelId); if (channel == null) { return(new NotFoundResult()); } channel.Location = channelUpdateLocation.Location; channel.Marked = true; _context.Channels.Update(channel); _context.SaveChanges(); return(new OkResult()); }
/// <summary> /// 填充通道缓存 /// </summary> /// <param name="memoryCache">缓存</param> /// <param name="channel">通道</param> /// <returns>通道</returns> public static DensityChannel FillChannel(this IMemoryCache memoryCache, DensityChannel channel) { if (channel != null) { channel.ChannelStatus_Desc = memoryCache.GetCode(typeof(DeviceStatus), channel.ChannelStatus); channel.ChannelType_Desc = memoryCache.GetCode(typeof(ChannelType), channel.ChannelType); if (channel.RtspProtocol.HasValue) { channel.RtspProtocol_Desc = memoryCache.GetCode(typeof(RtspProtocol), channel.RtspProtocol.Value); } } return(channel); }
/// <summary> /// 添加通道 /// </summary> /// <param name="channel">通道</param> /// <param name="userName">用户名</param> /// <returns>添加结果</returns> public ObjectResult Add(DensityChannel channel, string userName = null) { try { AddChannel(_context, channel); _context.SaveChanges(); _logger.LogInformation(new EventId((int)LogEvent.编辑通道, userName), $"添加通道 {channel}"); return(new OkObjectResult(channel)); } catch (Exception) { ModelStateDictionary modelState = CheckInsertError(_context, channel); if (modelState.IsValid) { throw; } else { return(new BadRequestObjectResult(modelState)); } } }
/// <summary> /// 检查通道添加错误原因 /// </summary> /// <param name="deviceContext">数据库实例</param> /// <param name="channel">通道</param> /// <returns>数据校验结果</returns> private static ModelStateDictionary CheckInsertError(DensityContext deviceContext, DensityChannel channel) { ModelStateDictionary modelState = CheckUpdateError(deviceContext, channel); if (deviceContext.Channels.Count(c => c.ChannelId == channel.ChannelId) > 0) { modelState.AddModelError("ChannelId", $"通道编号重复 {channel.ChannelId}"); } return(modelState); }
/// <summary> /// 检查通道更新错误原因 /// </summary> /// <param name="deviceContext">数据库实例</param> /// <param name="channel">通道</param> /// <returns>数据校验结果</returns> public static ModelStateDictionary CheckUpdateError(DensityContext deviceContext, DensityChannel channel) { ModelStateDictionary modelState = new ModelStateDictionary(); if (channel.CrossingId.HasValue) { if (deviceContext.RoadCrossings.Count(d => d.CrossingId == channel.CrossingId) == 0) { modelState.AddModelError("CrossingId", $"不存在路口编号 {channel.CrossingId}"); } } return(modelState); }
/// <summary> /// 添加通道 /// </summary> /// <param name="deviceContext">数据库上下文</param> /// <param name="newChannel">新通道</param> public static void AddChannel(DensityContext deviceContext, DensityChannel newChannel) { newChannel.ChannelStatus = (int)DeviceStatus.异常; newChannel.RoadCrossing = null; deviceContext.Channels.Add(newChannel); }
public static List <DensityDevice> CreateDensityDevice(IServiceProvider serviceProvider, int deviceCount, int channelCount, int regionCount, string ip = "127.0.0.1", bool initDatabase = false) { List <DensityDevice> devices = new List <DensityDevice>(); using (IServiceScope serviceScope = serviceProvider.CreateScope()) { using (DensityContext context = serviceScope.ServiceProvider.GetRequiredService <DensityContext>()) { if (initDatabase) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); } int deviceId = 20000; int crossingId = 20000; int regionId = 20000; int port = 17000; for (int i = 0; i < deviceCount; ++i) { DensityDevice device = new DensityDevice { DeviceId = deviceId, DeviceModel = (int)DeviceModel.MO_AF_A11_04_4X, Ip = ip, DataPort = port, Port = port }; device.DeviceName = "高点测试设备" + device.DataPort; device.DensityDevice_DensityChannels = new List <DensityDevice_DensityChannel>(); for (int j = 0; j < channelCount; ++j) { RoadCrossing roadCrossing = new RoadCrossing { CrossingId = crossingId, CrossingName = "高点测试路口" + crossingId }; DensityChannel channel = new DensityChannel() { ChannelId = $"channel_{device.DeviceId}_{j + 1}", ChannelName = $"高点测试通道 { device.DeviceId} {j + 1}", ChannelType = (int)ChannelType.GB28181, ChannelIndex = j + 1, CrossingId = crossingId, Regions = new List <TrafficRegion>(), RoadCrossing = roadCrossing }; DensityDevice_DensityChannel relation = new DensityDevice_DensityChannel { ChannelId = channel.ChannelId, DeviceId = device.DeviceId, Channel = channel }; port++; deviceId++; crossingId++; device.DensityDevice_DensityChannels.Add(relation); for (int k = 0; k < regionCount; ++k) { channel.Regions.Add(new TrafficRegion { ChannelId = channel.ChannelId, Channel = channel, RegionIndex = k + 1, RegionName = "高点测试区域" + regionId++, Region = "[]", IsVip = true, CarCount = 1, DensityRange = 1, Density = 1, Frequency = 1, Warning = 1, Saturation = 1, WarningDuration = 1 }); } } context.Devices.Add(device); devices.Add(device); context.SaveChanges(); } } } return(devices); }
public IActionResult Update([FromBody] DensityChannel updateChannel) { return(_manager.Update(updateChannel)); }
public IActionResult Add([FromBody] DensityChannel channel) { return(_manager.Add(channel, User?.Identity?.Name)); }
public void QueryVipRegions() { List <DensityDevice> devices = new List <DensityDevice>(); int deviceCount = 1; int channelCount = 1; int regionCount = 12; HashSet <string> vips = new HashSet <string>(); //随机创建重点区域 Random random = new Random(); using (IServiceScope serviceScope = TestInit.ServiceProvider.CreateScope()) { using (DensityContext context = serviceScope.ServiceProvider.GetRequiredService <DensityContext>()) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); int deviceId = 1; int crossingId = 1; int regionId = 1; int channelId = 1; for (int i = 0; i < deviceCount; ++i) { DensityDevice densityDevice = new DensityDevice { DeviceId = deviceId++, Ip = "192.168.200.204", Port = 18000 + i }; densityDevice.DeviceName = "设备" + densityDevice.Port; densityDevice.DensityDevice_DensityChannels = new List <DensityDevice_DensityChannel>(); for (int j = 0; j < channelCount; ++j) { RoadCrossing roadCrossing = new RoadCrossing { CrossingId = crossingId, CrossingName = "路口" + crossingId }; DensityChannel channel = new DensityChannel() { ChannelId = channelId.ToString(), ChannelName = $"通道 {densityDevice.DeviceId} {j+1}", ChannelIndex = j + 1, CrossingId = crossingId, Regions = new List <TrafficRegion>(), RoadCrossing = roadCrossing }; DensityDevice_DensityChannel relation = new DensityDevice_DensityChannel { ChannelId = channel.ChannelId, DeviceId = densityDevice.DeviceId, Channel = channel }; channelId++; crossingId++; densityDevice.DensityDevice_DensityChannels.Add(relation); for (int k = 0; k < regionCount; ++k) { int value = random.Next(1, 2); TrafficRegion region = new TrafficRegion { ChannelId = channel.ChannelId, Channel = channel, Region = "1", RegionIndex = k + 1, RegionName = "区域" + regionId++, IsVip = value == 1 }; if (value == 1) { vips.Add(region.DataId); } channel.Regions.Add(region); } } context.Devices.Add(densityDevice); devices.Add(densityDevice); } context.SaveChanges(); } DensityDbSimulator.CreateData(TestInit.ServiceProvider, devices, DataCreateMode.Fixed, DateTime.Today, DateTime.Today); TestInit.RefreshDensityCache(devices); DensitiesManager manager = TestInit.ServiceProvider.GetRequiredService <DensitiesManager>(); var v = manager.QueryVipRegions(); foreach (TrafficDensity density in v) { Assert.IsTrue(vips.Contains(density.DataId)); } } }