vince
2024-12-13 d3e48589ea2719cdde41b87fc1ad80f4ee509bac
src/main/java/com/fzzy/protocol/youxian0/analysis/AnalysisService.java
@@ -1,7 +1,6 @@
package com.fzzy.protocol.youxian0.analysis;
import com.alibaba.fastjson.JSONObject;
import com.fzzy.api.Constant;
import com.fzzy.api.data.GatewayDeviceType;
import com.fzzy.api.utils.BytesUtil;
import com.fzzy.api.utils.NumberUtil;
@@ -39,7 +38,6 @@
    @Resource
    private GatewayRemoteManager gatewayRemoteManager;
    private static Map<String, GrainRoot> contextGrainRoot = new HashMap<>();
@@ -80,26 +78,54 @@
        //粮情返回
        if (ServiceUtils.FUNCTION_66.equalsIgnoreCase(funId)) {
            this.analysisGrainStep1(device, msgId, strMsg);
            log.info("---------开始解析粮情信息---------");
            try{
                this.analysisGrainStep1(device, msgId, strMsg);
            }catch (Exception e){
                log.error(e.getMessage(),e);
            }
            log.info("---------解析粮情信息结束---------");
        }
        //温湿度返回
        if (ServiceUtils.FUNCTION_68.equalsIgnoreCase(funId)) {
            this.analysisGrainTh(device, strMsg);
            log.info("---------开始解析仓温湿度信息---------");
            try{
                this.analysisGrainTh(device, strMsg);
            }catch (Exception e){
                log.error(e.getMessage(),e);
            }
        }
    }
    private void analysisGrainTh(GatewayDevice device, String strMsg) {
        THDto th = new THDto();
        try{
            THDto th = new THDto();
        //TODO----->>> 待解析调整,先用外部气象信息
        //系统气象站信息
        WeatherWebDto weather = WeatherWebDto.contextMap.get("default");
        th.setTempIn(Double.valueOf(weather.getTem()) - 1);
        th.setHumidityIn(Double.valueOf(weather.getHumidity()) - 1);
            //TODO----->>> 待解析调整,先用外部气象信息
            //7E 00 01 01 00 06 00 00 A0 FF FF 68 1A 05 CC 16 3A 62 36 7E
            //系统气象站信息
//        WeatherWebDto weather = WeatherWebDto.contextMap.get("default");
//        th.setTempIn(Double.valueOf(weather.getTem()) - 2);
//        th.setHumidityIn(Double.valueOf(weather.getHumidity()) - 10);
            double t,h;
            String temp = strMsg.substring(30,32);
            t =  BytesUtil.hexToInt(temp)/2;
            log.info("温度:{}",t);
            temp = strMsg.substring(32,34);
            h = BytesUtil.hexToInt(temp);
            log.info("湿度:{}",h);
            th.setTempIn(t);
            th.setHumidityIn(h);
            this.add2ThMap(device.getDepotIdSys(), th);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        this.add2ThMap(device.getDepotIdSys(), th);
    }
@@ -113,52 +139,63 @@
     * @param msgId  命令ID
     */
    private void analysisGrainStep1(GatewayDevice device, int msgId, String strMsg) {
        String[] attCable = device.getCableRule().split("-");
        int cableZ = Integer.valueOf(attCable[0]);
        int cableY = Integer.valueOf(attCable[1]);
        int cableX = Integer.valueOf(attCable[2]);
        log.info("z={},x={},y={}",cableZ,cableX,cableY);
        //获取请求信息
        BaseReqData reqData = ProtocolUtils.getSyncReq(device.getDepotIdSys());
        if (null == reqData) {
            log.error("---------没有获取到请求信息,不执行解析------{}", device.getDeviceName());
            return;
        }
        //只保留粮情信息
        int start = 22 * 2;
        int start = 15 * 2;
        strMsg = strMsg.substring(start);
        //密钥和点数
        String kyeNumHex = strMsg.substring(2, 4);
        String kyeNumBin = BytesUtil.toBinary8String(BytesUtil.hexToInt(kyeNumHex));
        String key = kyeNumBin.substring(0, 3);
        int keyValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key));
        key = kyeNumBin.substring(4);
        int numValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key));
        //02 A4 BB BA BA B4
        start = 2 * 2;
        String tempHex;
        GrainRoot grainRoot = new GrainRoot();
        grainRoot.setKey(buildGrainRootKey(device.getDeviceSn(), msgId));
        grainRoot.setNum(msgId);
        double point = 0;
        for (int i = 0; i < numValue; i++) {
            start = start + i * 2;
            tempHex = strMsg.substring(start, start + 2);
            //实际温度=密钥*密钥*37(溢出为无符号字节)再异或加密后的温度/2。
            point = this.getGrainTemp(keyValue, tempHex);
            log.debug("--------解析后的温度点----{}---{}", tempHex, point);
            grainRoot.getPoints().add(point);
        }
        String tempStr = "";
        for (int j = 0;j<cableY;j++){
            String index = BytesUtil.intToHexStr1((msgId -1)*cableY + j +1);
            tempStr = strMsg.substring(strMsg.indexOf(index),strMsg.indexOf(index) + (6+2*cableZ) );
            strMsg = strMsg.substring(strMsg.indexOf(index) + (6+2*cableZ));
            //tempStr = strMsg.substring((6+2*cableZ) * j,(6+2*cableZ)  * j + (6+2*cableZ) );
            log.info("----线缆报文----:"+tempStr);
            log.info("----剩余报文----:"+strMsg);
            //密钥和点数 02 A4 BB BA BA B4
            String kyeNumHex = tempStr.substring(2, 4);
            String kyeNumBin = BytesUtil.toBinary8String(BytesUtil.hexToInt(kyeNumHex));
            String key = "00000" + kyeNumBin.substring(0, 3);
            int keyValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key));
            key = "0000" + kyeNumBin.substring(4);
            int numValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key));
            //02 A4 BB BA BA B4
            start = 2 * 2;
            String tempHex;
            double point = 0;
            log.info("----报文----:"+tempStr);
            log.info("----密钥="+keyValue +"----点数="+numValue+"----"+"层数="+cableZ+"----");
            for (int i = 0; i < cableZ; i++) {
                start = start + (i * 2);
                tempHex = tempStr.substring(start, start + 2);
                //实际温度=密钥*密钥*37(溢出为无符号字节)再异或加密后的温度/2。
                point = this.getGrainTemp(keyValue, tempHex);
                log.info("--------解析后的温度点----{}---{}", tempHex, point);
                grainRoot.getPoints().add(point);
                start = 2 * 2;
            }
        }
        this.add2GrainMap(grainRoot);
        //判断是不是最后一包数据,如果是最后一包执行解析
        String[] attCable = device.getCableRule().split("-");
        int cableZ = Integer.valueOf(attCable[0]);
        int cableY = Integer.valueOf(attCable[1]);
        int cableX = Integer.valueOf(attCable[2]);
        if (grainRoot.getNum() == cableX) {
            analysisGrainStep2(reqData, cableZ, cableY, cableX);
        }
@@ -176,14 +213,24 @@
        List<Double> points = new ArrayList<>();
        GrainRoot root;
        List<Double> t = null;
        for (int i = 1; i <= cableX; i++) {
            root = this.getGrainRoot(buildGrainRootKey(reqData.getDevice().getDeviceSn(), i));
            if (null == root || null == root.getPoints()) {
                log.error("-----------解析获取所有粮情检测点失败,取消执行---------{}", reqData.getDevice().getDeviceName());
                return;
                log.error("-----------解析获取所有粮情检测点失败,取消执行---------{}---{}", reqData.getDevice().getDeviceName(),i);
                ;
                t = new ArrayList<>();
                for (int x = 0;x<cableY*cableZ;x++
                     ) {
                    t.add(-100.00);
                }
                //return;
                points.addAll(t);
            }else{
                points.addAll(root.getPoints());
            }
            points.addAll(root.getPoints());
        }
        //执行封装解析
@@ -210,11 +257,11 @@
        GrainOutPut outPut = new GrainOutPut();
        double max = ReMessageBuilder.MAX_TEMP, min = ReMessageBuilder.MIN_TEMP, sumT = 0.0, sumNum = cableX * cableY * cableZ;
        double max = ReMessageBuilder.MAX_TEMP, min = ReMessageBuilder.MIN_TEMP, sumT = 0.0;
        List<GrainTemp> temperature = new ArrayList<>();
        //根号
        int cableNum = 1, position = 0;
        int cableNum = 1, position = 0,sumNum = 0;
        double curTemp;
        int x = 0, y = 0, z = 0;
@@ -230,12 +277,15 @@
            temperature.add(new GrainTemp(cableNum + "", z + "", curTemp + "", position + ""));
            sumT += curTemp;
            if (curTemp > max) {
            if (curTemp > max  && curTemp < 38) {
                max = curTemp;
            }
            if (curTemp < min) {
            if (curTemp < min && curTemp > 5) {
                min = curTemp;
            }
            if(curTemp > 5 &&  curTemp < 38){
                sumT += curTemp;
                sumNum++;
            }
        }
@@ -254,7 +304,7 @@
        outPut.setTemperature(temperature);
        outPut.setAvgTemperature(NumberUtil.keepPrecision((sumT / sumNum), 1) + "");
        outPut.setMinTemperature(min + "");
        outPut.setMaxTemperature(min + "");
        outPut.setMaxTemperature(max + "");
        List<GrainTH> ths = new ArrayList<>();
@@ -281,7 +331,7 @@
            weatherStation.setId(device.getDeviceId());
        }
        weatherStation.setAirPressure(weather.getPressure());
        weatherStation.setHumidity(weather.getHumidity());
        weatherStation.setHumidity(weather.getHumidity().replaceAll("%",""));
        weatherStation.setPm(weather.getAir_pm25());
        weatherStation.setRadiation("0");
        weatherStation.setRainfallAmount(weather.getWea());
@@ -297,10 +347,10 @@
        reqData.setData(JSONObject.toJSONString(grain));
        doPushGrain(reqData);
        doPushGrain(reqData,grain);
    }
    private void doPushGrain(BaseReqData reqData) {
    private void doPushGrain(BaseReqData reqData,GrainData grainData) {
        GatewayDeviceReportService reportService = gatewayRemoteManager.getDeviceReportService(reqData.getDevice().getPushProtocol());
        if (null == reportService) {
@@ -308,9 +358,11 @@
            return;
        }
        reportService.reportGrainData(reqData);
        reqData.setData(reportService.grainData2GatewayApiInfoKafka(grainData,reqData.getDevice()).getData());
        reportService.reportGrainDataByKafka(reqData);
    }
    private void add2GrainMap(GrainRoot grainRoot) {
    private synchronized  void add2GrainMap(GrainRoot grainRoot) {
        contextGrainRoot.put(grainRoot.getKey(), grainRoot);
    }
@@ -339,8 +391,12 @@
        int num1 = BytesUtil.hexToInt(valueHex);
        int num2 = BytesUtil.hexToInt(tempHex);
        if((num1 ^ num2) / 2.0 > 35){
            return  -100.00;
        }else {
            return  (num1 ^ num2) / 2.0;
        }
        return (num1 ^ num2) / 2.0;
    }
    private void add2ThMap(String depotIdSys, THDto th) {