Example #1
0
 public void Handle(DateTime lastTime, DateTime currentTime, DateTime nextTime)
 {
     _logger.LogInformation((int)LogEvent.分支切换, $"开始分支切换 {lastTime}->{currentTime}");
     _branchBlock.Close();
     _logger.LogInformation((int)LogEvent.分支切换, "保存当前数据");
     using (IServiceScope serviceScope = _serviceProvider.CreateScope())
     {
         string tableName = BranchDbConvert.GetTableName(lastTime);
         ChangeDatabase(serviceScope.ServiceProvider, tableName);
     }
     _branchBlock.SwitchBranch(currentTime, nextTime);
     _logger.LogInformation((int)LogEvent.分支切换, $"切换完成分支 {currentTime}->{nextTime}");
 }
Example #2
0
        /// <summary>
        /// 初始化数据库
        /// </summary>
        private async void InitDb()
        {
            _logger.LogInformation((int)LogEvent.系统, "初始化数据库");

            DateTime minTime = TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel);

            using (IServiceScope serviceScope = _serviceProvider.CreateScope())
            {
                using (FlowContext context = serviceScope.ServiceProvider.GetRequiredService <FlowContext>())
                {
                    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 = "02000000", Descirption = "智慧交通视频检测系统"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02010000", Descirption = "设备管理"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02010100", Descirption = "设备信息维护"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02010200", Descirption = "设备位置维护"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02010300", Descirption = "国标网关设置"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02010400", Descirption = "校时配置"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02010500", Descirption = "设备操作"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02010600", Descirption = "设备运行状态"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02010700", Descirption = "视频信息维护"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02010800", Descirption = "视频位置维护"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02020000", Descirption = "数据分析"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02020100", Descirption = "通行信息查询"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02020200", Descirption = "流量数据分析"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02020300", Descirption = "IO数据监测"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02020400", Descirption = "流量分布查询"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02020500", Descirption = "拥堵趋势分析"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02020600", Descirption = "状态时间统计"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02020700", Descirption = "交通状态分析"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02030000", Descirption = "系统设置"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02030100", Descirption = "路口维护"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02030200", Descirption = "路段维护"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02030300", Descirption = "用户管理"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02030400", Descirption = "角色管理"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02030600", Descirption = "字典管理"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02030700", Descirption = "参数管理"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02030800", Descirption = "日志查询"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02030900", Descirption = "系统监控"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02040000", Descirption = "状况监测"
                            },
                            new YumekoJabami.Models.Claim {
                                Type = ClaimTypes.Webpage, Value = "02040100", Descirption = "应用检测"
                            },
                        };
                        context.TrafficClaims.AddRange(claims);
                        foreach (YumekoJabami.Models.Claim claim in claims)
                        {
                            await userManager.AddClaimAsync(adminUser, new System.Security.Claims.Claim(claim.Type, claim.Value));
                        }
                        #endregion

                        #region 字典
                        _logger.LogInformation((int)LogEvent.系统, "创建字典");
                        List <Code> flowCodes = new List <Code>();

                        flowCodes.AddRange(Enum.GetValues(typeof(LogEvent))
                                           .Cast <LogEvent>()
                                           .Select(e => new Code {
                            Key = typeof(LogEvent).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.Add(new Code
                        {
                            Key         = "LogLevel",
                            Value       = (int)LogLevel.Debug,
                            Description = "调试"
                        });

                        flowCodes.Add(new Code
                        {
                            Key         = "LogLevel",
                            Value       = (int)LogLevel.Information,
                            Description = "消息"
                        });

                        flowCodes.Add(new Code
                        {
                            Key         = "LogLevel",
                            Value       = (int)LogLevel.Warning,
                            Description = "警告"
                        });

                        flowCodes.Add(new Code
                        {
                            Key         = "LogLevel",
                            Value       = (int)LogLevel.Error,
                            Description = "错误"
                        });


                        flowCodes.AddRange(Enum.GetValues(typeof(ChannelType))
                                           .Cast <ChannelType>()
                                           .Select(e => new Code {
                            Key = typeof(ChannelType).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.AddRange(Enum.GetValues(typeof(DeviceModel))
                                           .Cast <DeviceModel>()
                                           .Select(e => new Code {
                            Key = typeof(DeviceModel).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.AddRange(Enum.GetValues(typeof(DeviceStatus))
                                           .Cast <DeviceStatus>()
                                           .Select(e => new Code {
                            Key = typeof(DeviceStatus).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.AddRange(Enum.GetValues(typeof(RtspProtocol))
                                           .Cast <RtspProtocol>()
                                           .Select(e => new Code {
                            Key = typeof(RtspProtocol).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.AddRange(Enum.GetValues(typeof(SectionDirection))
                                           .Cast <SectionDirection>()
                                           .Select(e => new Code {
                            Key = typeof(SectionDirection).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.AddRange(Enum.GetValues(typeof(SectionType))
                                           .Cast <SectionType>()
                                           .Select(e => new Code {
                            Key = typeof(SectionType).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.AddRange(Enum.GetValues(typeof(Age))
                                           .Cast <Age>()
                                           .Select(e => new Code {
                            Key = typeof(Age).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.AddRange(Enum.GetValues(typeof(NonVehicle))
                                           .Cast <NonVehicle>()
                                           .Select(e => new Code {
                            Key = typeof(NonVehicle).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.AddRange(Enum.GetValues(typeof(CarColor))
                                           .Cast <CarColor>()
                                           .Select(e => new Code {
                            Key = typeof(CarColor).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.AddRange(Enum.GetValues(typeof(CarType))
                                           .Cast <CarType>()
                                           .Select(e => new Code {
                            Key = typeof(CarType).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.AddRange(Enum.GetValues(typeof(FlowDirection))
                                           .Cast <FlowDirection>()
                                           .Select(e => new Code {
                            Key = typeof(FlowDirection).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.Add(new Code
                        {
                            Key         = "VehicleType",
                            Value       = (int)FlowType.轮车,
                            Description = FlowType.轮车.ToString()
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "VehicleType",
                            Value       = (int)FlowType.卡车,
                            Description = FlowType.卡车.ToString()
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "VehicleType",
                            Value       = (int)FlowType.客车,
                            Description = FlowType.客车.ToString()
                        });

                        flowCodes.Add(new Code
                        {
                            Key         = "VehicleType",
                            Value       = (int)FlowType.轿车,
                            Description = FlowType.轿车.ToString()
                        });

                        flowCodes.Add(new Code
                        {
                            Key         = "VehicleType",
                            Value       = (int)FlowType.面包车,
                            Description = FlowType.面包车.ToString()
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "BikeType",
                            Value       = (int)FlowType.自行车,
                            Description = FlowType.自行车.ToString()
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "BikeType",
                            Value       = (int)FlowType.摩托车,
                            Description = FlowType.摩托车.ToString()
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "PedestrainType",
                            Value       = (int)FlowType.行人,
                            Description = FlowType.行人.ToString()
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "FlowType",
                            Value       = (int)FlowType.平均速度,
                            Description = FlowType.平均速度.ToString()
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "FlowType",
                            Value       = (int)FlowType.车头时距,
                            Description = FlowType.车头时距.ToString()
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "FlowType",
                            Value       = (int)FlowType.车头间距,
                            Description = FlowType.车头间距.ToString()
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "FlowType",
                            Value       = (int)FlowType.时间占有率,
                            Description = FlowType.时间占有率.ToString()
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "FlowType",
                            Value       = (int)FlowType.空间占有率,
                            Description = FlowType.空间占有率.ToString()
                        });

                        flowCodes.AddRange(Enum.GetValues(typeof(LaneDirection))
                                           .Cast <LaneDirection>()
                                           .Select(e => new Code {
                            Key = typeof(LaneDirection).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.AddRange(Enum.GetValues(typeof(LaneType))
                                           .Cast <LaneType>()
                                           .Select(e => new Code {
                            Key = typeof(LaneType).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.AddRange(Enum.GetValues(typeof(PlateType))
                                           .Cast <PlateType>()
                                           .Select(e => new Code {
                            Key = typeof(PlateType).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.AddRange(Enum.GetValues(typeof(Sex))
                                           .Cast <Sex>()
                                           .Select(e => new Code {
                            Key = typeof(Sex).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.AddRange(Enum.GetValues(typeof(TrafficStatus))
                                           .Cast <TrafficStatus>()
                                           .Select(e => new Code {
                            Key = typeof(TrafficStatus).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.AddRange(Enum.GetValues(typeof(UpperColor))
                                           .Cast <UpperColor>()
                                           .Select(e => new Code {
                            Key = typeof(UpperColor).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.AddRange(Enum.GetValues(typeof(VideoStructType))
                                           .Cast <VideoStructType>()
                                           .Select(e => new Code {
                            Key = typeof(VideoStructType).Name, Value = (int)e, Description = e.ToString()
                        }));

                        flowCodes.Add(new Code
                        {
                            Key         = "FlowDateLevel",
                            Value       = (int)DateTimeLevel.Minute,
                            Description = "一分钟"
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "FlowDateLevel",
                            Value       = (int)DateTimeLevel.FiveMinutes,
                            Description = "五分钟"
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "FlowDateLevel",
                            Value       = (int)DateTimeLevel.FifteenMinutes,
                            Description = "十五分钟"
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "FlowDateLevel",
                            Value       = (int)DateTimeLevel.Hour,
                            Description = "小时"
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "FlowDateLevel",
                            Value       = (int)DateTimeLevel.Day,
                            Description = "天"
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "FlowDateLevel",
                            Value       = (int)DateTimeLevel.Month,
                            Description = "月"
                        });

                        flowCodes.Add(new Code
                        {
                            Key         = "CongestionDateLevel",
                            Value       = (int)DateTimeLevel.Hour,
                            Description = "小时"
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "CongestionDateLevel",
                            Value       = (int)DateTimeLevel.Day,
                            Description = "天"
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "CongestionDateLevel",
                            Value       = (int)DateTimeLevel.Month,
                            Description = "月"
                        });

                        flowCodes.Add(new Code
                        {
                            Key         = "StatusTimeDateLevel",
                            Value       = (int)DateTimeLevel.Hour,
                            Description = "小时"
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "StatusTimeDateLevel",
                            Value       = (int)DateTimeLevel.Day,
                            Description = "天"
                        });
                        flowCodes.Add(new Code
                        {
                            Key         = "StatusTimeDateLevel",
                            Value       = (int)DateTimeLevel.Month,
                            Description = "月"
                        });
                        context.Codes.AddRange(flowCodes);

                        #endregion

                        context.Version.Add(new TrafficVersion
                        {
                            Version = Assembly.GetExecutingAssembly().GetName().Version.ToString()
                        });
                        context.SaveChanges();
                    }
                    else
                    {
                        try
                        {
                            LaneFlow laneFlow = context.LaneFlows_One.OrderByDescending(f => f.Id).FirstOrDefault();
                            if (laneFlow != null &&
                                TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel, laneFlow.DateTime) != minTime)
                            {
                                context.ChangeDatabase(BranchDbConvert.GetTableName(laneFlow.DateTime));
                            }

                            VideoVehicle vehicle = context.Vehicles.OrderByDescending(v => v.Id).FirstOrDefault();
                            if (vehicle != null &&
                                TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel, vehicle.DateTime) != minTime)
                            {
                                context.ChangeVehicleTable(BranchDbConvert.GetTableName(vehicle.DateTime));
                            }

                            VideoBike bike = context.Bikes.OrderByDescending(v => v.Id).FirstOrDefault();
                            if (bike != null &&
                                TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel, bike.DateTime) != minTime)
                            {
                                context.ChangeBikeTable(BranchDbConvert.GetTableName(bike.DateTime));
                            }

                            VideoPedestrain pedestrain = context.Pedestrains.OrderByDescending(v => v.Id).FirstOrDefault();
                            if (pedestrain != null &&
                                TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel, pedestrain.DateTime) != minTime)
                            {
                                context.ChangePedestrainTable(BranchDbConvert.GetTableName(pedestrain.DateTime));
                            }
                        }
                        catch (MySqlException)
                        {
                        }
                    }
                }
            }
        }
Example #3
0
        public static Dictionary <TrafficRegion, int> CreateData(IServiceProvider serviceProvider, List <DensityDevice> devices, List <DataCreateMode> modes, List <DateTime> startTimes, List <DateTime> endTimes, bool initDatabase = false)
        {
            if (initDatabase)
            {
                ResetDatabase(serviceProvider);
            }



            Dictionary <TrafficRegion, int> regions = new Dictionary <TrafficRegion, int>();

            for (int i = 0; i < startTimes.Count; ++i)
            {
                DateTime           minTime = TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel, startTimes[i]);
                DateTime           maxTime = TimePointConvert.NextTimePoint(BranchDbConvert.DateLevel, minTime);
                DensityBranchBlock branch  = new DensityBranchBlock(serviceProvider);
                branch.Open(devices, minTime, maxTime);
                Random random = new Random();
                foreach (DensityDevice device in devices)
                {
                    foreach (var relation in device.DensityDevice_DensityChannels)
                    {
                        foreach (TrafficRegion region in relation.Channel.Regions)
                        {
                            int randomValue = random.Next(1, 1000);
                            if (modes[i] == DataCreateMode.Random)
                            {
                                regions[region] = randomValue;
                            }

                            int value = 1;
                            for (int m = 0; m < 1440; ++m)
                            {
                                DateTime dataTime = startTimes[i].AddMinutes(m);
                                if (dataTime > DateTime.Now)
                                {
                                    break;
                                }
                                if (dataTime >= startTimes[i] && dataTime < endTimes[i])
                                {
                                    TrafficDensity density;
                                    if (modes[i] == DataCreateMode.Fixed)
                                    {
                                        density = new TrafficDensity
                                        {
                                            MatchId  = $"{device.Ip}_{relation.Channel.ChannelIndex}_{region.RegionIndex}",
                                            DateTime = new DateTime(dataTime.Year, dataTime.Month, dataTime.Day, dataTime.Hour, dataTime.Minute, 0),
                                            Value    = 1
                                        };
                                    }
                                    else if (modes[i] == DataCreateMode.Sequence)
                                    {
                                        density = new TrafficDensity
                                        {
                                            MatchId  = $"{device.Ip}_{relation.Channel.ChannelIndex}_{region.RegionIndex}",
                                            DateTime = new DateTime(dataTime.Year, dataTime.Month, dataTime.Day, dataTime.Hour, dataTime.Minute, 0),
                                            Value    = value
                                        };
                                    }
                                    else
                                    {
                                        density = new TrafficDensity
                                        {
                                            MatchId  = $"{device.Ip}_{relation.Channel.ChannelIndex}_{region.RegionIndex}",
                                            DateTime = new DateTime(dataTime.Year, dataTime.Month, dataTime.Day, dataTime.Hour, dataTime.Minute, 0),
                                            Value    = randomValue
                                        };
                                    }
                                    branch.Post(density);
                                }
                                ++value;
                            }
                        }
                    }
                }

                branch.Close();
                if (i == startTimes.Count - 1)
                {
                    DateTime currentTime = TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel);
                    if (minTime != currentTime)
                    {
                        using (DensityContext context = serviceProvider.GetRequiredService <DensityContext>())
                        {
                            context.ChangeDatabase(BranchDbConvert.GetTableName(minTime));
                        }
                        branch.SwitchBranch(maxTime, TimePointConvert.NextTimePoint(BranchDbConvert.DateLevel, maxTime));
                    }
                }
                else
                {
                    DateTime nextItem = TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel, startTimes[i + 1]);
                    if (minTime != nextItem)
                    {
                        using (DensityContext context = serviceProvider.GetRequiredService <DensityContext>())
                        {
                            context.ChangeDatabase(BranchDbConvert.GetTableName(minTime));
                        }
                        branch.SwitchBranch(maxTime, TimePointConvert.NextTimePoint(BranchDbConvert.DateLevel, maxTime));
                    }
                }
            }

            return(regions);
        }
Example #4
0
        public static Dictionary <string, List <LaneFlow> > CreateData(IServiceProvider serviceProvider, List <FlowDevice> devices, List <DataCreateMode> modes, List <DateTime> startTimes, List <DateTime> endTimes, bool initDatabase = false)
        {
            if (initDatabase)
            {
                ResetDatabase(serviceProvider);
            }
            Dictionary <string, List <LaneFlow> > datas = new Dictionary <string, List <LaneFlow> >();

            foreach (FlowDevice device in devices)
            {
                foreach (var relation in device.FlowDevice_FlowChannels)
                {
                    foreach (Lane lane in relation.Channel.Lanes)
                    {
                        datas.Add(lane.DataId, new List <LaneFlow>());
                    }
                }
            }
            for (int i = 0; i < startTimes.Count; ++i)
            {
                DateTime        minTime = TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel, startTimes[i]);
                DateTime        maxTime = TimePointConvert.NextTimePoint(BranchDbConvert.DateLevel, minTime);
                FlowBranchBlock branch  = new FlowBranchBlock(serviceProvider);
                branch.Open(devices, minTime, maxTime);
                Random random = new Random();
                foreach (FlowDevice device in devices)
                {
                    foreach (var relation in device.FlowDevice_FlowChannels)
                    {
                        foreach (Lane lane in relation.Channel.Lanes)
                        {
                            int value = 1;
                            for (int m = 0; m < 1440; ++m)
                            {
                                DateTime dataTime = startTimes[i].AddMinutes(m);
                                if (dataTime > DateTime.Now)
                                {
                                    break;
                                }
                                if (dataTime >= startTimes[i] && dataTime < endTimes[i])
                                {
                                    LaneFlow laneFlow;
                                    if (modes[i] == DataCreateMode.Fixed)
                                    {
                                        laneFlow = new LaneFlow
                                        {
                                            DataId           = lane.DataId,
                                            DateTime         = new DateTime(dataTime.Year, dataTime.Month, dataTime.Day, dataTime.Hour, dataTime.Minute, 0),
                                            Cars             = 1,
                                            Buss             = 1,
                                            Trucks           = 1,
                                            Vans             = 1,
                                            Tricycles        = 1,
                                            Motorcycles      = 1,
                                            Bikes            = 1,
                                            Persons          = 1,
                                            AverageSpeedData = 50,
                                            HeadDistance     = 10,
                                            Occupancy        = 30,
                                            TimeOccupancy    = 40,
                                            TrafficStatus    = TrafficStatus.轻度拥堵,
                                            Count            = 1
                                        };
                                    }
                                    else if (modes[i] == DataCreateMode.Sequence)
                                    {
                                        laneFlow = new LaneFlow
                                        {
                                            DataId           = lane.DataId,
                                            DateTime         = new DateTime(dataTime.Year, dataTime.Month, dataTime.Day, dataTime.Hour, dataTime.Minute, 0),
                                            Cars             = value++,
                                            Buss             = 2,
                                            Trucks           = 3,
                                            Vans             = 4,
                                            Tricycles        = 5,
                                            Motorcycles      = 6,
                                            Bikes            = 7,
                                            Persons          = 8,
                                            AverageSpeedData = 9,
                                            HeadDistance     = 10,
                                            Occupancy        = 12,
                                            TimeOccupancy    = 13,
                                            TrafficStatus    = TrafficStatus.通畅,
                                            Count            = 1
                                        };
                                    }
                                    else
                                    {
                                        laneFlow = new LaneFlow
                                        {
                                            DataId           = lane.DataId,
                                            DateTime         = new DateTime(dataTime.Year, dataTime.Month, dataTime.Day, dataTime.Hour, dataTime.Minute, 0),
                                            Cars             = random.Next(1, 10),
                                            Buss             = random.Next(1, 10),
                                            Trucks           = random.Next(1, 10),
                                            Vans             = random.Next(1, 10),
                                            Tricycles        = random.Next(1, 10),
                                            Motorcycles      = random.Next(1, 10),
                                            Bikes            = random.Next(1, 10),
                                            Persons          = random.Next(1, 10),
                                            AverageSpeedData = random.Next(1, 10),
                                            HeadDistance     = random.Next(1, 10),
                                            Occupancy        = random.Next(1, 10),
                                            TimeOccupancy    = random.Next(1, 10),
                                            TrafficStatus    = (TrafficStatus)random.Next(1, 6),
                                            Count            = 1
                                        };
                                    }

                                    laneFlow.SectionId     = lane.Channel.RoadSection.SectionId;
                                    laneFlow.SectionType   = lane.Channel.RoadSection.SectionType;
                                    laneFlow.SectionLength = lane.Channel.RoadSection.Length;
                                    laneFlow.FreeSpeed     = lane.Channel.RoadSection.FreeSpeed;
                                    laneFlow.Distance      = laneFlow.Vehicle * lane.Length;
                                    laneFlow.TravelTime    = laneFlow.AverageSpeedData > 0
                                        ? laneFlow.Vehicle * lane.Length / Convert.ToDouble(laneFlow.AverageSpeedData * 1000 / 3600)
                                        : 0;
                                    datas[lane.DataId].Add(laneFlow);
                                    branch.Post(laneFlow);
                                }
                                ++value;
                            }
                        }
                    }
                }

                branch.Close();
                if (i == startTimes.Count - 1)
                {
                    DateTime currentTime = TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel);
                    if (minTime != currentTime)
                    {
                        using (FlowContext context = serviceProvider.GetRequiredService <FlowContext>())
                        {
                            context.ChangeDatabase(BranchDbConvert.GetTableName(minTime));
                        }
                        branch.SwitchBranch(maxTime, TimePointConvert.NextTimePoint(BranchDbConvert.DateLevel, maxTime));
                    }
                }
                else
                {
                    DateTime nextItem = TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel, startTimes[i + 1]);
                    if (minTime != nextItem)
                    {
                        using (FlowContext context = serviceProvider.GetRequiredService <FlowContext>())
                        {
                            context.ChangeDatabase(BranchDbConvert.GetTableName(minTime));
                        }
                        branch.SwitchBranch(maxTime, TimePointConvert.NextTimePoint(BranchDbConvert.DateLevel, maxTime));
                    }
                }
            }

            return(datas);
        }
        public static void CreateData(IServiceProvider serviceProvider, List <FlowDevice> devices, DataCreateMode mode, DateTime startDate, DateTime endDate, bool initDatabase = false)
        {
            if (initDatabase)
            {
                using (FlowContext context = serviceProvider.CreateScope().ServiceProvider.GetRequiredService <FlowContext>())
                {
                    context.Database.EnsureDeleted();
                    context.Database.EnsureCreated();
                }
            }

            DateTime         minTime = TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel, startDate);
            DateTime         maxTime = TimePointConvert.NextTimePoint(BranchDbConvert.DateLevel, minTime);
            VideoBranchBlock branch  = new VideoBranchBlock(serviceProvider);

            branch.Open(minTime, maxTime);
            Random random = new Random();

            for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
            {
                int value = 1;
                for (int i = 0; i < 1440; ++i)
                {
                    DateTime startTime = startDate.AddMinutes(i);
                    foreach (FlowDevice device in devices)
                    {
                        foreach (var relation in device.FlowDevice_FlowChannels)
                        {
                            foreach (Lane lane in relation.Channel.Lanes)
                            {
                                VideoVehicle    videoVehicle;
                                VideoBike       bike;
                                VideoPedestrain pedestrain;
                                if (mode == DataCreateMode.Fixed)
                                {
                                    videoVehicle = new VideoVehicle
                                    {
                                        DataId      = lane.DataId,
                                        DateTime    = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, 0),
                                        Image       = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4p\r\nLSwzOko+MzZGNywtQFdBRkxOUlNSMj5aYVpQYEpRUk//2wBDAQ4ODhMREyYVFSZPNS01T09PT09P\r\nT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0//wAARCAAkACoDASEA\r\nAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA\r\nAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3\r\nODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm\r\np6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA\r\nAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx\r\nBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK\r\nU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3\r\nuLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDpWXAw\r\nenaq8gVepAq0SV32/wB4fnUDAdiDTGRlaNh9T+dSBvSDPTrjpTwba1toBdWJlnlUsVP8I/GmIYs+\r\nl4+bSnX6N/8AXqIrpV3J5NqlxBclSUBRsOR7nigZmKxkQMylTyCD2I60nFAjdhsIbq7Erq3mICUI\r\nJwD2JFTNbaurY/twrjpmyX5v1zUsaAwa4Rxq0eR62R5+tQuPEeQFvdJLg8eZE4/rQgMKK0uWv724\r\nu54PtJfE8MWdqnsR9RT/ACz6H8qoRd1bVptEEE8SQu8wYIkjY3Yx0OaxZviNerFgQWfmE4OA7bPc\r\n/wD66mwy4fHVoyIV1bD7RvH2QlQfbvUcXj2TczPp8NwycCVJNm4euG6UWGTaZcNq1tdauYxCLiXa\r\nEDZwB61YwaZJh/EuRo10srj5EcgdutS6c1hd2kHn6LphJRckQkE8A+tMZtxeF9AlSF20mAGTOQrO\r\nB/Os7xp4Z0bSfDVxc2NkscygBX3MSvPbJpAL4MHm+Cogxx++bOO/Irof7Ph/vP8AmP8ACmI//9k=",
                                        Feature     = "",
                                        CarType     = 1,
                                        CarBrand    = "brand" + 1,
                                        CarColor    = 1,
                                        PlateNumber = "京A0000" + 1,
                                        PlateType   = 1
                                    };
                                    bike = new VideoBike
                                    {
                                        DataId   = lane.DataId,
                                        DateTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, 0),
                                        Image    = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4p\r\nLSwzOko+MzZGNywtQFdBRkxOUlNSMj5aYVpQYEpRUk//2wBDAQ4ODhMREyYVFSZPNS01T09PT09P\r\nT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09P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aZJh/EuRo10srj5EcgdutS6c1hd2kHn6LphJRckQkE8A+tMZtxeF9AlSF20mAGTOQrO\r\nB/Os7xp4Z0bSfDVxc2NkscygBX3MSvPbJpAL4MHm+Cogxx++bOO/Irof7Ph/vP8AmP8ACmI//9k=",
                                        Feature  = "",
                                        BikeType = (int)NonVehicle.自行车
                                    };
                                    pedestrain = new VideoPedestrain
                                    {
                                        DataId     = lane.DataId,
                                        DateTime   = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, 0),
                                        Image      = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4p\r\nLSwzOko+MzZGNywtQFdBRkxOUlNSMj5aYVpQYEpRUk//2wBDAQ4ODhMREyYVFSZPNS01T09PT09P\r\nT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0//wAARCAAkACoDASEA\r\nAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA\r\nAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3\r\nODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm\r\np6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA\r\nAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx\r\nBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK\r\nU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3\r\nuLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDpWXAw\r\nenaq8gVepAq0SV32/wB4fnUDAdiDTGRlaNh9T+dSBvSDPTrjpTwba1toBdWJlnlUsVP8I/GmIYs+\r\nl4+bSnX6N/8AXqIrpV3J5NqlxBclSUBRsOR7nigZmKxkQMylTyCD2I60nFAjdhsIbq7Erq3mICUI\r\nJwD2JFTNbaurY/twrjpmyX5v1zUsaAwa4Rxq0eR62R5+tQuPEeQFvdJLg8eZE4/rQgMKK0uWv724\r\nu54PtJfE8MWdqnsR9RT/ACz6H8qoRd1bVptEEE8SQu8wYIkjY3Yx0OaxZviNerFgQWfmE4OA7bPc\r\n/wD66mwy4fHVoyIV1bD7RvH2QlQfbvUcXj2TczPp8NwycCVJNm4euG6UWGTaZcNq1tdauYxCLiXa\r\nEDZwB61YwaZJh/EuRo10srj5EcgdutS6c1hd2kHn6LphJRckQkE8A+tMZtxeF9AlSF20mAGTOQrO\r\nB/Os7xp4Z0bSfDVxc2NkscygBX3MSvPbJpAL4MHm+Cogxx++bOO/Irof7Ph/vP8AmP8ACmI//9k=",
                                        Feature    = "",
                                        Age        = (int)Age.儿童,
                                        Sex        = (int)Sex.女,
                                        UpperColor = (int)UpperColor.红
                                    };
                                }
                                else if (mode == DataCreateMode.Sequence)
                                {
                                    int temp = value;
                                    value       += 1;
                                    videoVehicle = new VideoVehicle
                                    {
                                        DataId      = lane.DataId,
                                        DateTime    = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, 0),
                                        Image       = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4p\r\nLSwzOko+MzZGNywtQFdBRkxOUlNSMj5aYVpQYEpRUk//2wBDAQ4ODhMREyYVFSZPNS01T09PT09P\r\nT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0//w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srj5EcgdutS6c1hd2kHn6LphJRckQkE8A+tMZtxeF9AlSF20mAGTOQrO\r\nB/Os7xp4Z0bSfDVxc2NkscygBX3MSvPbJpAL4MHm+Cogxx++bOO/Irof7Ph/vP8AmP8ACmI//9k=",
                                        Feature     = "",
                                        CarType     = 1,
                                        CarBrand    = "brand" + temp,
                                        CarColor    = 1,
                                        PlateNumber = "京A0000" + 1,
                                        PlateType   = 1
                                    };
                                    bike = new VideoBike
                                    {
                                        DataId   = lane.DataId,
                                        DateTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, 0),
                                        Image    = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4p\r\nLSwzOko+MzZGNywtQFdBRkxOUlNSMj5aYVpQYEpRUk//2wBDAQ4ODhMREyYVFSZPNS01T09PT09P\r\nT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0//wAARCAAkACoDASEA\r\nAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA\r\nAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3\r\nODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm\r\np6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA\r\nAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx\r\nBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK\r\nU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3\r\nuLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDpWXAw\r\nenaq8gVepAq0SV32/wB4fnUDAdiDTGRlaNh9T+dSBvSDPTrjpTwba1toBdWJlnlUsVP8I/GmIYs+\r\nl4+bSnX6N/8AXqIrpV3J5NqlxBclSUBRsOR7nigZmKxkQMylTyCD2I60nFAjdhsIbq7Erq3mICUI\r\nJwD2JFTNbaurY/twrjpmyX5v1zUsaAwa4Rxq0eR62R5+tQuPEeQFvdJLg8eZE4/rQgMKK0uWv724\r\nu54PtJfE8MWdqnsR9RT/ACz6H8qoRd1bVptEEE8SQu8wYIkjY3Yx0OaxZviNerFgQWfmE4OA7bPc\r\n/wD66mwy4fHVoyIV1bD7RvH2QlQfbvUcXj2TczPp8NwycCVJNm4euG6UWGTaZcNq1tdauYxCLiXa\r\nEDZwB61YwaZJh/EuRo10srj5EcgdutS6c1hd2kHn6LphJRckQkE8A+tMZtxeF9AlSF20mAGTOQrO\r\nB/Os7xp4Z0bSfDVxc2NkscygBX3MSvPbJpAL4MHm+Cogxx++bOO/Irof7Ph/vP8AmP8ACmI//9k=",
                                        Feature  = "",
                                        BikeType = (temp % 2 == 0 ? 2 : 3)
                                    };
                                    pedestrain = new VideoPedestrain
                                    {
                                        DataId     = lane.DataId,
                                        DateTime   = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, 0),
                                        Image      = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4p\r\nLSwzOko+MzZGNywtQFdBRkxOUlNSMj5aYVpQYEpRUk//2wBDAQ4ODhMREyYVFSZPNS01T09PT09P\r\nT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0//wAARCAAkACoDASEA\r\nAhEBAxEB/8QAHwAAAQUBAQE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lSF20mAGTOQrO\r\nB/Os7xp4Z0bSfDVxc2NkscygBX3MSvPbJpAL4MHm+Cogxx++bOO/Irof7Ph/vP8AmP8ACmI//9k=",
                                        Feature    = "",
                                        Age        = (int)Age.儿童,
                                        Sex        = (temp % 2 == 0 ? 1 : 2),
                                        UpperColor = (int)UpperColor.红
                                    };
                                }
                                else
                                {
                                    videoVehicle = new VideoVehicle
                                    {
                                        DataId      = lane.DataId,
                                        DateTime    = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, 0),
                                        Image       = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4p\r\nLSwzOko+MzZGNywtQFdBRkxOUlNSMj5aYVpQYEpRUk//2wBDAQ4ODhMREyYVFSZPNS01T09PT09P\r\nT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0//wAARCAAkACoDASEA\r\nAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA\r\nAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3\r\nODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm\r\np6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA\r\nAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx\r\nBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK\r\nU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3\r\nuLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDpWXAw\r\nenaq8gVepAq0SV32/wB4fnUDAdiDTGRlaNh9T+dSBvSDPTrjpTwba1toBdWJlnlUsVP8I/GmIYs+\r\nl4+bSnX6N/8AXqIrpV3J5NqlxBclSUBRsOR7nigZmKxkQMylTyCD2I60nFAjdhsIbq7Erq3mICUI\r\nJwD2JFTNbaurY/twrjpmyX5v1zUsaAwa4Rxq0eR62R5+tQuPEeQFvdJLg8eZE4/rQgMKK0uWv724\r\nu54PtJfE8MWdqnsR9RT/ACz6H8qoRd1bVptEEE8SQu8wYIkjY3Yx0OaxZviNerFgQWfmE4OA7bPc\r\n/wD66mwy4fHVoyIV1bD7RvH2QlQfbvUcXj2TczPp8NwycCVJNm4euG6UWGTaZcNq1tdauYxCLiXa\r\nEDZwB61YwaZJh/EuRo10srj5EcgdutS6c1hd2kHn6LphJRckQkE8A+tMZtxeF9AlSF20mAGTOQrO\r\nB/Os7xp4Z0bSfDVxc2NkscygBX3MSvPbJpAL4MHm+Cogxx++bOO/Irof7Ph/vP8AmP8ACmI//9k=",
                                        Feature     = "",
                                        CarType     = random.Next(1, 21),
                                        CarBrand    = "brand" + random.Next(1, 100),
                                        CarColor    = random.Next(1, 13),
                                        PlateNumber = "京A0000" + random.Next(1, 9),
                                        PlateType   = random.Next(1, 29)
                                    };
                                    bike = new VideoBike
                                    {
                                        DataId   = lane.DataId,
                                        DateTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, 0),
                                        Image    = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4p\r\nLSwzOko+MzZGNywtQFdBRkxOUlNSMj5aYVpQYEpRUk//2wBDAQ4ODhMREyYVFSZPNS01T09PT09P\r\nT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09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Jh/EuRo10srj5EcgdutS6c1hd2kHn6LphJRckQkE8A+tMZtxeF9AlSF20mAGTOQrO\r\nB/Os7xp4Z0bSfDVxc2NkscygBX3MSvPbJpAL4MHm+Cogxx++bOO/Irof7Ph/vP8AmP8ACmI//9k=",
                                        Feature  = "",
                                        BikeType = random.Next(2, 3)
                                    };
                                    pedestrain = new VideoPedestrain
                                    {
                                        DataId     = lane.DataId,
                                        DateTime   = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, 0),
                                        Image      = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4p\r\nLSwzOko+MzZGNywtQFdBRkxOUlNSMj5aYVpQYEpRUk//2wBDAQ4ODhMREyYVFSZPNS01T09PT09P\r\nT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0//wAARCAAkACoDASEA\r\nAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA\r\nAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3\r\nODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm\r\np6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA\r\nAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx\r\nBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK\r\nU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3\r\nuLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDpWXAw\r\nenaq8gVepAq0SV32/wB4fnUDAdiDTGRlaNh9T+dSBvSDPTrjpTwba1toBdWJlnlUsVP8I/GmIYs+\r\nl4+bSnX6N/8AXqIrpV3J5NqlxBclSUBRsOR7nigZmKxkQMylTyCD2I60nFAjdhsIbq7Erq3mICUI\r\nJwD2JFTNbaurY/twrjpmyX5v1zUsaAwa4Rxq0eR62R5+tQuPEeQFvdJLg8eZE4/rQgMKK0uWv724\r\nu54PtJfE8MWdqnsR9RT/ACz6H8qoRd1bVptEEE8SQu8wYIkjY3Yx0OaxZviNerFgQWfmE4OA7bPc\r\n/wD66mwy4fHVoyIV1bD7RvH2QlQfbvUcXj2TczPp8NwycCVJNm4euG6UWGTaZcNq1tdauYxCLiXa\r\nEDZwB61YwaZJh/EuRo10srj5EcgdutS6c1hd2kHn6LphJRckQkE8A+tMZtxeF9AlSF20mAGTOQrO\r\nB/Os7xp4Z0bSfDVxc2NkscygBX3MSvPbJpAL4MHm+Cogxx++bOO/Irof7Ph/vP8AmP8ACmI//9k=",
                                        Feature    = "",
                                        Age        = (int)Age.儿童,
                                        Sex        = (int)Sex.女,
                                        UpperColor = random.Next(1, 9)
                                    };
                                }
                                branch.Post(videoVehicle);
                                branch.Post(bike);
                                branch.Post(pedestrain);
                            }
                        }
                    }
                }
            }

            DateTime currentTime = TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel, DateTime.Now);

            branch.Close();
            if (currentTime != minTime)
            {
                using (FlowContext context = serviceProvider.GetRequiredService <FlowContext>())
                {
                    context.ChangeBikeTable(BranchDbConvert.GetTableName(minTime));
                    context.ChangePedestrainTable(BranchDbConvert.GetTableName(minTime));
                    context.ChangeVehicleTable(BranchDbConvert.GetTableName(minTime));
                }
            }
        }
Example #6
0
        /// <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)
                        {
                        }
                    }
                }
            }
        }