Access to Dahua access card swiping record by Java Web project

1. Download the official equipment network sdk_java64 of Dahua, https://www.dahuatech.com/service/downloadlists/836.html
2. There are examples in the sdk. Try to use the things packaged by Dahua. The following java classes are used to obtain access card records:

com/netsdk/lib
    NetSDKLib.java
    ToolKits.java
    Utils.java
com/netsdk/demo/module
    LoginModule.java

I thought that com.netsdk.demo.module.GateModule.java would have a ready-made implementation method to obtain access card swiping records, but it didn't. I wrote one by myself.

3. Introduce libs/jna.jar into your own spring boot project.

<dependency>
  <groupId>com.dahua</groupId>
  <artifactId>jna</artifactId>
  <version>1.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/lib/jna.jar</systemPath>
</dependency>

The dll file under libs/win64 is Dahua's class library. It needs to be read at runtime. There is a method to read it in Utils.java. You can store it in the path corresponding to your project according to the path inside. My project is spring boot. I put libs in the same level folder of src folder where I started the project.

len-web
    -src
    -libs
        -win64

4. Dahua's pit: in the notes of documents and classes, it is stated that access swipe card records can be retrieved through time periods, but some access control devices do not support it, so they will only give you all the records. Note that init, the initialization method in LoginModule, is used for java Web projects. When the service is started, it is OK to initialize once, and then directly call the login and logout methods, Do not call init method many times, so java memory will burst. I did it in the beginning (depressed) Also, the time recorded by swiping the card is not Beijing time, but 0 time zone time, so it will add 8 hours to Beijing time after getting it.

5. When packaging with maven, if jna.jar is missed, add spring boot Maven plugin in pom.xml

<plugins>
  <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
      <includeSystemScope>true</includeSystemScope>
    </configuration>
  </plugin>
</plugins>

6. GateModule.java code

package com.dahua.module;
import com.dahua.lib.NetSDKLib;
import com.dahua.lib.ToolKits;
import com.sun.jna.Memory;
public class GateModule {
    /**
         * Query swipe card record, get query handle
     * @param findCondition query criteria
     * @return
     */
    public static NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARDREC[] findRecords(NetSDKLib.NET_TIME startTime, NetSDKLib.NET_TIME endTime) {
        // Interface entry
        NetSDKLib.FIND_RECORD_ACCESSCTLCARDREC_CONDITION_EX findCondition = new NetSDKLib.FIND_RECORD_ACCESSCTLCARDREC_CONDITION_EX();
        findCondition.bCardNoEnable = 0;
        findCondition.stStartTime = startTime;
        findCondition.stEndTime = endTime;
        // Client? Findrecord interface input parameter
        NetSDKLib.NET_IN_FIND_RECORD_PARAM stIn = new NetSDKLib.NET_IN_FIND_RECORD_PARAM();
        stIn.emType = NetSDKLib.EM_NET_RECORD_TYPE.NET_RECORD_ACCESSCTLCARDREC_EX;
        stIn.pQueryCondition = findCondition.getPointer();
       
        // Client? Findrecord interface output parameter
        NetSDKLib.NET_OUT_FIND_RECORD_PARAM stOut = new NetSDKLib.NET_OUT_FIND_RECORD_PARAM();
        findCondition.write();
       
        NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARDREC[] pstRecordEx = new NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARDREC[0];
        // Get query handle
        if(LoginModule.netsdk.CLIENT_FindRecord(LoginModule.m_hLoginHandle, stIn, stOut, 5000)) {
            findCondition.read();
           
            // Used to apply for memory, assuming 2000 card swiping records
            int nFindCount = 2000;
            NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARDREC[] pstRecord = new NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARDREC[nFindCount];
            for(int i = 0; i < nFindCount; i++) {
                pstRecord[i] = new NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARDREC();
            }
           
            NetSDKLib.NET_IN_FIND_NEXT_RECORD_PARAM stNextIn = new NetSDKLib.NET_IN_FIND_NEXT_RECORD_PARAM();
            stNextIn.lFindeHandle = stOut.lFindeHandle;
            stNextIn.nFileCount = nFindCount;
           
            NetSDKLib.NET_OUT_FIND_NEXT_RECORD_PARAM stNextOut = new NetSDKLib.NET_OUT_FIND_NEXT_RECORD_PARAM();
            stNextOut.nMaxRecordNum = nFindCount;
            // Application memory
            stNextOut.pRecordList = new Memory(pstRecord[0].dwSize * nFindCount);
            stNextOut.pRecordList.clear(pstRecord[0].dwSize * nFindCount);
           
            // Copy array memory to pointer
            ToolKits.SetStructArrToPointerData(pstRecord, stNextOut.pRecordList);
           
            if(LoginModule.netsdk.CLIENT_FindNextRecord(stNextIn, stNextOut, 5000)) {
                if(stNextOut.nRetRecordNum == 0) {
                    return pstRecordEx;
                }
                // Get card information
                ToolKits.GetPointerDataToStructArr(stNextOut.pRecordList, pstRecord);
                       
                // Get useful information
                pstRecordEx = new NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARDREC[stNextOut.nRetRecordNum];
                for(int i = 0; i < stNextOut.nRetRecordNum; i++) {
                    pstRecordEx[i] = new NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARDREC();
                    pstRecordEx[i] = pstRecord[i];
                }
            }
           
            LoginModule.netsdk.CLIENT_FindRecordClose(stOut.lFindeHandle);
        }
        return pstRecordEx;
    }
}

7. Call mode

boolean flag = LoginModule.login(hall.getGateIp(), 37777, "admin", "123456");
if (flag) {
    // Retrieval end time: current time
    Date endDateTime = calendar.getTime();
    NetSDKLib.NET_TIME endTime = new NetSDKLib.NET_TIME();
    endTime.setTime(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DATE), calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
   
    // Get retrieval start time: current time - 5 minutes
    calendar.add(Calendar.MINUTE, -5);
    Date startDateTime = calendar.getTime();
    NetSDKLib.NET_TIME startTime = new NetSDKLib.NET_TIME();
    startTime.setTime(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DATE), calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
   
    NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARDREC[] cardRecords = GateModule.findRecords(startTime, endTime);
   
    for (NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARDREC cardRecord : cardRecords) {
        // Card number
        String cardNo = new String(cardRecord.szCardNo).trim();
        // If the card swiping status is not correct or the key is used to open the door, no data will be entered
        if (cardRecord.bStatus != 1 || "00000000".equals(cardNo)) {
            continue;
        }
        // Swipe time converted to Beijing time + 8 hours
        Date stuTime = DateUtil.convertStringToDate(cardRecord.stuTime.toStringTimeEx(), "yyyy-MM-dd HH:mm:ss");
        Calendar stuCalendar = DateUtil.getCalendar(stuTime);
        stuCalendar.add(Calendar.HOUR, 8);
    }
}
Published 1 original article, praised 0 and visited 5
Private letter follow

Tags: Java Spring Maven network

Posted on Thu, 16 Jan 2020 06:57:14 -0800 by maverickminds