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

namespace FLY.Simulation.Blowing
{
    public class GageAD:ISimulationGageAD
    {
        /// <summary>
        /// 1脉冲 = ? mm
        /// </summary>
        double mmpp = 0.1133;
        public double Mmpp
        {
            get { return mmpp; }
            set
            {
                mmpp = value;
            }
        }

        public Blowing mBlowing;
        /// <summary>
        /// 机架总长
        /// </summary>
        public int PosLen;
        /// <summary>
        /// 膜宽
        /// </summary>
        public int FilmWidth;
        /// <summary>
        /// 膜开始位置
        /// </summary>
        public int FilmBegin;

        /// <summary>
        /// 传感器直径
        /// </summary>
        public int SenserWidth;

        public int[] AirDatas;

        CurveCollection curve;
        public GageAD() 
        {
            curve = new CurveCollection();
            double mm_gagelen = 3000;//设备总长3m
            double mm_filmwidth = 2300;//膜宽2.3m
            double mm_senserwidth = 15;//传感器15mm
            double mm_filmbegin = 300;//mm

            PosLen = 8030;
            double ppmm = PosLen / mm_gagelen;


            FilmWidth = (int)(mm_filmwidth * ppmm);
            FilmBegin = (int)(mm_filmbegin * ppmm);
            SenserWidth = (int)(mm_senserwidth * ppmm);
            mBlowing = new Blowing();
            mBlowing.FilmWidth = FilmWidth;

            AirDatas = new int[PosLen];
            NewAirDatas();
        }
        void NewAirDatas() 
        {
            int a = 20;
            Random r = new Random();
            for (int i = 0; i < AirDatas.Count(); i++)
            {
                AirDatas[i] = r.Next(a) - a/2;
            }
        }
        public void OnPoll(DateTime now) 
        {
            //NewAirDatas();
            mBlowing.OnPoll(now);

        }
        public int GetAD(int pos) 
        {
            return GetAD_1(pos);
            //return GetAD_2(pos);
        }
        int GetAD_1(int pos) 
        {
                int idx = pos;
                if(idx<0)
                    idx = 0;
                else if(idx>=PosLen)
                    idx = PosLen-1;
                
                Random r = new Random();
                int data = r.Next(50) - 25;

                if ((idx >= FilmBegin) && (idx<(FilmBegin+FilmWidth))) 
                {
                    FilmData fd = mBlowing.GetData(idx-FilmBegin);
                    if (fd != null)
                    {
                        data += fd.data1 + fd.data2;
                    }
                }
            
            //求平均值
            int thick = (int)data;
            return curve.Value2AD(thick, CurveCollection.AD2ValueFlag.NoRevised);
        }
        int GetAD_2(int pos)
        {
            int[] datas = new int[SenserWidth];

            for (int i = 0; i < SenserWidth; i++)
            {
                datas[i] = GetAD_1(pos - SenserWidth / 2 + i);
            }
            //求平均值
            return (int)datas.Average();
        }
        /// <summary>
        /// 获取输入口
        /// </summary>
        /// <returns></returns>
        public UInt16 GetInput() 
        {
            UInt16 istatus = 0x0fff;
            if (!mBlowing.IsShieldI9)
            {
                if (mBlowing.CheckLimit0())
                    Misc.MyBase.CLEARBIT(ref istatus, IODefinition.IN_RENZIJIA_0 - 1);
            }
            if (mBlowing.CheckLimit1())
                Misc.MyBase.CLEARBIT(ref istatus, IODefinition.IN_RENZIJIA_1-1);

            if (mBlowing.CheckRoll())
                Misc.MyBase.CLEARBIT(ref istatus, IODefinition.IN_RSENSOR-1);

            return istatus;
        }


        public int GetPosition2()
        {
            return 0;
        }
        public int GetSpeed2() 
        {
            return 0;
        }
    }
}


