DataManager.cs 8.3 KB

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