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
{
/// <summary>
/// 报警管理辅助类
/// </summary>
public class AlarmHelper
{
#region 报警参数
#region 判断报警代码是否存在
/// <summary>
/// 判断报警代码是否存在
/// </summary>
/// <param name="alarmPLC">要判断的报警PLC</param>
/// <param name="alarmcode">要判断的报警代码</param>
/// <returns>如果存在返回true, 否则返回false</returns>
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 判断报警名称是否存在
/// <summary>
/// 判断报警名称是否存在
/// </summary>
/// <param name="alarmPLC">要判断的报警PLC</param>
/// <param name="alarmName">要判断的报警名称</param>
/// <returns>如果存在返回true, 否则返回false</returns>
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 判断报警数据块是否存在
/// <summary>
/// 判断报警数据块是否存在
/// </summary>
/// <param name="alarmblock">要判断的报警数据块</param>
/// <returns>如果存在返回true, 否则返回false</returns>
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 判断对应的报警地址和位是否存在
/// <summary>
/// 判断对应的报警地址和位是否存在
/// </summary>
/// <param name="alarmAddress">报警地址</param>
/// <param name="alarmBit">报警位</param>
/// <returns>存在返回true, 否则返回false</returns>
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 判断对应的报警参数是否存在
/// <summary>
/// 判断对应的报警参数是否存在
/// </summary>
/// <param name="alarmPLC">报警PLC</param>
/// <param name="alarmBlock">报警地址块</param>
/// <param name="alarmAddress">报警地址字</param>
/// <param name="alarmBit">报警地址位</param>
/// <returns>存在返回true, 不存在返回false</returns>
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 根据报警参数数据块、报警参数地址字和报警参数数据位查询报警参数
/// <summary>
/// 根据报警参数数据块、报警参数地址字和报警参数数据位查询报警参数
/// </summary>
/// <param name="alarmBlock">报警参数PLC</param>
/// <param name="alarmBlock">报警参数数据块</param>
/// <param name="alarmAddress">报警参数地址字</param>
/// <param name="alarmBit">报警参数位</param>
/// <returns>返回对应的报警参数数据行</returns>
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 ;
}
/// <summary>
/// 根据报警参数数据块、报警参数地址字和报警参数数据位查询报警参数
/// </summary>
/// <param name="alarmPLC">报警参数PLC</param>
/// <param name="alarmBlock">报警参数数据块</param>
/// <param name="alarmAddress">报警参数地址字</param>
/// <param name="alarmBit">报警参数位</param>
/// <returns>返回对应的报警参数实体</returns>
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 查询所有报警参数
/// <summary>
/// 查询所有报警参数
/// </summary>
/// <returns>返回报警参数数据表</returns>
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 ;
}
/// <summary>
/// 查询所有报警参数
/// </summary>
/// <returns>返回报警参数实体类列表</returns>
public static List < Entity . PmtAlarmInfo > GetPmtAlarmInfoList ( )
{
List < Entity . PmtAlarmInfo > lst = new List < Entity . PmtAlarmInfo > ( ) ;
DataTable dt = GetPmtAlarmInfoTable ( ) ;
if ( dt ! = null )
{
foreach ( DataRow dr in dt . Rows )
{
Entity . PmtAlarmInfo entity = ConvertDataRowToPmtAlarmInfo ( dr ) ;
lst . Add ( entity ) ;
}
}
return lst ;
}
# endregion
#region 分类查询
/// <summary>
/// 分类查询
/// </summary>
/// <returns>返回分类查询数据表</returns>
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_Word,Alarm_Bit, 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,Alarm_Word,Alarm_Bit" ;
DataTable dt = dbHelper . GetDataTableBySql ( sqlstr ) ;
return dt ;
}
/// <summary>
/// 分类查询
/// </summary>
/// <returns>返回分类查询实体列表</returns>
public static List < Entity . AlarmInfo > GetBlockStartLengthList ( )
{
List < Entity . AlarmInfo > list = new List < AlarmInfo > ( ) ;
DataTable dt = GetBlockStartLengthTable ( ) ;
if ( dt ! = null )
{
foreach ( DataRow dr in dt . Rows )
{
Entity . AlarmInfo entity = ConvertDataRowToAlarmInfo ( dr ) ;
list . Add ( entity ) ;
}
}
return list ;
}
# endregion
#region 把数据行转换为报警参数实体对象
/// <summary>
/// 把数据行转换为报警参数实体对象
/// </summary>
/// <param name="dr">报警参数数据行</param>
/// <returns>返回对应的报警参数实体对象</returns>
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 ;
}
/// <summary>
/// 把数据行转换为报警参数扩展实体对象
/// </summary>
/// <param name="dr">数据行</param>
/// <returns>返回对应的报警参数实体对象</returns>
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 ) ;
entity . AlarmWord = Mesnac . Basic . DataProcessor . RowValue ( dr , "Alarm_Word" , String . Empty ) ;
entity . AlarmBit = Mesnac . Basic . DataProcessor . RowValue ( dr , "Alarm_Bit" , String . Empty ) ;
return entity ;
}
return null ;
}
# endregion
# endregion
#region 报警记录
#region 添加报警记录
/// <summary>
/// 添加报警记录
/// </summary>
/// <param name="alarmData">报警记录实体数据</param>
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 更新报警记录
/// <summary>
/// 更新报警记录
/// </summary>
/// <param name="alarmID">报警记录ID</param>
/// <param name="clearTime">报警清除时间</param>
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
}
}