Пример #1
0
        public FileDatetimeInfo(string _filename, ServiceTools.DatetimeExtractionMethod method = ServiceTools.DatetimeExtractionMethod.Filename,
                                string _datetimeFilenameMask = "?????xxxxxxxxxxxxxxxxxxx*")
        {
            filename = _filename;
            datetimeExtractionMethod = method;
            datetimeFilenameMask     = _datetimeFilenameMask;

            switch (method)
            {
            case ServiceTools.DatetimeExtractionMethod.Filename:
            {
                try
                {
                    string strDateOfFile =
                        String.Concat(
                            Path.GetFileName(filename)
                            .Zip(datetimeFilenameMask, (c, s) => (s == 'x') ? (c.ToString()) : (""))
                            .ToArray());
                    datetime = CommonTools.DateTimeOfString(strDateOfFile);
                    DateTime.SpecifyKind(datetime, DateTimeKind.Utc);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                break;
            }

            case ServiceTools.DatetimeExtractionMethod.FileCreation:
            {
                datetime = new FileInfo(filename).CreationTimeUtc;
                DateTime.SpecifyKind(datetime, DateTimeKind.Utc);
                break;
            }

            case ServiceTools.DatetimeExtractionMethod.Content:
            {
                Dictionary <string, object> currDict = ServiceTools.ReadDictionaryFromXML(filename);
                currDict.Add("XMLfileName", Path.GetFileName(filename));
                ConcurrentData retVal = null;
                try
                {
                    retVal   = new ConcurrentData(currDict);
                    datetime = retVal.datetimeUTC;
                    DateTime.SpecifyKind(datetime, DateTimeKind.Utc);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                break;
            }

            default:
            {
                break;
            }
            }
        }
        public static string FindConcurrentDataXMLfile(string ImageFilename, string basePath        = "",
                                                       bool searchFilesRecursively                  = true,
                                                       ServiceTools.DatetimeExtractionMethod method = ServiceTools.DatetimeExtractionMethod.Filename)
        {
            if (basePath == "")
            {
                basePath = Directory.GetCurrentDirectory();
                basePath = basePath +
                           ((basePath.Last() == Path.DirectorySeparatorChar)
                               ? ("")
                               : (Path.DirectorySeparatorChar.ToString()));
                basePath += "results" + Path.DirectorySeparatorChar;
            }


            string nearestConcurrentDataXMLfilename = "";



            string currImgFilename = ImageFilename;

            ServiceTools.DatetimeExtractionMethod _method = method;

            #region find most close concurrent data XML file

            List <FileDatetimeInfo> lImagesConcurrentDataFilesInfo = new List <FileDatetimeInfo>();

            List <string> filesListConcurrentData =
                Directory.EnumerateFiles(basePath,
                                         ConventionalTransitions.ImageConcurrentDataFilesNamesPattern(),
                                         (searchFilesRecursively) ? (SearchOption.AllDirectories) : (SearchOption.TopDirectoryOnly))
                .ToList();


            foreach (string strConcDataXMLFile in filesListConcurrentData)
            {
                try
                {
                    lImagesConcurrentDataFilesInfo.Add(new FileDatetimeInfo(strConcDataXMLFile,
                                                                            _method));
                }
                catch (Exception ex)
                {
                    continue;
                }
            }


            string currImgFilenameWOext = Path.GetFileNameWithoutExtension(currImgFilename);

            string ptrn = @"(devID\d)";
            Regex  rgxp = new Regex(ptrn, RegexOptions.IgnoreCase);

            string strCurrImgDT = rgxp.Replace(currImgFilenameWOext.Substring(4), "");
            //2015-12-16T06-01-38
            strCurrImgDT = strCurrImgDT.Substring(0, 11) + strCurrImgDT.Substring(11).Replace("-", ":");

            DateTime currImgDT = DateTime.Parse(strCurrImgDT, null,
                                                DateTimeStyles.AdjustToUniversal);
            DateTime.SpecifyKind(currImgDT, DateTimeKind.Utc);

            FileDatetimeInfo nearestConcurrentDataXMLfileInfo = lImagesConcurrentDataFilesInfo.Aggregate((cDtFinfo1, cDtFinfo2) =>
            {
                TimeSpan tspan1 = new TimeSpan(Math.Abs((cDtFinfo1.datetime - currImgDT).Ticks));
                TimeSpan tspan2 = new TimeSpan(Math.Abs((cDtFinfo2.datetime - currImgDT).Ticks));
                return((tspan1 <= tspan2) ? (cDtFinfo1) : (cDtFinfo2));
            });


            if (new TimeSpan(Math.Abs((nearestConcurrentDataXMLfileInfo.datetime - currImgDT).Ticks)) >=
                new TimeSpan(0, 3, 0))
            {
                //theLogWindow = ServiceTools.LogAText(theLogWindow,
                //    "couldn`t find close enough concurrent data file for image:" + Environment.NewLine +
                //    currImgFilename + Environment.NewLine + "closest concurrent data file is:" +
                //    Environment.NewLine + nearestConcurrentDataXMLfileInfo.filename + Environment.NewLine +
                //    "with date-time value " + nearestConcurrentDataXMLfileInfo.datetime.ToString("o"));
                nearestConcurrentDataXMLfilename = "";
            }
            else
            {
                nearestConcurrentDataXMLfilename = nearestConcurrentDataXMLfileInfo.filename;
            }


            #endregion find most close concurrent data


            return(nearestConcurrentDataXMLfilename);
        }