DataManager.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using Aitex.Core.RT.ConfigCenter;
  2. using Aitex.Core.RT.DataCenter;
  3. using Aitex.Core.Util;
  4. using MECF.Framework.Common.DataCenter;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using OP = Common.OP.OP;
  13. namespace Common.DataCenter
  14. {
  15. /// <summary>
  16. ///
  17. /// 数据项命名规则:
  18. ///
  19. /// 一般数据项: 模块名.数据项名
  20. ///
  21. /// 设备类型数据项:模块名.设备类型.数据项名
  22. ///
  23. /// 界面显示设备数据项:模块名.数据项名
  24. ///
  25. /// </summary>
  26. public class DataManager : ICommonData
  27. {
  28. ConcurrentDictionary<string, DataItem<object>> _keyValueMap;
  29. SortedDictionary<string, Func<object>> _dbRecorderList;
  30. Func<object, bool> _isSubscriptionAttribute;
  31. Func<MemberInfo, bool> _hasSubscriptionAttribute;
  32. object _locker = new object();
  33. public DataManager()
  34. {
  35. }
  36. public void Initialize()
  37. {
  38. Initialize(true, true);
  39. }
  40. public void Initialize(bool enableService, bool enableStats = true)
  41. {
  42. _dbRecorderList = new SortedDictionary<string, Func<object>>();
  43. _keyValueMap = new ConcurrentDictionary<string, DataItem<object>>();
  44. _isSubscriptionAttribute = attribute => attribute is SubscriptionAttribute;
  45. _hasSubscriptionAttribute = mi => mi.GetCustomAttributes(false).Any(_isSubscriptionAttribute);
  46. DATA.InnerDataManager = this;
  47. //if (enableService)
  48. //{
  49. // Singleton<WcfServiceManager>.Instance.Initialize(new Type[]
  50. // {
  51. // typeof(QueryDataService)
  52. // });
  53. //}
  54. if (enableStats)
  55. {
  56. StatsDataManager.Instance.Initialize();
  57. }
  58. CONFIG.Subscribe("System", "NumericDataList", () => NumericDataList);
  59. }
  60. //private bool DatabaseExecute(string arg1, object[] arg2)
  61. //{
  62. // try
  63. // {
  64. // string sql = (string)arg2[0];
  65. // DB.Insert(sql);
  66. // //LOG.Write($"execute sql: {sql}");
  67. // }
  68. // catch (Exception ex)
  69. // {
  70. // LOG.WriteExeption(ex);
  71. // }
  72. // return true;
  73. //}
  74. public void Terminate()
  75. {
  76. }
  77. public List<string> NumericDataList
  78. {
  79. get
  80. {
  81. List<string> result = new List<string>();
  82. foreach (var dataItem in _keyValueMap)
  83. {
  84. object o = dataItem.Value.Value;
  85. if (o == null)
  86. continue;
  87. Type dataType = o.GetType();
  88. if (dataType == typeof(Boolean) || dataType == typeof(double) || dataType == typeof(float) ||
  89. dataType == typeof(int) || dataType == typeof(ushort) || dataType == typeof(short))
  90. result.Add(dataItem.Key);
  91. }
  92. return result;
  93. }
  94. }
  95. public Type GetDataType(string name)
  96. {
  97. if (_keyValueMap.ContainsKey(name))
  98. {
  99. object o = _keyValueMap[name].Value;
  100. if (o != null)
  101. return o.GetType();
  102. }
  103. return null;
  104. }
  105. public SortedDictionary<string, Func<object>> GetDBRecorderList()
  106. {
  107. SortedDictionary<string, Func<object>> result;
  108. lock (_locker)
  109. {
  110. result = new SortedDictionary<string, Func<object>>(_dbRecorderList);
  111. }
  112. return result;
  113. }
  114. public void Subscribe<T>(T instance, string keyPrefix = null) where T : class
  115. {
  116. if (instance == null)
  117. throw new ArgumentNullException("instance");
  118. Traverse(instance, keyPrefix);
  119. }
  120. public void Subscribe(string key, Func<object> getter, SubscriptionAttribute.FLAG flag)
  121. {
  122. Subscribe(key, new DataItem<object>(getter), flag);
  123. if (flag != SubscriptionAttribute.FLAG.IgnoreSaveDB)
  124. {
  125. lock (_locker)
  126. {
  127. _dbRecorderList[key] = getter;
  128. }
  129. }
  130. }
  131. public void Subscribe(string key, DataItem<object> dataItem, SubscriptionAttribute.FLAG flag)
  132. {
  133. if (string.IsNullOrWhiteSpace(key))
  134. throw new ArgumentNullException("key");
  135. if (_keyValueMap.ContainsKey(key))
  136. throw new Exception(string.Format("Duplicated Key:{0}", key));
  137. if (dataItem == null)
  138. throw new ArgumentNullException("dataItem");
  139. _keyValueMap.TryAdd(key, dataItem);
  140. }
  141. public Dictionary<string, object> Poll(IEnumerable<string> keys)
  142. {
  143. Dictionary<string, object> data = new Dictionary<string, object>();
  144. foreach (string name in keys)
  145. {
  146. if (_keyValueMap.ContainsKey(name))
  147. data[name] = _keyValueMap[name].Value;
  148. }
  149. return data;
  150. }
  151. public object Poll(string key)
  152. {
  153. return _keyValueMap.ContainsKey(key) ? _keyValueMap[key].Value : null;
  154. }
  155. public void Traverse(object instance, string keyPrefix)
  156. {
  157. Parallel.ForEach(instance.GetType().GetFields().Where<FieldInfo>(_hasSubscriptionAttribute), fi =>
  158. {
  159. string key = Parse(fi);
  160. key = string.IsNullOrWhiteSpace(keyPrefix) ? key : string.Format("{0}.{1}", keyPrefix, key);
  161. Subscribe(key, () => fi.GetValue(instance), 0);
  162. });
  163. Parallel.ForEach(instance.GetType().GetProperties().Where<PropertyInfo>(_hasSubscriptionAttribute), property =>
  164. {
  165. string key = Parse(property);
  166. key = string.IsNullOrWhiteSpace(keyPrefix) ? key : string.Format("{0}.{1}", keyPrefix, key);
  167. Subscribe(key, () => property.GetValue(instance, null), 0);
  168. });
  169. }
  170. string Parse(MemberInfo member)
  171. {
  172. return _hasSubscriptionAttribute(member) ? (member.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute).Key : null;
  173. }
  174. public Dictionary<string, object> PollData(IEnumerable<string> keys)
  175. {
  176. Dictionary<string, object> result = new Dictionary<string, object>();
  177. foreach (string key in keys)
  178. {
  179. if (_keyValueMap.ContainsKey(key))
  180. result[key] = _keyValueMap[key].Value;
  181. else
  182. {
  183. //LOG.Error("未定义的DataItem:" + key);
  184. }
  185. }
  186. return result;
  187. }
  188. }
  189. }