From c65e0a62ae3159836355cc86251125c9c886fcbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=84=8F=20=E5=BD=AD?= <2417589739@qq.com> Date: Wed, 11 May 2022 16:07:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=A5=E8=AD=A6=E4=BF=A1=E6=81=AF=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BPASmartClient.Device/Alarm.cs | 56 +++++++++++++ BPASmartClient.Device/AlarmHelper.cs | 99 +++++++++++++++++++++++ BPASmartClient.Device/AlarmLevel.cs | 14 ++++ BPASmartClient.Device/AlarmTriggerType.cs | 20 +++++ BPASmartClient.Device/BaseDevice.cs | 65 +++++++++++++++ 5 files changed, 254 insertions(+) create mode 100644 BPASmartClient.Device/Alarm.cs create mode 100644 BPASmartClient.Device/AlarmHelper.cs create mode 100644 BPASmartClient.Device/AlarmLevel.cs create mode 100644 BPASmartClient.Device/AlarmTriggerType.cs diff --git a/BPASmartClient.Device/Alarm.cs b/BPASmartClient.Device/Alarm.cs new file mode 100644 index 00000000..fecb1b9d --- /dev/null +++ b/BPASmartClient.Device/Alarm.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BPASmartClient.Device +{ + public class Alarm + { + /// + /// ID + /// + [Key] + public int Id { get { return _mId; } set { _mId = value; } } + private int _mId; + + /// + /// 编号ID + /// + public int NumId { get { return _mNumId; } set { _mNumId = value; } } + private int _mNumId; + + + /// + /// 日期 + /// + public string Date { get { return _mDate; } set { _mDate = value; } } + private string _mDate; + + /// + /// 时间 + /// + public string Time { get { return _mTime; } set { _mTime = value; } } + private string _mTime; + + /// + /// 报警信息 + /// + public string Info { get { return _mInfo; } set { _mInfo = value; } } + private string _mInfo; + + /// + /// 报警值 + /// + public string Value { get { return _mValue; } set { _mValue = value; } } + private string _mValue; + + /// + /// 报警等级 + /// + public string Grade { get { return _mGrade; } set { _mGrade = value; } } + private string _mGrade; + } +} diff --git a/BPASmartClient.Device/AlarmHelper.cs b/BPASmartClient.Device/AlarmHelper.cs new file mode 100644 index 00000000..7a0fd494 --- /dev/null +++ b/BPASmartClient.Device/AlarmHelper.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BPASmartClient.Device +{ + public class AlarmHelper + { + public List Alarms { get; set; } = new List(); + public List HistoryAlarms { get; set; } = new List(); + ConcurrentDictionary flagbit = new ConcurrentDictionary(); + public Action AddAction { get; set; } + public Action RemoveAction { get; set; } + public Action ChangeAction { get; set; } + + /// + /// 沿报警检测 + /// + /// 触发变量 + /// 报警信息 + /// 触发类型,上升沿 或 下降沿 + public void EdgeAlarm(bool Trigger, string text, AlarmLevel alarmLevel = AlarmLevel.一般报警, AlarmTriggerType edgeType = AlarmTriggerType.Rising) + { + if (!flagbit.ContainsKey(text)) flagbit.TryAdd(text, false); + if (edgeType == AlarmTriggerType.Rising ? Trigger : !Trigger) + { + 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; + } + + /// + /// 添加报警信息 + /// + /// 报警信息 + private void AddAlarm(object value, string AlarmInfo, AlarmLevel alarmLevel) + { + Alarm tempAlarm = new Alarm() + { + NumId = Alarms.Count, + Date = DateTime.Now.ToString("yyyy/MM/dd"), + Grade = alarmLevel.ToString(), + Info = AlarmInfo, + Value = value.ToString(), + Time = DateTime.Now.ToString("HH:mm:ss"), + }; + + //tempAlarm.Id = IotReport.GetInstance.HttpAddAlarm(new BPA.Message.API请求.AlarmTable + //{ + // AlarmTime=DateTime.Now, + // AlarmType= tempAlarm.Grade, + // AlarmMessage= tempAlarm.Info, + // AlarmVla= tempAlarm.Value, + // ClientId= InternetInfo.ClientId.ToString() + //}); + + //Sqlite.GetInstance.Base.Add(tempAlarm); + + if (Alarms.FirstOrDefault(p => p.Info == AlarmInfo) == null) + { + Alarms.Add(tempAlarm); + if (AddAction != null) AddAction(AlarmInfo);//添加报警通知 + if (ChangeAction != null) ChangeAction();//更改报警通知 + } + } + + /// + /// 移除报警信息 + /// + /// 报警信息 + private void RemoveAlarm(string AlarmInfo) + { + var result = Alarms.FirstOrDefault(p => p.Info == AlarmInfo); + if (result != null) + { + Alarms.Remove(result); + //IotReport.GetInstance.HttpDeleteAlarm(result.Id); + for (int i = 0; i < Alarms.Count; i++) + { + Alarms.ElementAt(i).NumId = i + 1; + } + if (RemoveAction != null) RemoveAction(AlarmInfo); + if (ChangeAction != null) ChangeAction(); + } + } + + } +} diff --git a/BPASmartClient.Device/AlarmLevel.cs b/BPASmartClient.Device/AlarmLevel.cs new file mode 100644 index 00000000..b7196ebc --- /dev/null +++ b/BPASmartClient.Device/AlarmLevel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BPASmartClient.Device +{ + public enum AlarmLevel + { + 一般报警, + 严重报警 + } +} diff --git a/BPASmartClient.Device/AlarmTriggerType.cs b/BPASmartClient.Device/AlarmTriggerType.cs new file mode 100644 index 00000000..29d9d55b --- /dev/null +++ b/BPASmartClient.Device/AlarmTriggerType.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BPASmartClient.Device +{ + public enum AlarmTriggerType + { + /// + /// 上升沿 + /// + Rising, + /// + /// 下降沿 + /// + Falling + } +} diff --git a/BPASmartClient.Device/BaseDevice.cs b/BPASmartClient.Device/BaseDevice.cs index 7f789960..23a7dd1e 100644 --- a/BPASmartClient.Device/BaseDevice.cs +++ b/BPASmartClient.Device/BaseDevice.cs @@ -137,6 +137,8 @@ namespace BPASmartClient.Device InitResetTask(); } + + private void GetGvlStatus() { this.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToList().ForEach(item => @@ -159,11 +161,74 @@ namespace BPASmartClient.Device }), $"{item.Name}:{DeviceId}"); })); } + else if (faces.Name == "IAlarm") + { + IAlarm alarm = item.GetValue(this) as IAlarm; + AlarmHelper alarmHelper = new AlarmHelper(); + alarmHelper.AddAction = new Action((s) => + { + var res = alarmHelper.Alarms.FirstOrDefault(p => p.Info == s); + if (res != null) + Error.TryAdd(DeviceId.ToString(), new + { + Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), + Type = res.Grade, + Text = res.Info + }); + }); + alarmHelper.RemoveAction = new Action((s) => + { + if (Error.ContainsKey(DeviceId.ToString())) + Error[DeviceId.ToString()] + }); + ThreadManage.GetInstance().StartLong(new Action(() => + { + AlarmMonitoring(alarm, alarmHelper); + Thread.Sleep(500); + }), $"报警检测监控:{DeviceId}"); + } } } }); } + /// + /// 报警监控 + /// + /// + /// + private void AlarmMonitoring(IAlarm alarm, AlarmHelper alarmHelper) + { + //ThreadManage.GetInstance().StartLong(new Action(() => + //{ + if (alarm != null) + { + foreach (var item in alarm.GetType().GetProperties()) + { + var res = item.GetValue(alarm); + if (res != null) + { + if (res is bool blen) + { + if (item.CustomAttributes.Count() > 0) + { + if (item.CustomAttributes.ElementAt(0)?.ConstructorArguments.Count() > 0) + { + var info = item.CustomAttributes.ElementAt(0)?.ConstructorArguments.ElementAt(0).Value; + if (info != null) + { + alarmHelper.EdgeAlarm(blen, info.ToString()); + } + } + } + } + } + } + } + // Thread.Sleep(500); + //}), $"报警检测监控:{DeviceId}"); + } + private void InitResetTask() { #region 复位程序