123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections.Concurrent;
- using System.Reflection;
- using Aitex.Core.Util;
- using System.Threading.Tasks;
- using Aitex.Core.RT.Log;
- namespace Aitex.Core.RT.DataCenter
- {
- /// <summary>
- ///
- /// 数据项命名规则:
- ///
- /// 一般数据项: 模块名.数据项名
- ///
- /// 设备类型数据项:模块名.设备类型.数据项名
- ///
- /// 界面显示设备数据项:模块名.数据项名
- ///
- /// </summary>
- public class DataManager : ICommonData
- {
- ConcurrentDictionary<string, DataItem<object>> _keyValueMap;
- SortedDictionary<string, Func<object>> _dbRecorderList;
- Func<object, bool> _isSubscriptionAttribute;
- Func<MemberInfo, bool> _hasSubscriptionAttribute;
- object _locker = new object();
- public DataManager()
- {
- }
- public void Initialize()
- {
- _dbRecorderList = new SortedDictionary<string, Func<object>>();
- _keyValueMap = new ConcurrentDictionary<string, DataItem<object>>();
- _isSubscriptionAttribute = attribute => attribute is SubscriptionAttribute;
- _hasSubscriptionAttribute = mi => mi.GetCustomAttributes(false).Any(_isSubscriptionAttribute);
- DATA.InnerDataManager = this;
- }
- public SortedDictionary<string, Func<object>> GetDBRecorderList()
- {
- SortedDictionary<string, Func<object>> result;
- lock (_locker)
- {
- result = new SortedDictionary<string, Func<object>>(_dbRecorderList);
- }
- return result;
- }
- public void Subscribe<T>(T instance, string keyPrefix = null) where T : class
- {
- if (instance == null)
- throw new ArgumentNullException("instance");
- Traverse(instance, keyPrefix);
- }
- public void Subscribe(string key, Func<object> getter, SubscriptionAttribute.FLAG flag)
- {
- if (string.IsNullOrWhiteSpace(key))
- throw new ArgumentNullException("key");
- if (_keyValueMap.ContainsKey(key))
- throw new Exception(string.Format("Duplicated Key:{0}", key));
- if (getter == null)
- throw new ArgumentNullException("getter");
- _keyValueMap.TryAdd(key, new DataItem<object>(getter));
- if (flag != SubscriptionAttribute.FLAG.IgnoreSaveDB)
- {
- lock (_locker)
- {
- _dbRecorderList[key] = getter;
- }
- }
- }
- public Dictionary<string, object> Poll(IEnumerable<string> keys)
- {
- Dictionary<string, object> data = new Dictionary<string, object>();
- foreach (string name in keys)
- {
- if (_keyValueMap.ContainsKey(name))
- data[name] =_keyValueMap[name].Value;
- }
- return data;
- }
- public object Poll(string key)
- {
- return _keyValueMap.ContainsKey(key) ? _keyValueMap[key].Value : null;
- }
- public void Traverse(object instance, string keyPrefix)
- {
- Parallel.ForEach(instance.GetType().GetFields().Where<FieldInfo>(_hasSubscriptionAttribute), fi =>
- {
- string key = Parse(fi);
- key = string.IsNullOrWhiteSpace(keyPrefix) ? key : string.Format("{0}.{1}", keyPrefix, key);
- Subscribe(key, () => fi.GetValue(instance), 0);
- });
- Parallel.ForEach(instance.GetType().GetProperties().Where<PropertyInfo>(_hasSubscriptionAttribute), property =>
- {
- string key = Parse(property);
- key = string.IsNullOrWhiteSpace(keyPrefix) ? key : string.Format("{0}.{1}", keyPrefix, key);
- Subscribe(key, () => property.GetValue(instance, null), 0);
- });
- }
- string Parse(MemberInfo member)
- {
- return _hasSubscriptionAttribute(member) ? (member.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute).Key : null;
- }
- public Dictionary<string, object> PollData(IEnumerable<string> keys)
- {
- Dictionary<string, object> result = new Dictionary<string,object>();
- foreach (string key in keys)
- {
- if (_keyValueMap.ContainsKey(key))
- result[key] = _keyValueMap[key].Value;
- else
- {
- //LOG.Error("未定义的DataItem:" + key);
- }
- }
- return result;
- }
- }
- }
|