using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace KingLib
{
public delegate void BaseConnectEvt(BasePar par);
public delegate void ReTryConnectEvt();
public delegate void BeforReTryConnecEvt(string BaseUsedByApp, out bool isReTry);
public delegate void KeyVoteEvt(KeyData data);
public class MySDK
{
public event BaseConnectEvt OnBaseConnect = null;
public event ReTryConnectEvt OnReTryConnectEvt = null;
public event BeforReTryConnecEvt OnBeforReTryConnecEvt = null;
public event KeyVoteEvt OnKeyVoteEvt = null;
public SunVote.BaseConnection BaseConn = null;
public SunVote.BaseManage BaseMng = null;
public SunVote.KeypadManage KeyMng = null;
public SunVote.HardwareTest HardTest = null;
public SunVote.SignIn SignIn = null;
public SunVote.Choices Choices = null;
public SunVote.Vote Vote = null;
public SunVote.TrueFalse TrueFalse = null;
public SunVote.Sequence Sequence = null;
public SunVote.Number Number = null;
public SunVote.RushAnswer RushAnswer = null;
public SunVote.Cloze Cloze = null;
public SunVote.Request Request = null;
public MySDK()
{
try
{
tmrConnect.Tick += tmrConnect_Tick;
BaseConn = new SunVote.BaseConnection();
BaseConn.BaseOnLine += BaseConn_BaseOnLine;
//必须注册才能使用设置键盘序列号和配对码等功能
BaseConn.License = "sunskysdk";
BaseMng = new SunVote.BaseManage();
BaseMng.BaseConnection = BaseConn;
KeyMng = new SunVote.KeypadManage();
KeyMng.BaseConnection = BaseConn;
HardTest = new SunVote.HardwareTest();
HardTest.BaseConnection = BaseConn;
//50R遥控器
Request = new SunVote.Request();
Request.BaseConnection = BaseConn;
//签到,签到码签到
SignIn = new SunVote.SignIn();
SignIn.BaseConnection = BaseConn;
SignIn.KeyStatus += SignIn_KeyStatus;
SignIn.KeyStatusSN += SignIn_KeyStatusSN;
//单选,多选,分组,判断,评议
Choices = new SunVote.Choices();
Choices.BaseConnection = BaseConn;
Choices.KeyStatus += Choices_KeyStatus;
Choices.KeyStatusSN += Choices_KeyStatusSN;
//表决
Vote = new SunVote.Vote();
Vote.BaseConnection = BaseConn;
Vote.KeyStatus += Vote_KeyStatus;
Vote.KeyStatusSN += Vote_KeyStatusSN;
//判断
TrueFalse = new SunVote.TrueFalse();
TrueFalse.BaseConnection = BaseConn;
TrueFalse.KeyStatus += TrueFalse_KeyStatus;
TrueFalse.KeyStatusSN += TrueFalse_KeyStatusSN;
//排序
Sequence = new SunVote.Sequence();
Sequence.BaseConnection = BaseConn;
Sequence.KeyStatus += Sequence_KeyStatus;
Sequence.KeyStatusSN += Sequence_KeyStatusSN;
//数字,选举,评分
Number = new SunVote.Number();
Number.BaseConnection = BaseConn;
Number.KeyStatus += Number_KeyStatus;
Number.KeyStatusSN += Number_KeyStatusSN;
//填空
Cloze = new SunVote.Cloze();
Cloze.BaseConnection = BaseConn;
Cloze.KeyStatus += Cloze_KeyStatus;
Cloze.KeyStatusSN += Cloze_KeyStatusSN;
//抢答
RushAnswer = new SunVote.RushAnswer();
RushAnswer.BaseConnection = BaseConn;
RushAnswer.KeyStatus += RushAnswer_KeyStatus;
RushAnswer.KeyStatusSN += RushAnswer_KeyStatusSN;
}
catch (Exception ex)
{
initInfo = ex.ToString();
}
}
private void RaiseKeyVote(int KeyID, string KeyValue, double KeyTime)
{
if (OnKeyVoteEvt != null)
{
KeyData d = new KeyData();
d.KeyID = KeyID.ToString();
d.KeyNum = KeyID;
d.KeyValue = KeyValue;
d.Speed = KeyTime;
OnKeyVoteEvt(d);
}
}
private void RaiseKeyVote(string KeySN, string KeyValue, double KeyTime)
{
if (OnKeyVoteEvt != null)
{
KeyData d = new KeyData();
d.KeyID = KeySN;
d.KeyNum = 0;
d.KeyValue = KeyValue;
d.Speed = KeyTime;
OnKeyVoteEvt(d);
}
}
///
/// 签到
///
void SignIn_KeyStatusSN(string BaseTag, string KeySN, int ValueType, string KeyValue, double KeyTime)
{
RaiseKeyVote(KeySN, KeyValue, KeyTime);
}
void SignIn_KeyStatus(string BaseTag, int KeyID, int ValueType, string KeyValue)
{
RaiseKeyVote(KeyID, KeyValue, 0);
}
///
/// 选择
///
void Choices_KeyStatusSN(string BaseTag, string KeySN, string KeyValue, double KeyTime)
{
RaiseKeyVote(KeySN, KeyValue, KeyTime);
}
void Choices_KeyStatus(string BaseTag, int KeyID, string KeyValue, double KeyTime)
{
RaiseKeyVote(KeyID, KeyValue, KeyTime);
}
///
/// 表决
///
void Vote_KeyStatusSN(string BaseTag, string KeySN, string KeyValue, double KeyTime)
{
RaiseKeyVote(KeySN, KeyValue, KeyTime);
}
void Vote_KeyStatus(string BaseTag, int KeyID, string KeyValue)
{
RaiseKeyVote(KeyID, KeyValue, 0);
}
void TrueFalse_KeyStatusSN(string BaseTag, string KeySN, string KeyValue, double KeyTime)
{
RaiseKeyVote(KeySN, KeyValue, KeyTime);
}
void TrueFalse_KeyStatus(string BaseTag, int KeyID, string KeyValue, double KeyTime)
{
RaiseKeyVote(KeyID, KeyValue, KeyTime);
}
///
/// 排序
///
void Sequence_KeyStatusSN(string BaseTag, string KeySN, string KeyValue, double KeyTime)
{
RaiseKeyVote(KeySN, KeyValue, KeyTime);
}
void Sequence_KeyStatus(string BaseTag, int KeyID, string KeyValue, double KeyTime)
{
RaiseKeyVote(KeyID, KeyValue, KeyTime);
}
///
/// 数字
///
void Number_KeyStatusSN(string BaseTag, string KeySN, string KeyValue, double KeyTime)
{
RaiseKeyVote(KeySN, KeyValue, KeyTime);
}
void Number_KeyStatus(string BaseTag, int KeyID, string KeyValue, double KeyTime)
{
RaiseKeyVote(KeyID, KeyValue, KeyTime);
}
///
/// 文本题
///
void Cloze_KeyStatusSN(string BaseTag, string KeySN, string KeyValue, double KeyTime)
{
RaiseKeyVote(KeySN, KeyValue, KeyTime);
}
void Cloze_KeyStatus(string BaseTag, int KeyID, string KeyValue, double KeyTime)
{
RaiseKeyVote(KeyID, KeyValue, KeyTime);
}
///
/// 抢答
///
void RushAnswer_KeyStatusSN(string BaseTag, string KeySN, string KeyValue, double KeyTime)
{
RaiseKeyVote(KeySN, KeyValue, KeyTime);
}
void RushAnswer_KeyStatus(string BaseTag, int KeyID, string KeyValue, double KeyTime)
{
RaiseKeyVote(KeyID, KeyValue, KeyTime);
}
private VoteType mVoteStartType = VoteType.None;
public VoteType VoteStartType
{
get
{
return mVoteStartType;
}
set
{
//if ((mVoteStartType != value) && VoteStart)
//{
// VoteStart = false;
//}
mVoteStartType = value;
}
}
public int VoteStartMode { get; set; }
///
/// 允许修改模式:1允许,0不允许
///
public int VoteModifyMode { get; set; }
///
/// 启用保密模式:0不保密,1保密
///
public int VoteSecrecyMode { get; set; }
private bool mVoteStart = false;
public bool VoteStart
{
get
{
return mVoteStart;
}
set
{
mVoteStart = value;
if (value)
{
switch (mVoteStartType)
{
case VoteType.SignIn:
SignIn.StartMode = VoteStartMode;
SignIn.Start();
break;
case VoteType.Choices:
Choices.ModifyMode = VoteModifyMode;
Choices.SecrecyMode = VoteSecrecyMode;
Choices.StartMode = VoteStartMode;
Choices.Start();
break;
case VoteType.Vote:
Vote.ModifyMode = VoteModifyMode;
Vote.SecrecyMode = VoteSecrecyMode;
Vote.StartMode = VoteStartMode;
Vote.Start();
break;
case VoteType.TrueFalse:
TrueFalse.ModifyMode = VoteModifyMode;
TrueFalse.SecrecyMode = VoteSecrecyMode;
TrueFalse.StartMode = VoteStartMode;
TrueFalse.Start();
break;
case VoteType.Sequence:
Sequence.ModifyMode = VoteModifyMode;
Sequence.SecrecyMode = VoteSecrecyMode;
Sequence.StartMode = VoteStartMode;
Sequence.Start();
break;
case VoteType.Number:
Number.ModifyMode = VoteModifyMode;
Number.SecrecyMode = VoteSecrecyMode;
Number.StartMode = VoteStartMode;
Number.Start();
break;
case VoteType.Cloze:
Cloze.ModifyMode = VoteModifyMode;
Cloze.SecrecyMode = VoteSecrecyMode;
Cloze.StartMode = VoteStartMode;
Cloze.Start();
break;
case VoteType.RushAnswer:
RushAnswer.StartMode = VoteStartMode;
RushAnswer.Start();
break;
}
}
else
{
switch (mVoteStartType)
{
case VoteType.SignIn:
SignIn.Stop();
break;
case VoteType.Choices:
Choices.Stop();
break;
case VoteType.Vote:
Vote.Stop();
break;
case VoteType.TrueFalse:
TrueFalse.Stop();
break;
case VoteType.Sequence:
Sequence.Stop();
break;
case VoteType.Number:
Number.Stop();
break;
case VoteType.Cloze:
Cloze.Stop();
break;
case VoteType.RushAnswer:
RushAnswer.Stop();
break;
}
}
}
}
private string initInfo = "";
public string InitInfo
{
get
{
return initInfo;
}
}
private Dictionary DicBaseCheck = new Dictionary();
///
/// 已连接成功的基站列表
///
public Dictionary DicBaseConnect = new Dictionary();
///
/// 记录调用的连接命令true=连接,false=关闭
///
private bool IsConnectCmd = false;
Timer TmrReTryConnect = null;
void BaseConn_BaseOnLine(int BaseID, int BaseState)
{
if (BaseState == -5)
{
if (OnBeforReTryConnecEvt != null)
{
bool isReTry = false;
OnBeforReTryConnecEvt(BaseConn.BaseUsedByApp, out isReTry);
if (isReTry)
{
if (TmrReTryConnect == null)
{
TmrReTryConnect = new Timer();
TmrReTryConnect.Tick += TmrReTryConnect_Tick;
}
TmrReTryConnect.Interval = 10;
TmrReTryConnect.Enabled = true;
}
}
//if (MessageBox.Show(LanText.BaseUsedByApp + "\r\n" + BaseConn.BaseUsedByApp, "", MessageBoxButtons.RetryCancel) == DialogResult.Retry)
//{
// if (TmrReTryConnect == null)
// {
// TmrReTryConnect = new Timer();
// TmrReTryConnect.Tick += TmrReTryConnect_Tick;
// }
// TmrReTryConnect.Interval = 10;
// TmrReTryConnect.Enabled = true;
//}
return;
}
if (BeCehckBase)
{
BasePar par = new BasePar();
if (BaseID > 0)
{
if (DicBaseCheck.ContainsKey(BaseID))
par = DicBaseCheck[BaseID];
else
DicBaseCheck.Add(BaseID, par);
}
par.BaseID = BaseID;
par.BaseState = BaseState;
//if (!DicBaseCheck.ContainsKey(BaseID))
// DicBaseCheck.Add(BaseID, new BasePar());
//BasePar par = DicBaseCheck[BaseID];
//par.BaseID = BaseID;
//par.BaseState = BaseState;
if (BaseState == 1)
{
//if (!DicBaseConnect.ContainsKey(BaseID))
// DicBaseConnect.Add(BaseID, new BasePar());
//BasePar parOK = DicBaseConnect[BaseID];
//parOK.BaseID = BaseID;
//parOK.BaseState = BaseState;
BasePar parOK = new BasePar();
if (BaseID > 0)
{
if (DicBaseConnect.ContainsKey(BaseID))
parOK = DicBaseConnect[BaseID];
else
DicBaseConnect.Add(BaseID, parOK);
}
parOK.BaseID = BaseID;
parOK.BaseState = BaseState;
//if (OnBaseConnect != null)
// OnBaseConnect(parOK);
}
else if (!BeSeatchMultiBase)//杨斌 2016-09-18
{
DicBaseConnect.Clear();
}
if ((DicBaseCheck.Count >= MaxBaseID)
|| (!BeSeatchMultiBase && DicBaseConnect.Count > 0)) //搜索完了,重新连
{
BaseCehck = "";
foreach (var v in DicBaseCheck)
{
if (v.Value.BaseState == 1)
{
if (BaseCehck.Length > 0)
BaseCehck += ",";
BaseCehck += v.Key;
}
}
if (IsConnectCmd)
{
tmrConnect.Interval = 1;
tmrConnect.Enabled = true;
}
}
}
else
{
BasePar parOK = new BasePar();
if (BaseID > 0)
{
if (DicBaseConnect.ContainsKey(BaseID))
parOK = DicBaseConnect[BaseID];
else
DicBaseConnect.Add(BaseID, parOK);
}
parOK.BaseID = BaseID;
parOK.BaseState = BaseState;
if (OnBaseConnect != null)
OnBaseConnect(parOK);
if (BaseState == -3)
{
if (DicBaseConnect.Count > 0)
DicBaseConnect.Clear();
}
else if (BaseState != 1)
{
if (DicBaseConnect.ContainsKey(BaseID))
DicBaseConnect.Remove(BaseID);
}
}
}
void TmrReTryConnect_Tick(object sender, EventArgs e)
{
TmrReTryConnect.Enabled = false;
if (OnReTryConnectEvt != null)
OnReTryConnectEvt();
}
public string BaseConnectOkIDs()
{
string res = "";
if (DicBaseConnect.Count > 0)
{
List lst = DicBaseConnect.Keys.OrderBy(o => o).ToList();
List sLst = new List();
foreach (var v in lst)
sLst.Add(v.ToString());
res = string.Join(",", sLst);
}
return res;
}
void tmrConnect_Tick(object sender, EventArgs e)
{
tmrConnect.Enabled = false;
if (IsConnectCmd)
{
if (connectMode == 1)
BaseConnect(BaseCehck);
else if (connectMode == 2)
{
string[] aryIP = BaseConn.BaseIP.Split(',');
if (aryIP.Length > 0)
BaseConnectIPOne(aryIP[0], BaseCehck);
}
}
}
Timer tmrConnect = new Timer();
///
/// 自动查找的最大基站编号
///
public static int MaxBaseID = 32;
private bool BeCehckBase = false;
private string BaseCehck = "";
///
/// 搜索多基站
///
private bool BeSeatchMultiBase = false;
private int connectMode = 1;
public int ConnectMode
{
get
{
return connectMode;
}
}
private string connectBaseID = "";
public string ConnectBaseID
{
get
{
return connectBaseID;
}
}
private string connectIPAddress = "";
public string ConnectIPAddress
{
get
{
return connectIPAddress;
}
}
public void BaseConnect(string baseID = "", bool beMultiBase = false)
{
DicBaseConnect.Clear();
string baseIDConn = "";
if (baseID.Length > 0)
{
BeCehckBase = false;
BeSeatchMultiBase = false;
baseIDConn = baseID;
}
else
{
DicBaseCheck.Clear();
DicBaseCheck.Clear();
BeSeatchMultiBase = beMultiBase;//杨斌 2016-09-18
BeCehckBase = beMultiBase;
if (beMultiBase)
baseIDConn = "1-" + MaxBaseID;
else
baseIDConn = "0";
}
if (BaseConn != null)
{
BaseConn.Open(1, baseIDConn);
}
connectMode = 1;
connectBaseID = baseIDConn;
IsConnectCmd = true;
}
public void BaseConnect(string baseID, string ipAddress)
{
DicBaseConnect.Clear();
BeCehckBase = false;
if (BaseConn != null)
{
BaseConn.BaseIP = ipAddress;
BaseConn.Open(2, baseID);
}
connectMode = 2;
connectBaseID = baseID;
connectIPAddress = BaseConn.BaseIP;
IsConnectCmd = true;
}
public void BaseConnectIPOne(string ipAddress, string baseID = "")
{
DicBaseConnect.Clear();
BeSeatchMultiBase = false;
string baseIDConn = "";
if (BaseConn != null)
{
if (baseID.Length > 0)
{
BeCehckBase = false;
BaseConn.BaseIP = ipAddress;
baseIDConn = baseID;
}
else
{
BeCehckBase = true;
DicBaseCheck.Clear();
baseIDConn = "1-" + MaxBaseID;
if (ipAddress.Length > 0)
{
string[] ary = ipAddress.Split(',');
string ips = "";
for (int i = 1; i <= MaxBaseID; i++)
{
if (ips.Length > 0)
ips += ",";
ips += ary[0];
}
BaseConn.BaseIP = ips;
}
else
BaseConn.BaseIP = ipAddress;
}
BaseConn.Open(2, baseIDConn);
}
connectMode = 2;
connectBaseID = baseIDConn;
connectIPAddress = BaseConn.BaseIP;
IsConnectCmd = true;
}
public void BaseDisConnect()
{
tmrConnect.Stop();
tmrConnect.Enabled = false;
if (BaseConn != null)
{
IsConnectCmd = false;
BaseConn.Close();
DicBaseConnect.Clear();
}
}
private static Dictionary DicBaseType = new Dictionary();
///
/// 取基站型号
///
///
///
public static string GetBaseType(int HModel)
{
string res = "";
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;
}
}
public enum VoteType
{
None = 0,
SignIn,
Choices,
Vote,
TrueFalse,
Sequence,
Number,
Cloze,
RushAnswer,
}
public class VoteSet
{
public VoteType VoteStartType = VoteType.None;
public int ModifyMode = 1;
public int SecrecyMode = 0;
}
public class KeypadTypeInfo
{
public string KeypadType = "";
///
/// 小于此电压为低电量
///
public double BatteryVoltageLow = 0;
///
/// 小于此电压为报警电量。杨斌 2016-08-16
///
public double BatteryVoltageWarning = 0;
public static void InitKeypadType()
{
DicKeypadType.Clear();
AddKeypad("M52Plus", 2.7, 2.84);
AddKeypad("M52Li", 3.7, 3.84);
AddKeypad("M52", 2.7, 2.84);
AddKeypad("M50", 2.7, 2.84);
AddKeypad("M50Plus", 2.7, 2.84);
AddKeypad("M50Plus", 2.7, 2.84);
AddKeypad("M30", 2.7, 2.84);//杨斌 2017-02-07
//AddKeypad("F00", 3.3);
//AddKeypad("M40", 2.6);
AddKeypad("M40L", 2.7, 2.84);
//AddKeypad("V52", 3.3);//李慧文说,跟M52Li一样的
AddKeypad("V52Li", 3.7, 3.84);
AddKeypad("S50", 2.7, 2.84);
AddKeypad("S50Plus", 2.7, 2.84);
AddKeypad("S50Li", 3.7, 3.84);
AddKeypad("S52", 2.7, 2.84);
AddKeypad("S52Plus", 2.7, 2.84);
AddKeypad("S56", 2.7, 2.84);
//AddKeypad("S56PLUS", 2.3);
//AddKeypad("S70", 2.4);
AddKeypad("E10", 3.7, 3.84);
}
public static Dictionary DicKeypadType = new Dictionary();
private static void AddKeypad(string keypadType, double batteryVoltageLow, double batteryVoltageWarning = 0)
{
if (!DicKeypadType.ContainsKey(keypadType))
{
DicKeypadType.Add(keypadType,
new KeypadTypeInfo()
{
KeypadType = keypadType,
BatteryVoltageLow = batteryVoltageLow,
BatteryVoltageWarning = batteryVoltageWarning
});
}
}
}
public class BasePar
{
///
/// 基站连接状态
///
public int BaseState = 0;
public int BaseID = 0;
public int BaseChannel = 0;
public int KeyIDMin = 0;
public int KeyIDMax = 0;
public int RFPower = 0;
///
/// 系统特性-键盘报告模式
///
public int KeyReportMode = 0;
public int KeyOffTime = 0;
public int BackLightMode = 0;
public int BuzzerMode = 0;
public int CommitMode = 0;
///
/// 基站序列号
///
public string HSerial = "";
public int HModel = 0;
public string HVer = "";
public string SVer = "";
///
/// 基站型号,根据HModel决定
///
public string BaseType = "";
///
/// 基站IP地址
///
public string TcpIPAddress = "";
public string TcpSubnetMask = "";
public string TcpGatewayAddress = "";
public string TcpMACAddress = "";
///
/// 基站配对码
///
public string MatchCode = "";
public int MatchMode = 0;
public string BaseName = "";
///
/// WiFi基站模式
///
public int BaseMode = 0;
public string RouterName = "";
public int AuthMode = 0;
public int EncryMode = 0;
public string RouterPwd = "";
}
public class BaseParBackup
{
///
/// 备份时间
///
public string BaseBackupTime = "";
public int BaseID = 0;
public int BaseChannel = 0;
public int KeyIDMin = 0;
public int KeyIDMax = 0;
public int RFPower = 0;
///
/// 系统特性-键盘报告模式
///
public int KeyReportMode = 0;
public int KeyOffTime = 0;
public int BackLightMode = 0;
public int BuzzerMode = 0;
public int CommitMode = 0;
///
/// 基站序列号
///
public string HSerial = "";
public int HModel = 0;
public string HVer = "";
public string SVer = "";
///
/// 基站IP地址
///
public string TcpIPAddress = "";
public string TcpSubnetMask = "";
public string TcpGatewayAddress = "";
public string TcpMACAddress = "";
///
/// 基站配对码
///
public string MatchCode = "";
public int MatchMode = 0;
public string BaseName = "";
}
public class KeyData
{
///
/// 关键字,键盘编号或SN
///
public string KeyID = "";
///
/// 用来按数字排序,键盘编号1-9999范围
///
public int KeyNum = 0;
public string KeyValue = "";
public double Speed = 0;
public double BatteryVoltage = 0;
public int LowBattery = 0;
public int WarningBattery = 0;
public int CommitOK = -1;//杨斌 2020-06-12
}
public class KeySNInfo
{
public int KeyNum = 0;
public string KeySN = "";
}
}