/// <summary> /// 添加路口 /// </summary> /// <param name="roadCrossing">路口</param> /// <param name="userName">用户名</param> /// <returns>添加结果</returns> public ObjectResult Add([FromBody] RoadCrossing roadCrossing, string userName = null) { _context.RoadCrossings.Add(roadCrossing); _context.SaveChanges(); _logger.LogInformation(new EventId((int)LogEvent.编辑路口, userName), $"添加路口 {roadCrossing}"); return(new OkObjectResult(roadCrossing)); }
/// <summary> /// 入库处理函数 /// </summary> /// <param name="data">交通事件</param> protected override void Handle(TrafficEvent data) { using (IServiceScope serviceScope = _serviceProvider.CreateScope()) { using (DensityContext context = serviceScope.ServiceProvider.GetRequiredService <DensityContext>()) { try { TrafficEvent dbData = context.Events.SingleOrDefault(e => e.DataId == data.DataId && e.DateTime == data.DateTime); if (dbData == null) { Failed += 1; _logger.LogWarning((int)LogEvent.事件数据块, "交通事件更新失败"); } else { dbData.EndTime = data.EndTime; context.SaveChanges(); Success += 1; } } catch (Exception ex) { _logger.LogError((int)LogEvent.事件数据块, ex, "事件数据更新异常"); Failed += 1; } } } }
/// <summary> /// 添加设备 /// </summary> /// <param name="deviceInsert">设备信息</param> /// <param name="userName">用户名</param> /// <returns>添加结果</returns> public ObjectResult Add(DensityDeviceInsert deviceInsert, string userName = null) { DensityDevice device = new DensityDevice { DeviceId = 0, DeviceName = deviceInsert.DeviceName, DeviceModel = deviceInsert.DeviceModel, DeviceStatus = (int)DeviceStatus.异常, Ip = deviceInsert.Ip, Port = deviceInsert.Port, DataPort = deviceInsert.DataPort }; 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="data">交通事件</param> protected override void Handle(TrafficEvent data) { using (IServiceScope serviceScope = _serviceProvider.CreateScope()) { using (DensityContext context = serviceScope.ServiceProvider.GetRequiredService <DensityContext>()) { try { context.Events.Add(data); context.SaveChanges(); Success += 1; } catch (Exception ex) { _logger.LogError((int)LogEvent.事件数据块, ex, "事件数据入库异常"); Failed += 1; } } } }
/// <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)); } } }
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 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)); } } }
/// <summary> /// 初始化数据库 /// </summary> private async void InitDb() { _logger.LogInformation((int)LogEvent.系统, "初始化数据库"); DateTime minTime = TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel); using (IServiceScope serviceScope = _serviceProvider.CreateScope()) { using (DensityContext context = serviceScope.ServiceProvider.GetRequiredService <DensityContext>()) { if (context.Database.EnsureCreated()) { #region 用户 _logger.LogInformation((int)LogEvent.系统, "创建管理员用户"); UserManager <IdentityUser> userManager = serviceScope.ServiceProvider.GetRequiredService <UserManager <IdentityUser> >(); await userManager.CreateAsync(new IdentityUser("admin"), "123456"); #endregion #region 权限 _logger.LogInformation((int)LogEvent.系统, "创建权限"); IdentityUser adminUser = await userManager.FindByNameAsync("admin"); List <YumekoJabami.Models.Claim> claims = new List <YumekoJabami.Models.Claim> { new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03000000", Descirption = "智慧高点视频检测系统" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03010000", Descirption = "设备管理" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03010100", Descirption = "设备信息维护" }, //new YumekoJabami.Models.Claim{ Type = ClaimTypes.Webpage, Value = "03010200", Descirption = "设备位置维护"}, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03010300", Descirption = "国标网关设置" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03010400", Descirption = "设备运行状态" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03010500", Descirption = "视频信息维护" }, //new YumekoJabami.Models.Claim{ Type = ClaimTypes.Webpage, Value = "03010600", Descirption = "视频位置维护"}, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03020000", Descirption = "数据分析" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03020100", Descirption = "交通密度查询" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03020200", Descirption = "交通密度分析" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03020300", Descirption = "拥堵事件统计" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03020400", Descirption = "拥堵事件排名" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03020500", Descirption = "拥堵高发时段" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03030000", Descirption = "系统设置" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03030100", Descirption = "路口维护" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03030300", Descirption = "用户管理" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03030400", Descirption = "角色管理" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03030600", Descirption = "字典管理" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03030700", Descirption = "参数管理" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03030800", Descirption = "日志查询" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03030900", Descirption = "系统监控" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03040000", Descirption = "状况监测" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03040100", Descirption = "应用检测" }, }; context.TrafficClaims.AddRange(claims); foreach (YumekoJabami.Models.Claim claim in claims) { await userManager.AddClaimAsync(adminUser, new Claim(claim.Type, claim.Value)); } #endregion #region 字典 _logger.LogInformation((int)LogEvent.系统, "创建字典"); List <Code> densityCodes = new List <Code> { new Code { Key = "DensityDateLevel", Value = (int)DateTimeLevel.FiveMinutes, Description = "五分钟密度" }, new Code { Key = "DensityDateLevel", Value = (int)DateTimeLevel.FifteenMinutes, Description = "十五分钟密度" }, new Code { Key = "DensityDateLevel", Value = (int)DateTimeLevel.Hour, Description = "一小时密度" }, new Code { Key = "DensityDateLevel", Value = (int)DateTimeLevel.Day, Description = "一天密度" }, new Code { Key = "DensityDateLevel", Value = (int)DateTimeLevel.Month, Description = "一月密度" } }; densityCodes.AddRange(Enum.GetValues(typeof(ChannelType)) .Cast <ChannelType>() .Select(e => new Code { Key = typeof(ChannelType).Name, Value = (int)e, Description = e.ToString() })); densityCodes.AddRange(Enum.GetValues(typeof(DeviceModel)) .Cast <DeviceModel>() .Select(e => new Code { Key = typeof(DeviceModel).Name, Value = (int)e, Description = e.ToString() })); densityCodes.AddRange(Enum.GetValues(typeof(DeviceStatus)) .Cast <DeviceStatus>() .Select(e => new Code { Key = typeof(DeviceStatus).Name, Value = (int)e, Description = e.ToString() })); densityCodes.AddRange(Enum.GetValues(typeof(RtspProtocol)) .Cast <RtspProtocol>() .Select(e => new Code { Key = typeof(RtspProtocol).Name, Value = (int)e, Description = e.ToString() })); densityCodes.AddRange(Enum.GetValues(typeof(LogEvent)) .Cast <LogEvent>() .Select(e => new Code { Key = typeof(LogEvent).Name, Value = (int)e, Description = e.ToString() })); densityCodes.Add(new Code { Key = "LogLevel", Value = (int)LogLevel.Debug, Description = "调试" }); densityCodes.Add(new Code { Key = "LogLevel", Value = (int)LogLevel.Information, Description = "消息" }); densityCodes.Add(new Code { Key = "LogLevel", Value = (int)LogLevel.Warning, Description = "警告" }); densityCodes.Add(new Code { Key = "LogLevel", Value = (int)LogLevel.Error, Description = "错误" }); context.Codes.AddRange(densityCodes); #endregion context.Version.Add(new TrafficVersion { Version = Assembly.GetExecutingAssembly().GetName().Version.ToString() }); context.SaveChanges(); } else { context.SaveChanges(); try { TrafficDensity_One density = context.Densities_One.OrderByDescending(d => d.Id).FirstOrDefault(); if (density != null && TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel, density.DateTime) != minTime) { context.ChangeDatabase(BranchDbConvert.GetTableName(density.DateTime)); } } catch (MySqlException) { } } } } }