|
|
using System.Collections.Generic;
|
|
|
using System.ComponentModel;
|
|
|
using System.Drawing;
|
|
|
using Mesnac.Controls.Base;
|
|
|
using System;
|
|
|
using Mesnac.Controls.Default;
|
|
|
//using Mesnac.Compressor.Data;
|
|
|
using Mesnac.Compressor.Entity;
|
|
|
//using Mesnac.Compressor.Unity;
|
|
|
|
|
|
namespace Mesnac.Controls.Compressor
|
|
|
{
|
|
|
[ToolboxBitmap(typeof(System.Windows.Forms.Button))]
|
|
|
public partial class AlarmButton : Button, IPurviewControl
|
|
|
{
|
|
|
#region 变量
|
|
|
private List<DesignAction> _clickActionList = new List<DesignAction>();
|
|
|
private List<DesignAction> _keyDownActionList = new List<DesignAction>();
|
|
|
private List<DesignAction> _keyUpActionList = new List<DesignAction>();
|
|
|
private bool _mcVisible = true; //保存可见性
|
|
|
private bool _mcEnabled = true; //保存可用性
|
|
|
private bool _isValid = true; //保存有效性
|
|
|
private object[] _mcvalue;
|
|
|
private string _nextpage;
|
|
|
private int _nextpagenum;
|
|
|
//一个单元有几个字
|
|
|
const int UnitLengh = 4;
|
|
|
private bool newAlarm;
|
|
|
|
|
|
List<int> addBit = new List<int>();
|
|
|
List<int> reduceBit = new List<int>();
|
|
|
|
|
|
public List<AlarmInfo> AlarmList = AlarmControl.Instance.AlarmLIst;
|
|
|
|
|
|
public List<AlarmInfo> InsertAlarm = new List<AlarmInfo>();
|
|
|
|
|
|
Dictionary<int, string> AlarmString1 = new Dictionary<int, string>();
|
|
|
Dictionary<int, string> AlarmString2 = new Dictionary<int, string>();
|
|
|
Dictionary<int, string> AlarmString3 = new Dictionary<int, string>();
|
|
|
Dictionary<int, string> AlarmString4 = new Dictionary<int, string>();
|
|
|
Dictionary<int, string> AlarmString5 = new Dictionary<int, string>();
|
|
|
Dictionary<int, string> AlarmString6 = new Dictionary<int, string>();
|
|
|
|
|
|
|
|
|
public AlarmButton()
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
}
|
|
|
|
|
|
public AlarmButton(IContainer container)
|
|
|
{
|
|
|
container.Add(this);
|
|
|
|
|
|
InitializeComponent();
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region 事件
|
|
|
public List<DesignAction> ClickActionList
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
return this._clickActionList;
|
|
|
}
|
|
|
set
|
|
|
{
|
|
|
this._clickActionList = value;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public List<DesignAction> KeyDownActionList
|
|
|
{
|
|
|
get { return _keyDownActionList; }
|
|
|
set { _keyDownActionList = value; }
|
|
|
}
|
|
|
|
|
|
public List<DesignAction> KeyUpActionList
|
|
|
{
|
|
|
get { return _keyUpActionList; }
|
|
|
set { _keyUpActionList = value; }
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 业务
|
|
|
//业务
|
|
|
public string MCKey
|
|
|
{
|
|
|
get;
|
|
|
set;
|
|
|
}
|
|
|
|
|
|
public object MCValue
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
return null;
|
|
|
}
|
|
|
set
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
newAlarm = false;
|
|
|
object[] objectarray;
|
|
|
if (!TryObject(value, out objectarray))
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
if (AlarmLists(objectarray))
|
|
|
{
|
|
|
this.BackColor = this.NewFillColor;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
this.BackColor = this.OldFillColor;
|
|
|
};
|
|
|
AlarmControl.Instance.UpdateFinish = true;
|
|
|
_mcvalue = objectarray;
|
|
|
if (newAlarm)
|
|
|
{
|
|
|
//Mesnac.ActionService.Service.Instance.Run((Mesnac.Controls.Base.IBaseControl)this, actions);
|
|
|
Mesnac.Gui.Common.MCEventHandler openpage = new Gui.Common.MCEventHandler();
|
|
|
openpage.ExecuteAction(this as object, null, "Click");
|
|
|
}
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService.Debug(e.ToString());
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private bool TryObject(object value, out object[] arry)
|
|
|
{
|
|
|
arry=null;
|
|
|
try
|
|
|
{
|
|
|
arry = (object[])value;
|
|
|
return true;
|
|
|
}
|
|
|
catch
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
public IBaseControl MCRoot
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
return null;
|
|
|
}
|
|
|
set
|
|
|
{
|
|
|
;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region 数据库连接
|
|
|
[TypeConverter(typeof(DataSourceConverter))]
|
|
|
|
|
|
[Description("数据连接"), Category("Data")]
|
|
|
public string MCDataSourceID
|
|
|
{
|
|
|
get;
|
|
|
set;
|
|
|
}
|
|
|
|
|
|
public MCDataSource MCDataSource
|
|
|
{
|
|
|
get;
|
|
|
set;
|
|
|
}
|
|
|
|
|
|
[Description("是否为数据库控件"), Category("Data")]
|
|
|
public bool IsDbControl
|
|
|
{
|
|
|
get;
|
|
|
set;
|
|
|
}
|
|
|
[Description("初始化SQL"), Category("Data")]
|
|
|
public string InitDataSource
|
|
|
{
|
|
|
get;
|
|
|
set;
|
|
|
}
|
|
|
[Description("执行SQL"), Category("Data")]
|
|
|
public string ActionDataSource
|
|
|
{
|
|
|
get;
|
|
|
set;
|
|
|
}
|
|
|
[Description("绑定数据源"), Category("Data")]
|
|
|
public object BindDataSource
|
|
|
{
|
|
|
get;
|
|
|
set;
|
|
|
}
|
|
|
[Description("操作类型"), Category("Data")]
|
|
|
public DbOptionTypes DbOptionType
|
|
|
{
|
|
|
get;
|
|
|
set;
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region 常规属性
|
|
|
|
|
|
[Description("是否可见"), Category("Behavior")]
|
|
|
public bool MCVisible
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
return this._mcVisible;
|
|
|
}
|
|
|
set
|
|
|
{
|
|
|
this._mcVisible = value == null ? true : value;
|
|
|
if (this.Site.DesignMode)
|
|
|
{
|
|
|
this.Visible = true;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
this.Visible = this._mcVisible;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
[Description("是否可用"), Category("Behavior")]
|
|
|
public bool MCEnabled
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
return this._mcEnabled;
|
|
|
}
|
|
|
set
|
|
|
{
|
|
|
this._mcEnabled = value == null ? true : value;
|
|
|
if (this.Site.DesignMode)
|
|
|
{
|
|
|
this.Enabled = true;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
this.Enabled = this._mcEnabled;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
[Description("是否控制权限"), Category("Behavior")]
|
|
|
public bool MCPurview
|
|
|
{
|
|
|
get;
|
|
|
set;
|
|
|
}
|
|
|
|
|
|
public bool IsValid
|
|
|
{
|
|
|
get { return _isValid; }
|
|
|
set { _isValid = value; }
|
|
|
}
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
private bool AlarmLists(object[] values)
|
|
|
{
|
|
|
|
|
|
//测试用,先把代码屏蔽掉
|
|
|
if (values == null)
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
InsertAlarm.Clear();
|
|
|
bool alarmflag = false;
|
|
|
//初始化
|
|
|
|
|
|
if (_mcvalue == null)
|
|
|
{
|
|
|
_mcvalue = new object[values.Length];
|
|
|
for (int i = 0; i < _mcvalue.Length; i++)
|
|
|
{
|
|
|
_mcvalue[i] = 0;
|
|
|
}
|
|
|
}
|
|
|
//i代表字,j代表位
|
|
|
for (int i = 0; i < values.Length; i++)
|
|
|
{
|
|
|
int newValue = Convert.ToInt32(values[i]);
|
|
|
int oldValue = Convert.ToInt32(_mcvalue[i]);
|
|
|
if (newValue != 0)
|
|
|
{
|
|
|
alarmflag = true;
|
|
|
}
|
|
|
if (newValue != oldValue)
|
|
|
{
|
|
|
AlarmControl.Instance.AlarmChange = true;
|
|
|
//判断哪一位不同
|
|
|
ChangebitIndex(oldValue, newValue, i);
|
|
|
//刷新报警列表
|
|
|
UpdateAlarmList(i);
|
|
|
|
|
|
if (InsertAlarm.Count > 0)
|
|
|
{
|
|
|
newAlarm = true;
|
|
|
//保存数据库
|
|
|
AlarmSave();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return alarmflag;
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取变化的位,添加位放入addBit,减少位放在reducebit
|
|
|
/// </summary>
|
|
|
/// <param name="oldValue"></param>
|
|
|
/// <param name="newValue"></param>
|
|
|
private void ChangebitIndex(int oldValue,int newValue,int wordindex)
|
|
|
{
|
|
|
int uniteid = UnitID(wordindex);
|
|
|
int StartIndex = wordStartIndex(wordindex);
|
|
|
string oldbit =intTo16bit(oldValue);
|
|
|
string newbit =intTo16bit(newValue);
|
|
|
|
|
|
byte[] newbyte = BitConverter.GetBytes(newValue);
|
|
|
for (int i = 0; i < 16; i++)
|
|
|
{
|
|
|
int obit = Convert.ToInt16(oldbit.Substring(15-i, 1));
|
|
|
int nbit = Convert.ToInt16(newbit.Substring(15-i, 1));
|
|
|
if (nbit != obit)
|
|
|
{
|
|
|
if (nbit == 0)
|
|
|
{
|
|
|
reduceBit.Add(StartIndex+i);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
addBit.Add(StartIndex+i);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//这里的序号不对,第一单元对应的第二个字的序号没改过来
|
|
|
private void UpdateAlarmList(int wordid)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
//单元号
|
|
|
int unitid = UnitID(wordid);
|
|
|
//把多余的去掉
|
|
|
foreach (int re in reduceBit)
|
|
|
{
|
|
|
AlarmInfo ai=AlarmList.Find(delegate(AlarmInfo deleAi)
|
|
|
{
|
|
|
return (deleAi.AlarmUnit == unitid && deleAi.AlarmIndex == re);
|
|
|
});
|
|
|
if (ai != null)
|
|
|
{
|
|
|
AlarmList.Remove(ai);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Dictionary<int, string> dic = GetList(unitid);
|
|
|
//把没有的补上
|
|
|
foreach(int ad in addBit)
|
|
|
{
|
|
|
AlarmInfo ai = new AlarmInfo();
|
|
|
ai.AlarmUnit = unitid;
|
|
|
ai.AlarmTime = DateTime.Now;
|
|
|
ai.AlarmIndex = ad;
|
|
|
ai.AlarmString = dic[ad];
|
|
|
AlarmList.Add(ai);
|
|
|
InsertAlarm.Add(ai);
|
|
|
}
|
|
|
}
|
|
|
catch
|
|
|
{
|
|
|
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
|
reduceBit.Clear();
|
|
|
addBit.Clear();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private Dictionary<int, string> GetList(int unitID)
|
|
|
{
|
|
|
switch (unitID)
|
|
|
{
|
|
|
case 1:
|
|
|
return AlarmControl.Instance.AlarmString1;
|
|
|
case 2:
|
|
|
return AlarmControl.Instance.AlarmString2;
|
|
|
case 3:
|
|
|
return AlarmControl.Instance.AlarmString3;
|
|
|
case 4:
|
|
|
return AlarmControl.Instance.AlarmString4;
|
|
|
case 5:
|
|
|
return AlarmControl.Instance.AlarmString5;
|
|
|
case 6:
|
|
|
return AlarmControl.Instance.AlarmString6;
|
|
|
default:
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 通过字获取UinitID
|
|
|
/// </summary>
|
|
|
/// <param name="wordIndex"></param>
|
|
|
/// <returns></returns>
|
|
|
private int UnitID(int wordIndex)
|
|
|
{
|
|
|
return wordIndex / UnitLengh + 1;
|
|
|
}
|
|
|
|
|
|
private int wordStartIndex(int wordIndex)
|
|
|
{
|
|
|
int dx = wordIndex % UnitLengh;
|
|
|
return dx * 16;
|
|
|
}
|
|
|
|
|
|
//保存信息到数据库
|
|
|
private void AlarmSave()
|
|
|
{
|
|
|
|
|
|
//try
|
|
|
//{
|
|
|
|
|
|
// DbHandler dbh = new DbHandler();
|
|
|
// foreach (AlarmInfo ai in InsertAlarm)
|
|
|
// {
|
|
|
// dbh.InsertAlarmInfo(Common.Instance.WorkName, ai);
|
|
|
// }
|
|
|
//}
|
|
|
//catch (Exception e)
|
|
|
//{
|
|
|
// ICSharpCode.Core.LoggingService.Debug("报警插入异常" + e.ToString());
|
|
|
//}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private string intTo16bit(int data)
|
|
|
{
|
|
|
string bitstr = Convert.ToString(data,2);
|
|
|
string bit16str =bitstr.PadLeft(16,'0');
|
|
|
// string bit16str = String.Format("%16s", bitstr);
|
|
|
return bit16str;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
} |