using Aitex.Core.RT.Device;
using Aitex.Core.RT.Routine;
using Aitex.Core.RT.SCCore;
using MECF.Framework.Common.Beckhoff.ModuleIO;
using MECF.Framework.Common.Routine;
using MECF.Framework.Common.TwinCat;
using CyberX8_Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MECF.Framework.Common.IOCore;
using MECF.Framework.Common.ToolLayout;
namespace CyberX8_RT.Devices.Metal
{
    public class StandardHotMetalCellPumpRoutine : RoutineBase, IRoutine
    {
        private enum PumpStep
        {
            Pump,
            Delay,
            CheckFlow,
            End
        }
        #region 常量 
        private const string CELL_PUMP = "CellPump";
        #endregion
        #region 内部变量
        private int _cellFlowFaultHoldOffTime = 5000;
        private double _minPumpFlow = 0.2;
        private bool _pumpOn = false;
        private bool _isSignalPump = false;
        private StandardHotMetalDevice _device;
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        public StandardHotMetalCellPumpRoutine(string module) : base(module)
        {
        }
        /// 
        /// 中止
        /// 
        public void Abort()
        {
            Runner.Stop("Manual Abort");
        }
        /// 
        /// 监控
        /// 
        /// 
        public RState Monitor()
        {
            Runner.RunIf(PumpStep.Pump, _isSignalPump, ExecutePump, () => { return _device.MetalDeviceData.CellPump ==_pumpOn; }, _delay_1s)
                .Delay(PumpStep.Delay, _cellFlowFaultHoldOffTime)
                .RunIf(PumpStep.CheckFlow, _pumpOn, CheckFlow, NullFun, _delay_1ms)
                .End(PumpStep.End, NullFun, _delay_1ms);
            return Runner.Status;
        }
        /// 
        /// 执行Pump开或关
        /// 
        /// 
        private bool ExecutePump()
        {
            string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{CELL_PUMP}");
            return IOModuleManager.Instance.WriteIoValue(ioName, _pumpOn);
        }
        /// 
        /// 检验Flow
        /// 
        /// 
        private bool CheckFlow()
        {
            return _device.MetalDeviceData.CellFlow >= _minPumpFlow;
        }
        /// 
        /// 启动
        /// 
        /// 
        /// 
        public RState Start(params object[] objs)
        {
            _pumpOn=(bool)objs[0];
            _device = DEVICE.GetDevice(Module.ToString());
            _cellFlowFaultHoldOffTime = SC.GetValue("Metal.CellFlowFaultHoldOffTime");
            _minPumpFlow = SC.GetValue("Metal.MinPumpFlow");
            if(!_pumpOn)
            {
                _cellFlowFaultHoldOffTime = 0;
            }
            MetalItem metalItem = MetalItemManager.Instance.GetMetalItem(Module);
            if (metalItem != null && metalItem.DepPump)
            {
                _isSignalPump = true;
            }
            return Runner.Start(Module.ToString(), "Cell Pump");
            
        }
    }
}