/*-------------------------------------------------------------------------------------------
* 键盘测试窗体
* 创建:赵丽 2011-11-30
* 修改:杨斌 2012-03-01 控件布局,Esc退出
* ----------------------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;
using Microsoft.Office.Interop.PowerPoint;
using GeneralLib;
namespace SunVoteARSPPT
{
public partial class FrmKeypadTest : Form
{
public delegate void ShowSelfEvent();
public event ShowSelfEvent ShowSelfEventHander;
///
/// 坐席图对象
///
Units Map;
///
/// 是否正在测试
///
bool IsStart;
///
/// 键盘状态画刷:在线
///
private Brush BrushOnline;
///
/// 键盘状态画刷:不在线
///
private Brush BrushOffline;
///
/// 键盘状态画刷:弱电
///
private Brush BrushELV;
///
/// 在线的键盘集合,用于统计个数
///
private StringCollection KeyOnline = new StringCollection();
private SortedList KeyTestList = null;
private string[] PersonList = null;
//private string[] KeyIDList=null;
///
/// 弱电的键盘集合,用于统计个数
///
private StringCollection KeyELV = new StringCollection();
///
/// 是单基站模式显示单个
///
public bool ShowSingle = false;
private int KeyNum = 0;
///
/// 基站编号
///
public int BaseID = 1;
public FrmKeypadTest()
{
InitializeComponent();
btnKeypadTest.Tag = 0;
cobTestType.SelectedIndex = 0;
KeyTestList = new SortedList();
//GlobalInfo.hardwareManage.HardwareMonitorEvent += new HardwareMonitorEvent(HardwareMonitorEventHander);
GlobalInfo.hardwareManage.KeyTestStatusEvent += new KeyTestStatusEvent(hardwareManage_KeyTestStatusEvent);
GlobalInfo.hardwareManage.KeyMonitorStatusEvent += new KeyMonitorStatusEvent(hardwareManage_KeyMonitorStatusEvent);
GlobalInfo.hardwareManage.ReadOnlyEvent += new ReadOnlyEvent(hardwareManage_ReadOnlyEvent);
GlobalInfo.baseConnect.ResponseStateEvent += new ResponseStateEventHander(baseConnect_ResponseStateEvent);
Roster.LoadRoster();//杨斌 2015-01-14
InitMap();
}
void baseConnect_ResponseStateEvent(bool isResponse)
{
btnKeypadTest.Enabled = (!GlobalInfo.hardwareManage.ReadBaseInfo) && (!isResponse);
}
void hardwareManage_ReadOnlyEvent(bool ReadOnly)
{
//2012-05-02 断开基站,需要停止键盘测试
if (Convert.ToInt32(btnKeypadTest.Tag) == 1)
btnKeypadTest_Click(null, null);
btnKeypadTest.Enabled = (!ReadOnly) && (!GlobalInfo.baseConnect.IsResponse);
if (ReadOnly)
ClearMap();
}
private void btnKeypadTest_Click(object sender, EventArgs e)
{
KeyTestList.Clear();
if (Convert.ToInt32(btnKeypadTest.Tag) == 0)
{
ClearMap();
btnKeypadTest.Tag = 1;
GlobalInfo.hardwareManage.KeyNum = 0;
GlobalInfo.hardwareManage.CheckBase = (cobBaseList.SelectedIndex == 0) ? "0" : cobBaseList.Text;
if (cobTestType.SelectedIndex == 0)
{
GlobalInfo.hardwareManage.HardwareTestMode = 0;
Map.IsShowData = true;//杨斌 2013-08-02
}//开启硬件监控
else
{
GlobalInfo.hardwareManage.HardwareTestMode = 1;
Map.IsShowData = true;
}//开启自动表决
GlobalInfo.hardwareManage.HardwareTestEnable = true;
System.Threading.Thread.Sleep(100);
//清空键盘状态
for (int i = 0; i < GlobalInfo.hardwareManage.KeyStatus.Count; i++)
{
GlobalInfo.hardwareManage[i].InputStatus = -1;
}
cobBaseList.Enabled = false;
cobTestType.Enabled = false;
btnKeypadTest.Text = GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "btnKeypadTest_Stop", "停止测试");
}
else
{
//Map.IsShowData = false;
btnKeypadTest.Tag = 0;
//关闭硬件测试
GlobalInfo.hardwareManage.HardwareTestEnable = false;
System.Threading.Thread.Sleep(100);
cobTestType.Enabled = true;
cobBaseList.Enabled = true;
btnKeypadTest.Text = GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "btnKeypadTest_Start", "开始测试");
DrawMap();//杨斌 2013-06-18
tmrMap.Enabled = false;
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
///
/// 初始化坐席图
/// 创建:杨斌 2010-07-14
///
private void InitMap()
{
//初始化键盘状态画刷
BrushOnline = new SolidBrush(pnlOnline.BackColor);
BrushOffline = new SolidBrush(pnlOffline.BackColor);
BrushELV = new SolidBrush(pnlELV.BackColor);
List keypads = new List();
//通过读取基站键盘范围来初始化坐席图单元。
//int keyCount = GlobalInfo. - GlobalInfo.BaseInfo.MinKeypadID + 1;
Map = new Units(this.picMap.ClientSize.Width, this.picMap.ClientSize.Height);
int personNum = GlobalInfo.hardwareManage.PersonNum;
if (EnableList)
{
PersonList = LoadPersonList();
Array.Sort(PersonList);
personNum = PersonList.Length;
}//启用名单
//编号先排序
string[] KeyIDList = PublicFunction.KeyIDSort();
//for (int i = 0; i < personNum; i++)//键盘数量=6000时,非常耗时。杨斌 2012-10-12
//{
// Unit unit = new Unit();
// //键盘范围=参加投票的人数
// unit.ID = "";
// unit.Data = "";
// unit.Brush = BrushOffline;
// Map.Add(unit);
// if (EnableList)
// {
// Map[i].ID = PersonList[i].ToString();
// }//启用名单
// else
// {
// if (PersonNumEqualKeyNum)
// {
// //编号先排序
// //string[] KeyIDList = KeyIDSort();
// Map[i].ID = KeyIDList[i];
// //Map[i].ID = GlobalInfo.hardwareManage[i].KeyID.ToString();
// }
// }//未启用名单
//}
//将名单判断放到循环外面,提高速度。杨斌 2012-10-12
if (EnableList)//启用名单
{
for (int i = 0; i < personNum; i++)
{
Unit unit = new Unit();
//键盘范围=参加投票的人数
unit.ID = PersonList[i].ToString();
unit.Data = "";
//杨斌 2015-01-14
unit.Name = "";
if (unit.ID.Length > 0)
{
for (int n = 0; n < Roster.Rows.Count; n++)
{
if (Roster.Rows[n].Cells[0] == unit.ID)
{
unit.Name = Roster.Rows[n].Cells[1];
break;
}
}
}
unit.Brush = BrushOffline;
Map.Add(unit);
}
}
else//未启用名单
{
if (PersonNumEqualKeyNum)//设置人数与有效范围个数正好相同
{
for (int i = 0; i < personNum; i++)
{
Unit unit = new Unit();
//键盘范围=参加投票的人数
unit.ID = KeyIDList[i];
unit.Data = "";
unit.Brush = BrushOffline;
Map.Add(unit);
}
}
else
{
for (int i = 0; i < personNum; i++)
{
Unit unit = new Unit();
//键盘范围=参加投票的人数
unit.ID = "";
unit.Data = "";
unit.Brush = BrushOffline;
Map.Add(unit);
}
}
}
Map.Sort();//杨斌 2013-06-18
//FrmKeypadTest_Resize(null, null);
labCount.Text = GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "labCount", "键盘总数:") + Map.Count;
}
public RosterList Roster = new RosterList();//杨斌 2015-01-14
///
/// 加载人员名单
///
public string[] LoadPersonList()
{
string[] personList = null;
try
{
//获取所有名单的键盘ID
string sSql = "select a.* from ST_RosterValue a,ST_RosterColumn b where a.RC_ID=b.RC_ID and b.RC_Index=1";
DataSet ds = null;
ds = GlobalInfo.DBOperation.OpenDataSet(sSql);
personList = new string[ds.Tables[0].Rows.Count];
if (ds.Tables[0].Rows.Count == 0) { return personList; }
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string keypadID = ds.Tables[0].Rows[i]["RV_Text"].ToString();
personList[i] = keypadID;//杨斌 2015-01-14
}
return personList;
}
catch (Exception ex)
{
return personList;
}
}
///
/// 更新键盘状态:在线、不在线、弱电
///
private void ShowKeyState()
{
lblOnline.Text = GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "lblOnline", "在线:") + (KeyOnline.Count + KeyELV.Count).ToString();
lblOffline.Text = GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "lblOffline", "不在线:") + (Map.Count - KeyOnline.Count - KeyELV.Count).ToString();
lblELV.Text = GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "lblELV", "弱电:") + KeyELV.Count.ToString();
}
private void FrmKeypadTest_Load(object sender, EventArgs e)
{
GlobalInfo.SysLanguage.SetLanguage(this.Name, this);
cobTestType.SelectedIndex = 0;
if (cobBaseList.Items.Count > 0)
cobBaseList.SelectedIndex = 0;
btnKeypadTest.Enabled = (!GlobalInfo.hardwareManage.ReadBaseInfo) && (!GlobalInfo.baseConnect.IsResponse);
GlobalInfo.hardwareManage.HardwareTestStarMode = 1;
if (SystemConfig.MultiBase == 0)
{
lblBaseList.Visible = false;
cobBaseList.SelectedIndex = -1;
cobBaseList.Visible = false;
pnlCtrl.Visible = true;
}
else
{
lblBaseList.Visible = true;
cobBaseList.Visible = true;
cobBaseList.Items.Clear();
cobBaseList.Items.Add(GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "cobBaseList", "所有基站"));
for (int i = 0; i < GlobalInfo.baseConnect.BaseList.Count; i++)
{
if (GlobalInfo.baseConnect[i].BaseConnectStatus == BasePara.ConnectStatus.csSuccess)
{
cobBaseList.Items.Add(GlobalInfo.baseConnect[i].BaseID.ToString());
}
}
cobBaseList.SelectedIndex = 0;
}
FrmKeypadTest_Resize(null, null);
ShowKeyState();
//杨斌 2015-03-13
if (GlobalInfo.GetSdkType() == 1)
{
lblTestType.Visible = cobTestType.Visible = false;
}
//tmrMap.Elapsed += tmrMap_Elapsed;
tmrMap.Tick += tmrMap_Tick;
//陈总说的,打开窗体即启动扫描 杨斌 2012-03-12
if (!GlobalInfo.hardwareManage.ReadBaseInfo)
btnKeypadTest_Click(null, null);
//杨斌 2012-06-11
switch (GlobalInfo.OEMLogo)
{
case OEMLogos.oem3eAnalyzer:
lblTestType.Visible = false;
cobTestType.Visible = false;
break;
case OEMLogos.SunVote:
default:
break;
}
}
///
/// 改变坐席图单元状态
/// 创建:杨斌 2013-06-18
///
///
///
///
private void ChangeMap(string BaseID, string KeyID, string sData, Brush BrushVal, bool ShowData)
{
//判断扫描查询的基站
if ((GlobalInfo.baseConnect.MultiBase == 1) && (cobBaseList.SelectedIndex > 0) && (cobBaseList.Text != BaseID.ToString())) { return; }
//启用名单以名单为主
if (EnableList)
{
//if (!PersonList.Contains(KeyID))
// return;
if (!Map.Contains(KeyID.ToString()))
return;
}
else
{
if (KeyTestList.Count > GlobalInfo.hardwareManage.PersonNum)//达到指定键盘数量,不做处理
return;
if (!GlobalInfo.hardwareManage.JudgeKeyStatus(KeyID))
return;
}
if (KeyTestList.ContainsKey(KeyID))
KeyTestList[KeyID] = sData;
else
KeyTestList.Add(KeyID, sData);
string sID = KeyID.ToString();
Unit u = null;
if (!Map.Contains(sID))
{
if (Map.CountNoID > 0)
{
u = Map[""];
Map.RemoveAt(Map.CountHaveID);//移除没有ID的单元
u.ID = sID;
Map.Add(u);
}
}
else
{
u = Map[sID];
}
if (u != null)
{
u.Brush = BrushVal;
u.Data = sData;
}
//杨斌 2015-03-13
tmrMap.Interval = 200;
tmrMap.Enabled = true;
}
//void tmrMap_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
void tmrMap_Tick(object sender, EventArgs e)
{
tmrMap.Enabled = false;
DrawMap();//杨斌 2013-06-18
if (!GlobalInfo.hardwareManage.HardwareTestEnable)
return;
tmrMap.Enabled = true;
}
///
/// 杨斌 2015-03-13
///
//System.Timers.Timer tmrMap = new System.Timers.Timer();
System.Windows.Forms.Timer tmrMap = new System.Windows.Forms.Timer();
///
/// 刷新坐席图和统计值:在开始清空时,数据改变时,停止时,坐席图滚动条滚动时
/// 创建:杨斌 2013-06-18
///
private void DrawMap()
{
TimerCount t = new TimerCount();
t.StartCount();
if (picMap.IsDisposed) return;
//需要排序和重新布局
if ((!PersonNumEqualKeyNum) && (!EnableList))
{
Map.Sort();
InitMapLayoutAtuo();
}
Map.DrawVisibleUnits(picMap, pnlMapBack);
//Map.DrawVisibleUnits(picMap, picMap.Parent);
SetBitmap();
int onLineKeyNum = GetOnLineNum();
RefreshLable(onLineKeyNum);
button1.Text = t.GetCountMS().ToString();
}
///
/// 刷新标签,在线个数
///
///
private void RefreshLable(int onLineKeyNum)
{
lblOnline.Text = GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "lblOnline", "在线:") + onLineKeyNum.ToString();
lblOffline.Text = GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "lblOffline", "不在线:") + (Map.Count - onLineKeyNum - GetELVNum()).ToString();
lblELV.Text = GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "lblELV", "弱电:") + GetELVNum().ToString();
}
///
/// 是否启用名单,初始化时调用一次,避免受到按键值时判断增加时间,影响速度
/// 杨斌 2013-06-14
///
private bool EnableList = GlobalInfo.response.EnableList;
///
/// 判断键盘数量是否正好等于范围,初始化时调用一次,避免受到按键值时判断增加时间,影响速度
/// 杨斌 2013-06-14
///
private bool PersonNumEqualKeyNum = GlobalInfo.hardwareManage.PersonNumEqualKeyNum();
///
/// 键盘监控触发事件
///
///
void hardwareManage_KeyMonitorStatusEvent(int BaseID, string KeyID, double BatteryVoltage)
{
Brush BrushVal = null;
if (BatteryVoltage < GlobalInfo.hardwareManage.Voltage)
BrushVal = BrushELV;
else
BrushVal = BrushOnline;
ChangeMap(BaseID.ToString(), KeyID.ToString(), BatteryVoltage.ToString(), BrushVal, false);
}
///
/// 自动表决触发事件
///
///
///
///
void hardwareManage_KeyTestStatusEvent(string BaseTag, string KeyID, string KeyValue)
{
ChangeMap(BaseTag, KeyID, KeyValue, BrushOnline, true);
}
///
/// 坐席图重新布局
/// 创建:杨斌 2013-06-18
///
private void InitMapLayoutAtuo_old()
{
Map.IsShowData = true;//杨斌 2013-08-02
int count = picMap.ClientSize.Width * pnlMapBack.ClientSize.Height / 2000;//自动根据面积计算个数 杨斌 2010-01-06
//杨斌 2012-10-15
if (cobTestType.SelectedIndex == 1)//自动表决测试
{
count = picMap.ClientSize.Width * pnlMapBack.ClientSize.Height / 2000;
}
else
{
//杨斌 2012-10-15
if (Map.Count > 500)
count = picMap.ClientSize.Width * pnlMapBack.ClientSize.Height / 1500;
else
count = picMap.ClientSize.Width * pnlMapBack.ClientSize.Height / 2000;
}
if (count < 5) count = 5;
Map.InitUnitsLayoutAtuo(count, picMap.ClientSize.Width, pnlMapBack.ClientSize.Height, picMap);
//picMap.CreateGraphics().Clear(picMap.BackColor);//先清空避免尺寸改变时难看
}
///
/// 坐席图重新布局
/// 创建:杨斌 2015-09-17
///
private void InitMapLayoutAtuo()
{
Map.IsShowData = true;//杨斌 2013-08-02
int count = picMap.ClientSize.Width * pnlMapBack.ClientSize.Height / 4500;//自动根据面积计算个数 杨斌 2010-01-06
//杨斌 2012-10-15
if (cobTestType.SelectedIndex == 1)//自动表决测试
{
count = picMap.ClientSize.Width * pnlMapBack.ClientSize.Height / 5000;
}
else
{
//杨斌 2012-10-15
if (Map.Count > 500)
count = picMap.ClientSize.Width * pnlMapBack.ClientSize.Height / 4500;
else
count = picMap.ClientSize.Width * pnlMapBack.ClientSize.Height / 5000;
}
if (count < 5) count = 5;
Map.InitUnitsLayoutAtuo(count, picMap.ClientSize.Width, pnlMapBack.ClientSize.Height, picMap, 1);
//picMap.CreateGraphics().Clear(picMap.BackColor);//先清空避免尺寸改变时难看
}
private void FrmKeypadTest_Resize(object sender, EventArgs e)
{
if (Map == null)
{
InitMap();
}
try
{
InitMapLayoutAtuo();//杨斌 2013-06-18
DrawMap();//杨斌 2013-06-18
}
catch (Exception ex)
{
SystemLog.WriterLog(ex);
}
}
///
/// 设置坐席图图片
/// 创建:杨斌 2010-07-14
///
private void SetBitmap()
{
try
{
if (picMap.IsDisposed) return;
this.picMap.BackgroundImage = Map.Bitmap;
}
catch (Exception ex)
{
SystemLog.WriterLog(ex);
}
}
private void pnlStatuTag_Click(object sender, EventArgs e)
{
if (!(this.Parent == null))
{
ShowSelfEventHander();
}
}
private void FrmKeypadTest_FormClosed(object sender, FormClosedEventArgs e)
{
//停止键盘测试
GlobalInfo.hardwareManage.HardwareTestEnable = false;
}
///
/// 清空键盘面板信息
///
private void ClearMap()
{
Graphics gc = picMap.CreateGraphics();
if ((!PersonNumEqualKeyNum) && (!EnableList))
{
List LstU = new List();
for (int i = 0; i < Map.Count; i++)
{
Map[i].ID = "";
Map[i].Data = "";
Map[i].Brush = BrushOffline;
Map[i].LineColor = Color.Red;
Map[i].LineBroad = 1;
LstU.Add(Map[i]);
}
Map.Clear();
for (int i = 0; i < LstU.Count; i++)
{
Map.Add(LstU[i]);
}
}
else
{
for (int i = 0; i < Map.Count; i++)
{
Map[i].Data = "";
Map[i].Brush = BrushOffline;
Map[i].LineColor = Color.Red;
Map[i].LineBroad = 1;
}
}
DrawMap();//杨斌 2013-06-18
lblOnline.Text = GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "lblOnline", "在线:") + "0";
lblOffline.Text = GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "lblOffline", "不在线:") + Map.Count.ToString();
lblELV.Text = GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "lblELV", "弱电:") + "0";
//杨斌 2015-01-16
labCount.Text = GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "labCount", "键盘总数:") + Map.Count;
}
///
/// 获得在线数量
///
///
private int GetOnLineNum()
{
int iNum = 0;
for (int i = 0; i < Map.Count; i++)
{
if (Map[i].Brush == BrushOnline)
iNum += 1;
}
return iNum;
}
///
/// 获得弱电数量
///
///
private int GetELVNum()
{
int iNum = 0;
for (int i = 0; i < Map.Count; i++)
{
if (Map[i].Brush == BrushELV)
iNum += 1;
}
return iNum;
}
private void picMap_DoubleClick(object sender, EventArgs e)
{
if (this.WindowState == System.Windows.Forms.FormWindowState.Maximized)
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
else
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
private void pnlMapBack_Scroll(object sender, ScrollEventArgs e)
{
DrawMap();//杨斌 2013-06-18
}
//杨斌 2012-10-15
private void cobTestType_SelectedIndexChanged(object sender, EventArgs e)
{
FrmKeypadTest_Resize(null, null);
}
int testCnt = 2000;
private void button1_Click(object sender, EventArgs e)
{
//TimerCount t = new TimerCount();
//t.StartCount();
Map.IsShowData = (cobTestType.SelectedIndex == 1);//YB 2013-06-18
//Map.IsShowData = true;
KeyTestList.Clear();
ClearMap();
//t.StartCount();
//for (int i = 0; i < Map.Count; i++)
//{
// Map[i].Brush = BrushOffline;
// Map[i].Data = "";
//}
Random rd = new Random();
for (int i = 1; i <= testCnt; i++)
{
if (cobTestType.SelectedIndex == 0)
hardwareManage_KeyMonitorStatusEvent(1, i.ToString(), 2 + rd.NextDouble());
else
hardwareManage_KeyTestStatusEvent("1", i.ToString(), ((int)(rd.NextDouble() * 3 + 1)).ToString());
}
//DrawMap();
if (testCnt == 2000)
testCnt = 1;
else
testCnt = 2000;
//button1.Text = t.GetCountMS().ToString();
}
}
}