using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using Aitex.Core.Util;
using PunkHPX8_Core;
using PunkHPX8_RT.Modules;
using PunkHPX8_RT.Modules.SRD;
using PunkHPX8_RT.Modules.Transporter;
using MECF.Framework.Common.Beckhoff.ModuleIO;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.Routine;
using MECF.Framework.Common.Utilities;
using System.Runtime.InteropServices;
using PunkHPX8_RT.Devices.AXIS;
namespace PunkHPX8_RT.Devices.Safety
{
    internal class SafetyAllOnRoutine : RoutineBase, IRoutine
    {
        private enum SafetyAllOnStep
        {
            VpwCell1SwitchOn,
            WaitSwitchOnVpwCell1,
            VpwCell2SwitchOn,
            WaitSwitchOnVpwCell2,
            PlatingCell1RotationSwitchOn,
            WaitSwitchOnPlatingCell1Rotation,
            PlatingCell2RotationSwitchOn,
            WaitSwitchOnPlatingCell2Rotation,
            PlatingCell3RotationSwitchOn,
            WaitSwitchOnPlatingCell3Rotation,
            PlatingCell4RotationSwitchOn,
            WaitSwitchOnPlatingCell4Rotation,
            PlatingCell12LiftSwitchOn,
            WaitSwitchOnPlatingCell12Lift,
            PlatingCell34LiftSwitchOn,
            WaitSwitchOnPlatingCell34Lift,
            SwitchOnSRD1,
            WaitSwitchOnSRD1,
            SwitchOnSRD2,
            WaitSwitchOnSRD2,
            End
        }
        #region 内部变量
        private SafetyDevice _device;
        private JetAxisBase _vpwCell1Rotation;
        private JetAxisBase _vpwCell2Rotation;
        private JetAxisBase _platingCell1Rotation;
        private JetAxisBase _platingCell2Rotation;
        private JetAxisBase _platingCell3Rotation;
        private JetAxisBase _platingCell4Rotation;
        private JetAxisBase _platingCell12Lift;
        private JetAxisBase _platingCell34Lift;
        private JetAxisBase _srd1Rotation;
        private JetAxisBase _srd2Rotation;
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        public SafetyAllOnRoutine(string module) : base(module)
        {
            
        }
        /// 
        /// 中止
        /// 
        public void Abort()
        {
            Runner.Stop($"Safety All On Abort");
        }
        /// 
        /// 监控
        /// 
        /// 
        public RState Monitor()
        {
            Runner.RunIf(SafetyAllOnStep.VpwCell1SwitchOn,ModuleHelper.IsInstalled(ModuleName.VPW1),()=>SwitchOnAxis(_vpwCell1Rotation),_delay_1ms)
                .WaitWithStopConditionIf(SafetyAllOnStep.WaitSwitchOnVpwCell1, ModuleHelper.IsInstalled(ModuleName.VPW1),
                    ()=>CheckSwitchOn(_vpwCell1Rotation),()=>CheckSwitchOnStopStatus(_vpwCell1Rotation))
                .RunIf(SafetyAllOnStep.VpwCell2SwitchOn, ModuleHelper.IsInstalled(ModuleName.VPW2), () => SwitchOnAxis(_vpwCell2Rotation), _delay_1ms)
                .WaitWithStopConditionIf(SafetyAllOnStep.WaitSwitchOnVpwCell2, ModuleHelper.IsInstalled(ModuleName.VPW2),
                    () => CheckSwitchOn(_vpwCell2Rotation), () => CheckSwitchOnStopStatus(_vpwCell2Rotation))
                .End(SafetyAllOnStep.End, NullFun, _delay_1ms);
            return Runner.Status;
        }
        /// 
        /// 电机SwitchOn
        /// 
        /// 
        /// 
        private bool SwitchOnAxis(JetAxisBase axis)
        {
            return axis.SwitchOn();
        }
        /// 
        /// 检验电机是否上电
        /// 
        /// 
        /// 
        private bool CheckSwitchOn(JetAxisBase axis) 
        {
            return axis.IsSwitchOn && axis.Status == RState.End;
        }
        /// 
        /// 检验电机上电是否异常
        /// 
        /// 
        /// 
        private bool CheckSwitchOnStopStatus(JetAxisBase axis)
        {
            return axis.Status == RState.Failed;
        }
        /// 
        /// 启动
        /// 
        /// 
        /// 
        public RState Start(params object[] objs)
        {
            _device = DEVICE.GetDevice(Module);
            _vpwCell1Rotation = DEVICE.GetDevice($"{ModuleName.VPW1}.Rotation");
            _vpwCell2Rotation = DEVICE.GetDevice($"{ModuleName.VPW2}.Rotation");
            return Runner.Start(Module, $"Safety All On");
        }
    }
}