NEWS

[C#] Chia sẻ source code Download dữ liệu máy chấm công Ronald Jack sử dụng thư viện Zkemkeeper.dll

[C#] Chia sẻ source code Download dữ liệu máy chấm công Ronald Jack sử dụng thư viện Zkemkeeper.dll
Đăng bởi: Thảo Meo - Lượt xem: 6608 09:00:55, 09/11/2022C#   In bài viết

Xin chào các bạn, bài viết này mình chia sẻ các bạn source code đọc dữ liệu máy chấm công Ronald Jack viết dưới dạng Console Windows, sử dụng thư viện Zkemkeeper.dll

[C#] Source code download AttLog Zkemkeeper.dll with SQLSERVER

Ứng dụng viết dưới dạng Console, giúp bạn có thể dễ dàng đưa vào CronJob hay Scheduler để đến giờ nó tự động tải dữ liệu về và upload dữ liệu lên SQL SERVER.

att_log_timekeeper

Đầu tiên, các bạn cần tạo cho mình một bảng Table AttLog 

CREATE TABLE [dbo].[AttLog]
(
[id] [uniqueidentifier] NOT NULL CONSTRAINT [DF_AttLog_id] DEFAULT (newsequentialid()),
[ipAddress] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[userID] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[dateTimeRecord] [datetime] NULL,
[machineID] [int] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[AttLog] ADD CONSTRAINT [PK_AttLog] PRIMARY KEY CLUSTERED ([id]) ON [PRIMARY]
GO

Kết quả khi tải dữ liệu chúng ta sẽ có được data như hình bên dưới:

att_log_data

Bao gồm các thông tin cơ bản: IpAddress, userID, DateTimeRecord và MachineID

Dưới đây, là file cấu hình config.json, để cấu hình thông tin cơ sở dữ liệu sqlsever và danh sách máy chấm công.

{
  "server": "TMV2209068SQLEXPRESS",
  "database": "WPFDemo",
  "username": "sa",
  "password": "LapTrinhVBNet@2022",
  "numDateDownload": 7, // -1 kh�ng filter
  "ronaljack": [
    {
      "ipaddress": "192.168.x.x",
      "port": "4370",
      "machineID": 15

    },
    {
      "ipaddress": "192.168.x.x",
      "port": "4370",
      "machineID": 12

    }
  ]
}

Thuộc tính numDateDownload = 7, là nó chỉ lưu dữ liệu chấm công trong vòng 7 ngày tính từ ngày hiện tại.

Nếu các bạn muốn lấy tất cả thì setup nó bằng: -1

Video demo ứng dụng tải dữ liệu:

Ở bài viết, mình có tạo effect typing trên console, các bạn khi sử dụng có thể comment nó lại để tăng tốc độ xử lý.

Source code C#:

using ConsoleApp.Helpers;
using ConsoleApp.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VBSQLHelper;
using System.IO;

namespace ConsoleApp
{
    public class AttHelper
    {
        DeviceManipulator manipulator = new DeviceManipulator();
        public ZkemClient objZkeeper;
        private bool isDeviceConnected = false;
        private int MACHINE_NUMBER = 1;
        private string _ipAddress;
        public AttHelper(string ipAddress, string port, int machine_number)
        {          
            this._ipAddress = ipAddress;
            MACHINE_NUMBER = machine_number;
            ConnectDevice(ipAddress, port); 
        }

        public void DownloadATTLog(ConfigFile configFile) {
            try
            {
                ICollection<MachineInfo> lstMachineInfo = manipulator.GetLogData(objZkeeper, MACHINE_NUMBER);

                if (lstMachineInfo != null && lstMachineInfo.Count > 0)
                {                   
                    ConsoleHelper.ShowSuccessMessage(lstMachineInfo.Count + " dữ liệu được tìm thấy !!");
                    var songayCanlay = configFile.numDateDownload;
                  
                    if (songayCanlay > -1)
                    {
                       var listnewFilter = lstMachineInfo.Where(x => (DateTime.Now - x.DateOnlyRecord).TotalDays <= songayCanlay).ToList(); // filter lại theo số ngày tăng hiệu suất lưu sql
                        ConsoleHelper.ShowFinishMessage($"Số dòng đã Filter theo {songayCanlay} ngày: " + listnewFilter.Count + " dữ liệu được tìm thấy.");

                        // Xử lý lưu dữ liệu vào DataBase
                        SaveATTLogToDataBase(listnewFilter);
                    }
                    else {
                        // Xử lý lưu dữ liệu vào DataBase
                        SaveATTLogToDataBase(lstMachineInfo);
                    }
                    

                 
                }
                else
                    ConsoleHelper.ShowErrorMessage("Không tìm thấy dữ liệu");
            }
            catch (Exception ex)
            {
                ConsoleHelper.ShowErrorMessage("[LỖI]: "+ ex.Message);
            }
        }

        private void SaveATTLogToDataBase(ICollection<MachineInfo> lstMachineInfo)
        {
            var stopwatch = new Stopwatch();
            stopwatch.Start();         
            string query = $@"INSERT INTO AttLog(ipAddress,
                                                userID,
                                                dateTimeRecord, machineID) 
                            SELECT  @ipAddress,
                                    @userID,
                                    @dateTimeRecord,
                                    @machineID
                                        WHERE NOT EXISTS ( SELECT 0 FROM AttLog WHERE ipAddress=@ipAddress and userID=@userID and dateTimeRecord=@dateTimeRecord)
            ";
            using (var connection = new SqlConnection(SQLHelper.CONNECTION_STRINGS))
            {
                connection.Open();
                using (var transaction = connection.BeginTransaction())
                {
                    using (var cmd = new SqlCommand(query, connection, transaction))
                    {
                        int result_count = 0;
                        cmd.CommandType = CommandType.Text;
                        cmd.Transaction = transaction;
                        try
                        {
                            foreach (var item in lstMachineInfo)
                            {
                                cmd.Parameters.Clear();
                                cmd.Parameters.AddWithValue("@ipAddress", _ipAddress);
                                cmd.Parameters.AddWithValue("@userID", item.IndRegID);
                                cmd.Parameters.AddWithValue("@dateTimeRecord", item.DateTimeRecord);                             
                                cmd.Parameters.AddWithValue("@machineID", item.MachineNumber);                             
                                result_count += cmd.ExecuteNonQuery();
                            }
                            transaction.Commit();


                        }
                        catch (Exception ex)
                        {                            
                            try
                            {
                                transaction.Rollback();
                            }
                            catch (SqlException exx)
                            {
                                if (transaction.Connection != null)
                                {
                                   ConsoleHelper.ShowErrorMessage($"An exception of type {exx.GetType()} was encountered while attempting to roll back the transaction.");

                                }
                            }

                            ConsoleHelper.ShowErrorMessage($"Neither record was written to database.");


                        }
                        finally
                        {
                            connection.Close();
                            ConsoleHelper.ShowSuccessMessage($"Upload dữ liệu hoàn tất. Đã thêm mới được {result_count} tin.");
                        }
                    }
                }

            }

            ConsoleHelper.ShowDefaultMessage(String.Format("{0} seconds with one transaction.", stopwatch.Elapsed.TotalSeconds));
        }

        private void ConnectDevice(string ipAddress, string port) {
            try
            {             

                if (IsDeviceConnected)
                {
                    IsDeviceConnected = false;                  

                    return;
                }             
             
                if (ipAddress == string.Empty || port == string.Empty)
                    ConsoleHelper.ShowErrorMessage("Địa chỉ IP và cổng của thiết bị là bắt buộc nhập !!");

                int portNumber = 4370;
                if (!int.TryParse(port, out portNumber))
                    ConsoleHelper.ShowErrorMessage("Cổng nhập phải là dạng số!");

                bool isValidIpA = UniversalStatic.ValidateIP(ipAddress);
                if (!isValidIpA)
                    ConsoleHelper.ShowErrorMessage("Địa chỉ IP không hợp lệ !!");

                isValidIpA = UniversalStatic.PingTheDevice(ipAddress);
                if (!isValidIpA)
                    ConsoleHelper.ShowErrorMessage("Thiết bị tại địa chỉ IP:" + ipAddress + "  và Port:" + port + " không phản hồi !!");

                objZkeeper = new ZkemClient(RaiseDeviceEvent);
                IsDeviceConnected = objZkeeper.Connect_Net(ipAddress, portNumber);

                if (IsDeviceConnected)
                {
                    string deviceInfo = manipulator.FetchDeviceInfo(objZkeeper, MACHINE_NUMBER);
                    ConsoleHelper.ShowDefaultMessage($"===================== {ipAddress} ===============================");
                    ConsoleHelper.ShowSuccessMessage(deviceInfo);
                    ConsoleHelper.ShowDefaultMessage("===================================================================");
                }

            }
            catch (Exception ex)
            {
                ConsoleHelper.ShowErrorMessage("[LỖI]: " + ex.Message);
            }
          
        }
        private void RaiseDeviceEvent(object sender, string actionType)
        {
            switch (actionType)
            {
                case UniversalStatic.acx_Disconnect:
                    {
                        ConsoleHelper.ShowSuccessMessage("Thiết bị đã tắt");                      
                        break;
                    }

                default:
                    break;
            }

        }

       



        private bool IsDeviceConnected
        {
            get { return isDeviceConnected; }
            set
            {
                isDeviceConnected = value;
                if (isDeviceConnected)
                {
                    ConsoleHelper.ShowSuccessMessage("Kết nối với máy chấm công thành công !!");
                  
                }
                else
                {
                    ConsoleHelper.ShowSuccessMessage("Máy chấm công đã ngắt !!");
                    objZkeeper.Disconnect();
                  
                }
            }
        }

    }
}

Ở bài viết, khi lưu trữ dữ liệu tránh trùng lắp mình cũng đã sử dụng Sqlserver Transaction để tối ưu tốc độ và kiểm tra dữ liệu nào đã tải về thi không lưu vào cơ sở dữ liệu SQL nữa.

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Chia sẻ source code Download dữ liệu máy chấm công Ronald Jack sử dụng thư viện Zkemkeeper.dll
Đăng bởi: Thảo Meo - Lượt xem: 6608 09:00:55, 09/11/2022C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.