diff --git a/.vs/DevicePolling/v16/.suo b/.vs/DevicePolling/v16/.suo index 34eaded..dad6bf6 100644 Binary files a/.vs/DevicePolling/v16/.suo and b/.vs/DevicePolling/v16/.suo differ diff --git a/DevicePolling/AttendanceMachine/AttendanceMachineDAO.cs b/DevicePolling/AttendanceMachine/AttendanceMachineDAO.cs index 2be33f0..08cb50c 100644 --- a/DevicePolling/AttendanceMachine/AttendanceMachineDAO.cs +++ b/DevicePolling/AttendanceMachine/AttendanceMachineDAO.cs @@ -12,6 +12,7 @@ namespace ZktecoAttendenceService public void update(AttendanceMachine attendanceMachine, MySqlConnection connection) { + connection.Open(); string query = "UPDATE `hrms`.`attendance_machine` SET `machine_ip` = @machine_ip, `port_number` = @port_number, `machine_name` = @machine_name, `site_id` = @site_id, `status` = @status, `last_sync_date` = @last_sync_date WHERE `machine_id` = @machine_id"; MySqlCommand cmd = new MySqlCommand(query, connection); cmd.Parameters.AddWithValue("@machine_ip", attendanceMachine.MachineIp); @@ -23,6 +24,7 @@ namespace ZktecoAttendenceService cmd.Parameters.AddWithValue("@machine_id", attendanceMachine.MachineId); cmd.ExecuteNonQuery(); + connection.Close(); } public void updateColony(AttendanceMachine attendanceMachine, MySqlConnection connection) { diff --git a/DevicePolling/MachineUsers/AttendanceMachineUserDAO.cs b/DevicePolling/MachineUsers/AttendanceMachineUserDAO.cs index c5d52de..65d5f61 100644 --- a/DevicePolling/MachineUsers/AttendanceMachineUserDAO.cs +++ b/DevicePolling/MachineUsers/AttendanceMachineUserDAO.cs @@ -216,16 +216,20 @@ namespace ZktecoAttendenceService public void UpdateTotalEmpInMachines(string machine_id, int total_users, MySqlConnection connection) { + connection.Open(); string query = "UPDATE `hrms`.`attendance_machine` SET `total_users` =" + total_users + " WHERE `machine_id` = '" + machine_id + "'"; MySqlCommand cmd = new MySqlCommand(query, connection); cmd.ExecuteNonQuery(); + connection.Close(); } public void UpdateFlag_InMachineUsers(string machine_id, string serial_no, MySqlConnection connection) { + connection.Open(); string query = "UPDATE `hrms`.`attendance_machine_user` SET `is_deleted` = 1 WHERE `machine_id` = '" + machine_id + "' and serial_number = " + serial_no + ""; MySqlCommand cmd = new MySqlCommand(query, connection); cmd.ExecuteNonQuery(); + connection.Close(); } } } diff --git a/DevicePolling/Polling.cs b/DevicePolling/Polling.cs index 75b9900..c6dd784 100644 --- a/DevicePolling/Polling.cs +++ b/DevicePolling/Polling.cs @@ -57,46 +57,38 @@ namespace ZktecoAttendenceService Application.Exit(); } - - public List StartPooling() { List responses = new List(); + AttendanceMachineDAO attendanceMachineDAO = new AttendanceMachineDAO(); + AttendanceMachineUserDAO attendanceMachineUserDAO = new AttendanceMachineUserDAO(); - try + using (var connection = ObjConnectionClass.Connection()) { - //timer1.Enabled = false; - - Ping ping = new Ping(); - string mIp = string.Empty; - Int16 mMacID = 0; - string mInOut = string.Empty; - - connection = ObjConnectionClass.Connection(); - if (ObjConnectionClass.OpenConnection(connection) == true) + if (ObjConnectionClass.OpenConnection(connection)) { - AttendanceMachineDAO attendanceMachineDAO = new AttendanceMachineDAO(); - List machines = attendanceMachineDAO.getAttendanceMachines(connection, "0"); + var machines = attendanceMachineDAO.getAttendanceMachines(connection, "0"); foreach (var machine in machines) { - - //if (machine.MachineIp == "192.168.52.17") + + // if (machine.MachineIp == "192.168.85.202") { - - - bool Bconnect = false; - - Bconnect = axCZKEM1.Connect_Net(machine.MachineIp, Convert.ToInt32(machine.PortNumber)); - axCZKEM1.EnableDevice(Convert.ToInt32(machine.MachineId), false);//disable the device - - - if (Bconnect == true) + bool isConnected = axCZKEM1.Connect_Net(machine.MachineIp, Convert.ToInt32(machine.PortNumber)); + if (isConnected) { + axCZKEM1.EnableDevice(Convert.ToInt32(machine.MachineId), false); if (machine.MachineId != "100") { + + //Saving the templates to the database + SaveTemplateDb saveTemplateDb = new SaveTemplateDb(); + saveTemplateDb.InsertFaceIntoDb(machine, connection,axCZKEM1); + + + machine.Status = "SYNCING"; attendanceMachineDAO.update(machine, connection); @@ -108,27 +100,25 @@ namespace ZktecoAttendenceService Console.WriteLine(machine.MachineId + " => " + machine.LastSyncDate); attendanceMachineDAO.update(machine, connection); - - List Newresponses = new List(); + List newResponses = new List(); GetAllFaceInfo(machine); - //Delete from machine and db attendanceMachineUserDAO.UpdateTotalEmpInMachines(machine.MachineId, empList.Count, connection); - Newresponses = (attendanceMachineUserDAO.DeleteFromDbAndMachine(empList, machine, connection)); - if (Newresponses.Count != 0) + newResponses = attendanceMachineUserDAO.DeleteFromDbAndMachine(empList, machine, connection); + if (newResponses.Count != 0) { - responses.AddRange(Newresponses); - + responses.AddRange(newResponses); } + + + + empList = new List(); axCZKEM1.EnableDevice(Convert.ToInt32(machine.MachineId), true); Cursor = Cursors.Default; axCZKEM1.Disconnect(); - } - - //for colony attendance else { machine.Status = "SYNCING"; @@ -145,36 +135,18 @@ namespace ZktecoAttendenceService axCZKEM1.Disconnect(); } } - else { responses.Add("MACHINE : " + machine.MachineIp + " : NOT CONNECTED"); - // return responses; machine.Status = "NOT CONNECTED"; attendanceMachineDAO.update(machine, connection); - } - - } - } - ObjConnectionClass.CloseConnection(connection); } - - return responses; - - } - catch (Exception ex) - { - return responses; - } - finally - { - ObjConnectionClass.CloseConnection(connection); } - + return responses; } public List poolMachineDataColony(string ip, int port, int machineId) diff --git a/DevicePolling/SaveTemplate/SaveTemplateDb.cs b/DevicePolling/SaveTemplate/SaveTemplateDb.cs new file mode 100644 index 0000000..a769c13 --- /dev/null +++ b/DevicePolling/SaveTemplate/SaveTemplateDb.cs @@ -0,0 +1,144 @@ +using MySql.Data.MySqlClient; +using Org.BouncyCastle.Crypto.IO; +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ZktecoAttendenceService +{ + internal class SaveTemplateDb + { + + + string empID; + string template; + int length = 0; + long empExist; long templateExist; + public void InsertFaceIntoDb(AttendanceMachine attendanceMachine, MySqlConnection connection, zkemkeeper.CZKEMClass axCZKEM1) + { + try + { + string sName; + string sPassword; + int iPrivilege; + bool bEnabled; + int machineNo = Convert.ToInt32(attendanceMachine.MachineId); + + // Create a list to store emp ID and template + + List<(string, string,long,long)> empTemplateList = new List<(string, string,long,long)>(); + // Get all emp + while (axCZKEM1.SSR_GetAllUserInfo(machineNo, out empID, out sName, out sPassword, out iPrivilege, out bEnabled)) + { + { + + ( empExist, templateExist) = CheckEmpInDb(empID, connection); + + + if (templateExist == 0 ) + { + + if (axCZKEM1.GetUserFaceStr(machineNo, empID, 50, ref template, ref length)) + { + Console.WriteLine("User ID: " + empID + " Name: " + sName + " Template: " + template + " Length: " + length); + // Store emp ID and template in the list + empTemplateList.Add((empID, template,empExist, templateExist)); + + } + } + } + } + + + axCZKEM1.EnableDevice(machineNo, true); + axCZKEM1.Disconnect(); + + + SaveInDb(empTemplateList, connection); + } + catch (Exception ex) + { + // Handle exception + } + } + //save template to db with emp id and template parameter + public void SaveInDb(List<(string, string, long, long)> empTemplateList, MySqlConnection connection) + { + + foreach (var item in empTemplateList) + { + + long empExist = item.Item3; + long empTemplateExist = item.Item4; + if (empExist == 0 && empTemplateExist == 0) + { + string insertQuery = "INSERT INTO attendance_machine_face_templates (serial_no, template_zkteco, created_date) VALUES (@emp_id, @template, @created_date)"; + + MySqlCommand command = new MySqlCommand(insertQuery, connection); + + command.Parameters.AddWithValue("@emp_id", item.Item1); + command.Parameters.AddWithValue("@template", item.Item2); + command.Parameters.AddWithValue("@created_date", DateTime.Now); + command.ExecuteNonQuery(); + command.Parameters.Clear(); + Console.WriteLine("Emp id " + item.Item1 + " Inserted"); + } + if (empExist == 1 && empTemplateExist == 0) + { + string updateQuery = "UPDATE attendance_machine_face_templates set template_zkteco = @template where serial_no = @emp_id and is_active = 1"; + MySqlCommand command = new MySqlCommand(updateQuery, connection); + + command.Parameters.AddWithValue("@emp_id", item.Item1); + command.Parameters.AddWithValue("@template", item.Item2); + command.Parameters.AddWithValue("@created_date", DateTime.Now); + command.ExecuteNonQuery(); + command.Parameters.Clear(); + Console.WriteLine("Employee already exist"); + } + } + connection.Close(); + } + + public (long EmpExist, long TemplateExist) CheckEmpInDb(string empId, MySqlConnection connection) + { + string query = @" + SELECT + COUNT(*) AS EmpExist, + CASE + WHEN COUNT(*) = 0 THEN 0 -- If no employee exists, set TemplateExist to 0 + ELSE SUM(CASE WHEN template_zkteco IS NULL THEN 0 ELSE 1 END) + END AS TemplateExist + FROM attendance_machine_face_templates + WHERE is_active = 1 AND serial_no = @emp_id"; + + using (MySqlCommand command = new MySqlCommand(query, connection)) + { + command.Parameters.AddWithValue("@emp_id", empId); + + if (connection.State != ConnectionState.Open) + { + connection.Open(); + } + + using (MySqlDataReader reader = command.ExecuteReader()) + { + if (reader.Read()) + { + long empExist = reader.GetInt64(0); // First column (EmpExist) + long templateExist = reader.GetInt64(1); // Second column (TemplateExist) + + return (empExist, templateExist); + } + } + } + + //close connection + connection.Close(); + return (0, 0); // Return (0, 0) if no data is found + } + } + +} diff --git a/DevicePolling/ZktecoAttendenceService.csproj b/DevicePolling/ZktecoAttendenceService.csproj index d17d660..7156e46 100644 --- a/DevicePolling/ZktecoAttendenceService.csproj +++ b/DevicePolling/ZktecoAttendenceService.csproj @@ -134,6 +134,7 @@ + Polling.cs diff --git a/DevicePolling/bin/Debug/DevicePolling.exe b/DevicePolling/bin/Debug/DevicePolling.exe index a858f52..95103bb 100644 Binary files a/DevicePolling/bin/Debug/DevicePolling.exe and b/DevicePolling/bin/Debug/DevicePolling.exe differ diff --git a/DevicePolling/bin/Debug/DevicePolling.pdb b/DevicePolling/bin/Debug/DevicePolling.pdb index 9b72b62..aa77aed 100644 Binary files a/DevicePolling/bin/Debug/DevicePolling.pdb and b/DevicePolling/bin/Debug/DevicePolling.pdb differ diff --git a/DevicePolling/obj/Debug/DesignTimeResolveAssemblyReferences.cache b/DevicePolling/obj/Debug/DesignTimeResolveAssemblyReferences.cache index 2c3f41b..78eb585 100644 Binary files a/DevicePolling/obj/Debug/DesignTimeResolveAssemblyReferences.cache and b/DevicePolling/obj/Debug/DesignTimeResolveAssemblyReferences.cache differ diff --git a/DevicePolling/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/DevicePolling/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache index b2aaafd..6323244 100644 Binary files a/DevicePolling/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache and b/DevicePolling/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ diff --git a/DevicePolling/obj/Debug/DevicePolling.exe b/DevicePolling/obj/Debug/DevicePolling.exe index a858f52..95103bb 100644 Binary files a/DevicePolling/obj/Debug/DevicePolling.exe and b/DevicePolling/obj/Debug/DevicePolling.exe differ diff --git a/DevicePolling/obj/Debug/DevicePolling.pdb b/DevicePolling/obj/Debug/DevicePolling.pdb index 9b72b62..aa77aed 100644 Binary files a/DevicePolling/obj/Debug/DevicePolling.pdb and b/DevicePolling/obj/Debug/DevicePolling.pdb differ diff --git a/DevicePolling/obj/Debug/ZktecoAttendenceService.csproj.AssemblyReference.cache b/DevicePolling/obj/Debug/ZktecoAttendenceService.csproj.AssemblyReference.cache index f5e894a..c7fd9df 100644 Binary files a/DevicePolling/obj/Debug/ZktecoAttendenceService.csproj.AssemblyReference.cache and b/DevicePolling/obj/Debug/ZktecoAttendenceService.csproj.AssemblyReference.cache differ diff --git a/DevicePolling/obj/Debug/ZktecoAttendenceService.csproj.CoreCompileInputs.cache b/DevicePolling/obj/Debug/ZktecoAttendenceService.csproj.CoreCompileInputs.cache index 783f36a..9bf3c12 100644 --- a/DevicePolling/obj/Debug/ZktecoAttendenceService.csproj.CoreCompileInputs.cache +++ b/DevicePolling/obj/Debug/ZktecoAttendenceService.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -e624d2b121c7896fcfa753a70d1d65c9f169c918 +a4ea5cb743704c01c21ff9b787f6590f17ead844 diff --git a/DevicePolling/obj/Debug/ZktecoAttendenceService.csproj.GenerateResource.cache b/DevicePolling/obj/Debug/ZktecoAttendenceService.csproj.GenerateResource.cache index 83f0e74..bf27e7d 100644 Binary files a/DevicePolling/obj/Debug/ZktecoAttendenceService.csproj.GenerateResource.cache and b/DevicePolling/obj/Debug/ZktecoAttendenceService.csproj.GenerateResource.cache differ