123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- using SuperWebSocket;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Configuration;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace ReadICCardFormsApplication
- {
- public partial class Form1 : Form
- {
- WebSocketServer server;
- public Form1()
- {
- InitializeComponent();
- start();
- }
-
- protected void start()
- {
- var ip = ConfigurationManager.AppSettings["APWebSocketIP"];
- var port = ConfigurationManager.AppSettings["APWebSocketPort"];
- //WebSocket服务器端启动
- server = new WebSocketServer();
- if (!server.Setup(int.Parse(port)))
- {
- //Debug.Write("WebSocket服务器端启动失败");
- //处理启动失败消息
- return;
- }
- //新的会话连接时
- server.NewSessionConnected += server_NewSessionConnected;
- //会话关闭
- server.SessionClosed += server_SessionClosed;
- //接收到新的消息时
- server.NewMessageReceived += server_NewMessageReceived;
- if (!server.Start())
- {
- //Debug.Write(string.Format("开启WebSocket服务侦听失败:{0}:{1}", server.Config.Ip, server.Config.Port));
- //处理监听失败消息
- return;
- }
- }
- [DllImport("fzyktclient.dll", EntryPoint = "ykt_opendevice", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- protected static extern int ykt_opendevice(int nDeviceType, int nCom, int nBaud, IntPtr pRegion, [MarshalAs(UnmanagedType.LPStr)] StringBuilder pWarnmsg, [MarshalAs(UnmanagedType.LPStr)] StringBuilder pErrmsg);
- [DllImport("fzyktclient.dll", EntryPoint = "ykt_readopencardno_local", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- 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);
- [DllImport("fzyktclient.dll", EntryPoint = "ykt_readopencardinfo_local", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- protected static extern int ykt_readopencardinfo_local(IntPtr pRegion, IntPtr pDeptid, IntPtr pDeptKey,
- [MarshalAs(UnmanagedType.LPStr)] StringBuilder pIssuedeptid,
- [MarshalAs(UnmanagedType.LPStr)] StringBuilder pIssuedate,
- [MarshalAs(UnmanagedType.LPStr)] StringBuilder pCardtype,
- [MarshalAs(UnmanagedType.LPStr)] StringBuilder pCardno,
- [MarshalAs(UnmanagedType.LPStr)] StringBuilder pIdtype,
- [MarshalAs(UnmanagedType.LPStr)] StringBuilder pId,
- [MarshalAs(UnmanagedType.LPStr)] StringBuilder pName,
- [MarshalAs(UnmanagedType.LPStr)] StringBuilder pSex,
- [MarshalAs(UnmanagedType.LPStr)] StringBuilder pBirthDate,
- [MarshalAs(UnmanagedType.LPStr)] StringBuilder pCompany,
- [MarshalAs(UnmanagedType.LPStr)] StringBuilder pAddress,
- [MarshalAs(UnmanagedType.LPStr)] StringBuilder pTelephone,
- [MarshalAs(UnmanagedType.LPStr)] StringBuilder pWarnmsg,
- [MarshalAs(UnmanagedType.LPStr)] StringBuilder pErrmsg);
- [DllImport("fzyktclient.dll", EntryPoint = "ykt_closedevice", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- protected static extern int ykt_closedevice([MarshalAs(UnmanagedType.LPStr)] StringBuilder pWarnmsg, [MarshalAs(UnmanagedType.LPStr)] StringBuilder pErrmsg);
-
- private void btOpenDevice_Click(object sender, EventArgs e)
- {
- string region = "3502";
- StringBuilder pWarnmsg = new StringBuilder(1024);
- StringBuilder pErrmsg = new StringBuilder(1024);
- try
- {
- int pval = ykt_opendevice(10, 9, 3, Marshal.StringToHGlobalAnsi(region), pWarnmsg, pErrmsg);
- if (pval == 0)
- {
- MessageBox.Show("打开成功");
- }
- else
- {
- MessageBox.Show(string.Format("打开失败:{0}", pErrmsg.ToString()));
- }
- }
- catch (Exception ex )
- {
- MessageBox.Show(ex.Message);
- MessageBox.Show(ex.StackTrace);
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- StringBuilder pCardtype = new StringBuilder(2);
- StringBuilder pCardno = new StringBuilder(20);
- StringBuilder pWarnmsg = new StringBuilder(1024);
- StringBuilder pErrmsg = new StringBuilder(1024);
- string region = "3502";
- int pval = ykt_opendevice(10, 9, 3, Marshal.StringToHGlobalAnsi(region), pWarnmsg, pErrmsg);
- if (pval != 0)
- {
- MessageBox.Show(string.Format("打开失败:{0}", pErrmsg.ToString()));
- return;
- }
-
- int pva2 = ykt_readopencardno_local(Marshal.StringToHGlobalAnsi("3502"), Marshal.StringToHGlobalAnsi("0303"), Marshal.StringToHGlobalAnsi("123"), pCardtype, pCardno, pWarnmsg, pErrmsg);
- if (pva2 == 0)
- {
- MessageBox.Show(string.Format("卡号:{0}", pCardno));
-
- }
- else
- {
- MessageBox.Show("读卡失败:" + pErrmsg.ToString());
- }
- int pva3 = ykt_closedevice(pWarnmsg, pErrmsg);
- if (pva3 != 0)
- {
- MessageBox.Show("关闭设备失败:" + pErrmsg.ToString());
- }
-
- }
- string KSessionId = "";
- string VSessionId = "";
- Dictionary<string, List<string>> msgDictionary = new Dictionary<string, List<string>>();
- private void server_NewMessageReceived(WebSocketSession session, string value)
- {
- log("接收到新的消息:" + value + " 来自:" + session.RemoteEndPoint + " 时间:" + DateTime.Now);
- //收集在线session然后发送消息
- //var sessions = CommunicationServer.WebSocketServer.GetAllSessions();
- //foreach (WebSocketSession session in sessions)
- //{
- // session.Send(DateTime.Now.ToString(Config.DateTimeFormat) + string.Format(format, arg));
- //}
- StringBuilder pCardtype = new StringBuilder(2);
- StringBuilder pCardno = new StringBuilder(40);
- StringBuilder pIssuedeptid = new StringBuilder(24);
- StringBuilder pIssuedate = new StringBuilder(8);
- StringBuilder pIdtype = new StringBuilder(2);
- StringBuilder pId = new StringBuilder(80);
- StringBuilder pName = new StringBuilder(40);
- StringBuilder pSex = new StringBuilder(1);
- StringBuilder pBirthDate = new StringBuilder(20);
- StringBuilder pCompany = new StringBuilder(70);
- StringBuilder pAddress = new StringBuilder(80);
- StringBuilder pTelephone = new StringBuilder(80);
- StringBuilder pWarnmsg = new StringBuilder(1024);
- StringBuilder pErrmsg = new StringBuilder(1024);
- if (value.Equals("readcardno"))
- {
- try
- {
- int pval = ykt_opendevice(10, 9, 3, Marshal.StringToHGlobalAnsi("3502"), pWarnmsg, pErrmsg);
- if (pval == 0)
- {
- log("打开成功");
- }
- else
- {
- log(string.Format("打开失败:{0}", pErrmsg.ToString()));
- }
- }
- catch (Exception e)
- {
- log(e.Message);
- log(e.StackTrace);
- }
- try
- {
- KSessionId = session.SessionID;
- int pval = ykt_readopencardinfo_local(
- Marshal.StringToHGlobalAnsi("3502"),
- Marshal.StringToHGlobalAnsi("0303"),
- Marshal.StringToHGlobalAnsi("123"),
- pIssuedeptid,
- pIssuedate,
- pCardtype,
- pCardno,
- pIdtype,
- pId,
- pName,
- pSex,
- pBirthDate,
- pCompany,
- pAddress,
- pTelephone,
- pWarnmsg,
- pErrmsg);
- if (pval == 0)
- {
- log(string.Format("cardno:{0},name:{1},idcard{2}", pCardno, pName, pId));
-
- SendMsgToRemotePoint(KSessionId, string.Format("cardno:{0},name:{1},idcard{2}", pCardno, pName,pId));
- }
- else
- {
- log("ykt_readopencardno_local:" + pErrmsg.ToString());
- }
- }
- catch (Exception e)
- {
- log(e.Message);
- log(e.StackTrace);
- }
- try
- {
- int pva3 = ykt_closedevice(pWarnmsg, pErrmsg);
- if (pva3 != 0)
- {
- log("关闭设备失败:" + pErrmsg.ToString());
- }
- }
- catch (Exception e)
- {
- log(e.Message);
- log(e.StackTrace);
- }
- }
- }
- /// <summary>
- /// 添加会话消息
- /// </summary>
- /// <param name="value"></param>
- private void AddMsgToSessionId(string value)
- {
- if (value != null)
- {
- //消息列表包含页面会话ID
- if (msgDictionary.ContainsKey(value))
- {
- msgDictionary[value].Add(value);
- }
- //消息列表不包含页面会话ID
- else
- msgDictionary.Add(value, new List<string>() { value });
- }
- }
- /// <summary>
- /// 会话关闭
- /// </summary>
- /// <param name="session"></param>
- /// <param name="value"></param>
- private void server_SessionClosed(WebSocketSession session, SuperSocket.SocketBase.CloseReason value)
- {
- log("会话关闭,关闭原因:" + value + " 来自:" + session.RemoteEndPoint + " 时间:" + DateTime.Now);
- if (session.SessionID == KSessionId)
- SendMsgToRemotePoint(VSessionId, "已断开");
- else if (session.SessionID == VSessionId)
- SendMsgToRemotePoint(KSessionId, VSessionId + "已断开");
- }
- /// <summary>
- /// 新的会话链接
- /// </summary>
- /// <param name="session"></param>
- private void server_NewSessionConnected(WebSocketSession session)
- {
- log("新的会话链接 server_NewSessionConnected");
- string region = "3502";
- StringBuilder pWarnmsg = new StringBuilder(1024);
- StringBuilder pErrmsg = new StringBuilder(1024);
- try
- {
- // int pval = ykt_opendevice(10, 9, 3, Marshal.StringToHGlobalAnsi(region), pWarnmsg, pErrmsg);
- // if (pval == 0)
- // {
- // log("打开成功");
- // }
- // else
- // {
- //
- // log(string.Format("打开失败:{0}", pErrmsg.ToString()));
- // }
- }
- catch (Exception e)
- {
- log(e.Message);
- log(e.StackTrace);
- }
- log("新的会话连接 来自:" + session.RemoteEndPoint + " SessionID:" + session.SessionID + " 时间:" + DateTime.Now);
- //判断是否键存在,如果存在则锁定该键,赋值
- if (msgDictionary.ContainsKey(session.SessionID))
- msgDictionary[session.SessionID].ForEach(item => session.Send(item));
- }
- /// <summary>
- /// 发送消息到
- /// </summary>
- /// <param name="sessionId"></param>
- /// <param name="msg"></param>
- private void SendMsgToRemotePoint(string sessionId, string msg)
- {
- var allSession = server.GetAppSessionByID(sessionId);
- if (allSession != null)
- allSession.Send(msg);
- }
- private void log(string msg)
- {
- using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"iccard_log.log", true))
- {
- file.WriteLine(msg);
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- StringBuilder pCardtype = new StringBuilder(4);
- StringBuilder pCardno = new StringBuilder(40);
- StringBuilder pIssuedeptid = new StringBuilder(24);
- StringBuilder pIssuedate = new StringBuilder(8);
- StringBuilder pIdtype = new StringBuilder(2);
- StringBuilder pId = new StringBuilder(80);
- StringBuilder pName = new StringBuilder(20);
- StringBuilder pSex = new StringBuilder(2);
- StringBuilder pBirthDate = new StringBuilder(20);
- StringBuilder pCompany = new StringBuilder(70);
- StringBuilder pAddress = new StringBuilder(80);
- StringBuilder pTelephone = new StringBuilder(20);
- StringBuilder pWarnmsg = new StringBuilder(1024);
- StringBuilder pErrmsg = new StringBuilder(1024);
- try
- {
- string region = "3502";
- int pval = ykt_opendevice(10, 9, 3, Marshal.StringToHGlobalAnsi(region), pWarnmsg, pErrmsg);
- if (pval != 0)
- {
- MessageBox.Show(string.Format("打开失败:{0}", pErrmsg.ToString()));
- return;
- }
- }
- catch (Exception ex) {
- MessageBox.Show(string.Format("打开失败:{0}",ex.Message));
- }
- try
- {
- int pva2 = ykt_readopencardinfo_local(
- Marshal.StringToHGlobalAnsi("3502"),
- Marshal.StringToHGlobalAnsi("0303"),
- Marshal.StringToHGlobalAnsi("123"),
- pIssuedeptid,
- pIssuedate,
- pCardtype,
- pCardno,
- pIdtype,
- pId,
- pName,
- pSex,
- pBirthDate,
- pCompany,
- pAddress,
- pTelephone,
- pWarnmsg,
- pErrmsg);
- if (pva2 == 0)
- {
- MessageBox.Show(string.Format("卡号:{0}\n姓名:{1}\n身份证号:{2}\n类型:{3}\n电话{4}", pCardno, pName, pId, pIdtype, pTelephone));
- }
- else
- {
- MessageBox.Show("ykt_readopencardno_local:" + pErrmsg.ToString());
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(string.Format("打开失败:{0}", ex.Message));
- }
- try
- {
- int pva3 = ykt_closedevice(pWarnmsg, pErrmsg);
- if (pva3 != 0)
- {
- MessageBox.Show("关闭设备失败:" + pErrmsg.ToString());
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(string.Format("打开失败:{0}", ex.Message));
- }
- }
- private void Form1_Resize(object sender, EventArgs e)
- {
- if (this.WindowState == FormWindowState.Minimized)
- {
- this.Visible = false;
- notifyIcon1.Visible = true;
- }
- else {
- this.Visible = true;
- notifyIcon1.Visible = false;
- }
- }
- private void notifyIcon1_DoubleClick(object sender, EventArgs e)
- {
- this.Visible = true;
- this.WindowState = FormWindowState.Normal;
- }
- }
- }
|