using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace MyProject
{
public class Alarm
{
public static DataTable CurrentAlarm { get; set; } = new DataTable();
public static DataTable HistoryAlarm { get; set; } = new DataTable();
static Alarm()
{
ColumnsAdd(CurrentAlarm);
ColumnsAdd(HistoryAlarm);
}
private static void ColumnsAdd(DataTable dataTable)
{
dataTable.Columns.Add("列表编码");
dataTable.Columns.Add("报警时间");
dataTable.Columns.Add("报警码");
dataTable.Columns.Add("报警描述");
}
public void AlarmAdd(int code, string message)
{
lock (this)
{
bool contain = false;
for (int i = 0; i < CurrentAlarm.Rows.Count; i++)
{
if (CurrentAlarm.Rows[i][2].ToString() == code.ToString())
{
contain = true;
}
}
if (!contain)
{
CurrentAlarm.Rows.Add(CurrentAlarm.Rows.Count, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:m"), code, message);
HistoryAlarmAdd(code, message);
string newMessage = "报警码:" + code.ToString() + "。报警描述:" + message + "。";
Log.LogMessage(@"..\..\LogAlarm", newMessage);
}
}
}
public void AlarmDelete(int code)
{
lock (this)
{
int row = CurrentAlarm.Rows.Count;
for (int i = 0; i < CurrentAlarm.Rows.Count; i++)
{
if (CurrentAlarm.Rows[i][2].ToString() == code.ToString())
{
row = i;
}
}
if (row < CurrentAlarm.Rows.Count)
{
CurrentAlarm.Rows[row].Delete();
for (int i = 0; i < CurrentAlarm.Rows.Count; i++)
{
CurrentAlarm.Rows[i][0] = i;
}
}
}
}
private void HistoryAlarmAdd(int code, string message)
{
HistoryAlarm.Rows.Add();
if (HistoryAlarm.Rows.Count > 1)
{
for (int i = HistoryAlarm.Rows.Count - 1; i > 0; i--)
{
HistoryAlarm.Rows[i][0] = i;
HistoryAlarm.Rows[i][1] = HistoryAlarm.Rows[i - 1][1];
HistoryAlarm.Rows[i][2] = HistoryAlarm.Rows[i - 1][2];
HistoryAlarm.Rows[i][3] = HistoryAlarm.Rows[i - 1][3];
}
}
HistoryAlarm.Rows[0][0] = 0;
HistoryAlarm.Rows[0][1] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:m");
HistoryAlarm.Rows[0][2] = code;
HistoryAlarm.Rows[0][3] = message;
}
}
}
08-12
3338
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-14
737
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-22