public static IoffeVesselDualNavDataConverted GetNavDataByDatetime(string navDataFilesPath, DateTime dt) { if (navDataFilesPath == "") { return(null); } else if (!Directory.Exists(navDataFilesPath)) { return(null); } TimeSpan span = new TimeSpan(0, 2, 0); List <IoffeVesselDualNavDataConverted> lAllNavData = new List <IoffeVesselDualNavDataConverted>(); string[] sNavFilenames = Directory.GetFiles(navDataFilesPath, "*.nv2, *.nav", SearchOption.AllDirectories); if (!sNavFilenames.Any()) { return(null); } foreach (string navFilename in sNavFilenames) { Tuple <DateTime, DateTime> timeSpan = GetNavFileDateTimeMargins(navFilename); if (timeSpan == null) { continue; } if ((dt < timeSpan.Item1) || (dt > timeSpan.Item2)) { continue; } List <IoffeVesselDualNavDataConverted> dataHasBeenRead = ReadNavFile(navFilename); if (dataHasBeenRead == null) { continue; } lAllNavData.AddRange(dataHasBeenRead); } if (!lAllNavData.Any()) { return(null); } lAllNavData.Sort((gpsRecord1, gpsRecord2) => { double dev1 = Math.Abs((gpsRecord1.gps.dateTimeUTC - dt).TotalMilliseconds); double dev2 = Math.Abs((gpsRecord2.gps.dateTimeUTC - dt).TotalMilliseconds); return((dev1 >= dev2) ? (1) : (-1)); }); IoffeVesselDualNavDataConverted retData = new IoffeVesselDualNavDataConverted() { dateTime = lAllNavData[0].dateTime, gps = lAllNavData[0].gps, }; return(retData); }
public static List <IoffeVesselDualNavDataConverted> ReadNavFile(string InputFilename) //, bool readOnlyThe1stRecord = false) { int bytesCountPerObj = Marshal.SizeOf(typeof(IoffeVesselDualNavData)); if ((new FileInfo(InputFilename)).Length < (4 + bytesCountPerObj)) { return(null); } BinaryReader reader = new BinaryReader(File.Open(InputFilename, FileMode.Open, FileAccess.Read, FileShare.Read)); char[] dualmode = reader.ReadChars(4); if (new string(dualmode) != "Dual") { // неправильный файл навигации - данные не в том формате return(null); } reader.Close(); byte[] fileData = File.ReadAllBytes(InputFilename); reader = new BinaryReader(new MemoryStream(fileData)); reader.ReadChars(4); List <IoffeVesselDualNavData> lFileNavData = new List <IoffeVesselDualNavData>(); int structCount = Convert.ToInt32((fileData.Count() - 4) / bytesCountPerObj); if (structCount == 0) { return(null); } int readStructs = 0; while (true) { //Stream stream = reader.BaseStream; long currPosition = reader.BaseStream.Position; try { //char[] possibleDualModeSign = reader.ReadChars(4); byte[] read4bytes = reader.ReadBytes(4); string possibleDualModeSign = System.Text.Encoding.Default.GetString(read4bytes); if (possibleDualModeSign.ToLower() != "dual") { // вернуть позицию на 4 назад //reader.BaseStream.Seek(-4, SeekOrigin.Current); reader.BaseStream.Seek(currPosition, SeekOrigin.Begin); } } catch (Exception ex) { ; } byte[] readBuffer = new byte[bytesCountPerObj]; readBuffer = reader.ReadBytes(bytesCountPerObj); GCHandle handle = GCHandle.Alloc(readBuffer, GCHandleType.Pinned); IoffeVesselDualNavData curRecord = (IoffeVesselDualNavData) Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(IoffeVesselDualNavData)); readStructs++; lFileNavData.Add(curRecord); //if ((readStructs >= structCount) || (readOnlyThe1stRecord)) if (readStructs >= structCount) { break; } } List <IoffeVesselDualNavDataConverted> lRetList = lFileNavData.ConvertAll(navData => { IoffeVesselDualNavDataConverted retNavData = new IoffeVesselDualNavDataConverted(); #region // -> ExtractDateTime() //string dateStr = navData.DateGPS.ToString(); //string timeStr = navData.curtime.ToString("D4"); //DateTime dt; //if (navData.curtime >= 2400) //{ // int daysToAdd = 0; // while (navData.curtime >= 2400) // { // navData.curtime -= 2400; // daysToAdd ++; // } // timeStr = (navData.curtime).ToString("D4"); // dt = new DateTime(2000 + Convert.ToInt32(dateStr.Substring(dateStr.Length - 2, 2)), // Convert.ToInt32(dateStr.Substring(dateStr.Length - 4, 2)), // Convert.ToInt32(dateStr.Substring(0, dateStr.Length - 4)), // Convert.ToInt32(timeStr.Substring(0, 2)), // Convert.ToInt32(timeStr.Substring(2, 2)), // 0); // dt = dt.AddDays(daysToAdd); //} //else //{ // dt = new DateTime(2000 + Convert.ToInt32(dateStr.Substring(dateStr.Length - 2, 2)), // Convert.ToInt32(dateStr.Substring(dateStr.Length - 4, 2)), // Convert.ToInt32(dateStr.Substring(0, dateStr.Length - 4)), // Convert.ToInt32(timeStr.Substring(0, 2)), // Convert.ToInt32(timeStr.Substring(2, 2)), // 0); //} #endregion // -> ExtractDateTime() retNavData.dateTime = ExtractDateTime(navData); GPSdata curGPS = new GPSdata() { GPSstring = "", lat = (double)navData.Lat, latHemisphere = "" + navData.SimLat, lon = (double)navData.Lon, lonHemisphere = "" + navData.SimLon, dateTimeUTC = retNavData.dateTime, validGPSdata = true, IOFFEdataHeadingTrue = navData.trueHead, IOFFEdataHeadingGyro = navData.Gyro, IOFFEdataSpeedKnots = navData.Speed, IOFFEdataDepth = navData.Depth, dataSource = GPSdatasources.IOFFEvesselDataServer }; retNavData.gps = curGPS; retNavData.Course = navData.Course; return(retNavData); }); return(lRetList); }