| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 | using Aitex.Core.RT.DataCenter;using Aitex.Core.RT.Device;using Aitex.Core.RT.Log;using Aitex.Core.RT.OperationCenter;using Aitex.Core.RT.SCCore;using Aitex.Core.Util;using MECF.Framework.Common.Beckhoff.ModuleIO;using MECF.Framework.Common.CommonData.Metal;using MECF.Framework.Common.CommonData.PlatingCell;using MECF.Framework.Common.CommonData.Reservoir;using MECF.Framework.Common.IOCore;using MECF.Framework.Common.Persistent.Reservoirs;using MECF.Framework.Common.ToolLayout;using PunkHPX8_Core;using PunkHPX8_RT.Devices.LinMot;using PunkHPX8_RT.Devices.PowerSupplier;using PunkHPX8_RT.Devices.Reservoir;using PunkHPX8_RT.Modules;using PunkHPX8_RT.Modules.PlatingCell;using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using System.Text;using System.Threading.Tasks;namespace PunkHPX8_RT.Devices.PlatingCell{    public class PlatingCellDevice : BaseDevice, IDevice    {        #region 常量         private const string PERSISTENT_VALUE = "PersistentValue";        private const string PLATINGCELLDATA = "PlatingCellData";        private const string AUTO = "Auto";        private const string MANUAL = "Manual";        private const string STRATUS = "Stratus";        private const string DISABLED = "Disabled";                        private const string IS_HEAD_TILT = "IsHeadTilt";        private const string IS_HEAD_VERTICAL = "IsHeadVertical";        private const string CLAMSHELL_DISTANCE = "ClamShellDistance";        private const string CLAMSHELL_CYLINDER_PRESSURE = "ClamShellCylinderPressure";        private const string OVERFLOW_LEVEL = "OverFlowLevel";        private const string CLAMSHELL_CLOSE = "ClamShellClose";        private const string HEAD_TILT = "HeadTilt";        #endregion        #region 内部变量        /// 变量是否初始化字典        /// </summary>        private Dictionary<string, bool> _variableInitializeDic = new Dictionary<string, bool>();        /// <summary>        /// 操作当前状态        /// </summary>        protected RState _status;        /// <summary>        /// 持久化数据        /// </summary>        protected PlatingCellPersistentValue _persistentValue;        /// <summary>        /// PlatingCell项        /// </summary>        private PlatingCellItem _platingCellItem;        /// <summary>        /// overflow        /// </summary>        private int _overflowLevelHigh = 85;        private int _overflowLevelLow = 25;        /// <summary>        /// 对应reservoir的名字        /// </summary>        private string _reservoirName;        #endregion        #region 属性        /// <summary>        /// 状态        /// </summary>        public RState Status { get { return _status; } }        /// <summary>        /// 是否禁用        /// </summary>        public bool IsDisable { get { return _persistentValue == null || _persistentValue.OperatingMode == DISABLED; } }               /// <summary>        /// 操作模式        /// </summary>        public string OperationMode { get { return _persistentValue.OperatingMode; } }        /// <summary>        /// 工程模式        /// </summary>        public string EngineerMode { get { return _persistentValue.RecipeOperatingMode; } }        /// <summary>        /// 是否为Auto        /// </summary>        public bool IsAuto { get { return _persistentValue != null ? _persistentValue.OperatingMode == AUTO : false; } }        /// <summary>        /// 是否为Auto        /// </summary>        public bool IsManual { get { return _persistentValue != null ? _persistentValue.OperatingMode == MANUAL : false; } }        #endregion        #region 共享变量        /// <summary>        /// 数据        /// </summary>        protected PlatingCellData _platingCellData = new PlatingCellData();        /// <summary>        /// 对应reservoir数据        /// </summary>        protected ReservoirData _reservoirData = new ReservoirData();        #endregion        #region 属性        /// <summary>        /// 设备数据        /// </summary>        public PlatingCellData PlatingCellDeviceData { get { return _platingCellData; } }        #endregion        /// <summary>        /// 构造函数        /// </summary>        /// <param name="moduleName"></param>        public PlatingCellDevice(string moduleName) : base(moduleName, moduleName, moduleName, moduleName)        {                    }        /// <summary>        /// 初始化        /// </summary>        /// <returns></returns>        public virtual bool Initialize()        {            InitializeParameter();            SubscribeData();            InitializeOperation();            SubscribeValueAction();            return true;        }        /// <summary>        /// 定时器执行        /// </summary>        public virtual bool OnTimer(int interval)        {            return true;        }        /// <summary>        /// 初始化参数        /// </summary>        private void InitializeParameter()        {            _persistentValue = PlatingCellPersistentManager.Instance.GetPlatingCellPersistentValue(Module);            if (_persistentValue == null)            {                LOG.WriteLog(eEvent.ERR_RESERVOIR, Module, "Persistent Value Object is not exist");            }            _platingCellItem = PlatingCellItemManager.Instance.GetPlatingCellItem(Module);            if (_platingCellItem != null)            {            }            _overflowLevelHigh = SC.GetValue<int>($"PlatingCell.OverflowLevelHigh");            _overflowLevelLow = SC.GetValue<int>($"PlatingCell.OverflowLevelLow");            _reservoirData = GetReservoirDevice().ReservoirData;            _reservoirName = GetReservoirDevice().Module;        }        protected virtual void SubscribeValueAction()        {            IoSubscribeUpdateVariable(IS_HEAD_TILT);            IoSubscribeUpdateVariable(IS_HEAD_VERTICAL);            IoSubscribeUpdateVariable(CLAMSHELL_DISTANCE);            IoSubscribeUpdateVariable(CLAMSHELL_CYLINDER_PRESSURE);            IoSubscribeUpdateVariable(OVERFLOW_LEVEL);            IoSubscribeUpdateVariable(CLAMSHELL_CLOSE);            IoSubscribeUpdateVariable(HEAD_TILT);           }        /// <summary>        /// 订阅IO变量        /// </summary>        /// <param name="variable"></param>        protected void IoSubscribeUpdateVariable(string variable)        {            _variableInitializeDic[variable] = false;            IOModuleManager.Instance.SubscribeModuleVariable(Module, variable, UpdateVariableValue);        }        /// <summary>        /// 更新变量数值        /// </summary>        /// <param name="variable"></param>        /// <param name="value"></param>        private void UpdateVariableValue(string variable, object value)        {            if (!_platingCellData.IsDataInitialized)            {                _platingCellData.IsDataInitialized = true;            }            PropertyInfo property = _platingCellData.GetType().GetProperty(variable);            if (property != null)            {                property.SetValue(_platingCellData, value);                if (variable == OVERFLOW_LEVEL)                {                    double waterLevel = CurrentToWaterLevel(Convert.ToDouble(value));                    _platingCellData.OverFlowLevel = waterLevel;                    if (_platingCellData.OverFlowLevel >= _overflowLevelHigh)                    {                        _platingCellData.OverFlowStatus = "High";                    }                    else if(_platingCellData.OverFlowLevel <= _overflowLevelLow)                    {                        _platingCellData.OverFlowStatus = "Empty";                    }                    else                    {                        _platingCellData.OverFlowStatus = "Full";                    }                 }            }            if (_variableInitializeDic.ContainsKey(variable) && !_variableInitializeDic[variable])            {                _variableInitializeDic[variable] = true;            }        }        /// <summary>        /// 把电流mA转成水深mm        /// </summary>        /// <param name="current"></param>        private double CurrentToWaterLevel(double current)        {            double result = (current - 4) / 8 / 9.8 * 1000;            return result;        }        /// <summary>        /// 写变量        /// </summary>        /// <param name="variable"></param>        /// <param name="value"></param>        /// <returns></returns>        protected bool WriteVariableValue(string variable, object value)        {            string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{variable}");            return IOModuleManager.Instance.WriteIoValue(ioName, value);        }        /// <summary>        /// 订阅数据        /// </summary>        private void SubscribeData()        {            DATA.Subscribe($"{Module}.{PERSISTENT_VALUE}", () => _persistentValue, SubscriptionAttribute.FLAG.IgnoreSaveDB);            DATA.Subscribe($"{Module}.{PLATINGCELLDATA}", () => _platingCellData, SubscriptionAttribute.FLAG.IgnoreSaveDB);            DATA.Subscribe($"{Module}.ReservoirCommonData", () => _reservoirData, SubscriptionAttribute.FLAG.IgnoreSaveDB);            DATA.Subscribe($"{Module}.ReservoirName", () => _reservoirName, SubscriptionAttribute.FLAG.IgnoreSaveDB);                           }        /// <summary>        /// 初始化操作        /// </summary>        protected virtual void InitializeOperation()        {            OP.Subscribe($"{Module}.DisabledAction", DisabledOperation);            OP.Subscribe($"{Module}.ManualAction", ManualOperation);            OP.Subscribe($"{Module}.AutoAction", AutoOperation);            OP.Subscribe($"{Module}.EngineeringModeAction", EngineeringModeOperation);            OP.Subscribe($"{Module}.ProductionModeAction", ProductionModeOperation);            OP.Subscribe($"{Module}.SetPlatingCellWaferSize", (cmd, args) => { return SetPlatingCellWaferSize(cmd, args); });                        OP.Subscribe($"{Module}.ClamShellClose", (cmd, para) => { return ClamShellClose(); });            OP.Subscribe($"{Module}.ClamShellOpen", (cmd, para) => { return ClamShellOpen(); });            OP.Subscribe($"{Module}.HeadtTilt", (cmd, para) => { return HeadtTiltAction(); });            OP.Subscribe($"{Module}.HeadVertical", (cmd, para) => { return HeadtVerticalAction(); });              }        #region Operation        public bool ClamShellClose()        {            return WriteVariableValue(CLAMSHELL_CLOSE, true);        }        public bool ClamShellOpen()        {            return WriteVariableValue(CLAMSHELL_CLOSE, false);        }        public bool HeadtTiltAction()        {            return WriteVariableValue(HEAD_TILT, true);        }        public bool HeadtVerticalAction()        {            return WriteVariableValue(HEAD_TILT, false);        }        /// <summary>        /// DisabledAction        /// </summary>        /// <param name="cmd"></param>        /// <param name="param"></param>        /// <returns></returns>        public bool DisabledOperation(string cmd, object[] args)        {            string currentOperation = "Disabled";            PlatingCellEntity platingCellEntity = Singleton<RouteManager>.Instance.GetModule<PlatingCellEntity>(Module);            if (platingCellEntity != null && _persistentValue != null && _persistentValue.OperatingMode != currentOperation)            {                string preOperation = _persistentValue.OperatingMode;                if (platingCellEntity.IsBusy)                {                    LOG.WriteLog(eEvent.ERR_METAL, Module, $"{Module} is Busy, can't switch to Disabled mode");                    return false;                }                //if (SchedulerMetalTimeManager.Instance.Contained(Module))                //{                //    LOG.WriteLog(eEvent.ERR_METAL, Module, $"{Module} is in scheduler, can't switch to Disabled mode");                //    return false;                //}                platingCellEntity.EnterInit();                _persistentValue.OperatingMode = currentOperation;                LOG.WriteLog(eEvent.INFO_METAL, Module, $"Operating mode is switched from {preOperation} to {currentOperation}");            }            PlatingCellPersistentManager.Instance.UpdatePersistentValue(Module);            return true;        }        /// <summary>        /// ManualAction        /// </summary>        /// <param name="cmd"></param>        /// <param name="param"></param>        /// <returns></returns>        public bool ManualOperation(string cmd, object[] args)        {            string currentOperation = "Manual";            PlatingCellEntity platingCellEntity = Singleton<RouteManager>.Instance.GetModule<PlatingCellEntity>(Module);            if (platingCellEntity != null && _persistentValue != null && _persistentValue.OperatingMode != currentOperation)            {                string preOperation = _persistentValue.OperatingMode;                if (platingCellEntity.IsBusy)                {                    LOG.WriteLog(eEvent.ERR_METAL, Module, $"{Module} is Busy, can't switch to Manual mode");                    return false;                }                //if (SchedulerMetalTimeManager.Instance.Contained(Module))                //{                //    LOG.WriteLog(eEvent.ERR_METAL, Module, $"{Module} is in scheduler, can't switch to Manual mode");                //    return false;                //}                platingCellEntity.EnterInit();                _persistentValue.OperatingMode = currentOperation;                LOG.WriteLog(eEvent.INFO_METAL, Module, $"Operating mode is switched from {preOperation} to {currentOperation}");            }            PlatingCellPersistentManager.Instance.UpdatePersistentValue(Module);            return true;        }        /// <summary>        /// AutoAction        /// </summary>        /// <param name="cmd"></param>        /// <param name="param"></param>        /// <returns></returns>        public bool AutoOperation(string cmd, object[] args)        {            string currentOperation = "Auto";            PlatingCellEntity platingCellEntity = Singleton<RouteManager>.Instance.GetModule<PlatingCellEntity>(Module);            if (platingCellEntity != null && _persistentValue != null && _persistentValue.OperatingMode != currentOperation)            {                string preOperation = _persistentValue.OperatingMode;                if (platingCellEntity.IsBusy)                {                    LOG.WriteLog(eEvent.ERR_METAL, Module, $"{Module} is Busy, can't switch to Auto mode");                    return false;                }                platingCellEntity.EnterInit();                _persistentValue.OperatingMode = currentOperation;                LOG.WriteLog(eEvent.INFO_METAL, Module, $"Operating mode is switched from {preOperation} to {currentOperation}");            }            PlatingCellPersistentManager.Instance.UpdatePersistentValue(Module);            return true;        }        /// <summary>        /// EngineeringModeAction        /// </summary>        /// <param name="cmd"></param>        /// <param name="param"></param>        /// <returns></returns>        private bool EngineeringModeOperation(string cmd, object[] args)        {            string currentRecipeOperation = "Engineering";            if (_persistentValue != null)            {                _persistentValue.RecipeOperatingMode = currentRecipeOperation;            }            PlatingCellPersistentManager.Instance.UpdatePersistentValue(Module);            return true;        }        /// <summary>        /// ProductionAction        /// </summary>        /// <param name="cmd"></param>        /// <param name="param"></param>        /// <returns></returns>        private bool ProductionModeOperation(string cmd, object[] args)        {            string currentRecipeOperation = "Production";            if (_persistentValue != null)            {                _persistentValue.RecipeOperatingMode = currentRecipeOperation;            }            PlatingCellPersistentManager.Instance.UpdatePersistentValue(Module);            return true;        }        private bool SetPlatingCellWaferSize(string cmd, object[] args)        {            string metalWaferSize = args[0] as string;            if (_persistentValue != null)            {                _persistentValue.PlatingCellWaferSize = int.Parse(metalWaferSize);            }            PlatingCellPersistentManager.Instance.UpdatePersistentValue(Module);            return true;        }        /// <summary>        /// 获取Reservoir设备        /// </summary>        /// <returns></returns>        private ReservoirDevice GetReservoirDevice()        {            string reservoir = ReservoirItemManager.Instance.GetReservoirByPlatingCell(Module);            return DEVICE.GetDevice<ReservoirDevice>(reservoir);        }        #endregion        public virtual void Monitor()        {        }        public virtual void Reset()        {        }        public virtual void Terminate()        {        }    }}
 |