using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
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.ToolLayout;
using MECF.Framework.Common.TwinCat;
using PunkHPX8_Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PunkHPX8_RT.Devices.Reservoir
{
    public class ANPumpOnRoutine : RoutineBase, IRoutine
    {
        private enum ANPumpStep
        {
            PumpSpeed,
            PumpEnable,
            Delay,
            CheckFlow,
            End
        }
        #region 内部变量
        /// 
        /// 默认泵速
        /// 
        private double _anPumpSpeed = 5000;
        /// 
        /// flow fault hold off时长
        /// 
        private int _flowFaultHoldOffTime = 10000;
        /// 
        /// 阳极最小流量 
        /// 
        private double _anMinFlow = 0.2;
        /// 
        /// Reservoir设备
        /// 
        private DMReservoirDevice _device;
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        public ANPumpOnRoutine(string module) : base(module)
        {
        }
        /// 
        /// 中止
        /// 
        public void Abort()
        {
            Runner.Stop("Manual abort");
        }
        /// 
        /// 监控
        /// 
        /// 
        public RState Monitor()
        {
            Runner.Run(ANPumpStep.PumpEnable,  () => { return ANPumpEnable(); }, _delay_1ms)
                .Delay(ANPumpStep.Delay, _flowFaultHoldOffTime)
                .Run(ANPumpStep.CheckFlow,CheckAllFlow,_delay_1ms)
                .End(ANPumpStep.End, NullFun, _delay_1ms);
            return Runner.Status;
        }
        /// 
        /// Pump Speed
        /// 
        /// 
        /// 
        private bool ANPumpSpeed(double speed)
        {            
            return _device.AnPumpSpeed(speed);
        }
        /// 
        /// Pump enable
        /// 
        /// 
        /// 
        private bool ANPumpEnable()
        {
            return _device.AnPumpOnOperation("",null);
        }
        /// 
        /// 检验所有流量
        /// 
        /// 
        private bool CheckAllFlow()
        {
            double anTotalFlow = _device.ReservoirData.AnFlow;
            if(anTotalFlow<=_anMinFlow)
            {
                _device.AnPumpOff();
                LOG.WriteLog(eEvent.ERR_RESERVOIR, Module, $"total flow {anTotalFlow} is not over {_anMinFlow}");
                return false;
            }
            else
            {
                LOG.WriteLog(eEvent.INFO_RESERVOIR, Module, $"total flow {anTotalFlow} is over {_anMinFlow}");
                return true;
            }
        }
        /// 
        /// 启动
        /// 
        /// 
        /// 
        public RState Start(params object[] objs)
        {
            _device = DEVICE.GetDevice(Module);
            _flowFaultHoldOffTime = SC.GetValue($"Reservoir.{Module}.FlowFaultHoldOffTime");
            return Runner.Start(Module, "AN Pump On");
        }
    }
}