Beispiel #1
0
 protected override void ChangeDatabase(IServiceProvider serviceProvider, string tableName)
 {
     using (FlowContext context = serviceProvider.GetRequiredService <FlowContext>())
     {
         try
         {
             context.ChangeDatabase(tableName);
             _logger.LogInformation((int)LogEvent.分支切换, $"切换流量数据表成功 {tableName}");
         }
         catch (MySqlException ex)
         {
             _logger.LogError((int)LogEvent.分支切换, ex, "切换流量数据表失败");
         }
     }
 }
Beispiel #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)
                        {
                        }
                    }
                }
            }
        }
Beispiel #3
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);
        }