Form2.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. }
  21. DataHelper dh = new DataHelper();
  22. //Create Standalone SDK class dynamicly.
  23. public zkemkeeper.CZKEMClass axCZKEM1 = new zkemkeeper.CZKEMClass();
  24. private bool bIsConnected = false;//the boolean value identifies whether the device is connected
  25. private int iMachineNumber = 1;//the serial number of the device.After connecting the device ,this value will be changed.
  26. private void Form2_Load(object sender, EventArgs e)
  27. {
  28. DataTable dt = new DataTable();
  29. dt.Columns.Add("UserID");
  30. dt.Columns.Add("AttState");
  31. dt.Columns.Add("VerifyMethod");
  32. dt.Columns.Add("Time");
  33. dt.Columns.Add("WorkCode");
  34. logInfo.DataSource = dt;
  35. }
  36. private void btnConnect_Click(object sender, EventArgs e)
  37. {
  38. if (txtIP.Text.Trim() == "" || txtPort.Text.Trim() == "")
  39. {
  40. MessageBox.Show("请先输入IP和端口", "Error");
  41. return;
  42. }
  43. int idwErrorCode = 0;
  44. Cursor = Cursors.WaitCursor;
  45. if (btnConnect.Text == "断开连接")
  46. {
  47. axCZKEM1.Disconnect();
  48. bIsConnected = false;
  49. btnConnect.Text = "连接";
  50. lblState.Text = "状态:未连接";
  51. startListening.Text = "开始实时事件";
  52. Cursor = Cursors.Default;
  53. return;
  54. }
  55. bIsConnected = axCZKEM1.Connect_Net(txtIP.Text, Convert.ToInt32(txtPort.Text));
  56. if (bIsConnected)
  57. {
  58. btnConnect.Text = "断开连接";
  59. btnConnect.Refresh();
  60. lblState.Text = "状态:已连接";
  61. 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.
  62. axCZKEM1.RegEvent(iMachineNumber, 65535);//Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all)
  63. }
  64. else
  65. {
  66. axCZKEM1.GetLastError(ref idwErrorCode);
  67. MessageBox.Show("无法连接设备,ErrorCode=" + idwErrorCode.ToString(), "Error");
  68. }
  69. Cursor = Cursors.Default;
  70. }
  71. private void startListening_Click(object sender, EventArgs e)
  72. {
  73. if (!bIsConnected)
  74. {
  75. MessageBox.Show("请先连接设备", "Error");
  76. return;
  77. }
  78. if (startListening.Text == "正在实时监听")
  79. {
  80. MessageBox.Show("已启动监听", "Error");
  81. return;
  82. }
  83. int idwErrorCode = 0;
  84. //开始注册考勤机的监听事件
  85. if (axCZKEM1.RegEvent(iMachineNumber, 1))//65535表示注册所有的事件
  86. {
  87. //注册事件
  88. this.axCZKEM1.OnAttTransactionEx += new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);
  89. //注册成功提示用户
  90. startListening.Text = "正在实时监听";
  91. }
  92. else
  93. {
  94. axCZKEM1.GetLastError(ref idwErrorCode);
  95. MessageBox.Show("注册监听事件失败,ErrorCode=" + idwErrorCode.ToString(), "Error");
  96. }
  97. }
  98. /// <summary>
  99. /// 考勤机验证通过
  100. /// </summary>
  101. /// <param name="sEnrollNumber">UserID</param>
  102. /// <param name="iIsInValid">1 为无效记录,0 为有效记录</param>
  103. /// <param name="iAttState">0—Check-In 1—Check-Out 2—Break-Out 3—Break-In 4—OT-In 5—OT-Out </param>
  104. /// <param name="iVerifyMethod">:0 为密码验证,1 为指纹验证,2 为卡验证 </param>
  105. /// <param name="iYear"></param>
  106. /// <param name="iMonth"></param>
  107. /// <param name="iDay"></param>
  108. /// <param name="iHour"></param>
  109. /// <param name="iMinute"></param>
  110. /// <param name="iSecond"></param>
  111. /// <param name="iWorkCode">WorkCode 返回验证时 WorkCode 值,当机器无 Workcode 功能时,该值返回 0</param>
  112. 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)
  113. {
  114. //实现实时获取考勤,取有效记录
  115. if (iIsInValid == 0)
  116. {
  117. //显示
  118. DataTable dt = (DataTable)logInfo.DataSource;
  119. DataRow dr = dt.NewRow();//新建行
  120. dr["UserID"] = sEnrollNumber;
  121. switch (iVerifyMethod)
  122. {
  123. case 0:
  124. dr["VerifyMethod"] = "密码";
  125. break;
  126. case 1:
  127. dr["VerifyMethod"] = "指纹";
  128. break;
  129. case 2:
  130. dr["VerifyMethod"] = "卡";
  131. break;
  132. default:
  133. break;
  134. }
  135. string Time = iYear + "-" + iMonth + "-" + iDay + " " + iHour + ":" + iMinute + ":" + iSecond;
  136. dr["Time"] = Time;
  137. dr["WorkCode"] = iWorkCode;
  138. dt.Rows.Add(dr);
  139. logInfo.DataSource = dt;
  140. //更新数据库
  141. 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");
  142. }
  143. else
  144. {
  145. //记录验证无效
  146. }
  147. }
  148. }
  149. }