|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- using BPA.Helper;
- using BPASmartClient.Model;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Reflection;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
-
- namespace BPASmartClient.CustomResource.Pages.Model
- {
-
- public class AlarmHelper<AlarmT> where AlarmT : class, new()
- {
- private volatile static ConcurrentDictionary<string, AlarmT> _Instance;
- private static readonly object Obj_Lock = new object();
- public static AlarmT GetInstance(string name = "")
- {
- lock (Obj_Lock)
- {
- if (string.IsNullOrEmpty(name)) name = typeof(AlarmT).Name;
- if (_Instance == null) _Instance = new ConcurrentDictionary<string, AlarmT>();
- if (!_Instance.ContainsKey(name)) _Instance.TryAdd(name, new AlarmT());
- return _Instance[name];
- }
- }
- private AlarmHelper() { }
-
-
- public static List<Alarm> Alarms { get; set; } = new List<Alarm>();
- public static List<Alarm> HistoryAlarms { get; set; } = new List<Alarm>();
- static ConcurrentDictionary<string, bool> flagbit = new ConcurrentDictionary<string, bool>();
- static ConcurrentDictionary<string, Delay> delays = new ConcurrentDictionary<string, Delay>();
- public static Action<string> AddAction { get; set; }
- public static Action<string> RemoveAction { get; set; }
- public static Action ChangeAction { get; set; }
-
- //public static AlarmT Alarm { get; set; } = new AlarmT();
-
- public static void Init()
- {
- //AlarmViewModel.AlarmInfos = Alarms;
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- if (_Instance != null)
- {
- foreach (var temp in _Instance)
- {
- foreach (var item in temp.Value.GetType().GetProperties())
- {
- if (item.CustomAttributes.Count() > 0)
- {
- var AlarmModel = item.GetCustomAttribute<AlarmAttribute>();
- if (AlarmModel != null)
- {
- bool value = Convert.ToBoolean(_Instance[temp.Key].GetType().GetProperty(item.Name)?.GetValue(_Instance[temp.Key]));
- string text = typeof(AlarmT).Name == temp.Key ? AlarmModel.AlarmInfo : $"{temp.Key}:{AlarmModel.AlarmInfo}";
- EdgeAlarm(value, text, 1, AlarmModel.AlarmLevel, AlarmModel.AlarmType);
- }
- }
- }
- }
- }
- Thread.Sleep(100);
- }), $"{typeof(AlarmT).Name},报警通用模块监听");
-
- }
-
- /// <summary>
- /// 沿报警检测
- /// </summary>
- /// <param name="Trigger">触发变量</param>
- /// <param name="text">报警信息</param>
- /// <param name="edgeType">触发类型,上升沿 或 下降沿</param>
- private static void EdgeAlarm(bool Trigger, string text, int delay = 2, AlarmLevel alarmLevel = AlarmLevel.一般报警, AlarmTriggerType edgeType = AlarmTriggerType.Rising)
- {
- if (!flagbit.ContainsKey(text)) flagbit.TryAdd(text, false);
- if (!delays.ContainsKey(text)) delays.TryAdd(text, Delay.GetInstance(text));
- if (edgeType == AlarmTriggerType.Rising ? delays[text].Start(Trigger, delay) : delays[text].Start(!Trigger, delay))
- {
- if (edgeType == AlarmTriggerType.Rising ? !flagbit[text] : flagbit[text])
- {
- AddAlarm(Trigger, text, alarmLevel);
- flagbit[text] = edgeType == AlarmTriggerType.Rising ? true : false;
- }
- }
- else RemoveAlarm(text);
- if (edgeType == AlarmTriggerType.Rising ? flagbit[text] : !flagbit[text]) flagbit[text] = Trigger;
- }
-
- /// <summary>
- /// 添加报警信息
- /// </summary>
- /// <param name="AlarmInfo">报警信息</param>
- private static void AddAlarm(object value, string AlarmInfo, AlarmLevel alarmLevel)
- {
- Alarm tempAlarm = new Alarm()
- {
- NumId = Alarms.Count + 1,
- Date = DateTime.Now.ToString("yyyy/MM/dd"),
- Grade = alarmLevel.ToString(),
- Info = AlarmInfo,
- Value = value.ToString(),
- Time = DateTime.Now.ToString("HH:mm:ss"),
- };
-
- var res = Sqlite<Alarm>.GetInstance.Base.Add(tempAlarm);
- Sqlite<Alarm>.GetInstance.Save();
-
- if (Alarms.FirstOrDefault(p => p.Info == AlarmInfo) == null)
- {
- Alarms.Insert(0, tempAlarm);
- for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
-
- AddAction?.Invoke(AlarmInfo);//添加报警通知
- ChangeAction?.Invoke();//更改报警通知
- }
- }
-
- /// <summary>
- /// 移除报警信息
- /// </summary>
- /// <param name="AlarmInfo">报警信息</param>
- private static void RemoveAlarm(string AlarmInfo)
- {
- var result = Alarms.FirstOrDefault(p => p.Info == AlarmInfo);
- if (result != null)
- {
- Alarms.Remove(result);
- for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
-
- if (RemoveAction != null) RemoveAction(AlarmInfo);
- if (ChangeAction != null) ChangeAction();
- }
- }
-
- }
-
- //public class AlarmHelper<AlarmT> where AlarmT : class, new()
- //{
-
-
- // //private volatile static ConcurrentDictionary<string, AlarmT> _Instance;
- // //public static AlarmT GetInstance(string name)
- // //{
- // // if (_Instance == null) _Instance = new ConcurrentDictionary<string, AlarmT>();
- // // if (!_Instance.ContainsKey(name)) _Instance.TryAdd(name, new AlarmT());
- // // return _Instance[name];
- // //}
- // //private AlarmHelper() { }
-
-
- // public static ObservableCollection<Alarm> Alarms { get; set; } = new ObservableCollection<Alarm>();
- // public static List<Alarm> HistoryAlarms { get; set; } = new List<Alarm>();
- // static ConcurrentDictionary<string, bool> flagbit = new ConcurrentDictionary<string, bool>();
- // static ConcurrentDictionary<string, Delay> delays = new ConcurrentDictionary<string, Delay>();
- // public static Action<string> AddAction { get; set; }
- // public static Action<string> RemoveAction { get; set; }
- // public static Action ChangeAction { get; set; }
-
- // public static AlarmT Alarm { get; set; } = new AlarmT();
-
- // public static void Init()
- // {
- // AlarmViewModel.AlarmInfos = Alarms;
- // ThreadManage.GetInstance().StartLong(new Action(() =>
- // {
- // foreach (var item in Alarm.GetType().GetProperties())
- // {
- // if (item.CustomAttributes.Count() > 0)
- // {
- // var AlarmModel = item.GetCustomAttribute<AlarmAttribute>();
- // if (AlarmModel != null)
- // {
- // bool value = Convert.ToBoolean(Alarm.GetType().GetProperty(item.Name)?.GetValue(Alarm));
- // EdgeAlarm(value, AlarmModel.AlarmInfo, 1, AlarmModel.AlarmLevel, AlarmModel.AlarmType);
- // }
- // }
- // }
- // Thread.Sleep(100);
- // }), $"{typeof(AlarmT).Name},报警通用模块监听");
-
- // }
-
-
- // //public static void AnalogAlarm(dynamic Trigger, string info, dynamic HH = null, dynamic H = 0, dynamic L = 0, dynamic LL = 0)
- // //{
-
- // //}
-
-
-
-
- // /// <summary>
- // /// 沿报警检测
- // /// </summary>
- // /// <param name="Trigger">触发变量</param>
- // /// <param name="text">报警信息</param>
- // /// <param name="edgeType">触发类型,上升沿 或 下降沿</param>
- // private static void EdgeAlarm(bool Trigger, string text, int delay = 2, AlarmLevel alarmLevel = AlarmLevel.一般报警, AlarmTriggerType edgeType = AlarmTriggerType.Rising)
- // {
- // if (!flagbit.ContainsKey(text)) flagbit.TryAdd(text, false);
- // if (!delays.ContainsKey(text)) delays.TryAdd(text, Delay.GetInstance(text));
- // if (edgeType == AlarmTriggerType.Rising ? delays[text].Start(Trigger, delay) : delays[text].Start(!Trigger, delay))
- // {
- // if (edgeType == AlarmTriggerType.Rising ? !flagbit[text] : flagbit[text])
- // {
- // AddAlarm(Trigger, text, alarmLevel);
- // flagbit[text] = edgeType == AlarmTriggerType.Rising ? true : false;
- // }
- // }
- // else RemoveAlarm(text);
- // if (edgeType == AlarmTriggerType.Rising ? flagbit[text] : !flagbit[text]) flagbit[text] = Trigger;
- // }
-
- // /// <summary>
- // /// 添加报警信息
- // /// </summary>
- // /// <param name="AlarmInfo">报警信息</param>
- // private static void AddAlarm(object value, string AlarmInfo, AlarmLevel alarmLevel)
- // {
- // Alarm tempAlarm = new Alarm()
- // {
- // NumId = Alarms.Count + 1,
- // Date = DateTime.Now.ToString("yyyy/MM/dd"),
- // Grade = alarmLevel.ToString(),
- // Info = AlarmInfo,
- // Value = value.ToString(),
- // Time = DateTime.Now.ToString("HH:mm:ss"),
- // };
-
- // var res = Sqlite<Alarm>.GetInstance.Base.Add(tempAlarm);
- // Sqlite<Alarm>.GetInstance.Save();
-
- // if (Alarms.FirstOrDefault(p => p.Info == AlarmInfo) == null)
- // {
- // Application.Current.Dispatcher.Invoke(new Action(() =>
- // {
- // Alarms.Insert(0, tempAlarm);
- // for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
- // }));
-
- // AddAction?.Invoke(AlarmInfo);//添加报警通知
- // ChangeAction?.Invoke();//更改报警通知
- // }
- // }
-
- // /// <summary>
- // /// 移除报警信息
- // /// </summary>
- // /// <param name="AlarmInfo">报警信息</param>
- // private static void RemoveAlarm(string AlarmInfo)
- // {
- // var result = Alarms.FirstOrDefault(p => p.Info == AlarmInfo);
- // if (result != null)
- // {
- // Application.Current.Dispatcher.Invoke(new Action(() =>
- // {
- // Alarms.Remove(result);
- // for (int i = 0; i < Alarms.Count; i++) { Alarms.ElementAt(i).NumId = i + 1; }
- // }));
-
- // if (RemoveAction != null) RemoveAction(AlarmInfo);
- // if (ChangeAction != null) ChangeAction();
- // }
- // }
-
- //}
- }
|