Add save tempate in db feature

main
muhammad.faique 2025-02-26 13:20:58 +05:00
parent 32cff1392e
commit f64e75970c
15 changed files with 178 additions and 55 deletions

Binary file not shown.

View File

@ -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)
{

View File

@ -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();
}
}
}

View File

@ -57,46 +57,38 @@ namespace ZktecoAttendenceService
Application.Exit();
}
public List<string> StartPooling()
{
List<string> responses = new List<string>();
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<AttendanceMachine> 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<string> Newresponses = new List<string>();
List<string> newResponses = new List<string>();
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<string>();
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<string> poolMachineDataColony(string ip, int port, int machineId)

View File

@ -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
}
}
}

View File

@ -134,6 +134,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="MachineUsers\AttendanceMachineUserDAO.cs" />
<Compile Include="MachineUsers\AttendanceMachineUsers.cs" />
<Compile Include="SaveTemplate\SaveTemplateDb.cs" />
<Compile Include="WriteToTxtFile.cs" />
<EmbeddedResource Include="Polling.resx">
<DependentUpon>Polling.cs</DependentUpon>

View File

@ -1 +1 @@
e624d2b121c7896fcfa753a70d1d65c9f169c918
a4ea5cb743704c01c21ff9b787f6590f17ead844