Browse Source

Merge branch 'hotfix/1.2.2'

Zhenghanjv 5 months ago
parent
commit
4580166f9c

+ 1 - 1
ai-fueling/pom.xml

@@ -4,7 +4,7 @@
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.tokheim</groupId>
     <artifactId>ai-fueling</artifactId>
-    <version>1.2.0</version>
+    <version>1.2.2</version>
     <name>ai-fueling</name>
     <description>ai-fueling</description>
     <properties>

+ 50 - 42
ai-fueling/src/main/java/com/tokheim/aifueling/communication/toInternalInterface/InterfaceApi.java

@@ -1,42 +1,50 @@
-package com.tokheim.aifueling.communication.toInternalInterface;
-
-import com.google.gson.Gson;
-import com.tokheim.aifueling.communication.entitys.BaseInfo;
-import com.tokheim.aifueling.communication.toInternalInterface.entity.FuelSendRequest;
-import com.tokheim.aifueling.httpClinet.HttpClientConfig;
-import com.tokheim.aifueling.httpClinet.HttpClientUtils;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.stereotype.Component;
-
-import java.io.IOException;
-
-/**
- * 封装对内部接口的请求
- */
-@Component
-@Slf4j
-public class InterfaceApi {
-
-    @Autowired
-    private HttpClientUtils httpClientUtils;
-
-    @Value("${internalInterface.sendData}")
-    public String SEND_DATA;
-
-    /**
-     * 发送数据给内部接口
-     * @param dataType 数据类型
-     * @param data 参数
-     * @return 响应结果 json
-     * @throws IOException
-     */
-    public String sendData(Integer dataType, BaseInfo data) throws IOException {
-        FuelSendRequest requestParams = FuelSendRequest.convert(dataType, data);
-        String json = new Gson().toJson(requestParams);
-        log.info("发送请求:{}\n请求参数:{}", SEND_DATA, json);
-        return httpClientUtils.doPost(SEND_DATA,json);
-    }
-
-}
+package com.tokheim.aifueling.communication.toInternalInterface;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParser;
+import com.tokheim.aifueling.communication.entitys.BaseInfo;
+import com.tokheim.aifueling.communication.toInternalInterface.entity.FuelResponse;
+import com.tokheim.aifueling.communication.toInternalInterface.entity.FuelSendRequest;
+import com.tokheim.aifueling.httpClinet.HttpClientConfig;
+import com.tokheim.aifueling.httpClinet.HttpClientUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+
+/**
+ * 封装对内部接口的请求
+ */
+@Component
+@Slf4j
+public class InterfaceApi {
+
+    @Autowired
+    private HttpClientUtils httpClientUtils;
+
+    @Value("${internalInterface.sendData}")
+    public String SEND_DATA;
+
+    /**
+     * 发送数据给内部接口
+     * @param dataType 数据类型
+     * @param data 参数
+     * @return 响应结果 json
+     * @throws IOException
+     */
+    public String sendData(Integer dataType, BaseInfo data) throws IOException {
+        FuelSendRequest requestParams = FuelSendRequest.convert(dataType, data);
+        Gson gson = new GsonBuilder().setPrettyPrinting().create();
+        String json = gson.toJson(requestParams);
+        log.info("发送请求:{}\n请求参数:{}", SEND_DATA, json);
+        String response = httpClientUtils.doPost(SEND_DATA, json);
+        JsonElement jsonElement = JsonParser.parseString(response);
+//        FuelResponse fuelResponse = gson.fromJson(response, FuelResponse.class);
+        return gson.toJson(jsonElement);
+    }
+
+}

+ 34 - 34
ai-fueling/src/main/java/com/tokheim/aifueling/communication/toInternalInterface/entity/FuelResponse.java

@@ -1,34 +1,34 @@
-package com.tokheim.aifueling.communication.toInternalInterface.entity;
-
-import lombok.Data;
-
-/**
- * 内部接口的响应结构
- * <h2>code: 响应码</h2>
- * <h2>message: 响应信息</h2>
- * <h2>data: 数据</h2>
- *      <p>oilAmount:定量加油金额,0表示不定量</p>
- *      <p>oilLiters:定量加油升数,0表示不定量</p>
- *      <p>licensePlate:车牌号</p>
- * <h2>type: 事件类型</h2>
- * <h2>AiId: AI ID</h2>
- * <h2>GLId: 格林 ID</h2>
- */
-@Data
-public class FuelResponse {
-    public Integer code;
-    public String message;
-    public Data data;
-    public Object type;
-    public String AiId;
-    public String GLId;
-    public Boolean MQTT_Public;
-    public Object DataJson;
-
-    @lombok.Data
-    public static class Data {
-        public String oilAmount;
-        public String oilLiters;
-        public String licensePlate;
-    }
-}
+package com.tokheim.aifueling.communication.toInternalInterface.entity;
+
+import lombok.Data;
+
+/**
+ * 内部接口的响应结构
+ * <h2>code: 响应码</h2>
+ * <h2>message: 响应信息</h2>
+ * <h2>data: 数据</h2>
+ *      <p>oilAmount:定量加油金额,0表示不定量</p>
+ *      <p>oilLiters:定量加油升数,0表示不定量</p>
+ *      <p>licensePlate:车牌号</p>
+ * <h2>type: 事件类型</h2>
+ * <h2>AiId: AI ID</h2>
+ * <h2>GLId: 格林 ID</h2>
+ */
+@Data
+public class FuelResponse {
+    public Integer code;
+    public String message;
+    public Data data;
+    public Object type;
+    public String aiId;
+    public String gLId;
+    public Boolean mqtT_Public;
+    public Object dataJson;
+
+    @lombok.Data
+    public static class Data {
+        public String oilAmount;
+        public String oilLiters;
+        public String licensePlate;
+    }
+}

+ 188 - 188
ai-fueling/src/main/java/com/tokheim/aifueling/communication/toMachine/MachineWriter.java

@@ -1,188 +1,188 @@
-package com.tokheim.aifueling.communication.toMachine;
-
-import cn.hutool.core.convert.Convert;
-import com.tokheim.aifueling.communication.entitys.DecimalToBcdAid;
-import com.tokheim.aifueling.utils.ByteArrayUtils;
-import com.tokheim.aifueling.utils.SM4Utils;
-import io.netty.channel.ChannelHandlerContext;
-import lombok.extern.slf4j.Slf4j;
-
-import java.math.BigDecimal;
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * 封装对油机发送信息操作
- */
-@Slf4j
-public class MachineWriter {
-    //数据包头
-    private static byte[] dataHead = new byte[]{0x02,0x01,0x01,0x00,0x00,0x62};
-
-    private static Map<String, ChannelHandlerContext> ctxMap = new HashMap<>();
-
-    public static void setCtxMap(String ip, ChannelHandlerContext ctx) {
-        ctxMap.put(ip, ctx);
-    }
-
-    public static void removeCtxMap(String ip) {
-        ctxMap.remove(ip);
-    }
-
-    /**
-     * 定金额加油
-     * @param ip            发送目标油机ip
-     * @param fuelPoint     加油点
-     * @param internalNum   内部枪号
-     * @param amount        定量金额
-     */
-    public static boolean sendQuantitativeByAmount(String ip,int fuelPoint,int internalNum,String amount) {
-        if (!checkBaseParam(ip, fuelPoint, internalNum)) {
-            log.error("sendQuantitative:参数:{},{},{}错误",ip,fuelPoint,internalNum);
-            return false;
-        }
-
-        ChannelHandlerContext context = ctxMap.get(ip);
-        if (context == null) {
-            log.error("sendQuantitative:Cannot find handler for ip {}", ip);
-            return false;
-        }
-
-        //拼接实际内容
-        byte[] nonAmountBytes = {0x05, (byte) (fuelPoint + 32),0x24, (byte) internalNum,0x27};
-        DecimalToBcdAid decimalToBcdAid = ByteArrayUtils.decimalStrToBCD(amount);
-        byte[] lens = {decimalToBcdAid.getAllLen(),decimalToBcdAid.getIntPartLen()};
-        byte[] amountBytes = ByteArrayUtils.add(lens, decimalToBcdAid.getBcdBytes());
-        byte[] waitEncryptBytes = ByteArrayUtils.add(nonAmountBytes, amountBytes);
-
-        return sendData(waitEncryptBytes,context);
-    }
-
-    /**
-     * 定升数加油
-     * @param ip            发送目标油机ip
-     * @param fuelPoint     加油点
-     * @param internalNum   内部枪号
-     * @param volume        定量升数
-     */
-    public static boolean sendQuantitativeByVolume(String ip,int fuelPoint,int internalNum,String volume) {
-        if (!checkBaseParam(ip, fuelPoint, internalNum)) {
-            log.error("sendQuantitative:参数:{},{},{}错误",ip,fuelPoint,internalNum);
-            return false;
-        }
-
-        ChannelHandlerContext context = ctxMap.get(ip);
-        if (context == null) {
-            log.error("sendQuantitative:Cannot find handler for ip {}", ip);
-            return false;
-        }
-
-        //拼接实际内容
-        byte[] nonAmountBytes = {0x05, (byte) (fuelPoint + 32),0x24, (byte) internalNum,0x28};
-        DecimalToBcdAid decimalToBcdAid = ByteArrayUtils.decimalStrToBCD(volume);
-        byte[] lens = {decimalToBcdAid.getAllLen(),decimalToBcdAid.getIntPartLen()};
-        byte[] amountBytes = ByteArrayUtils.add(lens, decimalToBcdAid.getBcdBytes());
-        byte[] waitEncryptBytes = ByteArrayUtils.add(nonAmountBytes, amountBytes);
-
-        return sendData(waitEncryptBytes,context);
-    }
-
-    /**
-     * 发送车牌号
-     * @param ip            发送目标油机ip
-     * @param fuelPoint     加油点
-     * @param internalNum   内部枪号
-     * @param carNum        车牌号
-     */
-    public static boolean sendCarNumber(String ip,int fuelPoint,int internalNum,String carNum) {
-        if (!checkBaseParam(ip, fuelPoint, internalNum)) {
-            log.error("sendCarNumber:参数:{},{},{}错误",ip,fuelPoint,internalNum);
-            return false;
-        }
-        ChannelHandlerContext context = ctxMap.get(ip);
-        if (context == null) {
-            log.error("sendCarNumber:Cannot find handler for ip {}", ip);
-            return false;
-        }
-
-        //拼接实际内容
-        byte[] nonCarNum = {0x06, (byte) (fuelPoint + 32),0x24, (byte) internalNum,0x29};
-        byte[] carNumContentBytes = carNum.getBytes(Charset.forName("GB2312"));
-        byte[] carNumBytes = ByteArrayUtils.add(new byte[]{(byte) carNumContentBytes.length}, carNumContentBytes);
-        byte[] waitEncryptBytes = ByteArrayUtils.add(nonCarNum, carNumBytes);
-
-        return sendData(waitEncryptBytes,context);
-    }
-
-    /**
-     * 停止加油
-     * @param ip            发送目标油机ip
-     * @param fuelPoint     加油点
-     * @param internalNum   内部枪号
-     */
-    public static boolean stopFueling(String ip,Integer fuelPoint,Integer internalNum) {
-        if (!checkBaseParam(ip, fuelPoint, internalNum)) {
-            log.error("stopFueling:参数:{},{},{}错误",ip,fuelPoint,internalNum);
-            return false;
-        }
-        ChannelHandlerContext context = ctxMap.get(ip);
-        if (context == null) {
-            log.error("stopFueling:Cannot find handler for ip {}", ip);
-            return false;
-        }
-
-        //拼接实际内容
-        byte[] waitEncryptBytes = {0x04, (byte) (fuelPoint + 32),0x24, Convert.toByte(internalNum)};
-
-        return sendData(waitEncryptBytes,context);
-    }
-
-    //加密及添加长度
-    private static byte[] encryptAndLen(byte[] waitEncryptBytes) {
-        byte[] noneHeaderBytes = new byte[0];
-        try {
-            byte[] encrypt = SM4Utils.encrypt(waitEncryptBytes);
-            byte[] lenBytes = ByteArrayUtils.intToTwoByteArray(encrypt.length);
-            if (lenBytes != null) {
-                noneHeaderBytes = ByteArrayUtils.add(lenBytes, encrypt);
-            }
-        } catch (Exception e) {
-            log.error("加密失败:{}", ByteArrayUtils.bytesToHexString(waitEncryptBytes));
-            throw new RuntimeException(e);
-        }
-        return noneHeaderBytes;
-    }
-
-    //检查基础信息
-    private static boolean checkBaseParam(String ip,int fuelPoint,int internalNum) {
-        if (ip == null || ip.isEmpty()) return false;
-        return Convert.toInt(fuelPoint) != null && Convert.toInt(internalNum) != null;
-    }
-
-
-
-    /**
-     * 将数据打包好发送到油机
-     * 加密,添加长度,添加包头
-     * @param waitEncryptBytes  待加密数据
-     * @param context           发送油机 channel
-     * @return                  成功与否
-     */
-    private static boolean sendData(byte[] waitEncryptBytes,ChannelHandlerContext context) {
-        //加密并加上长度
-        byte[] noneHeaderBytes = encryptAndLen(waitEncryptBytes);
-
-        //拼接包头并发送
-        if (noneHeaderBytes.length != 0) {
-            byte[] content = ByteArrayUtils.add(dataHead,noneHeaderBytes);
-
-            log.info("发送数据到油机:{},数据:{}",context.channel().remoteAddress(),ByteArrayUtils.bytesToHexString(content));
-            //write 在接口处调用时客户端会接收不到数据,要用 writeAndFlush
-            context.channel().writeAndFlush(content);
-            return true;
-        }
-        return false;
-    }
-}
+package com.tokheim.aifueling.communication.toMachine;
+
+import cn.hutool.core.convert.Convert;
+import com.tokheim.aifueling.communication.entitys.DecimalToBcdAid;
+import com.tokheim.aifueling.utils.ByteArrayUtils;
+import com.tokheim.aifueling.utils.SM4Utils;
+import io.netty.channel.ChannelHandlerContext;
+import lombok.extern.slf4j.Slf4j;
+
+import java.math.BigDecimal;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 封装对油机发送信息操作
+ */
+@Slf4j
+public class MachineWriter {
+    //数据包头
+    private static byte[] dataHead = new byte[]{0x02,0x01,0x01,0x00,0x00,0x62};
+
+    private static Map<String, ChannelHandlerContext> ctxMap = new HashMap<>();
+
+    public static void setCtxMap(String ip, ChannelHandlerContext ctx) {
+        ctxMap.put(ip, ctx);
+    }
+
+    public static void removeCtxMap(String ip) {
+//        ctxMap.remove(ip);
+    }
+
+    /**
+     * 定金额加油
+     * @param ip            发送目标油机ip
+     * @param fuelPoint     加油点
+     * @param internalNum   内部枪号
+     * @param amount        定量金额
+     */
+    public static boolean sendQuantitativeByAmount(String ip,int fuelPoint,int internalNum,String amount) {
+        if (!checkBaseParam(ip, fuelPoint, internalNum)) {
+            log.error("sendQuantitative:参数:{},{},{}错误",ip,fuelPoint,internalNum);
+            return false;
+        }
+
+        ChannelHandlerContext context = ctxMap.get(ip);
+        if (context == null) {
+            log.error("sendQuantitative:Cannot find handler for ip {}", ip);
+            return false;
+        }
+
+        //拼接实际内容
+        byte[] nonAmountBytes = {0x05, (byte) (fuelPoint + 32),0x24, (byte) internalNum,0x27};
+        DecimalToBcdAid decimalToBcdAid = ByteArrayUtils.decimalStrToBCD(amount);
+        byte[] lens = {decimalToBcdAid.getAllLen(),decimalToBcdAid.getIntPartLen()};
+        byte[] amountBytes = ByteArrayUtils.add(lens, decimalToBcdAid.getBcdBytes());
+        byte[] waitEncryptBytes = ByteArrayUtils.add(nonAmountBytes, amountBytes);
+
+        return sendData(waitEncryptBytes,context);
+    }
+
+    /**
+     * 定升数加油
+     * @param ip            发送目标油机ip
+     * @param fuelPoint     加油点
+     * @param internalNum   内部枪号
+     * @param volume        定量升数
+     */
+    public static boolean sendQuantitativeByVolume(String ip,int fuelPoint,int internalNum,String volume) {
+        if (!checkBaseParam(ip, fuelPoint, internalNum)) {
+            log.error("sendQuantitative:参数:{},{},{}错误",ip,fuelPoint,internalNum);
+            return false;
+        }
+
+        ChannelHandlerContext context = ctxMap.get(ip);
+        if (context == null) {
+            log.error("sendQuantitative:Cannot find handler for ip {}", ip);
+            return false;
+        }
+
+        //拼接实际内容
+        byte[] nonAmountBytes = {0x05, (byte) (fuelPoint + 32),0x24, (byte) internalNum,0x28};
+        DecimalToBcdAid decimalToBcdAid = ByteArrayUtils.decimalStrToBCD(volume);
+        byte[] lens = {decimalToBcdAid.getAllLen(),decimalToBcdAid.getIntPartLen()};
+        byte[] amountBytes = ByteArrayUtils.add(lens, decimalToBcdAid.getBcdBytes());
+        byte[] waitEncryptBytes = ByteArrayUtils.add(nonAmountBytes, amountBytes);
+
+        return sendData(waitEncryptBytes,context);
+    }
+
+    /**
+     * 发送车牌号
+     * @param ip            发送目标油机ip
+     * @param fuelPoint     加油点
+     * @param internalNum   内部枪号
+     * @param carNum        车牌号
+     */
+    public static boolean sendCarNumber(String ip,int fuelPoint,int internalNum,String carNum) {
+        if (!checkBaseParam(ip, fuelPoint, internalNum)) {
+            log.error("sendCarNumber:参数:{},{},{}错误",ip,fuelPoint,internalNum);
+            return false;
+        }
+        ChannelHandlerContext context = ctxMap.get(ip);
+        if (context == null) {
+            log.error("sendCarNumber:Cannot find handler for ip {}", ip);
+            return false;
+        }
+
+        //拼接实际内容
+        byte[] nonCarNum = {0x06, (byte) (fuelPoint + 32),0x24, (byte) internalNum,0x29};
+        byte[] carNumContentBytes = carNum.getBytes(Charset.forName("GB2312"));
+        byte[] carNumBytes = ByteArrayUtils.add(new byte[]{(byte) carNumContentBytes.length}, carNumContentBytes);
+        byte[] waitEncryptBytes = ByteArrayUtils.add(nonCarNum, carNumBytes);
+
+        return sendData(waitEncryptBytes,context);
+    }
+
+    /**
+     * 停止加油
+     * @param ip            发送目标油机ip
+     * @param fuelPoint     加油点
+     * @param internalNum   内部枪号
+     */
+    public static boolean stopFueling(String ip,Integer fuelPoint,Integer internalNum) {
+        if (!checkBaseParam(ip, fuelPoint, internalNum)) {
+            log.error("stopFueling:参数:{},{},{}错误",ip,fuelPoint,internalNum);
+            return false;
+        }
+        ChannelHandlerContext context = ctxMap.get(ip);
+        if (context == null) {
+            log.error("stopFueling:Cannot find handler for ip {}", ip);
+            return false;
+        }
+
+        //拼接实际内容
+        byte[] waitEncryptBytes = {0x04, (byte) (fuelPoint + 32),0x24, Convert.toByte(internalNum)};
+
+        return sendData(waitEncryptBytes,context);
+    }
+
+    //加密及添加长度
+    private static byte[] encryptAndLen(byte[] waitEncryptBytes) {
+        byte[] noneHeaderBytes = new byte[0];
+        try {
+            byte[] encrypt = SM4Utils.encrypt(waitEncryptBytes);
+            byte[] lenBytes = ByteArrayUtils.intToTwoByteArray(encrypt.length);
+            if (lenBytes != null) {
+                noneHeaderBytes = ByteArrayUtils.add(lenBytes, encrypt);
+            }
+        } catch (Exception e) {
+            log.error("加密失败:{}", ByteArrayUtils.bytesToHexString(waitEncryptBytes));
+            throw new RuntimeException(e);
+        }
+        return noneHeaderBytes;
+    }
+
+    //检查基础信息
+    private static boolean checkBaseParam(String ip,int fuelPoint,int internalNum) {
+        if (ip == null || ip.isEmpty()) return false;
+        return Convert.toInt(fuelPoint) != null && Convert.toInt(internalNum) != null;
+    }
+
+
+
+    /**
+     * 将数据打包好发送到油机
+     * 加密,添加长度,添加包头
+     * @param waitEncryptBytes  待加密数据
+     * @param context           发送油机 channel
+     * @return                  成功与否
+     */
+    private static boolean sendData(byte[] waitEncryptBytes,ChannelHandlerContext context) {
+        //加密并加上长度
+        byte[] noneHeaderBytes = encryptAndLen(waitEncryptBytes);
+
+        //拼接包头并发送
+        if (noneHeaderBytes.length != 0) {
+            byte[] content = ByteArrayUtils.add(dataHead,noneHeaderBytes);
+
+            log.info("发送数据到油机:{},数据:{}",context.channel().remoteAddress(),ByteArrayUtils.bytesToHexString(content));
+            //write 在接口处调用时客户端会接收不到数据,要用 writeAndFlush
+            context.channel().writeAndFlush(content);
+            return true;
+        }
+        return false;
+    }
+}

+ 2 - 2
ai-fueling/src/main/java/com/tokheim/aifueling/netty/handler/UnpackHandler.java

@@ -43,7 +43,7 @@ public class UnpackHandler extends ChannelInboundHandlerAdapter {
         log.info("客户端断开连接" + ctx.channel().remoteAddress());
         cacheData = new byte[0];
         String ip = getIP(ctx.channel().remoteAddress().toString());
-        MachineWriter.removeCtxMap(ip);
+//        MachineWriter.removeCtxMap(ip);
     }
 
     //报错
@@ -85,7 +85,7 @@ public class UnpackHandler extends ChannelInboundHandlerAdapter {
 
 
         //数据长度不够
-        if (cacheData.length <= headIndex + 8 + dataLen) return;
+        if (cacheData.length < headIndex + 8 + dataLen) return;
 
         //将加密截取(命令字+实际数据)发送到下一个 handler
         byte[] dataBytes = ByteArrayUtils.slipt(cacheData, headIndex + 8, headIndex + 8 + dataLen);