DataManager.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections.Concurrent;
  6. using System.Reflection;
  7. using Aitex.Core.Util;
  8. using System.Threading.Tasks;
  9. using Aitex.Core.RT.Log;
  10. namespace Aitex.Core.RT.DataCenter
  11. {
  12. /// <summary>
  13. ///
  14. /// 数据项命名规则:
  15. ///
  16. /// 一般数据项: 模块名.数据项名
  17. ///
  18. /// 设备类型数据项:模块名.设备类型.数据项名
  19. ///
  20. /// 界面显示设备数据项:模块名.数据项名
  21. ///
  22. /// </summary>
  23. public class DataManager : ICommonData
  24. {
  25. ConcurrentDictionary<string, DataItem<object>> _keyValueMap;
  26. SortedDictionary<string, Func<object>> _dbRecorderList;
  27. Func<object, bool> _isSubscriptionAttribute;
  28. Func<MemberInfo, bool> _hasSubscriptionAttribute;
  29. object _locker = new object();
  30. public DataManager()
  31. {
  32. }
  33. public void Initialize()
  34. {
  35. _dbRecorderList = new SortedDictionary<string, Func<object>>();
  36. _keyValueMap = new ConcurrentDictionary<string, DataItem<object>>();
  37. _isSubscriptionAttribute = attribute => attribute is SubscriptionAttribute;
  38. _hasSubscriptionAttribute = mi => mi.GetCustomAttributes(false).Any(_isSubscriptionAttribute);
  39. DATA.InnerDataManager = this;
  40. }
  41. public SortedDictionary<string, Func<object>> GetDBRecorderList()
  42. {
  43. SortedDictionary<string, Func<object>> result;
  44. lock (_locker)
  45. {
  46. result = new SortedDictionary<string, Func<object>>(_dbRecorderList);
  47. }
  48. return result;
  49. }
  50. public void Subscribe<T>(T instance, string keyPrefix = null) where T : class
  51. {
  52. if (instance == null)
  53. throw new ArgumentNullException("instance");
  54. Traverse(instance, keyPrefix);
  55. }
  56. public void Subscribe(string key, Func<object> getter, SubscriptionAttribute.FLAG flag)
  57. {
  58. if (string.IsNullOrWhiteSpace(key))
  59. throw new ArgumentNullException("key");
  60. if (_keyValueMap.ContainsKey(key))
  61. throw new Exception(string.Format("Duplicated Key:{0}", key));
  62. if (getter == null)
  63. throw new ArgumentNullException("getter");
  64. _keyValueMap.TryAdd(key, new DataItem<object>(getter));
  65. if (flag != SubscriptionAttribute.FLAG.IgnoreSaveDB)
  66. {
  67. lock (_locker)
  68. {
  69. _dbRecorderList[key] = getter;
  70. }
  71. }
  72. }
  73. public Dictionary<string, object> Poll(IEnumerable<string> keys)
  74. {
  75. Dictionary<string, object> data = new Dictionary<string, object>();
  76. foreach (string name in keys)
  77. {
  78. if (_keyValueMap.ContainsKey(name))
  79. data[name] =_keyValueMap[name].Value;
  80. }
  81. return data;
  82. }
  83. public object Poll(string key)
  84. {
  85. return _keyValueMap.ContainsKey(key) ? _keyValueMap[key].Value : null;
  86. }
  87. public void Traverse(object instance, string keyPrefix)
  88. {
  89. Parallel.ForEach(instance.GetType().GetFields().Where<FieldInfo>(_hasSubscriptionAttribute), fi =>
  90. {
  91. string key = Parse(fi);
  92. key = string.IsNullOrWhiteSpace(keyPrefix) ? key : string.Format("{0}.{1}", keyPrefix, key);
  93. Subscribe(key, () => fi.GetValue(instance), 0);
  94. });
  95. Parallel.ForEach(instance.GetType().GetProperties().Where<PropertyInfo>(_hasSubscriptionAttribute), property =>
  96. {
  97. string key = Parse(property);
  98. key = string.IsNullOrWhiteSpace(keyPrefix) ? key : string.Format("{0}.{1}", keyPrefix, key);
  99. Subscribe(key, () => property.GetValue(instance, null), 0);
  100. });
  101. }
  102. string Parse(MemberInfo member)
  103. {
  104. return _hasSubscriptionAttribute(member) ? (member.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute).Key : null;
  105. }
  106. public Dictionary<string, object> PollData(IEnumerable<string> keys)
  107. {
  108. Dictionary<string, object> result = new Dictionary<string,object>();
  109. foreach (string key in keys)
  110. {
  111. if (_keyValueMap.ContainsKey(key))
  112. result[key] = _keyValueMap[key].Value;
  113. else
  114. {
  115. //LOG.Error("未定义的DataItem:" + key);
  116. }
  117. }
  118. return result;
  119. }
  120. }
  121. }