C# Clear Biometric logs after logging into postgresql database without any loss of data

646 views Asked by At

I have developed a windows service which will log all the bio-metric logs. For this to run, i have used npgsql for postgres connectivity. The below code will do all the work and to activate the logging , i have used the timer in the windows service which will run the below function for every 1 hour. The below code works fine for collecting the attendance log from the bio-metric. But, What i need is, after the collecting the log and writing it to the database table, it should clear the logs in the bio-metric device. Please consider the below scenario before doing that.

1.at 1:00 windows service starts collecting data. 2.at 1:10 service finish collecting data. 3.at 1:10, service needs to clear the log. 4.While clearing the log, if any person access the device, that log should not be omitted. OpenConn() & CloseConn() are used to open and close the database connection. Flush() is used to clear the memory.

public void getLog()
    {
        bIsConnected = axCZKEM1.Connect_Net("xxx.xxx.xxx.xxx", Convert.ToInt32("4370"));
        if (bIsConnected == true)
        {
            LogEntry(string.Format("{0} : Biometric Device Connected", DateTime.Now));
            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 idwErrorCode = 0;
            int iGLCount = 0;
            // int iIndex = 0;            

            if (this.axCZKEM1.ReadGeneralLogData(iMachineNumber))//read all the attendance records to the memory
            {
                while (this.axCZKEM1.SSR_GetGeneralLogData(iMachineNumber, 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
                {
                    var first = iGLCount.ToString();
                    var second = sdwEnrollNumber;//modify by Darcy on Nov.26 2009                 
                    var third = idwVerifyMode.ToString();
                    var fourth = idwInOutMode.ToString();
                    var fifth = idwYear.ToString() + "-" + idwMonth.ToString() + "-" + idwDay.ToString() + " " + idwHour.ToString() + ":" + idwMinute.ToString() + ":" + idwSecond.ToString();
                    var sixth = idwWorkcode.ToString();
                    string check1 = "False";
                    try
                    {
                        this.OpenConn();
                        //LogEntry(string.Format("{0} : Opens connection", DateTime.Now));
                        string SQL = "select exists(select bio_enrollno from bio_branch where bio_datetime = '" + fifth + "')";
                        using (NpgsqlCommand cmd1 = new NpgsqlCommand(SQL, conn))
                        {
                            NpgsqlDataReader dr = cmd1.ExecuteReader();
                            while (dr.Read())
                            {
                                for (int i = 0; i < dr.FieldCount; i++)
                                {
                                    string output = dr[i].ToString();
                                    if (output.Equals(check1))
                                    {
                                        this.CloseConn();
                                        //LogEntry(string.Format("{0} : string equal and opens connection", DateTime.Now));
                                        this.OpenConn();
                                        string SQL1 = "INSERT INTO bio_branch(bio_enrollno, bio_verifymode, bio_inoutmode, bio_datetime, bio_workcode) VALUES ('" + second + "','" + third + "','" + fourth + "' ,'" + fifth + "', '" + sixth + "')";
                                        NpgsqlCommand command1 = new NpgsqlCommand(SQL1, conn);
                                        Int32 rowsaffected1 = command1.ExecuteNonQuery();
                                        //LogEntry(string.Format("{0} : DB Write Successfull", DateTime.Now));
                                        this.CloseConn();
                                    }
                                }
                            }
                            //LogEntry(string.Format("{0} : Close Connection", DateTime.Now));
                            this.CloseConn();
                        }
                    }
                    catch (Exception err)
                    {
                        LogEntry(string.Format("{1} : Database Insert Error : {0}", err, DateTime.Now));
                        this.CloseConn();
                        break;
                    }                        
                }
                Flush();
            }
            else
            {
                axCZKEM1.GetLastError(ref idwErrorCode);
                if (idwErrorCode != 0)
                {
                    LogEntry(string.Format("{0} : Reading data from terminal failed,ErrorCode : {1}", DateTime.Now, idwErrorCode.ToString()));
                }
                else
                {
                    LogEntry(string.Format("{0} : No data from terminal returns!", DateTime.Now));
                }
            }
            axCZKEM1.Disconnect();                
            LogEntry(string.Format("{0} : Biometric Disconnected", DateTime.Now));
            return;
        }
        else
        {
            LogEntry(string.Format("{0} : Please connect the device", DateTime.Now));
            return;
        }
    }
1

There are 1 answers

0
Alejandro del Río On

After your SSR_GetGeneralLogData, you can add the following code

    if (axCZKEM1.ClearGLog(iMachineNumber))
    {
        axCZKEM1.RefreshData(iMachineNumber);//the data in the device should be refreshed
        LogEntry(string.Format("{0} : All att Logs have been cleared from teiminal!, Success", DateTime.Now));
    }
    else
    {
        axCZKEM1.GetLastError(ref idwErrorCode);
        LogEntry(string.Format("{0} : Clear operation failed", DateTime.Now));
    }

This will clear the log data in the device (do it after you saved the data) and the next download will be much faster because the machine allways will be with just a few logs.