protected override void Handle(VideoPedestrain[] datas) { using (IServiceScope serviceScope = _serviceProvider.CreateScope()) { using (FlowContext context = serviceScope.ServiceProvider.GetRequiredService <FlowContext>()) { try { context.Pedestrains.AddRange(datas); context.BulkSaveChanges(options => options.BatchSize = datas.Length); Success += datas.Length; } catch (Exception ex) { _logger.LogError((int)LogEvent.视频数据块, ex, "行人数据入库异常"); Failed += datas.Length; } } } }
protected override void Handle(VideoVehicle[] datas) { _logger.LogDebug((int)LogEvent.视频数据块, "开始保存视频结构化数据"); using (IServiceScope serviceScope = _serviceProvider.CreateScope()) { using (FlowContext context = serviceScope.ServiceProvider.GetRequiredService <FlowContext>()) { try { context.Vehicles.AddRange(datas); context.BulkSaveChanges(options => options.BatchSize = datas.Length); Success += datas.Length; } catch (Exception ex) { _logger.LogError((int)LogEvent.视频数据块, ex, "机动车数据入库异常"); Failed += datas.Length; } } } _logger.LogDebug((int)LogEvent.视频数据块, "保存视频结构化数据完成"); }
/// <summary> /// 保存路段小时状态 /// </summary> /// <param name="time">当前计算时间</param> private void SaveSectionHourStatus(DateTime time) { if (time.Hour != _currentHour) { _logger.LogInformation((int)LogEvent.路段状态, $"保存路段小时状态 {_currentHour}"); using (IServiceScope serviceScope = _serviceProvider.CreateScope()) { using (FlowContext context = serviceScope.ServiceProvider.GetRequiredService <FlowContext>()) { try { context.SectionStatuses.AddRange(_sectionStatuses.Values); context.BulkSaveChanges(options => options.BatchSize = _sectionStatuses.Count); } catch (Exception ex) { _logger.LogError((int)LogEvent.路段状态, ex, "路段小时状态存储异常"); } } } _sectionStatuses.Clear(); _currentHour = time.Hour; } }
protected override void Handle(LaneFlow[] datas) { using (IServiceScope serviceScope = _serviceProvider.CreateScope()) { using (FlowContext context = serviceScope.ServiceProvider.GetRequiredService <FlowContext>()) { if (datas[0].DateLevel == DateTimeLevel.Minute) { try { context.LaneFlows_One.AddRange(datas.Select(data => new LaneFlow_One { DataId = data.DataId, DateTime = data.DateTime, Cars = data.Cars, Buss = data.Buss, Vans = data.Vans, Tricycles = data.Tricycles, Trucks = data.Trucks, Motorcycles = data.Motorcycles, Bikes = data.Bikes, Persons = data.Persons, TravelTime = data.TravelTime, Distance = data.Distance, HeadDistance = data.HeadDistance, Occupancy = data.Occupancy, TimeOccupancy = data.TimeOccupancy, Count = data.Count })); context.BulkSaveChanges(options => options.BatchSize = datas.Length); Success_One += datas.Length; } catch (Exception ex) { _logger.LogError((int)LogEvent.流量数据块, ex, "一分钟流量数据存储异常"); Failed_One += datas.Length; } } else if (datas[0].DateLevel == DateTimeLevel.FiveMinutes) { try { context.LaneFlows_Five.AddRange(datas.Select(data => new LaneFlow_Five { DataId = data.DataId, DateTime = data.DateTime, Cars = data.Cars, Buss = data.Buss, Vans = data.Vans, Tricycles = data.Tricycles, Trucks = data.Trucks, Motorcycles = data.Motorcycles, Bikes = data.Bikes, Persons = data.Persons, TravelTime = data.TravelTime, Distance = data.Distance, HeadDistance = data.HeadDistance, Occupancy = data.Occupancy, TimeOccupancy = data.TimeOccupancy, Count = data.Count })); context.BulkSaveChanges(options => options.BatchSize = datas.Length); Success_Five += datas.Length; } catch (Exception ex) { _logger.LogError((int)LogEvent.流量数据块, ex, "五分钟流量数据存储异常"); Failed_Five += datas.Length; } } else if (datas[0].DateLevel == DateTimeLevel.FifteenMinutes) { try { context.LaneFlows_Fifteen.AddRange(datas.Select(data => new LaneFlow_Fifteen { DataId = data.DataId, DateTime = data.DateTime, Cars = data.Cars, Buss = data.Buss, Vans = data.Vans, Tricycles = data.Tricycles, Trucks = data.Trucks, Motorcycles = data.Motorcycles, Bikes = data.Bikes, Persons = data.Persons, TravelTime = data.TravelTime, Distance = data.Distance, HeadDistance = data.HeadDistance, Occupancy = data.Occupancy, TimeOccupancy = data.TimeOccupancy, Count = data.Count })); context.BulkSaveChanges(options => options.BatchSize = datas.Length); Success_Fifteen += datas.Length; } catch (Exception ex) { _logger.LogError((int)LogEvent.流量数据块, ex, "十五分钟流量数据存储异常"); Failed_Fifteen += datas.Length; } } else if (datas[0].DateLevel == DateTimeLevel.Hour) { try { context.LaneFlows_Hour.AddRange(datas.Select(data => new LaneFlow_Hour { DataId = data.DataId, DateTime = data.DateTime, Cars = data.Cars, Buss = data.Buss, Vans = data.Vans, Tricycles = data.Tricycles, Trucks = data.Trucks, Motorcycles = data.Motorcycles, Bikes = data.Bikes, Persons = data.Persons, TravelTime = data.TravelTime, Distance = data.Distance, HeadDistance = data.HeadDistance, Occupancy = data.Occupancy, TimeOccupancy = data.TimeOccupancy, Count = data.Count })); context.BulkSaveChanges(options => options.BatchSize = datas.Length); Success_Sixty += datas.Length; } catch (Exception ex) { _logger.LogError((int)LogEvent.流量数据块, ex, "一小时流量数据存储异常"); Failed_Sixty += datas.Length; } } } } }
public static void CreateData(IServiceProvider serviceProvider, List <FlowDevice> devices, DateTime startDate, int minutes) { using (FlowContext context = serviceProvider.CreateScope().ServiceProvider.GetRequiredService <FlowContext>()) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); } for (int m = 0; m < minutes; ++m) { DateTime dataTime = startDate.AddMinutes(m); using (FlowContext context = serviceProvider.CreateScope().ServiceProvider.GetRequiredService <FlowContext>()) { foreach (FlowDevice device in devices) { foreach (var relation in device.FlowDevice_FlowChannels) { foreach (Lane lane in relation.Channel.Lanes) { LaneFlow_One laneFlow = new LaneFlow_One { 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 }; context.LaneFlows_One.Add(laneFlow); if ((m + 1) % 5 == 0) { LaneFlow_Five laneFlow5 = new LaneFlow_Five { DataId = lane.DataId, DateTime = new DateTime(dataTime.Year, dataTime.Month, dataTime.Day, dataTime.Hour, dataTime.Minute, 0), Cars = 5, Buss = 5, Trucks = 5, Vans = 5, Tricycles = 5, Motorcycles = 5, Bikes = 5, Persons = 5, AverageSpeedData = 50, HeadDistance = 10, Occupancy = 30, TimeOccupancy = 40, TrafficStatus = TrafficStatus.轻度拥堵, Count = 5 }; context.LaneFlows_Five.Add(laneFlow5); } if ((m + 1) % 15 == 0) { LaneFlow_Fifteen laneFlow15 = new LaneFlow_Fifteen { DataId = lane.DataId, DateTime = new DateTime(dataTime.Year, dataTime.Month, dataTime.Day, dataTime.Hour, dataTime.Minute, 0), Cars = 15, Buss = 15, Trucks = 15, Vans = 15, Tricycles = 15, Motorcycles = 15, Bikes = 15, Persons = 15, AverageSpeedData = 50, HeadDistance = 10, Occupancy = 30, TimeOccupancy = 40, TrafficStatus = TrafficStatus.轻度拥堵, Count = 15 }; context.LaneFlows_Fifteen.Add(laneFlow15); } if ((m + 1) % 60 == 0) { LaneFlow_Hour laneFlow60 = new LaneFlow_Hour { DataId = lane.DataId, DateTime = new DateTime(dataTime.Year, dataTime.Month, dataTime.Day, dataTime.Hour, 0, 0), Cars = 60, Buss = 60, Trucks = 60, Vans = 60, Tricycles = 60, Motorcycles = 60, Bikes = 60, Persons = 60, AverageSpeedData = 50, HeadDistance = 10, Occupancy = 30, TimeOccupancy = 40, TrafficStatus = TrafficStatus.轻度拥堵, Count = 60 }; context.LaneFlows_Hour.Add(laneFlow60); } } } } context.BulkSaveChanges(options => options.BatchSize = 1000); } LogPool.Logger.LogInformation($"{dataTime:yyyy-MM-dd HH:mm:ss}"); } }