Browse Source

优化数据接收逻辑。

tomatozq 5 years ago
parent
commit
83d28de1e2

+ 1 - 1
common/src/main/java/com/jpsoft/smart/modules/base/service/PersonDeviceFilterLogService.java

@@ -28,7 +28,7 @@ public interface PersonDeviceFilterLogService {
      * @param libMatInfoListJson
      * @param matchPersonInfo
      */
-    void deviceInsertFilterLog(String deviceNo, BigDecimal temperature, JSONObject faceImageJson, JSONObject libMatInfoListJson, JSONObject matchPersonInfo, Date date,Integer maskFlag);
+    void deviceInsertFilterLog(String deviceNo, BigDecimal temperature, JSONObject faceImageJson, JSONObject libMatInfoListJson, JSONObject matchPersonInfo, Date date,Integer maskFlag,String faceImageUrl);
 
     Page<PersonDeviceFilterLog> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize, boolean count, List<Sort> sortList);
 

+ 1 - 1
common/src/main/java/com/jpsoft/smart/modules/base/service/PersonDeviceLogService.java

@@ -29,7 +29,7 @@ public interface PersonDeviceLogService {
      * @param libMatInfoListJson
      * @param matchPersonInfo
      */
-    void deviceInsertLog(String deviceNo, BigDecimal temperature, JSONObject faceImageJson, JSONObject libMatInfoListJson, JSONObject matchPersonInfo, Date date,Integer maskFlag);
+    void deviceInsertLog(String deviceNo, BigDecimal temperature, JSONObject faceImageJson, JSONObject libMatInfoListJson, JSONObject matchPersonInfo, Date date,Integer maskFlag,String faceImageUrl);
     Page<PersonDeviceLog> pageSearch(Map<String, Object> searchParams, int pageNum, int pageSize,boolean count, List<Sort> sortList);
     List<PersonDeviceLog> findByPersonAndDate(Long personId,Date startDate,Date endDate);
     Page<PersonDeviceLog> simplePageSearch(Map<String, Object> searchParams, int pageNum, int pageSize,boolean count, List<Sort> sortList);

+ 3 - 27
common/src/main/java/com/jpsoft/smart/modules/base/service/impl/PersonDeviceFilterLogServiceImpl.java

@@ -63,33 +63,9 @@ public class PersonDeviceFilterLogServiceImpl implements PersonDeviceFilterLogSe
     }
 
     @Override
-    @Async
-    public void deviceInsertFilterLog(String deviceNo, BigDecimal temperature, JSONObject faceImageJson, JSONObject libMatInfoListJson, JSONObject matchPersonInfo, Date date,Integer maskFlag) {
+    public void deviceInsertFilterLog(String deviceNo, BigDecimal temperature, JSONObject faceImageJson, JSONObject libMatInfoListJson, JSONObject matchPersonInfo, Date date,Integer maskFlag,String faceImageUrl) {
 
         try {
-
-
-            String retFileUrl = "";
-            try {
-
-                BASE64Decoder decoder = new BASE64Decoder();
-
-                byte[] imgData = decoder.decodeBuffer(faceImageJson.getStr("Data"));
-
-                for (int i = 0; i < imgData.length; ++i) {
-                    if (imgData[i] < 0) {// 调整异常数据
-                        imgData[i] += 256;
-                    }
-                }
-
-                ByteArrayInputStream inputStream = new ByteArrayInputStream(imgData);
-
-
-                retFileUrl = OSSUtil.upload(ossConfig, "/devicePersonLog", faceImageJson.getStr("Name"), inputStream);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-
             //记录的时间段
             String recordTimeZons = DateUtil.format(date, "yyyy-MM-dd-HH-mm");
             String timeZones = TimeZonesUtil.getUpperTimeZone(recordTimeZons);
@@ -109,7 +85,7 @@ public class PersonDeviceFilterLogServiceImpl implements PersonDeviceFilterLogSe
                     personDeviceFilterLog.setUpdateTime(new Date());
                     personDeviceFilterLog.setTemperature(temperature);
                     personDeviceFilterLog.setRecordTime(date);
-                    personDeviceFilterLog.setFaceImage(retFileUrl);
+                    personDeviceFilterLog.setFaceImage(faceImageUrl);
                     personDeviceFilterLog.setCreateTime(new Date());
 
                     if (StringUtils.isNotBlank(libMatInfoListJson.getStr("MatchStatus"))) {
@@ -144,7 +120,7 @@ public class PersonDeviceFilterLogServiceImpl implements PersonDeviceFilterLogSe
                         personDeviceFilterLog1.setMatchStatus(0);
                         personDeviceFilterLog1.setMatchFaceId(0);
                     }
-                    personDeviceFilterLog1.setFaceImage(retFileUrl);
+                    personDeviceFilterLog1.setFaceImage(faceImageUrl);
                     personDeviceFilterLog1.setRecordTime(date);
                     personDeviceFilterLog1.setTimeZones(timeZones);
                     personDeviceFilterLog1.setDelFlag(false);

+ 2 - 32
common/src/main/java/com/jpsoft/smart/modules/base/service/impl/PersonDeviceLogServiceImpl.java

@@ -68,34 +68,8 @@ public class PersonDeviceLogServiceImpl implements PersonDeviceLogService {
     @Resource(name = "deviceInfoDAO")
     private DeviceInfoDAO deviceInfoDAO;
 
-
     @Override
-    @Async
-    public void deviceInsertLog(String deviceNo, BigDecimal temperature, JSONObject faceImageJson, JSONObject libMatInfoListJson, JSONObject matchPersonInfo, Date date,Integer maskFlag) {
-        String retFileUrl = "";
-        try {
-
-            if (StringUtils.isNotBlank(faceImageJson.getStr("Data"))){
-                BASE64Decoder decoder = new BASE64Decoder();
-
-                byte[] imgData = decoder.decodeBuffer(faceImageJson.getStr("Data"));
-
-                for (int i = 0; i < imgData.length; ++i) {
-                    if (imgData[i] < 0) {// 调整异常数据
-                        imgData[i] += 256;
-                    }
-                }
-
-                ByteArrayInputStream inputStream = new ByteArrayInputStream(imgData);
-
-
-                retFileUrl = OSSUtil.upload(ossConfig, "/devicePersonLog", faceImageJson.getStr("Name"), inputStream);
-            }
-
-        } catch (Exception e) {
-            log.error("保存图片路径错误" + e.getMessage());
-        }
-
+    public void deviceInsertLog(String deviceNo, BigDecimal temperature, JSONObject faceImageJson, JSONObject libMatInfoListJson, JSONObject matchPersonInfo, Date date,Integer maskFlag,String faceImageUrl) {
         PersonInfo personInfo = LApiUtil.getPersonCode(libMatInfoListJson, matchPersonInfo);
       //  PersonInfo peronInfoReturn = personInfoService.addPersonForCompany(personInfo,deviceNo);
       //  Integer personId = LApiUtil.getPersonCode(libMatInfoListJson, matchPersonInfo);
@@ -121,7 +95,7 @@ public class PersonDeviceLogServiceImpl implements PersonDeviceLogService {
         personDeviceLog.setRecordTime(date);
         personDeviceLog.setDelFlag(false);
         personDeviceLog.setCreateTime(new Date());
-        personDeviceLog.setFaceImage(retFileUrl);
+        personDeviceLog.setFaceImage(faceImageUrl);
         personDeviceLogDAO.insert(personDeviceLog);
 
         //人员识别成功,温度超过限定时发送模板消息给监管者
@@ -135,12 +109,8 @@ public class PersonDeviceLogServiceImpl implements PersonDeviceLogService {
                 log.error(ex.getMessage(),ex);
             }
         }).start();
-
-
-
     }
 
-
     public void sendTemperatureAlarmsyncTask(PersonDeviceLog personDeviceLog) {
 
         try {

+ 59 - 22
lapi/src/main/java/com/jpsoft/smart/lapi/handler/PersonVerificationHandler.java

@@ -1,12 +1,14 @@
 package com.jpsoft.smart.lapi.handler;
 
 import cn.hutool.json.JSONObject;
+import com.jpsoft.smart.config.OSSConfig;
 import com.jpsoft.smart.lapi.dto.PersonVerification;
 import com.jpsoft.smart.modules.base.entity.PersonInfo;
 import com.jpsoft.smart.modules.base.service.PersonDeviceFilterLogService;
 import com.jpsoft.smart.modules.base.service.PersonDeviceLogService;
 import com.jpsoft.smart.modules.business.service.WorkAttendanceService;
 import com.jpsoft.smart.modules.common.utils.LApiUtil;
+import com.jpsoft.smart.modules.common.utils.OSSUtil;
 import com.jpsoft.smart.modules.sys.entity.SysLog;
 import com.jpsoft.smart.modules.sys.service.SysLogService;
 import io.netty.channel.ChannelHandler;
@@ -15,9 +17,12 @@ import io.netty.channel.SimpleChannelInboundHandler;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Component;
+import sun.misc.BASE64Decoder;
 import sun.misc.BASE64Encoder;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
@@ -31,6 +36,8 @@ import java.util.HashMap;
 @Component
 @ChannelHandler.Sharable
 public class PersonVerificationHandler extends SimpleChannelInboundHandler<PersonVerification> {
+    @Autowired
+    private OSSConfig ossConfig;
 
     @Autowired
     private PersonDeviceLogService personDeviceLogService;
@@ -46,21 +53,27 @@ public class PersonVerificationHandler extends SimpleChannelInboundHandler<Perso
 
     @Override
     protected void channelRead0(ChannelHandlerContext ctx, PersonVerification personVerification) throws Exception {
-        log.warn("人员记录");
+        log.warn("===人员记录 begin===");
+        asyncWork(ctx,personVerification);
+        log.warn("===人员记录 end===");
+    }
 
-        JSONObject jsonObject = personVerification.getJsonObject();
+    @Async
+    public void asyncWork(ChannelHandlerContext ctx,PersonVerification personVerification) {
+        log.warn("===人员记录异步处理 begin===");
 
-       // log.warn(jsonObject.toString());
+        JSONObject jsonObject = personVerification.getJsonObject();
 
         JSONObject faceJson = new JSONObject(jsonObject.getStr("FaceInfoList").substring(1,jsonObject.getStr("FaceInfoList").length()-1));
 
         Long timestamp = faceJson.getLong("Timestamp");
+
         if (timestamp.toString().length()<13){
-             double timestampDouble = timestamp *  Math.pow(10, 13 - timestamp.toString().length());
-             timestamp =new Double(timestampDouble).longValue() ;
+            double timestampDouble = timestamp *  Math.pow(10, 13 - timestamp.toString().length());
+            timestamp =new Double(timestampDouble).longValue() ;
         }
 
-    //    log.warn(String.valueOf(timestamp));
+        //log.warn(String.valueOf(timestamp));
         Date date = new Date(timestamp);
 
         //设备序列号
@@ -78,7 +91,7 @@ public class PersonVerificationHandler extends SimpleChannelInboundHandler<Perso
         //匹配状态信息
         JSONObject libMatInfoListJson = new JSONObject();
         if (StringUtils.isNotBlank(jsonObject.getStr("LibMatInfoList").substring(1,jsonObject.getStr("LibMatInfoList").length()-1))){
-             libMatInfoListJson = new JSONObject(jsonObject.getStr("LibMatInfoList").substring(1,jsonObject.getStr("LibMatInfoList").length()-1));
+            libMatInfoListJson = new JSONObject(jsonObject.getStr("LibMatInfoList").substring(1,jsonObject.getStr("LibMatInfoList").length()-1));
         }
 
         //匹配人员信息
@@ -96,37 +109,61 @@ public class PersonVerificationHandler extends SimpleChannelInboundHandler<Perso
         map.put("matchPersonInfo",matchPersonInfo);
         log.warn(map.toString());
 
-        new Thread(()->{
-            SysLog sysLog = new SysLog();
-            sysLog.setPointcut(deviceNo);
-            sysLog.setUrl("/LAPI/V1.0/System/Event/Notification/PersonVerification");
-            sysLog.setData(map.toString());
-            sysLog.setRemark("收到人脸识别信息");
-            SocketAddress remoteAddress = ctx.channel().remoteAddress();
-            sysLog.setRemoteIp(remoteAddress.toString());
-            sysLog.setCreateTime(new Date());
-            sysLogService.insert(sysLog);
-        }).start();
+        SysLog sysLog = new SysLog();
+        sysLog.setPointcut(deviceNo);
+        sysLog.setUrl("/LAPI/V1.0/System/Event/Notification/PersonVerification");
+        sysLog.setData(map.toString());
+        sysLog.setRemark("收到人脸识别信息");
+        SocketAddress remoteAddress = ctx.channel().remoteAddress();
+        sysLog.setRemoteIp(remoteAddress.toString());
+        sysLog.setCreateTime(new Date());
+        sysLogService.insert(sysLog);
+
+        //上传人脸信息
+        String faceImageUrl = "";
+
+        try {
+            if (StringUtils.isNotBlank(faceImageJson.getStr("Data"))){
+                BASE64Decoder decoder = new BASE64Decoder();
+
+                byte[] imgData = decoder.decodeBuffer(faceImageJson.getStr("Data"));
+
+                for (int i = 0; i < imgData.length; ++i) {
+                    if (imgData[i] < 0) {// 调整异常数据
+                        imgData[i] += 256;
+                    }
+                }
+
+                ByteArrayInputStream inputStream = new ByteArrayInputStream(imgData);
+
+
+                faceImageUrl = OSSUtil.upload(ossConfig, "/devicePersonLog", faceImageJson.getStr("Name"), inputStream);
+            }
+
+        } catch (Exception e) {
+            log.error("保存图片路径错误" + e.getMessage());
+        }
 
         //记录人员记录
-        personDeviceLogService.deviceInsertLog(deviceNo,temperature,faceImageJson,libMatInfoListJson,matchPersonInfo,date,maskFlag);
+        personDeviceLogService.deviceInsertLog(deviceNo,temperature,faceImageJson,libMatInfoListJson,matchPersonInfo,date,maskFlag,faceImageUrl);
 
         //记录人员过滤记录
-        personDeviceFilterLogService.deviceInsertFilterLog(deviceNo,temperature,faceImageJson,libMatInfoListJson,matchPersonInfo,date,maskFlag);
+        personDeviceFilterLogService.deviceInsertFilterLog(deviceNo,temperature,faceImageJson,libMatInfoListJson,matchPersonInfo,date,maskFlag,faceImageUrl);
 
         //记录考勤信息
         try {
-          //  final Integer personId = LApiUtil.getPersonCode(libMatInfoListJson, matchPersonInfo);
             final PersonInfo personInfo = LApiUtil.getPersonCode(libMatInfoListJson, matchPersonInfo);
             final BigDecimal fTemperature = temperature;
             final Date recordDate = date;
 
             if(personInfo.getId()!=null && personInfo.getId()!=0L) {
-                new Thread(() -> workAttendanceService.punchIn(personInfo.getId(), fTemperature, recordDate)).start();
+                workAttendanceService.punchIn(personInfo.getId(), fTemperature, recordDate);
             }
         }
         catch (Exception ex){
             log.error(ex.getMessage(),ex);
         }
+
+        log.warn("===人员记录异步处理 end===");
     }
 }