using FLY.Thick.Base.Common;
using FLY.Thick.Base.Server;
using FlyADBase;
using Misc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.Thick.FilmCasting.Server
{
    /// <summary>
    /// 扫描 的运动控制
    /// 所有周期，都是以时间计算
    /// </summary>
    public class ScanMotion : INotifyPropertyChanged
    {

        #region 成员变量

        /// <summary>
        /// 归原点 样品 间隔时间，单位分钟， 小于1无效，只在开始时动作一次
        /// </summary>
        public int OrgInterval { get; set; } = 20;

        /// <summary>
        /// 扫描范围
        /// </summary>
        public RangeStruct ScanRange = RangeStruct.InvalidValue;

        /// <summary>
        /// 扫描速度, pps
        /// </summary>
        public UInt32 Velocity { get; set; } = 500;

        public DateTime StartDataEndTime;

        /// <summary>
        /// 扫描过程中，数据推送事件
        /// </summary>
        public event EndEventHandler DataPosEvent;

        /// <summary>
        /// 扫描完一次，数据推送事件
        /// </summary>
        public event EndEventHandler DataEndEvent;

        public event PropertyChangedEventHandler PropertyChanged;

        enum CTRLST
        {
            /// <summary>
            /// 动作完成等待
            /// </summary>
            IDLE,
            /// <summary>
            /// 判断是否归0
            /// </summary>
            ORG,
            /// <summary>
            /// 正在归0
            /// </summary>
            ORG_ING,
            /// <summary>
            /// 前进取样开始
            /// </summary>
            FORW_SAMPLE,
            /// <summary>
            /// 前进取样准备，涵盖扫描范围，需要推送扫描数据 ***伺服电机使用***
            /// </summary>
            FORW_SAMPLE_READY,
            /// <summary>
            /// 前进取样 ***伺服电机使用***
            /// </summary>
            FORW_SAMPLE_ING,
            /// <summary>
            /// 前进取样，涵盖扫描范围，需要推送扫描数据 ***VF0使用***
            /// </summary>
            FORW_SAMPLE_ING2,

            /// <summary>
            /// 后退取样开始
            /// </summary>
            BACKW_SAMPLE,
            /// <summary>
            /// 后退取样准备，涵盖扫描范围，需要推送扫描数据
            /// </summary>
            BACKW_SAMPLE_READY,
            /// <summary>
            /// 后退取样
            /// </summary>
            BACKW_SAMPLE_ING,
            /// <summary>
            /// 后退取样，涵盖扫描范围，需要推送扫描数据
            /// </summary>
            BACKW_SAMPLE_ING2,
            /// <summary>
            /// 从归0，直接到取样准备
            /// </summary>
            BACKW_SAMPLE_READY2,

            /// <summary>
            /// 前进扫描开始
            /// </summary>
            FORW,
            /// <summary>
            /// 前进扫描等待
            /// </summary>
            FORW_ING,

            /// <summary>
            /// 后退扫描开始
            /// </summary>
            BACKW,
            /// <summary>
            /// 后退扫描等待
            /// </summary>
            BACKW_ING
        }



        /// <summary>
        /// 当前状态
        /// </summary>
        CTRLST State { get; set; }




        /// <summary>
        /// 每种动作
        /// </summary>
        enum SubMotion
        {
            /// <summary>
            /// 空
            /// </summary>
            IDLE,
            /// <summary>
            /// 归0
            /// </summary>
            ORG,
            /// <summary>
            /// 扫描
            /// </summary>
            SCAN
        }

        SubMotion CurrSubMotion = SubMotion.IDLE;
        #endregion

        protected GSample gsample;
        protected InitParam initParam;
        protected OBJComponents.Server.WarningSystem2 warningSystem;
        /// <summary>
        /// 归0开始时间点，用于计算什么时候需要归0
        /// </summary>
        private TimeSpan orgStartTime;
        bool isFromZero = false;
        /// <summary>
        /// 使用timer.Elapsed 代替 DateTime.Now 使用
        /// </summary>
        private Stopwatch timer = new Stopwatch();
        FlyAD7 mFlyAD;


        /// <summary>
        /// 
        /// </summary>
        /// <param name="flyad"></param>
        /// <param name="gsample"></param>
        public ScanMotion() { 
        
        }

        public void Init(FlyAD7 flyAd, GSample gsample, InitParam initParam, OBJComponents.Server.WarningSystem2 warningSystem)
        {
            this.mFlyAD = flyAd;
            this.gsample = gsample;
            this.initParam = initParam;
            this.warningSystem = warningSystem;


            Misc.BindingOperations.SetBinding(initParam, nameof(initParam.AutoOrgInterval), this, nameof(OrgInterval));
            Misc.BindingOperations.SetBinding(initParam, nameof(initParam.VScan), this, nameof(Velocity));
        }
        /// <summary>
        /// 开始扫描
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public bool Start(int start, int end)
        {
            ScanRange = new RangeStruct(start, end);

            orgStartTime = TimeSpan.Zero;
            CurrSubMotion = SubMotion.ORG;
            State = CTRLST.IDLE;

            if (gsample != null)
                gsample.Reset(start, end);

            timer.Restart();


            if (!gsample.Enable || !gsample.Samples.Any(s => s.Enable))
            {
                warningSystem.Add(
                        ERRNOs.Instance.SCAN_ERRNO_FATAL.Code,
                       ERRNOs.Instance.SCAN_ERRNO_FATAL.Descrption + ":样品采集未启动");
                Stop();
                return false;
            }

            //复位错误
            warningSystem.Remove(
                ERRNOs.Instance.SCAN_ERRNO_FATAL.Code);

            //开始扫描事件
            return true;
        }
        private void FlyAD_DataEndEvent(DIRECTION direction) 
        {
            int pos1, pos2;

            pos1 = ScanRange.Begin;
            pos2 = ScanRange.End;
            int start_grid = pos1 / mFlyAD.PosOfGrid;
            int end_grid = pos2 / mFlyAD.PosOfGrid;

            if (start_grid < 0)
                start_grid = 0;

            int len = end_grid - start_grid + 1;
            int[] dat;
            mFlyAD.GetGrid(direction, start_grid, len, out dat);

            DataEndEvent?.Invoke(this, new MiniGridEventArgs()
            {
                grid_start = start_grid,
                direction = direction,
                posOfGrid = mFlyAD.PosOfGrid,
                buf = dat
            });


            //var timeGridAdv2 = mFlyAD.GetTimeGridAdv2Event(mStartDataEndTime);
            //DataEndEvent?.Invoke(this, timeGridAdv2);
        }
        private void FlyAD_SampleEvent(DIRECTION direciton) {

            //var timeGridAdv2 = mFlyAD.GetTimeGridAdv2Event(mStartDataEndTime);
            ////转为 grid
            //int posOfGrid = initParam.PosOfGrid;
            //int gridLen = initParam.PosLength / posOfGrid;

            ////转为grid图
            //int[] data = TimeGridAdvHelperExt.ToGrid(timeGridAdv2.AdList, timeGridAdv2.PosList, posOfGrid, gridLen);

            //取样
            gsample.Do(direciton);
        }

        /// <summary>
        /// 停止扫描
        /// </summary>
        public void Stop()
        {
            mFlyAD.MiniGridEvent -= Flyad_MiniGridEvent;
            mFlyAD.Stop();

            timer.Stop();
            CurrSubMotion = SubMotion.IDLE;
        }


        /// <summary>
        /// 归0动作 与 采样
        /// </summary>
        bool OnPoll_org()
        {
            /// Org/Idle
            /// ↓
            /// OrgIng
            /// ↓
            /// ForwSample─────不需要正向采样─────→BackwSample
            /// │                                                        
            /// ├───────→ForwSampleIng2──→ForwSampleEnd               
            /// ↓                                                    
            /// ForwSampleReady                        
            /// ↓                                                
            /// ForwSampleIng                                     
            /// ↓                                               
            /// ForwSampleEnd                                                                
            /// ↓  
            /// BackwSample─────不需要反向采样─────→Idle
            /// │
            /// │                                      
            /// ├──────从ForwSample直接过来───┬──────→ BackSampleReady2
            /// │                                      │                     ↓
            /// │                                      └──────→  BackwSampleIng   
            /// │           
            /// ├───────→BackwSampleIng2──→BackwSampleEnd   
            /// ↓                            
            /// BackwSampleReady
            /// ↓                    
            /// BackwSampleIng 
            /// ↓                           
            /// BackwSampleEnd
            /// ↓  
            /// Idle 
            switch (State)
            {
                case CTRLST.IDLE:
                case CTRLST.ORG:
                    {
                        mFlyAD.Origin();
                        State = CTRLST.ORG_ING;
                        break;
                    }
                case CTRLST.ORG_ING:
                    {
                        if (!mFlyAD.IsFinish)
                            break;
                        if (mFlyAD.DriveStatus == DRIVE_MAN_STATUS.STOP_MANUAL)//被手动停止了
                            return true;
                        if (mFlyAD.IsTimeToPushTimeGridAdv)//等待 timegridadv 完成
                            break;

                        State = CTRLST.FORW_SAMPLE;
                    }
                    break;
                case CTRLST.FORW_SAMPLE:
                    {
                        Range r = gsample.GetSampleRange(Misc.DIRECTION.FORWARD);
                        if (!r.IsValid)
                        {
                            //不需要前进样品采集
                            //不用前进取样，转到后退取样
                            State = CTRLST.BACKW_SAMPLE;
                            //从归零直接到后退取样
                            isFromZero = true;
                            break;
                        }
                        isFromZero = false;
                        
                        //要变速，分开两段
                        
                        int to = r.Begin;
                        State = CTRLST.FORW_SAMPLE_READY;
                        

                        //需要边界获取,扫描数据
                        StartDataEndTime = mFlyAD.Now;

                        //需要显示动态扫描图
                        mFlyAD.MiniGridEvent += Flyad_MiniGridEvent;

                        //以扫描速度到目的地
                        mFlyAD.SetVelocity(Velocity);
                        mFlyAD.Runto(to);
                    }
                    break;
                case CTRLST.FORW_SAMPLE_READY:
                    {
                        if (!mFlyAD.IsFinish)
                            break;
                        if (mFlyAD.DriveStatus == DRIVE_MAN_STATUS.STOP_MANUAL)
                            return true;
                        if (mFlyAD.IsTimeToPushTimeGridAdv)//等待 timegridadv 完成
                            break;

                        mFlyAD.MiniGridEvent -= Flyad_MiniGridEvent;

                        FlyAD_DataEndEvent(DIRECTION.FORWARD);

                        
                        Range r = gsample.GetSampleRange(Misc.DIRECTION.FORWARD);
                        int to = r.End;
                        //以扫描速度到目的地
                        mFlyAD.SetVelocity(gsample.Velocity);

                        //需要采样数据
                        StartDataEndTime = mFlyAD.Now;

                        mFlyAD.Runto(to);
                        State = CTRLST.FORW_SAMPLE_ING;
                    }
                    break;
                case CTRLST.FORW_SAMPLE_ING:
                    {
                        if (!mFlyAD.IsFinish)
                            break;
                        if (mFlyAD.DriveStatus == DRIVE_MAN_STATUS.STOP_MANUAL)
                            return true;
                        if (mFlyAD.IsTimeToPushTimeGridAdv)//等待 timegridadv 完成
                            break;

                        FlyAD_SampleEvent(DIRECTION.FORWARD);

                        State = CTRLST.BACKW_SAMPLE;
                    }
                    break;
                case CTRLST.BACKW_SAMPLE:
                    {
                        Range r = gsample.GetSampleRange(Misc.DIRECTION.BACKWARD);
                        if (!r.IsValid)
                        {
                            //归0与取样动作完成
                            State = CTRLST.IDLE;
                            break;
                        }

                        //后退取样很特别，需要判断当前位置，有可能刚才归0，直接跳到这来而已
                        if (isFromZero)
                        {
                            //刚才归0，直接跳到这来而已

                            //要变速，分开两段
                            mFlyAD.SetVelocity(Velocity);
                            mFlyAD.Runto(r.End);
                            //不需要AD测量
                            State = CTRLST.BACKW_SAMPLE_READY2;
                            
                        }
                        else
                        {
                            //正常的情况，刚做完正向样品采集，现在反向

                            //要变速，分开两段
                            
                            mFlyAD.MiniGridEvent += Flyad_MiniGridEvent;
                            StartDataEndTime = mFlyAD.Now;

                            mFlyAD.SetVelocity(Velocity);
                            mFlyAD.Runto(r.End);
                            State = CTRLST.BACKW_SAMPLE_READY;
                            
                        }
                    }
                    break;
                case CTRLST.BACKW_SAMPLE_READY:
                    {
                        //从归0过来， 完成DataEnd 与样品采样
                        if (!mFlyAD.IsFinish)
                            break;
                        if (mFlyAD.DriveStatus == DRIVE_MAN_STATUS.STOP_MANUAL)
                            return true;
                        if (mFlyAD.IsTimeToPushTimeGridAdv)//等待 timegridadv 完成
                            break;
                        mFlyAD.MiniGridEvent -= Flyad_MiniGridEvent;
                        FlyAD_DataEndEvent(DIRECTION.BACKWARD);

                        int to;
                        Range r = gsample.GetSampleRange(Misc.DIRECTION.BACKWARD);
                        to = r.Begin;
                        //以扫描速度到目的地
                        mFlyAD.SetVelocity(gsample.Velocity);
                        StartDataEndTime = mFlyAD.Now;
                        mFlyAD.Runto(to);
                        State = CTRLST.BACKW_SAMPLE_ING;
                    }
                    break;
                case CTRLST.BACKW_SAMPLE_ING:
                    {
                        if (!mFlyAD.IsFinish)
                            break;
                        if (mFlyAD.DriveStatus == DRIVE_MAN_STATUS.STOP_MANUAL)
                            return true;
                        if (mFlyAD.IsTimeToPushTimeGridAdv)//等待 timegridadv 完成
                            break;
                        FlyAD_SampleEvent(DIRECTION.BACKWARD);
                        //完成！！！
                        State = CTRLST.IDLE;
                    }
                    break;

                case CTRLST.BACKW_SAMPLE_READY2:
                    {
                        if (!mFlyAD.IsFinish)
                            break;
                        if (mFlyAD.DriveStatus == DRIVE_MAN_STATUS.STOP_MANUAL)
                            return true;

                        int to;
                        Range r = gsample.GetSampleRange(Misc.DIRECTION.BACKWARD);
                        to = r.Begin;
                        //以扫描速度到目的地
                        mFlyAD.SetVelocity(gsample.Velocity);
                        StartDataEndTime = mFlyAD.Now;

                        mFlyAD.Runto(to);
                        State = CTRLST.BACKW_SAMPLE_ING;
                    }
                    break;
            }

            return false;
        }


        /// <summary>
        /// 归0动作 与 采样
        /// </summary>
        bool OnPoll_org_vf0()
        {
            /// Org/Idle
            /// ↓
            /// OrgIng
            /// ↓
            /// ForwSample─────不需要正向采样─────→BackwSample
            /// │                                                        
            /// ├───────→ForwSampleIng2──→ForwSampleEnd               
            /// ↓                                                    
            /// ForwSampleReady                        
            /// ↓                                                
            /// ForwSampleIng                                     
            /// ↓                                               
            /// ForwSampleEnd                                                                
            /// ↓  
            /// BackwSample─────不需要反向采样─────→Idle
            /// │
            /// │                                      
            /// ├──────从ForwSample直接过来───┬──────→ BackSampleReady2
            /// │                                      │                     ↓
            /// │                                      └──────→  BackwSampleIng   
            /// │           
            /// ├───────→BackwSampleIng2──→BackwSampleEnd   
            /// ↓                            
            /// BackwSampleReady
            /// ↓                    
            /// BackwSampleIng 
            /// ↓                           
            /// BackwSampleEnd
            /// ↓  
            /// Idle 
            switch (State)
            {
                case CTRLST.IDLE:
                case CTRLST.ORG:
                    {
                        mFlyAD.Origin();
                        State = CTRLST.ORG_ING;
                        break;
                    }
                case CTRLST.ORG_ING:
                    {
                        if (!mFlyAD.IsFinish)
                            break;
                        if (mFlyAD.DriveStatus == DRIVE_MAN_STATUS.STOP_MANUAL)//被手动停止了
                            return true;

                        State = CTRLST.FORW_SAMPLE;
                    }
                    break;
                case CTRLST.FORW_SAMPLE:
                    {
                        Range r = gsample.GetSampleRange(Misc.DIRECTION.FORWARD);
                        if (!r.IsValid)
                        {
                            //不需要前进样品采集
                            //不用前进取样，转到后退取样
                            State = CTRLST.BACKW_SAMPLE;
                            //从归零直接到后退取样
                            isFromZero = true;
                            break;
                        }
                        isFromZero = false;
                        
                        //直接去到取样的位置
                        int to = r.End;
                        State = CTRLST.FORW_SAMPLE_ING2;

                        //需要边界获取,扫描数据
                        StartDataEndTime = mFlyAD.Now;

                        //需要显示动态扫描图
                        mFlyAD.MiniGridEvent += Flyad_MiniGridEvent;

                        //以扫描速度到目的地
                        mFlyAD.SetVelocity(Velocity);
                        mFlyAD.Runto(to);
                    }
                    break;

                case CTRLST.FORW_SAMPLE_ING2:
                    {
                        //从归0过来， 完成DataEnd 与样品采样
                        if (!mFlyAD.IsFinish)
                            break;
                        if (mFlyAD.DriveStatus == DRIVE_MAN_STATUS.STOP_MANUAL)
                            return true;
                        if (mFlyAD.IsTimeToPushTimeGridAdv)//等待 timegridadv 完成
                            break;
                        mFlyAD.MiniGridEvent -= Flyad_MiniGridEvent;
                        FlyAD_DataEndEvent(Misc.DIRECTION.FORWARD);
                        FlyAD_SampleEvent(Misc.DIRECTION.FORWARD);

                        State = CTRLST.BACKW_SAMPLE;
                    }
                    break;
                case CTRLST.BACKW_SAMPLE:
                    {
                        Range r = gsample.GetSampleRange(Misc.DIRECTION.BACKWARD);
                        if (!r.IsValid)
                        {
                            //归0与取样动作完成
                            State = CTRLST.IDLE;
                            break;
                        }

                        //后退取样很特别，需要判断当前位置，有可能刚才归0，直接跳到这来而已
                        if (isFromZero)
                        {
                            //刚才归0，直接跳到这来而已

                            if (mFlyAD.Position > r.End)
                            {
                                //当前在样品的后面，
                                //直接去到取样的位置
                                StartDataEndTime = DateTime.Now;
                                mFlyAD.Runto(r.Begin);
                                State = CTRLST.BACKW_SAMPLE_ING;
                                
                            }
                            else
                            {
                                //情况很异常，
                                //当前在样品的中间或前面

                                mFlyAD.SetVelocity(Velocity);
                                mFlyAD.Runto(r.End);
                                //不需要AD测量
                                State = CTRLST.BACKW_SAMPLE_READY2;
                            }
                        }
                        else
                        {
                            //正常的情况，刚做完正向样品采集，现在反向
                            mFlyAD.MiniGridEvent += Flyad_MiniGridEvent;
                            StartDataEndTime = mFlyAD.Now;

                            //直接去到取样的位置
                            mFlyAD.Runto(r.Begin);
                            State = CTRLST.BACKW_SAMPLE_ING2;
                        }
                    }
                    break;
                case CTRLST.BACKW_SAMPLE_ING:
                    {
                        if (!mFlyAD.IsFinish)
                            break;
                        if (mFlyAD.DriveStatus == DRIVE_MAN_STATUS.STOP_MANUAL)
                            return true;
                        if (mFlyAD.IsTimeToPushTimeGridAdv)//等待 timegridadv 完成
                            break;
                        FlyAD_SampleEvent(Misc.DIRECTION.BACKWARD);
                        //完成！！！
                        State = CTRLST.IDLE;
                    }
                    break;
                case CTRLST.BACKW_SAMPLE_ING2:
                    {
                        if (!mFlyAD.IsFinish)
                            break;
                        if (mFlyAD.DriveStatus == DRIVE_MAN_STATUS.STOP_MANUAL)
                            return true;
                        if (mFlyAD.IsTimeToPushTimeGridAdv)//等待 timegridadv 完成
                            break;
                        mFlyAD.MiniGridEvent -= Flyad_MiniGridEvent;
                        FlyAD_SampleEvent(DIRECTION.BACKWARD);
                        FlyAD_DataEndEvent(DIRECTION.BACKWARD);
                        //完成！！！
                        State = CTRLST.IDLE;
                    }
                    break;
                case CTRLST.BACKW_SAMPLE_READY2:
                    {
                        if (!mFlyAD.IsFinish)
                            break;
                        if (mFlyAD.DriveStatus == DRIVE_MAN_STATUS.STOP_MANUAL)
                            return true;

                        int to;
                        Range r = gsample.GetSampleRange(Misc.DIRECTION.BACKWARD);
                        to = r.Begin;
                        //以扫描速度到目的地
                        mFlyAD.SetVelocity(gsample.Velocity);
                        StartDataEndTime = mFlyAD.Now;

                        mFlyAD.Runto(to);
                        State = CTRLST.BACKW_SAMPLE_ING;
                    }
                    break;
            }

            return false;
        }

        /// <summary>
        /// 扫描 边界查找 动作
        /// </summary>
        bool OnPoll_scan()
        {
            switch (State)
            {
                case CTRLST.IDLE:
                case CTRLST.FORW:
                    {
                        
                        int to = ScanRange.End;
                        mFlyAD.SetVelocity(Velocity);


                        StartDataEndTime = mFlyAD.Now;
                        mFlyAD.MiniGridEvent+= Flyad_MiniGridEvent;
                        mFlyAD.Runto(to);

                        State = CTRLST.FORW_ING;
                        
                    }
                    break;
                case CTRLST.FORW_ING:
                    {

                        if (!mFlyAD.IsFinish)
                            break;
                        if (mFlyAD.DriveStatus == DRIVE_MAN_STATUS.STOP_MANUAL)//被手动停止了
                            return true;
                        if (mFlyAD.IsTimeToPushTimeGridAdv)//等待 timegridadv 完成
                            break;
                        mFlyAD.MiniGridEvent -= Flyad_MiniGridEvent;

                        FlyAD_DataEndEvent(DIRECTION.FORWARD);
                        State = CTRLST.BACKW;
                    }
                    break;
                case CTRLST.BACKW:
                    {
                        int to = ScanRange.Begin;
                        mFlyAD.SetVelocity(Velocity);

                        StartDataEndTime = mFlyAD.Now;
                        mFlyAD.MiniGridEvent += Flyad_MiniGridEvent;

                        mFlyAD.Runto(to);
                        State = CTRLST.BACKW_ING;
                    }
                    break;
                case CTRLST.BACKW_ING:
                    {
                        if (!mFlyAD.IsFinish)
                            break;
                        if (mFlyAD.DriveStatus == DRIVE_MAN_STATUS.STOP_MANUAL)//被手动停止了
                            return true;
                        if (mFlyAD.IsTimeToPushTimeGridAdv)//等待 timegridadv 完成
                            break;

                        FlyAD_DataEndEvent(DIRECTION.BACKWARD);
                        mFlyAD.MiniGridEvent -= Flyad_MiniGridEvent;
                        State = CTRLST.IDLE;
                    }
                    break;
            }
            return false;
        }

        /// <summary>
        /// 返回 false, 还没完成， true 已经完成
        /// </summary>
        public bool OnPoll()
        {
            switch (CurrSubMotion)
            {
                case SubMotion.ORG:
                    {
                        if (mFlyAD.MotorType == FlyADBase.MOTORTYPE.VF0)
                        {
                            if (OnPoll_org_vf0())
                                return true;
                        }
                        else
                        {
                            if (OnPoll_org())
                                return true;
                        }
                        if (State == CTRLST.IDLE)
                        {
                            //动作完成
                            CurrSubMotion = SubMotion.IDLE;
                            if (gsample.OK())//只有取样成功，才能结束！！！！
                                orgStartTime = timer.Elapsed;
                        }
                    }
                    break;
                case SubMotion.SCAN:
                    {
                        if (OnPoll_scan())
                            return true;
                        if (State == CTRLST.IDLE)
                        {
                            //动作完成
                            CurrSubMotion = SubMotion.IDLE;
                        }
                    }
                    break;
            }
            if (CurrSubMotion == SubMotion.IDLE)
            {
                //判断时间，进入状态！！！！！
                if (timer.Elapsed >= (orgStartTime + TimeSpan.FromMinutes(OrgInterval)))
                {
                    CurrSubMotion = SubMotion.ORG;
                }
                else
                {
                    CurrSubMotion = SubMotion.SCAN;
                }
            }
            return false;
        }

        private void Flyad_MiniGridEvent(object sender, MiniGridEventArgs e)
        {
            DataPosEvent?.Invoke(this, e);
        }
    }
}
