/// <summary> /// 检查数据库是否有同样的故障信息 /// </summary> /// <param name="info"></param> private void CheckAlarmInfoInDB(AlarmInfo info) { string query = "SELECT * FROM alarm_log"; DataSet ds = SqlAccess.ExecuteQuery(query); if (ds != null && ds.Tables.Count > 0) { DataTable table = ds.Tables[0]; if (table.Rows.Count > 0) { for (int i = 0; i < table.Rows.Count; i++) { DataRow dataRow = table.Rows[i]; string EndTime = dataRow[4].ToString(); string DeviceName = dataRow[7].ToString(); string Column = dataRow[10].ToString(); string Brand = dataRow[11].ToString(); if (string.IsNullOrEmpty(EndTime) && DeviceName.Equals(info.DeviceName) && Column.Equals(info.Column) && Brand.Equals(info.Brand)) { return; } } // 有数据且没有同一个故障报警 InsertAlarmIntoDB(info); return; } else { //数据库没有数据 InsertAlarmIntoDB(info); } } }
/// <summary> /// 向数据库插入一条警报信息 /// </summary> private void InsertAlarmIntoDB(AlarmInfo info) { string[] values = { info.IsConfirm.ToString(), info.About, info.StartTime, info.Status, info.Value, info.DeviceName, info.Show.ToString(), info.TableName, info.Column, info.Brand }; SqlAccess InserAlarmInto = new SqlAccess(); InserAlarmInto.InsertInto("alarm_log", col, values); }
/// <summary> /// 检测异常故障 /// </summary> private void CheckAlarm() { foreach (KeyValuePair <string, List <string> > pair in allCheckColumns) { string tableName_realTime = pair.Key; string tableName_limit = pair.Key.Replace("realtime", "limit"); foreach (string column in pair.Value) { //查询id string idQuery = "SELECT max(_id) FROM " + tableName_realTime; DataSet dsID = SqlAccess.ExecuteQuery(idQuery); if (dsID.Tables.Count > 0) { DataTable table = dsID.Tables[0]; if (table.Rows.Count > 0) { string id = table.Rows[0][0].ToString(); if (!string.IsNullOrEmpty(id)) { //查询最新实时值 string real = "SELECT " + column + ",brand,name,realTime FROM " + tableName_realTime + " WHERE _id=" + id; DataSet dsReal = SqlAccess.ExecuteQuery(real); if (dsReal.Tables.Count > 0) { DataTable valueTable = dsReal.Tables[0]; if (valueTable.Rows.Count > 0) { string brand = valueTable.Rows[0][1].ToString(); string standardQuery = "SELECT " + column + " FROM " + tableName_limit + " WHERE options='standard_value' AND brand=" + brand; string rangeQuery = "SELECT " + column + " FROM " + tableName_limit + " WHERE options='error_range' AND brand=" + brand; string cn = "SELECT " + column + " FROM " + tableName_limit + " WHERE options='cn'"; DataSet dsStandard = SqlAccess.ExecuteQuery(standardQuery); DataSet dsRange = SqlAccess.ExecuteQuery(rangeQuery); DataSet dsCN = SqlAccess.ExecuteQuery(cn); if (dsStandard.Tables[0].Rows.Count > 0 && dsRange.Tables[0].Rows.Count > 0 && dsCN.Tables[0].Rows.Count > 0) { string value = dsReal.Tables[0].Rows[0][0].ToString(); string standard = dsStandard.Tables[0].Rows[0][0].ToString(); string range = dsRange.Tables[0].Rows[0][0].ToString(); string name = dsReal.Tables[0].Rows[0][2].ToString(); string time = dsReal.Tables[0].Rows[0][3].ToString(); string columnCN = dsCN.Tables[0].Rows[0][0].ToString(); if (string.IsNullOrEmpty(value)) { continue; } double realValue = double.Parse(value); double standardValue = double.Parse(standard); double rangeValue = double.Parse(range); if (Math.Abs((float)realValue - (float)standardValue) > rangeValue) { AlarmInfo info = new AlarmInfo(); info.IsConfirm = 0; info.Show = 0; info.About = name + "【" + columnCN + "】数据出现异常(标准值:" + standardValue + ";误差范围:" + rangeValue + ";当前值:" + realValue + ")"; info.DeviceName = name; info.Status = ""; info.Value = value; info.StartTime = time; info.TableName = tableName_realTime; info.Column = column; info.Brand = brand; CheckAlarmInfoInDB(info); } } } } } } } } } }