AlarmListManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.RT.OperationCenter;
  4. using Aitex.Core.Util;
  5. using CyberX8_Core;
  6. using DocumentFormat.OpenXml.Packaging;
  7. using MECF.Framework.Common.CommonData;
  8. using MECF.Framework.Common.DBCore;
  9. using MECF.Framework.Common.Equipment;
  10. using System;
  11. using System.Collections.Concurrent;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Reflection;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. namespace MECF.Framework.Common.Alarm
  18. {
  19. public class AlarmListManager : Singleton<AlarmListManager>
  20. {
  21. #region 内部变量
  22. /// <summary>
  23. /// 模块报警字典
  24. /// </summary>
  25. private ConcurrentDictionary<string, AlarmList> _moduleAlarmDictionary = new ConcurrentDictionary<string, AlarmList>();
  26. /// <summary>
  27. /// 模块数据告警字典(key-模块数据项,value-warn对象)
  28. /// </summary>
  29. private ConcurrentDictionary<string, AlarmList> _moduleDataItemWarnDictionary = new ConcurrentDictionary<string, AlarmList>();
  30. /// <summary>
  31. /// 模块报警数据
  32. /// </summary>
  33. List<AlarmList> _alarmLists = new List<AlarmList>();
  34. #endregion
  35. /// <summary>
  36. /// 初始化
  37. /// </summary>
  38. public void Initialize()
  39. {
  40. _alarmLists = AlarmListRecorder.GetAllAlarmListDatas();
  41. DATA.Subscribe("System.AlarmList", () => _alarmLists, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  42. OP.Subscribe($"Alarm.Acknowledge", AcknowledgeAction);
  43. foreach (AlarmList item in _alarmLists)
  44. {
  45. if (item.AlarmType == (int)AlarmType.Error)
  46. {
  47. _moduleAlarmDictionary[item.ModuleName] = item;
  48. }
  49. else if (item.AlarmType == (int)AlarmType.Warning || item.AlarmType == (int)AlarmType.DataError)
  50. {
  51. _moduleDataItemWarnDictionary[$"{item.ModuleName}.{item.DataItem}"] = item;
  52. }
  53. }
  54. }
  55. public bool AcknowledgeAction(string cmd, object[] args)
  56. {
  57. string moduleName = args[0].ToString();
  58. string dataItem = args[1].ToString();
  59. Acknowledge(moduleName, dataItem);
  60. return true;
  61. }
  62. /// <summary>
  63. /// 是否包含
  64. /// </summary>
  65. /// <param name="moduleName"></param>
  66. /// <returns></returns>
  67. public bool IsContained(string moduleName)
  68. {
  69. return _moduleAlarmDictionary.ContainsKey(moduleName);
  70. }
  71. /// <summary>
  72. /// 增加报警
  73. /// </summary>
  74. /// <param name="alarm"></param>
  75. public void AddAlarm(AlarmList alarm)
  76. {
  77. if (!_moduleAlarmDictionary.ContainsKey(alarm.ModuleName))
  78. {
  79. _moduleAlarmDictionary[alarm.ModuleName] = alarm;
  80. AlarmListRecorder.AddAlarmList(alarm);
  81. _alarmLists.Add(alarm);
  82. }
  83. else
  84. {
  85. _moduleAlarmDictionary[alarm.ModuleName] = alarm;
  86. AlarmListRecorder.UpdateAlarmList(alarm);
  87. AlarmList alarmList = _alarmLists.Find(O => O.ModuleName == alarm.ModuleName && O.AlarmType == (int)AlarmType.Error);
  88. if (alarmList != null)
  89. {
  90. alarmList.Clone(alarm);
  91. }
  92. }
  93. }
  94. /// <summary>
  95. /// 增加Warn
  96. /// </summary>
  97. /// <param name="moduleName"></param>
  98. /// <param name="dataItem"></param>
  99. /// <param name="warnMsg"></param>
  100. public bool AddWarn(string moduleName, string dataItem, string warnMsg)
  101. {
  102. string str = $"{moduleName}.{dataItem}";
  103. if (!_moduleDataItemWarnDictionary.ContainsKey(str))
  104. {
  105. AlarmList alarm = new AlarmList();
  106. alarm.ModuleName = moduleName;
  107. alarm.DataItem = dataItem;
  108. alarm.AlarmMsg = warnMsg;
  109. alarm.AlarmType = (int)AlarmType.Warning;
  110. alarm.CreateTime = DateTime.Now;
  111. _moduleDataItemWarnDictionary[str] = alarm;
  112. AlarmListRecorder.AddAlarmList(alarm);
  113. _alarmLists.Add(alarm);
  114. return true;
  115. }
  116. return false;
  117. }
  118. /// <summary>
  119. /// 增加DataError
  120. /// </summary>
  121. /// <param name="moduleName"></param>
  122. /// <param name="dataItem"></param>
  123. /// <param name="warnMsg"></param>
  124. public bool AddDataError(string moduleName, string dataItem, string warnMsg)
  125. {
  126. string str = $"{moduleName}.{dataItem}";
  127. if (!_moduleDataItemWarnDictionary.ContainsKey(str))
  128. {
  129. AlarmList alarm = new AlarmList();
  130. alarm.ModuleName = moduleName;
  131. alarm.DataItem = dataItem;
  132. alarm.AlarmMsg = warnMsg;
  133. alarm.AlarmType = (int)AlarmType.DataError;
  134. alarm.CreateTime = DateTime.Now;
  135. _moduleDataItemWarnDictionary[str] = alarm;
  136. AlarmListRecorder.AddAlarmList(alarm);
  137. _alarmLists.Add(alarm);
  138. return true;
  139. }
  140. return false;
  141. }
  142. /// <summary>
  143. /// 确认
  144. /// </summary>
  145. /// <param name="moduleName"></param>
  146. /// <param name="dataItem"></param>
  147. public void Acknowledge(string moduleName, string dataItem)
  148. {
  149. string str = $"{moduleName}.{dataItem}";
  150. if (_moduleDataItemWarnDictionary.ContainsKey(str))
  151. {
  152. _moduleDataItemWarnDictionary.TryRemove(str, out AlarmList alarmList);
  153. if (alarmList != null)
  154. {
  155. AlarmListRecorder.RemoveAlarmList(alarmList);
  156. AlarmList alarm = _alarmLists.Find(O => O.ModuleName == moduleName && (O.AlarmType == (int)AlarmType.Warning || O.AlarmType == (int)AlarmType.DataError) && O.DataItem == alarmList.DataItem);
  157. if (alarm != null)
  158. {
  159. _alarmLists.Remove(alarm);
  160. }
  161. }
  162. }
  163. }
  164. /// <summary>
  165. /// 获取模块报警信息
  166. /// </summary>
  167. /// <param name="moduleName"></param>
  168. /// <returns></returns>
  169. public AlarmList GetAlarmListByModule(string moduleName)
  170. {
  171. return _moduleAlarmDictionary.ContainsKey(moduleName) ? _moduleAlarmDictionary[moduleName] : null;
  172. }
  173. /// <summary>
  174. /// 检验模块Alarm确认后移除
  175. /// </summary>
  176. /// <param name="moduleName"></param>
  177. /// <param name="state"></param>
  178. public void CheckModuleAlamAndRemove(string moduleName, string state)
  179. {
  180. if (IsContained(moduleName))
  181. {
  182. AlarmList alarmList = GetAlarmListByModule(moduleName);
  183. if (alarmList != null)
  184. {
  185. if (alarmList.ModuleState == state || string.IsNullOrEmpty(state))
  186. {
  187. RemoveAlarm(moduleName);
  188. AlarmList alarm = _alarmLists.Find(O => O.ModuleName == moduleName);
  189. if (alarm != null)
  190. {
  191. _alarmLists.Remove(alarm);
  192. }
  193. }
  194. }
  195. }
  196. }
  197. /// <summary>
  198. /// 移除报警
  199. /// </summary>
  200. /// <param name="moduleName"></param>
  201. private void RemoveAlarm(string moduleName)
  202. {
  203. if (_moduleAlarmDictionary.ContainsKey(moduleName))
  204. {
  205. _moduleAlarmDictionary.TryRemove(moduleName, out AlarmList alarmList);
  206. if (alarmList != null)
  207. {
  208. AlarmListRecorder.RemoveAlarmList(alarmList);
  209. int index = _alarmLists.FindIndex(O => O.ModuleName == moduleName && O.AlarmType == (int)AlarmType.Error);
  210. if (index != -1)
  211. {
  212. _alarmLists.RemoveAt(index);
  213. }
  214. }
  215. }
  216. }
  217. /// <summary>
  218. /// 初始化成功时移除相关的DataError
  219. /// </summary>
  220. /// <param name="moduleName"></param>
  221. public void RemoveDataError(string moduleName)
  222. {
  223. foreach (var item in _moduleDataItemWarnDictionary.Keys)
  224. {
  225. if (item.Contains(moduleName) && _moduleDataItemWarnDictionary[item].AlarmType == (int)AlarmType.DataError)
  226. {
  227. _moduleDataItemWarnDictionary.TryRemove(item, out AlarmList alarmList);
  228. if (alarmList != null)
  229. {
  230. AlarmListRecorder.RemoveAlarmList(alarmList);
  231. AlarmList alarm = _alarmLists.Find(O => O.ModuleName == moduleName && O.DataItem == alarmList.DataItem);
  232. if (alarm != null)
  233. {
  234. _alarmLists.Remove(alarm);
  235. }
  236. }
  237. }
  238. }
  239. }
  240. /// <summary>
  241. /// 判断是否存在dataerror
  242. /// </summary>
  243. /// <param name="module"></param>
  244. /// <param name="dataItem"></param>
  245. /// <returns></returns>
  246. public bool IsContainDataError(string module, string dataItem)
  247. {
  248. bool result = false;
  249. result = _moduleDataItemWarnDictionary.ContainsKey($"{module}.{dataItem}");
  250. return result;
  251. }
  252. /// <summary>
  253. /// 清除所有报警
  254. /// </summary>
  255. public void ClearAllAlarm()
  256. {
  257. _alarmLists.Clear();
  258. _moduleAlarmDictionary.Clear();
  259. AlarmListRecorder.RemoveAllAlarmList();
  260. }
  261. }
  262. }