ImageSavingBl.cs 5.41 KB
using OS.Spin.Common;
using OS.Spin.Modle.BusinessLayer;
using OS.Spin.Running;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Threading;

namespace OS.Spin.BusinessLayer.SubBusiness
{
    public class ImageSavingBl
    {
        private static ImageSavingBl _savingBl = null;

        private static object _objLock = new object();
        public List<string> lstManMade = new List<string>();

        private System.Threading.Thread _runing;

        private ConcurrentQueue<MFileCopy> _copys = new ConcurrentQueue<MFileCopy>();
        private bool _stop = false;
        private ImageSavingBl()
        {
            _runing = new System.Threading.Thread(Working);

            _runing.Start();
        }

        public static ImageSavingBl Getinstance()
        {
            lock (_objLock)
            {
                if (null == _savingBl)
                {
                    _savingBl = new ImageSavingBl();
                }

                return _savingBl;
            }
        }

        public void PutCopy(MFileCopy copy)
        {
            _copys.Enqueue(copy);
        }

        private void Working()
        {
            while (true)
            {
                try
                {
                    if (_copys.Count == 0)
                    {
                        System.Threading.Thread.Sleep(100);
                        continue;
                    }

                    var id = Cache.GetInstance().CacheImgId;
                    MFileCopy copy;
                    _copys.TryDequeue(out copy);

                    Copying(copy);
                }
                catch (Exception ex)
                {
                    LogisTrac.WriteLog(string.Format("Working:{0}",ex.Message));
                }
                //var count = _copys.Count();

                //var stop = _stop;

                //for (var i = 0; i < count; i++)
                //{
                //    var copy = _copys[i];

                //    if (id < copy.MaxId && !stop)
                //    {
                //        continue;
                //    }

                //    Copying(copy);
                //    _copys.Remove(copy);
                //}

                //if(stop)
                //{
                //    _stop = false;
                //}
            }
        }


        public void Stop()
        {
            _stop = true;
        }


        public void Dispose()
        {
            if (null != _runing && _runing.IsAlive)
            {
                _runing.Abort();
            }

            _runing = null;
        }
        private void Copying(MFileCopy copy)
        {
            try
            {
                var auto = "AutoMade";

                var file = Path.Combine(System.IO.Directory.GetCurrentDirectory(), string.Format(@"Flaws\{0}\{1}", auto, DateTime.Now.ToString("yyyyMMdd")));

                switch (copy.CopyType)
                {
                    case 0:
                        auto = "AutoMade";
                        file = Path.Combine(System.IO.Directory.GetCurrentDirectory(), string.Format(@"Flaws\{0}\{1}", auto, DateTime.Now.ToString("yyyyMMdd")));
                        break;
                    case 1:
                        auto = "ManMade";
                        file = Path.Combine(System.IO.Directory.GetCurrentDirectory(), string.Format(@"Flaws\{0}\{1}\{2}", auto, DateTime.Now.ToString("yyyyMMdd"), copy.FileName));
                        break;
                    case 2:
                        auto = "ManSnap";
                        file = Path.Combine(System.IO.Directory.GetCurrentDirectory(), string.Format(@"Flaws\{0}\{1}", auto, DateTime.Now.ToString("yyyyMMdd")));
                        break;
                }

                if (!Directory.Exists(file))
                {
                    Directory.CreateDirectory(file);
                }

                for (var id = copy.MinId; id <= copy.MaxId; id++)
                {
                    for (var c = 0; c < copy.CameraCount; c++)
                    {
                        var src = Path.Combine(System.IO.Directory.GetCurrentDirectory(), string.Format(@"running\camera\{0}\{1}.jpg", c, id));


                        bool isBreak = true;

                        for (var i = 0; i < 3; i++)
                        {
                            if (File.Exists(src))
                            {
                                isBreak = false;
                                break;
                            }

                            Thread.Sleep(300);
                        }

                        if (isBreak)
                        {
                            continue;
                        }

                        var des = Path.Combine(file, string.Format("{0}_{1}.jpg", (copy.CopyType == 1) ? (id - copy.MinId).ToString() : copy.FileName, c + 1));
                        File.Copy(src, des, true);
                        if (copy.CopyType == 0)
                        {
                            //每次只存最新的三张
                            if (lstManMade.Count == 3)
                                lstManMade.Clear();
                            lstManMade.Add(des);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogisTrac.WriteLog(string.Format("Copying:{0}", ex.Message));
            }
        }
    }
}