GateLogService.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.greentech.gateservice.task;
  2. import com.greentech.gateservice.entity.GateOplog;
  3. import com.greentech.gateservice.mapper.GateOplogMapper;
  4. import com.greentech.gateservice.util.LoginModule;
  5. import com.netsdk.common.Res;
  6. import com.netsdk.lib.NetSDKLib;
  7. import com.netsdk.lib.ToolKits;
  8. import com.sun.jna.Memory;
  9. import lombok.Data;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.scheduling.annotation.Scheduled;
  13. import org.springframework.stereotype.Component;
  14. import org.springframework.stereotype.Service;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import java.text.ParseException;
  17. import java.text.SimpleDateFormat;
  18. import java.time.LocalDateTime;
  19. import java.util.*;
  20. @RequestMapping("crud")
  21. @Data
  22. @Component
  23. @Service
  24. public class GateLogService {
  25. @Autowired
  26. GateOplogMapper gateOplogMapper;
  27. @Value("${customer.project}")
  28. private int project;
  29. @Scheduled(cron = "0 0 * * * *")
  30. public void syncLog() {
  31. System.out.println("--开始执行日志查询同步任务--");
  32. String format = "yyyy-MM-dd HH:mm:ss";
  33. SimpleDateFormat sdf = new SimpleDateFormat(format);
  34. sdf.setTimeZone(TimeZone.getTimeZone("GMT+0"));
  35. //获取前一小时
  36. LocalDateTime now = LocalDateTime.now();
  37. LocalDateTime startTime = now.minusHours(1);
  38. NetSDKLib.NET_TIME s = new NetSDKLib.NET_TIME();
  39. s.setTime(startTime.getYear(),startTime.getMonthValue(),startTime.getDayOfMonth(),startTime.getHour(),0,0);
  40. NetSDKLib.NET_TIME e = new NetSDKLib.NET_TIME();
  41. e.setTime(startTime.getYear(),startTime.getMonthValue(),startTime.getDayOfMonth(),startTime.getHour(),59,59);
  42. synchronized(LoginModule.lock){
  43. for (NetSDKLib.LLong key : LoginModule.m_stDeviceInfo.keySet()) {
  44. //获取handle句柄和设备信息
  45. NetSDKLib.NET_DEVICEINFO_Ex value = LoginModule.m_stDeviceInfo.get(key);
  46. System.out.println("Key: " + key + ", Value: " + value);
  47. //查询逻辑
  48. NetSDKLib.NET_IN_FIND_RECORD_PARAM inParam = new NetSDKLib.NET_IN_FIND_RECORD_PARAM();
  49. inParam.emType = NetSDKLib.EM_NET_RECORD_TYPE.NET_RECORD_ACCESSCTLCARDREC_EX;
  50. //查询条件
  51. NetSDKLib.FIND_RECORD_ACCESSCTLCARDREC_CONDITION_EX condition = new NetSDKLib.FIND_RECORD_ACCESSCTLCARDREC_CONDITION_EX();
  52. condition.bTimeEnable = 1;
  53. condition.stStartTime = s;
  54. condition.stEndTime = e;
  55. inParam.pQueryCondition = new Memory(condition.size());
  56. ToolKits.SetStructDataToPointer(condition, inParam.pQueryCondition, 0);
  57. NetSDKLib.NET_OUT_FIND_RECORD_PARAM outParam = new NetSDKLib.NET_OUT_FIND_RECORD_PARAM();
  58. boolean f = LoginModule.netsdk.CLIENT_FindRecord(key,inParam,outParam,5000);
  59. if (!f) {
  60. System.err.println("查询日志失败1!");
  61. return;
  62. }
  63. System.out.println("CLIENT_FindRecord success!");
  64. int max = 100;
  65. NetSDKLib.NET_IN_FIND_NEXT_RECORD_PARAM inNextParam = new NetSDKLib.NET_IN_FIND_NEXT_RECORD_PARAM();
  66. inNextParam.lFindeHandle = outParam.lFindeHandle;
  67. inNextParam.nFileCount = max;
  68. NetSDKLib.NET_OUT_FIND_NEXT_RECORD_PARAM outNextParam = new NetSDKLib.NET_OUT_FIND_NEXT_RECORD_PARAM();
  69. outNextParam.nMaxRecordNum = max;
  70. NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARDREC[] rets = new NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARDREC[max];
  71. for (int i = 0; i < max; i++) {
  72. rets[i] = new NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARDREC();
  73. }
  74. outNextParam.pRecordList = new Memory(rets[0].size() * max);
  75. ToolKits.SetStructArrToPointerData(rets, outNextParam.pRecordList);
  76. boolean f2 = LoginModule.netsdk.CLIENT_FindNextRecord(inNextParam, outNextParam, 5000);
  77. if (!f2) {
  78. System.err.println("查询日志失败2!");
  79. return;
  80. }
  81. System.out.println("CLIENT_FindNextRecord success!");
  82. ToolKits.GetPointerDataToStructArr(outNextParam.pRecordList, rets);
  83. System.out.println("开始时间:"+s.toStringTimeEx() + "结束时间:"+e.toStringTimeEx());
  84. System.out.println("查询到的结果数量:"+outNextParam.nRetRecordNum+"最大数量:"+outNextParam.nMaxRecordNum);
  85. for (int i = 0; i < outNextParam.nRetRecordNum; i++) {
  86. String szCardName = new String(rets[i].szCardName);
  87. String openMethod = Res.string().getOpenMethods(rets[i].emMethod);
  88. System.out.println("人名:" + szCardName + "时间:" + rets[i].stuTime.toStringTimeEx() + "开门方式" + openMethod);
  89. System.out.println("开门方向:"+rets[i].emDirection);
  90. //bStatus 开门结果
  91. GateOplog opLog = new GateOplog();
  92. opLog.setGateName("");
  93. opLog.setProjectId(project);
  94. opLog.setUserName(szCardName);
  95. opLog.setEventType(openMethod);
  96. if (rets[i].emDirection == NetSDKLib.NET_ENUM_DIRECTION_ACCESS_CTL.NET_ENUM_DIRECTION_ENTRY) {
  97. opLog.setStatus("进门");
  98. } else if (rets[i].emDirection == NetSDKLib.NET_ENUM_DIRECTION_ACCESS_CTL.NET_ENUM_DIRECTION_EXIT) {
  99. opLog.setStatus("出门");
  100. } else {
  101. opLog.setStatus("");
  102. }
  103. opLog.setSerialNum(new String(value.sSerialNumber));
  104. Date d = new Date();
  105. try {
  106. d = sdf.parse(rets[i].stuTime.toStringTimeEx());
  107. }catch (ParseException excep) {
  108. System.err.println("解析时间发生错误: " + excep.getMessage());
  109. }
  110. opLog.setCreatedTime(d);
  111. opLog.setCheckType("");
  112. byte isDelete = 0;
  113. opLog.setIsDelete(isDelete);
  114. gateOplogMapper.insert(opLog);
  115. }
  116. //断开查询句柄
  117. LoginModule.netsdk.CLIENT_FindRecordClose(outParam.lFindeHandle);
  118. System.out.println("查询结束");
  119. }
  120. }
  121. System.out.println("--结束执行日志查询同步任务--");
  122. }
  123. }