终端一体化运控平台
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

289 行
12 KiB

  1. using BPASmartClient.CustomResource.Pages.ViewModel;
  2. using BPASmartClient.Helper;
  3. using BPASmartClient.Model;
  4. using System;
  5. using System.Collections.Concurrent;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. namespace BPASmartClient.CustomResource.Pages.Model
  14. {
  15. public class AlarmHelper<AlarmT> where AlarmT : class, new()
  16. {
  17. private volatile static ConcurrentDictionary<string, AlarmT> _Instance;
  18. private static readonly object Obj_Lock = new object();
  19. public static AlarmT GetInstance(string name = "")
  20. {
  21. lock (Obj_Lock)
  22. {
  23. if (string.IsNullOrEmpty(name)) name = typeof(AlarmT).Name;
  24. if (_Instance == null) _Instance = new ConcurrentDictionary<string, AlarmT>();
  25. if (!_Instance.ContainsKey(name)) _Instance.TryAdd(name, new AlarmT());
  26. return _Instance[name];
  27. }
  28. }
  29. private AlarmHelper() { }
  30. public static ObservableCollection<Alarm> Alarms { get; set; } = new ObservableCollection<Alarm>();
  31. public static List<Alarm> HistoryAlarms { get; set; } = new List<Alarm>();
  32. static ConcurrentDictionary<string, bool> flagbit = new ConcurrentDictionary<string, bool>();
  33. static ConcurrentDictionary<string, Delay> delays = new ConcurrentDictionary<string, Delay>();
  34. public static Action<string> AddAction { get; set; }
  35. public static Action<string> RemoveAction { get; set; }
  36. public static Action ChangeAction { get; set; }
  37. //public static AlarmT Alarm { get; set; } = new AlarmT();
  38. public static void Init()
  39. {
  40. AlarmViewModel.AlarmInfos = Alarms;
  41. ThreadManage.GetInstance().StartLong(new Action(() =>
  42. {
  43. if (_Instance != null)
  44. {
  45. foreach (var temp in _Instance)
  46. {
  47. foreach (var item in temp.Value.GetType().GetProperties())
  48. {
  49. if (item.CustomAttributes.Count() > 0)
  50. {
  51. var AlarmModel = item.GetCustomAttribute<AlarmAttribute>();
  52. if (AlarmModel != null)
  53. {
  54. bool value = Convert.ToBoolean(_Instance[temp.Key].GetType().GetProperty(item.Name)?.GetValue(_Instance[temp.Key]));
  55. string text = typeof(AlarmT).Name == temp.Key ? AlarmModel.AlarmInfo : $"{temp.Key}:{AlarmModel.AlarmInfo}";
  56. EdgeAlarm(value, text, 1, AlarmModel.AlarmLevel, AlarmModel.AlarmType);
  57. }
  58. }
  59. }
  60. }
  61. }
  62. Thread.Sleep(100);
  63. }), $"{typeof(AlarmT).Name},报警通用模块监听");
  64. }
  65. /// <summary>
  66. /// 沿报警检测
  67. /// </summary>
  68. /// <param name="Trigger">触发变量</param>
  69. /// <param name="text">报警信息</param>
  70. /// <param name="edgeType">触发类型,上升沿 或 下降沿</param>
  71. private static void EdgeAlarm(bool Trigger, string text, int delay = 2, AlarmLevel alarmLevel = AlarmLevel.一般报警, AlarmTriggerType edgeType = AlarmTriggerType.Rising)
  72. {
  73. if (!flagbit.ContainsKey(text)) flagbit.TryAdd(text, false);
  74. if (!delays.ContainsKey(text)) delays.TryAdd(text, Delay.GetInstance(text));
  75. if (edgeType == AlarmTriggerType.Rising ? delays[text].Start(Trigger, delay) : delays[text].Start(!Trigger, delay))
  76. {
  77. if (edgeType == AlarmTriggerType.Rising ? !flagbit[text] : flagbit[text])
  78. {
  79. AddAlarm(Trigger, text, alarmLevel);
  80. flagbit[text] = edgeType == AlarmTriggerType.Rising ? true : false;
  81. }
  82. }
  83. else RemoveAlarm(text);
  84. if (edgeType == AlarmTriggerType.Rising ? flagbit[text] : !flagbit[text]) flagbit[text] = Trigger;
  85. }
  86. /// <summary>
  87. /// 添加报警信息
  88. /// </summary>
  89. /// <param name="AlarmInfo">报警信息</param>
  90. private static void AddAlarm(object value, string AlarmInfo, AlarmLevel alarmLevel)
  91. {
  92. Alarm tempAlarm = new Alarm()
  93. {
  94. NumId = Alarms.Count + 1,
  95. Date = DateTime.Now.ToString("yyyy/MM/dd"),
  96. Grade = alarmLevel.ToString(),
  97. Info = AlarmInfo,
  98. Value = value.ToString(),
  99. Time = DateTime.Now.ToString("HH:mm:ss"),
  100. };
  101. var res = Sqlite<BPASmartClient.Model.Alarm>.GetInstance.Base.Add(tempAlarm);
  102. try
  103. {
  104. Sqlite<BPASmartClient.Model.Alarm>.GetInstance.Save();
  105. }
  106. catch (Exception)
  107. {
  108. //throw;
  109. }
  110. if (Alarms.FirstOrDefault(p => p.Info == AlarmInfo) == null)
  111. {
  112. Application.Current.Dispatcher.Invoke(new Action(() =>
  113. {
  114. Alarms.Insert(0, tempAlarm);
  115. for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
  116. }));
  117. AddAction?.Invoke(AlarmInfo);//添加报警通知
  118. ChangeAction?.Invoke();//更改报警通知
  119. }
  120. }
  121. /// <summary>
  122. /// 移除报警信息
  123. /// </summary>
  124. /// <param name="AlarmInfo">报警信息</param>
  125. private static void RemoveAlarm(string AlarmInfo)
  126. {
  127. var result = Alarms.FirstOrDefault(p => p.Info == AlarmInfo);
  128. if (result != null)
  129. {
  130. Application.Current.Dispatcher.Invoke(new Action(() =>
  131. {
  132. Alarms.Remove(result);
  133. for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
  134. }));
  135. if (RemoveAction != null) RemoveAction(AlarmInfo);
  136. if (ChangeAction != null) ChangeAction();
  137. }
  138. }
  139. }
  140. //public class AlarmHelper<AlarmT> where AlarmT : class, new()
  141. //{
  142. // //private volatile static ConcurrentDictionary<string, AlarmT> _Instance;
  143. // //public static AlarmT GetInstance(string name)
  144. // //{
  145. // // if (_Instance == null) _Instance = new ConcurrentDictionary<string, AlarmT>();
  146. // // if (!_Instance.ContainsKey(name)) _Instance.TryAdd(name, new AlarmT());
  147. // // return _Instance[name];
  148. // //}
  149. // //private AlarmHelper() { }
  150. // public static ObservableCollection<Alarm> Alarms { get; set; } = new ObservableCollection<Alarm>();
  151. // public static List<Alarm> HistoryAlarms { get; set; } = new List<Alarm>();
  152. // static ConcurrentDictionary<string, bool> flagbit = new ConcurrentDictionary<string, bool>();
  153. // static ConcurrentDictionary<string, Delay> delays = new ConcurrentDictionary<string, Delay>();
  154. // public static Action<string> AddAction { get; set; }
  155. // public static Action<string> RemoveAction { get; set; }
  156. // public static Action ChangeAction { get; set; }
  157. // public static AlarmT Alarm { get; set; } = new AlarmT();
  158. // public static void Init()
  159. // {
  160. // AlarmViewModel.AlarmInfos = Alarms;
  161. // ThreadManage.GetInstance().StartLong(new Action(() =>
  162. // {
  163. // foreach (var item in Alarm.GetType().GetProperties())
  164. // {
  165. // if (item.CustomAttributes.Count() > 0)
  166. // {
  167. // var AlarmModel = item.GetCustomAttribute<AlarmAttribute>();
  168. // if (AlarmModel != null)
  169. // {
  170. // bool value = Convert.ToBoolean(Alarm.GetType().GetProperty(item.Name)?.GetValue(Alarm));
  171. // EdgeAlarm(value, AlarmModel.AlarmInfo, 1, AlarmModel.AlarmLevel, AlarmModel.AlarmType);
  172. // }
  173. // }
  174. // }
  175. // Thread.Sleep(100);
  176. // }), $"{typeof(AlarmT).Name},报警通用模块监听");
  177. // }
  178. // //public static void AnalogAlarm(dynamic Trigger, string info, dynamic HH = null, dynamic H = 0, dynamic L = 0, dynamic LL = 0)
  179. // //{
  180. // //}
  181. // /// <summary>
  182. // /// 沿报警检测
  183. // /// </summary>
  184. // /// <param name="Trigger">触发变量</param>
  185. // /// <param name="text">报警信息</param>
  186. // /// <param name="edgeType">触发类型,上升沿 或 下降沿</param>
  187. // private static void EdgeAlarm(bool Trigger, string text, int delay = 2, AlarmLevel alarmLevel = AlarmLevel.一般报警, AlarmTriggerType edgeType = AlarmTriggerType.Rising)
  188. // {
  189. // if (!flagbit.ContainsKey(text)) flagbit.TryAdd(text, false);
  190. // if (!delays.ContainsKey(text)) delays.TryAdd(text, Delay.GetInstance(text));
  191. // if (edgeType == AlarmTriggerType.Rising ? delays[text].Start(Trigger, delay) : delays[text].Start(!Trigger, delay))
  192. // {
  193. // if (edgeType == AlarmTriggerType.Rising ? !flagbit[text] : flagbit[text])
  194. // {
  195. // AddAlarm(Trigger, text, alarmLevel);
  196. // flagbit[text] = edgeType == AlarmTriggerType.Rising ? true : false;
  197. // }
  198. // }
  199. // else RemoveAlarm(text);
  200. // if (edgeType == AlarmTriggerType.Rising ? flagbit[text] : !flagbit[text]) flagbit[text] = Trigger;
  201. // }
  202. // /// <summary>
  203. // /// 添加报警信息
  204. // /// </summary>
  205. // /// <param name="AlarmInfo">报警信息</param>
  206. // private static void AddAlarm(object value, string AlarmInfo, AlarmLevel alarmLevel)
  207. // {
  208. // Alarm tempAlarm = new Alarm()
  209. // {
  210. // NumId = Alarms.Count + 1,
  211. // Date = DateTime.Now.ToString("yyyy/MM/dd"),
  212. // Grade = alarmLevel.ToString(),
  213. // Info = AlarmInfo,
  214. // Value = value.ToString(),
  215. // Time = DateTime.Now.ToString("HH:mm:ss"),
  216. // };
  217. // var res = Sqlite<Alarm>.GetInstance.Base.Add(tempAlarm);
  218. // Sqlite<Alarm>.GetInstance.Save();
  219. // if (Alarms.FirstOrDefault(p => p.Info == AlarmInfo) == null)
  220. // {
  221. // Application.Current.Dispatcher.Invoke(new Action(() =>
  222. // {
  223. // Alarms.Insert(0, tempAlarm);
  224. // for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
  225. // }));
  226. // AddAction?.Invoke(AlarmInfo);//添加报警通知
  227. // ChangeAction?.Invoke();//更改报警通知
  228. // }
  229. // }
  230. // /// <summary>
  231. // /// 移除报警信息
  232. // /// </summary>
  233. // /// <param name="AlarmInfo">报警信息</param>
  234. // private static void RemoveAlarm(string AlarmInfo)
  235. // {
  236. // var result = Alarms.FirstOrDefault(p => p.Info == AlarmInfo);
  237. // if (result != null)
  238. // {
  239. // Application.Current.Dispatcher.Invoke(new Action(() =>
  240. // {
  241. // Alarms.Remove(result);
  242. // for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
  243. // }));
  244. // if (RemoveAction != null) RemoveAction(AlarmInfo);
  245. // if (ChangeAction != null) ChangeAction();
  246. // }
  247. // }
  248. //}
  249. }