added clearlog after successful data post

main
muhammad.faique 2025-04-04 12:29:30 +05:00
parent fc3823feb8
commit 6b03af550f
5 changed files with 60 additions and 45 deletions

View File

@ -80,7 +80,7 @@ namespace ZktecoAttendenceService
foreach (var machine in machines) 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)); bool isConnected = axCZKEM1.Connect_Net(machine.MachineIp, Convert.ToInt32(machine.PortNumber));
if (isConnected) if (isConnected)
@ -160,75 +160,90 @@ namespace ZktecoAttendenceService
public List<string> poolMachineDataColony(string ip, int port, int machineId, MySqlConnection connection) public List<string> poolMachineDataColony(string ip, int port, int machineId, MySqlConnection connection)
{ {
List<string> responses = new List<string>(); List<string> responses = new List<string>();
int idwErrorCode = 0;
int countAttendance = 0;
bool allRecordsSaved = true; // Flag to track if all records are saved successfully
// Ensure axCZKEM1 is initialized before using it
string mPath = string.Empty; if (axCZKEM1 == null)
{
string sdwEnrollNumber = ""; responses.Add($"MACHINE: {ip} - ERROR: SDK Not Initialized");
int idwVerifyMode = 0; return responses;
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
try try
{ {
// Connect to the machine
//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) bool isConnected = axCZKEM1.Connect_Net(ip, port);
if (axCZKEM1.ReadAllGLogData(machineId))//read all the attendance records to the memory 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, 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))
{
try
{ {
DateTime date = new DateTime(idwYear, idwMonth, idwDay, idwHour, idwMinute, idwSecond); DateTime date = new DateTime(idwYear, idwMonth, idwDay, idwHour, idwMinute, idwSecond);
try
{
Attendance log = new Attendance(sdwEnrollNumber, date, false, machineId.ToString(), "1", ip, DateTime.Now); Attendance log = new Attendance(sdwEnrollNumber, date, false, machineId.ToString(), "1", ip, DateTime.Now);
attendanceDAO.AddColony(log, connection); attendanceDAO.AddColony(log, connection);
CountAttendance++; countAttendance++;
responses.Add("MACHINE : " + ip + " : , EMP NO : " + sdwEnrollNumber + " , TIME : " + date + ""); responses.Add($"MACHINE: {ip} - EMP NO: {sdwEnrollNumber} - TIME: {date}");
//return response;
} }
catch (Exception ex) catch (Exception ex)
{ {
responses.Add(ex.Message.ToString()); responses.Add($"MACHINE: {ip} - ERROR: {ex.Message}");
allRecordsSaved = false; // Mark as false if any record fails
} }
} }
// **Clear Attendance Logs Only If All Records Are Saved**
// responses.Add(GetTotalFromMachine(machineId)); if (allRecordsSaved)
axCZKEM1.ClearGLog(machineId); {
//axCZKEM1.EnableDevice(machineId, true); if (axCZKEM1.ClearGLog(machineId))
{
// axCZKEM1.Disconnect(); responses.Add($"MACHINE: {ip} - ATTENDANCE LOGS CLEARED SUCCESSFULLY");
} }
else else
{ {
responses.Add("MACHINE : " + ip + " : NO DATA"); axCZKEM1.GetLastError(ref idwErrorCode);
return responses; 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
{
axCZKEM1.GetLastError(ref idwErrorCode);
responses.Add($"MACHINE: {ip} - NO DATA (Error Code {idwErrorCode})");
} }
return responses;
} }
catch (Exception ex) catch (Exception ex)
{ {
responses.Add($"MACHINE: {ip} - ERROR: {ex.Message}");
responses.Add(ex.Message.ToString());
return responses;
} }
finally
{
// Re-enable the device and disconnect
axCZKEM1.EnableDevice(machineId, true);
axCZKEM1.Disconnect();
}
return responses;
} }
int errorCode = 0; int errorCode = 0;
public List<string> poolMachineData(string ip, int port, int machineId, MySqlConnection connection) public List<string> poolMachineData(string ip, int port, int machineId, MySqlConnection connection)