Browse Source

报警信息添加

样式分支
pry 2 years ago
parent
commit
c65e0a62ae
5 changed files with 254 additions and 0 deletions
  1. +56
    -0
      BPASmartClient.Device/Alarm.cs
  2. +99
    -0
      BPASmartClient.Device/AlarmHelper.cs
  3. +14
    -0
      BPASmartClient.Device/AlarmLevel.cs
  4. +20
    -0
      BPASmartClient.Device/AlarmTriggerType.cs
  5. +65
    -0
      BPASmartClient.Device/BaseDevice.cs

+ 56
- 0
BPASmartClient.Device/Alarm.cs View File

@@ -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
{
/// <summary>
/// ID
/// </summary>
[Key]
public int Id { get { return _mId; } set { _mId = value; } }
private int _mId;

/// <summary>
/// 编号ID
/// </summary>
public int NumId { get { return _mNumId; } set { _mNumId = value; } }
private int _mNumId;


/// <summary>
/// 日期
/// </summary>
public string Date { get { return _mDate; } set { _mDate = value; } }
private string _mDate;

/// <summary>
/// 时间
/// </summary>
public string Time { get { return _mTime; } set { _mTime = value; } }
private string _mTime;

/// <summary>
/// 报警信息
/// </summary>
public string Info { get { return _mInfo; } set { _mInfo = value; } }
private string _mInfo;

/// <summary>
/// 报警值
/// </summary>
public string Value { get { return _mValue; } set { _mValue = value; } }
private string _mValue;

/// <summary>
/// 报警等级
/// </summary>
public string Grade { get { return _mGrade; } set { _mGrade = value; } }
private string _mGrade;
}
}

+ 99
- 0
BPASmartClient.Device/AlarmHelper.cs View File

@@ -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<Alarm> Alarms { get; set; } = new List<Alarm>();
public List<Alarm> HistoryAlarms { get; set; } = new List<Alarm>();
ConcurrentDictionary<string, bool> flagbit = new ConcurrentDictionary<string, bool>();
public Action<string> AddAction { get; set; }
public Action<string> RemoveAction { get; set; }
public Action ChangeAction { get; set; }

/// <summary>
/// 沿报警检测
/// </summary>
/// <param name="Trigger">触发变量</param>
/// <param name="text">报警信息</param>
/// <param name="edgeType">触发类型,上升沿 或 下降沿</param>
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;
}

/// <summary>
/// 添加报警信息
/// </summary>
/// <param name="AlarmInfo">报警信息</param>
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<Alarm>.GetInstance.Base.Add(tempAlarm);

if (Alarms.FirstOrDefault(p => p.Info == AlarmInfo) == null)
{
Alarms.Add(tempAlarm);
if (AddAction != null) AddAction(AlarmInfo);//添加报警通知
if (ChangeAction != null) ChangeAction();//更改报警通知
}
}

/// <summary>
/// 移除报警信息
/// </summary>
/// <param name="AlarmInfo">报警信息</param>
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();
}
}

}
}

+ 14
- 0
BPASmartClient.Device/AlarmLevel.cs View File

@@ -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
{
一般报警,
严重报警
}
}

+ 20
- 0
BPASmartClient.Device/AlarmTriggerType.cs View File

@@ -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
{
/// <summary>
/// 上升沿
/// </summary>
Rising,
/// <summary>
/// 下降沿
/// </summary>
Falling
}
}

+ 65
- 0
BPASmartClient.Device/BaseDevice.cs View File

@@ -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<string>((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<string>((s) =>
{
if (Error.ContainsKey(DeviceId.ToString()))
Error[DeviceId.ToString()]
});
ThreadManage.GetInstance().StartLong(new Action(() =>
{
AlarmMonitoring(alarm, alarmHelper);
Thread.Sleep(500);
}), $"报警检测监控:{DeviceId}");
}
}
}
});
}

/// <summary>
/// 报警监控
/// </summary>
/// <param name="alarm"></param>
/// <param name="alarmHelper"></param>
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 复位程序


Loading…
Cancel
Save