///--------------------------------------------------------------------------
/// 文 件 名:HardwareManageARS.cs
/// 功能描述:基站连接类
/// 基站连接
/// 创建标识:赵丽 2011-10-30
/// 修改标识:赵丽 2011-11-20
///--------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SunVote;
using System.Collections.Specialized;
using System.Collections;
using System.Threading;
using System.Windows.Forms;
using GeneralLib;
namespace SunVoteARSPPT
{
#region 委托
///
/// 基站连接委托
///
///
///
public delegate void ConnectEvent(string statusStr);//基站连接委托,返回连接状态字符串
///
/// 杨斌 2016-04-19
///
public delegate void ReTryConnectEvt();
///
/// 传基站连接状态
///
///
public delegate void ConnectStatusEvent(int BaseID);
///
/// 读基站信息委托
///
///
public delegate void ReadBaseInfoEvent(int BaseID);//连接成功读基站信息
///
/// 写配置文件
/// 单基站需要改基站ID
///
///
///
///
public delegate void SysConfigBaseListEvent(int BaseID, string key, object val);
///
/// 是否为反馈状态
///
public delegate void ResponseStateEventHander(bool beRespons);
///
/// 激活窗体交给幻灯片的窗体,防止Windows7下播放幻灯片时,拔出基站时系统崩溃
/// 创建标志 2012-05-09
///
public delegate void ActiveSlideWindowEventHander();
#endregion
#region 基站信息类
///
/// 基站信息类
///
public class BasePara
{
///
/// 构造函数,初始化信息
///
public BasePara()
{
BaseConnectStatus = ConnectStatus.csDisconnect;
}
///
/// 连接类型
///
public enum ConnectStatus
{
///
/// 连接成功
///
csSuccess = 1,
///
/// 基站错误
///
csBaseErr = 2,
///
/// 不支持的连接方式
///
csConnWayErr = 3,
///
/// 串口错误
///
csPortErr = 4,
///
/// 手动断开连接
///
csDisconnect = 5
}
#region 公有属性
///
/// 基站编号
///
private int baseID = 1;
///
/// 基站ID
///
public int BaseID
{
get { return baseID; }
set
{
if (baseID != value)
{
baseID = value;
}
}
}
///
/// 启用状态
///
private bool enableStatus = true;
///
/// 启用状态
///
public bool EnableStatus
{
get { return enableStatus; }
set
{
if (enableStatus != value)
{
enableStatus = value;
}
}
}
///
/// 基站IP
///
private string baseIPAddress = "";
///
/// 基站IP地址
///
public string BaseIPAddress
{
get { return baseIPAddress; }
set
{
if (baseIPAddress != value)
{
baseIPAddress = value;
}
}
}
private static Dictionary DicBaseType = new Dictionary();
///
/// 取基站型号
///
///
///
public static string GetBaseType(int HModel)
{
string res = HModel.ToString();
string hex = HModel.ToString("X");
if (DicBaseType.Count < 1)
{
DicBaseType.Add("B1", "EA1000");
DicBaseType.Add("B2", "EA2000T");
DicBaseType.Add("B3", "PVS-3000/2.4G");
DicBaseType.Add("B4", "PVS-2010/2.4G");
DicBaseType.Add("B5", "PVS-2010/433M");
DicBaseType.Add("B6", "C1000");
DicBaseType.Add("B7", "EA2100T");
DicBaseType.Add("B8", "C2000T");
DicBaseType.Add("B9", "EA4000T");
DicBaseType.Add("BA", "EA4000R");
DicBaseType.Add("BB", "C1200");//(建议取消)
DicBaseType.Add("BC", "EA1200");
DicBaseType.Add("BD", "EA4100T");
DicBaseType.Add("BE", "C4000T");
DicBaseType.Add("BF", "EA4200W");//WiFi
}
if (DicBaseType.ContainsKey(hex))
res = DicBaseType[hex];
return res;
}
///
/// 基站型号
/// 修改:杨斌 2012-04-05
///
private int baseModel = 0;
public int BaseModel
{
get { return baseModel; }
set
{
//if (baseModel != value)
//{
baseModel = value;
BaseModelName = GetBaseType(baseModel);
//switch (baseModel)
//{
// case 177:
// BaseModelName = "EA1000";
// break;
// case 178:
// BaseModelName = "EA2000";
// break;
// case 182:
// BaseModelName = "C1000";
// break;
// case 185:
// BaseModelName = "EA4000T";
// break;
// case 188:
// BaseModelName = "EA1200";
// break;
// default:
// BaseModelName = baseModel.ToString();
// break;
//}
//}
}
}
public string BaseModelName = "";
///
/// 基站频点
///
private int baseChannel = 0;
public int BaseChannel
{
get { return baseChannel; }
set
{
if (baseChannel != value)
{
baseChannel = value;
}
}
}
///
/// 基站序列号
///
public string BaseSerialNumber { get; set; }
///
/// 基站配对模式
/// 杨斌 2013-05-08
///
public int BaseMatchMode { get; set; }
///
/// 基站名称
/// 杨斌 2015-01-06
///
public string BaseName { get; set; }
///
/// 在线键盘
///
private int onLineKeyNum = 0;
public int OnLineKeyNum
{
get { return onLineKeyNum; }
set
{
if (onLineKeyNum != value)
{
onLineKeyNum = value;
}
}
}
///
/// 在线键盘
///
private int eLVKeyNum = 0;
public int ELVKeyNum
{
get { return eLVKeyNum; }
set
{
if (eLVKeyNum != value)
{
eLVKeyNum = value;
}
}
}
///
/// 连接状态
///
private ConnectStatus baseConnectStatus = ConnectStatus.csDisconnect;
public ConnectStatus BaseConnectStatus
{
get { return baseConnectStatus; }
set
{
if (baseConnectStatus != value)
{
baseConnectStatus = value;
}
}
}
#endregion
}
#endregion
#region 基站连接
///
/// 基站连接类
///
public class BaseConnect
{
public enum ConnectModel
{
cmConnAll = 1,
cmConnOne = 2
}
///
/// 基站连接类
///
public SunVote.BaseConnection baseConnection = null;
///
/// 创建定时器
///
//private System.Windows.Forms.Timer tmrConnectBase = null;
System.Timers.Timer tmrConnectBase = null;
//static System.Threading.Timer tmrConnectBase = null;
#region 属性
///
/// 多基站
///
public int MultiBase { get; set; }
///
/// 最小键盘编号
///
public int KeyIDMin { get; set; }
///
/// 最大键盘编号
///
public int KeyIDMax { get; set; }
///
/// 射频功率等级
///
public int RFPower { get; set; }
///
/// 键盘报告状态模式
///
public int KeyReportMode { get; set; }
///
/// 键盘关机时间
///
public int KeyOffTime { get; set; }
///
/// 键盘提交模式
///
public int CommitMode { get; set; }
///
/// 背光模式
///
public int BackLightMode { get; set; }
///
/// 蜂鸣模式
///
public int BuzzerMode { get; set; }
///
/// 连接类型
///
private int connectType = 1;
///
/// 连接模式:1:USB,2:TCP/IP
///
public int ConnectType
{
get
{
return connectType;
}
set
{
if (connectType != value)
{
connectType = value;
DisConnect();
Connect();
}//连接类型改变,重连基站
}
}
///
/// 基站列表
///
private OrderedDictionary baseList = null;
///
/// 基站列表,存储各基站的信息
///
public OrderedDictionary BaseList
{
get
{
return baseList;
}
set
{
if (baseList != value)
{
baseList = value;
}
}
}
///
/// 单基站模式下上一次连接成功的IP
///
private string singleBaseIP = "";
public string SingleBaseIP
{
get
{
return singleBaseIP;
}
set
{
if (singleBaseIP != value)
{
singleBaseIP = value;
scanBaseIPs = value;
for (int i = 0; i < cMaxBaseCount; i++)//杨斌 2012-10-11
{
if (i != 0)
scanBaseIPs += "," + value;
}
}
}
}
///
/// 上一次连接成功的基站编号
///
private int singleBaseID = 1;
public int SingleBaseID
{
get { return singleBaseID; }
set
{
if (singleBaseID != value)
{
singleBaseID = value;
scanBaseIDs = value.ToString();
for (int i = 1; i <= cMaxBaseCount; i++)//杨斌 2012-10-11
{
if (i != value)
{
scanBaseIDs += ",";
scanBaseIDs += i.ToString();
}
}
}
}
}
public const int cMaxBaseCount = 32;//杨斌 2013-03-29
///
/// 基站扫描基站ID号
/// 2015-09-14
///
//private string scanBaseIDs = "1,2,3,4,5,6,7,8";//杨斌 2013-03-29
private string scanBaseIDs = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32";//杨斌 2013-03-29
///
/// 基站扫描基站IP地址
///
private string scanBaseIPs = "";
///
/// 断开基站的标志
///
public bool DisConnectFlag = false;
///
/// 重连基站
///
public bool ReConnect
{
set
{
if (value)
{
ConnecOne = false;
tmrConnectBase.Enabled = true;
//tmrConnectBase.Change(100, 0);
}
}
}
///
/// 记录连接成功的基站个数
///
public int BaseNum = 0;
///
/// 处于反馈过程时,不读基站的信息
///
public bool NeedReadBaseInfo = true;
///
/// 是否处于反馈状态
///
private bool isResponse = false;
public bool IsResponse
{
get { return isResponse; }
set
{
if (isResponse != value)
{
isResponse = value;
if (ResponseStateEvent != null)
ResponseStateEvent(value);
}
}
}
#endregion
#region 构造函数
///
/// 构造函数
///
public BaseConnect()
{
//tmrConnectBase = new System.Windows.Forms.Timer();
//tmrConnectBase.Interval = 100;
//tmrConnectBase.Tick += tmrConnectBase_Tick;
tmrConnectBase = new System.Timers.Timer();
tmrConnectBase.Interval = 100;
tmrConnectBase.Elapsed += tmrConnectBase_Elapsed;
//tmrConnectBase = new System.Threading.Timer(tmrConnectBase_CallBack);
//杨斌 2012-05-07 调试问题
//此行代码,在Win7的普通用户(Users组)下运行导致PowerPoint停止运行
//创建其业务对象他如Choices也是如此
/*屏蔽。杨斌 2017-04-19。这里只要new了,自动测试可能收不齐:400个可能只有392,399等,且速度稍慢
baseConnection = new SunVote.BaseConnection();
baseConnection.ProtocolType = 0;//ARS=0,CRS=1,EVS=2 杨斌 2013-04-01
baseConnection.BaseOnLine += new SunVote.IBaseConnectionEvents_BaseOnLineEventHandler(baseConnection_baseOnLine);
//*/
connectType = 1;
//初始化基站列表
baseList = new OrderedDictionary(cMaxBaseCount);//杨斌 2012-10-11
//iniBaseParaList();
FrmPvsServer.GetFrmPvs().OnBaseConnectEvent += myPVS_BaseConnectEvent;
FrmPvsServer.GetFrmPvs().OnBaseEvent += myPVS_BaseEvent;
FrmPvsServer.GetFrmPvs().OnCommErrEx += myPVS_CommErrEx;
FrmPvsServer.GetFrmPvs().OnCommErrSys += myPVS_CommErrSys;
}
void myPVS_BaseConnectEvent(int ID, int iStatus)
{
try
{
BasePara.ConnectStatus baseConnectStatus = GetBaseConnectStatus(iStatus);
if (ID == 0)
{
for (int i = 0; i < baseList.Count; i++)
{
this[i].BaseConnectStatus = baseConnectStatus;
}//没有基站连接时重新扫描基站
if (ConnectStatusEvent != null) { ConnectStatusEvent(ID); }
if (ConnectEvent != null) { ConnectEvent(GetBaseIDs(false)); }
}//没有连接基站
else
{
if (!baseList.Contains(ID.ToString()))
{
baseList.Add(ID.ToString(), new BasePara());
}
this[ID.ToString()].BaseID = ID;
this[ID.ToString()].BaseConnectStatus = baseConnectStatus;
if (ConnectEvent != null) { ConnectEvent(GetBaseIDs(false)); }
if (ConnectStatusEvent != null) { ConnectStatusEvent(ID); }
if (iStatus == 1)
{
if (ReadBaseInfoEvent != null) { ReadBaseInfoEvent(ID); }
}
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString());
SystemLog.WriterLog(ex, false);
}
}
void myPVS_BaseEvent(int ID, int iMode, string sInfo)
{
}
void myPVS_CommErrSys(string ID, int ErrNo, string ErrInfo)
{
}
void myPVS_CommErrEx(string ID, int ErrNo, string ErrInfo)
{
}
#endregion
public void tmrConnectBaseRun()
{
if (Globals.SunVoteARSAddIn.IsExitApp)//杨斌 2015-11-10
return;
if (GlobalInfo.GetSdkType() == 1)
return;
//tmrConnectBase.Interval = 2147483647;
tmrConnectBase.Enabled = false;
//修改标志 2012-05-09
//在连接前通知激活SlideShow窗体
if (ActiveSlideWindowEvent != null)
ActiveSlideWindowEvent();
//MessageBox.Show("tmrConnectBase");
//连接指定基站或者所有基站
Connect();
//MessageBox.Show("Connect");
if (ReadBaseInfoEvent != null)
{
for (int i = 0; i < this.BaseList.Count; i++)
{
if (this[i].BaseConnectStatus == BasePara.ConnectStatus.csSuccess)
ReadBaseInfoEvent(this[i].BaseID);
}
}
//MessageBox.Show("ReadBaseInfoEvent");
}
System.Windows.Forms.Timer TmrConn = null;
void TmrConn_Tick(object sender, EventArgs e)
{
TmrConn.Enabled = false;
tmrConnectBaseRun();
}
private void tmrConnectBase_CallBack(object state)
{
//Action