From 8efb5a8cd2c8a6b40e58f4f3fb851d54cf415af9 Mon Sep 17 00:00:00 2001 From: jiazx0107@163.com <jiazx0107@163.com> Date: 星期日, 24 十二月 2023 01:23:55 +0800 Subject: [PATCH] 游仙协议解析-1 --- src/main/java/com/fzzy/protocol/youxian0/client/ClientHandler.java | 59 +++++ src/main/java/com/fzzy/protocol/youxian0/service/Youxian0GatewayGrainService.java | 228 ++++++++++++++++++++ src/main/java/com/fzzy/api/utils/BytesUtil.java | 24 -- src/main/java/com/fzzy/protocol/ProtocolUtils.java | 6 src/main/java/com/fzzy/protocol/youxian0/package-info.java | 4 src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java | 5 src/main/java/com/fzzy/gateway/GatewayUtils.java | 1 src/main/java/com/fzzy/gateway/GatewayRunner.java | 1 src/main/java/com/fzzy/protocol/youxian0/client/ClientEngine.java | 147 +++++++++++++ src/main/java/com/fzzy/protocol/youxian0/ServiceUtils.java | 26 ++ src/main/java/com/fzzy/protocol/youxian0/analysis/AnalysisService.java | 96 ++++++++ src/main/java/com/fzzy/protocol/youxian0/data/GrainRoot.java | 19 + 12 files changed, 591 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java b/src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java index 1cd7427..c26897a 100644 --- a/src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java +++ b/src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java @@ -15,6 +15,8 @@ GRAIN_FZZY_IGDS_V40("GRAIN_FZZY_IGDS_V40", "绮儏-椋庢鑷磋繙绯荤粺V40"), GRAIN_FZZY_BHZN_WEB("GRAIN_FZZY_BHZN_WEB", "绮儏-FZZY-閭︽捣鏅鸿兘缃戝彛鍗忚"), GRAIN_FZZY_ZLDZ_WEB("GRAIN_FZZY_ZLDZ_WEB", "绮儏-FZZY-姝f潵鐢靛瓙缃戝彛鍗忚"), + GRAIN_YOUXIAN0_2023("GRAIN_YOUXIAN0_2023", "绮儏-娓镐粰涓诲簱鍗忚"), + GRAIN_YOUXIAN1_2023("GRAIN_YOUXIAN1_2023", "绮儏-娓镐粰鍒嗗簱鍗忚"), DEVICE_WEIGHT_HTTP("DEVICE_WEIGHT_HTTP", "鍦扮-HTTP鍗忚"), DEVICE_WEIGHT_TCP_YH("DEVICE_WEIGHT_TCP_YH", "鍦扮-鑰�鍗嶵CP鍗忚"), DEVICE_IDCARD_HTTP("DEVICE_IDCARD_HTTP", "韬唤璇�-HTTP鍗忚"), @@ -32,6 +34,9 @@ list.add(new ApiTrigger(GRAIN_FZZY_IGDS_V40.getCode(), GRAIN_FZZY_IGDS_V40.getName())); list.add(new ApiTrigger(GRAIN_FZZY_BHZN_WEB.getCode(), GRAIN_FZZY_BHZN_WEB.getName())); 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(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())); diff --git a/src/main/java/com/fzzy/api/utils/BytesUtil.java b/src/main/java/com/fzzy/api/utils/BytesUtil.java index b62f453..af4348e 100644 --- a/src/main/java/com/fzzy/api/utils/BytesUtil.java +++ b/src/main/java/com/fzzy/api/utils/BytesUtil.java @@ -320,29 +320,6 @@ return String.format("%04x", num).toUpperCase(); } - // 璁$畻16杩涘埗瀵瑰簲鐨勬暟鍊� - public static int GetHex(char ch) throws Exception { - if (ch >= '0' && ch <= '9') - return (int) (ch - '0'); - if (ch >= 'a' && ch <= 'f') - return (int) (ch - 'a' + 10); - if (ch >= 'A' && ch <= 'F') - return (int) (ch - 'A' + 10); - throw new Exception("error param"); - } - - // 璁$畻骞� - public static int GetPower(int nValue, int nCount) throws Exception { - if (nCount < 0) - throw new Exception("nCount can't small than 1!"); - if (nCount == 0) - return 1; - int nSum = 1; - for (int i = 0; i < nCount; ++i) { - nSum = nSum * nValue; - } - return nSum; - } public static void main(String[] args) { @@ -353,6 +330,7 @@ System.out.println(d2); System.out.println(d1 + d2); + } diff --git a/src/main/java/com/fzzy/gateway/GatewayRunner.java b/src/main/java/com/fzzy/gateway/GatewayRunner.java index 5c257ee..ab5dc98 100644 --- a/src/main/java/com/fzzy/gateway/GatewayRunner.java +++ b/src/main/java/com/fzzy/gateway/GatewayRunner.java @@ -3,7 +3,6 @@ import com.fzzy.gateway.hx2023.service.ApiInitService; import com.fzzy.mqtt.MqttPublishService; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Service; diff --git a/src/main/java/com/fzzy/gateway/GatewayUtils.java b/src/main/java/com/fzzy/gateway/GatewayUtils.java index 076834c..e1e132f 100644 --- a/src/main/java/com/fzzy/gateway/GatewayUtils.java +++ b/src/main/java/com/fzzy/gateway/GatewayUtils.java @@ -4,7 +4,6 @@ import com.fzzy.api.data.ApiCommonDevice; import com.fzzy.api.data.GatewayDeviceType; import com.fzzy.gateway.entity.GatewayDevice; -import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import java.util.*; diff --git a/src/main/java/com/fzzy/protocol/ProtocolUtils.java b/src/main/java/com/fzzy/protocol/ProtocolUtils.java index 6630c7c..63c9a93 100644 --- a/src/main/java/com/fzzy/protocol/ProtocolUtils.java +++ b/src/main/java/com/fzzy/protocol/ProtocolUtils.java @@ -3,6 +3,7 @@ import com.fzzy.gateway.data.BaseReqData; import com.fzzy.protocol.data.THDto; +import io.netty.channel.Channel; import java.util.HashMap; import java.util.Map; @@ -11,6 +12,10 @@ * 鍗忚瑙f瀽鍏ㄥ眬甯搁噺 */ public class ProtocolUtils { + + + + /** * 璇锋眰缂撳瓨 key = deviceId,value = requData @@ -52,4 +57,5 @@ public static THDto getCacheTh(String deviceId) { return contextThMap.get(deviceId); } + } diff --git a/src/main/java/com/fzzy/protocol/youxian0/ServiceUtils.java b/src/main/java/com/fzzy/protocol/youxian0/ServiceUtils.java new file mode 100644 index 0000000..a70f734 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/youxian0/ServiceUtils.java @@ -0,0 +1,26 @@ +package com.fzzy.protocol.youxian0; + + +/** + * 宸ュ叿绫� + */ +public class ServiceUtils { + + + //璺敱=6A + public static String FUNCTION_6A = "6a"; + + //鎼滅储=62 + public static String FUNCTION_62 = "6a"; + + //娴嬫俯=66 + public static String FUNCTION_66 = "66"; + + //浠撳婀垮害=67 + public static String FUNCTION_67 = "67"; + + //浠撳唴婀垮害=68 + public static String FUNCTION_68 = "68"; + + +} diff --git a/src/main/java/com/fzzy/protocol/youxian0/analysis/AnalysisService.java b/src/main/java/com/fzzy/protocol/youxian0/analysis/AnalysisService.java new file mode 100644 index 0000000..0b225f2 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/youxian0/analysis/AnalysisService.java @@ -0,0 +1,96 @@ +package com.fzzy.protocol.youxian0.analysis; + +import com.fzzy.api.Constant; +import com.fzzy.api.utils.BytesUtil; +import com.fzzy.gateway.GatewayUtils; +import com.fzzy.gateway.entity.GatewayDevice; +import com.fzzy.protocol.ProtocolUtils; +import com.fzzy.protocol.youxian0.ServiceUtils; +import org.springframework.stereotype.Component; + +import java.net.InetAddress; + +/** + * 娓镐粰涓诲簱锛岃繑鍥炴姤鏂囪В鏋� + */ +@Component(AnalysisService.BEAN_ID) +public class AnalysisService { + + public static final String BEAN_ID = "youxian0.analysisService"; + + + /** + * 鍗忚瑙f瀽 + * <p> + * 7E 00 02 02 00 77 00 00 A0 FF FF 66 FF 04 BE 01 64 6A 6B 6A 68 CE 02 A4 BB BA BA B4 CE 03 E4 32 3D 32 3D CE 04 44 BC B3 BC B3 20 47 7E + * + * @param address + * @param port + * @param strMsg + */ + public void analysis(InetAddress address, int port, String strMsg) { + + //鍒嗘満ID + int start = 2 * 2; + String deviceSn = strMsg.substring(start, start + 2); + deviceSn = BytesUtil.hexToInt(deviceSn) + ""; + + + //鍛戒护ID + start = 5 * 2; + String msgIdHex = strMsg.substring(start, start + 2); + int msgId = BytesUtil.hexToInt(msgIdHex); + + //鍛戒护绫诲瀷 + start = 11 * 2; + String funId = strMsg.substring(start, start + 2); + + + GatewayDevice device = GatewayUtils.getCacheByDeviceSn(deviceSn); + + //绮儏杩斿洖 + if (ServiceUtils.FUNCTION_66.equalsIgnoreCase(funId)) { + this.analysisGrain(device, msgId, strMsg); + } + + } + + /** + * 绮儏瑙f瀽 + * <p> + * 7E 00 02 02 00 77 00 00 A0 FF FF 66 FF 04 BE 01 64 6A 6B 6A 68 CE + * 02 A4 BB BA BA B4 CE 03 E4 32 3D 32 3D CE 04 44 BC B3 BC B3 20 47 7E + * + * @param device + * @param msgId 鍛戒护ID + */ + private void analysisGrain(GatewayDevice device, int msgId, String strMsg) { + + //鍙繚鐣欑伯鎯呬俊鎭� + int start = 22 * 2; + strMsg = strMsg.substring(start); + + + //瀵嗛挜鍜岀偣鏁� + String kyeNumHex = strMsg.substring(2, 4); + String kyeNumBin = BytesUtil.toBinary8String(BytesUtil.hexToInt(kyeNumHex)); + + String key = kyeNumBin.substring(0, 3); + int keyValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key)); + key = kyeNumBin.substring(4); + int numValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key)); + + + //02 A4 BB BA BA B4 + start = 2 * 2; + String tempHex; + for (int i = 0; i < numValue; i++) { + start = start + i * 2; + tempHex = strMsg.substring(start, start + 2); + //瀹為檯娓╁害锛濆瘑閽�*瀵嗛挜*37(婧㈠嚭涓烘棤绗﹀彿瀛楄妭)鍐嶅紓鎴栧姞瀵嗗悗鐨勬俯搴�/2銆� + + } + + + } +} diff --git a/src/main/java/com/fzzy/protocol/youxian0/client/ClientEngine.java b/src/main/java/com/fzzy/protocol/youxian0/client/ClientEngine.java new file mode 100644 index 0000000..044b901 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/youxian0/client/ClientEngine.java @@ -0,0 +1,147 @@ +package com.fzzy.protocol.youxian0.client; + +import com.fzzy.api.utils.BytesUtil; +import com.fzzy.protocol.ProtocolUtils; +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 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()); + } + }); + + // 鍙戣捣寮傛杩炴帴璇锋眰锛岀粦瀹氳繛鎺ョ鍙e拰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={},杩炴帴鎴愬姛"); + } else { + log.info("-----IP={},杩炴帴澶辫触锛岃嚜鍔ㄥ叧闂嚎绋�"); + 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; + } + + +} diff --git a/src/main/java/com/fzzy/protocol/youxian0/client/ClientHandler.java b/src/main/java/com/fzzy/protocol/youxian0/client/ClientHandler.java new file mode 100644 index 0000000..0e99869 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/youxian0/client/ClientHandler.java @@ -0,0 +1,59 @@ +package com.fzzy.protocol.youxian0.client; + +import com.fzzy.api.utils.BytesUtil; +import com.fzzy.api.utils.SpringUtil; +import com.fzzy.protocol.youxian0.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("杩炴帴缁堢鎺夌嚎锛孖P={},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("缁堢杩斿洖淇℃伅锛孖P={},port={}锛宮sg={}", socketAddress.getAddress(), socketAddress.getPort(), strMsg); + + if (null == analysisService) { + analysisService = SpringUtil.getBean(AnalysisService.class); + } + + analysisService.analysis(socketAddress.getAddress(), socketAddress.getPort(), strMsg); + + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + ctx.close(); + } + +} diff --git a/src/main/java/com/fzzy/protocol/youxian0/data/GrainRoot.java b/src/main/java/com/fzzy/protocol/youxian0/data/GrainRoot.java new file mode 100644 index 0000000..7221834 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/youxian0/data/GrainRoot.java @@ -0,0 +1,19 @@ +package com.fzzy.protocol.youxian0.data; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class GrainRoot { + + private long time = System.currentTimeMillis(); + + private int num; + + //閫楀彿闅斿紑鐨勬俯搴﹀�� + private String points; + + private String key; + +} diff --git a/src/main/java/com/fzzy/protocol/youxian0/package-info.java b/src/main/java/com/fzzy/protocol/youxian0/package-info.java new file mode 100644 index 0000000..b22a9c2 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/youxian0/package-info.java @@ -0,0 +1,4 @@ +/** + * 娓镐粰涓诲簱锛岄�氳鍗忚 + */ +package com.fzzy.protocol.youxian0; \ No newline at end of file diff --git a/src/main/java/com/fzzy/protocol/youxian0/service/Youxian0GatewayGrainService.java b/src/main/java/com/fzzy/protocol/youxian0/service/Youxian0GatewayGrainService.java new file mode 100644 index 0000000..070c5bd --- /dev/null +++ b/src/main/java/com/fzzy/protocol/youxian0/service/Youxian0GatewayGrainService.java @@ -0,0 +1,228 @@ +package com.fzzy.protocol.youxian0.service; + +import com.fzzy.api.data.GatewayDeviceProtocol; +import com.fzzy.api.utils.BytesUtil; +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.ServiceUtils; +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; + +/** + * 娓镐粰涓诲簱鍗忚-绮儏閲囬泦鍏ュ彛 + */ +@Slf4j +@Component +public class Youxian0GatewayGrainService implements GatewaySyncGranService { + + + @Override + public String getGrainProtocol() { + return GatewayDeviceProtocol.GRAIN_YOUXIAN0_2023.getCode(); + } + + /** + * 鍙戦�乕娴嬫俯]鍛戒护 9绮惧害鐨� 杩斿洖娓╁害鏁版嵁鏄崟涓瓧鑺� + * 鍙戦�佹暟鎹�: 7e 01 00 00 01 0b 00 02 00 01 a0 ff ff 66 ff 05 4e 83 7e + * 鎺ユ敹鏁版嵁: 00 01 01 00 0B 00 00 A0 FF FF 66 FF 05 BE 01 64 78 7B 79 79 CC 02 A4 A8 A8 A9 A8 (鍘绘帀7e澶�7e灏� 鍘绘帀鏈�鍚庝袱浣峜rc鏍¢獙) + * 鏉嗗彿:1 [ 26.5 27.0 26.0 26.0 ] + * 鏉嗗彿:2 [ 26.0 27.0 26.0 26.0 ] + * + * @param reqData + * @return + */ + @Override + public 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[] attCable = device.getCableRule().split("-"); + int cableY = Integer.valueOf(attCable[1]); + int cableX = Integer.valueOf(attCable[2]); + + int start = 0, length = cableY; + // 鐢熸垚绮儏淇℃伅 + String hexStr = ""; + InvokeResult message; + for (int i = 1; i <= cableX; i++) { + if (1 == i) start = 255; + start = i * cableY + 1; + + hexStr = this.buildGrainCmd(device, i, start, length); + + // 鍙戦�佸懡浠� + Channel channel = ClientEngine.getChannel(device.getIp()); + if (null == channel) { + ClientEngine clientEngine = new ClientEngine(device.getIp(), device.getPort()); + clientEngine.start(); + Thread.sleep(500); + channel = clientEngine.getChannel(); + } + message = ClientEngine.send2(hexStr, channel); + + log.error("骞冲彴------>>>>鎺у埗鏌滐細鍙戦�佺伯鎯呮娴嬪懡浠�-{}", message); + + // 灏佽杩斿洖淇℃伅 + 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; + } + + /** + * 鐢熸垚绮儏閲囬泦鍛戒护 + * + * @param device + * @return + */ + private String buildGrainCmd(GatewayDevice device, int cur, int startCur, int length) { + + 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}"; + + //寮�濮嬪皝瑁呮秷鎭綋-涓绘満ID + String deviceSn = device.getDeviceSn(); + deviceSn = BytesUtil.intToHexStr1(Integer.valueOf(deviceSn)); + content = content.replaceAll("\\{id}", deviceSn); + + //鍛戒护ID + String msgIdHex = BytesUtil.intToHexStr1(cur); + content = content.replace("\\{msgId}", msgIdHex); + + //鍛戒护绫诲瀷 + content = content.replace("\\{funId}", ServiceUtils.FUNCTION_66); + + //寮�濮嬫牴鍙� + String startCurHex = BytesUtil.intToHexStr1(startCur); + content = content.replace("\\{start}", startCurHex); + + //鎴彇闀垮害 + String lenHex = BytesUtil.intToHexStr1(length); + content = content.replace("\\{length}", lenHex); + + //鏍¢獙鐮� + String crcCode = this.getCRC(content); + + String end = "7e"; + + return start + content + crcCode + end; + } + + /** + * 鍙傝�冪粨鏋滐細http://www.ip33.com/crc.html + * <p> + * 7e 01 00 00 01 00 00 02 00 01 a0 ff ff 66 ff 05 3a 59 7e + * <p> + * 娴嬭瘯锛�010000010000020001a0ffff66ff05 + * 杩斿洖锛�3a59 + * + * @param content + * @return + */ + private String getCRC(String content) { + byte[] bytes = HexStringToBytes(content);//16杩涘埗瀛楃涓茶浆鎴�16杩涘埗瀛楃涓叉暟缁� + int i = CRC16_XMODEM(bytes);//杩涜CRC鈥擷MODEM鏍¢獙寰楀埌鍗佽繘鍒舵牎楠屾暟 + String CRC = Integer.toHexString(i);//10杩涘埗杞�16杩涘埗 + + //璋冩暣楂樹綅鍦ㄥ彸锛屽湴浣嶅湪宸︿晶 + CRC = tran_LH(CRC); + + return CRC; + } + + public static String tran_LH(String info) { + return info.substring(2) + info.substring(0, 2); + } + + public static int CRC16_XMODEM(byte[] buffer) { + int wCRCin = 0x0000; // initial value 65535 + int wCPoly = 0x1021; // 0001 0000 0010 0001 (0, 5, 12) + for (byte b : buffer) { + for (int i = 0; i < 8; i++) { + boolean bit = ((b >> (7 - i) & 1) == 1); + boolean c15 = ((wCRCin >> 15 & 1) == 1); + wCRCin <<= 1; + if (c15 ^ bit) + wCRCin ^= wCPoly; + } + } + wCRCin &= 0xffff; + return wCRCin ^= 0x0000; + } + + public static byte[] HexStringToBytes(String src) { + int len = src.length() / 2; + byte[] ret = new byte[len]; + byte[] tmp = src.getBytes(); + for (int i = 0; i < len; i++) { + ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]); + } + return ret; + } + + public static byte uniteBytes(byte src0, byte src1) { + byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})) + .byteValue(); + _b0 = (byte) (_b0 << 4); + byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})) + .byteValue(); + byte ret = (byte) (_b0 ^ _b1); + return ret; + } + + + @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(); + } +} -- Gitblit v1.9.3