Form2.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using Check.DataOperate;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace UAS_CheckWork
  14. {
  15. public partial class Form2 : Form
  16. {
  17. public Form2()
  18. {
  19. InitializeComponent();
  20. //new Thread(() =>
  21. //{
  22. // while (true)
  23. // {
  24. // try { label1.BeginInvoke(new MethodInvoker(() => label1.Text = DateTime.Now.ToString())); }
  25. // catch { }
  26. // Thread.Sleep(1000);
  27. // }
  28. //})
  29. //{ IsBackground = true }.Start();
  30. }
  31. DataHelper dh = new DataHelper();
  32. //Create Standalone SDK class dynamicly.
  33. public zkemkeeper.CZKEMClass axCZKEM1 = new zkemkeeper.CZKEMClass();
  34. private bool bIsConnected = false;//the boolean value identifies whether the device is connected
  35. private int iMachineNumber = 1;//the serial number of the device.After connecting the device ,this value will be changed.
  36. private void Form2_Load(object sender, EventArgs e)
  37. {
  38. DataTable dt = new DataTable();
  39. dt.Columns.Add("UserID");
  40. dt.Columns.Add("AttState");
  41. dt.Columns.Add("VerifyMethod");
  42. dt.Columns.Add("Time");
  43. dt.Columns.Add("WorkCode");
  44. logInfo.DataSource = dt;
  45. }
  46. private void btnConnect_Click(object sender, EventArgs e)
  47. {
  48. if (txtIP.Text.Trim() == "" || txtPort.Text.Trim() == "")
  49. {
  50. MessageBox.Show("请先输入IP和端口", "Error");
  51. return;
  52. }
  53. int idwErrorCode = 0;
  54. Cursor = Cursors.WaitCursor;
  55. if (btnConnect.Text == "断开连接")
  56. {
  57. axCZKEM1.Disconnect();
  58. bIsConnected = false;
  59. btnConnect.Text = "连接";
  60. lblState.Text = "状态:未连接";
  61. startListening.Text = "开始实时事件";
  62. Cursor = Cursors.Default;
  63. return;
  64. }
  65. bIsConnected = axCZKEM1.Connect_Net(txtIP.Text, Convert.ToInt32(txtPort.Text));
  66. if (bIsConnected)
  67. {
  68. btnConnect.Text = "断开连接";
  69. btnConnect.Refresh();
  70. lblState.Text = "状态:已连接";
  71. iMachineNumber = 1;//In fact,when you are using the tcp/ip communication,this parameter will be ignored,that is any integer will all right.Here we use 1.
  72. axCZKEM1.RegEvent(iMachineNumber, 65535);//Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all)
  73. }
  74. else
  75. {
  76. axCZKEM1.GetLastError(ref idwErrorCode);
  77. MessageBox.Show("无法连接设备,ErrorCode=" + idwErrorCode.ToString(), "Error");
  78. }
  79. Cursor = Cursors.Default;
  80. }
  81. private void startListening_Click(object sender, EventArgs e)
  82. {
  83. if (!bIsConnected)
  84. {
  85. MessageBox.Show("请先连接设备", "Error");
  86. return;
  87. }
  88. if (startListening.Text == "正在实时监听")
  89. {
  90. MessageBox.Show("已启动监听", "Error");
  91. return;
  92. }
  93. int idwErrorCode = 0;
  94. //开始注册考勤机的监听事件
  95. if (axCZKEM1.RegEvent(iMachineNumber, 1))//65535表示注册所有的事件
  96. {
  97. //注册事件
  98. this.axCZKEM1.OnAttTransactionEx += new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);
  99. //注册成功提示用户
  100. startListening.Text = "正在实时监听";
  101. }
  102. else
  103. {
  104. axCZKEM1.GetLastError(ref idwErrorCode);
  105. MessageBox.Show("注册监听事件失败,ErrorCode=" + idwErrorCode.ToString(), "Error");
  106. }
  107. }
  108. /// <summary>
  109. /// 考勤机验证通过
  110. /// </summary>
  111. /// <param name="sEnrollNumber">UserID</param>
  112. /// <param name="iIsInValid">1 为无效记录,0 为有效记录</param>
  113. /// <param name="iAttState">0—Check-In 1—Check-Out 2—Break-Out 3—Break-In 4—OT-In 5—OT-Out </param>
  114. /// <param name="iVerifyMethod">:0 为密码验证,1 为指纹验证,2 为卡验证 </param>
  115. /// <param name="iYear"></param>
  116. /// <param name="iMonth"></param>
  117. /// <param name="iDay"></param>
  118. /// <param name="iHour"></param>
  119. /// <param name="iMinute"></param>
  120. /// <param name="iSecond"></param>
  121. /// <param name="iWorkCode">WorkCode 返回验证时 WorkCode 值,当机器无 Workcode 功能时,该值返回 0</param>
  122. private void axCZKEM1_OnAttTransactionEx(string sEnrollNumber, int iIsInValid, int iAttState, int iVerifyMethod, int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond, int iWorkCode)
  123. {
  124. //实现实时获取考勤,取有效记录
  125. if (iIsInValid == 0)
  126. {
  127. //显示
  128. DataTable dt = (DataTable)logInfo.DataSource;
  129. DataRow dr = dt.NewRow();//新建行
  130. dr["UserID"] = sEnrollNumber;
  131. switch (iVerifyMethod)
  132. {
  133. case 0:
  134. dr["VerifyMethod"] = "密码";
  135. break;
  136. case 1:
  137. dr["VerifyMethod"] = "指纹";
  138. break;
  139. case 2:
  140. dr["VerifyMethod"] = "卡";
  141. break;
  142. default:
  143. break;
  144. }
  145. string Time = iYear + "-" + iMonth + "-" + iDay + " " + iHour + ":" + iMinute + ":" + iSecond;
  146. dr["Time"] = Time;
  147. dr["WorkCode"] = iWorkCode;
  148. dt.Rows.Add(dr);
  149. logInfo.DataSource = dt;
  150. //更新数据库
  151. dh.ExecuteSql("insert into cardlog (cl_id,cl_cardcode,cl_time,cl_address) values (cardlog_seq.nextval,'" + sEnrollNumber + "',TO_DATE('" + Time + "','yyyy-MM-dd hh24:mi:ss'),'考勤机数据导入')", "insert");
  152. }
  153. else
  154. {
  155. //记录验证无效
  156. }
  157. }
  158. }
  159. }