Cache.cs 7.37 KB
using OpenCvSharp;
using OS.Spin.Common;
using OS.Spin.Modle.BusinessLayer;
using System;
using System.Collections.Generic;
using System.Linq;

namespace OS.Spin.Running
{
    public sealed class Cache
    {
        private Dictionary<int, List<Modle.Sdk.StructInfos.DATA_IMAGE>> _flawImgs = new Dictionary<int, List<Modle.Sdk.StructInfos.DATA_IMAGE>>();

        private Dictionary<int, List<Modle.Sdk.StructInfos.Fore_Result>> _foreResults = new Dictionary<int, List<Modle.Sdk.StructInfos.Fore_Result>>();

        private Dictionary<int, List<Mat>> _imgo_Roi = new Dictionary<int, List<Mat>>();

        private Dictionary<int, Mat> _mats = new Dictionary<int, Mat>();

        private MGateWidth _gate = new MGateWidth();
        private int _cloths_kind = 0;

        private double _findWall = 0.7;
        /// <summary>
        /// 检测强调
        /// </summary>
        public double FindWall
        {
            get { return _findWall; }
            set
            {
                _findWall = value;
            }
        }

        private bool _camerasOk = false;
        public int ClothWidth { get; set; }
        public int GetCount = 0;
        private const int _cameraCount = 3;
        private int _okCount = 0;

        private object _objCameraLock = new object();

        public bool CamerasOk
        {
            get { return _camerasOk; }
            set
            {
                lock (_objCameraLock)
                {
                    if (!value)
                    {
                        _okCount = 0;
                    }

                    if (value && _okCount < _cameraCount)
                    {
                        _okCount++;
                        return;
                    }
                    _camerasOk = value;
                }
            }
        }
        private double _mhWall = 0.9;
        /// <summary>
        /// 检测质量
        /// </summary>
        public double MhWall
        {
            get { return _mhWall; }
            set
            {
                _mhWall = value;
            }
        }

        private double _jdWall = 0.4;
        /// <summary>
        /// 检测精度
        /// </summary>
        public double JdWall
        {
            get { return _jdWall; }
            set
            {
                _jdWall = value;
            }
        }

        public int Cloths_kind
        {
            get { return _cloths_kind; }
            set
            {
                _cloths_kind = value;
            }
        }

        private object _lock = new object();
        public MGateWidth GateInfo
        {
            get { return _gate; }
        }
        public Dictionary<int, Mat> Mats
        {
            get { return _mats; }
            set { _mats = value; }
        }

        private Dictionary<int, bool> _cameraImg = new Dictionary<int, bool>();

        private static Cache _cDk;
        private static readonly object ObjLock = new object();

        private int _flawId = 1;

        private int _cacheImgId = 0;

        public int CacheImgId
        {
            get { return _cacheImgId; }
            set { _cacheImgId = value; }
        }

        public int FlawId
        {
            get { return _flawId++; }
            set { _flawId = value; }
        }

        public static Cache GetInstance()
        {
            lock (ObjLock)
            {
                return _cDk ?? (_cDk = new Cache());
            }
        }

        private Cache()
        {

        }

        public List<Modle.Sdk.StructInfos.Fore_Result> GetFore_Result(int cameraId)
        {
            try
            {
                if (_foreResults.Keys.Contains(cameraId))
                {
                    return _foreResults[cameraId];
                }
                return null;
            }
            catch (Exception ex)
            {
                LogisTrac.WriteLog(string.Format("GetFore_Result:{0}", ex.Message));
                return null;
            }
        }

        public void SetFore_Result(int cameraId, List<Modle.Sdk.StructInfos.Fore_Result> result)
        {
            lock (_lock)
            {
                _foreResults[cameraId] = result;
            }

        }


        public List<Mat> GetImgo_Roi(int cameraId)
        {
            try
            {
                if (_foreResults.Keys.Contains(cameraId))
                {
                    return _imgo_Roi[cameraId];
                }
                return null;
            }
            catch (Exception ex)
            {
                LogisTrac.WriteLog(string.Format("GetImgo_Roi:{0}", ex.Message));
                return null;
            }
        }

        public void SetImgo_Roi(int cameraId, List<Mat> result)
        {
            _imgo_Roi[cameraId] = result;
        }

        //public List<Modle.Sdk.StructInfos.DATA_IMAGE> GetCameraImg(int cameraId)
        //{
        //    if (_flawImgs.Keys.Contains(cameraId))
        //    {
        //        return _flawImgs[cameraId];
        //    }
        //    return null;
        //}

        public void ClearFlawImg(int cameraId)
        {
            //_flawImgs[cameraId] = new List<Modle.Sdk.StructInfos.DATA_IMAGE>();
        }

        //public void AddFlawImg(int cameraId, Mat img)
        //{
        //    if(!_flawImgs.Keys.Contains(cameraId))
        //    {
        //        _flawImgs[cameraId] = new List<Modle.Sdk.StructInfos.DATA_IMAGE>();
        //    }

        //    var flaw = new Modle.Sdk.StructInfos.DATA_IMAGE();

        //    flaw.channel = 3;
        //    flaw.data = img.Data;
        //    flaw.width = img.Width;
        //    flaw.height = img.Height;

        //    _flawImgs[cameraId].Add(flaw);
        //}

        //private byte _figureId;
        //public byte FigureId
        //{
        //    set
        //    {
        //        if(value != _figureId)
        //        {
        //            ReLoadAlgorithmInfo(value);
        //        }
        //        _figureId = value;
        //    }
        //    get
        //    {
        //        return _figureId;
        //    }
        //}

        private double _cMeter = 0;
        public double CMeter
        {
            get { return _cMeter; }
            set { _cMeter = value; }
        }

        private double _speed = 0;
        public double Speed
        {
            get { return _speed; }
            set { _speed = value; }
        }

        private bool _autoFlaw = true;
        public bool AutoFlaw
        {
            get { return _autoFlaw; }
            set { _autoFlaw = value; }
        }

        //private void ReLoadAlgorithmInfo(byte figureId)
        //{
        //    // Todo 加载信息
        //}

        //private Modle.Sdk.StructInfos.DATA_IMAGE _algorithmWidth ;
        //public Modle.Sdk.StructInfos.DATA_IMAGE AlgorithmWidth
        //{
        //    get { return _algorithmWidth; }
        //    private set { _algorithmWidth = value; }
        //}

        //private Modle.Sdk.StructInfos.DATA_IMAGE _algorithmHeight;
        //public Modle.Sdk.StructInfos.DATA_IMAGE AlgorithmHeight
        //{
        //    get { return _algorithmHeight; }
        //    private set { _algorithmHeight = value; }
        //}

        //private Modle.Sdk.StructInfos.DETC_PARAM _detectionParams;
        //public Modle.Sdk.StructInfos.DETC_PARAM DetectionParams
        //{
        //    get { return _detectionParams; }
        //    set { _detectionParams = value; }
        //}
    }
}