DataFileManager.cs 23.1 KB
Newer Older
cdy816's avatar
cdy816 已提交
1 2 3 4 5 6 7 8 9
//==============================================================
//  Copyright (C) 2019  Inc. All rights reserved.
//
//==============================================================
//  Create by 种道洋 at 2019/12/27 18:45:02.
//  Version 1.0
//  种道洋
//==============================================================
using System;
cdy816's avatar
cdy816 已提交
10
using System.Collections.Generic;
cdy816's avatar
cdy816 已提交
11
using System.IO;
cdy816's avatar
cdy816 已提交
12
using System.Linq;
cdy816's avatar
cdy816 已提交
13
using System.Text;
cdy816's avatar
cdy816 已提交
14
using System.Threading;
15
using System.Threading.Tasks;
cdy816's avatar
cdy816 已提交
16 17 18 19 20 21 22 23 24 25 26

namespace Cdy.Tag
{
    /// <summary>
    /// 
    /// </summary>
    public class DataFileManager
    {

        #region ... Variables  ...

27
        private Dictionary<int,Dictionary<int, YearTimeFile>> mTimeFileMaps = new Dictionary<int,Dictionary<int, YearTimeFile>>();
cdy816's avatar
cdy816 已提交
28

cdy816's avatar
cdy816 已提交
29 30 31 32 33 34
        /// <summary>
        /// 
        /// </summary>
        private Dictionary<string, LogFileInfo> mLogFileMaps = new Dictionary<string, LogFileInfo>();

        /// <summary>
cdy816's avatar
cdy816 已提交
35
        /// 记录所有变量历史记录中最后的时间
cdy816's avatar
cdy816 已提交
36 37 38
        /// </summary>
        internal static Dictionary<string, DateTime> CurrentDateTime = new Dictionary<string, DateTime>();

cdy816's avatar
cdy816 已提交
39 40
        private string mDatabaseName;

cdy816's avatar
cdy816 已提交
41 42 43 44 45
        /// <summary>
        /// 数据文件扩展名
        /// </summary>
        public const string DataFileExtends = ".dbd";

cdy816's avatar
cdy816 已提交
46 47 48 49 50
        public const string LogFileExtends = ".log";

        /// <summary>
        /// 
        /// </summary>
cdy816's avatar
cdy816 已提交
51 52
        public const int FileHeadSize = 72;

cdy816's avatar
cdy816 已提交
53 54 55
        private System.IO.FileSystemWatcher hisDataWatcher;

        private System.IO.FileSystemWatcher logDataWatcher;
56 57

        private object mLocker = new object();
cdy816's avatar
cdy816 已提交
58 59 60 61 62 63 64 65 66 67 68 69

        private ManualResetEvent mResetEvent = new ManualResetEvent(false);
        private bool mIsClosed = false;

        private Dictionary<string, WatcherChangeTypes> mFileCach = new Dictionary<string, WatcherChangeTypes>();

        private Dictionary<string, WatcherChangeTypes> mHisFileCach = new Dictionary<string, WatcherChangeTypes>();

        private int mResetCount = 0;

        private Thread mFileProcessThread;

cdy816's avatar
cdy816 已提交
70 71 72 73 74 75 76
        #endregion ...Variables...

        #region ... Events     ...

        #endregion ...Events...

        #region ... Constructor...
cdy816's avatar
cdy816 已提交
77

cdy816's avatar
cdy816 已提交
78 79 80 81 82 83 84 85 86 87 88 89
        /// <summary>
        /// 
        /// </summary>
        public DataFileManager(string dbname)
        {
            mDatabaseName = dbname;
        }

        #endregion ...Constructor...

        #region ... Properties ...

90 91 92
        /// <summary>
        /// 单个文件内变量的个数
        /// </summary>
93
        public int TagCountOneFile { get; set; } = 100000;
94

cdy816's avatar
cdy816 已提交
95 96 97 98 99
        /// <summary>
        /// 
        /// </summary>
        public string PrimaryHisDataPath { get; set; }

cdy816's avatar
cdy816 已提交
100 101 102
        /// <summary>
        /// 
        /// </summary>
cdy816's avatar
cdy816 已提交
103 104
        public string PrimaryLogDataPath { get; set; }

cdy816's avatar
cdy816 已提交
105 106 107 108 109
        /// <summary>
        /// 
        /// </summary>
        public string BackHisDataPath { get; set; }

cdy816's avatar
cdy816 已提交
110 111 112 113
        #endregion ...Properties...

        #region ... Methods    ...

cdy816's avatar
cdy816 已提交
114 115 116 117 118 119 120 121 122
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string GetPrimaryHisDataPath()
        {
            return string.IsNullOrEmpty(PrimaryHisDataPath) ? PathHelper.helper.GetDataPath(this.mDatabaseName,"HisData") : System.IO.Path.IsPathRooted(PrimaryHisDataPath) ? PrimaryHisDataPath : PathHelper.helper.GetDataPath(this.mDatabaseName,PrimaryHisDataPath);
        }

cdy816's avatar
cdy816 已提交
123 124 125 126 127 128 129 130 131
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string GetPrimaryLogDataPath()
        {
            return string.IsNullOrEmpty(PrimaryLogDataPath)?PathHelper.helper.GetDataPath(this.mDatabaseName, "Log"): System.IO.Path.IsPathRooted(PrimaryLogDataPath) ? PrimaryLogDataPath : PathHelper.helper.GetDataPath(this.mDatabaseName, PrimaryLogDataPath);
        }

cdy816's avatar
cdy816 已提交
132 133 134 135 136 137 138 139 140 141
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string GetBackHisDataPath()
        {
            return string.IsNullOrEmpty(BackHisDataPath) ? PathHelper.helper.GetDataPath(this.mDatabaseName, "HisData") : System.IO.Path.IsPathRooted(BackHisDataPath) ? BackHisDataPath : PathHelper.helper.GetDataPath(this.mDatabaseName, BackHisDataPath);
        }


cdy816's avatar
cdy816 已提交
142 143 144
        /// <summary>
        /// 
        /// </summary>
145
        public async Task Int()
cdy816's avatar
cdy816 已提交
146
        {
cdy816's avatar
cdy816 已提交
147
            string datapath = GetPrimaryHisDataPath();
cdy816's avatar
cdy816 已提交
148
            
cdy816's avatar
cdy816 已提交
149 150 151 152 153 154 155 156 157
            await Scan(datapath);
            if (System.IO.Directory.Exists(datapath))
            {
                hisDataWatcher = new System.IO.FileSystemWatcher(GetPrimaryHisDataPath());
                hisDataWatcher.Changed += HisDataWatcher_Changed;
                hisDataWatcher.EnableRaisingEvents = true;
            }

            string logpath = GetPrimaryLogDataPath();
cdy816's avatar
cdy816 已提交
158
            ScanLogFile(logpath);
cdy816's avatar
cdy816 已提交
159 160 161 162 163 164 165
            if (System.IO.Directory.Exists(logpath))
            {
                logDataWatcher = new System.IO.FileSystemWatcher(logpath);
                logDataWatcher.Changed += LogDataWatcher_Changed;
                logDataWatcher.EnableRaisingEvents = true;
            }

cdy816's avatar
cdy816 已提交
166 167 168 169 170 171 172 173
            foreach (var vv in this.mTimeFileMaps)
            {
                foreach (var vvv in vv.Value)
                {
                    vvv.Value.UpdateLastDatetime();
                }
            }

cdy816's avatar
cdy816 已提交
174
           //await Scan(GetBackHisDataPath());
cdy816's avatar
cdy816 已提交
175 176
        }

cdy816's avatar
cdy816 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
        

        /// <summary>
        /// 
        /// </summary>
        public void Start()
        {
            mIsClosed= false;
            mFileProcessThread = new Thread(FileProcess);
            mFileProcessThread.IsBackground = true;
            mFileProcessThread.Start();
        }

        /// <summary>
        /// 
        /// </summary>
        public void Stop()
        {
            mIsClosed = true;
            mResetEvent.Close();
            while (mFileProcessThread.IsAlive) Thread.Sleep(1);
        }

        /// <summary>
        /// 
        /// </summary>
        private void FileProcess()
        {
            List<KeyValuePair<string, WatcherChangeTypes>> ltmp = null;
            while (!mIsClosed)
            {
                mResetEvent.WaitOne();
                mResetEvent.Reset();
                if (mIsClosed) break;
                mResetCount = 0;

                while (mResetCount<10)
                {
                    Thread.Sleep(100);
                    mResetCount++;
                }

                if(mFileCach.Count>0)
                {
                   
                    lock(mLocker)
                    {
                        ltmp = mFileCach.ToList();
                        mFileCach.Clear();
                    }

                    foreach(var vv in ltmp)
                    {
                        LoggerService.Service.Info("DataFileMananger", "LogFile " + vv.Value + " add to FileCach!");
                        ParseLogFile(vv.Key);
                    }
                }


                if(mHisFileCach.Count>0)
                {
                    lock (mLocker)
                    {
                        ltmp = this.mHisFileCach.ToList();
                        mHisFileCach.Clear();
                    }

                    foreach (var vv in ltmp)
                    {
                        var vifno = new System.IO.FileInfo(vv.Key);
                        if (vv.Value == System.IO.WatcherChangeTypes.Created)
                        {
                            LoggerService.Service.Info("DataFileMananger", "HisDataFile " + vv.Key + " is Created & will be add to dataFileCach!");
                            ParseFileName(vifno);
                        }
                        else
                        {
                            LoggerService.Service.Info("DataFileMananger", "HisDataFile " + vv.Key + " is changed & will be processed!");

                            var vfile = CheckAndGetDataFile(vv.Key);
                            if (vfile != null)
                            {
                                vfile.UpdateLastDatetime();
                            }
                            else
                            {
                                ParseFileName(vifno);
                            }
                        }
                    }
                }
            }
        }

cdy816's avatar
cdy816 已提交
271 272 273 274 275 276 277
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LogDataWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
cdy816's avatar
cdy816 已提交
278 279
            mResetEvent.Set();
            mResetCount = 0;
cdy816's avatar
cdy816 已提交
280 281 282 283 284 285 286 287 288
            if (e.ChangeType == System.IO.WatcherChangeTypes.Deleted)
            {
                if(mLogFileMaps.ContainsKey(e.FullPath))
                {
                    mLogFileMaps.Remove(e.FullPath);
                }
            }
            else 
            {
cdy816's avatar
cdy816 已提交
289 290 291 292 293 294 295 296 297
                lock (mLocker)
                {
                    if (!mFileCach.ContainsKey(e.FullPath))
                    {
                        mFileCach.Add(e.FullPath, e.ChangeType);
                    }
                }
                //LoggerService.Service.Info("DataFileMananger", "LogFile "+ e.Name + " add to FileCach!");
                //ParseLogFile(e.FullPath);
cdy816's avatar
cdy816 已提交
298 299 300 301 302
            }
        }

        private void HisDataWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
cdy816's avatar
cdy816 已提交
303 304 305
            mResetEvent.Set();
            mResetCount = 0;
            if (e.ChangeType == System.IO.WatcherChangeTypes.Created)
cdy816's avatar
cdy816 已提交
306
            {
307
                lock (mLocker)
cdy816's avatar
cdy816 已提交
308
                {
cdy816's avatar
cdy816 已提交
309
                    if(!mHisFileCach.ContainsKey(e.FullPath))
310
                    {
cdy816's avatar
cdy816 已提交
311 312 313 314 315 316
                        var vifno = new System.IO.FileInfo(e.FullPath);
                        if (vifno.Extension == DataFileExtends)
                        {
                            mHisFileCach.Add(e.FullPath, e.ChangeType);
                            //ParseFileName(vifno);
                        }
317
                    }
cdy816's avatar
cdy816 已提交
318 319
                    //LoggerService.Service.Info("DataFileMananger", "HisDataFile " + e.Name + " is Created & will be add to dataFileCach!");
                    
cdy816's avatar
cdy816 已提交
320 321 322 323
                }
            }
            else if(e.ChangeType == System.IO.WatcherChangeTypes.Changed)
            {
cdy816's avatar
cdy816 已提交
324 325
               // LoggerService.Service.Info("DataFileMananger", "HisDataFile " + e.Name + " is changed & will be processed!");
               
326
                lock (mLocker)
cdy816's avatar
cdy816 已提交
327
                {
cdy816's avatar
cdy816 已提交
328
                    if (!mHisFileCach.ContainsKey(e.FullPath))
cdy816's avatar
cdy816 已提交
329
                    {
cdy816's avatar
cdy816 已提交
330 331
                        var vifno = new System.IO.FileInfo(e.FullPath);
                        if (vifno.Extension == DataFileExtends)
332
                        {
cdy816's avatar
cdy816 已提交
333 334
                            mHisFileCach.Add(e.FullPath, e.ChangeType);
                            //ParseFileName(vifno);
335
                        }
cdy816's avatar
cdy816 已提交
336
                    }
cdy816's avatar
cdy816 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349

                    //if (vtmp.Extension == DataFileExtends)
                    //{
                    //    var vfile = CheckAndGetDataFile(e.Name);
                    //    if (vfile != null)
                    //    {
                    //        vfile.UpdateLastDatetime();
                    //    }
                    //    else
                    //    {
                    //        ParseFileName(vtmp);
                    //    }
                    //}
cdy816's avatar
cdy816 已提交
350 351 352 353
                }
            }
        }

cdy816's avatar
cdy816 已提交
354
        public void ScanLogFile(string path)
cdy816's avatar
cdy816 已提交
355 356 357 358 359 360 361 362 363 364 365
        {
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
            if (dir.Exists)
            {
                foreach (var vv in dir.GetFiles())
                {
                    if (vv.Extension == LogFileExtends)
                    {
                        ParseLogFile(vv.FullName);
                    }
                }
cdy816's avatar
cdy816 已提交
366 367 368 369
                //foreach (var vv in dir.GetDirectories())
                //{
                //    await ScanLogFile(vv.FullName);
                //}
cdy816's avatar
cdy816 已提交
370 371 372
            }
        }

cdy816's avatar
cdy816 已提交
373 374 375 376
        /// <summary>
        /// 搜索文件
        /// </summary>
        /// <param name="path"></param>
377
        public async Task Scan(string path)
cdy816's avatar
cdy816 已提交
378 379
        {
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
cdy816's avatar
cdy816 已提交
380
            if (dir.Exists)
cdy816's avatar
cdy816 已提交
381
            {
cdy816's avatar
cdy816 已提交
382
                foreach (var vv in dir.GetFiles())
cdy816's avatar
cdy816 已提交
383
                {
cdy816's avatar
cdy816 已提交
384
                    if (vv.Extension == DataFileExtends)
cdy816's avatar
cdy816 已提交
385 386 387 388 389 390 391
                    {
                        ParseFileName(vv);
                    }
                }
                foreach (var vv in dir.GetDirectories())
                {
                    await Scan(vv.FullName);
cdy816's avatar
cdy816 已提交
392 393 394 395
                }
            }
        }

cdy816's avatar
cdy816 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
        /// <summary>
        /// 
        /// </summary>
        /// <param name="file"></param>
        private void ParseLogFile(string sfileName)
        {

            var vname = System.IO.Path.GetFileNameWithoutExtension(sfileName);

            DateTime dt = new DateTime(int.Parse(vname.Substring(0, 4)), int.Parse(vname.Substring(4, 2)), int.Parse(vname.Substring(6, 2)), int.Parse(vname.Substring(8, 2)), int.Parse(vname.Substring(10, 2)), int.Parse(vname.Substring(12, 2)));
            int timelen = int.Parse(vname.Substring(14, 3));

            if(!mLogFileMaps.ContainsKey(sfileName))
            {
                mLogFileMaps.Add(sfileName, new LogFileInfo() { FileName = sfileName, StartTime = dt, EndTime = dt.AddSeconds(timelen) });
            }
        }

        private DataFileInfo CheckAndGetDataFile(string file)
        {
            string sname = file.Replace(DataFileExtends, "");
            string stime = sname.Substring(sname.Length - 12, 12);
            int yy = 0, mm = 0, dd = 0;

            int id = -1;
            int.TryParse(sname.Substring(sname.Length - 15, 3), out id);

            if (id == -1)
                return null;

            if (!int.TryParse(stime.Substring(0, 4), out yy))
            {
                return null;
            }

            if (!int.TryParse(stime.Substring(4, 2), out mm))
            {
                return null;
            }

            if (!int.TryParse(stime.Substring(6, 2), out dd))
            {
                return null;
            }
            int hhspan = int.Parse(stime.Substring(8, 2));

            int hhind = int.Parse(stime.Substring(10, 2));

            int hh = hhspan * hhind;


            DateTime startTime = new DateTime(yy, mm, dd, hh, 0, 0);
448
            try
cdy816's avatar
cdy816 已提交
449
            {
450 451 452 453 454 455 456 457
                if (mTimeFileMaps.ContainsKey(id))
                {
                    return mTimeFileMaps[id][yy].GetDataFile(startTime);
                }
            }
            catch(Exception ex)
            {
                LoggerService.Service.Erro("DataFileMananger", ex.StackTrace);
cdy816's avatar
cdy816 已提交
458 459 460 461
            }
            return null;
        }

cdy816's avatar
cdy816 已提交
462 463 464 465 466 467
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        private void ParseFileName(System.IO.FileInfo file)
        {
cdy816's avatar
cdy816 已提交
468
            string sname = file.Name.Replace(DataFileExtends, "");
cdy816's avatar
cdy816 已提交
469
            string stime = sname.Substring(sname.Length - 12, 12);
470
            int yy=0, mm=0, dd=0;
cdy816's avatar
cdy816 已提交
471

472 473 474 475 476 477
            int id = -1;
            int.TryParse(sname.Substring(sname.Length - 15, 3), out id);

            if (id == -1)
                return;

478 479 480 481
            if (!int.TryParse(stime.Substring(0, 4),out yy))
            {
                return;
            }
cdy816's avatar
cdy816 已提交
482

483 484 485 486 487 488 489 490 491 492
            if (!int.TryParse(stime.Substring(4, 2), out mm))
            {
                return;
            }

            if (!int.TryParse(stime.Substring(6, 2), out dd))
            {
                return;
            }
            int hhspan = int.Parse(stime.Substring(8, 2));
cdy816's avatar
cdy816 已提交
493
            
494
            int hhind = int.Parse(stime.Substring(10, 2));
cdy816's avatar
cdy816 已提交
495

496 497 498 499
            int hh = hhspan * hhind;


            DateTime startTime = new DateTime(yy, mm, dd, hh, 0, 0);
cdy816's avatar
cdy816 已提交
500 501

            YearTimeFile yt = new YearTimeFile() { TimeKey = yy };
cdy816's avatar
cdy816 已提交
502
           
503
            if(mTimeFileMaps.ContainsKey(id))
cdy816's avatar
cdy816 已提交
504
            {
505 506 507 508 509 510 511 512
                if (mTimeFileMaps[id].ContainsKey(yy))
                {
                    yt = mTimeFileMaps[id][yy];
                }
                else
                {
                    mTimeFileMaps[id].Add(yy, yt);
                }
cdy816's avatar
cdy816 已提交
513 514 515
            }
            else
            {
516 517
                mTimeFileMaps.Add(id, new Dictionary<int, YearTimeFile>());
                mTimeFileMaps[id].Add(yy, yt);
cdy816's avatar
cdy816 已提交
518
            }
cdy816's avatar
cdy816 已提交
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
            yt.AddFile(startTime, new TimeSpan(hhspan, 0, 0), new DataFileInfo() { Duration = new TimeSpan(hhspan, 0, 0), StartTime = startTime, FileName = file.FullName,FId= mDatabaseName + id });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        private LogFileInfo GetLogDataFile(DateTime time)
        {
            foreach(var vv in mLogFileMaps.Values.ToArray())
            {
                if (vv.StartTime <= time && time < vv.EndTime) return vv;
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="time"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        private bool  CheckDataInLogFile(DateTime time,int id)
        {
544 545 546 547 548 549 550
            string sname = mDatabaseName + id;
            if(CurrentDateTime.ContainsKey(sname))
            return CurrentDateTime[sname] < time;
            else
            {
                return false;
            }
cdy816's avatar
cdy816 已提交
551 552 553 554 555 556
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="time"></param>
557
        /// <param name="Id"></param>
cdy816's avatar
cdy816 已提交
558
        /// <returns></returns>
559
        public DataFileInfo GetDataFile(DateTime time,int Id)
cdy816's avatar
cdy816 已提交
560
        {
561 562
            int id = Id / TagCountOneFile;

cdy816's avatar
cdy816 已提交
563 564 565 566 567 568
            if (CheckDataInLogFile(time,id))
            {
                //如果查询时间,比最近更新的时间还要新,则需要查询日志文件
                return null;
            }
            else
cdy816's avatar
cdy816 已提交
569
            {
cdy816's avatar
cdy816 已提交
570 571 572 573
                if (mTimeFileMaps.ContainsKey(id) && mTimeFileMaps[id].ContainsKey(time.Year))
                {
                    return mTimeFileMaps[id][time.Year].GetDataFile(time);
                }
cdy816's avatar
cdy816 已提交
574 575 576 577
            }
            return null;
        }

578

cdy816's avatar
cdy816 已提交
579 580 581 582 583
        /// <summary>
        /// 
        /// </summary>
        /// <param name="starttime"></param>
        /// <param name="endtime"></param>
584
        /// <param name="Id"></param>
cdy816's avatar
cdy816 已提交
585
        /// <returns></returns>
cdy816's avatar
cdy816 已提交
586
        public List<DataFileInfo> GetDataFiles(DateTime starttime, DateTime endtime, out Tuple<DateTime, DateTime> logFileTimes, int Id)
cdy816's avatar
cdy816 已提交
587
        {
cdy816's avatar
cdy816 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
            //string sid = mDatabaseName + Id;
            //if (CurrentDateTime.ContainsKey(sid))
            //{
            //    if (starttime > CurrentDateTime[sid])
            //    {
            //        logFileTimes = new Tuple<DateTime, DateTime>(starttime, endtime);
            //        return new List<DataFileInfo>();
            //    }
            //    else if (endtime <= CurrentDateTime[sid])
            //    {
            //        logFileTimes = new Tuple<DateTime, DateTime>(DateTime.MinValue, DateTime.MinValue);
            //        return GetDataFiles(starttime, endtime - starttime, Id);
            //    }
            //    else
            //    {
            //        logFileTimes = new Tuple<DateTime, DateTime>(CurrentDateTime[sid], endtime);
            //        return GetDataFiles(starttime, CurrentDateTime[sid] - starttime, Id);
            //    }
            //}
            //else
            //{
            //    logFileTimes = new Tuple<DateTime, DateTime>(starttime, endtime);
            //    return GetDataFiles(starttime, endtime - starttime, Id);
            //}
            DateTime dt = DateTime.MinValue;
            var vfiles = GetDataFiles(starttime, endtime - starttime, Id);
            foreach (var vv in vfiles)
cdy816's avatar
cdy816 已提交
615
            {
cdy816's avatar
cdy816 已提交
616
                dt = vv.LastTime>dt?vv.LastTime:dt;
cdy816's avatar
cdy816 已提交
617
            }
cdy816's avatar
cdy816 已提交
618 619 620
            logFileTimes = new Tuple<DateTime, DateTime>(dt, endtime);
            return vfiles;

cdy816's avatar
cdy816 已提交
621 622 623 624 625
        }

        /// <summary>
        /// 
        /// </summary>
626 627 628
        /// <param name="startTime"></param>
        /// <param name="span"></param>
        /// <param name="Id"></param>
cdy816's avatar
cdy816 已提交
629
        /// <returns></returns>
630
        public List<DataFileInfo> GetDataFiles(DateTime startTime, TimeSpan span, int Id)
cdy816's avatar
cdy816 已提交
631
        {
632 633
            List<DataFileInfo> re = new List<DataFileInfo>();
            int id = Id / TagCountOneFile;
634
            if (mTimeFileMaps.ContainsKey(id))
cdy816's avatar
cdy816 已提交
635
            {
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
                var nxtYear = new DateTime(startTime.Year+1, 1, 1);
                if (nxtYear > startTime + span)
                {
                    int mon = startTime.Year;
                    if (mTimeFileMaps[id].ContainsKey(mon))
                    {
                        re.AddRange((mTimeFileMaps[id][mon]).GetDataFiles(startTime, span));
                    }
                }
                else
                {
                    int mon = startTime.Year;
                    if (mTimeFileMaps[id].ContainsKey(mon))
                    {
                        re.AddRange((mTimeFileMaps[id][mon]).GetDataFiles(startTime, span));
                    }
                    re.AddRange(GetDataFiles(nxtYear, startTime + span - nxtYear,Id));
                }
cdy816's avatar
cdy816 已提交
654 655 656 657 658 659 660
            }
            return re;
        }

        /// <summary>
        /// 
        /// </summary>
661 662
        /// <param name="times"></param>
        /// <param name="Id"></param>
cdy816's avatar
cdy816 已提交
663
        /// <returns></returns>
cdy816's avatar
cdy816 已提交
664
        public SortedDictionary<DateTime, DataFileInfo> GetDataFiles(List<DateTime> times, List<DateTime> logFileTimes,int Id)
cdy816's avatar
cdy816 已提交
665
        {
666 667 668
            SortedDictionary<DateTime, DataFileInfo> re = new SortedDictionary<DateTime, DataFileInfo>();
            foreach(var vv in times)
            {
cdy816's avatar
cdy816 已提交
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
                if (CheckDataInLogFile(vv, Id))
                {
                    logFileTimes.Add(vv);
                }
                else
                {
                    re.Add(vv, GetDataFile(vv, Id));
                }
            }
            return re;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="times"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public SortedDictionary<DateTime, LogFileInfo> GetLogDataFiles(List<DateTime> times)
        {
            SortedDictionary<DateTime, LogFileInfo> re = new SortedDictionary<DateTime, LogFileInfo>();
            foreach (var vvd in times)
            {
                re.Add(vvd, GetLogDataFile(vvd));
693 694
            }
            return re;
cdy816's avatar
cdy816 已提交
695 696
        }

cdy816's avatar
cdy816 已提交
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
        /// <summary>
        /// 
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endtime"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public List<LogFileInfo> GetLogDataFiles(DateTime startTime, DateTime endtime)
        {
            List<LogFileInfo> re = new List<LogFileInfo>();
            foreach (var vv in mLogFileMaps.ToArray())
            {
                if ((vv.Value.StartTime >= startTime && vv.Value.StartTime < endtime) || (vv.Value.EndTime >= startTime && vv.Value.EndTime < endtime))
                {
                    re.Add(vv.Value);
                }
            }
            return re;
        }
716

cdy816's avatar
cdy816 已提交
717 718 719 720 721 722 723
        #endregion ...Methods...

        #region ... Interfaces ...

        #endregion ...Interfaces...
    }
}