using Aitex.Core.RT.Log; using Aitex.Core.RT.SCCore; using Aitex.Core.Util; using MECF.Framework.Common.Beckhoff.ModuleIO; using MECF.Framework.Common.Device.Festo; using MECF.Framework.Common.Device.Galil; using MECF.Framework.Common.Device.Wago; using MECF.Framework.Common.TwinCat; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MECF.Framework.Common.IOCore { public class IOModuleManager : Singleton { #region 内部变量 /// /// 模块变量更新委托字典(key-模块.变量,value-变量委托事件 /// private Dictionary _moduleVariableActionDic = new Dictionary(); /// /// 变量数值字典(key-io变量名称,value-数值) /// private Dictionary _nameVariableValueDic = new Dictionary(); /// /// Festo名称集合 /// private List _festoIONames = new List(); /// /// Wago IO集合 /// private List _wagoIONames = new List(); #endregion /// /// 初始化 /// public void Initialize() { FestoControllerCfgManager.Instance.Initialize(_festoIONames); WagoControllerCfgManager.Instance.Initialize(_wagoIONames); } /// /// 写IO数值 /// /// /// /// public bool WriteIoValue(string name, object value) { if (_festoIONames.Contains(name)) { return FestoControllerCfgManager.Instance.SetDoValue(name, (bool)value); } else if (_wagoIONames.Contains(name)) { return WagoControllerCfgManager.Instance.WriteIOValue(name, value); } else { return false; } } /// /// 注册变量数值发生变化回调 /// /// /// /// public void SubscribeModuleVariable(string moduleName, string variable, BeckhoffDelegate.OnUpdateModuleVariableValue onUpdateModuleVariableValue) { string name = $"{moduleName}.{variable}"; _moduleVariableActionDic[name] = onUpdateModuleVariableValue; //根据模块变量名称获取相应的IO变量名称 string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName(name); if (!string.IsNullOrEmpty(ioName)) { if (_nameVariableValueDic.ContainsKey(ioName)) { _moduleVariableActionDic[name].Invoke(variable, _nameVariableValueDic[ioName]); } } } /// /// 更新IO数值 /// /// /// public void UpdateIoValue(string name, object value) { bool _enableRead = SC.GetValue("Twincat.EnableReadLog"); if (_enableRead) { LOG.WriteBackgroundLog(eEvent.INFO_TWINCAT, "System", $"read {name} value {value}"); } _nameVariableValueDic[name] = value; //根据io变量名称获取模块变量名称 string innerModuleName = BeckhoffModuleIOManager.Instance.GetInnerModuleNameByIOName(name); if (!string.IsNullOrEmpty(innerModuleName)) { if (_moduleVariableActionDic.ContainsKey(innerModuleName)) { string[] strAry = innerModuleName.Split('.'); if (strAry.Length != 0) { _moduleVariableActionDic[innerModuleName].Invoke(strAry[strAry.Length - 1], value); } } } } } }