Form2.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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;
  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 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. Cursor = Cursors.Default;
  52. return;
  53. }
  54. bIsConnected = axCZKEM1.Connect_Net(txtIP.Text, Convert.ToInt32(txtPort.Text));
  55. if (bIsConnected)
  56. {
  57. btnConnect.Text = "断开连接";
  58. btnConnect.Refresh();
  59. lblState.Text = "状态:已连接";
  60. 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.
  61. axCZKEM1.RegEvent(iMachineNumber, 65535);//Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all)
  62. }
  63. else
  64. {
  65. axCZKEM1.GetLastError(ref idwErrorCode);
  66. MessageBox.Show("无法连接设备,ErrorCode=" + idwErrorCode.ToString(), "Error");
  67. }
  68. Cursor = Cursors.Default;
  69. }
  70. private void startListening_Click(object sender, EventArgs e)
  71. {
  72. if (!bIsConnected)
  73. {
  74. MessageBox.Show("请先连接设备", "Error");
  75. return;
  76. }
  77. int idwErrorCode = 0;
  78. //开始注册考勤机的监听事件
  79. if (axCZKEM1.RegEvent(iMachineNumber, 1))//65535表示注册所有的事件
  80. {
  81. //注册事件
  82. this.axCZKEM1.OnAttTransactionEx += new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);
  83. }
  84. else
  85. {
  86. axCZKEM1.GetLastError(ref idwErrorCode);
  87. MessageBox.Show("注册监听事件失败,ErrorCode=" + idwErrorCode.ToString(), "Error");
  88. }
  89. }
  90. /// <summary>
  91. /// 考勤机验证通过
  92. /// </summary>
  93. /// <param name="sEnrollNumber">UserID</param>
  94. /// <param name="iIsInValid">1 为无效记录,0 为有效记录</param>
  95. /// <param name="iAttState">0—Check-In 1—Check-Out 2—Break-Out 3—Break-In 4—OT-In 5—OT-Out </param>
  96. /// <param name="iVerifyMethod">:0 为密码验证,1 为指纹验证,2 为卡验证 </param>
  97. /// <param name="iYear"></param>
  98. /// <param name="iMonth"></param>
  99. /// <param name="iDay"></param>
  100. /// <param name="iHour"></param>
  101. /// <param name="iMinute"></param>
  102. /// <param name="iSecond"></param>
  103. /// <param name="iWorkCode">WorkCode 返回验证时 WorkCode 值,当机器无 Workcode 功能时,该值返回 0</param>
  104. 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)
  105. {
  106. //实现实时获取考勤,取有效记录
  107. if (iIsInValid == 0)
  108. {
  109. //显示
  110. DataTable dt = (DataTable)logInfo.DataSource;
  111. DataRow dr = dt.NewRow();//新建行
  112. dr["UserID"] = sEnrollNumber;
  113. switch (iVerifyMethod)
  114. {
  115. case 0:
  116. dr["VerifyMethod"] = "密码";
  117. break;
  118. case 1:
  119. dr["VerifyMethod"] = "指纹";
  120. break;
  121. case 2:
  122. dr["VerifyMethod"] = "卡";
  123. break;
  124. default:
  125. break;
  126. }
  127. dr["Time"] = iYear + "-" + iMonth + "-" + iDay + " " + iHour + ":" + iMinute + ":" + iSecond;
  128. dr["WorkCode"] = iWorkCode;
  129. logInfo.DataSource = dt;
  130. //更新数据库
  131. dh.ExecuteSql("insert into cardlog (cl_id,cl_cardcode,cl_time) values (cardlog_seq.nextval,'"+sEnrollNumber+"',TO_DATE('"+ dr["Time"] + "','yyyy-MM-dd hh24:mi:ss'))","insert");
  132. }
  133. else
  134. {
  135. //记录验证无效
  136. }
  137. }
  138. }
  139. }