DataManager.cs 6.8 KB

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