diff --git a/DevicePolling/Polling.cs b/DevicePolling/Polling.cs index 82ce909..81c36a1 100644 --- a/DevicePolling/Polling.cs +++ b/DevicePolling/Polling.cs @@ -80,7 +80,7 @@ namespace ZktecoAttendenceService foreach (var machine in machines) { - //if (machine.MachineIp == "192.168.50.8") + if (machine.MachineIp == "192.168.50.8") { bool isConnected = axCZKEM1.Connect_Net(machine.MachineIp, Convert.ToInt32(machine.PortNumber)); if (isConnected) @@ -160,75 +160,90 @@ namespace ZktecoAttendenceService public List poolMachineDataColony(string ip, int port, int machineId, MySqlConnection connection) { List responses = new List(); + int idwErrorCode = 0; + int countAttendance = 0; + bool allRecordsSaved = true; // Flag to track if all records are saved successfully - - string mPath = string.Empty; - - 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 CountAttendance = 0; - - //Cursor = Cursors.WaitCursor; - //Bconnect = axCZKEM1.Connect_Net(ip, Convert.ToInt32(port)); - //axCZKEM1.EnableDevice(machineId, false);//disable the device + // Ensure axCZKEM1 is initialized before using it + if (axCZKEM1 == null) + { + responses.Add($"MACHINE: {ip} - ERROR: SDK Not Initialized"); + return responses; + } try { - - //axCZKEM1.RegEvent(Convert.ToInt32(machineId), 65535);//Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all) - if (axCZKEM1.ReadAllGLogData(machineId))//read all the attendance records to the memory + // Connect to the machine + bool isConnected = axCZKEM1.Connect_Net(ip, port); + if (!isConnected) { + axCZKEM1.GetLastError(ref idwErrorCode); + responses.Add($"MACHINE: {ip} - ERROR: Unable to connect (Code {idwErrorCode})"); + return responses; + } + + // Disable the device during data retrieval + axCZKEM1.EnableDevice(machineId, false); + string sdwEnrollNumber; + int idwVerifyMode, idwInOutMode, idwYear, idwMonth, idwDay, idwHour, idwMinute, idwSecond, idwWorkcode = 0; + + if (axCZKEM1.ReadAllGLogData(machineId)) // Read all attendance logs + { + while (axCZKEM1.SSR_GetGeneralLogData(machineId, 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 + out idwInOutMode, out idwYear, out idwMonth, out idwDay, out idwHour, out idwMinute, out idwSecond, ref idwWorkcode)) { - DateTime date = new DateTime(idwYear, idwMonth, idwDay, idwHour, idwMinute, idwSecond); try { - + DateTime date = new DateTime(idwYear, idwMonth, idwDay, idwHour, idwMinute, idwSecond); Attendance log = new Attendance(sdwEnrollNumber, date, false, machineId.ToString(), "1", ip, DateTime.Now); attendanceDAO.AddColony(log, connection); - CountAttendance++; - responses.Add("MACHINE : " + ip + " : , EMP NO : " + sdwEnrollNumber + " , TIME : " + date + ""); - - //return response; - + countAttendance++; + responses.Add($"MACHINE: {ip} - EMP NO: {sdwEnrollNumber} - TIME: {date}"); } catch (Exception ex) { - responses.Add(ex.Message.ToString()); + responses.Add($"MACHINE: {ip} - ERROR: {ex.Message}"); + allRecordsSaved = false; // Mark as false if any record fails } } - - // responses.Add(GetTotalFromMachine(machineId)); - axCZKEM1.ClearGLog(machineId); - //axCZKEM1.EnableDevice(machineId, true); - - // axCZKEM1.Disconnect(); - + // **Clear Attendance Logs Only If All Records Are Saved** + if (allRecordsSaved) + { + if (axCZKEM1.ClearGLog(machineId)) + { + responses.Add($"MACHINE: {ip} - ATTENDANCE LOGS CLEARED SUCCESSFULLY"); + } + else + { + axCZKEM1.GetLastError(ref idwErrorCode); + responses.Add($"MACHINE: {ip} - ERROR: Failed to clear logs (Code {idwErrorCode})"); + } + } + else + { + responses.Add($"MACHINE: {ip} - LOGS NOT CLEARED: Some records failed to save."); + } } else { - responses.Add("MACHINE : " + ip + " : NO DATA"); - return responses; + axCZKEM1.GetLastError(ref idwErrorCode); + responses.Add($"MACHINE: {ip} - NO DATA (Error Code {idwErrorCode})"); } - return responses; } catch (Exception ex) { - - responses.Add(ex.Message.ToString()); - return responses; - + responses.Add($"MACHINE: {ip} - ERROR: {ex.Message}"); } + finally + { + // Re-enable the device and disconnect + axCZKEM1.EnableDevice(machineId, true); + axCZKEM1.Disconnect(); + } + + return responses; } int errorCode = 0; public List poolMachineData(string ip, int port, int machineId, MySqlConnection connection) diff --git a/DevicePolling/bin/Debug/DevicePolling.exe b/DevicePolling/bin/Debug/DevicePolling.exe index 055cf74..fa7c0c8 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 0462ed0..6f727dd 100644 Binary files a/DevicePolling/bin/Debug/DevicePolling.pdb and b/DevicePolling/bin/Debug/DevicePolling.pdb differ diff --git a/DevicePolling/obj/Debug/DevicePolling.exe b/DevicePolling/obj/Debug/DevicePolling.exe index 055cf74..fa7c0c8 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 0462ed0..6f727dd 100644 Binary files a/DevicePolling/obj/Debug/DevicePolling.pdb and b/DevicePolling/obj/Debug/DevicePolling.pdb differ