消防栓数据接入Java

消防栓数据接入Java,第1张

说明

接入为拓普索尔 无线智能消防栓监测终端

厂家报文说明

sample_data说明 

代码
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ByteBuf buf = (ByteBuf) msg;
        byte[] req = new byte[buf.readableBytes()];
        buf.readBytes(req);

        String strReq = Util.bytesToHexString(req);
        log.info("收到的请求是:" + strReq);
        log.info("收到请求字节长度是:" + req.length);

        log.info("header:"+Util.convertHexToString(strReq.substring(0,8)));
        strReq = strReq.substring(8);
        log.info("type:"+strReq.substring(0,2));
        strReq = strReq.substring(2);

        log.info("length:" +Integer.parseInt(strReq.substring(0,4),16));
        strReq = strReq.substring(4);

        log.info("device type:" +strReq.substring(0,2));
        strReq = strReq.substring(2);

        log.info("device id:" +Util.convertHexToString(strReq.substring(0,32)));
        strReq = strReq.substring(32);

        log.info("send_time yy :" +strReq.substring(0,2));
        log.info("send_time mm :" +strReq.substring(2,4));
        log.info("send_time dd :" +strReq.substring(4,6));
        log.info("send_time hh :" +strReq.substring(6,8));
        log.info("send_time mm :" +strReq.substring(8,10));
        log.info("send_time ss:" +strReq.substring(10,12));
        strReq = strReq.substring(12);

        // 电池电量
        log.info("battery_level:" +Integer.parseInt(strReq.substring(0,2),16));
        strReq = strReq.substring(2);

        //信号强度
        log.info("signal_strength:" +Integer.parseInt(strReq.substring(0,2),16));
        strReq = strReq.substring(2);


        String dataHex = strReq.substring(0,8);
        log.info("sample_data:" +dataHex);
        Integer deviceType = Integer.parseInt(dataHex.substring(0,1),16);

        switch (deviceType){
            case 1:
                System.out.println("表示数据阈值下限告警");
                break;
            case 2:
                System.out.println("表示数据阈值上限");
                break;
            case 3:
                System.out.println("表示设备故障");
                break;
            case 4:
                System.out.println("表示数据动态变化阈值告警");
                break;
            case 5:
                System.out.println("表示碰撞告警");
                break;
            case 6:
                System.out.println("表示倾斜告警");
                break;
            case 7:
                System.out.println("表示水流告警");
                break;
            case 8:
                System.out.println("表示进水告警");
                break;
            case 9:
                System.out.println("表示低电量告警");
                break;
            default:
                break;
        }

        System.out.println("unit:"+Util.convertHexToUnit(dataHex.substring(1,2)));


        Integer validBit = Integer.parseInt(dataHex.substring(2,3));
        System.out.println("有效位数:"+ validBit);

        BigDecimal value = new BigDecimal(Math.pow(10,-validBit) * Integer.parseInt(dataHex.substring(3,8))).setScale(4, BigDecimal.ROUND_DOWN);
        System.out.println("value:"+ value);


        strReq = strReq.substring(8);
        log.info("crc16:" +strReq.substring(0,4));
        strReq = strReq.substring(4);

        log.info("iot:" +strReq.substring(0,6));

        buf.clear();
    }
工具类
    /**
     * 单位转换
     * @param hex
     * @return
     */
    public static String convertHexToUnit(String hex){
        Integer hexInt = Integer.parseInt(hex,16);
        switch (hexInt){
            case 1:
                return "MPa";
            case 2:
                return "Bar";
            case 3:
                return "KPa";
            case 4:
                return "M";
            case 5:
                return "℃";
            case 6:
                return "m³/h";
            case 7:
                return "°";
            default:
                break;

        }
        return "";
    }

    /**
     * hex to ascii
     * @param hex
     * @return
     */
    public static String convertHexToString(String hex){
        StringBuilder sb = new StringBuilder();
        StringBuilder temp = new StringBuilder();
        for( int i=0; i

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/871754.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)