src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/protocol/wujia/analysis/AnalysisService.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/protocol/wujia/client/ClientEngine.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/protocol/wujia/client/ClientHandler.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/protocol/wujia/package-info.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/protocol/wujia/service/WujiaGatewayGrainService.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java
@@ -17,6 +17,7 @@ GRAIN_FZZY_ZLDZ_WEB("GRAIN_FZZY_ZLDZ_WEB", "ç²®æ -FZZY-ZLDZç½å£åè®®"), GRAIN_YOUXIAN0_2023("GRAIN_YOUXIAN0_2023", "ç²®æ -游ä»ä¸»åºåè®®"), GRAIN_YOUXIAN1_2023("GRAIN_YOUXIAN1_2023", "ç²®æ -游ä»ååºåè®®"), GRAIN_WUJIA_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åè®®"), src/main/java/com/fzzy/protocol/wujia/analysis/AnalysisService.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,232 @@ package com.fzzy.protocol.wujia.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.cmd.ReMessageBuilder; import lombok.extern.slf4j.Slf4j; 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 = "wujia.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); } } /** * ç²®æ è§£æ * X1 data1L data1H data2L data2H data3L data3Hâ¦â¦â¦datanL datanH DATA1H DATA1L DATA2H DATA2L AA BB CC * X1---åéçåºå· AA BB CC----ç»ææ å¿ * DATA1H DATA1L----ä»å 湿度 * DATA2H DATA2L----ä»å 温度 * data1L data1H-----18B20è¿å温度 * 注æï¼ç²®æ¸©è¿åæ°æ®é«ä½å¨åï¼ä½ä½å¨åï¼è½¬æ¢ä¸ºåè¿å¶åä¹ä»¥0.0625ï¼å¾å°å®é æ¸©åº¦å¼ * 仿¸©æ¹¿åº¦é«ä½å¨åï¼ä½ä½å¨åï¼è½¬æ¢ä¸ºåè¿å¶åä¹ä»¥0.1å¾å°å®é æ¸©æ¹¿åº¦å¼ * * @param strMsg */ private void analysisGrainStep1(InetAddress address, String 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; } //ä»å 湿度 start = 1 * 2; len = 2 * 2; tag = strMsg.substring(start, start + len); double hIn = BytesUtil.hexToInt(tag) * 0.1; //ä»å 温度 start = 3 * 2; len = 2 * 2; tag = strMsg.substring(start, start + len); double tIn = BytesUtil.hexToInt(tag) * 0.1; 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 = 5 * 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; tag = strMsg.substring(start, start + len); temp = BytesUtil.hexToInt(BytesUtil.tran_LH(tag)) * 0.0625; temp = NumberUtil.keepPrecision(temp, 1); points.add(temp); } //æ§è¡å°è£ è§£æ analysisGrainStep2(reqData, cableZ, cableY, cableX, points, hIn, tIn); } private void analysisGrainStep2(BaseReqData reqData, int cableZ, int cableY, int cableX, List<Double> points, double hIn, double tIn) { GatewayDevice device = reqData.getDevice(); //æ°æ®å°è£ 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 = ReMessageBuilder.MAX_TEMP, min = ReMessageBuilder.MIN_TEMP, sumT = 0.0, sumNum = cableX * cableY * cableZ; List<GrainTemp> temperature = new ArrayList<>(); //æ ¹å· int cableNum = 1, position = 0; double curTemp; int x = 0, y = 0, z = 0; for (int i = 0; i < points.size(); 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 + "")); sumT += curTemp; if (curTemp > max) { max = curTemp; } if (curTemp < min) { min = curTemp; } } if (sumNum == 0) { sumNum = 1; log.warn("---å½åç²®æ ééå¼å¸¸--"); } //è¿æ»¤æ¯è¾ç¨çæå¤§æå°å¼ if (max == 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 + ""); List<GrainTH> ths = new ArrayList<>(); ths.add(new GrainTH(tIn + "", hIn + "", "1")); outPut.setTemperatureAndhumidity(ths); grain.setOutput(JSONObject.toJSONString(outPut)); GatewayDevice gatewayDeviceWeather = GatewayUtils.getCacheByDeviceTypeOne(GatewayDeviceType.TYPE_09.getCode()); //ç³»ç»æ°è±¡ç«ä¿¡æ¯ WeatherWebDto weather = WeatherWebDto.contextMap.get("default"); //æ°è±¡ä¿¡æ¯ GrainWeather weatherStation = new GrainWeather(); weatherStation.setMessageId(ScConstant.getMessageId()); weatherStation.setMessgeId(weatherStation.getMessageId()); if (null != gatewayDeviceWeather) { weatherStation.setId(gatewayDeviceWeather.getDeviceId()); } else { weatherStation.setId(device.getDeviceId()); } weatherStation.setAirPressure(weather.getPressure()); weatherStation.setHumidity(weather.getHumidity()); weatherStation.setPm(weather.getAir_pm25()); weatherStation.setRadiation("0"); weatherStation.setRainfallAmount(weather.getWea()); weatherStation.setTemperature(weather.getTem()); weatherStation.setWindDirection(weather.getWin()); weatherStation.setWindPower(weather.getWin_meter()); weatherStation.setWindSpeed(weather.getWin_speed()); grain.setWeatherStation(JSONObject.toJSONString(weatherStation)); //å°è£ å¥½çæ°æ® 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/wujia/client/ClientEngine.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,147 @@ package com.fzzy.protocol.wujia.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; private 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 { 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 Channel getChannel() { return defaultChannel; } public static 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(); } 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/wujia/client/ClientHandler.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,65 @@ package com.fzzy.protocol.wujia.client; import com.fzzy.api.utils.BytesUtil; import com.fzzy.api.utils.SpringUtil; import com.fzzy.protocol.wujia.analysis.AnalysisService; 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()); } @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/wujia/package-info.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,4 @@ /** * å´å®¶ç²®åº2023 */ package com.fzzy.protocol.wujia; src/main/java/com/fzzy/protocol/wujia/service/WujiaGatewayGrainService.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,160 @@ package com.fzzy.protocol.wujia.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.youxian0.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 WujiaGatewayGrainService implements GatewaySyncGranService { @Override public String getGrainProtocol() { return GatewayDeviceProtocol.GRAIN_WUJIA_2023.getCode(); } /** * ä»åºå½ä»¤ */ public static Map<String, String> depotGrainCmdMap = new HashMap<>(); static { depotGrainCmdMap.put("KEY_1", "FC0101010314030100BDDB"); depotGrainCmdMap.put("KEY_2", "FC0102040318030200BDDB"); depotGrainCmdMap.put("KEY_3", "FC0203010318040100BDDB"); depotGrainCmdMap.put("KEY_4", "FC0304010318040100BDDB"); depotGrainCmdMap.put("KEY_5", "FC0305040318040200BDDB"); depotGrainCmdMap.put("KEY_6", "FC0406010210030100BDDB"); depotGrainCmdMap.put("KEY_7", "FC0407030314030200BDDB"); depotGrainCmdMap.put("KEY_8", "FC0408060314030300BDDB"); depotGrainCmdMap.put("KEY_9", "FC050901042A040100BDDB"); depotGrainCmdMap.put("KEY_10", "FC050A010324040100BDDB"); depotGrainCmdMap.put("KEY_11", "FC060B04042A040200BDDB"); } /** * 1ä»ï¼ FC 01 01 01 03 14 03 01 00 BD DB * 2ä»ï¼ FC 01 02 04 03 18 03 02 00 BD DB * 3ä»ï¼ FC 02 03 01 03 18 04 01 00 BD DB * 4ä»ï¼ FC 03 04 01 03 18 04 01 00 BD DB * 5ä»ï¼ FC 03 05 04 03 18 04 02 00 BD DB * 6ä»ï¼ FC 04 06 01 02 10 03 01 00 BD DB * 7ä»ï¼ FC 04 07 03 03 14 03 02 00 BD DB * 8ä»ï¼ FC 04 08 06 03 14 03 03 00 BD DB * 9ä»ï¼ FC 05 09 01 04 2A 04 01 00 BD DB * 10ä»ï¼FC 05 0A 01 03 24 04 01 00 BD DB * 11ä»ï¼FC 06 0B 04 04 2A 04 02 00 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 = null; if (null == channel) { ClientEngine clientEngine = new ClientEngine(device.getIp(), device.getPort()); clientEngine.start(); Thread.sleep(300); 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(); } }