Form1.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. using SuperWebSocket;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Configuration;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace ReadICCardFormsApplication
  14. {
  15. public partial class Form1 : Form
  16. {
  17. WebSocketServer server;
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. start();
  22. }
  23. protected void start()
  24. {
  25. var ip = ConfigurationManager.AppSettings["APWebSocketIP"];
  26. var port = ConfigurationManager.AppSettings["APWebSocketPort"];
  27. //WebSocket服务器端启动
  28. server = new WebSocketServer();
  29. if (!server.Setup(int.Parse(port)))
  30. {
  31. //Debug.Write("WebSocket服务器端启动失败");
  32. //处理启动失败消息
  33. return;
  34. }
  35. //新的会话连接时
  36. server.NewSessionConnected += server_NewSessionConnected;
  37. //会话关闭
  38. server.SessionClosed += server_SessionClosed;
  39. //接收到新的消息时
  40. server.NewMessageReceived += server_NewMessageReceived;
  41. if (!server.Start())
  42. {
  43. //Debug.Write(string.Format("开启WebSocket服务侦听失败:{0}:{1}", server.Config.Ip, server.Config.Port));
  44. //处理监听失败消息
  45. return;
  46. }
  47. }
  48. [DllImport("fzyktclient.dll", EntryPoint = "ykt_opendevice", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  49. protected static extern int ykt_opendevice(int nDeviceType, int nCom, int nBaud, IntPtr pRegion, [MarshalAs(UnmanagedType.LPStr)] StringBuilder pWarnmsg, [MarshalAs(UnmanagedType.LPStr)] StringBuilder pErrmsg);
  50. [DllImport("fzyktclient.dll", EntryPoint = "ykt_readopencardno_local", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  51. protected static extern int ykt_readopencardno_local(IntPtr pRegion, IntPtr pDeptid, IntPtr pDeptKey, [MarshalAs(UnmanagedType.LPStr)] StringBuilder pCardtype, [MarshalAs(UnmanagedType.LPStr)] StringBuilder pCardno, [MarshalAs(UnmanagedType.LPStr)] StringBuilder pWarnmsg, [MarshalAs(UnmanagedType.LPStr)] StringBuilder pErrmsg);
  52. [DllImport("fzyktclient.dll", EntryPoint = "ykt_readopencardinfo_local", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  53. protected static extern int ykt_readopencardinfo_local(IntPtr pRegion, IntPtr pDeptid, IntPtr pDeptKey,
  54. [MarshalAs(UnmanagedType.LPStr)] StringBuilder pIssuedeptid,
  55. [MarshalAs(UnmanagedType.LPStr)] StringBuilder pIssuedate,
  56. [MarshalAs(UnmanagedType.LPStr)] StringBuilder pCardtype,
  57. [MarshalAs(UnmanagedType.LPStr)] StringBuilder pCardno,
  58. [MarshalAs(UnmanagedType.LPStr)] StringBuilder pIdtype,
  59. [MarshalAs(UnmanagedType.LPStr)] StringBuilder pId,
  60. [MarshalAs(UnmanagedType.LPStr)] StringBuilder pName,
  61. [MarshalAs(UnmanagedType.LPStr)] StringBuilder pSex,
  62. [MarshalAs(UnmanagedType.LPStr)] StringBuilder pBirthDate,
  63. [MarshalAs(UnmanagedType.LPStr)] StringBuilder pCompany,
  64. [MarshalAs(UnmanagedType.LPStr)] StringBuilder pAddress,
  65. [MarshalAs(UnmanagedType.LPStr)] StringBuilder pTelephone,
  66. [MarshalAs(UnmanagedType.LPStr)] StringBuilder pWarnmsg,
  67. [MarshalAs(UnmanagedType.LPStr)] StringBuilder pErrmsg);
  68. [DllImport("fzyktclient.dll", EntryPoint = "ykt_closedevice", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  69. protected static extern int ykt_closedevice([MarshalAs(UnmanagedType.LPStr)] StringBuilder pWarnmsg, [MarshalAs(UnmanagedType.LPStr)] StringBuilder pErrmsg);
  70. private void btOpenDevice_Click(object sender, EventArgs e)
  71. {
  72. string region = "3502";
  73. StringBuilder pWarnmsg = new StringBuilder(1024);
  74. StringBuilder pErrmsg = new StringBuilder(1024);
  75. try
  76. {
  77. int pval = ykt_opendevice(10, 9, 3, Marshal.StringToHGlobalAnsi(region), pWarnmsg, pErrmsg);
  78. if (pval == 0)
  79. {
  80. MessageBox.Show("打开成功");
  81. }
  82. else
  83. {
  84. MessageBox.Show(string.Format("打开失败:{0}", pErrmsg.ToString()));
  85. }
  86. }
  87. catch (Exception ex )
  88. {
  89. MessageBox.Show(ex.Message);
  90. MessageBox.Show(ex.StackTrace);
  91. }
  92. }
  93. private void button2_Click(object sender, EventArgs e)
  94. {
  95. StringBuilder pCardtype = new StringBuilder(2);
  96. StringBuilder pCardno = new StringBuilder(20);
  97. StringBuilder pWarnmsg = new StringBuilder(1024);
  98. StringBuilder pErrmsg = new StringBuilder(1024);
  99. string region = "3502";
  100. int pval = ykt_opendevice(10, 9, 3, Marshal.StringToHGlobalAnsi(region), pWarnmsg, pErrmsg);
  101. if (pval != 0)
  102. {
  103. MessageBox.Show(string.Format("打开失败:{0}", pErrmsg.ToString()));
  104. return;
  105. }
  106. int pva2 = ykt_readopencardno_local(Marshal.StringToHGlobalAnsi("3502"), Marshal.StringToHGlobalAnsi("0303"), Marshal.StringToHGlobalAnsi("123"), pCardtype, pCardno, pWarnmsg, pErrmsg);
  107. if (pva2 == 0)
  108. {
  109. MessageBox.Show(string.Format("卡号:{0}", pCardno));
  110. }
  111. else
  112. {
  113. MessageBox.Show("读卡失败:" + pErrmsg.ToString());
  114. }
  115. int pva3 = ykt_closedevice(pWarnmsg, pErrmsg);
  116. if (pva3 != 0)
  117. {
  118. MessageBox.Show("关闭设备失败:" + pErrmsg.ToString());
  119. }
  120. }
  121. string KSessionId = "";
  122. string VSessionId = "";
  123. Dictionary<string, List<string>> msgDictionary = new Dictionary<string, List<string>>();
  124. private void server_NewMessageReceived(WebSocketSession session, string value)
  125. {
  126. log("接收到新的消息:" + value + " 来自:" + session.RemoteEndPoint + " 时间:" + DateTime.Now);
  127. //收集在线session然后发送消息
  128. //var sessions = CommunicationServer.WebSocketServer.GetAllSessions();
  129. //foreach (WebSocketSession session in sessions)
  130. //{
  131. // session.Send(DateTime.Now.ToString(Config.DateTimeFormat) + string.Format(format, arg));
  132. //}
  133. StringBuilder pCardtype = new StringBuilder(2);
  134. StringBuilder pCardno = new StringBuilder(40);
  135. StringBuilder pIssuedeptid = new StringBuilder(24);
  136. StringBuilder pIssuedate = new StringBuilder(8);
  137. StringBuilder pIdtype = new StringBuilder(2);
  138. StringBuilder pId = new StringBuilder(80);
  139. StringBuilder pName = new StringBuilder(40);
  140. StringBuilder pSex = new StringBuilder(1);
  141. StringBuilder pBirthDate = new StringBuilder(20);
  142. StringBuilder pCompany = new StringBuilder(70);
  143. StringBuilder pAddress = new StringBuilder(80);
  144. StringBuilder pTelephone = new StringBuilder(80);
  145. StringBuilder pWarnmsg = new StringBuilder(1024);
  146. StringBuilder pErrmsg = new StringBuilder(1024);
  147. if (value.Equals("readcardno"))
  148. {
  149. try
  150. {
  151. int pval = ykt_opendevice(10, 9, 3, Marshal.StringToHGlobalAnsi("3502"), pWarnmsg, pErrmsg);
  152. if (pval == 0)
  153. {
  154. log("打开成功");
  155. }
  156. else
  157. {
  158. log(string.Format("打开失败:{0}", pErrmsg.ToString()));
  159. }
  160. }
  161. catch (Exception e)
  162. {
  163. log(e.Message);
  164. log(e.StackTrace);
  165. }
  166. try
  167. {
  168. KSessionId = session.SessionID;
  169. int pval = ykt_readopencardinfo_local(
  170. Marshal.StringToHGlobalAnsi("3502"),
  171. Marshal.StringToHGlobalAnsi("0303"),
  172. Marshal.StringToHGlobalAnsi("123"),
  173. pIssuedeptid,
  174. pIssuedate,
  175. pCardtype,
  176. pCardno,
  177. pIdtype,
  178. pId,
  179. pName,
  180. pSex,
  181. pBirthDate,
  182. pCompany,
  183. pAddress,
  184. pTelephone,
  185. pWarnmsg,
  186. pErrmsg);
  187. if (pval == 0)
  188. {
  189. log(string.Format("cardno:{0},name:{1},idcard{2}", pCardno, pName, pId));
  190. SendMsgToRemotePoint(KSessionId, string.Format("cardno:{0},name:{1},idcard{2}", pCardno, pName,pId));
  191. }
  192. else
  193. {
  194. log("ykt_readopencardno_local:" + pErrmsg.ToString());
  195. }
  196. }
  197. catch (Exception e)
  198. {
  199. log(e.Message);
  200. log(e.StackTrace);
  201. }
  202. try
  203. {
  204. int pva3 = ykt_closedevice(pWarnmsg, pErrmsg);
  205. if (pva3 != 0)
  206. {
  207. log("关闭设备失败:" + pErrmsg.ToString());
  208. }
  209. }
  210. catch (Exception e)
  211. {
  212. log(e.Message);
  213. log(e.StackTrace);
  214. }
  215. }
  216. }
  217. /// <summary>
  218. /// 添加会话消息
  219. /// </summary>
  220. /// <param name="value"></param>
  221. private void AddMsgToSessionId(string value)
  222. {
  223. if (value != null)
  224. {
  225. //消息列表包含页面会话ID
  226. if (msgDictionary.ContainsKey(value))
  227. {
  228. msgDictionary[value].Add(value);
  229. }
  230. //消息列表不包含页面会话ID
  231. else
  232. msgDictionary.Add(value, new List<string>() { value });
  233. }
  234. }
  235. /// <summary>
  236. /// 会话关闭
  237. /// </summary>
  238. /// <param name="session"></param>
  239. /// <param name="value"></param>
  240. private void server_SessionClosed(WebSocketSession session, SuperSocket.SocketBase.CloseReason value)
  241. {
  242. log("会话关闭,关闭原因:" + value + " 来自:" + session.RemoteEndPoint + " 时间:" + DateTime.Now);
  243. if (session.SessionID == KSessionId)
  244. SendMsgToRemotePoint(VSessionId, "已断开");
  245. else if (session.SessionID == VSessionId)
  246. SendMsgToRemotePoint(KSessionId, VSessionId + "已断开");
  247. }
  248. /// <summary>
  249. /// 新的会话链接
  250. /// </summary>
  251. /// <param name="session"></param>
  252. private void server_NewSessionConnected(WebSocketSession session)
  253. {
  254. log("新的会话链接 server_NewSessionConnected");
  255. string region = "3502";
  256. StringBuilder pWarnmsg = new StringBuilder(1024);
  257. StringBuilder pErrmsg = new StringBuilder(1024);
  258. try
  259. {
  260. // int pval = ykt_opendevice(10, 9, 3, Marshal.StringToHGlobalAnsi(region), pWarnmsg, pErrmsg);
  261. // if (pval == 0)
  262. // {
  263. // log("打开成功");
  264. // }
  265. // else
  266. // {
  267. //
  268. // log(string.Format("打开失败:{0}", pErrmsg.ToString()));
  269. // }
  270. }
  271. catch (Exception e)
  272. {
  273. log(e.Message);
  274. log(e.StackTrace);
  275. }
  276. log("新的会话连接 来自:" + session.RemoteEndPoint + " SessionID:" + session.SessionID + " 时间:" + DateTime.Now);
  277. //判断是否键存在,如果存在则锁定该键,赋值
  278. if (msgDictionary.ContainsKey(session.SessionID))
  279. msgDictionary[session.SessionID].ForEach(item => session.Send(item));
  280. }
  281. /// <summary>
  282. /// 发送消息到
  283. /// </summary>
  284. /// <param name="sessionId"></param>
  285. /// <param name="msg"></param>
  286. private void SendMsgToRemotePoint(string sessionId, string msg)
  287. {
  288. var allSession = server.GetAppSessionByID(sessionId);
  289. if (allSession != null)
  290. allSession.Send(msg);
  291. }
  292. private void log(string msg)
  293. {
  294. using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"iccard_log.log", true))
  295. {
  296. file.WriteLine(msg);
  297. }
  298. }
  299. private void button1_Click(object sender, EventArgs e)
  300. {
  301. StringBuilder pCardtype = new StringBuilder(4);
  302. StringBuilder pCardno = new StringBuilder(40);
  303. StringBuilder pIssuedeptid = new StringBuilder(24);
  304. StringBuilder pIssuedate = new StringBuilder(8);
  305. StringBuilder pIdtype = new StringBuilder(2);
  306. StringBuilder pId = new StringBuilder(80);
  307. StringBuilder pName = new StringBuilder(20);
  308. StringBuilder pSex = new StringBuilder(2);
  309. StringBuilder pBirthDate = new StringBuilder(20);
  310. StringBuilder pCompany = new StringBuilder(70);
  311. StringBuilder pAddress = new StringBuilder(80);
  312. StringBuilder pTelephone = new StringBuilder(20);
  313. StringBuilder pWarnmsg = new StringBuilder(1024);
  314. StringBuilder pErrmsg = new StringBuilder(1024);
  315. try
  316. {
  317. string region = "3502";
  318. int pval = ykt_opendevice(10, 9, 3, Marshal.StringToHGlobalAnsi(region), pWarnmsg, pErrmsg);
  319. if (pval != 0)
  320. {
  321. MessageBox.Show(string.Format("打开失败:{0}", pErrmsg.ToString()));
  322. return;
  323. }
  324. }
  325. catch (Exception ex) {
  326. MessageBox.Show(string.Format("打开失败:{0}",ex.Message));
  327. }
  328. try
  329. {
  330. int pva2 = ykt_readopencardinfo_local(
  331. Marshal.StringToHGlobalAnsi("3502"),
  332. Marshal.StringToHGlobalAnsi("0303"),
  333. Marshal.StringToHGlobalAnsi("123"),
  334. pIssuedeptid,
  335. pIssuedate,
  336. pCardtype,
  337. pCardno,
  338. pIdtype,
  339. pId,
  340. pName,
  341. pSex,
  342. pBirthDate,
  343. pCompany,
  344. pAddress,
  345. pTelephone,
  346. pWarnmsg,
  347. pErrmsg);
  348. if (pva2 == 0)
  349. {
  350. MessageBox.Show(string.Format("卡号:{0}\n姓名:{1}\n身份证号:{2}\n类型:{3}\n电话{4}", pCardno, pName, pId, pIdtype, pTelephone));
  351. }
  352. else
  353. {
  354. MessageBox.Show("ykt_readopencardno_local:" + pErrmsg.ToString());
  355. }
  356. }
  357. catch (Exception ex)
  358. {
  359. MessageBox.Show(string.Format("打开失败:{0}", ex.Message));
  360. }
  361. try
  362. {
  363. int pva3 = ykt_closedevice(pWarnmsg, pErrmsg);
  364. if (pva3 != 0)
  365. {
  366. MessageBox.Show("关闭设备失败:" + pErrmsg.ToString());
  367. }
  368. }
  369. catch (Exception ex)
  370. {
  371. MessageBox.Show(string.Format("打开失败:{0}", ex.Message));
  372. }
  373. }
  374. private void Form1_Resize(object sender, EventArgs e)
  375. {
  376. if (this.WindowState == FormWindowState.Minimized)
  377. {
  378. this.Visible = false;
  379. notifyIcon1.Visible = true;
  380. }
  381. else {
  382. this.Visible = true;
  383. notifyIcon1.Visible = false;
  384. }
  385. }
  386. private void notifyIcon1_DoubleClick(object sender, EventArgs e)
  387. {
  388. this.Visible = true;
  389. this.WindowState = FormWindowState.Normal;
  390. }
  391. }
  392. }