src/main/java/com/fzzy/gateway/controller/GatewayDeviceController.java
@@ -310,7 +310,7 @@ //获取到 resp = gatewayRemoteManager.getGatewayTestService(device.getPushProtocol()).testGrainKafka(reqData); log.info("resp = "+resp.toString()); //自动推送 if (BaseResp.CODE_200 == resp.getCode()) { reqData.setData(resp.getData()); src/main/java/com/fzzy/gateway/hx2023/service/ScGatewayTestServiceImpl.java
@@ -80,7 +80,7 @@ //获取第一条数据执行推送 Fz40Grain lastData = listGrain.get(0); log.info(lastData.toString()); return deviceReportService.grainData2GatewayApiInfo(lastData,device); } src/main/java/com/fzzy/protocol/wujia/analysis/AnalysisService.java
@@ -59,10 +59,17 @@ * data1L data1H-----18B20返回温度 * 注意:粮温返回数据高位在后,低位在前,转换为十进制后乘以0.0625,得到实际温度值 * 仓温湿度高位在前,低位在后,转换为十进制后乘以0.1得到实际温湿度值 * * 02 * 89 00 7A 00 E2 00 8C 00 7D 00 E9 00 99 00 82 00 EC 00 87 00 87 00 ED 00 83 00 77 00 EF 00 92 00 7D 00 08 01 87 00 77 00 03 01 7F 00 7A 00 F6 00 * 9B 00 5D 00 D1 00 * 84 00 65 00 D4 00 8D 00 6D 00 D6 00 89 00 6E 00 DD 00 87 00 61 00 CA 00 81 00 6B 00 DA 00 81 00 68 00 E2 00 86 00 69 00 D0 00 * 87 00 6D 00 E4 00 88 00 71 00 07 01 8E 00 76 00 13 01 82 00 75 00 EB 00 86 00 6D 00 DB 00 83 00 71 00 F1 00 87 00 7B 00 03 01 79 00 7D 00 F7 00 * 02 C1 00 6D AA BB CC * @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); @@ -78,13 +85,13 @@ //仓内湿度 start = 1 * 2; len = 2 * 2; tag = strMsg.substring(start, start + len); tag = strMsg.substring(strMsg.length()-8,strMsg.length() -4); double hIn = BytesUtil.hexToInt(tag) * 0.1; //仓内温度 start = 3 * 2; len = 2 * 2; tag = strMsg.substring(start, start + len); tag = strMsg.substring(strMsg.length()-4,strMsg.length() ); double tIn = BytesUtil.hexToInt(tag) * 0.1; GatewayDevice device = reqData.getDevice(); @@ -97,12 +104,12 @@ int sumNum = cableZ * cableY * cableX; //粮温信息 start = 5 * 2; start = 2; len = 2 * 2; List<Double> points = new ArrayList<>(); double temp = 0.0; for (int i = 0; i < sumNum; i++) { start = start + i * 2 * 2; start = i * 2 * 2 +2; tag = strMsg.substring(start, start + len); temp = BytesUtil.hexToInt(BytesUtil.tran_LH(tag)) * 0.0625; temp = NumberUtil.keepPrecision(temp, 1); src/main/java/com/fzzy/protocol/wujia/client/ClientEngine.java
@@ -16,7 +16,7 @@ import java.util.Map; /** * 通讯协议 * 游仙主库,通讯协议 */ @Slf4j public class ClientEngine implements Runnable { @@ -29,7 +29,7 @@ private String host; private int port; private Channel defaultChannel; public static Channel defaultChannel; public ClientEngine(String host, int port) { @@ -45,6 +45,7 @@ @Override public void run() { try { startClient(); } catch (Exception e) { e.printStackTrace(); @@ -52,7 +53,10 @@ } public void startClient() throws Exception { if(defaultChannel != null){ log.info("-----IP={},连接存在,直接使用",host); return; } EventLoopGroup group = new OioEventLoopGroup(); Bootstrap b = new Bootstrap(); //默认长连接 @@ -82,9 +86,9 @@ @Override public void operationComplete(ChannelFuture arg0) throws Exception { if (channelFuture.isSuccess()) { log.info("-----IP={},连接成功", host); log.info("-----IP={},连接成功",host); } else { log.info("-----IP={},连接失败,自动关闭线程", host); log.info("-----IP={},连接失败,自动关闭线程",host); channelFuture.cause().printStackTrace(); group.shutdownGracefully(); // 关闭线程组 } @@ -107,11 +111,11 @@ } public Channel getChannel() { public static Channel getChannel() { return defaultChannel; } public static void add2ChannelMap(String key, Channel channel) { public void add2ChannelMap(String key, Channel channel) { clientChannelMap.put(key, channel); } @@ -121,18 +125,19 @@ * @param key * @return */ public static Channel getChannel(String key) { 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 { public static InvokeResult send2(String hex,Channel channel) throws InterruptedException { if (null == channel) { return InvokeResult.SOCKET_NOT_CREATE; } src/main/java/com/fzzy/protocol/wujia/client/ClientHandler.java
@@ -32,7 +32,7 @@ InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress(); log.info("连接终端掉线,IP={},port={}", socketAddress.getAddress(), socketAddress.getPort()); //ClientEngine.defaultChannel = null; } @@ -48,6 +48,7 @@ msgStr+= strMsg; if(msgStr.endsWith("AABBCC")){ log.info("消息接收完整开始解析,IP={},port={},msg={}", socketAddress.getAddress(), socketAddress.getPort(), msgStr); if (null == analysisService) { @@ -58,7 +59,7 @@ }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/wujia/service/WujiaGatewayGrainService.java
@@ -6,7 +6,7 @@ import com.fzzy.gateway.data.BaseResp; import com.fzzy.gateway.entity.GatewayDevice; import com.fzzy.protocol.ProtocolUtils; import com.fzzy.protocol.youxian0.client.ClientEngine; import com.fzzy.protocol.wujia.client.ClientEngine; import com.ld.io.api.InvokeResult; import io.netty.channel.Channel; import lombok.extern.slf4j.Slf4j; @@ -84,11 +84,11 @@ String hexStr = buildGrainCmd(device); Channel channel = null; Channel channel = ClientEngine.getChannel(); if (null == channel) { ClientEngine clientEngine = new ClientEngine(device.getIp(), device.getPort()); clientEngine.start(); Thread.sleep(300); Thread.sleep(1000); channel = clientEngine.getChannel(); }