using FestoDebugger.Common; using FestoDebugger.Service; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TwinCAT.TypeSystem; namespace FestoDebugger.Beckoff { public class BeckhoffIOManager : Singleton { #region 内部常量 private const string DI_ITEMLIST = "System.DIItemList"; private const string DO_ITEMLIST = "System.DOItemList"; private const string DIGITAL = "Digital"; #endregion #region 内部变量 private Dictionary _diMap = new Dictionary(); private Dictionary _doMap = new Dictionary(); private Dictionary> _ioItemList = new Dictionary>(); private int _size = 0; /// /// 模块变量更新委托字典(key-模块.变量,value-变量委托事件 /// private Dictionary _moduleVariableActionDic = new Dictionary(); /// /// 变量数值字典(key-io变量名称,value-数值) /// private Dictionary _nameVariableValueDic = new Dictionary(); /// /// do对应地址(多个do对应同一址) /// private List _doAddress = new List(); #endregion #region 属性 /// /// IO数据长度 /// public int Size { get { return _size; } } /// /// 模块变量-数值字典 /// private Dictionary _moduleNameVariableValueDic = new Dictionary(); #endregion /// /// 初始化 /// /// public void Initialize(BeckhoffCfg cfg) { SubscribeIoItemList(); InitialAccessors(cfg); } public Dictionary GetNameValueDic() { _moduleNameVariableValueDic.Clear(); List keys = _nameVariableValueDic.Keys.ToList(); foreach (string item in keys) { if(!_nameVariableValueDic.ContainsKey(item)) { continue; } string ModuleName = BeckhoffModuleIOManager.Instance.GetInnerModuleNameByIOName(item); _moduleNameVariableValueDic.Add(ModuleName, _nameVariableValueDic[item]); } return _moduleNameVariableValueDic; } /// /// 初始化 /// /// private void InitialAccessors(BeckhoffCfg cfg) { foreach (BeckhoffInput item in cfg.Controller.Inputs) { string key = $"{item.Name}"; if (item.Type == DIGITAL) { if (!_diMap.ContainsKey(key)) { BeckhoffDIAccessor accessor = new BeckhoffDIAccessor(key, item.Address, item.Scaling); _diMap[key] = accessor; _ioItemList[DI_ITEMLIST].Add(new NotifiableIoItem() { Address = accessor.Address, Name = key }); BeckhoffItemManager.Instance.InitBeckhoffItem(key, item.Address, item.DataType, "di", 1, false, 0, item.Invert); _size++; } } } foreach (BeckhoffOutput item in cfg.Controller.Outputs) { string key = $"{item.Name}"; if (item.Type == DIGITAL) { if (!_doMap.ContainsKey(key)) { BeckhoffDOAccessor accessor = new BeckhoffDOAccessor(key, item.Address, item.Scaling, item.BitOperated, item.Bit); _doMap[key] = accessor; _ioItemList[DO_ITEMLIST].Add(new NotifiableIoItem() { Address = accessor.Address, Name = key }); BeckhoffItemManager.Instance.InitBeckhoffItem(key, item.Address, item.DataType, "do", 1, item.BitOperated, item.Bit, item.Invert); BeckhoffItemManager.Instance.InitWriteBeckoffItem(key, item.Address, item.DataType, "do", 1, item.BitOperated, item.Bit, item.Invert); _size++; } } } } /// /// 写DO变量数值 /// /// /// /// public bool WriteDoOperation(string arg1, object[] args) { string moduleName = (string)args[0]; bool setpoint = (bool)args[1]; string IOName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName(moduleName); BeckhoffItem item = BeckhoffItemManager.Instance.GetWriteBeckhoffItem(IOName); return TwincatAdoManager.Instance.WriteValue(IOName, setpoint); } /// /// 订阅IO集合 /// private void SubscribeIoItemList() { if (!_ioItemList.ContainsKey(DI_ITEMLIST)) { _ioItemList[DI_ITEMLIST] = new List(); } if (!_ioItemList.ContainsKey(DO_ITEMLIST)) { _ioItemList[DO_ITEMLIST] = new List(); } } #region 获取Accessor和Item public BeckhoffDIAccessor GetDIAccessor(string name) { return _diMap.ContainsKey(name) ? _diMap[name] : null; } public BeckhoffDOAccessor GetDOAccessor(string name) { return _doMap.ContainsKey(name) ? _doMap[name] : null; } #endregion /// /// 注册变量数值发生变化回调 /// /// /// /// 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) { _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); } } } } } }