Ver Fonte

慷慨斯GK309协议校时包/打卡包回复

vinson há 5 anos atrás
pai
commit
cc709d9fc4

+ 91 - 8
src/main/java/com/tidecloud/dataacceptance/service/impl/GK309GpsServerHandler.java

@@ -2,11 +2,8 @@ package com.tidecloud.dataacceptance.service.impl;
 
 import com.accept.client.DeviceMsgClient;
 import com.tidecloud.dataacceptance.codec.HeaderTailDelimiterFrameDecoder;
-import com.tidecloud.dataacceptance.common.CRCUtil;
-import com.tidecloud.dataacceptance.common.DateUtil;
-import com.tidecloud.dataacceptance.common.NumUtil;
+import com.tidecloud.dataacceptance.common.*;
 import com.tidecloud.dataacceptance.entity.YiTongGPSDevice;
-import com.tidecloud.dataacceptance.entity.YiTongGpsForWarnDevice;
 import com.tidecloud.dataacceptance.service.HexBinaryAcceptanceHandlerAdapter;
 import com.tidecloud.dataacceptance.util.FileUtils;
 import io.netty.bootstrap.ServerBootstrap;
@@ -16,8 +13,7 @@ import io.netty.channel.*;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.SocketChannel;
 import io.netty.channel.socket.nio.NioServerSocketChannel;
-import io.netty.util.concurrent.Future;
-import io.netty.util.concurrent.GenericFutureListener;
+import org.apache.commons.lang.ArrayUtils;
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -27,6 +23,7 @@ import org.springframework.context.annotation.Scope;
 import org.springframework.stereotype.Component;
 
 import javax.xml.bind.DatatypeConverter;
+import java.util.Calendar;
 import java.util.Date;
 
 /**
@@ -57,6 +54,9 @@ public class GK309GpsServerHandler extends HexBinaryAcceptanceHandlerAdapter {
     private static final byte STATUS_MSG = 0x13;// 心跳,状态信息包  (必须回复)
     private static final byte LOCATION_MSG = 0x10;// 一般GPS 位置信息上传
     private static final byte CORRECT_TIME_MSG = 0x1F;// 校验时间
+    private static final byte SIGN_IN_MSG = (byte)0xB0;// 打卡
+
+    private static final byte[] END_BITS = new byte[]{0X0D, 0x0A};
 
     private static final Integer DATA_SIZE = 6;
 
@@ -120,14 +120,50 @@ public class GK309GpsServerHandler extends HexBinaryAcceptanceHandlerAdapter {
                         deviceId,2,
                         DatatypeConverter.printHexBinary(bytes), System.currentTimeMillis());
             } else if (CORRECT_TIME_MSG == msgType) {
-                reply(channel, CORRECT_TIME_MSG, "校时包");
-            } else {
+                // 1位长度位和6位时间位,后面才是序列号
+                in.skipBytes(7);
+                short x = in.readShort();
+                byte[] ret = short2Bytes(x);
+
+//                reply(channel, CORRECT_TIME_MSG, "校时包");
+                replyForTime(channel, ret);
+            } else if (SIGN_IN_MSG == msgType) {
+
+                in.skipBytes(7);
+                short terminalReservedWord = in.readShort();
+                byte[] reservedWorkBytes = short2Bytes(terminalReservedWord);
+
+                // 减去前面跳过的7位,再减去最后的两位(校验位)
+                in.skipBytes(length - 7 - 2);
+                short serNum = in.readShort();
+                byte[] serNumBytes = short2Bytes(serNum);
+
+                long currentTimeMillis = System.currentTimeMillis();
+                byte[]
+//                        timeOfSignIn = new BCD8421Operater().string2Bcd(String.valueOf(currentTimeMillis));
+                        timeOfSignIn = BitOperator.longToBytes(currentTimeMillis, 6);
+
+                // 状态: 0(失败), 1(成功) 类型: 1(上班), 2(下班)
+                byte[] signResult = new byte[]{0x01, 0x01};
+                // 0X0F是长度,固定长度
+                byte[] contentsOfPackage = mergeByteArray(new byte[]{0X0F, SIGN_IN_MSG},
+                        timeOfSignIn, signResult, reservedWorkBytes, serNumBytes);
+                writeToClient(channel, "打卡包", contentsOfPackage);
+
+            } else  {
                 logger.info("client send data without handle type ...");
             }
         }
 
     }
 
+    private byte[] short2Bytes(short x) {
+        byte[]  ret = new byte[2];
+        ret[0] = (byte)(x & 0xff);
+        ret[1] = (byte)((x >> 8) & 0xff);
+        return ret;
+    }
+
     /**
      * 登录管理
      *
@@ -179,6 +215,53 @@ public class GK309GpsServerHandler extends HexBinaryAcceptanceHandlerAdapter {
         });
     }
 
+    private byte[] mergeByteArray(byte[]... bytesArray) {
+        byte[] result = ArrayUtils.EMPTY_BYTE_ARRAY;
+        for (byte[] bytes : bytesArray) {
+            result = ArrayUtils.addAll(result, bytes);
+        }
+        return result;
+    }
+
+    /**
+     * 校时回复
+     * @param channel
+     */
+    private void replyForTime(Channel channel, byte[] serialNumber) {
+        byte msgType = CORRECT_TIME_MSG;
+        String msg = "校验时间";
+
+        /**
+         * 起始位[]长度[] 协议号[]	序列号[]CRC校验[]停止位[2]
+         */
+        long utcSeconds = Calendar.getInstance().getTimeInMillis() / 1000L;
+
+        // 长度(固定0B)-协议号(固定)-utc时间秒数-预留位(2个0x00)-序列号
+        byte[] utcBytes = BitOperator.longToBytes(utcSeconds, 4);
+        byte[] seriNumbytes = mergeByteArray(new byte[]{0x00, 0x00}, serialNumber);
+        byte[] contentsOfPackage = mergeByteArray(new byte[]{0x0B, msgType}, utcBytes, seriNumbytes);
+
+        writeToClient(channel, msg, contentsOfPackage);
+    }
+
+    private void writeToClient(Channel channel, String msg, byte[] contentsOfPackage) {
+        ByteBuf buffer = Unpooled.buffer();
+        byte[] bytesToReply = wrapeContents(contentsOfPackage);
+        buffer.writeBytes(bytesToReply);
+        ChannelFuture channelFuture = channel.write(buffer);
+        channelFuture.addListener(future -> {
+            String deviceId = channelDeviceMap.get(channel);
+            logger.info("server reply [{}] to client device [{}] success:[{}] ", msg, deviceId, DatatypeConverter.printHexBinary(bytesToReply));
+        });
+    }
+
+    private byte[] wrapeContents(byte[] contentsOfPackage) {
+        int doCrc = CRCUtil.do_crc(65535, contentsOfPackage);//CRC-ITU
+        byte[] crcBytes = NumUtil.intToByte(doCrc, 2);
+
+        return mergeByteArray(new byte[]{START_BIT, START_BIT}, contentsOfPackage, crcBytes, END_BITS);
+    }
+
     /**
      * 原始数据重新组装
      *