vince
2024-01-16 5f92b2034f84caeab9cb6efc61ecbe22407935b4
优化主库协议,支持路由
已修改8个文件
157 ■■■■ 文件已修改
src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/gateway/hx2023/ScConstant.java 5 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/gateway/hx2023/controller/GatewayController.java 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/protocol/wujia/client/ClientHandler.java 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/protocol/youxian0/analysis/AnalysisService.java 24 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/protocol/youxian0/client/ClientEngine.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/protocol/youxian0/client/ClientHandler.java 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/protocol/youxian0/service/Youxian0GatewayGrainService.java 77 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java
@@ -38,6 +38,7 @@
        list.add(new ApiTrigger(GRAIN_FZZY_ZLDZ_WEB.getCode(), GRAIN_FZZY_ZLDZ_WEB.getName()));
        list.add(new ApiTrigger(GRAIN_YOUXIAN0_2023.getCode(), GRAIN_YOUXIAN0_2023.getName()));
        list.add(new ApiTrigger(GRAIN_YOUXIAN1_2023.getCode(), GRAIN_YOUXIAN1_2023.getName()));
        list.add(new ApiTrigger(GRAIN_WUJIA_2023.getCode(), GRAIN_WUJIA_2023.getName()));
        list.add(new ApiTrigger(DEVICE_WEIGHT_HTTP.getCode(), DEVICE_WEIGHT_HTTP.getName()));
        list.add(new ApiTrigger(DEVICE_WEIGHT_TCP_YH.getCode(), DEVICE_WEIGHT_TCP_YH.getName()));
src/main/java/com/fzzy/gateway/hx2023/ScConstant.java
@@ -46,7 +46,10 @@
     * 下发指令回复报文topic
     */
    public static String TOPIC_REPORT = "/${productId}/${deviceId}/properties/report";
    /**
     * 下发指令回复报文topic
     */
    public static String TOPIC_REPORT_GRAIN = "TOPIC_ZLJ_GRAIN_TEMPERATURE";
    /**
     * 设备相关信息恢复报文-topic
     */
src/main/java/com/fzzy/gateway/hx2023/controller/GatewayController.java
@@ -7,6 +7,7 @@
import com.fzzy.gateway.entity.GatewayConf;
import com.fzzy.gateway.hx2023.data.GatewayAuthData;
import com.fzzy.gateway.service.GatewayConfService;
import com.ld.license.LicenseVerify;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@@ -84,7 +85,21 @@
        json.put("message", "成功");
        json.put("status", 0);
        json.put("code", 200);
        return json;
        try{
            LicenseVerify licenseVerify = new LicenseVerify();
            //校验证书是否有效
            boolean verifyResult = licenseVerify.verify();
            if(verifyResult){
                return json;
            }else{
                log.error("证书验证失败!拒绝访问");
                return null;
            }
        }catch (Exception e){
            log.error("证书验证失败:" + e.getMessage(),e);
            return null;
        }
    }
    public void updateGatewayToken(String token, String username) {
src/main/java/com/fzzy/protocol/wujia/client/ClientHandler.java
@@ -15,6 +15,7 @@
    private AnalysisService analysisService;
    private static String msgStr = "";
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel()
@@ -44,16 +45,24 @@
        String strMsg = BytesUtil.bytesToString(bytes);
        log.info("终端返回信息,IP={},port={},msg={}", socketAddress.getAddress(), socketAddress.getPort(), strMsg);
        msgStr+= strMsg;
        if (null == analysisService) {
            analysisService = SpringUtil.getBean(AnalysisService.class);
        if(msgStr.endsWith("AABBCC")){
            log.info("消息接收完整开始解析,IP={},port={},msg={}", socketAddress.getAddress(), socketAddress.getPort(), msgStr);
            if (null == analysisService) {
                analysisService = SpringUtil.getBean(AnalysisService.class);
            }
            try{
                analysisService.analysis(socketAddress.getAddress(), socketAddress.getPort(), msgStr);
            }catch (Exception e){
                log.error(e.getMessage(),e);
            }
        }else{
            log.info("消息不完整,等待消息完整,IP={},port={},msg={}", socketAddress.getAddress(), socketAddress.getPort(), strMsg);
        }
       try{
           analysisService.analysis(socketAddress.getAddress(), socketAddress.getPort(), strMsg);
       }catch (Exception e){
           log.error(e.getMessage(),e);
       }
    }
src/main/java/com/fzzy/protocol/youxian0/analysis/AnalysisService.java
@@ -160,7 +160,7 @@
        String tempStr = "";
        for (int j = 0;j<cableY;j++){
            tempStr = strMsg.substring(12 * j,12 * j +12);
            tempStr = strMsg.substring((6+2*cableZ) * j,(6+2*cableZ)  * j + (6+2*cableZ) );
            //密钥和点数 02 A4 BB BA BA B4
            String kyeNumHex = tempStr.substring(2, 4);
            String kyeNumBin = BytesUtil.toBinary8String(BytesUtil.hexToInt(kyeNumHex));
@@ -207,14 +207,24 @@
        List<Double> points = new ArrayList<>();
        GrainRoot root;
        List<Double> t = null;
        for (int i = 1; i <= cableX; i++) {
            root = this.getGrainRoot(buildGrainRootKey(reqData.getDevice().getDeviceSn(), i));
            if (null == root || null == root.getPoints()) {
                log.error("-----------解析获取所有粮情检测点失败,取消执行---------{}---{}", reqData.getDevice().getDeviceName(),i);
                return;
                ;
                t = new ArrayList<>();
                for (int x = 0;x<cableY*cableZ;x++
                     ) {
                    t.add(-100.00);
                }
                //return;
                points.addAll(t);
            }else{
                points.addAll(root.getPoints());
            }
            points.addAll(root.getPoints());
        }
        //执行封装解析
@@ -342,7 +352,7 @@
        reportService.reportGrainData(reqData);
    }
    private void add2GrainMap(GrainRoot grainRoot) {
    private synchronized  void add2GrainMap(GrainRoot grainRoot) {
        contextGrainRoot.put(grainRoot.getKey(), grainRoot);
    }
@@ -371,8 +381,12 @@
        int num1 = BytesUtil.hexToInt(valueHex);
        int num2 = BytesUtil.hexToInt(tempHex);
        if((num1 ^ num2) / 2.0 > 35){
            return  -100.00;
        }else {
            return  (num1 ^ num2) / 2.0;
        }
        return (num1 ^ num2) / 2.0;
    }
    private void add2ThMap(String depotIdSys, THDto th) {
src/main/java/com/fzzy/protocol/youxian0/client/ClientEngine.java
@@ -30,7 +30,7 @@
    private String host;
    private int port;
    public  static  Channel defaultChannel;
    public static Channel defaultChannel;
    public ClientEngine(String host, int port) {
@@ -116,7 +116,7 @@
        return defaultChannel;
    }
    public static void add2ChannelMap(String key, Channel channel) {
    public  void add2ChannelMap(String key, Channel channel) {
        clientChannelMap.put(key, channel);
    }
@@ -133,6 +133,7 @@
            return channel;
        } else {
            channel.close();
            defaultChannel = null;
        }
        return null;
    }
src/main/java/com/fzzy/protocol/youxian0/client/ClientHandler.java
@@ -3,6 +3,7 @@
import com.fzzy.api.utils.BytesUtil;
import com.fzzy.api.utils.SpringUtil;
import com.fzzy.protocol.youxian0.analysis.AnalysisService;
import com.fzzy.protocol.youxian0.service.Youxian0GatewayGrainService;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
@@ -31,7 +32,7 @@
        InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        log.info("连接终端掉线,IP={},port={}", socketAddress.getAddress(), socketAddress.getPort());
        //ClientEngine.defaultChannel = null;
    }
src/main/java/com/fzzy/protocol/youxian0/service/Youxian0GatewayGrainService.java
@@ -38,7 +38,7 @@
     * @return
     */
    @Override
    public synchronized BaseResp syncGrain(BaseReqData reqData) {
    public  synchronized BaseResp syncGrain(BaseReqData reqData) {
        BaseResp resp = new BaseResp();
@@ -64,6 +64,13 @@
            // 生成粮情信息
            String  hexStr = "";
            InvokeResult message;
            Channel channel = ClientEngine.getChannel();
            if (null == channel) {
                ClientEngine clientEngine = new ClientEngine(device.getIp(), device.getPort());
                clientEngine.start();
                Thread.sleep(1000);
                channel = clientEngine.getChannel();
            }
            for (int i = 1; i <= cableX; i++) {
                if (1 == i) {
                    start = 255;
@@ -73,13 +80,7 @@
                hexStr = buildGrainCmd(device, i, start, length);
                // 发送命令 TODO----->>>暂时调整为每次创建一个新连接
                //Channel channel = ClientEngine.getChannel(device.getIp());
                Channel channel = null;
                if (null == channel) {
                    ClientEngine clientEngine = new ClientEngine(device.getIp(), device.getPort());
                    clientEngine.start();
                    Thread.sleep(300);
                    channel = clientEngine.getChannel();
                }
                message = ClientEngine.send2(hexStr, channel);
                log.error("平台------>>>>主控:发送粮情检测命令-{}---{}", message,hexStr);
@@ -90,7 +91,7 @@
                    resp.setCode(500);
                    resp.setMsg("平台------>>>>控制柜:发送粮情检测命令-失败:" + message.getMessage());
                }
                Thread.sleep(500);
                Thread.sleep(3000);
            }
        } catch (Exception e) {
            log.error("粮情检测异常:{}", e);
@@ -155,14 +156,36 @@
        String start = "7e";
        //测温命令--7e 01 00 00 01 00 00 02 00 01 a0 ff ff 66 ff 05 3a 59 7e
        String content = "{id}0000{id}{msgId}000200{id}a0ffff{funId}{start}{length}";
        //7e 0f 00 00 10 5f 00 03 00 0f 10 a0 ff ff 66 ff 05 9e 43 7e
        //7e 06 00 00 06 29 00 02 00 06 a0 ff ff 66 1a 05 58 0f 7e
        String content = "{depotId}0000{id1}{msgId}00{type}00{id2}a0ffff{funId}{start}{length}";
        String type = "02";
        //开始封装消息体-主机ID
        String deviceSn = device.getDeviceSn();
        String depotId = device.getDepotIdSys();
        deviceSn = BytesUtil.intToHexStr1(Integer.valueOf(deviceSn));
        content = content.replace("{id}", deviceSn);
        content = content.replace("{id}", deviceSn);
        content = content.replace("{id}", deviceSn);
        depotId = BytesUtil.intToHexStr1(Integer.valueOf(depotId));
        if(!depotId.equals(deviceSn)){
            type = "03";
            content = content.replace("{depotId}", depotId);
            content = content.replace("{id1}", deviceSn);
            if(deviceSn.toLowerCase().equals("0e".toLowerCase())){
                content = content.replace("{id2}", depotId +"13"+ deviceSn);
                type = "04";
            }else{
                content = content.replace("{id2}", depotId + deviceSn);
            }
            content = content.replace("{type}", type);
        }else{
            content = content.replace("{type}", type);
            content = content.replace("{depotId}", depotId);
            content = content.replace("{id1}", deviceSn);
            content = content.replace("{id2}", deviceSn);
        }
        //命令ID
        String msgIdHex = BytesUtil.intToHexStr1(cur);
        content = content.replace("{msgId}", msgIdHex);
@@ -189,14 +212,34 @@
        String start = "7e";
        //测温命令--7e 01 00 00 01 06 00 02 00 01 a0 ff ff 68 1a 05 88 5c 7e
        String content = "{id}0000{id}{msgId}000200{id}a0ffff{funId}{start}{length}";
        //                {depotId}0000{id1}{msgId}00{type}00{id2}
        String content = "{depotId}0000{id1}{msgId}00{type}00{id2}a0ffff{funId}{start}{length}";
        String type = "02";
        //开始封装消息体-主机ID
        String deviceSn = device.getDeviceSn();
        String depotId = device.getDepotIdSys();
        deviceSn = BytesUtil.intToHexStr1(Integer.valueOf(deviceSn));
        content = content.replace("{id}", deviceSn);
        content = content.replace("{id}", deviceSn);
        content = content.replace("{id}", deviceSn);
        depotId = BytesUtil.intToHexStr1(Integer.valueOf(depotId));
        if(!depotId.equals(deviceSn)){
            type = "03";
            content = content.replace("{depotId}", depotId);
            content = content.replace("{id1}", deviceSn);
            if(deviceSn.toLowerCase().equals("0e".toLowerCase())){
                content = content.replace("{id2}", depotId +"13"+ deviceSn);
                type = "04";
            }else{
                content = content.replace("{id2}", depotId + deviceSn);
            }
            content = content.replace("{type}", type);
        }else{
            content = content.replace("{type}", type);
            content = content.replace("{depotId}", depotId);
            content = content.replace("{id1}", deviceSn);
            content = content.replace("{id2}", deviceSn);
        }
        //命令ID
        String msgIdHex = BytesUtil.intToHexStr1(20);
        content = content.replace("{msgId}", msgIdHex);