using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.SCCore;
using CyberX8_RT.Devices.Metal;
using MECF.Framework.Common.CommonData.Metal;
using MECF.Framework.Common.CommonData.Reservoir;
using MECF.Framework.Common.RecipeCenter;
using MECF.Framework.Common.ToolLayout;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace CyberX8_RT.Devices.Reservoir
{
    public class ReservoirPumpSpeedHelper
    {
        #region 内部变量
        /// 
        /// 时间
        /// 
        private DateTime _updateTime=DateTime.Now;
        /// 
        /// 模块名称
        /// 
        private string _moduleName;
        /// 
        /// AN流量总和
        /// 
        private double _anTotalFlow = 0;
        /// 
        /// CA流量总和
        /// 
        private double _caTotalFlow = 0;
        /// 
        /// 设备对象
        /// 
        StandardHotReservoirDevice _device;
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        public ReservoirPumpSpeedHelper(string moduleName, StandardHotReservoirDevice reservoirDevice)
        {
            _moduleName = moduleName;
            _device = reservoirDevice;
        }
        /// 
        /// 监控
        /// 
        public void Monitor(ResRecipe resRecipe)
        {
            int cellFlowUpdatePeriod = SC.GetValue("Reservoir.CellFlowUpdatePeriod");
            if(DateTime.Now.Subtract(_updateTime).TotalSeconds > cellFlowUpdatePeriod)
            {
                _updateTime = DateTime.Now;
                AdjustCAPumpSpeed(resRecipe);
            }
        }
        
        /// 
        /// 调节阴极泵速
        /// 
        private void AdjustCAPumpSpeed(ResRecipe resRecipe)
        {
            if (_device.ReservoirData.RegulatePumpSignalIn)
            {
                double caPumpSpeed = _device.ReservoirData.RegulatePumpSpeed;
                double averageCAFlow = GetAverageCAFlow();
                if (averageCAFlow == 0)
                {
                    return;
                }
                double caPumpMaxSpeed = SC.GetValue("Reservoir.MaxPumpSpeed");
                double caFlowDelta = resRecipe.CAFlowSetPoint - averageCAFlow;
                double newCAPumpSpeed = 330 * caFlowDelta + caPumpSpeed;
                if (newCAPumpSpeed <= 0 || newCAPumpSpeed > caPumpMaxSpeed)
                {
                    return;
                }
                if (Math.Abs(newCAPumpSpeed - caPumpSpeed) >= 10)
                {
                    _device.RegulatePumpSpeed(newCAPumpSpeed);
                }
            }
        }
        /// 
        /// 获取平均阳极流量 
        /// 
        /// 
        private double GetAverageCAFlow()
        {
            int count = 0;
            _caTotalFlow = 0;
            ReservoirItem reservoirItem = ReservoirItemManager.Instance.GetReservoirItem(_moduleName);
            if (reservoirItem != null)
            {
                List metalItems = reservoirItem.MetalCells;
                if (metalItems != null && metalItems.Count > 0)
                {
                    foreach (MetalItem metalItem in metalItems)
                    {
                        StandardHotMetalDevice metalDevice = DEVICE.GetDevice(metalItem.ModuleName);
                        if (metalDevice != null&&metalDevice.IsAuto)
                        {
                             count++;
                             _caTotalFlow += metalDevice.MetalDeviceData.CellFlow;
                        }
                    }
                }
                if (count != 0)
                {
                    return _caTotalFlow / count;
                }
            }
            return 0;
        }
    }
}