DataManager.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 Type GetDataType(string name)
  100. {
  101. if (_keyValueMap.ContainsKey(name))
  102. {
  103. object o = _keyValueMap[name].Value;
  104. if (o != null)
  105. return o.GetType();
  106. }
  107. return null;
  108. }
  109. public SortedDictionary<string, Func<object>> GetDBRecorderList()
  110. {
  111. SortedDictionary<string, Func<object>> result;
  112. lock (_locker)
  113. {
  114. result = new SortedDictionary<string, Func<object>>(_dbRecorderList);
  115. }
  116. return result;
  117. }
  118. public void Subscribe<T>(T instance, string keyPrefix = null) where T : class
  119. {
  120. if (instance == null)
  121. throw new ArgumentNullException("instance");
  122. Traverse(instance, keyPrefix);
  123. }
  124. public void Subscribe(string key, Func<object> getter, SubscriptionAttribute.FLAG flag)
  125. {
  126. Subscribe(key, new DataItem<object>(getter), flag);
  127. if (flag != SubscriptionAttribute.FLAG.IgnoreSaveDB)
  128. {
  129. lock (_locker)
  130. {
  131. _dbRecorderList[key] = getter;
  132. }
  133. }
  134. }
  135. public void Subscribe(string key, DataItem<object> dataItem, SubscriptionAttribute.FLAG flag)
  136. {
  137. if (string.IsNullOrWhiteSpace(key))
  138. throw new ArgumentNullException("key");
  139. if (_keyValueMap.ContainsKey(key))
  140. throw new Exception(string.Format("Duplicated Key:{0}", key));
  141. if (dataItem == null)
  142. throw new ArgumentNullException("dataItem");
  143. _keyValueMap.TryAdd(key, dataItem);
  144. }
  145. public Dictionary<string, object> Poll(IEnumerable<string> keys)
  146. {
  147. Dictionary<string, object> data = new Dictionary<string, object>();
  148. foreach (string name in keys)
  149. {
  150. if (_keyValueMap.ContainsKey(name))
  151. data[name] =_keyValueMap[name].Value;
  152. }
  153. return data;
  154. }
  155. public object Poll(string key)
  156. {
  157. return _keyValueMap.ContainsKey(key) ? _keyValueMap[key].Value : null;
  158. }
  159. public void Traverse(object instance, string keyPrefix)
  160. {
  161. Parallel.ForEach(instance.GetType().GetFields().Where<FieldInfo>(_hasSubscriptionAttribute), fi =>
  162. {
  163. string key = Parse(fi);
  164. key = string.IsNullOrWhiteSpace(keyPrefix) ? key : string.Format("{0}.{1}", keyPrefix, key);
  165. Subscribe(key, () => fi.GetValue(instance), 0);
  166. });
  167. Parallel.ForEach(instance.GetType().GetProperties().Where<PropertyInfo>(_hasSubscriptionAttribute), property =>
  168. {
  169. string key = Parse(property);
  170. key = string.IsNullOrWhiteSpace(keyPrefix) ? key : string.Format("{0}.{1}", keyPrefix, key);
  171. Subscribe(key, () => property.GetValue(instance, null), 0);
  172. });
  173. }
  174. string Parse(MemberInfo member)
  175. {
  176. return _hasSubscriptionAttribute(member) ? (member.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute).Key : null;
  177. }
  178. public Dictionary<string, object> PollData(IEnumerable<string> keys)
  179. {
  180. Dictionary<string, object> result = new Dictionary<string,object>();
  181. foreach (string key in keys)
  182. {
  183. if (_keyValueMap.ContainsKey(key))
  184. result[key] = _keyValueMap[key].Value;
  185. else
  186. {
  187. //LOG.Error("未定义的DataItem:" + key);
  188. }
  189. }
  190. return result;
  191. }
  192. }
  193. }