DataManager.cs 9.4 KB

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