2021与蓝度共同重构项目,服务端
fix
zhanzhiqin
2022-05-27 feac6e0c2db4e60d55417fde1971a0bc6d703566
ximon-admin/src/main/java/com/sandu/ximon/admin/manager/iot/frame/inner/report/A5LightHeartbeatReportInnerFrame.java
@@ -69,7 +69,7 @@
        private Integer sec;
        //  设备温度 2  1字节整数1字节小数
        private Double deviceTemperature;
        //  亮度百分比 1
        //  灯1亮度百分比 1
        private Integer lightPercent;
        //  电网电压 2 1字节整数1字节小数
        private Double voltage;
@@ -91,6 +91,8 @@
        private Double recentlyUsingPower;
        //  累计用电电量 4
        private Double totalUsingPower;
        //  灯2亮度百分比 1
        private Integer light2Percent;
        //  保留 11
        private String retain;
        //  原帧
@@ -106,7 +108,9 @@
            this.min = HexUtil.hexToInt(hex.substring(8, 10));
            this.sec = HexUtil.hexToInt(hex.substring(10, 12));
            this.deviceTemperature = NumberUtil.round(HexUtil.hexToInt(hex.substring(12, 16)) * 0.01, 2).doubleValue();
//            //需要把十六进制转二进制
//            this.deviceTemperature = NumberUtil.round(HexUtil.hexToInt(hex.substring(12, 16)) * 0.01, 2).doubleValue();
            this.deviceTemperature = NumberUtil.round(temperatureTransition(hex.substring(12, 16)), 2).doubleValue();
            this.lightPercent = HexUtil.hexToInt(hex.substring(16, 18));
            this.voltage = NumberUtil.round(HexUtil.hexToInt(hex.substring(18, 22)) * 0.1, 1).doubleValue();
            this.electricCurrent = NumberUtil.round(HexUtil.hexToInt(hex.substring(22, 26)) * 0.001, 3).doubleValue();
@@ -118,13 +122,54 @@
            this.totalLightSec = HexUtil.hexToLong(hex.substring(50, 58));
            this.recentlyUsingPower = NumberUtil.round(HexUtil.hexToInt(hex.substring(58, 66)) * (double) 0.001, 3).doubleValue();
            this.totalUsingPower = NumberUtil.round(HexUtil.hexToInt(hex.substring(66, 74)) * (double) 0.001, 3).doubleValue();
            this.retain = hex.substring(74, 96);
            this.light2Percent = HexUtil.hexToInt(hex.substring(74, 76));
            this.retain = hex.substring(76, 96);
            return this;
        }
        /**
         * 将16进制转成2进制,进行补码(反码基础上+1),得到正确数值
         * 传入16进制的温度,类型为String
         * 如F500
         */
        public static Double temperatureTransition(String temperature) {
            //将传进来的16进制的转为2进制
            String twoBinStr = hexStr2BinStr(temperature);
            if ("1".equals(twoBinStr.substring(0, 1))) {
                //最高位是1,为负数,将16进制的进行补码,返回
                int max = 0b1111111111111111;
                double result = (max-HexUtil.hexToInt(temperature))*(-0.01);
                return result;
            } else if ("0".equals(twoBinStr.substring(0, 1))) {
                //最高位是0,正数,直接返回
                double result = (HexUtil.hexToInt(temperature))*(0.01);
                return result;
            }
            //
            return 0.00;
        }
        /**
         * 16进制字符串转为二进制
         * */
        public static String hexStr2BinStr(String hexStr)
        {
            if (hexStr == null || hexStr.length() % 2 != 0)
            {
                return null;
            }
            String bString = "", tmp;
            for (int i = 0; i < hexStr.length(); i++)
            {
                tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexStr.substring(i, i + 1), 16));
                bString += tmp.substring(tmp.length() - 4);
            }
            return bString;
        }
    }
    public static void main(String[] args) {
        System.out.println(HexUtil.hexToLong("F64D020F"));
        System.out.println(5 * (float) 0.001);
    }
}