123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- 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<BeckhoffIOManager>
- {
- #region 内部常量
- private const string DI_ITEMLIST = "System.DIItemList";
- private const string DO_ITEMLIST = "System.DOItemList";
- private const string DIGITAL = "Digital";
- #endregion
- #region 内部变量
- private Dictionary<string, BeckhoffDIAccessor> _diMap = new Dictionary<string, BeckhoffDIAccessor>();
- private Dictionary<string, BeckhoffDOAccessor> _doMap = new Dictionary<string, BeckhoffDOAccessor>();
- private Dictionary<string, List<NotifiableIoItem>> _ioItemList = new Dictionary<string, List<NotifiableIoItem>>();
- private int _size = 0;
- /// <summary>
- /// 模块变量更新委托字典(key-模块.变量,value-变量委托事件
- /// </summary>
- private Dictionary<string, BeckhoffDelegate.OnUpdateModuleVariableValue> _moduleVariableActionDic = new Dictionary<string, BeckhoffDelegate.OnUpdateModuleVariableValue>();
- /// <summary>
- /// 变量数值字典(key-io变量名称,value-数值)
- /// </summary>
- private Dictionary<string, object> _nameVariableValueDic = new Dictionary<string, object>();
- /// <summary>
- /// do对应地址(多个do对应同一址)
- /// </summary>
- private List<string> _doAddress = new List<string>();
- #endregion
- #region 属性
- /// <summary>
- /// IO数据长度
- /// </summary>
- public int Size { get { return _size; } }
- /// <summary>
- /// 模块变量-数值字典
- /// </summary>
- private Dictionary<string, object> _moduleNameVariableValueDic = new Dictionary<string, object>();
- #endregion
- /// <summary>
- /// 初始化
- /// </summary>
- /// <param name="cfg"></param>
- public void Initialize(BeckhoffCfg cfg)
- {
- SubscribeIoItemList();
- InitialAccessors(cfg);
- }
- public Dictionary<string, object> GetNameValueDic()
- {
- _moduleNameVariableValueDic.Clear();
- List<string> 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;
- }
- /// <summary>
- /// 初始化
- /// </summary>
- /// <param name="cfg"></param>
- 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++;
-
- }
- }
- }
- }
- /// <summary>
- /// 写DO变量数值
- /// </summary>
- /// <param name="arg1"></param>
- /// <param name="args"></param>
- /// <returns></returns>
- 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);
- }
-
- /// <summary>
- /// 订阅IO集合
- /// </summary>
- private void SubscribeIoItemList()
- {
- if (!_ioItemList.ContainsKey(DI_ITEMLIST))
- {
- _ioItemList[DI_ITEMLIST] = new List<NotifiableIoItem>();
- }
- if (!_ioItemList.ContainsKey(DO_ITEMLIST))
- {
- _ioItemList[DO_ITEMLIST] = new List<NotifiableIoItem>();
- }
- }
- #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
- /// <summary>
- /// 注册变量数值发生变化回调
- /// </summary>
- /// <param name="moduleName"></param>
- /// <param name="variable"></param>
- /// <param name="onUpdateModuleVariableValue"></param>
- 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]);
- }
- }
- }
- /// <summary>
- /// 更新IO数值
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- 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);
- }
- }
- }
- }
- }
- }
|