private void connect(zkemkeeper.CZKEMClass axCZKEM1, string iface_Ip, int port, int retry) { bool bIsConnected = axCZKEM1.Connect_Net(iface_Ip, port); string deviceName = iface_Ip + "_" + port; if (bIsConnected == true) { LogHelper.Log(LogLevel.Debug, "Connected " + deviceName + " successed by retrying " + retry + " times."); 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. bool regEvent = axCZKEM1.RegEvent(iMachineNumber, 65535); //Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all) if (regEvent == true) { LogHelper.Log(LogLevel.Debug, deviceName + " regEvent value: " + regEvent); axCZKEM1.OnAttTransactionEx += new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx); } else { LogHelper.Log(LogLevel.Debug, deviceName + " regEvent failed, disconnecting device..."); axCZKEM1.Disconnect(); } } else { axCZKEM1.GetLastError(ref idwErrorCode); axCZKEM1.OnAttTransactionEx -= new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx); LogHelper.Log(LogLevel.Debug, "Unable to connect " + deviceName + " by retrying " + retry + " times. ErrorCode=" + idwErrorCode.ToString() + ", connect failed."); } }
public async Task <object> Invoke(string txtIP, string txtPort) { zkemkeeper.CZKEMClass axCZKEM1 = new zkemkeeper.CZKEMClass(); int idwErrorCode = 0; string mseeage = ""; bool bIsConnected = false; //the boolean value identifies whether the device is connected int iMachineNumber = 1; //the serial number of the device.After connecting the device ,this value will be changed. bIsConnected = axCZKEM1.Connect_Net(txtIP, Convert.ToInt32(txtPort)); if (bIsConnected == true) { 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. axCZKEM1.RegEvent(iMachineNumber, 65535); //Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all) mseeage = "1"; } else { axCZKEM1.GetLastError(ref idwErrorCode); mseeage = "Unable to connect the device,ErrorCode=" + idwErrorCode.ToString(); } // 方法体 return(mseeage); }
//GetSerialNumber of Device public static string GetDeviceSN(int iMachineNumber, ref int iError) { var z = new zkemkeeper.CZKEMClass(); int iTempError = 0; string sSN = string.Empty; if (z.Connect_USB(iMachineNumber)) { string sTempSerialNumber; if (z.GetSerialNumber(iMachineNumber, out sTempSerialNumber)) { sSN = sTempSerialNumber; } else { z.GetLastError(ref iTempError); } } else { sSN = string.Empty; } z.Disconnect(); return(sSN); }
public clsZkDoor() { if (axCZKEM1 == null) { axCZKEM1 = new zkemkeeper.CZKEMClass(); } }
/// <summary> /// 从考勤机读取面部识别 /// </summary> /// <param name="userNo"></param> /// <param name="device"></param> /// <returns></returns> private string getUserFaceFromDevice(string userNo, string device, zkemkeeper.CZKEMClass axCZKEM1) { string[] ds = device.Split(new char[] { ',' }); //bool bIsConnected = axCZKEM1.Connect_Net(ds[0], Convert.ToInt32(ds[1])); int idwErrorCode = 0; int iTmpLength = 0; int iFaceIndex = 50;//the only possible parameter value string face = ""; //addLog(userNo, false); if (axCZKEM1.GetUserFaceStr(iMachineNumber, userNo, iFaceIndex, ref face, ref iTmpLength)) { return(face); } else { axCZKEM1.GetLastError(ref idwErrorCode); if (idwErrorCode != 0) { if (idwErrorCode != -4993) { addLog("从考勤机读取用户" + userNo + "的面部数据异常,错误代码: " + idwErrorCode.ToString(), Color.Black); } } else { addLog("考勤机中用户" + userNo + "的面部数据数据!" + device, Color.Black); } } //axCZKEM1.Disconnect(); return(face); }
public bool connect(string ipAddress, string port) { this.ipAddress = ipAddress; this.port = Convert.ToInt16(port); // Connection procedure ATTENDANCE = new zkemkeeper.CZKEMClass(); connected = ATTENDANCE.Connect_Net(ipAddress, Convert.ToInt32(port)); if (connected) { ATTENDANCE.RegEvent(iMachineNumber, 65535);//Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all) } return(connected); // This particular part may spend a lot of time... }
/// <summary> /// 清除 控制器类 及所控制的门 /// </summary> public void Clear() { if (axCZKEM1 != null) { if (blConnoct) { DisConnect(); } axCZKEM1 = null; } if (doorInfo != null) { doorInfo = null; } }
/// <summary> /// 从考勤机读取用户列表 /// </summary> /// <param name="device"></param> /// <returns></returns> private void readUserListFromDevice(string device, zkemkeeper.CZKEMClass axCZKEM1) { int idwErrorCode = 0; string sdwEnrollNumber = ""; string sdwName = ""; string sdwPassword = ""; int sdwPrivilege = 0; bool sdwEnabled = false; if (axCZKEM1.ReadAllUserID(iMachineNumber)) { while (axCZKEM1.SSR_GetAllUserInfo(iMachineNumber, out sdwEnrollNumber, out sdwName, out sdwPassword, out sdwPrivilege, out sdwEnabled)) { AttendanceUser user = new AttendanceUser(); user.userNo = sdwEnrollNumber; string name = sdwName; int index = name.IndexOf('\0'); if (index > 0) { name = name.Substring(0, index); } user.name = name; user.password = sdwPassword; user.manager = sdwPrivilege == Config.Privilege_Administrator; allUserList.Add(user); } } else { axCZKEM1.GetLastError(ref idwErrorCode); if (idwErrorCode != 0) { addLog("从考勤机读取用户列表异常,错误代码: " + idwErrorCode.ToString(), Color.Black); } else { addLog("考勤机中没有用户数据!" + device, Color.Black); } } }
public static int TestConnection(String IP, String port) { bool isConnected = false; zkemkeeper.CZKEMClass axCZKEM1 = new zkemkeeper.CZKEMClass(); int idwErrorCode = 0; try { isConnected = axCZKEM1.Connect_Net(IP.Trim(), Convert.ToInt32(port.Trim())); if (!isConnected) { axCZKEM1.GetLastError(ref idwErrorCode); } } catch (Exception ex) { axCZKEM1.GetLastError(ref idwErrorCode); } if (!isConnected && idwErrorCode == 0) { return(-1); } return(idwErrorCode); }
public ZKReader() { czkem = new zkemkeeper.CZKEMClass(); }
public static List <Attendance> getAttendanceList(zkemkeeper.CZKEMClass axCZKEM1, string ip, int port, string deviceSN) { //Console.WriteLine("aaaaa"); int idwErrorCode = 0; List <Attendance> list = new List <Attendance>(); string sdwEnrollNumber = ""; int idwVerifyMode = 0; int idwInOutMode = 0; int idwYear = 0; int idwMonth = 0; int idwDay = 0; int idwHour = 0; int idwMinute = 0; int idwSecond = 0; int idwWorkcode = 0; int iMachineNumber = 1; bool bIsConnected = axCZKEM1.Connect_Net(ip, port); //axCZKEM1.EnableDevice(iMachineNumber, false);//disable the device if (axCZKEM1.ReadGeneralLogData(iMachineNumber))//read all the attendance records to the memory { while (axCZKEM1.SSR_GetGeneralLogData(iMachineNumber, out sdwEnrollNumber, out idwVerifyMode, out idwInOutMode, out idwYear, out idwMonth, out idwDay, out idwHour, out idwMinute, out idwSecond, ref idwWorkcode))//get records from the memory { Attendance att = new Attendance(); att.userNo = sdwEnrollNumber; att.deviceSerialNo = deviceSN; if (idwVerifyMode == 1) { att.attendanceMode = "FINGERPRINT"; } else if (idwVerifyMode == 15) { att.attendanceMode = "FACE"; } else if (idwVerifyMode == 0) { att.attendanceMode = "PASSWORD"; } else { att.attendanceMode = "PASSWORD"; } string month = idwMonth.ToString(); if (month.Length == 1) { month = "0" + month; } string day = idwDay.ToString(); if (day.Length == 1) { day = "0" + day; } string date = idwYear.ToString() + "-" + month + "-" + day + " " + idwHour.ToString() + ":" + idwMinute.ToString() + ":00"; att.createDate = date; list.Add(att); } } else { axCZKEM1.GetLastError(ref idwErrorCode); if (idwErrorCode != 0) { axCZKEM1.Disconnect(); throw new Exception("[" + ip + "]读取考勤数据发生错误,错误代码: " + idwErrorCode.ToString()); } } axCZKEM1.Disconnect(); return(list); }
private void readAttData(zkemkeeper.CZKEMClass axCZKEM1, string deviceName) { //axCZKEM1.EnableDevice(iMachineNumber, false);//disable the device LogHelper.Log(LogLevel.Debug, "begin to check attendence records for " + deviceName); int count = 0; int repeat = 0; int total = 0; long start_time = DateTime.Now.Ticks / 10000; if (axCZKEM1.ReadGeneralLogData(iMachineNumber))//read all the attendance records to the memory { long end_time = DateTime.Now.Ticks / 10000; LogHelper.Log(LogLevel.Debug, "read attendence records for " + deviceName + " cost time: " + (end_time - start_time)); string sdwEnrollNumber = ""; int idwVerifyMode = 0; int idwInOutMode = 0; int idwYear = 0; int idwMonth = 0; int idwDay = 0; int idwHour = 0; int idwMinute = 0; int idwSecond = 0; int idwWorkcode = 0; HashSet <string> set = dictionary[deviceName]; string time_3days_ago_str = DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd") + " 00:00:00"; DateTime time_3days_ago = Convert.ToDateTime(time_3days_ago_str); while (axCZKEM1.SSR_GetGeneralLogData(iMachineNumber, out sdwEnrollNumber, out idwVerifyMode, out idwInOutMode, out idwYear, out idwMonth, out idwDay, out idwHour, out idwMinute, out idwSecond, ref idwWorkcode))//get records from the memory { total++; string time = idwYear.ToString() + "-" + (idwMonth < 10 ? "0" : "") + idwMonth.ToString() + "-" + (idwDay < 10 ? "0" : "") + idwDay.ToString() + " " + (idwHour < 10 ? "0" : "") + idwHour.ToString() + ":" + (idwMinute < 10 ? "0" : "") + idwMinute.ToString() + ":" + (idwSecond < 10 ? "0" : "") + idwSecond.ToString(); DateTime recordTime = Convert.ToDateTime(time); string record = sdwEnrollNumber + "@" + time; if (recordTime < time_3days_ago) { if (set.Contains(record)) { set.Remove(record); } continue; } if (set.Contains(record)) { repeat++; continue; } else { set.Add(record); count++; LogHelper.Log(LogLevel.Debug, "Teacher " + sdwEnrollNumber + " attendance @" + time + " by " + deviceName + " from check."); SaveAttData(new IFaceAttendance(sdwEnrollNumber, 0, idwInOutMode, idwVerifyMode, idwWorkcode, recordTime, deviceName)); } } LogHelper.Log(LogLevel.Debug, count + " records checked, " + repeat + " repeat, total " + total + " for " + deviceName); if (total == 0) { axCZKEM1.OnAttTransactionEx -= new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx); axCZKEM1.Disconnect(); LogHelper.Log(LogLevel.Debug, "total 0 records checked for " + deviceName + ", disconnect the device and retry."); } } else { long end_time = DateTime.Now.Ticks / 10000; axCZKEM1.OnAttTransactionEx -= new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx); axCZKEM1.Disconnect(); LogHelper.Log(LogLevel.Debug, "failed to checke records from " + deviceName + " cost time: " + (end_time - start_time) + ", disconnect the device and retry."); } //axCZKEM1.EnableDevice(iMachineNumber, true);//enable the device }
public void ConnectDevice(string iface_Ip, int port, int commKey) { string deviceName = iface_Ip + "_" + port; if (!dictionary.ContainsKey(deviceName)) { HashSet <string> set = new HashSet <string>(); dictionary.Add(deviceName, set); } Thread createComAndMessagePumpThread = new Thread(() => { int machineNumber = 1; LogHelper.Log(LogLevel.Debug, "connectting to device:" + deviceName); zkemkeeper.CZKEMClass axCZKEM1 = new zkemkeeper.CZKEMClass(); if (commKey != 0) { axCZKEM1.SetCommPassword(commKey); } connect(axCZKEM1, iface_Ip, port, 0); System.Timers.Timer timer = new System.Timers.Timer { Interval = int.Parse(IFaceCheckInterval), Enabled = true }; timer.Elapsed += new System.Timers.ElapsedEventHandler((object sender, System.Timers.ElapsedEventArgs e) => { DateTime now = DateTime.Now; // compare time foreach (string[] time_pair in checkTimes) { if (now >= Convert.ToDateTime(time_pair[0]) && now <= Convert.ToDateTime(time_pair[1])) { string IPAddr = ""; int retry_times = 1; while (!axCZKEM1.GetDeviceIP(machineNumber, IPAddr)) { axCZKEM1.GetLastError(ref idwErrorCode); if (retry_times > 2) // only retry 2 times { retry_times--; LogHelper.Log(LogLevel.Debug, "Connecting to " + deviceName + " failed " + retry_times + " times, ErrorCode=" + idwErrorCode.ToString() + ", stop connecting."); return; } LogHelper.Log(LogLevel.Debug, "Connecting to " + deviceName + " failed, ErrorCode=" + idwErrorCode.ToString() + ", reConnecting: " + retry_times); connect(axCZKEM1, iface_Ip, port, retry_times); retry_times++; } LogHelper.Log(LogLevel.Debug, "device " + deviceName + " connected status is ok."); readAttData(axCZKEM1, iface_Ip + "_" + port); } else { LogHelper.Log(LogLevel.Debug, "device " + deviceName + " time not in " + time_pair[0] + "--" + time_pair[1] + ", continued."); continue; } } }); Application.Run(); }); createComAndMessagePumpThread.SetApartmentState(ApartmentState.STA); createComAndMessagePumpThread.Name = deviceName; createComAndMessagePumpThread.IsBackground = true; createComAndMessagePumpThread.Start(); LogHelper.Log(LogLevel.Debug, deviceName + ": Thread Started."); }
static void Main(string[] args) { Console.WriteLine("console app send absen is running don't close this app \n"); Console.WriteLine("this app sleep every 45 seconds \n"); zkemkeeper.CZKEMClass axCZKEM1 = new zkemkeeper.CZKEMClass(); kembali: FetchApi sendAbsen = new FetchApi(); SendMail sendmail = new SendMail(); Log log = new Log(); DataSet ds = new DataSet(); var Konfigurasi = Configurasi.LoadJson(); string url = Konfigurasi.url.ToString(); string MachineIp = Konfigurasi.ip.ToString(); string MachinePort = Konfigurasi.port.ToString(); string cabang = Konfigurasi.cabang.ToString(); int iMachineNumber = 1; string jadwal = Konfigurasi.jadwal.ToString(); string DataTerbaru = Konfigurasi.terbaru.ToString(); string datelog = DateTime.Now.ToString("yyyy-MM-dd"); string CekDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm"); string jam = DateTime.Now.ToString("HH:mm"); if (jam == jadwal) { Console.WriteLine("jadwal push jam {0} mulai ", jadwal.ToString()); #region proses absen try { #region baca mesin absen int idwErrorCode = 0; string sdwEnrollNumber = ""; int idwVerifyMode = 0; int idwInOutMode = 0; int idwYear = 0; int idwMonth = 0; int idwDay = 0; int idwHour = 0; int idwMinute = 0; int idwSecond = 0; int idwWorkcode = 0; int iGLCount = 0; Boolean bIsConnected = axCZKEM1.Connect_Net(MachineIp, Convert.ToInt32(MachinePort)); if (bIsConnected) { 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. axCZKEM1.RegEvent(iMachineNumber, 65535); //Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all) Console.WriteLine("mesin absen {0} is connected with port {1} status is {2} \n \n", MachineIp, MachinePort, bIsConnected); axCZKEM1.EnableDevice(iMachineNumber, false); //disable the device if (axCZKEM1.ReadGeneralLogData(iMachineNumber)) //read all the attendance records to the memory { #region looping data absen Console.WriteLine("start read data to machine \n"); Console.WriteLine("please wait.. \n"); DataTable dt = new DataTable("logabsen"); dt.Columns.Add("id"); dt.Columns.Add("badgenumber"); dt.Columns.Add("time"); dt.Columns.Add("cabang"); while (axCZKEM1.SSR_GetGeneralLogData(iMachineNumber, out sdwEnrollNumber, out idwVerifyMode, out idwInOutMode, out idwYear, out idwMonth, out idwDay, out idwHour, out idwMinute, out idwSecond, ref idwWorkcode))//get records from the memory { iGLCount++; string timeAbsen = idwYear.ToString() + "-" + idwMonth.ToString() + "-" + idwDay.ToString() + " " + idwHour.ToString() + ":" + idwMinute.ToString() + ":" + idwSecond.ToString(); var startDate = DateTime.Parse(datelog); var endDate = new DateTime(int.Parse(idwYear.ToString()), int.Parse(idwMonth.ToString()), int.Parse(idwDay.ToString())); var dateDiff = endDate.Subtract(startDate); if (DataTerbaru == null || DataTerbaru == "n" || DataTerbaru == "N" || DataTerbaru == "") { dt.Rows.Add(iGLCount, sdwEnrollNumber, timeAbsen, cabang); } else if (DataTerbaru == "Ya" || DataTerbaru == "Y") { if (dateDiff.TotalDays == 0) { dt.Rows.Add(iGLCount, sdwEnrollNumber, timeAbsen, cabang); } } } Console.WriteLine("finish read data to machine total {0} \n", iGLCount); ds.Tables.Add(dt); #endregion } else { axCZKEM1.GetLastError(ref idwErrorCode); if (idwErrorCode != 0) { Console.WriteLine("Reading data from terminal failed,ErrorCode: {0} ", idwErrorCode.ToString()); } else { Console.WriteLine("No data from terminal returns!"); } } axCZKEM1.EnableDevice(iMachineNumber, true);//enable the device axCZKEM1.Disconnect(); #region send data Absen int i = 0; int y = 0; Int32 totalData = ds.Tables[0].Rows.Count; Console.WriteLine("Please wait ... \n"); Console.WriteLine("Absen is sending to HO \n"); foreach (DataRow dr in ds.Tables[0].Rows) { string badge = dr["badgenumber"].ToString(); string checktime = dr["time"].ToString(); string checktype = "O"; string API_URL = url + "api/v1/absensi/" + badge + '/' + checktime + '/' + checktype + '/' + cabang; Boolean sendApi = sendAbsen.FetchApiAbsensi(API_URL, cabang); if (sendApi == true) { i++; } else { y++; } } Console.WriteLine("Send Data {0} dari total {1} Success Send Data {2} , Failed Send {3} ", i, totalData, i, y); sendmail.SendEmailKeIT("Sukses Kirim Absen Cabang " + cabang, "Total Kirim Data success " + i + " \n Gagal Kirim data " + y); #endregion } else { Console.WriteLine(DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss") + " Mesin Failed to Connected."); log.CreateLog("error-konek", "error ke ip ", MachineIp, MachinePort); sendmail.SendEmailKeIT("Gagal Konek ke Mesin dari cabang " + cabang + "\n ip mesin " + MachineIp + " PORT " + MachinePort + "Pada jam " + jadwal, "Gagal Konek Cabang" + cabang); } #endregion Thread.Sleep(45000); // 45 detik Console.WriteLine(DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss") + " Belum Ada Jadwal Push."); bIsConnected = false; } catch (Exception e) { string Log_proses = null; axCZKEM1.EnableDevice(iMachineNumber, true);//enable the device Log_proses = e.Message; axCZKEM1.Disconnect(); log.CreateLog("error-log", e.StackTrace, e.Message, null); //sendmail("Notif Error Push", e.Message); } #endregion } else { //1 detik 1000 Thread.Sleep(45000); // 45 detik Console.WriteLine(DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss") + " Belum Ada Jadwal Push."); } goto kembali; }
/// <summary> /// 从考勤机读取指纹列表 /// </summary> /// <param name="userNo"></param> /// <param name="device"></param> /// <returns></returns> private List <Fingerprint> getFingerprintListFromDevice(string userNo, string device, zkemkeeper.CZKEMClass axCZKEM1) { List <Fingerprint> fingerList = new List <Fingerprint>(); int idwFingerIndex = 0; int iTmpLength = 0; int iFlag = 0; string sTmpData = ""; for (idwFingerIndex = 0; idwFingerIndex < 10; idwFingerIndex++) { if (axCZKEM1.GetUserTmpExStr(iMachineNumber, userNo, idwFingerIndex, out iFlag, out sTmpData, out iTmpLength)) { Fingerprint finger = new Fingerprint(); finger.index = idwFingerIndex; finger.fingerprint = sTmpData; fingerList.Add(finger); } } return(fingerList); }
public ZKReader() { czkem = new zkemkeeper.CZKEMClass(); }