using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
namespace Mesnac.Controls.Feeding
{
[ToolboxBitmap(typeof(System.Windows.Forms.Label))]
public partial class AlarmLabel : Label
{
#region 字段定义
private string _dataName;
private DataTable _data;
private DataTable _cloneData;
private int _alarmWord1; //报警地址字1
private int _alarmWord2; //报警地址字2
private string _alarmName1; //报警变量1
private string _alarmName2; //报警变量2
private int _alarmValue1; //报警值1
private int _alarmValue2; //报警值2
private bool _isMuilt; //是否换行显示
private bool _isScroll = false; //是否滚动显示
private int _scrollStep = 20; //滚动步长
public string Msg1 = string.Empty;//第一块报警信息
public string Msg2 = string.Empty;//第二块报警信息
string split = ";"; //报警信息分割符
string enter = "\r\n"; //报警信息换行符
private string _controlAlarmValue; //控件报警信息
private string _controlAlarmName; //控件报警动画字段
#endregion
#region 构造方法
public AlarmLabel()
{
InitializeComponent();
this.AutoSize = true;
}
public AlarmLabel(IContainer container)
{
container.Add(this);
InitializeComponent();
}
#endregion
#region 属性
///
/// 报警值1
///
public int AlarmValue1
{
get { return _alarmValue1; }
set
{
if (this._alarmValue1 != value)
{
_alarmValue1 = value;
Msg1 = string.Empty;
int[] values = ParseBinaryValue(_alarmValue1, 16);
Msg1 = GetAlarmInfo1(values, 16, _alarmWord1);
//this.Text = Msg1 + Msg2;
string temp = String.Empty;
if (_isMuilt)
{
temp = String.IsNullOrEmpty(this._controlAlarmValue) ? String.Empty : this._controlAlarmValue.Replace(",", enter);
}
else
{
temp = String.IsNullOrEmpty(this._controlAlarmValue) ? String.Empty : this._controlAlarmValue.Replace(",", split);
}
this.Text = Msg1 + Msg2 + temp;
}
if (this._isScroll)
{
this.ProcessScroll();
}
}
}
///
/// 报警值2
///
public int AlarmValue2
{
get { return _alarmValue2; }
set
{
if (this._alarmValue2 != value)
{
_alarmValue2 = value;
Msg2 = string.Empty;
int[] values = ParseBinaryValue(_alarmValue2, 16);
Msg2 = GetAlarmInfo2(values, 16, _alarmWord2);
//this.Text = Msg1 + Msg2;
string temp = String.Empty;
if (_isMuilt)
{
temp = String.IsNullOrEmpty(this._controlAlarmValue) ? String.Empty : this._controlAlarmValue.Replace(",", enter);
}
else
{
temp = String.IsNullOrEmpty(this._controlAlarmValue) ? String.Empty : this._controlAlarmValue.Replace(",", split);
}
this.Text = Msg1 + Msg2 + temp;
}
if (this._isScroll)
{
this.ProcessScroll();
}
}
}
///
/// 控件报警信息
///
public string ControlAlarmValue
{
get { return _controlAlarmValue; }
set
{
if (_controlAlarmValue != value)
{
this._controlAlarmValue = value;
Msg2 = string.Empty;
int[] values = ParseBinaryValue(_alarmValue2, 16);
Msg2 = GetAlarmInfo2(values, 16, _alarmWord2);
//this.Text = Msg1 + Msg2;
string temp = String.Empty;
if (_isMuilt)
{
temp = String.IsNullOrEmpty(this._controlAlarmValue) ? String.Empty : this._controlAlarmValue.Replace(",", enter);
}
else
{
temp = String.IsNullOrEmpty(this._controlAlarmValue) ? String.Empty : this._controlAlarmValue.Replace(",", split);
}
this.Text = Msg1 + Msg2 + temp;
}
if (this._isScroll)
{
this.ProcessScroll();
}
}
}
///
/// 报警地址字1
///
public int AlarmWord1
{
get { return _alarmWord1; }
set { _alarmWord1 = value; }
}
///
/// 报警地址字2
///
public int AlarmWord2
{
get { return _alarmWord2; }
set { _alarmWord2 = value; }
}
///
/// 是否滚动显示
///
public bool IsScroll
{
get { return _isScroll; }
set { _isScroll = value; }
}
///
/// 是否换行
///
public bool IsMuilt
{
get { return _isMuilt; }
set { _isMuilt = value; }
}
///
/// 滚动步长
///
public int ScrollStep
{
get { return _scrollStep; }
set { _scrollStep = value; }
}
///
/// 报警信息数据源
///
public DataTable Data
{
get { return _data; }
set
{
_data = value;
//if (_data != null)
//{
// _cloneData = _data;
//}
if (this._cloneData == null || this._cloneData.TableName != this._data.TableName)
{
InitData();
}
}
}
#endregion
#region 动画属性
///
/// 报警信息数据源
///
public string DataName
{
get { return _dataName; }
set { _dataName = value; }
}
///
/// 报警变量1
///
public string AlarmName1
{
get { return _alarmName1; }
set { _alarmName1 = value; }
}
///
/// 报警变量2
///
public string AlarmName2
{
get { return _alarmName2; }
set { _alarmName2 = value; }
}
///
/// 控件报警动画属性
///
public string ControlAlarmName
{
get { return _controlAlarmName; }
set { _controlAlarmName = value; }
}
#endregion
#region 辅助方法
///
/// 初始化数据
///
public void InitData()
{
if (this._cloneData != null)
{
this._cloneData.Dispose();
this._cloneData = null;
}
if (this._data != null)
{
this._cloneData = this._data.Copy();
}
}
///
/// 显示报警信息1
///
///
///
///
private string GetAlarmInfo1(int[] values, int WordLength, int alarmBlock)
{
string tempMsg1 = String.Empty;
if (values == null)
return String.Empty;
for (int i = 0; i < WordLength; i++)
{
if (values[i].ToString() == "1")
{
string info = this.AlarmStr(alarmBlock, i);
if (_isMuilt)
{
//Msg1 = Msg1 + info + enter;
tempMsg1 += info + enter;
}
else
{
//Msg1 = Msg1 + this.AlarmStr(alarmBlock, i) + split;
tempMsg1 += info + split;
}
}
}
return tempMsg1;
//if (_isMuilt)
// this.Text = this.Text + Msg1;
//else
//{
// if (Msg1 != string.Empty)
// this.Text = this.Text + Msg1.Substring(0, Msg1.Length - 1);
// else
// this.Text = this.Text + Msg1;
//}
}
///
/// 显示报警信息2
///
///
///
///
private string GetAlarmInfo2(int[] values, int WordLength, int alarmBlock)
{
string tempMsg2 = String.Empty;
if (values == null)
return String.Empty;
for (int i = 0; i < WordLength; i++)
{
if (values[i].ToString() == "1")
{
string info = this.AlarmStr(alarmBlock, i);
if (_isMuilt)
{
//Msg2 = Msg2 + info + enter;
tempMsg2 += info + enter;
}
else
{
//Msg2 = Msg2 + this.AlarmStr(alarmBlock, i) + split;
tempMsg2 += info + split;
}
}
}
return tempMsg2;
//if (_isMuilt)
// this.Text = this.Text + Msg2;
//else
//{
// if (Msg2 != string.Empty)
// this.Text = this.Text + Msg2.Substring(0, Msg2.Length - 1);
// else
// this.Text = this.Text + Msg2;
//}
}
///
/// 查找报警信息
///
///
///
///
private string AlarmStr(int alarm_address, int alarm_bit)
{
if (_cloneData == null || _cloneData.Rows.Count == 0)
return string.Empty;
if (_cloneData.Select(string.Format("Alarm_address='{0}' and Alarm_bit='{1}'", alarm_address, alarm_bit)).Length==0)
{
return string.Empty;
}
DataRow[] rows = _cloneData.Select(string.Format("Alarm_address='{0}' and Alarm_bit='{1}'", alarm_address, alarm_bit));
return rows[0]["Alarm_Name"].ToString(); ;
}
///
/// 解析二进制位
///
/// 要解析的变量
/// 解析的长度
/// 返回解析的二进制值数组
private int[] ParseBinaryValue(int x, int len)
{
int[] result = new int[len];
int b = 1;
for (int i = 0; i < len; i++)
{
result[i] = (x & b) == 0 ? 0 : 1;
b = b << 1;
}
return result;
}
///
/// 处理滚动的方法
///
private void ProcessScroll()
{
Control parent = this.Parent;
if (parent != null)
{
this.Left -= this._scrollStep;
if (this.Left < 10)
{
this.Left = parent.Width - 10;
}
}
}
#endregion
}
}