Browse Source

设备提交人员记录到服务器

M墨鱼—_mo 5 years ago
parent
commit
b526c0125e

+ 8 - 0
common/pom.xml

@@ -137,6 +137,14 @@
             <artifactId>aliyun-sdk-oss</artifactId>
             <version>3.1.0</version>
         </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-web</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-web</artifactId>
+        </dependency>
         <!-- aliyun oss end -->
     </dependencies>
 

+ 2 - 0
common/src/main/java/com/jpsoft/smart/modules/base/dao/PersonDeviceLogDAO.java

@@ -1,5 +1,6 @@
 package com.jpsoft.smart.modules.base.dao;
 
+import com.jpsoft.smart.modules.base.entity.PersonDeviceLog;
 import org.springframework.stereotype.Repository;
 
 /**
@@ -10,4 +11,5 @@ import org.springframework.stereotype.Repository;
 public interface PersonDeviceLogDAO {
 
 
+    void insert(PersonDeviceLog personDeviceLog);
 }

+ 2 - 1
common/src/main/java/com/jpsoft/smart/modules/base/entity/PersonDeviceLog.java

@@ -5,6 +5,7 @@ import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 
 import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.util.Date;
 
 /**
@@ -32,7 +33,7 @@ public class PersonDeviceLog {
      * 人员id
      */
     @ApiModelProperty(value = "人员id")
-    private String personId;
+    private Integer personId;
 
     /**
      * 记录温度

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

@@ -2,7 +2,9 @@ package com.jpsoft.smart.modules.base.service;
 
 import cn.hutool.json.JSONObject;
 
+import java.io.IOException;
 import java.math.BigDecimal;
+import java.util.Date;
 
 /**
  * @author 墨鱼_mo
@@ -18,5 +20,5 @@ public interface PersonDeviceLogService {
      * @param libMatInfoListJson
      * @param matchPersonInfo
      */
-    void deviceInsertLog(String deviceNo, BigDecimal temperature, JSONObject faceImageJson, JSONObject libMatInfoListJson, JSONObject matchPersonInfo);
+    void deviceInsertLog(String deviceNo, BigDecimal temperature, JSONObject faceImageJson, JSONObject libMatInfoListJson, JSONObject matchPersonInfo, Date date) ;
 }

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

@@ -1,17 +1,29 @@
 package com.jpsoft.smart.modules.base.service.impl;
 
 import cn.hutool.json.JSONObject;
+import com.jpsoft.smart.config.OSSConfig;
 import com.jpsoft.smart.modules.base.dao.AlarmInfoDAO;
 import com.jpsoft.smart.modules.base.dao.PersonDeviceLogDAO;
 import com.jpsoft.smart.modules.base.entity.PersonDeviceLog;
 import com.jpsoft.smart.modules.base.service.PersonDeviceLogService;
+import com.jpsoft.smart.modules.common.utils.BASE64DecodedMultipartFile;
+import com.jpsoft.smart.modules.common.utils.Base64;
 import com.jpsoft.smart.modules.common.utils.IApiUtil;
+import com.jpsoft.smart.modules.common.utils.OSSUtil;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.multipart.MultipartFile;
+import sun.misc.BASE64Decoder;
 
 import javax.annotation.Resource;
+import javax.xml.crypto.Data;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
 import java.math.BigDecimal;
+import java.util.Date;
 import java.util.UUID;
 
 /**
@@ -22,11 +34,14 @@ import java.util.UUID;
 @Component(value="personDeviceLogService")
 public class PersonDeviceLogServiceImpl implements PersonDeviceLogService {
 
+    @Autowired
+    private OSSConfig ossConfig;
+
     @Resource(name="personDeviceLogDAO")
     private PersonDeviceLogDAO personDeviceLogDAO;
 
-    @Override
-    public void deviceInsertLog(String deviceNo, BigDecimal temperature, JSONObject faceImageJson, JSONObject libMatInfoListJson, JSONObject matchPersonInfo) {
+    /*@Override
+    public void deviceInsertLog(String deviceNo, BigDecimal temperature, JSONObject faceImageJson, JSONObject libMatInfoListJson, JSONObject matchPersonInfo, Data data) {
         PersonDeviceLog personDeviceLog = new PersonDeviceLog();
         personDeviceLog.setId(UUID.randomUUID().toString());
         personDeviceLog.setDeviceNo(deviceNo);
@@ -35,10 +50,60 @@ public class PersonDeviceLogServiceImpl implements PersonDeviceLogService {
         personDeviceLog.setMatchStatus(libMatInfoListJson.getInt("MatchStatus"));
         personDeviceLog.setMatchMsg(IApiUtil.getMatchMsg(libMatInfoListJson.getInt("MatchStatus")));
         personDeviceLog.setMatchFaceId(libMatInfoListJson.getInt("MatchFaceID"));
+        personDeviceLog.setRecordTime(data);
+
+
+    }*/
+
+    @Override
+    public void deviceInsertLog(String deviceNo, BigDecimal temperature, JSONObject faceImageJson, JSONObject libMatInfoListJson, JSONObject matchPersonInfo, Date date)  {
+        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;
+                }
+            }
 
-      //  long timestamp = jsonObject.getLong("Timestamp");
-      //  personDeviceLog.setRecordTime();
+            ByteArrayInputStream inputStream = new ByteArrayInputStream(imgData);
 
 
+
+             retFileUrl = OSSUtil.upload(ossConfig,"/devicePersonLog",faceImageJson.getStr("Name"), inputStream);
+        }catch (Exception e){
+            e.printStackTrace();
+        }
+
+
+        PersonDeviceLog personDeviceLog = new PersonDeviceLog();
+        personDeviceLog.setId(UUID.randomUUID().toString());
+        personDeviceLog.setDeviceNo(deviceNo);
+        if (!StringUtils.isBlank(matchPersonInfo.getStr("PersonCode"))){
+            personDeviceLog.setPersonId(matchPersonInfo.getInt("PersonCode"));
+        }else {
+            personDeviceLog.setPersonId(0);
+        }
+
+        personDeviceLog.setTemperature(temperature);
+        if (StringUtils.isNotBlank(libMatInfoListJson.getStr("MatchStatus"))){
+            System.out.println();
+            personDeviceLog.setMatchStatus(libMatInfoListJson.getInt("MatchStatus"));
+            personDeviceLog.setMatchMsg(IApiUtil.getMatchMsg(libMatInfoListJson.getInt("MatchStatus")));
+            personDeviceLog.setMatchFaceId(libMatInfoListJson.getInt("MatchFaceID"));
+        }else {
+            personDeviceLog.setMatchStatus(0);
+            personDeviceLog.setMatchFaceId(0);
+        }
+
+        personDeviceLog.setRecordTime(date);
+        personDeviceLog.setDelFlag(false);
+        personDeviceLog.setCreateTime(new Date());
+        personDeviceLog.setFaceImage(retFileUrl);
+        personDeviceLogDAO.insert(personDeviceLog);
     }
 }

+ 95 - 0
common/src/main/java/com/jpsoft/smart/modules/common/utils/BASE64DecodedMultipartFile.java

@@ -0,0 +1,95 @@
+package com.jpsoft.smart.modules.common.utils;
+
+import org.springframework.web.multipart.MultipartFile;
+import sun.misc.BASE64Decoder;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * @author 墨鱼_mo
+ * @date 2020-3-15 16:06
+ */
+public class BASE64DecodedMultipartFile implements MultipartFile {
+
+    private final byte[] imgContent;
+    private final String header;
+
+    public BASE64DecodedMultipartFile(byte[] imgContent, String header) {
+        this.imgContent = imgContent;
+        this.header = header.split(";")[0];
+    }
+
+
+
+    @Override
+    public String getName() {
+        return null;
+    }
+
+    @Override
+    public String getOriginalFilename() {
+        return null;
+    }
+
+    @Override
+    public String getContentType() {
+        return null;
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return false;
+    }
+
+    @Override
+    public long getSize() {
+        return 0;
+    }
+
+    @Override
+    public byte[] getBytes() throws IOException {
+        return new byte[0];
+    }
+
+    @Override
+    public InputStream getInputStream() throws IOException {
+        return null;
+    }
+
+    @Override
+    public void transferTo(File file) throws IOException, IllegalStateException {
+
+    }
+
+    /**
+     * base64转MultipartFile文件
+     *
+     * @param base64
+     * @return
+     */
+    public static MultipartFile base64ToMultipart(String base64) {
+        try {
+            String[] baseStrs = base64.split(",");
+
+            System.out.println("base64:"+base64);
+            BASE64Decoder decoder = new BASE64Decoder();
+            byte[] b = new byte[0];
+            b = decoder.decodeBuffer(baseStrs[1]);
+
+            for (int i = 0; i < b.length; ++i) {
+                if (b[i] < 0) {
+                    b[i] += 256;
+                }
+            }
+
+            return new BASE64DecodedMultipartFile(b, baseStrs[0]);
+        } catch (IOException e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+
+}

+ 4 - 0
common/src/main/java/com/jpsoft/smart/modules/common/utils/IApiUtil.java

@@ -3,6 +3,7 @@ package com.jpsoft.smart.modules.common.utils;
 import cn.hutool.http.HttpRequest;
 import com.alibaba.fastjson.JSONObject;
 import org.springframework.stereotype.Component;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.util.HashMap;
 
@@ -99,5 +100,8 @@ public class IApiUtil {
 
         return matchMsg;
     }
+
+
+
 }
 

+ 51 - 0
common/src/main/resources/mapper/base/PersonDeviceLog.xml

@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<!-- namespace必须指向DAO接口 -->
+<mapper namespace="com.jpsoft.smart.modules.base.dao.PersonDeviceLogDAO">
+    <resultMap id="PersonDeviceLogMap" type="PersonDeviceLog">
+        <id property="id" column="id_" />
+        <result property="createBy" column="create_by" />
+        <result property="createTime" column="create_time" />
+        <result property="updateBy" column="update_by" />
+        <result property="updateTime" column="update_time" />
+        <result property="delFlag" column="del_flag" />
+        <result property="deviceNo" column="device_no" />
+        <result property="personId" column="person_id" />
+        <result property="temperature" column="temperature_" />
+        <result property="matchStatus" column="match_status" />
+        <result property="matchMsg" column="match_msg" />
+        <result property="matchFaceId" column="match_face_id" />
+        <result property="faceImage" column="face_image" />
+        <result property="recordTime" column="record_time" />
+    </resultMap>
+    <insert id="insert" parameterType="PersonDeviceLog">
+        <!--
+        <selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
+            select sys_guid() from dual
+        </selectKey>
+        -->
+        <![CDATA[
+		insert into base_person_device_log
+	    (id_,create_by,create_time,update_by,update_time,del_flag,device_no,person_id,temperature_,match_status,match_msg,match_face_id,face_image,record_time)
+		values
+		(
+#{id,jdbcType=VARCHAR}
+,#{createBy,jdbcType=VARCHAR}
+,#{createTime,jdbcType= TIMESTAMP }
+,#{updateBy,jdbcType=VARCHAR}
+,#{updateTime,jdbcType= TIMESTAMP }
+,#{delFlag,jdbcType= NUMERIC }
+,#{deviceNo,jdbcType=VARCHAR}
+,#{personId,jdbcType=VARCHAR}
+,#{temperature,jdbcType=NUMERIC}
+,#{matchStatus,jdbcType=INTEGER}
+,#{matchMsg,jdbcType=VARCHAR}
+,#{matchFaceId,jdbcType= INTEGER}
+,#{faceImage,jdbcType=VARCHAR}
+,#{recordTime,jdbcType=TIMESTAMP}
+		)
+	]]>
+    </insert>
+
+</mapper>

+ 1 - 1
lapi/pom.xml

@@ -9,7 +9,7 @@
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <artifactId>lapi</artifactId>
-    <packaging>com.jpsoft.smart.lapi</packaging>
+    <packaging>jar</packaging>
     <properties>
         <netty-all.version>4.1.6.Final</netty-all.version>
     </properties>

+ 1 - 0
lapi/src/main/java/com/jpsoft/smart/LapiApplication.java

@@ -3,6 +3,7 @@ package com.jpsoft.smart;
 import com.jpsoft.smart.lapi.handler.HeartReportHandler;
 import com.jpsoft.smart.lapi.handler.ParseRequestHandler;
 import com.jpsoft.smart.lapi.handler.PersonVerificationHandler;
+import com.jpsoft.smart.lapi.handler.TextDecodeHandler;
 import io.netty.bootstrap.ServerBootstrap;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelFuture;

+ 30 - 8
lapi/src/main/java/com/jpsoft/smart/lapi/handler/PersonVerificationHandler.java

@@ -7,6 +7,7 @@ import io.netty.channel.ChannelHandler;
 import io.netty.channel.ChannelHandlerContext;
 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.stereotype.Component;
 
@@ -24,17 +25,26 @@ public class PersonVerificationHandler extends SimpleChannelInboundHandler<Perso
 
     @Override
     protected void channelRead0(ChannelHandlerContext channelHandlerContext, PersonVerification personVerification) throws Exception {
-        log.warn(personVerification.toString());
+     //   log.warn(personVerification.toString());
         JSONObject jsonObject = personVerification.getJsonObject();
 
-        log.warn("DeviceCode"+jsonObject.getStr("DeviceCode"));
-        log.warn(jsonObject.toString());
+    //    log.warn("DeviceCode"+jsonObject.getStr("DeviceCode"));
+     //   log.warn(jsonObject.toString());
       //  JSONObject faceJson = jsonObject.get("FaceInfoList");
-        log.warn("FaceInfoList" +jsonObject.getStr("FaceInfoList"));
+     //   log.warn("FaceInfoList" +jsonObject.getStr("FaceInfoList"));
 
 
         JSONObject faceJson = new JSONObject(jsonObject.getStr("FaceInfoList").substring(1,jsonObject.getStr("FaceInfoList").length()-1));
-        log.warn(faceJson.getStr("FaceImage"));
+      //  log.warn(faceJson.getStr("FaceImage"));
+
+        long timestamp = faceJson.getLong("Timestamp");
+
+        Date date = new Date(timestamp);
+
+     //   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+
+
 
         //设备序列号
         String deviceNo = jsonObject.getStr("DeviceCode");
@@ -45,14 +55,26 @@ public class PersonVerificationHandler extends SimpleChannelInboundHandler<Perso
         //人员抓拍信息
         JSONObject faceImageJson = faceJson.getJSONObject("FaceImage");
 
+        //log.warn("jsonObject========"+jsonObject);
+      //  log.warn("LibMatInfoList:----"+ jsonObject.getStr("LibMatInfoList"));
         //匹配状态信息
-        JSONObject libMatInfoListJson = new JSONObject(jsonObject.getStr("LibMatInfoList").substring(1,jsonObject.getStr("LibMatInfoList").length()-1));
+        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));
+        }
+
 
         //匹配人员信息
-        JSONObject matchPersonInfo = libMatInfoListJson.getJSONObject("MatchPersonInfo");
+        JSONObject matchPersonInfo = new JSONObject();
+        if (StringUtils.isNotBlank(libMatInfoListJson.getStr("MatchPersonInfo"))){
+            matchPersonInfo = libMatInfoListJson.getJSONObject("MatchPersonInfo");
+        }
+       // log.warn("MatchPersonInfo==="+libMatInfoListJson.getStr("MatchPersonInfo"));
+
 
+      //  log.warn("准备写入");
 
-        personDeviceLogService.deviceInsertLog(deviceNo,temperature,faceImageJson,libMatInfoListJson,matchPersonInfo);
+        personDeviceLogService.deviceInsertLog(deviceNo,temperature,faceImageJson,libMatInfoListJson,matchPersonInfo,date);