欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

中控考勤机接口开发

程序员文章站 2022-04-09 17:09:37
本文参考手册:《TFT 系列脱机通讯开发包开发手册》 主要内容: 1、定时同步考勤记录到数据库 2、用户信息、指纹操作 3、考勤记录批量操作 正文 1、定时同步考勤记录 1-1、定义全局变量 1 #region 窗体变量 2 string USERID = "";//同步到数据库的参数 3 stri ......

本文参考手册:《tft 系列脱机通讯开发包开发手册》

主要内容:

1、定时同步考勤记录到数据库

2、用户信息、指纹操作

3、考勤记录批量操作

 

正文

1、定时同步考勤记录

1-1、定义全局变量

 1         #region 窗体变量
 2         string userid = "";//同步到数据库的参数
 3         string charge_time = "";//同步到数据库的参数
 4         string verifymode = "";//同步到数据库的参数
 5         string workcode = "";//同步到数据库的参数
 6         string password = "";//同步到数据库的参数
 7         string privilege = "";//同步到数据库的参数
 8         string enable = "";//同步到数据库的参数
 9         system.timers.timer atimer = new system.timers.timer(); //定时任务
10         #endregion    
11 
12         #region 考勤机变量
13         public czkemclass axczkem1 = new czkemclass();
14         private bool bisconnected = false;
15         private int imachinenumber = 1;
16 
17         string sdwenrollnumber = "";//用户id
18         string sname = "";//姓名
19         string spassword = "";//密码
20         int iprivilege = 0;
21         bool benabled = false;
22         string scardnumber = "";//卡号
23         int dwworkcode = 0;
24         int dwverifymode;
25         int dwinoutmode;
26         int dwyear;//刷卡时间
27         int dwmonth;
28         int dwday;
29         int dwhour;
30         int dwminute;
31         int dwsecond;
32         string verifymode = "";
33         string privilege = "";
34         string enabled = "";
35         #endregion

1-2、连接考勤机

 1 #region 考勤机功能:连接
 2         private void btnconnect_click(object sender, eventargs e)
 3         {
 4             if (txtip.text.trim() == "" || txtport.text.trim() == "")//考勤机的ip地址与端口
 5             {
 6                 messagebox.show("ip and port cannot be null", "error");
 7                 return;
 8             }
 9             int idwerrorcode = 0;
10 
11             cursor = cursors.waitcursor;
12             if (btnconnect.text == "disconnect")
13             {
14                 axczkem1.disconnect();
15                 bisconnected = false;
16                 btnconnect.text = "connect";
17                 lblstate.text = "current state:disconnected";
18                 cursor = cursors.default;      
19                 return;
20             }
21 
22             bisconnected = axczkem1.connect_net(txtip.text, convert.toint32(txtport.text));
23             if (bisconnected == true)
24             {
25                 btnconnect.text = "disconnect";
26                 btnconnect.refresh();
27                 lblstate.text = "current state:connected";
28                 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.
29                 axczkem1.regevent(imachinenumber, 65535);//here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all)
30             }
31             else
32             {
33                 axczkem1.getlasterror(ref idwerrorcode);
34                 messagebox.show("unable to connect the device,errorcode=" + idwerrorcode.tostring(), "error");
35             }
36             cursor = cursors.default;
37         }
38         #endregion

1-3、配置定时任务

(这里定时任务用到的是system.timers.timer,我在使用过程中遇到一个问题:开启定时任务创建一个线程后调用atimer.stop()无法关闭,只有关闭整个应用程序才会结束这个线程。如果知道怎么解决的朋友还请告知^ ^~)

 1         #region 定时任务配置
 2         public void timing_task()
 3         {
 4             atimer.enabled = true;
 5             atimer.interval = 60000; //执行间隔时间,单位为毫秒; 这里实际间隔为1分钟  
 6             atimer.elapsed += new system.timers.elapsedeventhandler(getlogdata);
 7         }
 8         public void timing_task_star()
 9         {
10             atimer.enabled = true;
11             atimer.start();
12         }
13         public void timing_task_close()
14         {
15             atimer.elapsed -= new system.timers.elapsedeventhandler(getlogdata);
16             atimer.stop();
17             atimer.dispose();
18         }
19         #endregion

1-4、同步程序

 1         #region 执行定时任务(同步数据到mes)
 2         private void getlogdata(object source, elapsedeventargs e)
 3         {
 4             if (bisconnected == false)
 5             {
 6                 messagebox.show("please connect the device first!", "error");
 7                 return;
 8             }
 9             cursor = cursors.waitcursor;
10             //先禁用机器
11             axczkem1.enabledevice(imachinenumber, false);
12             //获取所有用户的考勤记录到缓存区
13             axczkem1.readallglogdata(imachinenumber);
14             while (axczkem1.ssr_getgenerallogdata(imachinenumber, out sdwenrollnumber, out dwverifymode, out dwinoutmode, out dwyear, out dwmonth, out dwday, out dwhour, out dwminute, out dwsecond, ref dwworkcode))//get user information from memory
15             {
16                 switch (dwverifymode)
17                 {
18                     case 0:
19                         verifymode = "密码验证";
20                         break;
21                     case 1:
22                         verifymode = "指纹验证";
23                         break;
24                     case 2:
25                         verifymode = "卡验证";
26                         break;
27                     default:
28                         verifymode = "其他验证";
29                         break;
30                 }
31                 //循环获取用户信息
32                 axczkem1.ssr_getuserinfo(imachinenumber, sdwenrollnumber, out sname, out spassword, out iprivilege, out benabled);
33                 if (axczkem1.getstrcardnumber(out scardnumber))//get the card number from the memory
34                 {
35                     switch (iprivilege)
36                     {
37                         case 0:
38                             privilege = "普通用户";
39                             break;
40                         case 3:
41                             privilege = "管理员";
42                             break;
43                         default:
44                             privilege = "其他权限";
45                             break;
46                     }
47                     switch (benabled)
48                     {
49                         case true:
50                             enabled = "启用";
51                             break;
52                         case false:
53                             enabled = "禁用";
54                             break;
55                     }
56                     userid = sdwenrollnumber;
57                     charge_time = dwyear.tostring() + "-" + dwmonth.tostring() + "-" + dwday.tostring() + " " + dwhour.tostring() + ":" + dwminute.tostring() + ":" + dwsecond.tostring();
58                     verifymode = verifymode;
59                     workcode = dwworkcode.tostring();
60                     //name = sname+ "\0";
61                     //name = name.replace("\0", "");
62                     password = spassword;
63                     privilege = privilege;
64                     enable = enabled;
65                     string id = dwyear.tostring() + dwmonth.tostring() + dwday.tostring() + dwhour.tostring() + dwminute.tostring() + dwsecond.tostring();
66                     datetime now = datetime.now;
67                     string thisyear = now.year.tostring();
68                     string thismonth = now.month.tostring();
69                     string thisday = now.day.tostring();
70                     if (dwyear.tostring() == thisyear && dwmonth.tostring() == thismonth && dwday.tostring() == thisday)
71                     {
72                         //取时间为今天的刷卡记录,若id不存在则插入
73                         //insert when not exists (select t.id from table t where t.t_id = '??') then into table(...) values (...)
74                     }
75                 }
76             }
77             axczkem1.enabledevice(imachinenumber, true);//enable the device
78             cursor = cursors.default;
79 
80             checkding();//检测是否超时
81         }
82         #endregion

 

2、用户信息/指纹

2-1、获取考勤机内已注册用户到list

(注意:如果考勤机内没有注册指纹信息,代码里面有查询指纹,那么加载会变得非常慢。要提前注释掉查询指纹的代码!)

 1         #region 考勤机功能:获取用户
 2         private void btngetstrcardnumber_click(object sender, eventargs e)
 3         {
 4             if (bisconnected == false)
 5             {
 6                 messagebox.show("please connect the device first!", "error");
 7                 return;
 8             }
 9             lvcard.items.clear();
10             lvcard.beginupdate();
11             cursor = cursors.waitcursor;
12             //先禁用机器
13             axczkem1.enabledevice(imachinenumber, false);//disable the device
14             //获取所有用户的记录到缓存区
15             axczkem1.readalluserid(imachinenumber);//获取所有信息到缓存区(提高效率)
16             axczkem1.readalltemplate(imachinenumber);//获取所有指纹信息到缓存区(提高效率)
17             while (axczkem1.ssr_getalluserinfo(imachinenumber, out sdwenrollnumber, out sname, out spassword, out iprivilege, out benabled))//get user information from memory
18             {
19                 //循环获取用户信息
20                 if (axczkem1.getstrcardnumber(out scardnumber))//get the card number from the memory
21                 {
22                     listviewitem list = new listviewitem();
23                     list.text = sdwenrollnumber;
24                     list.subitems.add(sname);
25                     list.subitems.add(scardnumber);
26                     list.subitems.add(iprivilege.tostring());
27                     list.subitems.add(spassword);
28                     if (benabled == true)
29                     {
30                         list.subitems.add("true");
31                     }
32                     else
33                     {
34                         list.subitems.add("false");
35                     }
36 
37                     for (int dwfingerindex = 0; dwfingerindex < 10; dwfingerindex++)
38                     {
39                         if (axczkem1.ssr_getusertmpstr(imachinenumber, sdwenrollnumber, dwfingerindex, out dwtmpdata, out dwtmplength))
40                         {
41                             list.subitems.add(dwtmpdata);
42                         }
43                         else
44                         {
45                             list.subitems.add("");
46                         }
47                     }
48 
49                     lvcard.items.add(list);
50                 }
51             }
52             
53             axczkem1.enabledevice(imachinenumber, true);//enable the device
54             lvcard.endupdate();
55             cursor = cursors.default;
56         }
57         #endregion

2-2、注册单个用户到考勤机

 1         #region 考勤机功能:注册用户
 2         private void btnsetstrcardnumber_click(object sender, eventargs e)
 3         {
 4             if (bisconnected == false)
 5             {
 6                 messagebox.show("please connect the device first!", "error");
 7                 return;
 8             }
 9             //下发用户到机器        
10             //1、设置卡号
11             //string cardno = system.convert.toint32(txtcardno.text, 16).tostring();//转换16进制到十进制
12             axczkem1.setstrcardnumber(txtcardno.text);
13             //2、设置用户信息
14             axczkem1.ssr_setuserinfo(imachinenumber, "用户id", "用户姓名", "密码", 0, true);
15         }
16         #endregion

2-3、批量注册用户

 1         #region 考勤机功能:批量注册用户
 2         private void btnsetstrcardnumbers_click(object sender, eventargs e)
 3         {
 4             if (xcmessagebox.show("确定?", xcmessagebox.buttons.okcancel, xcmessagebox.icon.question) == dialogresult.ok)
 5             {
 6                 system.data.datatable dt = getemp();//getemp()获取数据库中需要注册的用户
 7                 string[] useruid = new string[dt.rows.count];
 8                 string[] useruname = new string[dt.rows.count];
 9                 string[] cardid = new string[dt.rows.count];
10                 for (int i = 0; i < dt.rows.count; i++)
11                 {
12                     datarow dr = dt.rows[i];
13                     useruid[i] = convert.tostring(dr["t_userid"]);
14                     useruname[i] = convert.tostring(dr["t_username"]);
15                     cardid[i] = convert.tostring(dr["cardid"]);
16                     //16进制转10进制
17                     //string cardno = system.convert.toint32(cardid[i], 16).tostring();
18                     axczkem1.setstrcardnumber(cardid[i]);
19                     //2、设置用户信息
20                     bool a = axczkem1.ssr_setuserinfo(imachinenumber, useruid[i], useruname[i], useruid[i], 0, true);
21                 }
22             }  
23         }
24         #endregion

2-4、删除用户

 1         #region 考勤机功能:删除用户
 2         private void btndelstrcardnumber_click(object sender, eventargs e)
 3         {
 4             if (bisconnected == false)
 5             {
 6                 messagebox.show("please connect the device first!", "error");
 7                 return;
 8             }
 9             if (xcmessagebox.show("确定?", xcmessagebox.buttons.okcancel, xcmessagebox.icon.question) == dialogresult.ok)
10             {
11                 axczkem1.ssr_deleteenrolldata(imachinenumber, "用户id", 12);
12             }
13         }
14         #endregion

2-5、增删指纹

(这里有一点要注意:考勤机算法不同的话,增加指纹所调用的方法不同,使用前先查看考勤机算法)

 1         #region 考勤机功能:增删指纹
 2         private void btndeltmp_click(object sender, eventargs e)
 3         {
 4             if (bisconnected == false)
 5             {
 6                 messagebox.show("please connect the device first!", "error");
 7                 return;
 8             }
 9             if (xcmessagebox.show("确定?", xcmessagebox.buttons.okcancel, xcmessagebox.icon.question) == dialogresult.ok)
10             {
11                 bool a = axczkem1.ssr_delusertmp(imachinenumber, "用户id", "(int)第几个指纹");
12                 //bool a = axczkem1.ssr_deleteenrolldataext(imachinenumber, txtempno.text, 13);//删除所有指纹
13             }
14         }
15 
16         private void btnaddtmp_click(object sender, eventargs e)
17         {
18             if (bisconnected == false)
19             {
20                 messagebox.show("please connect the device first!", "error");
21                 return;
22             }
23             system.data.datatable dt = new system.data.datatable();
24             for (int i = 0; i < 10; i++)
25             {
26                 
27                 //循环获取十个指纹
28                 dt = getemptmp(i.tostring());//获取数据库中第i个指纹
29                 for (int j = 0; j < dt.rows.count; j++)
30                 {
31                     
32                     //循环数据库中的用户数
33                     userid = dt.rows[j]["数据库中用户id"].tostring();
34                     dwtmpdata = dt.rows[j]["数据库中第i个指纹"].tostring();
35                     bool a = axczkem1.setusertmpexstr(imachinenumber, userid, i, 1, dwtmpdata);
36                 }    
37             }
38 
39             //获取算法版本
40             /*string svalue;
41             axczkem1.getsysoption(1, "~zkfpversion", out svalue);
42             txtinfo.text = svalue;*/
43         }
44         #endregion

2-6、清空考勤机所有注册的用户

 1         #region 考勤机功能:清空考勤机用户
 2         private void btndel_click(object sender, eventargs e)
 3         {
 4             if (bisconnected == false)
 5             {
 6                 messagebox.show("please connect the device first!", "error");
 7                 return;
 8             }
 9             if (xcmessagebox.show("确定?", xcmessagebox.buttons.okcancel, xcmessagebox.icon.question) == dialogresult.ok)
10             {
11                 cursor = cursors.waitcursor;
12                 //先禁用机器
13                 axczkem1.enabledevice(imachinenumber, false);//disable the device
14                                                              //获取所有用户的记录到缓存区
15                 axczkem1.readalluserid(imachinenumber);//获取所有信息到缓存区(提高效率)
16                 while (axczkem1.ssr_getalluserinfo(imachinenumber, out sdwenrollnumber, out sname, out spassword, out iprivilege, out benabled))//get user information from memory
17                 {
18                     axczkem1.ssr_deleteenrolldataext(imachinenumber, sdwenrollnumber, 12);
19                 }
20 
21                 axczkem1.enabledevice(imachinenumber, true);//enable the device
22                 cursor = cursors.default;
23             }
24         }
25         #endregion

 

3、考勤记录批量操作

3-1、清空考勤机所有刷卡记录

 1         #region 考勤机功能:清空考勤记录
 2         private void btndellogdata_click(object sender, eventargs e)
 3         {
 4             if (bisconnected == false)
 5             {
 6                 messagebox.show("please connect the device first!", "error");
 7                 return;
 8             }
 9             if (xcmessagebox.show("确定?", xcmessagebox.buttons.okcancel, xcmessagebox.icon.question) == dialogresult.ok)
10             {
11                 axczkem1.clearglog(imachinenumber);
12             }
13         }
14         #endregion

3-2、同步考勤机所有刷卡记录到数据库

 1         #region 考勤机功能:同步所有考勤机数据到数据库
 2         private void btngetalldata_click(object sender, eventargs e)
 3         {
 4             if (bisconnected == false)
 5             {
 6                 messagebox.show("please connect the device first!", "error");
 7                 return;
 8             }
 9             if (xcmessagebox.show("确定?", xcmessagebox.buttons.okcancel, xcmessagebox.icon.question) == dialogresult.ok)
10             {
11                 cursor = cursors.waitcursor;
12                 //先禁用机器
13                 axczkem1.enabledevice(imachinenumber, false);
14                 //获取所有用户的考勤记录到缓存区
15                 axczkem1.readallglogdata(imachinenumber);
16                 while (axczkem1.ssr_getgenerallogdata(imachinenumber, out sdwenrollnumber, out dwverifymode, out dwinoutmode, out dwyear, out dwmonth, out dwday, out dwhour, out dwminute, out dwsecond, ref dwworkcode))//get user information from memory
17                 {
18                     switch (dwverifymode)
19                     {
20                         case 0:
21                             verifymode = "密码验证";
22                             break;
23                         case 1:
24                             verifymode = "指纹验证";
25                             break;
26                         case 2:
27                             verifymode = "卡验证";
28                             break;
29                         default:
30                             verifymode = "其他验证";
31                             break;
32                     }
33                     //循环获取用户信息
34                     axczkem1.ssr_getuserinfo(imachinenumber, sdwenrollnumber, out sname, out spassword, out iprivilege, out benabled);
35                     if (axczkem1.getstrcardnumber(out scardnumber))//get the card number from the memory
36                     {
37                         switch (iprivilege)
38                         {
39                             case 0:
40                                 privilege = "普通用户";
41                                 break;
42                             case 3:
43                                 privilege = "管理员";
44                                 break;
45                             default:
46                                 privilege = "其他权限";
47                                 break;
48                         }
49                         switch (benabled)
50                         {
51                             case true:
52                                 enabled = "启用";
53                                 break;
54                             case false:
55                                 enabled = "禁用";
56                                 break;
57                         }
58                         userid = sdwenrollnumber;
59                         charge_time = dwyear.tostring() + "-" + dwmonth.tostring() + "-" + dwday.tostring() + " " + dwhour.tostring() + ":" + dwminute.tostring() + ":" + dwsecond.tostring();
60                         verifymode = verifymode;
61                         workcode = dwworkcode.tostring();
62                         //name = sname + "\0";
63                         //name = name.replace("\0", "");
64                         password = spassword;
65                         privilege = privilege;
66                         enable = enabled;
67                         string id = dwyear.tostring() + dwmonth.tostring() + dwday.tostring() + dwhour.tostring() + dwminute.tostring() + dwsecond.tostring();
68                         addrecords(userid, charge_time, verifymode, workcode, password, privilege, enable, id);//插入到数据库表
69                     }
70                 }
71                 axczkem1.enabledevice(imachinenumber, true);//enable the device
72                 cursor = cursors.default;
73             }
74         }
75         #endregion