DataManager.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. ScheduleMaintenanceDataManager.Instance.Initialize();
  67. CONFIG.Subscribe("System", "NumericDataList", ()=>NumericDataList);
  68. logCleaner.Run();
  69. diskManager.Run();
  70. OP.Subscribe("System.DBExecute", DatabaseExecute);
  71. }
  72. private bool DatabaseExecute(string arg1, object[] arg2)
  73. {
  74. try
  75. {
  76. string sql = (string)arg2[0];
  77. DB.Insert(sql);
  78. LOG.Write($"execute sql: {sql}");
  79. }
  80. catch (Exception ex)
  81. {
  82. LOG.Write(ex);
  83. }
  84. return true;
  85. }
  86. public void Terminate()
  87. {
  88. logCleaner.Stop();
  89. diskManager.Stop();
  90. }
  91. public List<string> NumericDataList
  92. {
  93. get
  94. {
  95. List<string> result = new List<string>();
  96. foreach (var dataItem in _keyValueMap)
  97. {
  98. object o = dataItem.Value.Value;
  99. if (o == null)
  100. continue;
  101. Type dataType = o.GetType();
  102. if (dataType == typeof(Boolean) || dataType == typeof(double) || dataType == typeof(float) || dataType == typeof(bool) ||
  103. dataType == typeof(int) || dataType == typeof(ushort) || dataType == typeof(short))
  104. result.Add(dataItem.Key);
  105. }
  106. return result;
  107. }
  108. }
  109. public List<VIDItem> VidDataList
  110. {
  111. get
  112. {
  113. List<VIDItem> result = new List<VIDItem>();
  114. foreach (var dataItem in _keyValueMap)
  115. {
  116. object o = dataItem.Value.Value;
  117. if (o == null)
  118. continue;
  119. Type dataType = o.GetType();
  120. if (dataType == typeof(Boolean) || dataType == typeof(double) || dataType == typeof(float) ||
  121. dataType == typeof(bool) ||
  122. dataType == typeof(int) || dataType == typeof(ushort) || dataType == typeof(short))
  123. {
  124. result.Add(new VIDItem()
  125. {
  126. DataType = dataType.ToString(),
  127. Description = "",
  128. Index = 0,
  129. Name = dataItem.Key,
  130. Unit = "",
  131. });
  132. }
  133. }
  134. return result;
  135. }
  136. }
  137. public List<string> BuiltInDataList
  138. {
  139. get
  140. {
  141. List<string> result = new List<string>();
  142. foreach (var dataItem in _keyValueMap)
  143. {
  144. object o = dataItem.Value.Value;
  145. if (o == null)
  146. continue;
  147. Type dataType = o.GetType();
  148. if (dataType == typeof(Boolean) || dataType == typeof(double) || dataType == typeof(float) || dataType == typeof(bool) ||
  149. dataType == typeof(int) || dataType == typeof(ushort) || dataType == typeof(short) || dataType == typeof(string))
  150. result.Add(dataItem.Key);
  151. }
  152. return result;
  153. }
  154. }
  155. public List<string> FullDataList
  156. {
  157. get
  158. {
  159. return _keyValueMap.Keys.ToList();
  160. }
  161. }
  162. public Type GetDataType(string name)
  163. {
  164. if (_keyValueMap.ContainsKey(name))
  165. {
  166. object o = _keyValueMap[name].Value;
  167. if (o != null)
  168. return o.GetType();
  169. }
  170. return null;
  171. }
  172. public SortedDictionary<string, Func<object>> GetDBRecorderList()
  173. {
  174. SortedDictionary<string, Func<object>> result;
  175. lock (_locker)
  176. {
  177. result = new SortedDictionary<string, Func<object>>(_dbRecorderList);
  178. }
  179. return result;
  180. }
  181. public void Subscribe<T>(T instance, string keyPrefix = null) where T : class
  182. {
  183. if (instance == null)
  184. throw new ArgumentNullException("instance");
  185. Traverse(instance, keyPrefix);
  186. }
  187. public void Subscribe(string key, Func<object> getter, SubscriptionAttribute.FLAG flag)
  188. {
  189. Subscribe(key, new DataItem<object>(getter), flag);
  190. if (flag != SubscriptionAttribute.FLAG.IgnoreSaveDB)
  191. {
  192. lock (_locker)
  193. {
  194. _dbRecorderList[key] = getter;
  195. }
  196. }
  197. }
  198. public void Subscribe(string key, DataItem<object> dataItem, SubscriptionAttribute.FLAG flag)
  199. {
  200. if (string.IsNullOrWhiteSpace(key))
  201. throw new ArgumentNullException("key");
  202. if (_keyValueMap.ContainsKey(key))
  203. throw new Exception(string.Format("Duplicated Key:{0}", key));
  204. if (dataItem == null)
  205. throw new ArgumentNullException("dataItem");
  206. _keyValueMap.TryAdd(key, dataItem);
  207. }
  208. public Dictionary<string, object> Poll(IEnumerable<string> keys)
  209. {
  210. Dictionary<string, object> data = new Dictionary<string, object>();
  211. foreach (string name in keys)
  212. {
  213. if (_keyValueMap.ContainsKey(name))
  214. data[name] =_keyValueMap[name].Value;
  215. }
  216. return data;
  217. }
  218. public object Poll(string key)
  219. {
  220. return _keyValueMap.ContainsKey(key) ? _keyValueMap[key].Value : null;
  221. }
  222. public void Traverse(object instance, string keyPrefix)
  223. {
  224. Parallel.ForEach(instance.GetType().GetFields().Where<FieldInfo>(_hasSubscriptionAttribute), fi =>
  225. {
  226. string key = Parse(fi);
  227. key = string.IsNullOrWhiteSpace(keyPrefix) ? key : string.Format("{0}.{1}", keyPrefix, key);
  228. Subscribe(key, () => fi.GetValue(instance), 0);
  229. });
  230. Parallel.ForEach(instance.GetType().GetProperties().Where<PropertyInfo>(_hasSubscriptionAttribute), property =>
  231. {
  232. string key = Parse(property);
  233. key = string.IsNullOrWhiteSpace(keyPrefix) ? key : string.Format("{0}.{1}", keyPrefix, key);
  234. Subscribe(key, () => property.GetValue(instance, null), 0);
  235. });
  236. }
  237. string Parse(MemberInfo member)
  238. {
  239. return _hasSubscriptionAttribute(member) ? (member.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute).Key : null;
  240. }
  241. public Dictionary<string, object> PollData(IEnumerable<string> keys)
  242. {
  243. Dictionary<string, object> result = new Dictionary<string,object>();
  244. foreach (string key in keys)
  245. {
  246. if (_keyValueMap.ContainsKey(key))
  247. result[key] = _keyValueMap[key].Value;
  248. else
  249. {
  250. //LOG.Error("未定义的DataItem:" + key);
  251. }
  252. }
  253. return result;
  254. }
  255. }
  256. }