public void loadVhHistoricalInfo() { string[] file_rows_data = File.ReadAllLines(@"C:\LogFiles\OHxC\VehicleHistoricalInfo.log", System.Text.Encoding.Default); BlockingCollection <Vo.VehicleHistoricalInfo> listTemp = new BlockingCollection <Vo.VehicleHistoricalInfo>(); Parallel.For(0, file_rows_data.Count(), rowCount => { Vo.VehicleHistoricalInfo Historyinfo = new Vo.VehicleHistoricalInfo(); MatchCollection matches = Regex.Matches(file_rows_data[rowCount], pattern); string sTime = matches.Count > 0 ? matches[0].Value.Replace("[", "").Replace("]", "") : string.Empty; string sVh_ID = matches.Count > 1 ? matches[1].Value.Replace("[", "").Replace("]", "") : string.Empty; string sRaw_data = matches.Count > 2 ? matches[2].Value.Replace("[", "").Replace("]", "") : string.Empty; DateTime parseDateTime = default(DateTime); if (!DateTime.TryParseExact (sTime, sc.App.SCAppConstants.DateTimeFormat_23, CultureInfo.InvariantCulture, DateTimeStyles.None, out parseDateTime)) { logger.Warn($"{nameof(sTime)} parse fail. value{sTime}"); } byte[] vh_info_bytes = Common.WinFromUtility.unCompressString(sRaw_data); sc.ProtocolFormat.OHTMessage.VEHICLE_INFO vh_info = sc.BLL.VehicleBLL.Convert2Object_VehicleInfo(vh_info_bytes); Historyinfo.Time = parseDateTime; Historyinfo.ID = sVh_ID; Historyinfo.VhInfo = vh_info; listTemp.Add(Historyinfo); // 每處理幾筆就停止5ms if (rowCount != 0 && rowCount % 20000 == 0) { System.Threading.SpinWait.SpinUntil(() => false, 5); } }); vh_historical_infos = listTemp.OrderBy(info => info.Time).ToList(); StartPlayTime = vh_historical_infos.First().Time; var groupResult = from vh_historical_info in vh_historical_infos group vh_historical_info by vh_historical_info.Time; Vh_Historical_Info_GroupByTime = groupResult.OrderBy(infos => infos.Key).ToDictionary (infos => infos.Key, infos => infos.ToList()); currentPlayIndex = 0; //foreach (var info in vh_historical_infos) //{ // Console.WriteLine(info.VhInfo.ToString()); //} }
public void loadVhHistoricalInfo(DateTime start_time, DateTime end_time) { var node = new Uri($"http://{Common.ElasticSearchManager.ELASTIC_URL}:9200"); // var node = new Uri("http://192.168.9.211:9200"); var settings = new ConnectionSettings(node).DefaultIndex("default"); settings.DisableDirectStreaming(); var client = new ElasticClient(settings); //SearchRequest sr = new SearchRequest("ohtc-test-objecthistoricalinfo*"); SearchRequest sr = new SearchRequest("mfoht100-ohtc1-objecthistoricalinfo*"); DateRangeQuery dq = new DateRangeQuery { Field = "@timestamp", GreaterThan = start_time, LessThan = end_time, }; //TermsQuery tsq = new TermsQuery //{ //}; int startIndex = 0; int eachSearchSize = 9999; List <ObjectHistricalInfo> object_infos = new List <ObjectHistricalInfo>(); do { sr.From = startIndex; sr.Size = eachSearchSize; dq = new DateRangeQuery { Field = "@timestamp", GreaterThan = start_time, LessThan = end_time, }; sr.Query = dq; sr.Source = new SourceFilter() { Includes = new string[] { "@timestamp", "OBJECT_ID", "RAWDATA" }, }; var result = client.Search <ObjectHistricalInfo>(sr); if (result.Documents.Count == 0) { break; } var result_info = result.Documents.OrderBy(info => info.timestamp); object_infos.AddRange(result_info.ToList()); // if (object_infos.Count >= result.Total) break; //startIndex += eachSearchSize; if (result.Documents.Count < eachSearchSize) { break; } start_time = result_info.Last().timestamp.AddMilliseconds(1); }while (true); if (object_infos.Count == 0) { return; } BlockingCollection <Vo.VehicleHistoricalInfo> listTemp = new BlockingCollection <Vo.VehicleHistoricalInfo>(); Parallel.For(0, object_infos.Count(), rowCount => { Vo.VehicleHistoricalInfo Historyinfo = new Vo.VehicleHistoricalInfo(); DateTime Time = object_infos[rowCount].timestamp; string sVh_ID = object_infos[rowCount].OBJECT_ID; string sRaw_data = object_infos[rowCount].RAWDATA; byte[] vh_info_bytes = Common.WinFromUtility.unCompressString(sRaw_data); sc.ProtocolFormat.OHTMessage.VEHICLE_INFO vh_info = sc.BLL.VehicleBLL.Convert2Object_VehicleInfo(vh_info_bytes); Historyinfo.Time = Time.ToLocalTime(); Historyinfo.ID = sVh_ID; Historyinfo.VhInfo = vh_info; listTemp.Add(Historyinfo); // 每處理幾筆就停止5ms if (rowCount != 0 && rowCount % 20000 == 0) { System.Threading.SpinWait.SpinUntil(() => false, 5); } }); vh_historical_infos = listTemp.OrderBy(info => info.Time).ToList(); StartPlayTime = vh_historical_infos.First().Time; var groupResult = from vh_historical_info in vh_historical_infos group vh_historical_info by vh_historical_info.Time; Vh_Historical_Info_GroupByTime = groupResult.OrderBy(infos => infos.Key).ToDictionary (infos => infos.Key, infos => infos.ToList()); startIndex = 0; LoadComplete?.Invoke(this, Vh_Historical_Info_GroupByTime); //foreach (var info in vh_historical_infos) //{ // Console.WriteLine(info.VhInfo.ToString()); //} }