using Aitex.Core.RT.Device;
using Aitex.Core.RT.Routine;
using MECF.Framework.Common.Routine;
using CyberX8_Core;
using CyberX8_RT.Devices.LinMot;
using CyberX8_RT.Devices.Prewet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CyberX8_RT.Modules.Prewet
{
    public class PrewetInitializeRoutine : RoutineBase, IRoutine
    {
        private enum InitializeStep
        {
            PumpValveOff,
            ResetLinmot,
            WaitReset,
            End
        }
        #region 内部变量
        private PrewetDevice _prewetDevice;
        private LinMotAxis _linmotAxis;
        #endregion
        /// 
        /// 当前执行到哪一步
        /// 
        public string CurrentStateMachine
        {
            get { return Runner.CurrentStep.ToString(); ; }
        }
        /// 
        /// 构造函数
        /// 
        /// 
        public PrewetInitializeRoutine(string module,LinMotAxis linMotAxis) : base(module)
        {
            _linmotAxis = linMotAxis;
        }
        /// 
        /// 中止
        /// 
        public void Abort()
        {
            Runner.Stop("Manual Abort");
        }
        /// 
        /// 监控
        /// 
        /// 
        public RState Monitor()
        {
            Runner.Run(InitializeStep.PumpValveOff, () => { return _prewetDevice.PumpValveClose(); }, () => { return !_prewetDevice.PrewetPumpData.PumpValve && !_prewetDevice.PrewetPumpData.PumpEnable; }, _delay_5s)
                .Run(InitializeStep.ResetLinmot, ResetLinmot, _delay_1ms)
                .WaitWithStopCondition(InitializeStep.WaitReset, () => { return _linmotAxis.Status == RState.End; }, () => { return _linmotAxis.Status == RState.Failed; })
                .End(InitializeStep.End, NullFun, _delay_1ms);
            return Runner.Status;
        }
        /// 
        /// Reset Linmot
        /// 
        /// 
        private bool ResetLinmot()
        {
            return _linmotAxis.ResetOperation("", true);
        }
        /// 
        /// 启动
        /// 
        /// 
        /// 
        /// 
        public RState Start(params object[] objs)
        {
            _prewetDevice = DEVICE.GetDevice(Module);
            return Runner.Start(Module, "Initialize");
        }
    }
}