From 23d05bdda8f245fa61a5d23980f1191a874a44a5 Mon Sep 17 00:00:00 2001 From: vince <757871790@qq.com> Date: 星期二, 04 七月 2023 18:10:40 +0800 Subject: [PATCH] 邦海出入库控制协议 --- igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/analysis/AnalysisService.java | 6 igds-core/src/main/java/com/ld/igds/io/constant/ProtocolEnum.java | 2 igds-web/src/main/resources/static/admin/inout/in-weight.js | 1 igds-protocol-bhzn/pom.xml | 6 igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/client/BHZNClientEngine.java | 119 ++++++ igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/analysis/AnalysisES.java | 95 +++++ igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/client/ClientHandler.java | 52 ++ igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/builder/EsCommandBuilder.java | 52 ++ igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/utils/CRC8.java | 186 ++++++++++ igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/dto/Res208.java | 42 ++ /dev/null | 63 --- igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/utils/CRC.java | 52 ++ igds-core/src/main/java/com/ld/igds/inout/ApiInoutService.java | 4 igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/command/RemoteEsServiceImpl.java | 48 ++ igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/utils/CRC16.java | 135 +++++++ igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/client/ReqGate.java | 18 + igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/BHZNWeightServiceImpl.java | 188 ++++++++++ 17 files changed, 1,002 insertions(+), 67 deletions(-) diff --git a/igds-core/src/main/java/com/ld/igds/inout/ApiInoutService.java b/igds-core/src/main/java/com/ld/igds/inout/ApiInoutService.java index b3f9547..70d5a5e 100644 --- a/igds-core/src/main/java/com/ld/igds/inout/ApiInoutService.java +++ b/igds-core/src/main/java/com/ld/igds/inout/ApiInoutService.java @@ -51,7 +51,7 @@ * @param param * @return */ - String gateControl(ApiInoutData param); + String gateControl(ApiInoutData param, InoutData data); /** @@ -60,7 +60,7 @@ * @param param * @return */ - String lightControl(ApiInoutData param); + String lightControl(ApiInoutData param, InoutData data); /** diff --git a/igds-core/src/main/java/com/ld/igds/io/constant/ProtocolEnum.java b/igds-core/src/main/java/com/ld/igds/io/constant/ProtocolEnum.java index 6384c63..b431f12 100644 --- a/igds-core/src/main/java/com/ld/igds/io/constant/ProtocolEnum.java +++ b/igds-core/src/main/java/com/ld/igds/io/constant/ProtocolEnum.java @@ -30,7 +30,7 @@ TCP_BHZH_GRAIN_FM_V1("TCP_BHZH_GRAIN_FM_V1", "閭︽捣鏅鸿兘鏃犵嚎涓绘満鐗圴1"), TCP_BHZH_GRAIN_V2("TCP_BHZH_GRAIN_V2", "閭︽捣鏅鸿兘鏈夌嚎鐗圴2"), TCP_BHZH_VERB_V2("TCP_BHZH_VERB_V2", "閭︽捣鎺у埗鏌滄櫤鑳芥湁绾跨増V2"), - TCP_BHZH_INOTU_V1("TCP_BHZH_INOTU_V1", "閭︽捣鏅鸿兘鍑哄叆搴撴帶鍒跺櫒鍗忚"), + TCP_BHZH_INOUT_V1("TCP_BHZH_INOUT_V1", "閭︽捣鏅鸿兘鍑哄叆搴撴帶鍒跺櫒鍗忚"), FZZY_OPENAPI_HTTP("FZZY_OPENAPI_HTTP", "椋庢鑷磋繙API-HTTP鍗忚"); private String code; private String msg; diff --git a/igds-protocol-bhzn/pom.xml b/igds-protocol-bhzn/pom.xml index 13c9edc..7a1a3ad 100644 --- a/igds-protocol-bhzn/pom.xml +++ b/igds-protocol-bhzn/pom.xml @@ -20,6 +20,12 @@ <artifactId>igds-core</artifactId> <version>${igds.version}</version> </dependency> + <!-- 寮曞叆鑳借�楀寘 --> + <dependency> + <groupId>com.ld.igds</groupId> + <artifactId>igds-es</artifactId> + <version>${igds.version}</version> + </dependency> <!-- 寮曞叆IO鍖� --> <dependency> diff --git a/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/BHZNWeightServiceImpl.java b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/BHZNWeightServiceImpl.java new file mode 100644 index 0000000..d09d9be --- /dev/null +++ b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/BHZNWeightServiceImpl.java @@ -0,0 +1,188 @@ +package com.ld.igds.protocol.bhzn.inout; + +import com.ld.igds.inout.ApiInoutService; +import com.ld.igds.inout.dto.ApiInoutData; +import com.ld.igds.inout.dto.InoutData; +import com.ld.igds.io.constant.ProtocolEnum; +import com.ld.igds.protocol.bhzn.inout.client.BHZNClientEngine; +import com.ld.igds.protocol.bhzn.utils.CRC16; +import com.ld.igds.util.BytesUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.io.UnsupportedEncodingException; + +/** + * @Desc: 鍦扮涓绘帶鍗忚瀹炵幇锛屽嚭鍏ュ簱鐩稿叧鎺ュ彛 + */ +@Slf4j +@Component +public class BHZNWeightServiceImpl implements ApiInoutService { + +// @Autowired +// private Inoutservice inoutservice; + + @Override + public String getProtocol() { + return ProtocolEnum.TCP_BHZH_INOUT_V1.getCode(); + } + + /** + * @param param + * @return + */ + @Override + public ApiInoutData getPlateNum(ApiInoutData param) { + return param; + } + + /** + * 涓嶉渶瑕佸疄鐜� + * + * @param param + * @return + */ + @Override + public String initLpr(ApiInoutData param) { + return "褰撳墠妯″紡鏃犻渶鍒濆鍖栬溅鐗岃瘑鍒�"; + } + + + /** + * 鏆傛椂涓嶅疄鐜� + * + * @param param deviceId鍜宎ctionCode 涓嶅彲涓虹┖ + * @return + */ + @Override + public String gateControl(ApiInoutData param, InoutData data) { + try{ +// ReqGate reqGate = new ReqGate(); + BHZNClientEngine test = new BHZNClientEngine( + param.getIp(), param.getPort()); + test.start(); + String msg = "<BHZN>{\"cmd\":226,\"content\":{" + + "\"value\":[1,1]," + + "\"LEDValue\":[1,1]" + + "},\"orderId\":\"5009\",\"result\":\"\",\"sign\":\"0000\",\"stNum\":1,\"version\":\"V1.0000000\"}<END>"; + + // Thread.sleep(3000L); + + test.send(BytesUtil.hexStrToBytes(msg)); + }catch (Exception e){ + log.error(e.getMessage(),e); + return "ERROR"; + } + + return "SUCCESS"; + } + + @Override + public String lightControl(ApiInoutData param, InoutData data) { + return null; + } + + @Override + public String noticeRadio(ApiInoutData param, InoutData data) { + return null; + } + + @Override + public String noticeLed(ApiInoutData param, InoutData data) { + // todo + + return null; + } + + /** + * 澧炲姞鐧藉悕鍗� --璋冪敤瀛愮嚎绋嬪疄鐜� + * + * @param param deviceId鍜宎ctionCode 涓嶅彲涓虹┖ + * @return + */ + public String addWhitePlate(ApiInoutData param) { + + return "SUCCESS"; + } + + /** + * 鍒犻櫎鐧藉悕鍗�---璋冪敤瀛愮嚎绋嬪疄鐜� + * + * @param param + * @return + */ + public String delWhitePlate(ApiInoutData param) { + + return "SUCCESS"; + } + + + /** + * @param param + * @return + */ + @Override + public ApiInoutData getIcCardNum(ApiInoutData param) { + return param; + } + + + + public static void main(String[] args) { + try{ + String str1 = "璁稿浆",str2="绮123456",str3 = "3鍙蜂粨" ,str4 = "灏忛害",str5="286000",str6 = "30000",str7 ="鍚堟牸"; + String test = "鎴戠埍涓埅杞欢"; + //78340100290000000000000000 1400 0B00 01 02 00 01 0C 00 + + String lengh = BytesUtil.tran_LH(BytesUtil.intToHexStr(8+test.length())); + String msg = "78340100290000000000000000 " + lengh+ "0B00010200010C00"; + msg = msg + convertStr(test); + msg = msg.toUpperCase(); + +// String t = "78 34 01 00 29 BC FD 00 00 00 00 00 00 14 00 01 00 01 02 06 01 0C 00 CE D2 B0 AE D6 D0 BA BD C8 ED BC FE"; +// t = t.replaceAll(" ",""); + String check = BytesUtil.tran_LH(BytesUtil.intToHexStr(CRC16.calcCrc16(BytesUtil.hexStrToBytes(msg)))); + System.out.println(check); + + msg+=check; + msg += "A5"; + + System.out.println(msg); + }catch (Exception e){ + e.printStackTrace(); + } + + + } + + + + + //hexToBytes鏂规硶锛氬崄鍏繘鍒跺瓧绗︿覆杞琤yte[]锛岀綉涓婂緢澶� + //getRepair0鏂规硶锛氳ˉ0鐨勶紝寰堢畝鍗曪紝鑷繁瀹炵幇鍚� + /** + * + * new StringBuffer(str).reverse() + * 杞琣sk鐮� + * @param msg + * @return + * @throws UnsupportedEncodingException + */ + public static String convertStr(String msg) throws Exception { + //鍏堟妸瀛楃涓叉寜gb2312杞垚byte鏁扮粍 + byte[] bytes = msg.getBytes("gb2312"); + StringBuilder gbString = new StringBuilder(); + + for (byte b : bytes) + { + // 鍐嶇敤Integer涓殑鏂规硶锛屾妸姣忎釜byte杞崲鎴�16杩涘埗杈撳嚭 + String temp = Integer.toHexString(b); + //鍒ゆ柇杩涜鎴彇 + if(temp.length()>=8){ + temp = temp.substring(6, 8); + } + gbString.append(temp); + } + return gbString.toString(); + } +} diff --git a/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/BhznInoutService.java b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/BhznInoutService.java deleted file mode 100644 index 2ce862e..0000000 --- a/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/BhznInoutService.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.ld.igds.protocol.bhzn.inout; - -import com.ld.igds.inout.ApiInoutService; -import com.ld.igds.inout.dto.ApiInoutData; -import com.ld.igds.inout.dto.InoutData; -import com.ld.igds.io.constant.ProtocolEnum; -import org.springframework.stereotype.Component; - -/** - * @Desc: 閭︽捣鏅鸿兘鍑哄叆搴撴帶鍒舵帴鍙e疄鐜� - * @author: andy.jia - * @update-time: 2023/7/4 15:48 - */ -@Component -public class BhznInoutService implements ApiInoutService { - - @Override - public String getProtocol() { - return ProtocolEnum.TCP_BHZH_INOTU_V1.getCode(); - } - - @Override - public ApiInoutData getIcCardNum(ApiInoutData param) { - param.setErrorMsg("褰撳墠鍗忚涓嶆敮鎸�"); - return param; - } - - @Override - public ApiInoutData getPlateNum(ApiInoutData param) { - param.setErrorMsg("褰撳墠鍗忚涓嶆敮鎸�"); - return param; - } - - @Override - public String initLpr(ApiInoutData param) { - return "褰撳墠鍗忚涓嶆敮鎸�"; - } - - @Override - public String gateControl(ApiInoutData param) { - - //TODO - return null; - } - - @Override - public String lightControl(ApiInoutData param) { - //TODO - return null; - } - - @Override - public String noticeRadio(ApiInoutData param, InoutData data) { - //TODO - return null; - } - - @Override - public String noticeLed(ApiInoutData param, InoutData data) { - //TODO - return null; - } -} diff --git a/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/client/BHZNClientEngine.java b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/client/BHZNClientEngine.java new file mode 100644 index 0000000..76842b9 --- /dev/null +++ b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/client/BHZNClientEngine.java @@ -0,0 +1,119 @@ +package com.ld.igds.protocol.bhzn.inout.client; + +import com.ld.igds.util.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; + +/** + * 鍑哄叆搴撴帶鍒跺櫒 鏈嶅姟锛屽钩鍙颁綔涓哄鎴风涓诲姩杩炴帴 + * + * @author vicne + * @description: + * @version: + */ +public class BHZNClientEngine implements Runnable { + + private String host; + private int port; + // public ChannelFuture channelFuture; + public Channel channel; + + public BHZNClientEngine(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 { + startRun(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void startRun() 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.channel = channelFuture.channel(); + // channelFuture.channel().closeFuture().sync(); + + channelFuture.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture arg0) throws Exception { + if (channelFuture.isSuccess()) { + System.out.println("杩炴帴鏈嶅姟鍣ㄦ垚鍔�"); + } else { + System.out.println("杩炴帴鏈嶅姟鍣ㄥけ璐�"); + channelFuture.cause().printStackTrace(); + group.shutdownGracefully(); // 鍏抽棴绾跨▼缁� + } + } + }); + } + + public InvokeResult send(byte[] array) throws InterruptedException { + if (null == channel) { + return InvokeResult.SOCKET_NOT_CREATE; + } + if (!channel.isActive()) { + return InvokeResult.CHANNEL_CLOSED; + } + channel.writeAndFlush(Unpooled.copiedBuffer(array)).sync(); + + return InvokeResult.SUCCESS; + } + + public static void main(String[] args) { + try { + + String msg = "000000000006010600070012"; + BHZNClientEngine test = new BHZNClientEngine( + "192.168.1.5", 502); + + test.start(); + + msg = "000000000006010300000007"; + + // Thread.sleep(3000L); + + test.send(BytesUtil.hexStrToBytes(msg)); + + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public Channel getChannel() { + return channel; + } + +} diff --git a/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/client/ClientHandler.java b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/client/ClientHandler.java new file mode 100644 index 0000000..d90d622 --- /dev/null +++ b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/client/ClientHandler.java @@ -0,0 +1,52 @@ +package com.ld.igds.protocol.bhzn.inout.client; + +import com.ld.igds.util.BytesUtil; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.util.internal.logging.InternalLogger; +import io.netty.util.internal.logging.InternalLoggerFactory; + +import java.net.InetSocketAddress; + +/** + * Handles a client-side channel. + */ + +public class ClientHandler extends SimpleChannelInboundHandler<Object> { + private final InternalLogger log = InternalLoggerFactory.getInstance(this + .getClass()); + @Override + public void channelActive(ChannelHandlerContext ctx) { + InetSocketAddress insocket = (InetSocketAddress) ctx.channel() + .remoteAddress(); + log.info("鍑哄叆搴撴帶鍒跺櫒鎴愬姛杩炴帴锛孖P={},port={}", insocket.getAddress() + .getHostAddress(), insocket.getPort()); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + super.channelInactive(ctx); + + InetSocketAddress insocket = (InetSocketAddress) ctx.channel() + .remoteAddress(); + + log.info("鍑哄叆搴撴帶鍒跺櫒鏂紑杩炴帴锛孖P={},port={}", insocket.getAddress(),insocket.getPort()); + } + + @Override + public void channelRead0(ChannelHandlerContext ctx, Object msg) + throws Exception { + byte[] bytes = (byte[]) msg; + + log.info("鍑哄叆搴撴帶鍒跺櫒杩斿洖淇℃伅=" + BytesUtil.bytesToString(bytes)); + + InetSocketAddress insocket = (InetSocketAddress) ctx.channel() + .remoteAddress(); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + ctx.close(); + } + +} diff --git a/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/client/ReqGate.java b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/client/ReqGate.java new file mode 100644 index 0000000..d49ae79 --- /dev/null +++ b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/client/ReqGate.java @@ -0,0 +1,18 @@ +package com.ld.igds.protocol.bhzn.inout.client; + +import lombok.Data; + +/** + * @version v1.0 + * @ProjectName: igds-v4 + * @ClassName: ReqGate + * @Author: vince + * @Date: 2023/7/4 11:53 + */ +@Data +public class ReqGate { +// private Integer StNum = 1; +// private Integer Cmd = 226; + private Integer[] Value = new Integer[]{1,1}; + private Integer[] LEDValue = new Integer[]{1,1}; +} diff --git a/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/utils/CRC.java b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/utils/CRC.java new file mode 100644 index 0000000..241c604 --- /dev/null +++ b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/utils/CRC.java @@ -0,0 +1,52 @@ +package com.ld.igds.protocol.bhzn.utils; + +public class CRC { + + /*瀵勫瓨鍣ㄥ垵濮嬪��*/ + private final int regInitValue; + private int register; + /*CRC 澶氶」寮� modbus 澶氶」寮�0x8005*/ + private final int polynomial; + + public CRC() { + this(0xffff, 0x8005); + } + + public CRC(int register, int polynomial) { + this.register = register; + this.regInitValue = register; + this.polynomial = Integer.reverse(polynomial)>>>Integer.numberOfLeadingZeros(polynomial); + } + + private void byteCRC(byte byt) { + register ^= (byt&0xff); + for(int i=0; i<8; i++){ + if((register&1)==0){ + register >>= 1; + }else{ + register = (register >> 1)^polynomial; + } + } + } + + public synchronized int crc(byte[] byts) { + if(null==byts||byts.length==0){ + return 0; + } + for(int i=0; i<byts.length; i++){ + byteCRC(byts[i]); + } + int retn = register; + register = this.regInitValue; + return retn; + } + + public static void main(String[] args){ + CRC crc = new CRC(); + byte[] data = {(byte)0x81, 0x12, 0x17, (byte)0xfe}; + System.out.println(Integer.toHexString(crc.crc(data))); + data = new byte[]{2, 7}; + System.out.println(Integer.toHexString(crc.crc(data))); + } + +} \ No newline at end of file diff --git a/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/utils/CRC16.java b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/utils/CRC16.java new file mode 100644 index 0000000..fc3004d --- /dev/null +++ b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/utils/CRC16.java @@ -0,0 +1,135 @@ +package com.ld.igds.protocol.bhzn.utils; + +/*** + * + * @author Andy + * + */ +public class CRC16 { + + static byte[] crc16_tab_h = { (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40 }; + + static byte[] crc16_tab_l = { (byte) 0x00, (byte) 0xC0, (byte) 0xC1, (byte) 0x01, (byte) 0xC3, (byte) 0x03, + (byte) 0x02, (byte) 0xC2, (byte) 0xC6, (byte) 0x06, (byte) 0x07, (byte) 0xC7, (byte) 0x05, (byte) 0xC5, + (byte) 0xC4, (byte) 0x04, (byte) 0xCC, (byte) 0x0C, (byte) 0x0D, (byte) 0xCD, (byte) 0x0F, (byte) 0xCF, + (byte) 0xCE, (byte) 0x0E, (byte) 0x0A, (byte) 0xCA, (byte) 0xCB, (byte) 0x0B, (byte) 0xC9, (byte) 0x09, + (byte) 0x08, (byte) 0xC8, (byte) 0xD8, (byte) 0x18, (byte) 0x19, (byte) 0xD9, (byte) 0x1B, (byte) 0xDB, + (byte) 0xDA, (byte) 0x1A, (byte) 0x1E, (byte) 0xDE, (byte) 0xDF, (byte) 0x1F, (byte) 0xDD, (byte) 0x1D, + (byte) 0x1C, (byte) 0xDC, (byte) 0x14, (byte) 0xD4, (byte) 0xD5, (byte) 0x15, (byte) 0xD7, (byte) 0x17, + (byte) 0x16, (byte) 0xD6, (byte) 0xD2, (byte) 0x12, (byte) 0x13, (byte) 0xD3, (byte) 0x11, (byte) 0xD1, + (byte) 0xD0, (byte) 0x10, (byte) 0xF0, (byte) 0x30, (byte) 0x31, (byte) 0xF1, (byte) 0x33, (byte) 0xF3, + (byte) 0xF2, (byte) 0x32, (byte) 0x36, (byte) 0xF6, (byte) 0xF7, (byte) 0x37, (byte) 0xF5, (byte) 0x35, + (byte) 0x34, (byte) 0xF4, (byte) 0x3C, (byte) 0xFC, (byte) 0xFD, (byte) 0x3D, (byte) 0xFF, (byte) 0x3F, + (byte) 0x3E, (byte) 0xFE, (byte) 0xFA, (byte) 0x3A, (byte) 0x3B, (byte) 0xFB, (byte) 0x39, (byte) 0xF9, + (byte) 0xF8, (byte) 0x38, (byte) 0x28, (byte) 0xE8, (byte) 0xE9, (byte) 0x29, (byte) 0xEB, (byte) 0x2B, + (byte) 0x2A, (byte) 0xEA, (byte) 0xEE, (byte) 0x2E, (byte) 0x2F, (byte) 0xEF, (byte) 0x2D, (byte) 0xED, + (byte) 0xEC, (byte) 0x2C, (byte) 0xE4, (byte) 0x24, (byte) 0x25, (byte) 0xE5, (byte) 0x27, (byte) 0xE7, + (byte) 0xE6, (byte) 0x26, (byte) 0x22, (byte) 0xE2, (byte) 0xE3, (byte) 0x23, (byte) 0xE1, (byte) 0x21, + (byte) 0x20, (byte) 0xE0, (byte) 0xA0, (byte) 0x60, (byte) 0x61, (byte) 0xA1, (byte) 0x63, (byte) 0xA3, + (byte) 0xA2, (byte) 0x62, (byte) 0x66, (byte) 0xA6, (byte) 0xA7, (byte) 0x67, (byte) 0xA5, (byte) 0x65, + (byte) 0x64, (byte) 0xA4, (byte) 0x6C, (byte) 0xAC, (byte) 0xAD, (byte) 0x6D, (byte) 0xAF, (byte) 0x6F, + (byte) 0x6E, (byte) 0xAE, (byte) 0xAA, (byte) 0x6A, (byte) 0x6B, (byte) 0xAB, (byte) 0x69, (byte) 0xA9, + (byte) 0xA8, (byte) 0x68, (byte) 0x78, (byte) 0xB8, (byte) 0xB9, (byte) 0x79, (byte) 0xBB, (byte) 0x7B, + (byte) 0x7A, (byte) 0xBA, (byte) 0xBE, (byte) 0x7E, (byte) 0x7F, (byte) 0xBF, (byte) 0x7D, (byte) 0xBD, + (byte) 0xBC, (byte) 0x7C, (byte) 0xB4, (byte) 0x74, (byte) 0x75, (byte) 0xB5, (byte) 0x77, (byte) 0xB7, + (byte) 0xB6, (byte) 0x76, (byte) 0x72, (byte) 0xB2, (byte) 0xB3, (byte) 0x73, (byte) 0xB1, (byte) 0x71, + (byte) 0x70, (byte) 0xB0, (byte) 0x50, (byte) 0x90, (byte) 0x91, (byte) 0x51, (byte) 0x93, (byte) 0x53, + (byte) 0x52, (byte) 0x92, (byte) 0x96, (byte) 0x56, (byte) 0x57, (byte) 0x97, (byte) 0x55, (byte) 0x95, + (byte) 0x94, (byte) 0x54, (byte) 0x9C, (byte) 0x5C, (byte) 0x5D, (byte) 0x9D, (byte) 0x5F, (byte) 0x9F, + (byte) 0x9E, (byte) 0x5E, (byte) 0x5A, (byte) 0x9A, (byte) 0x9B, (byte) 0x5B, (byte) 0x99, (byte) 0x59, + (byte) 0x58, (byte) 0x98, (byte) 0x88, (byte) 0x48, (byte) 0x49, (byte) 0x89, (byte) 0x4B, (byte) 0x8B, + (byte) 0x8A, (byte) 0x4A, (byte) 0x4E, (byte) 0x8E, (byte) 0x8F, (byte) 0x4F, (byte) 0x8D, (byte) 0x4D, + (byte) 0x4C, (byte) 0x8C, (byte) 0x44, (byte) 0x84, (byte) 0x85, (byte) 0x45, (byte) 0x87, (byte) 0x47, + (byte) 0x46, (byte) 0x86, (byte) 0x82, (byte) 0x42, (byte) 0x43, (byte) 0x83, (byte) 0x41, (byte) 0x81, + (byte) 0x80, (byte) 0x40 }; + + /** + * 璁$畻CRC16鏍¢獙 + * + * @param data + * 闇�瑕佽绠楃殑鏁扮粍 + * @return CRC16鏍¢獙鍊� + */ + public static int calcCrc16(byte[] data) { + return calcCrc16(data, 0, data.length); + } + + /** + * 璁$畻CRC16鏍¢獙 + * + * @param data + * 闇�瑕佽绠楃殑鏁扮粍 + * @param offset + * 璧峰浣嶇疆 + * @param len + * 闀垮害 + * @return CRC16鏍¢獙鍊� + */ + public static int calcCrc16(byte[] data, int offset, int len) { + return calcCrc16(data, offset, len, 0xffff); + } + + /** + * 璁$畻CRC16鏍¢獙 + * + * @param data + * 闇�瑕佽绠楃殑鏁扮粍 + * @param offset + * 璧峰浣嶇疆 + * @param len + * 闀垮害 + * @param preval + * 涔嬪墠鐨勬牎楠屽�� + * @return CRC16鏍¢獙鍊� + */ + public static int calcCrc16(byte[] data, int offset, int len, int preval) { + int ucCRCHi = (preval & 0xff00) >> 8; + int ucCRCLo = preval & 0x00ff; + int iIndex; + for (int i = 0; i < len; ++i) { + iIndex = (ucCRCLo ^ data[offset + i]) & 0x00ff; + ucCRCLo = ucCRCHi ^ crc16_tab_h[iIndex]; + ucCRCHi = crc16_tab_l[iIndex]; + } + return ((ucCRCHi & 0x00ff) << 8) | (ucCRCLo & 0x00ff) & 0xffff; + } + + // 娴嬭瘯 + public static void main(String[] args) { + // 0x02 05 00 03 FF 00 , crc16=7C 09 + int crc = CRC16.calcCrc16(new byte[] { 0x02, 0x05, 0x00, 0x03, (byte) 0xff, 0x00 }); + System.out.println(String.format("0x%04x", crc)); + } +} diff --git a/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/utils/CRC8.java b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/utils/CRC8.java new file mode 100644 index 0000000..12878f9 --- /dev/null +++ b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/utils/CRC8.java @@ -0,0 +1,186 @@ +package com.ld.igds.protocol.bhzn.utils; + +/** + *璁$畻CRC8鏍¢獙鍊� + */ +public class CRC8 { + + public static void main(String[] args) { + + } + + static short[] gCrc16Table = { (short) 0x0000, (short) 0x1021, (short) 0x2042, (short) 0x3063, (short) 0x4084, + (short) 0x50a5, (short) 0x60c6, (short) 0x70e7, (short) 0xd94c, (short) 0xc96d, (short) 0xf90e, + (short) 0xe92f, (short) 0x99c8, (short) 0x89e9, (short) 0xb98a, (short) 0xa9ab, (short) 0x5844, + (short) 0x4865, (short) 0x7806, (short) 0x6827, (short) 0x18c0, (short) 0x08e1, (short) 0x3882, + (short) 0x28a3, (short) 0xcb7d, (short) 0xdb5c, (short) 0xeb3f, (short) 0xfb1e, (short) 0x8bf9, + (short) 0x9bd8, (short) 0xabbb, (short) 0xbb9a, (short) 0x4a75, (short) 0x5a54, (short) 0x6a37, + (short) 0x7a16, (short) 0x0af1, (short) 0x1ad0, (short) 0x2ab3, (short) 0x3a92, (short) 0xfd2e, + (short) 0xed0f, (short) 0xdd6c, (short) 0xcd4d, (short) 0xbdaa, (short) 0xad8b, (short) 0x9de8, + (short) 0x8dc9, (short) 0xc9cc, (short) 0xd9ed, (short) 0xe98e, (short) 0xf9af, (short) 0x8948, + (short) 0x9969, (short) 0xa90a, (short) 0xb92b, (short) 0x5af5, (short) 0x4ad4, (short) 0x7ab7, + (short) 0x6a96, (short) 0x1a71, (short) 0x0a50, (short) 0x3a33, (short) 0x2a12, (short) 0xdbfd, + (short) 0xcbdc, (short) 0xfbbf, (short) 0xeb9e, (short) 0x9b79, (short) 0x8b58, (short) 0xbb3b, + (short) 0xab1a, (short) 0x6ca6, (short) 0x7c87, (short) 0x4ce4, (short) 0x5cc5, (short) 0x2c22, + (short) 0x3c03, (short) 0x0c60, (short) 0x1c41, (short) 0x8108, (short) 0x9129, (short) 0xa14a, + (short) 0xb16b, (short) 0xc18c, (short) 0xd1ad, (short) 0xe1ce, (short) 0xf1ef, (short) 0x1231, + (short) 0x0210, (short) 0x3273, (short) 0x2252, (short) 0x52b5, (short) 0x4294, (short) 0x72f7, + (short) 0x62d6, (short) 0x9339, (short) 0x8318, (short) 0xb37b, (short) 0xa35a, (short) 0xd3bd, + (short) 0xc39c, (short) 0xf3ff, (short) 0xe3de, (short) 0x2462, (short) 0x3443, (short) 0x0420, + (short) 0x1401, (short) 0x64e6, (short) 0x74c7, (short) 0x44a4, (short) 0x5485, (short) 0xa56a, + (short) 0xb54b, (short) 0x8528, (short) 0x9509, (short) 0xe5ee, (short) 0xf5cf, (short) 0xc5ac, + (short) 0xd58d, (short) 0x3653, (short) 0x2672, (short) 0x1611, (short) 0x0630, (short) 0x76d7, + (short) 0x66f6, (short) 0x5695, (short) 0x46b4, (short) 0xb75b, (short) 0xa77a, (short) 0x9719, + (short) 0x8738, (short) 0xf7df, (short) 0xe7fe, (short) 0xd79d, (short) 0xc7bc, (short) 0x48c4, + (short) 0x58e5, (short) 0x6886, (short) 0x78a7, (short) 0x0840, (short) 0x1861, (short) 0x2802, + (short) 0x3823, (short) 0x83b9, (short) 0x9398, (short) 0xa3fb, (short) 0xb3da, (short) 0xc33d, + (short) 0xd31c, (short) 0xe37f, (short) 0xf35e, (short) 0x02b1, (short) 0x1290, (short) 0x22f3, + (short) 0x32d2, (short) 0x4235, (short) 0x5214, (short) 0x6277, (short) 0x7256, (short) 0xb5ea, + (short) 0xa5cb, (short) 0x95a8, (short) 0x8589, (short) 0xf56e, (short) 0xe54f, (short) 0xd52c, + (short) 0xc50d, (short) 0x34e2, (short) 0x24c3, (short) 0x14a0, (short) 0x0481, (short) 0x7466, + (short) 0x6447, (short) 0x5424, (short) 0x4405, (short) 0xa7db, (short) 0xb7fa, (short) 0x8799, + (short) 0x97b8, (short) 0xe75f, (short) 0xf77e, (short) 0xc71d, (short) 0xd73c, (short) 0x26d3, + (short) 0x36f2, (short) 0x0691, (short) 0x16b0, (short) 0x6657, (short) 0x7676, (short) 0x4615, + (short) 0x5634, (short) 0xedae, (short) 0xfd8f, (short) 0xcdec, (short) 0xddcd, (short) 0xad2a, + (short) 0xbd0b, (short) 0x8d68, (short) 0x9d49, (short) 0x7e97, (short) 0x6eb6, (short) 0x5ed5, + (short) 0x4ef4, (short) 0x3e13, (short) 0x2e32, (short) 0x1e51, (short) 0x0e70, (short) 0xff9f, + (short) 0xefbe, (short) 0xdfdd, (short) 0xcffc, (short) 0xbf1b, (short) 0xaf3a, (short) 0x9f59, + (short) 0x8f78, (short) 0x9188, (short) 0x81a9, (short) 0xb1ca, (short) 0xa1eb, (short) 0xd10c, + (short) 0xc12d, (short) 0xf14e, (short) 0xe16f, (short) 0x1080, (short) 0x00a1, (short) 0x30c2, + (short) 0x20e3, (short) 0x5004, (short) 0x4025, (short) 0x7046, (short) 0x6067, (short) 0x7c26, + (short) 0x6c07, (short) 0x5c64, (short) 0x4c45, (short) 0x3ca2, (short) 0x2c83, (short) 0x1ce0, + (short) 0x0cc1, (short) 0xef1f, (short) 0xff3e, (short) 0xcf5d, (short) 0xdf7c, (short) 0xaf9b, + (short) 0xbfba, (short) 0x8fd9, (short) 0x9ff8, (short) 0x6e17, (short) 0x7e36, (short) 0x4e55, + (short) 0x5e74, (short) 0x2e93, (short) 0x3eb2, (short) 0x0ed1, (short) 0x1ef0 }; + + static short[] crc8_tab = { (short) 0x00, (short) 0x07, (short) 0x0E, (short) 0x09, (short) 0x1C, (short) 0x1B, + (short) 0x12, (short) 0x15, (short) 0x38, (short) 0x3F, (short) 0x36, (short) 0x31, (short) 0x24, + (short) 0x23, (short) 0x2A, (short) 0x2D, (short) 0x70, (short) 0x77, (short) 0x7E, (short) 0x79, + (short) 0x6C, (short) 0x6B, (short) 0x62, (short) 0x65, (short) 0x48, (short) 0x4F, (short) 0x46, + (short) 0x41, (short) 0x54, (short) 0x53, (short) 0x5A, (short) 0x5D, (short) 0xE0, (short) 0xE7, + (short) 0xEE, (short) 0xE9, (short) 0xFC, (short) 0xFB, (short) 0xF2, (short) 0xF5, (short) 0xD8, + (short) 0xDF, (short) 0xD6, (short) 0xD1, (short) 0xC4, (short) 0xC3, (short) 0xCA, (short) 0xCD, + (short) 0x90, (short) 0x97, (short) 0x9E, (short) 0x99, (short) 0x8C, (short) 0x8B, (short) 0x82, + (short) 0x85, (short) 0xA8, (short) 0xAF, (short) 0xA6, (short) 0xA1, (short) 0xB4, (short) 0xB3, + (short) 0xBA, (short) 0xBD, (short) 0xC7, (short) 0xC0, (short) 0xC9, (short) 0xCE, (short) 0xDB, + (short) 0xDC, (short) 0xD5, (short) 0xD2, (short) 0xFF, (short) 0xF8, (short) 0xF1, (short) 0xF6, + (short) 0xE3, (short) 0xE4, (short) 0xED, (short) 0xEA, (short) 0xB7, (short) 0xB0, (short) 0xB9, + (short) 0xBE, (short) 0xAB, (short) 0xAC, (short) 0xA5, (short) 0xA2, (short) 0x8F, (short) 0x88, + (short) 0x81, (short) 0x86, (short) 0x93, (short) 0x94, (short) 0x9D, (short) 0x9A, (short) 0x27, + (short) 0x20, (short) 0x29, (short) 0x2E, (short) 0x3B, (short) 0x3C, (short) 0x35, (short) 0x32, + (short) 0x1F, (short) 0x18, (short) 0x11, (short) 0x16, (short) 0x03, (short) 0x04, (short) 0x0D, + (short) 0x0A, (short) 0x57, (short) 0x50, (short) 0x59, (short) 0x5E, (short) 0x4B, (short) 0x4C, + (short) 0x45, (short) 0x42, (short) 0x6F, (short) 0x68, (short) 0x61, (short) 0x66, (short) 0x73, + (short) 0x74, (short) 0x7D, (short) 0x7A, (short) 0x89, (short) 0x8E, (short) 0x87, (short) 0x80, + (short) 0x95, (short) 0x92, (short) 0x9B, (short) 0x9C, (short) 0xB1, (short) 0xB6, (short) 0xBF, + (short) 0xB8, (short) 0xAD, (short) 0xAA, (short) 0xA3, (short) 0xA4, (short) 0xF9, (short) 0xFE, + (short) 0xF7, (short) 0xF0, (short) 0xE5, (short) 0xE2, (short) 0xEB, (short) 0xEC, (short) 0xC1, + (short) 0xC6, (short) 0xCF, (short) 0xC8, (short) 0xDD, (short) 0xDA, (short) 0xD3, (short) 0xD4, + (short) 0x69, (short) 0x6E, (short) 0x67, (short) 0x60, (short) 0x75, (short) 0x72, (short) 0x7B, + (short) 0x7C, (short) 0x51, (short) 0x56, (short) 0x5F, (short) 0x58, (short) 0x4D, (short) 0x4A, + (short) 0x43, (short) 0x44, (short) 0x19, (short) 0x1E, (short) 0x17, (short) 0x10, (short) 0x05, + (short) 0x02, (short) 0x0B, (short) 0x0C, (short) 0x21, (short) 0x26, (short) 0x2F, (short) 0x28, + (short) 0x3D, (short) 0x3A, (short) 0x33, (short) 0x34, (short) 0x4E, (short) 0x49, (short) 0x40, + (short) 0x47, (short) 0x52, (short) 0x55, (short) 0x5C, (short) 0x5B, (short) 0x76, (short) 0x71, + (short) 0x78, (short) 0x7F, (short) 0x6A, (short) 0x6D, (short) 0x64, (short) 0x63, (short) 0x3E, + (short) 0x39, (short) 0x30, (short) 0x37, (short) 0x22, (short) 0x25, (short) 0x2C, (short) 0x2B, + (short) 0x06, (short) 0x01, (short) 0x08, (short) 0x0F, (short) 0x1A, (short) 0x1D, (short) 0x14, + (short) 0x13, (short) 0xAE, (short) 0xA9, (short) 0xA0, (short) 0xA7, (short) 0xB2, (short) 0xB5, + (short) 0xBC, (short) 0xBB, (short) 0x96, (short) 0x91, (short) 0x98, (short) 0x9F, (short) 0x8A, + (short) 0x8D, (short) 0x84, (short) 0x83, (short) 0xDE, (short) 0xD9, (short) 0xD0, (short) 0xD7, + (short) 0xC2, (short) 0xC5, (short) 0xCC, (short) 0xCB, (short) 0xE6, (short) 0xE1, (short) 0xE8, + (short) 0xEF, (short) 0xFA, (short) 0xFD, (short) 0xF4, (short) 0xF3 }; + + static byte[] crc8_tab2 = { (byte) 0x00, (byte) 0x07, (byte) 0x0E, (byte) 0x09, (byte) 0x1C, (byte) 0x1B, + (byte) 0x12, (byte) 0x15, (byte) 0x38, (byte) 0x3F, (byte) 0x36, (byte) 0x31, (byte) 0x24, (byte) 0x23, + (byte) 0x2A, (byte) 0x2D, (byte) 0x70, (byte) 0x77, (byte) 0x7E, (byte) 0x79, (byte) 0x6C, (byte) 0x6B, + (byte) 0x62, (byte) 0x65, (byte) 0x48, (byte) 0x4F, (byte) 0x46, (byte) 0x41, (byte) 0x54, (byte) 0x53, + (byte) 0x5A, (byte) 0x5D, (byte) 0xE0, (byte) 0xE7, (byte) 0xEE, (byte) 0xE9, (byte) 0xFC, (byte) 0xFB, + (byte) 0xF2, (byte) 0xF5, (byte) 0xD8, (byte) 0xDF, (byte) 0xD6, (byte) 0xD1, (byte) 0xC4, (byte) 0xC3, + (byte) 0xCA, (byte) 0xCD, (byte) 0x90, (byte) 0x97, (byte) 0x9E, (byte) 0x99, (byte) 0x8C, (byte) 0x8B, + (byte) 0x82, (byte) 0x85, (byte) 0xA8, (byte) 0xAF, (byte) 0xA6, (byte) 0xA1, (byte) 0xB4, (byte) 0xB3, + (byte) 0xBA, (byte) 0xBD, (byte) 0xC7, (byte) 0xC0, (byte) 0xC9, (byte) 0xCE, (byte) 0xDB, (byte) 0xDC, + (byte) 0xD5, (byte) 0xD2, (byte) 0xFF, (byte) 0xF8, (byte) 0xF1, (byte) 0xF6, (byte) 0xE3, (byte) 0xE4, + (byte) 0xED, (byte) 0xEA, (byte) 0xB7, (byte) 0xB0, (byte) 0xB9, (byte) 0xBE, (byte) 0xAB, (byte) 0xAC, + (byte) 0xA5, (byte) 0xA2, (byte) 0x8F, (byte) 0x88, (byte) 0x81, (byte) 0x86, (byte) 0x93, (byte) 0x94, + (byte) 0x9D, (byte) 0x9A, (byte) 0x27, (byte) 0x20, (byte) 0x29, (byte) 0x2E, (byte) 0x3B, (byte) 0x3C, + (byte) 0x35, (byte) 0x32, (byte) 0x1F, (byte) 0x18, (byte) 0x11, (byte) 0x16, (byte) 0x03, (byte) 0x04, + (byte) 0x0D, (byte) 0x0A, (byte) 0x57, (byte) 0x50, (byte) 0x59, (byte) 0x5E, (byte) 0x4B, (byte) 0x4C, + (byte) 0x45, (byte) 0x42, (byte) 0x6F, (byte) 0x68, (byte) 0x61, (byte) 0x66, (byte) 0x73, (byte) 0x74, + (byte) 0x7D, (byte) 0x7A, (byte) 0x89, (byte) 0x8E, (byte) 0x87, (byte) 0x80, (byte) 0x95, (byte) 0x92, + (byte) 0x9B, (byte) 0x9C, (byte) 0xB1, (byte) 0xB6, (byte) 0xBF, (byte) 0xB8, (byte) 0xAD, (byte) 0xAA, + (byte) 0xA3, (byte) 0xA4, (byte) 0xF9, (byte) 0xFE, (byte) 0xF7, (byte) 0xF0, (byte) 0xE5, (byte) 0xE2, + (byte) 0xEB, (byte) 0xEC, (byte) 0xC1, (byte) 0xC6, (byte) 0xCF, (byte) 0xC8, (byte) 0xDD, (byte) 0xDA, + (byte) 0xD3, (byte) 0xD4, (byte) 0x69, (byte) 0x6E, (byte) 0x67, (byte) 0x60, (byte) 0x75, (byte) 0x72, + (byte) 0x7B, (byte) 0x7C, (byte) 0x51, (byte) 0x56, (byte) 0x5F, (byte) 0x58, (byte) 0x4D, (byte) 0x4A, + (byte) 0x43, (byte) 0x44, (byte) 0x19, (byte) 0x1E, (byte) 0x17, (byte) 0x10, (byte) 0x05, (byte) 0x02, + (byte) 0x0B, (byte) 0x0C, (byte) 0x21, (byte) 0x26, (byte) 0x2F, (byte) 0x28, (byte) 0x3D, (byte) 0x3A, + (byte) 0x33, (byte) 0x34, (byte) 0x4E, (byte) 0x49, (byte) 0x40, (byte) 0x47, (byte) 0x52, (byte) 0x55, + (byte) 0x5C, (byte) 0x5B, (byte) 0x76, (byte) 0x71, (byte) 0x78, (byte) 0x7F, (byte) 0x6A, (byte) 0x6D, + (byte) 0x64, (byte) 0x63, (byte) 0x3E, (byte) 0x39, (byte) 0x30, (byte) 0x37, (byte) 0x22, (byte) 0x25, + (byte) 0x2C, (byte) 0x2B, (byte) 0x06, (byte) 0x01, (byte) 0x08, (byte) 0x0F, (byte) 0x1A, (byte) 0x1D, + (byte) 0x14, (byte) 0x13, (byte) 0xAE, (byte) 0xA9, (byte) 0xA0, (byte) 0xA7, (byte) 0xB2, (byte) 0xB5, + (byte) 0xBC, (byte) 0xBB, (byte) 0x96, (byte) 0x91, (byte) 0x98, (byte) 0x9F, (byte) 0x8A, (byte) 0x8D, + (byte) 0x84, (byte) 0x83, (byte) 0xDE, (byte) 0xD9, (byte) 0xD0, (byte) 0xD7, (byte) 0xC2, (byte) 0xC5, + (byte) 0xCC, (byte) 0xCB, (byte) 0xE6, (byte) 0xE1, (byte) 0xE8, (byte) 0xEF, (byte) 0xFA, (byte) 0xFD, + (byte) 0xF4, (byte) 0xF3 }; + + /** + * 璁$畻鏁扮粍鐨凜RC8鏍¢獙鍊� + * + * @param data + * 闇�瑕佽绠楃殑鏁扮粍 + * @return CRC8鏍¢獙鍊� + */ + public static byte calcCrc8(byte[] data) { + return calcCrc8(data, (short) 2, (short) (data.length - 2), (byte) 0); + } + + /** + * 璁$畻CRC8鏍¢獙鍊� + * + * @param data + * 鏁版嵁 + * @param offset + * 璧峰浣嶇疆 + * @param len + * 闀垮害 + * @return 鏍¢獙鍊� + */ + public static byte calcCrc8(byte[] data, short offset, short len) { + return calcCrc8(data, offset, len, (byte) 0); + } + + public static byte calcCrc8(byte[] ucPtr, short offset, short ucLen, byte preval) { + short ucIndex; // CRC8鏍¢獙琛ㄦ牸绱㈠紩 + short ucCRC8 = 0; // CRC8瀛楄妭鍒濆鍖� + short i = 0; + // 杩涜CRC8浣嶆牎楠� + while ((ucLen--) > 0) { + short indexI = (short) (i + offset); + short data = ucPtr[indexI]; + if (data < 0) { + int sd = Byte.toUnsignedInt(ucPtr[indexI]); + data = (short) sd; + } + ucIndex = (short) (ucCRC8 ^ data); + ucCRC8 = crc8_tab[(short) ucIndex]; + i++; + } + // 杩斿洖CRC8鏍¢獙鏁版嵁 + return (byte) (~ucCRC8); + } + + public static short crc16Byte(short crc, byte data) { + return (short) ((crc << 8) ^ gCrc16Table[((crc >> 8) ^ data) & 0xff]); + } + + public static short calculateCrc16(byte[] buf, short offset, short length) { + short crc = 0; + short i = 0; + while ((length--) > 0) { + crc = crc16Byte(crc, buf[offset + (i++)]); + } + return crc; + } +} diff --git a/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/analysis/AnalysisES.java b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/analysis/AnalysisES.java new file mode 100644 index 0000000..737ed83 --- /dev/null +++ b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/analysis/AnalysisES.java @@ -0,0 +1,95 @@ +package com.ld.igds.protocol.bhzn.verb.analysis; + +import com.alibaba.fastjson.JSONObject; +import com.ld.igds.common.CoreCommonService; +import com.ld.igds.common.CoreDeviceService; +import com.ld.igds.constant.BizType; +import com.ld.igds.es.dto.EsData; +import com.ld.igds.es.service.CoreEsService; +import com.ld.igds.gas.CoreGasService; +import com.ld.igds.io.constant.OrderRespEnum; +import com.ld.igds.io.notify.NotifyWebInvoker; +import com.ld.igds.models.DepotConf; +import com.ld.igds.models.DeviceSer; +import com.ld.igds.models.Gas; +import com.ld.igds.models.GasInfo; +import com.ld.igds.protocol.bhzn.verb.command.BaseRemoteImpl; +import com.ld.igds.protocol.bhzn.verb.dto.IoMessage; +import com.ld.igds.protocol.bhzn.verb.dto.Res208; +import com.ld.igds.protocol.bhzn.verb.dto.Res209; +import com.ld.igds.util.ContextUtil; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * 鑳借�楄В鏋� + */ +@Slf4j +@Component(AnalysisES.BEAN_ID) +public class AnalysisES extends BaseRemoteImpl { + + public static final String BEAN_ID = "bhzn.analysisEs"; + + @Autowired + private CoreDeviceService deviceService; + @Autowired + private NotifyWebInvoker notifyInvoker; + @Autowired + private CoreCommonService commonService; + @Autowired + private CoreEsService esService; + + /** + * 璁惧鐘舵�� + * + * @param reMessage + * @param ser + */ + public void analysis(IoMessage reMessage, DeviceSer ser) { + + log.info("鑳借�楀紑濮嬭В鏋�"); + Res208 res208 = JSONObject.parseObject(reMessage.getContentStr(), Res208.class); + if(2==res208.getState()){ + + }else{ + log.info("姘斾綋娌℃湁閲囬泦瀹屾垚锛屽彇娑堣В鏋愶紒"); + return; + } + DepotConf depotConf = commonService.getCacheDepotConfBySerId(ser.getCompanyId(),ser.getId() ); + + EsData esData = new EsData(); + esData.setCompanyId(ser.getCompanyId()); + esData.setDepotId(depotConf.getDepotId()); + esData.setDeviceId("0"); + esData.setDeviceName(""); + esData.setUpdateTime(new Date()); + esData.setUa(Double.valueOf(res208.getVA())); + esData.setUb(Double.valueOf(res208.getVB())); + esData.setUc(Double.valueOf(res208.getVC())); + + esData.setIa(Double.valueOf(res208.getIA())); + esData.setIb(Double.valueOf(res208.getIB())); + esData.setIc(Double.valueOf(res208.getIC())); + esData.setF(Double.valueOf(res208.getZPL())); + + esData.setEp(0.0); + esData.setEq(0.0); + esData.setEs(0.0); + esData.setEsFan(0.0); + esData.setEsAir(0.0); + esData.setEsLamp(0.0); + esData.setEs(res208.getZNH()); + log.info("鎺у埗鏌�----->>>骞冲彴锛氳兘鑰楁暟鎹В鏋愬畬鎴�-浠撳簱={}", esData.getDepotId()); + + esService.saveAndUpdateInc(esData); + } + +} diff --git a/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/analysis/AnalysisService.java b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/analysis/AnalysisService.java index f6b00ce..04f7a1f 100644 --- a/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/analysis/AnalysisService.java +++ b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/analysis/AnalysisService.java @@ -49,7 +49,8 @@ private AnalysisDevice analysisDevice; @Autowired private AnalysisGas analysisGas; - + @Autowired + private AnalysisES analysisEs; /** * @param message @@ -75,6 +76,9 @@ case 209: analysisGas.analysis(message, ser); break; + case 240: + analysisEs.analysis(message, ser); + break; default: log.error("鎺у埗鏌滃懡浠ょ爜瑙f瀽澶辫触锛�"); break; diff --git a/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/builder/EsCommandBuilder.java b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/builder/EsCommandBuilder.java new file mode 100644 index 0000000..efe145a --- /dev/null +++ b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/builder/EsCommandBuilder.java @@ -0,0 +1,52 @@ +package com.ld.igds.protocol.bhzn.verb.builder; + +import com.alibaba.fastjson.JSON; +import com.ld.igds.constant.DeviceStatus; +import com.ld.igds.io.request.BaseRequest; +import com.ld.igds.io.request.ExeDevice; +import com.ld.igds.io.request.TempControlRequest; +import com.ld.igds.protocol.bhzn.verb.dto.IoMessage; +import com.ld.igds.protocol.bhzn.verb.dto.Req248; +import com.ld.igds.protocol.bhzn.verb.server.BhznVerbServerUtils; +import com.ld.igds.util.BytesUtil; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class EsCommandBuilder extends CommandBuilder { + + private final static EsCommandBuilder instance = new EsCommandBuilder(); + + private EsCommandBuilder() { + } + + public static EsCommandBuilder getInstance() { + return instance; + } + + public IoMessage buildMessage(BaseRequest request) { + + IoMessage message = new IoMessage(); + message.setIp(request.getIp()); + message.setPort(request.getPort()); + message.setBegin(BhznVerbServerUtils.MSG_START1); + // 璋冩暣淇℃伅 + buildInfo(message, request); + message.setSign("0000"); + message.setVersion("V1.0000000"); + message.setStNum(Integer.parseInt(request.getDepotId())); + message.setSn(request.getSerId()); + message.setCmd(240); + message.setResult(""); + message.setOrderId(request.getOrderId()); + message.setEnd(BhznVerbServerUtils.MSG_END); + // 鐢熸垚鏁翠綋鍛戒护淇℃伅鍜孊Yte淇℃伅 + build(message); + + return message; + } + + public void buildInfo(IoMessage message, BaseRequest request) { + message.setContentStr("{}"); + } + +} diff --git a/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/command/RemoteEsServiceImpl.java b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/command/RemoteEsServiceImpl.java new file mode 100644 index 0000000..98d2b5d --- /dev/null +++ b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/command/RemoteEsServiceImpl.java @@ -0,0 +1,48 @@ +package com.ld.igds.protocol.bhzn.verb.command; + +import com.ld.igds.io.RemoteEsService; +import com.ld.igds.io.constant.OrderRespEnum; +import com.ld.igds.io.request.BaseRequest; +import com.ld.igds.io.response.BaseResponse; +import com.ld.igds.protocol.bhzn.verb.builder.EsCommandBuilder; +import com.ld.igds.protocol.bhzn.verb.dto.IoMessage; +import com.ld.io.api.InvokeResult; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +/** + * 褰撳墠鍗忚瀹炵幇锛岃兘鑰楀崗璁� + * + * @author jiazx + */ +@Slf4j +@Component(RemoteEsServiceImpl.BEAN_ID) +public class RemoteEsServiceImpl extends BaseRemoteImpl implements RemoteEsService { + + public static final String BEAN_ID = "bhzn.remoteEsServiceImpl"; + + @Override + public BaseResponse checkEs(BaseRequest request) { + + try { + // Step1 鐢熸垚鑳借�楀懡浠� + IoMessage message = EsCommandBuilder.getInstance().buildMessage(request); + InvokeResult result = send(request.getIp(), request.getPort(), message.getByteMsg()); + + log.error("骞冲彴------>>>>鎺у埗鏌滐細鑳借�楁煡璇�-{}", message.toString()); + + if (InvokeResult.SUCCESS == result) { + return new BaseResponse(OrderRespEnum.ORDER_SUCCESS.getCode(), + "鑳借�楀懡浠ゅ彂閫佹垚鍔燂紒"); + } else { + log.error("骞冲彴------>>>>鎺у埗鏌滐細鑳借�楁煡璇�-澶辫触{}", result.getMessage()); + return new BaseResponse(OrderRespEnum.ORDER_ERROR.getCode(), + "鍛戒护鍙戦�佸紓甯革細" + result.getMessage()); + } + + } catch (Exception e) { + log.error("鑳借�楁娴嬪紓甯革細{}", e); + return new BaseResponse(OrderRespEnum.ORDER_ERROR.getCode(), "鍚庣寮傚父锛�" + e.getMessage()); + } + } +} diff --git a/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/dto/Res208.java b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/dto/Res208.java new file mode 100644 index 0000000..0f6d3de --- /dev/null +++ b/igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/dto/Res208.java @@ -0,0 +1,42 @@ +package com.ld.igds.protocol.bhzn.verb.dto; + +import lombok.Data; + +/** + * 姘斾綋妫�娴嬭繑鍥� + */ +@Data +public class Res208 { + + private Integer State ;// 鐘舵�� + private String Error ;// 鏁呴殰淇℃伅 + private float VA ;//A鐩哥數鍘� + private float VB ;//B鐩哥數鍘� + private float VC ;//C鐩哥數鍘� + private float ZI ;//鎬荤數娴� + private float IA ;//A鐩哥數娴� + private float IB ;//B鐩哥數娴� + private float IC ;//C鐩哥數娴� + private float ZYG ;//鎬绘湁鍔熷姛鐜� + private float YGA ;//A鐩告湁鍔熷姛鐜� + private float YGB ;//B鐩告湁鍔熷姛鐜� + private float YGC ;//C鐩告湁鍔熷姛鐜� + private float ZWG ;//鎬绘棤鍔熷姛鐜� + private float WGA ;//A鐩告棤鍔熷姛鐜� + private float WGB ;//B鐩告棤鍔熷姛鐜� + private float WGC ;//C鐩告棤鍔熷姛鐜� + private float ZSG ;//鎬昏鍦ㄥ姛鐜� + private float SGA ;//A鐩歌鍦ㄥ姛鐜� + private float SGB ;//B鐩歌鍦ㄥ姛鐜� + private float SGC ;//C鐩歌鍦ㄥ姛鐜� + private float ZGY ;//鎬诲姛鐜囧洜鏁� + private float GYA ;//A鐩稿姛鐜囧洜鏁� + private float GYB ;//B鐩稿姛鐜囧洜鏁� + private float GYC ;//C鐩稿姛鐜囧洜鏁� + private float XJA ;//A鐩歌搴� + private float XJB ;//B鐩歌搴� + private float XJC ;//C鐩歌搴� + private float ZPL ;//棰戠巼 + private float ZNH ;//鎬昏兘鑰� + +} diff --git a/igds-web/src/main/resources/static/admin/inout/in-weight.js b/igds-web/src/main/resources/static/admin/inout/in-weight.js index df5b067..93b01f3 100644 --- a/igds-web/src/main/resources/static/admin/inout/in-weight.js +++ b/igds-web/src/main/resources/static/admin/inout/in-weight.js @@ -115,6 +115,7 @@ deptId: deptId, progress: recordData.progress, bizType: bizType, + bizId: recordData.id, plateNum: recordData.plateNum, id1: snapDto1.id, id2: snapDto2.id, -- Gitblit v1.9.3