vince
2024-03-04 070b1e9cc8fccd36e7b65720a778ac46e118c792
增加西山油罐协议
已修改4个文件
已添加6个文件
707 ■■■■■ 文件已修改
src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/protocol/wujia/client/ClientHandler.java 19 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/protocol/xsyg/1.txt 56 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/protocol/xsyg/analysis/AnalysisService.java 214 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/protocol/xsyg/client/ClientEngine.java 152 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/protocol/xsyg/client/ClientHandler.java 66 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/protocol/xsyg/package-info.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/protocol/xsyg/service/XsygGatewayGrainService.java 190 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application-devGateway.yml 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application.yml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java
@@ -19,6 +19,7 @@
    GRAIN_YOUXIAN0_2023("GRAIN_YOUXIAN0_2023", "粮情-游仙主库协议"),
    GRAIN_YOUXIAN1_2023("GRAIN_YOUXIAN1_2023", "粮情-游仙分库协议"),
    GRAIN_WUJIA_2023("GRAIN_WUJIA_2023", "粮情-吴家粮库协议"),
    GRAIN_XSYK_2023("GRAIN_WUJIA_2023", "油情-西山油库协议"),
    DEVICE_WEIGHT_HTTP("DEVICE_WEIGHT_HTTP", "地磅-HTTP协议"),
    DEVICE_WEIGHT_TCP_YH("DEVICE_WEIGHT_TCP_YH", "地磅-耀华TCP协议"),
    DEVICE_IDCARD_HTTP("DEVICE_IDCARD_HTTP", "身份证-HTTP协议"),
@@ -40,7 +41,7 @@
        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(GRAIN_XSYK_2023.getCode(), GRAIN_XSYK_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()));
        list.add(new ApiTrigger(DEVICE_IDCARD_HTTP.getCode(), DEVICE_IDCARD_HTTP.getName()));
src/main/java/com/fzzy/protocol/wujia/client/ClientHandler.java
@@ -2,7 +2,8 @@
import com.fzzy.api.utils.BytesUtil;
import com.fzzy.api.utils.SpringUtil;
import com.fzzy.protocol.wujia.analysis.AnalysisService;
import com.fzzy.protocol.youxian0.analysis.AnalysisService;
import com.fzzy.protocol.youxian0.client.ClientEngine;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
@@ -15,7 +16,6 @@
    private AnalysisService analysisService;
    private static String msgStr = "";
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel()
@@ -32,7 +32,7 @@
        InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        log.info("连接终端掉线,IP={},port={}", socketAddress.getAddress(), socketAddress.getPort());
        //ClientEngine.defaultChannel = null;
        ClientEngine.defaultChannel = null;
    }
@@ -45,24 +45,15 @@
        String strMsg = BytesUtil.bytesToString(bytes);
        log.info("终端返回信息,IP={},port={},msg={}", socketAddress.getAddress(), socketAddress.getPort(), strMsg);
        msgStr+= strMsg;
        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);
           analysisService.analysis(socketAddress.getAddress(), socketAddress.getPort(), strMsg);
            }catch (Exception e){
                log.error(e.getMessage(),e);
            }
            msgStr = "";
        }else{
            log.info("消息不完整,等待消息完整,IP={},port={},msg={}", socketAddress.getAddress(), socketAddress.getPort(), strMsg);
        }
    }
src/main/java/com/fzzy/protocol/xsyg/1.txt
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,56 @@
01 01 3C 01 1E 01 11 01 07 00 9E 00 9C 00 9B 00 95 FF FF 15 E9 AA BB CC
02 01 4F 01 2C 01 1C 01 0E 01 03 00 FB 00 F2 00 8E FF FF 25 70 AA BB CC
03 01 57 01 37 00 A8 00 A2 00 A3 00 9F 00 9C 00 97 FF FF 0B 39 AA BB CC
04 01 2F 01 22 01 13 01 07 00 FB 00 F2 00 89 00 8D FF FF 21 B8 AA BB CC
05 01 46 01 2B 01 19 00 9E 00 9F 00 9C 00 99 00 96 FF FF 0E 82 AA BB CC
06 01 53 01 35 01 23 00 A0 00 9F 00 9F 00 9B 00 96 FF FF 0E BF AA BB CC
07 01 47 01 23 01 0E 01 01 00 F6 00 EE 00 E5 00 87 FF FF 26 27 AA BB CC
08 01 46 01 29 01 17 01 0A 01 01 00 97 00 97 00 90 FF FF 17 AD AA BB CC
09 01 5A 00 FD 00 A2 00 A1 00 9C 00 9A 00 97 00 92 FF FF 06 86 AA BB CC
0A 01 41 01 28 01 1A 00 CF 01 05 00 A6 00 A2 00 A1 FF FF 0E AD AA BB CC
0B 01 5D 00 C2 00 A2 00 A2 00 9E 00 9B 00 9A 00 92 FF FF 05 F5 AA BB CC
0C 01 2E 01 17 00 A5 00 A5 00 A0 00 9C 00 99 00 93 FF FF 07 A9 AA BB CC
0E 01 5D 01 38 00 A5 00 A5 00 A1 00 9F 00 9B 00 97 FF FF 07 8A AA BB CC
0F 01 46 01 22 01 10 01 02 00 99 00 9A 00 95 00 92 FF FF 12 3E AA BB CC
10 01 52 01 2B 00 AF 00 A0 00 A1 00 9E 00 9B 00 96 FF FF 0B 2D AA BB CC
11 01 0A 00 97 00 8D 00 87 FF FF FF FF FF FF FF FF FF FF 06 80 AA BB CC
12 01 42 01 2A 01 1A 01 0E 00 97 00 9B 00 99 00 95 00 90 15 1D AA BB CC
13 01 1E 01 0E 00 F9 00 E8 00 93 00 92 00 93 FF FF FF FF 14 0F AA BB CC
14 01 48 01 2B 01 1A 00 A0 00 A0 00 9C 00 9A 00 96 00 94 0D 8E AA BB CC
15 01 22 01 14 01 00 00 9D 00 9C 00 98 00 92 FF FF FF FF 10 43 AA BB CC
16 01 4B 01 2D 01 1E 00 A2 00 A0 00 9F 00 9B 00 99 00 96 0D 26 AA BB CC
17 00 EC 00 8B 00 84 00 85 FF FF FF FF FF FF FF FF FF FF 07 9D AA BB CC
18 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 AA BB CC
19 00 FA 00 EB 00 92 00 8B FF FF FF FF FF FF FF FF FF FF 0D FD AA BB CC
1A 01 0A 00 BB 00 AF 00 91 FF FF FF FF FF FF FF FF FF FF 07 E7 AA BB CC
命令
【Receive from 10.0.0.17 : 49853】:
FC 64 01 01 00 09 01 1A 10 BD DB
FC 64 02 02 00 09 01 1A 10 BD DB
FC 64 03 03 00 09 01 1A 10 BD DB
FC 64 04 04 00 09 01 1A 10 BD DB
FC 64 05 05 00 09 01 1A 10 BD DB
FC 64 06 06 00 09 01 1A 10 BD DB
FC 64 07 07 00 09 01 1A 10 BD DB
FC 64 08 08 00 09 01 1A 10 BD DB
FC 64 09 09 00 09 01 1A 10 BD DB
FC 64 0A 0A 00 09 01 1A 10 BD DB
FC 64 0B 0B 00 09 01 1A 10 BD DB
FC 64 0C 0C 00 09 01 1A 10 BD DB
FC 64 0D 0D 00 09 01 1A 10 BD DB
FC 64 0E 0E 00 09 01 1A 10 BD DB
FC 64 0F 0F 00 09 01 1A 10 BD DB
FC 64 10 10 00 09 01 1A 10 BD DB
FC 64 11 11 00 09 01 1A 10 BD DB
FC 64 12 12 00 09 01 1A 10 BD DB
FC 64 13 13 00 09 01 1A 10 BD DB
FC 64 14 14 00 09 01 1A 10 BD DB
FC 64 15 15 00 09 01 1A 10 BD DB
FC 64 16 16 00 09 01 1A 10 BD DB
FC 64 17 17 00 09 01 1A 10 BD DB
FC 64 18 18 00 09 01 1A 10 BD DB
FC 64 19 19 00 09 01 1A 10 BD DB
FC 64 1A 1A 00 09 01 1A 10 BD DB
src/main/java/com/fzzy/protocol/xsyg/analysis/AnalysisService.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,214 @@
package com.fzzy.protocol.xsyg.analysis;
import com.alibaba.fastjson.JSONObject;
import com.fzzy.api.data.GatewayDeviceType;
import com.fzzy.api.utils.BytesUtil;
import com.fzzy.api.utils.NumberUtil;
import com.fzzy.gateway.GatewayUtils;
import com.fzzy.gateway.api.GatewayDeviceReportService;
import com.fzzy.gateway.api.GatewayRemoteManager;
import com.fzzy.gateway.data.BaseReqData;
import com.fzzy.gateway.data.WeatherWebDto;
import com.fzzy.gateway.entity.GatewayDevice;
import com.fzzy.gateway.hx2023.ScConstant;
import com.fzzy.gateway.hx2023.data.*;
import com.fzzy.protocol.ProtocolUtils;
import com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Component(AnalysisService.BEAN_ID)
public class AnalysisService {
    public static final String BEAN_ID = "xsyg.analysisService";
    @Resource
    private GatewayRemoteManager gatewayRemoteManager;
    /**
     * ç²®æƒ…接线
     *
     * @param address
     * @param port
     * @param strMsg
     */
    public void analysis(InetAddress address, int port, String strMsg) {
        if (strMsg.startsWith("FE")) {
            //DO NOTHING
        } else {
            this.analysisGrainStep1(address, strMsg);
        }
    }
    /**
     *byte(i)  01 7C   01 67   01 66   01 81 ã€‚。。   02 89  AA BB CC
     *   byte(i)--------包头
     * AA BB CC-----包尾
     * è“è‰²æ•°æ®æ¯ä¸¤ä¸ªå­—节表示一个测温点的值(转换为十进制后*0.0625).共有cwcs[1]*2个字节。
     * 02 89 ä¸¤ä¸ªå­—节表示该油罐的空高值(高位在前)单位毫米。(FF FF或00 00表示故障,请在软件上做出处理,把该空高值赋值成传感器安装高度,表示该罐是空罐)。用H值减去该数据,得到油位高度。
     *
     * @param strMsg
     */
    private void analysisGrainStep1(InetAddress address, String strMsg) {
        strMsg = strMsg.substring(0,strMsg.length()-6);
        log.info("完整的数据体:"+strMsg);
        int start = 0;
        int len = 1 * 2;
        String tag = strMsg.substring(start, start + 2);
        int depotIdSys = BytesUtil.hexToInt(tag);
        BaseReqData reqData = ProtocolUtils.getSyncReq(depotIdSys + "");
        if (null == reqData) {
            log.error("---------没有获取到请求信息,不执行解析------{}", address);
            return;
        }
        //仓内湿度
        GatewayDevice device = reqData.getDevice();
        String[] attCable = device.getCableRule().split("-");
        int cableZ = Integer.valueOf(attCable[0]);
        int cableY = Integer.valueOf(attCable[1]);
        int cableX = Integer.valueOf(attCable[2]);
        log.info("z={},x={},y={}", cableZ, cableX, cableY);
        int sumNum = cableZ * cableY * cableX;
        //粮温信息
        start = 2;
        len = 2 * 2;
        List<Double> points = new ArrayList<>();
        double temp = 0.0;
        for (int i = 0; i < sumNum; i++) {
            start = i * 2 * 2 +2;
            tag = strMsg.substring(start, start + len);
            if("FFFF".equals(tag)){
                points.add(-100.00);
                continue;
            }
            temp = BytesUtil.hexToInt(BytesUtil.tran_LH(tag)) * 0.0625;
            temp = NumberUtil.keepPrecision(temp, 1);
            points.add(temp);
        }
        double height = 0.0;
        tag = strMsg.substring(strMsg.length()-4);
        height = NumberUtil.keepPrecision((double) (BytesUtil.hexToInt(tag) /1000), 2);
        //执行封装解析
        analysisGrainStep2(reqData, cableZ, cableY, cableX, points, -100, -100,height);
    }
    private void analysisGrainStep2(BaseReqData reqData, int cableZ, int cableY, int cableX, List<Double> points, double hIn, double tIn,double height) {
        GatewayDevice device = reqData.getDevice();
        int sumNum = points.size();
        //数据封装
        GrainData grain = new GrainData();
        grain.setMessageId(ScConstant.getMessageId());
        grain.setDeviceId(device.getDeviceId());
        grain.setTimestamp(System.currentTimeMillis() + "");
        ClientHeaders headers = new ClientHeaders();
        headers.setDeviceName(device.getDeviceName());
        headers.setProductId(device.getProductId());
        headers.setOrgId(device.getOrgId());
        headers.setMsgId(reqData.getMessageId());
        grain.setHeaders(headers);
        GrainOutPut outPut = new GrainOutPut();
        double max = com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder.MAX_TEMP, min = com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder.MIN_TEMP, sumT = 0.0;
        List<GrainTemp> temperature = new ArrayList<>();
        //根号
        int cableNum = 1, position = 0;
        double curTemp;
        int x = 0, y = 0, z = 0;
        for (int i = 0; i < sumNum; i++) {
            curTemp = points.get(i);
            position = i;
            z = i % cableZ + 1;
            x = i / (cableZ * cableY);
            y = x * (cableZ * cableY);
            y = (i - y) / cableZ;
            //根号
            cableNum = (i / cableZ) + 1;
            temperature.add(new GrainTemp(cableNum + "", z + "", curTemp + "", position + ""));
            //求最大最小值
            if (curTemp < -900) {
                sumNum--;
            } else {
                sumT += curTemp;
                if (curTemp > max) {
                    max = curTemp;
                }
                if (curTemp < min) {
                    min = curTemp;
                }
            }
        }
        if (sumNum == 0) {
            sumNum = 1;
            log.warn("---当前粮情采集异常--");
        }
        //过滤比较用的最大最小值
        if (max == com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder.MAX_TEMP) {
            max = 0.0;
        }
        if (min == ReMessageBuilder.MIN_TEMP) {
            min = 0.0;
        }
        outPut.setTemperature(temperature);
        outPut.setAvgTemperature(NumberUtil.keepPrecision((sumT / sumNum), 1) + "");
        outPut.setMinTemperature(min + "");
        outPut.setMaxTemperature(min + "");
        JSONObject properties = new JSONObject();
        properties.put("data", outPut);
        properties.put("timestamp", grain.getTimestamp());
        properties.put("liquidHeight", height);
        grain.setProperties(properties);
        //封装好的数据
        log.info("---浅圆仓封装完成----开始执行推送");
        reqData.setData(JSONObject.toJSONString(grain));
        doPushGrain(reqData);
    }
    private void doPushGrain(BaseReqData reqData) {
        GatewayDeviceReportService reportService = gatewayRemoteManager.getDeviceReportService(reqData.getDevice().getPushProtocol());
        if (null == reportService) {
            log.error("------------粮情推送失败,系统不存在当前协议执行类----{}", reqData.getDevice().getDeviceName());
            return;
        }
        reportService.reportGrainData(reqData);
    }
}
src/main/java/com/fzzy/protocol/xsyg/client/ClientEngine.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,152 @@
package com.fzzy.protocol.xsyg.client;
import com.fzzy.api.utils.BytesUtil;
import com.ld.io.api.InvokeResult;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.oio.OioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.oio.OioSocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.Map;
/**
 * æ¸¸ä»™ä¸»åº“,通讯协议
 */
@Slf4j
public class ClientEngine implements Runnable {
    /**
     * å®¢æˆ·ç«¯æ¨¡å¼åˆ›å»ºçš„连接通道
     */
    public static Map<String, Channel> clientChannelMap = new HashMap<>();
    private String host;
    private int port;
    public static Channel defaultChannel;
    public ClientEngine(String host, int port) {
        this.host = host;
        this.port = port;
    }
    public void start() {
        Thread thread = new Thread(this);
        thread.start();
    }
    @Override
    public void run() {
        try {
            startClient();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void startClient() throws Exception {
        if(defaultChannel != null){
            log.info("-----IP={},连接存在,直接使用",host);
            return;
        }
        EventLoopGroup group = new OioEventLoopGroup();
        Bootstrap b = new Bootstrap();
        //默认长连接
        b.option(ChannelOption.SO_KEEPALIVE, true);
        b.group(group).channel(OioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch)
                            throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        // å­—符串解码 å’Œ ç¼–码
                        p.addLast("decoder", new ByteArrayDecoder());
                        p.addLast("encoder", new ByteArrayEncoder());
                        // è‡ªå·±çš„逻辑Handler
                        p.addLast("handler", new ClientHandler());
                    }
                });
        // å‘起异步连接请求,绑定连接端口和host信息
        ChannelFuture channelFuture = b.connect(host, port);
        this.defaultChannel = channelFuture.channel();
        // channelFuture.channel().closeFuture().sync();
        this.add2ChannelMap(host, defaultChannel);
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture arg0) throws Exception {
                if (channelFuture.isSuccess()) {
                    log.info("-----IP={},连接成功",host);
                } else {
                    log.info("-----IP={},连接失败,自动关闭线程",host);
                    channelFuture.cause().printStackTrace();
                    group.shutdownGracefully(); // å…³é—­çº¿ç¨‹ç»„
                }
            }
        });
    }
    public InvokeResult send(byte[] array) throws InterruptedException {
        if (null == defaultChannel) {
            return InvokeResult.SOCKET_NOT_CREATE;
        }
        if (!defaultChannel.isActive()) {
            return InvokeResult.CHANNEL_CLOSED;
        }
        defaultChannel.writeAndFlush(Unpooled.copiedBuffer(array)).sync();
        return InvokeResult.SUCCESS;
    }
    public static Channel getChannel() {
        return defaultChannel;
    }
    public  void add2ChannelMap(String key, Channel channel) {
        clientChannelMap.put(key, channel);
    }
    /**
     * å¦‚æžœ
     *
     * @param key
     * @return
     */
    public static  Channel getChannel(String key) {
        Channel channel = clientChannelMap.get(key);
        if (null == channel) return null;
        if (channel.isActive()) {
            return channel;
        } else {
            channel.close();
            defaultChannel = null;
        }
        return null;
    }
    public static InvokeResult send2(String hex,Channel channel) throws InterruptedException {
        if (null == channel) {
            return InvokeResult.SOCKET_NOT_CREATE;
        }
        if (!channel.isActive()) {
            return InvokeResult.CHANNEL_CLOSED;
        }
        channel.writeAndFlush(Unpooled.copiedBuffer(BytesUtil.hexStrToBytes(hex))).sync();
        return InvokeResult.SUCCESS;
    }
}
src/main/java/com/fzzy/protocol/xsyg/client/ClientHandler.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,66 @@
package com.fzzy.protocol.xsyg.client;
import com.fzzy.api.utils.BytesUtil;
import com.fzzy.api.utils.SpringUtil;
import com.fzzy.protocol.youxian0.analysis.AnalysisService;
import com.fzzy.protocol.youxian0.client.ClientEngine;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import java.net.InetSocketAddress;
@Slf4j
public class ClientHandler extends SimpleChannelInboundHandler<Object> {
    private AnalysisService analysisService;
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel()
                .remoteAddress();
        log.info("连接终成功,IP={},port={}", socketAddress.getAddress()
                .getHostAddress(), socketAddress.getPort());
    }
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
        InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        log.info("连接终端掉线,IP={},port={}", socketAddress.getAddress(), socketAddress.getPort());
        ClientEngine.defaultChannel = null;
    }
    @Override
    public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        byte[] bytes = (byte[]) msg;
        InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        String strMsg = BytesUtil.bytesToString(bytes);
        log.info("终端返回信息,IP={},port={},msg={}", socketAddress.getAddress(), socketAddress.getPort(), strMsg);
        if (null == analysisService) {
            analysisService = SpringUtil.getBean(AnalysisService.class);
        }
       try{
           analysisService.analysis(socketAddress.getAddress(), socketAddress.getPort(), strMsg);
       }catch (Exception e){
           log.error(e.getMessage(),e);
       }
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        ctx.close();
    }
}
src/main/java/com/fzzy/protocol/xsyg/package-info.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,4 @@
/**
 * å´å®¶ç²®åº“2023
 */
package com.fzzy.protocol.wujia;
src/main/java/com/fzzy/protocol/xsyg/service/XsygGatewayGrainService.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,190 @@
package com.fzzy.protocol.xsyg.service;
import com.fzzy.api.data.GatewayDeviceProtocol;
import com.fzzy.gateway.api.GatewaySyncGranService;
import com.fzzy.gateway.data.BaseReqData;
import com.fzzy.gateway.data.BaseResp;
import com.fzzy.gateway.entity.GatewayDevice;
import com.fzzy.protocol.ProtocolUtils;
import com.fzzy.protocol.wujia.client.ClientEngine;
import com.ld.io.api.InvokeResult;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
 * ç²®æƒ…采集入口
 */
@Slf4j
@Component
public class XsygGatewayGrainService implements GatewaySyncGranService {
    @Override
    public String getGrainProtocol() {
        return GatewayDeviceProtocol.GRAIN_XSYK_2023.getCode();
    }
    /**
     * ä»“库命令
     */
    public static Map<String, String> depotGrainCmdMap = new HashMap<>();
    static {
        depotGrainCmdMap.put("KEY_1",  "FC6401010009011A10BDDB");
        depotGrainCmdMap.put("KEY_2",  "FC6402020009011A10BDDB");
        depotGrainCmdMap.put("KEY_3",  "FC6403030009011A10BDDB");
        depotGrainCmdMap.put("KEY_4",  "FC6404040009011A10BDDB");
        depotGrainCmdMap.put("KEY_5",  "FC6405050009011A10BDDB");
        depotGrainCmdMap.put("KEY_6",  "FC6406060009011A10BDDB");
        depotGrainCmdMap.put("KEY_7",  "FC6407070009011A10BDDB");
        depotGrainCmdMap.put("KEY_8",  "FC6408080009011A10BDDB");
        depotGrainCmdMap.put("KEY_9",  "FC6409090009011A10BDDB");
        depotGrainCmdMap.put("KEY_10", "FC640A0A0009011A10BDDB");
        depotGrainCmdMap.put("KEY_11", "FC640B0B0009011A10BDDB");
        depotGrainCmdMap.put("KEY_12", "FC640C0C0009011A10BDDB");
        depotGrainCmdMap.put("KEY_13", "FC640D0D0009011A10BDDB");
        depotGrainCmdMap.put("KEY_14", "FC640E0E0009011A10BDDB");
        depotGrainCmdMap.put("KEY_15", "FC640F0F0009011A10BDDB");
        depotGrainCmdMap.put("KEY_16", "FC6410100009011A10BDDB");
        depotGrainCmdMap.put("KEY_17", "FC6411110009011A10BDDB");
        depotGrainCmdMap.put("KEY_18", "FC6412120009011A10BDDB");
        depotGrainCmdMap.put("KEY_19", "FC6413130009011A10BDDB");
        depotGrainCmdMap.put("KEY_20", "FC6414140009011A10BDDB");
        depotGrainCmdMap.put("KEY_21", "FC6415150009011A10BDDB");
        depotGrainCmdMap.put("KEY_22", "FC6416160009011A10BDDB");
        depotGrainCmdMap.put("KEY_23", "FC6417170009011A10BDDB");
        depotGrainCmdMap.put("KEY_24", "FC6418180009011A10BDDB");
        depotGrainCmdMap.put("KEY_25", "FC6419190009011A10BDDB");
        depotGrainCmdMap.put("KEY_26", "FC641A1A0009011A10BDDB");
    }
    /**
     * FC 64 01 01 00 09 01 1A 10 BD DB
     * FC 64 02 02 00 09 01 1A 10 BD DB
     * FC 64 03 03 00 09 01 1A 10 BD DB
     * FC 64 04 04 00 09 01 1A 10 BD DB
     * FC 64 05 05 00 09 01 1A 10 BD DB
     * FC 64 06 06 00 09 01 1A 10 BD DB
     * FC 64 07 07 00 09 01 1A 10 BD DB
     * FC 64 08 08 00 09 01 1A 10 BD DB
     * FC 64 09 09 00 09 01 1A 10 BD DB
     * FC 64 0A 0A 00 09 01 1A 10 BD DB
     * FC 64 0B 0B 00 09 01 1A 10 BD DB
     * FC 64 0C 0C 00 09 01 1A 10 BD DB
     * FC 64 0D 0D 00 09 01 1A 10 BD DB
     * FC 64 0E 0E 00 09 01 1A 10 BD DB
     * FC 64 0F 0F 00 09 01 1A 10 BD DB
     * FC 64 10 10 00 09 01 1A 10 BD DB
     * FC 64 11 11 00 09 01 1A 10 BD DB
     * FC 64 12 12 00 09 01 1A 10 BD DB
     * FC 64 13 13 00 09 01 1A 10 BD DB
     * FC 64 14 14 00 09 01 1A 10 BD DB
     * FC 64 15 15 00 09 01 1A 10 BD DB
     * FC 64 16 16 00 09 01 1A 10 BD DB
     * FC 64 17 17 00 09 01 1A 10 BD DB
     * FC 64 18 18 00 09 01 1A 10 BD DB
     * FC 64 19 19 00 09 01 1A 10 BD DB
     * FC 64 1A 1A 00 09 01 1A 10 BD DB
     *
     * @param reqData
     * @return
     */
    @Override
    public synchronized BaseResp syncGrain(BaseReqData reqData) {
        BaseResp resp = new BaseResp();
        GatewayDevice device = reqData.getDevice();
        if (null == device) {
            resp.setCode(500);
            resp.setMsg("系统未获取到下行连接设备信息,无法执行");
            log.error("----------------系统未获取到下行连接设备信息,无法执行---------");
            return resp;
        }
        try {
            //Step è¯·æ±‚信息放入内存
            ProtocolUtils.addSyncReq2Map(device.getDepotIdSys(), reqData);
            String hexStr = buildGrainCmd(device);
            Channel channel = ClientEngine.getChannel();
            if (null == channel) {
                ClientEngine clientEngine = new ClientEngine(device.getIp(), device.getPort());
                clientEngine.start();
                Thread.sleep(1000);
                channel = clientEngine.getChannel();
            }
            InvokeResult message = ClientEngine.send2(hexStr, channel);
            log.error("平台------>>>>主控:发送粮情检测命令-{}---{}", message, hexStr);
            // å°è£…返回信息
            if (!InvokeResult.SUCCESS.getCode().equals(message.getCode())) {
                log.error("平台------>>>>控制柜:发送粮情检测命令-失败{}", message.getMessage());
                resp.setCode(500);
                resp.setMsg("平台------>>>>控制柜:发送粮情检测命令-失败:" + message.getMessage());
            }
        } catch (
                Exception e) {
            log.error("粮情检测异常:{}", e);
            resp.setCode(500);
            resp.setMsg("平台------>>>>控制柜:发送粮情检测命令:" + e.getMessage());
            return resp;
        }
        return resp;
    }
    @Override
    public BaseResp syncGrainTh(BaseReqData reqData) {
        BaseResp resp = new BaseResp();
        return resp;
    }
    /**
     * ç”Ÿæˆç²®æƒ…采集命令
     *
     * @param device
     * @return
     */
    private String buildGrainCmd(GatewayDevice device) {
        String key = "KEY_" + device.getDepotIdSys();
        return depotGrainCmdMap.get(key);
    }
    @Override
    public BaseResp syncConf(BaseReqData reqData) {
        return new BaseResp();
    }
    @Override
    public BaseResp writeConf(BaseReqData reqData) {
        return new BaseResp();
    }
    @Override
    public BaseResp initCable(BaseReqData reqData) {
        return new BaseResp();
    }
    @Override
    public BaseResp disconnect(BaseReqData reqData) {
        return new BaseResp();
    }
    @Override
    public BaseResp transparent(BaseReqData reqData) {
        return new BaseResp();
    }
}
src/main/resources/application-devGateway.yml
@@ -12,6 +12,7 @@
    #次数据源
    secondary:
      url: jdbc:mysql://127.0.0.1:3306/igds_5012?useUnicode=true&useSSL=false&characterEncoding=utf-8
      username: root
      password: Abc123..
      driver-class-name: com.mysql.jdbc.Driver
src/main/resources/application.yml
@@ -1,7 +1,7 @@
##########################  Server   ##########################
spring:
  profiles:
    active: devGateway
    active: proGateway
  application:
    name: igds-api
  main: