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.Tasks;
namespace CyberX8_RT.Modules.Prewet
{
    public class PrepareToTransferRoutine : RoutineBase, IRoutine
    {
        private enum PrepareToTransferStep
        {
            PumpValveOff,
            LinmotDisable,
            End
        }
        #region 内部变量
        private LinMotAxis _linmotAxis;
        private PrewetDevice _prewetDevice;
        private int _timeout = 5000;
        private bool _isNeedStopLinmot;
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        public PrepareToTransferRoutine(string module,LinMotAxis linMotAxis) : base(module)
        {
            _linmotAxis = linMotAxis;
        }
        public void Abort()
        {
            Runner.Stop("Manual Abort");
        }
        /// 
        /// 监控
        /// 
        /// 
        /// 
        public RState Monitor()
        {
            Runner.Run(PrepareToTransferStep.PumpValveOff, () => { return _prewetDevice.PumpValveClose(); }, () => { return !_prewetDevice.PrewetPumpData.PumpValve && !_prewetDevice.PrewetPumpData.PumpEnable; }, _timeout)
                .RunIf(PrepareToTransferStep.LinmotDisable,_isNeedStopLinmot,() => { return _linmotAxis.SwitchOff(); }, () => { return _linmotAxis.IsDisabled; }, _delay_2s)
                .End(PrepareToTransferStep.End, NullFun, _delay_1ms);
            return Runner.Status;
        }
        /// 
        /// 启动
        /// 
        /// 
        /// 
        /// 
        public RState Start(params object[] objs)
        {
            _prewetDevice = DEVICE.GetDevice(Module);
            _isNeedStopLinmot = objs.Length > 0 ? ((bool)objs[0]) : true;
            return Runner.Start(Module, "PrepareToTransfer");
        }
    }
}