using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace BeltScaleSystem
{
    public delegate void MeasChangedEventHandler(object sender, MeasEventArgs e);//
    public delegate void CalibrateChangedEventHandler(object sender, CalibrateEventArgs e);

    /// <summary>
    /// 皮带仪表类型
    /// </summary>
    public enum StrapMeasType
    {
        SA600 = 0,
        ICS300 = 1,
        XR6000 = 2,
    }

    /// <summary>
    /// 仪表数据接收事件数据
    /// </summary>
    public class MeasEventArgs : EventArgs
    {
        private MeasData _value;
        public MeasData Value
        {
            get { return _value; }
            set { this._value = value; }
        }
        public MeasEventArgs()
        {
            _value = new MeasData();
        }
        public MeasEventArgs(MeasData value)
        {
            this._value = value;
        }
    }

    public class CalibrateEventArgs : EventArgs
    {
        private CalibrateData _value;
        public CalibrateData Value
        {
            get { return _value; }
            set { this._value = value; }
        }
        public CalibrateEventArgs()
        {
            _value = new CalibrateData();
        }
        public CalibrateEventArgs(CalibrateData value)
        {
            this._value = value;
        }
    }

    public class CoreStrapMeas
    {
        public MeasChangedEventHandler onMeasChanged = null;
        public CalibrateChangedEventHandler CalibrateChanged = null;

        private SA600Meas _sa600 = null;
        private int[] _ics300DevNos = new int[] { 1 };//仪表编号，默认取第一个仪表重量
        private int[] _xr6000DevNos = new int[] { 1 };//仪表编号，默认取第一个仪表重量

        private Thread _measThread = null;
        private Thread _calibrateThread = null;
        public Boolean IsClose = false; 

        private bool _isOpen = false;
        /// <summary>
        /// 仪表连接是否打开
        /// </summary>
        public bool IsOpen
        {
            get { return _isOpen; }
            set { _isOpen = value; }
        }

        private bool _isReceiveMeasData = false;
        /// <summary>
        /// 设置或获取是否采集仪表数据
        /// </summary>
        public bool IsReceiveMeasData
        {
            get { return _isReceiveMeasData; }
            set { _isReceiveMeasData = value; }
        }

        private StrapMeasType _measType = StrapMeasType.SA600;
        public CoreStrapMeas(StrapMeasType measType)
        {
            _measType = measType;
        }
        /// <summary>
        /// ICS300 多仪表或单仪表编号不为1时使用此函数
        /// </summary>
        /// <param name="devNums"></param>
        public bool Open(String addr, int[] devNums)
        {
            bool ret = Open(addr);
            _ics300DevNos = devNums;
            return ret;
        }

        /// <summary>
        /// 打开仪表连接
        /// </summary>
        /// <param name="addr">IPAddress｜ComPortName,BaudRate,Parity,DataBits,StopBits</param>
        /// <returns></returns>
        public bool Open(String addr)
        {
            bool ret = true;
            try
            {
                switch (_measType)
                {
                    case StrapMeasType.SA600:
                        _sa600 = new SA600Meas();
                        _sa600.IP = addr;
                        break;
                }
                _isReceiveMeasData = true;//开始接收仪表数据

                if (ret)
                {
                    _measThread = new Thread(new ThreadStart(FreshMeasData));
                    _measThread.Start();
                  
                }
            }
            catch (Exception ex)
            {
                ret = false;
            }
            _isOpen = ret;
            return ret;
        }

        public bool Close()
        {
            bool ret = true;
            if (_measThread != null)
            {
                _measThread.Abort();
            }
            if (_calibrateThread != null)
            {
                _calibrateThread.Abort();
            }
          
            switch (_measType)
            {
                case StrapMeasType.SA600:
                    break;
            }
            IsClose = true;
            return ret;
        }

        private void FreshMeasData()
        {
            while (true)
            {
                //Thread.Sleep(500);//20210303liao
                Thread.Sleep(800);//20210303liao500改为800
                if (_isReceiveMeasData)
                {
                    MeasData measData = new MeasData();
                    measData.MeasType = _measType;
                    switch (_measType)
                    {
                        case StrapMeasType.SA600:
                            _sa600.ReadRing();
                            measData.Flow = _sa600.Flow;
                            measData.FieldTemp = _sa600.FieldTemp;
                            measData.Total = _sa600.Total;
                            measData.BeltSpeed = _sa600.BeltSpeed;
                            measData.CalibrateValue = _sa600.CalibrateValue;
                            measData.MeterStatus = !_sa600.MeasStatus;
                            measData.IsConnect = _sa600.IsConnect;               
                             _sa600.ReadRingbt();//20210301liao
                            measData.StdValue = _sa600.StdValue;//20210301liao
                            if (onMeasChanged != null)
                            {
                                onMeasChanged(this, new MeasEventArgs(measData));
                            }
                            break;
                       
                    }
                }
            }
        }
      

        public CalibrateData[] FreshCalibrateData()
        {
            CalibrateData []cbDatas = null;
            CalibrateData cbData = null;

            switch (_measType)
            {
                case StrapMeasType.SA600:
                    _sa600.ReadRing();
                  //  _sa600.ReadRingbt();//20210301liaobtxx
                    
                    cbData = new CalibrateData();
                    cbData.MeasType = StrapMeasType.SA600;
                    cbData.NewZero = _sa600.CalibrateValue; //校零数值
                    cbData.RemainSeconds = _sa600.ProgressTime;
                    cbData.DevStatus = _sa600.CalibrateFlag;

                    cbDatas = new CalibrateData[]{cbData};
                    break;
            }
            return cbDatas;
        }

        public void StartCalibrateZero()
        {
            _isReceiveMeasData = false;
            Thread.Sleep(2000);
            switch (_measType)
            {
                case StrapMeasType.SA600:
                    _sa600.StartCalibrateZero();
                    break;
            }
        }

        public void ConfirmCalibrateValue()
        {
            switch (_measType)
            {
                case StrapMeasType.SA600:
                    _sa600.SaveCalibrateValue();
                    break;
            }
            _isReceiveMeasData = true;
        }
        public void setMatParameter(byte[] byteArray) {
            switch (_measType)
            {
                case StrapMeasType.SA600:
                    _sa600.SaveBaseValue(byteArray);
                    break;
            }
            _isReceiveMeasData = true;
        }

        public void ConfirmCalibrateValue(int devNum)
        {
            switch (_measType)
            {
                case StrapMeasType.SA600:
                    _sa600.SaveCalibrateValue();
                    break;
            }
        }

        public void CancelCalibrateZero()
        {
            switch (_measType)
            {
                case StrapMeasType.SA600:
                    _sa600.StopCalibrateZero();
                    break;
            }
            _isReceiveMeasData = true;
        }

        public void ClearTotal()
        {
            _isReceiveMeasData = false;
            Thread.Sleep(2000);
            switch (_measType)
            {
                case StrapMeasType.SA600:
                    _sa600.ClearTotal();
                    break;
            }

            _isReceiveMeasData = true;
        }
    }
}
