using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using ICSharpCode.Core; using Mesnac.Codd.Session; using Mesnac.Action.ChemicalWeighing.Entity; namespace Mesnac.Action.ChemicalWeighing.Alarm { /// /// 报警管理辅助类 /// public class AlarmHelper { #region 报警参数 #region 判断报警代码是否存在 /// /// 判断报警代码是否存在 /// /// 要判断的报警PLC /// 要判断的报警代码 /// 如果存在返回true,否则返回false public static bool IsExistsAlarmCode(string alarmcode) { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } string sqlstr = "select count(Alarm_ID) from Pmt_Alarm where Alarm_ID = @AlarmCode"; dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlstr; dbHelper.ClearParameter(); dbHelper.AddParameter("@AlarmCode", alarmcode); object result = dbHelper.ToScalar(); if (result != null && result != System.DBNull.Value) { int intResult = 0; if (int.TryParse(result.ToString(), out intResult)) { if (intResult > 0) { return true; } } } return false; } #endregion #region 判断报警名称是否存在 /// /// 判断报警名称是否存在 /// /// 要判断的报警PLC /// 要判断的报警名称 /// 如果存在返回true,否则返回false public static bool IsExistsAlarmName(string alarmPLC, string alarmName) { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } string sqlstr = "select count(Alarm_ID) from Pmt_Alarm where Alarm_PLC = @AlarmPLC and Alarm_Cn_Info = @AlarmName"; dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlstr; dbHelper.ClearParameter(); dbHelper.AddParameter("@AlarmPLC", alarmPLC); dbHelper.AddParameter("@AlarmName", alarmName); object result = dbHelper.ToScalar(); if (result != null && result != System.DBNull.Value) { int intResult = 0; if (int.TryParse(result.ToString(), out intResult)) { if (intResult > 0) { return true; } } } return false; } #endregion #region 判断报警数据块是否存在 /// /// 判断报警数据块是否存在 /// /// 要判断的报警数据块 /// 如果存在返回true,否则返回false public static bool IsExistsAlarmBlock(string alarmblock) { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } string sqlstr = "select count(Alarm_ID) from Pmt_Alarm where Alarm_Block = @AlarmBlock"; dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@AlarmBlock", alarmblock); object result = dbHelper.ToScalar(); if (result != null && result != System.DBNull.Value) { int intResult = 0; if (int.TryParse(result.ToString(), out intResult)) { if (intResult > 0) { return true; } } } return false; } #endregion #region 判断对应的报警地址和位是否存在 /// /// 判断对应的报警地址和位是否存在 /// /// 报警地址 /// 报警位 /// 存在返回true,否则返回false public static bool IsExistsAlarmAddressAndBit(string alarmAddress, string alarmBit) { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } string sqlstr = "select count(Alarm_ID) from Pmt_Alarm where Alarm_Word = @AlarmAddress and Alarm_Bit = @AlarmBit"; dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@AlarmAddress", alarmAddress); dbHelper.AddParameter("@AlarmBit", alarmBit); object result = dbHelper.ToScalar(); if (result != null && result != System.DBNull.Value) { int intResult = 0; if (int.TryParse(result.ToString(), out intResult)) { if (intResult > 0) { return true; } } } return false; } #endregion #region 判断对应的报警参数是否存在 /// /// 判断对应的报警参数是否存在 /// /// 报警PLC /// 报警地址块 /// 报警地址字 /// 报警地址位 /// 存在返回true,不存在返回false public static bool IsExists(string alarmPLC, string alarmBlock, int alarmAddress, int alarmBit) { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } string sqlstr = "select count(Alarm_ID) from Pmt_Alarm where Alarm_PLC = @AlarmPLC and Alarm_Block = @AlarmBlock and Alarm_Word = @AlarmAddress and Alarm_Bit = @AlarmBit"; dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlstr; dbHelper.ClearParameter(); dbHelper.AddParameter("@AlarmPLC", alarmPLC); dbHelper.AddParameter("@AlarmBlock", alarmBlock); dbHelper.AddParameter("@AlarmAddress", alarmAddress); dbHelper.AddParameter("@AlarmBit", alarmBit); object result = dbHelper.ToScalar(); if (result != null && result != System.DBNull.Value) { int intResult = 0; if (int.TryParse(result.ToString(), out intResult)) { if (intResult > 0) { return true; } } } return false; } #endregion #region 根据报警参数数据块、报警参数地址字和报警参数数据位查询报警参数 /// /// 根据报警参数数据块、报警参数地址字和报警参数数据位查询报警参数 /// /// 报警参数PLC /// 报警参数数据块 /// 报警参数地址字 /// 报警参数位 /// 返回对应的报警参数数据行 public static DataRow GetAlarmInfoDataRow(string alarmPLC, string alarmBlock, int alarmAddress, int alarmBit) { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } string sqlstr = "select * from Pmt_Alarm where Alarm_PLC = @AlarmPLC and Alarm_Word = @AlarmAddress and Alarm_Bit = @AlarmBit"; dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlstr; dbHelper.ClearParameter(); dbHelper.AddParameter("@AlarmPLC", alarmPLC); dbHelper.AddParameter("@AlarmBlock", alarmBlock); dbHelper.AddParameter("@AlarmAddress", alarmAddress); dbHelper.AddParameter("@AlarmBit", alarmBit); DataTable dt = dbHelper.ToDataTable(); if (dt != null && dt.Rows.Count > 0) { return dt.Rows[0]; } return null; } /// /// 根据报警参数数据块、报警参数地址字和报警参数数据位查询报警参数 /// /// 报警参数PLC /// 报警参数数据块 /// 报警参数地址字 /// 报警参数位 /// 返回对应的报警参数实体 public static Entity.PmtAlarmInfo GetAlarmInfoEntity(string alarmPLC, string alarmBlock, int alarmAddress, int alarmBit) { DataRow dr = GetAlarmInfoDataRow(alarmPLC, alarmBlock, alarmAddress, alarmBit); Entity.PmtAlarmInfo entity = ConvertDataRowToPmtAlarmInfo(dr); return entity; } #endregion #region 查询所有报警参数 /// /// 查询所有报警参数 /// /// 返回报警参数数据表 public static DataTable GetPmtAlarmInfoTable() { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } string sqlstr = "select * from Pmt_Alarm order by Alarm_PLC, Alarm_Block, Alarm_Word, Alarm_Bit"; DataTable dt = dbHelper.GetDataTableBySql(sqlstr); return dt; } /// /// 查询所有报警参数 /// /// 返回报警参数实体类列表 public static List GetPmtAlarmInfoList() { List lst = new List(); DataTable dt = GetPmtAlarmInfoTable(); if (dt != null) { foreach(DataRow dr in dt.Rows) { Entity.PmtAlarmInfo entity = ConvertDataRowToPmtAlarmInfo(dr); lst.Add(entity); } } return lst; } #endregion #region 分类查询 /// /// 分类查询 /// /// 返回分类查询数据表 public static DataTable GetBlockStartLengthTable() { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } string sqlstr = "select Alarm_PLC, Alarm_Block, MIN(Alarm_Word) as Start, (MAX(Alarm_Word) - MIN(Alarm_Word) + 1) as Length from Pmt_Alarm group by Alarm_PLC, Alarm_Block"; DataTable dt = dbHelper.GetDataTableBySql(sqlstr); return dt; } /// /// 分类查询 /// /// 返回分类查询实体列表 public static List GetBlockStartLengthList() { List list = new List(); DataTable dt = GetBlockStartLengthTable(); if (dt != null) { foreach(DataRow dr in dt.Rows) { Entity.AlarmInfo entity = ConvertDataRowToAlarmInfo(dr); list.Add(entity); } } return list; } #endregion #region 把数据行转换为报警参数实体对象 /// /// 把数据行转换为报警参数实体对象 /// /// 报警参数数据行 /// 返回对应的报警参数实体对象 public static Entity.PmtAlarmInfo ConvertDataRowToPmtAlarmInfo(DataRow dr) { if (dr != null) { Entity.PmtAlarmInfo entity = new Entity.PmtAlarmInfo(); entity.GUID = Mesnac.Basic.DataProcessor.RowValue(dr, "GUID", String.Empty); entity.Equip_Code = Mesnac.Basic.DataProcessor.RowValue(dr, "Equip_Code", String.Empty); entity.Alarm_ID = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_ID", 0); entity.AlarmPLC = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_PLC", String.Empty); entity.AlarmBlock = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_Block", String.Empty); entity.AlarmAddress = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_Word", 0); entity.AlarmBit = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_Bit", 0); entity.AlarmCode = Mesnac.Basic.DataProcessor.RowValue(dr, "AlarmCode", String.Empty); entity.AlarmName = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_Cn_Info", String.Empty); entity.AlarmPosition = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_Other_Info", String.Empty); entity.Remark = Mesnac.Basic.DataProcessor.RowValue(dr, "Remark", String.Empty); entity.RecordTime = Mesnac.Basic.DataProcessor.RowValue(dr, "RecordTime", DateTime.Now); entity.RecordUser = Mesnac.Basic.DataProcessor.RowValue(dr, "RecordUser", Mesnac.Basic.UserInfo.Instance.UserName); entity.DataSource = Mesnac.Basic.DataProcessor.RowValue(dr, "DataSource", "0"); entity.PLCType = Mesnac.Basic.DataProcessor.RowValue(dr, "Plc_Type", 4); entity.SaveFlag = (char)Mesnac.Basic.DataProcessor.RowValue(dr, "Save_Flag", 1); return entity; } return null; } /// /// 把数据行转换为报警参数扩展实体对象 /// /// 数据行 /// 返回对应的报警参数实体对象 public static Entity.AlarmInfo ConvertDataRowToAlarmInfo(DataRow dr) { if (dr != null) { Entity.AlarmInfo entity = new AlarmInfo(); entity.AlarmPLC = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_PLC", String.Empty); entity.AlarmBlock = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_Block", String.Empty); entity.Start = Mesnac.Basic.DataProcessor.RowValue(dr, "Start", 0); entity.Length = Mesnac.Basic.DataProcessor.RowValue(dr, "Length", 0); return entity; } return null; } #endregion #endregion #region 报警记录 #region 添加报警记录 /// /// 添加报警记录 /// /// 报警记录实体数据 public static void InsertAlarmLogData(Entity.LR_Alarmlog alarmData) { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } string sqlstr = @"insert into LR_Alarmlog(Equip_Code,Alarm_ID,Alarm_OccurTime,Alarm_ClearTime,Alarm_Status,Alarm_GUID) values(@Equip_Code,@Alarm_ID,@Alarm_OccurTime,@Alarm_ClearTime,@Alarm_Status,@Alarm_GUID)"; dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@Equip_Code", alarmData.Equip_Code); dbHelper.AddParameter("@Alarm_ID", alarmData.Alarm_ID); dbHelper.AddParameter("@Alarm_OccurTime", alarmData.Alarm_OccurTime); dbHelper.AddParameter("@Alarm_ClearTime", alarmData.Alarm_ClearTime); dbHelper.AddParameter("@Alarm_Status", alarmData.Alarm_Status); dbHelper.AddParameter("@Alarm_GUID", alarmData.Alarm_GUID); dbHelper.ExecuteNonQuery(); } #endregion #region 更新报警记录 /// /// 更新报警记录 /// /// 报警记录ID /// 报警清除时间 public static void UpdateAlarmLogData(int alarmID,string clearTime) { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } string sqlstr = @"update LR_Alarmlog set Alarm_Status = 0,Alarm_ClearTime = @Alarm_ClearTime where Alarm_ID = @Alarm_ID"; dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@Alarm_ID", alarmID); dbHelper.AddParameter("@Alarm_ClearTime", clearTime); dbHelper.ExecuteNonQuery(); } #endregion #endregion } }