private void bgwCopier_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker SelfWorker = sender as BackgroundWorker; object[] bgwArgs = e.Argument as object[]; string fromPath = bgwArgs[0] as string; string concurrentDataFilesPath = bgwArgs[1] as string; string toPath = bgwArgs[2] as string; string imagesStatsXMLfilesDir = bgwArgs[3] as string; DirectoryInfo dir = new DirectoryInfo(fromPath); String destDirectory = toPath + ((toPath.Last() == Path.DirectorySeparatorChar) ? ("") : (Path.DirectorySeparatorChar.ToString())); if (!dir.Exists) { theLogWindow = ServiceTools.LogAText(theLogWindow, "Операция не выполнена. Не найдена директория:" + Environment.NewLine + fromPath + Environment.NewLine); //ThreadSafeOperations.SetTextTB(tbLog, "Операция не выполнена. Не найдена директория:" + Environment.NewLine + fromPath + Environment.NewLine, true); return; } FileInfo[] FileList2Process = dir.GetFiles("*.jpg", SearchOption.AllDirectories); List <Tuple <string, string> > imagesStatsXMLfiles = new List <Tuple <string, string> >(); if (Directory.Exists(imagesStatsXMLfilesDir)) { imagesStatsXMLfiles = (new DirectoryInfo(imagesStatsXMLfilesDir)).EnumerateFiles( ConventionalTransitions.ImageGrIxYRGBstatsFileNamesPattern(), SearchOption.AllDirectories) .ToList() .ConvertAll(fInfo => new Tuple <string, string>(fInfo.Name, fInfo.FullName)); } DirectoryInfo dirConcurrentDataFiles = new DirectoryInfo(concurrentDataFilesPath); List <Tuple <string, DateTime> > lConcurrentDataFiles = dirConcurrentDataFiles.EnumerateFiles(ConventionalTransitions.ImageConcurrentDataFilesNamesPattern(), SearchOption.AllDirectories).ToList().ConvertAll(fInfo => { // data-2015-12-15T06-12-56.0590302Z.xml string strDateTimeOfFile = Path.GetFileNameWithoutExtension(fInfo.Name).Substring(5, 28); strDateTimeOfFile = strDateTimeOfFile.Substring(0, 11) + strDateTimeOfFile.Substring(11).Replace('-', ':'); DateTime currFileDT = DateTime.Parse(strDateTimeOfFile, null, System.Globalization.DateTimeStyles.AdjustToUniversal); currFileDT = DateTime.SpecifyKind(currFileDT, DateTimeKind.Utc); return(new Tuple <string, DateTime>(fInfo.FullName, currFileDT)); }); int filesCount = FileList2Process.Length; theLogWindow = ServiceTools.LogAText(theLogWindow, "searching in directory: " + dir.FullName + Environment.NewLine); //ThreadSafeOperations.SetTextTB(tbLog, "searching in directory: " + dir.FullName + Environment.NewLine, true); theLogWindow = ServiceTools.LogAText(theLogWindow, "files found count: " + filesCount + Environment.NewLine); //ThreadSafeOperations.SetTextTB(tbLog, "files found count: " + filesCount + Environment.NewLine, true); String usedDateTimes = ""; List <DateTime> listUsedHours = new List <DateTime>(); int counter = 0; foreach (FileInfo fileInfo in FileList2Process) { if (SelfWorker.CancellationPending) { break; } counter++; double percCounter = (double)counter * 1000.0d / (double)filesCount; SelfWorker.ReportProgress(Convert.ToInt32(percCounter)); Image anImage = Image.FromFile(fileInfo.FullName); ImageInfo newIInfo = new ImageInfo(anImage); //ThreadSafeOperations.SetTextTB(tbLog, "processing file " + fileInfo.Name + Environment.NewLine, true); //String curDateTime = ""; int minute = 0; //String dateTime = (String)newIInfo.getValueByKey("DateTime"); String strDateTimeEXIF = (String)newIInfo.getValueByKey("ExifDTOrig"); if (strDateTimeEXIF == null) { //попробуем вытащить из имени файла string strDateTime = fileInfo.Name; strDateTime = strDateTime.Substring(4, 19); strDateTimeEXIF = strDateTime; } //curDateTime = dateTime; DateTime curImgDateTime; DateTime theHour = RoundToHour(DateTime.UtcNow); try { //curImgDateTime = DateTimeOfString(strDateTimeEXIF); curImgDateTime = ConventionalTransitions.DateTimeOfSkyImageFilename(fileInfo.Name); theHour = RoundToHour(curImgDateTime); } catch (Exception ex) { continue; } //minute = Convert.ToInt32(strDateTimeEXIF.Substring(14, 2)); //if ((minute == 0) && (!listUsedHours.Contains(theHour))) if (new TimeSpan(Math.Abs((theHour - curImgDateTime).Ticks)) <= filterTolerance) { #region copy the image file itself listUsedHours.Add(theHour); string dateDirectorySuffix = curImgDateTime.ToString("yyyy-MM-dd"); string currDateDestDirectory = destDirectory + dateDirectorySuffix + Path.DirectorySeparatorChar; if (!ServiceTools.CheckIfDirectoryExists(currDateDestDirectory)) { currDateDestDirectory = destDirectory; } String newFileName = currDateDestDirectory + fileInfo.Name; File.Copy(fileInfo.FullName, newFileName); theLogWindow = ServiceTools.LogAText(theLogWindow, "COPY: " + fileInfo.FullName + " >>> " + newFileName + Environment.NewLine); //ThreadSafeOperations.SetTextTB(tbLog, "COPY: " + fileInfo.FullName + " >>> " + newFileName + Environment.NewLine, true); #endregion copy the image file itself #region find and copy the GrIx,YRGB stats data file if (imagesStatsXMLfiles.Any()) { string xmlStatsFileName = ConventionalTransitions.ImageGrIxYRGBstatsDataFileName(fileInfo.FullName, "", false); Tuple <string, string> foundXMLfile = imagesStatsXMLfiles.Find(tpl => tpl.Item1 == xmlStatsFileName); if (foundXMLfile != null) { string sStatsXMLfilename = foundXMLfile.Item2; string newStatsXMLfilename = currDateDestDirectory + foundXMLfile.Item1; File.Copy(sStatsXMLfilename, newStatsXMLfilename); theLogWindow = ServiceTools.LogAText(theLogWindow, "COPY: " + sStatsXMLfilename + " >>> " + newStatsXMLfilename + Environment.NewLine); //ThreadSafeOperations.SetTextTB(tbLog, // "COPY: " + sStatsXMLfilename + " >>> " + newStatsXMLfilename + Environment.NewLine, // true); } else { theLogWindow = ServiceTools.LogAText(theLogWindow, "========== ERROR: couldn`t find GrIx,YRGB stats XML file" + Environment.NewLine); //ThreadSafeOperations.SetTextTB(tbLog, // "========== ERROR: couldn`t find GrIx,YRGB stats XML file" + Environment.NewLine, true); } } #endregion find and copy the GrIx,YRGB stats data file #region find and copy concurrent data XML file if (lConcurrentDataFiles.Any()) { //найдем ближайший по времени List <Tuple <string, TimeSpan> > lCurrFileConcurrentDataNearest = lConcurrentDataFiles.ConvertAll( tpl => new Tuple <string, TimeSpan>(tpl.Item1, new TimeSpan(Math.Abs((tpl.Item2 - curImgDateTime).Ticks)))); lCurrFileConcurrentDataNearest.Sort(new Comparison <Tuple <string, TimeSpan> >((tpl1, tpl2) => { if (tpl1 == null) { if (tpl2 == null) { return(0); } else { return(-1); } } else { if (tpl2 == null) { return(1); } else { return(tpl1.Item2.CompareTo(tpl2.Item2)); } } })); GPSdata gps = new GPSdata(); Tuple <string, TimeSpan> nearestConcurrentDataFile = null; int concurrentDataFileIdx = 0; while (!gps.validGPSdata) { nearestConcurrentDataFile = lCurrFileConcurrentDataNearest[concurrentDataFileIdx]; Dictionary <string, object> dictSavedData = ServiceTools.ReadDictionaryFromXML(nearestConcurrentDataFile.Item1); gps = new GPSdata((string)dictSavedData["GPSdata"], GPSdatasources.CloudCamArduinoGPS, DateTime.Parse((string)dictSavedData["GPSDateTimeUTC"], null, System.Globalization.DateTimeStyles.RoundtripKind)); concurrentDataFileIdx++; } string currValidConcurrentDataFile = nearestConcurrentDataFile.Item1; string currValidConcurrentDataFileToCopyTo = currDateDestDirectory + "data-" + Path.GetFileNameWithoutExtension(fileInfo.FullName) + ".xml"; File.Copy(currValidConcurrentDataFile, currValidConcurrentDataFileToCopyTo); theLogWindow = ServiceTools.LogAText(theLogWindow, "COPY: " + currValidConcurrentDataFile + " >>> " + currValidConcurrentDataFileToCopyTo + Environment.NewLine); //ThreadSafeOperations.SetTextTB(tbLog, // "COPY: " + currValidConcurrentDataFile + " >>> " + currValidConcurrentDataFileToCopyTo + // Environment.NewLine, true); } else { theLogWindow = ServiceTools.LogAText(theLogWindow, "========== ERROR: couldn`t find concurrent data file for " + fileInfo.FullName + Environment.NewLine); //ThreadSafeOperations.SetTextTB(tbLog, "========== ERROR: couldn`t find concurrent data file for " + fileInfo.FullName + Environment.NewLine, true); } #endregion find and copy concurrent data XML file theLogWindow.ClearLog(); } } }