Explorar o código

数据接收端修改,门禁记录推送时增加返回报文。

zhengqiang %!s(int64=5) %!d(string=hai) anos
pai
achega
a4efde2f3b

+ 7 - 9
lapi/src/main/java/com/jpsoft/smart/LapiApplication.java

@@ -9,8 +9,7 @@ import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelInitializer;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.nio.NioServerSocketChannel;
-import io.netty.handler.codec.http.HttpObjectAggregator;
-import io.netty.handler.codec.http.HttpServerCodec;
+import io.netty.handler.codec.http.*;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -51,13 +50,12 @@ public class LapiApplication implements CommandLineRunner {
                 .childHandler(new ChannelInitializer(){
                     @Override
                     protected void initChannel(Channel channel) throws Exception {
-//				channel.pipeline().addLast(new TextDecodeHandler());
-//				channel.pipeline().addLast(new HttpRequestDecoder());
-                        channel.pipeline().addLast(new HttpServerCodec());
-                        channel.pipeline().addLast(new HttpObjectAggregator(2048*10000));
-                        channel.pipeline().addLast(new ParseHttpHandler());
-                        channel.pipeline().addLast(heartReportHandler);
-                        channel.pipeline().addLast(personVerificationHandler);
+        //				channel.pipeline().addLast(new TextDecodeHandler());
+//                        channel.pipeline().addLast(new HttpServerCodec());
+//                        channel.pipeline().addLast(new HttpObjectAggregator(2048*10000));
+//                        channel.pipeline().addLast(new ParseHttpHandler());
+//                        channel.pipeline().addLast(heartReportHandler);
+//                        channel.pipeline().addLast(personVerificationHandler);
                     }
                 });
 

+ 1 - 2
lapi/src/main/java/com/jpsoft/smart/lapi/LapiTcpServer.java

@@ -47,14 +47,13 @@ public class LapiTcpServer {
 //                        channel.pipeline().addLast(new ParseResponseHandler());
 //                        channel.pipeline().addLast(new HttpClientCodec());
 //                        channel.pipeline().addLast(new HttpServerCodec());
+                        channel.pipeline().addLast(new HttpEncoder());
                         channel.pipeline().addLast(new HttpDecoder());
                         channel.pipeline().addLast("aggregator", new HttpObjectAggregator(10 * 1024 * 1024));
                         channel.pipeline().addLast(new ParseHttpHandler());
                         channel.pipeline().addLast(heartReportHandler);
                         channel.pipeline().addLast(personVerificationHandler);
                         channel.pipeline().addLast(responseDataHandler);
-                        channel.pipeline().addLast(new HttpRequestEncoder());
-                        channel.pipeline().addLast(new HttpResponseEncoder());
                     }
                 });
 

+ 30 - 0
lapi/src/main/java/com/jpsoft/smart/lapi/dto/PersonVerificationResponseInfo.java

@@ -0,0 +1,30 @@
+package com.jpsoft.smart.lapi.dto;
+
+import lombok.Data;
+
+@Data
+public class PersonVerificationResponseInfo {
+    private ResponseClass Response;
+
+    public PersonVerificationResponseInfo(){
+        Response = new ResponseClass();
+    }
+
+    @Data
+    public class ResponseClass{
+        private String ResponseURL;
+        private int StatusCode;
+        private String StatusString;
+        private DataClass Data;
+
+        public ResponseClass() {
+            Data = new DataClass();
+        }
+    }
+
+    @Data
+    public class DataClass{
+        private int RecordID;
+        private String Time;
+    }
+}

+ 40 - 0
lapi/src/main/java/com/jpsoft/smart/lapi/handler/HttpEncoder.java

@@ -0,0 +1,40 @@
+package com.jpsoft.smart.lapi.handler;
+
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.MessageToMessageEncoder;
+import io.netty.handler.codec.http.HttpRequest;
+import io.netty.handler.codec.http.HttpResponse;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+/**
+ * @Description
+ * @Date 2019/5/23 9:48
+ * @Created by x06086
+ */
+public class HttpEncoder<I> extends MessageToMessageEncoder {
+
+    /**
+     * request编码器
+     */
+    private UnvHttpRequestEncoder httpRequestEncoder = new UnvHttpRequestEncoder();
+
+    /**日志对象 */
+    private static org.slf4j.Logger LOGGER = LoggerFactory.getLogger(HttpEncoder.class);
+
+    /**
+     * response编码器
+     */
+    private UnvHttpResponseEncoder httpResponseEncoder = new UnvHttpResponseEncoder();
+
+
+    @Override
+    protected void encode(ChannelHandlerContext ctx, Object msg, List out) throws Exception {
+        if (msg instanceof HttpRequest) {
+            httpRequestEncoder.encode(ctx, msg, out);
+        } else if (msg instanceof HttpResponse) {
+            httpResponseEncoder.encode(ctx, msg, out);
+        }
+    }
+}

+ 46 - 4
lapi/src/main/java/com/jpsoft/smart/lapi/handler/PersonVerificationHandler.java

@@ -1,8 +1,11 @@
 package com.jpsoft.smart.lapi.handler;
 
+import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
 import com.jpsoft.smart.config.OSSConfig;
 import com.jpsoft.smart.lapi.dto.PersonVerification;
+import com.jpsoft.smart.lapi.dto.PersonVerificationResponseInfo;
 import com.jpsoft.smart.modules.base.entity.PersonInfo;
 import com.jpsoft.smart.modules.base.service.PersonDeviceFilterLogService;
 import com.jpsoft.smart.modules.base.service.PersonDeviceLogService;
@@ -11,11 +14,16 @@ 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.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 import io.netty.channel.ChannelHandler;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.SimpleChannelInboundHandler;
+import io.netty.handler.codec.http.*;
+import io.netty.util.CharsetUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
+import org.joda.time.DateTime;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Component;
@@ -51,6 +59,13 @@ public class PersonVerificationHandler extends SimpleChannelInboundHandler<Perso
     @Autowired
     private SysLogService sysLogService;
 
+    private  final String X_FRAME_OPTIONS = "X-Frame-Options";
+    private  final String SAMEORIGIN = "SAMEORIGIN";
+    private  final String JSON_PLAIN = "application/json";
+    private  final String CONTENT_LENGTH = "Content-Length";
+    private  final String CONTENT_TYPE = "Content-Type";
+    private  final String CONNECTION = "Connection";
+
     @Override
     protected void channelRead0(ChannelHandlerContext ctx, PersonVerification personVerification) throws Exception {
         new Thread(()->{
@@ -88,10 +103,7 @@ public class PersonVerificationHandler extends SimpleChannelInboundHandler<Perso
         JSONObject faceImageJson = faceJson.getJSONObject("FaceImage");
 
         //匹配状态信息
-        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 libMatInfoListJson = jsonObject.getJSONArray("LibMatInfoList").getJSONObject(0);
 
         //匹配人员信息
         JSONObject matchPersonInfo = new JSONObject();
@@ -100,12 +112,15 @@ public class PersonVerificationHandler extends SimpleChannelInboundHandler<Perso
         }
 
         log.warn("人员记录写入");
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
         HashMap map = new HashMap<>();
         map.put("deviceNo",deviceNo);
         map.put("temperature",temperature);
         map.put("libMatInfoListJson",libMatInfoListJson);
         map.put("matchPersonInfo",matchPersonInfo);
+        map.put("recordTime",sdf.format(date));
+
         log.warn(map.toString());
 
         SysLog sysLog = new SysLog();
@@ -164,5 +179,32 @@ public class PersonVerificationHandler extends SimpleChannelInboundHandler<Perso
         }
 
         log.warn("===人员记录异步处理 end===");
+
+        //返回数据
+        try {
+            log.warn("===人员记录回包处理 end===");
+            PersonVerificationResponseInfo responseInfo = new PersonVerificationResponseInfo();
+
+            responseInfo.getResponse().setResponseURL("/LAPI/V1.0/PACS/Controller/Event/Notifications");
+            responseInfo.getResponse().setStatusCode(0);
+            responseInfo.getResponse().setStatusString("Succeed");
+            responseInfo.getResponse().getData().setRecordID(libMatInfoListJson.getInt("ID"));
+            responseInfo.getResponse().getData().setTime(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
+
+            String json = JSONUtil.toJsonStr(responseInfo);
+
+            FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(json.getBytes("UTF-8")));
+            response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
+            response.headers().set(CONTENT_TYPE, JSON_PLAIN);
+            response.headers().set(CONNECTION, HttpHeaderValues.CLOSE);
+            response.headers().set(X_FRAME_OPTIONS, SAMEORIGIN);
+
+            ctx.channel().writeAndFlush(response);
+
+            log.warn("===人员记录回包结束 end===");
+        }
+        catch (Exception ex){
+            log.error(ex.getMessage(),ex);
+        }
     }
 }

+ 19 - 0
lapi/src/main/java/com/jpsoft/smart/lapi/handler/UnvHttpRequestEncoder.java

@@ -0,0 +1,19 @@
+package com.jpsoft.smart.lapi.handler;
+
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.http.HttpRequestEncoder;
+
+import java.util.List;
+
+/**
+ * @Description 放大了netty请求编码器中编码方法的访问权限
+ * @Date 2019/5/23 10:08
+ * @Created by x06086
+ */
+public class UnvHttpRequestEncoder extends HttpRequestEncoder {
+
+    @Override
+    public void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
+        super.encode(ctx, msg, out);
+    }
+}

+ 19 - 0
lapi/src/main/java/com/jpsoft/smart/lapi/handler/UnvHttpResponseEncoder.java

@@ -0,0 +1,19 @@
+package com.jpsoft.smart.lapi.handler;
+
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.http.HttpResponseEncoder;
+
+import java.util.List;
+
+/**
+ * @Description 自定义的HttpResponseEncoder,就是放大了netty编码器中的访问权限
+ * @Date 2019/5/23 10:04
+ * @Created by x06086
+ */
+public class UnvHttpResponseEncoder extends HttpResponseEncoder {
+
+    @Override
+    public void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
+        super.encode(ctx, msg, out);
+    }
+}