vince
2023-07-04 23d05bdda8f245fa61a5d23980f1191a874a44a5
邦海出入库控制协议
已删除1个文件
已修改5个文件
已添加11个文件
1069 ■■■■■ 文件已修改
igds-core/src/main/java/com/ld/igds/inout/ApiInoutService.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
igds-core/src/main/java/com/ld/igds/io/constant/ProtocolEnum.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
igds-protocol-bhzn/pom.xml 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/BHZNWeightServiceImpl.java 188 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/BhznInoutService.java 63 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/client/BHZNClientEngine.java 119 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/client/ClientHandler.java 52 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/client/ReqGate.java 18 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/utils/CRC.java 52 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/utils/CRC16.java 135 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/utils/CRC8.java 186 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/analysis/AnalysisES.java 95 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/analysis/AnalysisService.java 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/builder/EsCommandBuilder.java 52 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/command/RemoteEsServiceImpl.java 48 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/verb/dto/Res208.java 42 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
igds-web/src/main/resources/static/admin/inout/in-weight.js 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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);
    /**
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", "邦海智能无线主机版V1"),
    TCP_BHZH_GRAIN_V2("TCP_BHZH_GRAIN_V2", "邦海智能有线版V2"),
    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;
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>
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和actionCode ä¸å¯ä¸ºç©º
     * @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和actionCode ä¸å¯ä¸ºç©º
     * @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="粤A123456",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方法:十六进制字符串转byte[],网上很多
    //getRepair0方法:补0的,很简单,自己实现吧
    /**
     *
     * new StringBuffer(str).reverse()
     * è½¬ask码
     * @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();
    }
}
igds-protocol-bhzn/src/main/java/com/ld/igds/protocol/bhzn/inout/BhznInoutService.java
ÎļþÒÑɾ³ý
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());
                    }
                });
        // å‘起异步连接请求,绑定连接端口和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;
    }
}
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("出入库控制器成功连接,IP={},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("出入库控制器断开连接,IP={},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();
    }
}
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};
}
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)));
    }
}
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));
    }
}
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 };
    /**
     * è®¡ç®—数组的CRC8校验值
     *
     * @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;
    }
}
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);
    }
}
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("控制柜命令码解析失败!");
                    break;
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);
        // ç”Ÿæˆæ•´ä½“命令信息和BYte信息
        build(message);
        return message;
    }
    public void buildInfo(IoMessage message, BaseRequest request) {
            message.setContentStr("{}");
    }
}
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());
        }
    }
}
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 ;//总能耗
}
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,