using Module.Plc.ModbusTcp.Tool; using MorClient; using RS.BLL; using RS.Common; using RS.Model; using Sunny.UI; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1.UserControls { public partial class Alarm : UserControl { DataTable dtResult = new DataTable(); DataGridViewHandle gridHdl = new DataGridViewHandle(); BOperateLogInfo bOperateLogInfo = new BOperateLogInfo(); List operateAlarmLogs = new List(); PlcModbusTool PlcModbusTool = new PlcModbusTool(); private delegate void PrintCSStateValueCallBack(); //委托事件 private PrintCSStateValueCallBack ShowCSStateValue; private bool IsPrintReadValue = true; /// /// 记录尚未恢复的报警数量 /// int alarmCount = 0; /// /// 鼠标点击 所在行数 /// static int rowLocate; public Alarm() { InitializeComponent(); //LoadDataNew(); GenerateDataGridViewColumns(); } #region 数据库中加载的数据 // 生成DataGridView列 private void GenerateDataGridViewColumns() { if (dgvCeshiInfo.DataSource != null) { if (dgvCeshiInfo.Columns.Count > 0) { dgvCeshiInfo.Columns[0].HeaderText = "报警编号"; dgvCeshiInfo.Columns[1].HeaderText = "报警名称"; dgvCeshiInfo.Columns[2].HeaderText = "报警内容"; dgvCeshiInfo.Columns[3].HeaderText = "报警时间"; dgvCeshiInfo.Columns[4].HeaderText = "恢复时间"; // dgvCeshiInfo.Columns[4].DefaultCellStyle.Format = "yyyy/MM/dd HH:mm:ss"; gridHdl._dbgrid = dgvCeshiInfo; gridHdl.GenerateBaseDataGridViewAlarm(); } } else { } foreach (DataGridViewColumn column in dgvCeshiInfo.Columns) { column.SortMode = DataGridViewColumnSortMode.NotSortable; } } #endregion 数据库中加载的报警数据 /// /// 加载和打开线程 /// /// /// public void Alarm_Load() { // 定义列的名称和类型 dtResult = new DataTable(); dtResult.Columns.Add("报警编号", typeof(string)); dtResult.Columns.Add("报警内容", typeof(string)); dtResult.Columns.Add("报警时间", typeof(string)); if (!PubStaticClass.bthreedAlarm) { PubStaticClass.bthreedAlarm = true; //委托展示参数实时值 ShowCSStateValue = new PrintCSStateValueCallBack(PrintParamRealValue); //开始显示实时值线程 System.Threading.Thread printRealValueThread = new System.Threading.Thread(StartPrintParamRealValue); printRealValueThread.IsBackground = true; printRealValueThread.Start(); } } private void dgvCeshiInfo_CellContentClick(object sender, DataGridViewCellEventArgs e) { rowLocate = e.RowIndex; } /// /// 开始显示实时值线程 /// private void StartPrintParamRealValue() { while (IsPrintReadValue) { if (this.IsHandleCreated) { this.BeginInvoke(ShowCSStateValue, null); } System.Threading.Thread.Sleep(2000); } } /// /// 展示参数实时值 /// private /*async*/ void PrintParamRealValue() { uiButton3.BackColor = PlcModbusTool.ReadOneBitStyle("4802", 0) ? Color.FromArgb(146, 208, 80) : Color.FromArgb(156, 156, 156); dtResult.Clear(); operateAlarmLogs = Common.operateAlarmLogs; for (int i = Common.operateAlarmLogs.Count - 1; i >= 0; i--) { var item = Common.operateAlarmLogs[i]; dtResult.Rows.Add(item.alarmLogNum, item.alarmContent, item.alarmStartTime); } #region 设置顺序 // 获取 DataTable 的 DefaultView DataView view = dtResult.DefaultView; // 使用 Sort 方法对 DataTable 进行排序 // 可以按照一个或多个列进行排序,并提供排序方向(升序或降序) view.Sort = "报警时间 DESC"; // 先按 Age 升序排序,然后按 Name 降序排序 dtResult = view.ToTable(); #endregion dgvCeshiInfo.DataSource = dtResult; gridHdl._dbgrid = dgvCeshiInfo; gridHdl.GenerateBaseDataGridView(); GC.Collect(); GC.WaitForPendingFinalizers(); foreach (DataGridViewColumn column in dgvCeshiInfo.Columns) { column.SortMode = DataGridViewColumnSortMode.NotSortable; } for (int i = 0; i < dgvCeshiInfo.Rows.Count; i++) { dgvCeshiInfo.Rows[i].ReadOnly = true; } } private void uiButton3_MouseDown(object sender, MouseEventArgs e) { PlcModbusTool.WriteOneBitStyle("4802", 0, true); } private void uiButton3_MouseUp(object sender, MouseEventArgs e) { PlcModbusTool.WriteOneBitStyle("4802", 0, false); } #region 注释 /// /// 复位按钮 /// /// /// private void uiButton3_Click(object sender, EventArgs e) { string armInfo = dgvCeshiInfo.Rows[rowLocate].Cells[2].Value.ToString(); if (PublicPara.armAndaddress.TryGetValue(armInfo, out var exam)) { PlcModbusTool.WriteOneBitStyle(exam.Item1, exam.Item2, false); #region 页面删除当前报警 if (rowLocate >= 0 && rowLocate < dgvCeshiInfo.RowCount) { //更新报警解除时间 DataGridViewRow dataGridViewRow = dgvCeshiInfo.Rows[rowLocate] as DataGridViewRow; string alarmName = dataGridViewRow.Cells[1].Value.ToString(); DateTime dateTime = DateTime.Now; string formattedDateTime = dateTime.ToString("yyyy-MM-dd HH:mm:ss"); bool isUpdateSuccess = bOperateLogInfo.UpdateAlarmLogInfo(alarmName, formattedDateTime); if (isUpdateSuccess) { dgvCeshiInfo.Rows.RemoveAt(rowLocate); } } else { MessageBox.Show("请选择要删除的行"); } #endregion 页面删除当前报警 } else { MessageBox.Show("当前报警信息未录入"); } } #endregion } }