vince
2025-02-17 320350eb18e32a9a008c4e6409441730732e2e97
src/main/java/com/fzzy/protocol/bhzn/v0/analysis/AnalysisService.java
@@ -3,9 +3,13 @@
import com.alibaba.fastjson.JSONObject;
import com.fzzy.api.Constant;
import com.fzzy.api.data.ApiCommonDevice;
import com.fzzy.api.data.DepotType;
import com.fzzy.api.data.GatewayDeviceType;
import com.fzzy.api.utils.BytesUtil;
import com.fzzy.api.utils.NumberUtil;
import com.fzzy.api.utils.RedisConst;
import com.fzzy.api.utils.RedisUtil;
import com.fzzy.data.ConfigData;
import com.fzzy.gateway.GatewayUtils;
import com.fzzy.gateway.api.GatewayDeviceReportService;
import com.fzzy.gateway.api.GatewayRemoteManager;
@@ -20,8 +24,10 @@
import com.fzzy.protocol.bhzn.v0.data.IoMessage;
import com.fzzy.protocol.bhzn.v0.server.BhznGrainV0ServerEngine;
import com.fzzy.protocol.bhzn.v0.server.BhznGrainV0ServerUtils;
import com.fzzy.gateway.data.GrainCableData;
import com.fzzy.protocol.data.THDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@@ -41,6 +47,10 @@
    @Resource
    private GatewayRemoteManager gatewayRemoteManager;
    @Resource
    private ConfigData configData;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 针对分包粮情报文进行封装
@@ -97,22 +107,20 @@
     */
    private void analysisGrain(IoMessage message) {
        try {
            //根据分机SN获取设备配置信息
            GatewayDevice gatewayDevice = GatewayUtils.getCacheByDeviceSn(message.getAddr());
            GatewayDevice gatewayDevice = BhznGrainV0ServerUtils.contextOrder;
            if (null == gatewayDevice) {
                replayGrain(message);
                log.error("主机-------->>平台,解析粮情失败,未获取到系统设备配置信息:" + message.getAddr());
                return;
            }
            //根据分机地址获取分机信息
            ApiCommonDevice commonDevice = Constant.getCommonDeviceCache(message.getIp());
            if (commonDevice == null) {
                replayGrain(message);
                log.error("主机-------->>平台,解析粮情失败,未获取到系统粮情主机配置:" + message.getAddr());
                return;
            }
//            ApiCommonDevice commonDevice = Constant.getCommonDeviceCache(message.getIp());
//            if (commonDevice == null) {
//                replayGrain(message);
//                log.error("主机-------->>平台,解析粮情失败,未获取到系统粮情主机配置:" + message.getAddr());
//                return;
//            }
            //获取请求信息
            BaseReqData reqData = ProtocolUtils.getSyncReq(gatewayDevice.getDepotIdSys());
@@ -123,11 +131,7 @@
            }
            // 判断数据有没有收取完整
            String[] attCable = gatewayDevice.getCableRule().split("-");
            int cableZ = Integer.valueOf(attCable[0]);
            int cableY = Integer.valueOf(attCable[1]);
            int cableX = Integer.valueOf(attCable[2]);
            int sumPoint = cableZ * cableY * cableX;
            GrainCableData cableData = GatewayUtils.getCableData(gatewayDevice);
            //获取当前粮情温度报文
            String grainHex = message.getContent().substring(16);
@@ -136,11 +140,11 @@
            int curPoint = BytesUtil.hexToInt(BytesUtil.tran_LH(message.getContent().substring(4, 8)));
            //如果当前包的数据个数大于等于当前仓库的配置点位则表示单包返回
            if (curPoint >= sumPoint) {
            if (curPoint >= cableData.getSumNum()) {
                log.info("分机------>>>平台:粮情数据单包=" + grainHex);
                //返回粮情接收信息
                replayGrain(message);
                analysisGrain2(message, reqData, grainHex);
                analysisGrain2(message, reqData, grainHex, cableData);
                return;
            }
@@ -157,13 +161,12 @@
                grainHex = oldGrainHex + grainHex;
            }
            if (grainHex.length() >= sumPoint * 4) {
            if (grainHex.length() >= cableData.getSumNum() * 4) {
                //返回粮情接收信息
                replayGrain(message);
                log.info("分机------>>>平台:粮情数据多包,完整数据=" + grainHex);
                analysisGrain2(message, reqData, grainHex);
                analysisGrain2(message, reqData, grainHex, cableData);
                return;
            } else {
                log.info("分机------>>>平台:将第一包数据存入内存=" + grainHex);
                contextMapGrain.put(key, grainHex);
@@ -185,20 +188,22 @@
        String hexStr = CommandBuild.getMsgGrainReply(message.getAddr());
        log.info("平台--------->>>主机,返回粮情报文收到信息,报文={}", hexStr);
        BhznGrainV0ServerEngine.push(message.getIp(), message.getPort(), BytesUtil.hexStrToBytes(hexStr));
        BhznGrainV0ServerEngine.pushByMin(hexStr);
    }
    private void analysisGrain2(IoMessage message, BaseReqData reqData, String grainStr) {
    /**
     * 获取到所有的粮情数据,开始解析
     *
     * @param message
     * @param reqData
     * @param grainStr
     */
    private void analysisGrain2(IoMessage message, BaseReqData reqData, String grainStr, GrainCableData cableData) {
        GatewayDevice device = reqData.getDevice();
        // 获取完整的粮情包信息
        String[] attCable = device.getCableRule().split("-");
        int cableZ = Integer.valueOf(attCable[0]);
        int cableY = Integer.valueOf(attCable[1]);
        int cableX = Integer.valueOf(attCable[2]);
        // 根据层行列获取指定长度
        int start = 0;
        int len = 4 * cableZ * cableY * cableX;
        int len = 4 * cableData.getSumNum();
        log.info("分机------>>>平台:返回粮情完整信息,分机={}", device.getDeviceName());
@@ -234,25 +239,48 @@
        String key = "GRAIN_" + message.getAddr();
        contextMapGrain.put(key, null);
        // 将集合解析成坐标数据
        addPoint1(temps, reqData, thDto);
        if(DepotType.TYPE_03.getCode().equals(device.getDepotType())){
            //筒仓
            analysisAndPush3(temps, reqData, thDto, cableData);
        }else if(DepotType.TYPE_02.getCode().equals(device.getDepotType())){
            analysisAndPush2(temps, reqData, thDto, cableData);
        }else if(DepotType.TYPE_04.getCode().equals(device.getDepotType())){
            analysisAndPush2(temps, reqData, thDto, cableData);
        }else {
            //平房仓
            analysisAndPush1(temps, reqData, thDto, cableData);
        }
    }
    /**
     * 筒仓,解析第二步,解析到坐标数据
     * @param temps
     * @param reqData
     * @param thDto
     * @param cableData
     */
    private void analysisAndPush2(List<Double> temps, BaseReqData reqData, THDto thDto, GrainCableData cableData) {
        //TODO
        log.info("-------------------------暂未实现----------------");
    }
    /**
     * 平房仓,解析第二步,解析到坐标数据
     * 油罐仓,解析第二步,解析到坐标数据
     *
     * @param temps
     * @throws Exception
     */
    private void addPoint1(List<Double> temps, BaseReqData reqData, THDto thDto) {
    private void analysisAndPush3(List<Double> temps, BaseReqData reqData, THDto thDto, GrainCableData cableData) {
        GatewayDevice device = reqData.getDevice();
        String[] attCable = device.getCableRule().split("-");
        int cableZ = Integer.valueOf(attCable[0]);
        int cableY = Integer.valueOf(attCable[1]);
        int cableX = Integer.valueOf(attCable[2]);
        int cableZ = cableData.getCableZ();
        int cableY = cableData.getCableY();
        int cableX = cableData.getCableX();
        int sumNum = 0;
        //数据封装
        GrainData grain = new GrainData();
@@ -271,8 +299,7 @@
        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<>();
        //根号
@@ -283,6 +310,7 @@
        for (int i = 0; i < temps.size(); i++) {
            curTemp = temps.get(i);
            position = i;
            z = i % cableZ + 1;
            x = i / (cableZ * cableY);
            y = x * (cableZ * cableY);
@@ -293,16 +321,26 @@
            temperature.add(new GrainTemp(cableNum + "", z + "", curTemp + "", position + ""));
            //求最大最小值
            if (curTemp < -900) {
                sumNum--;
            } else {
//            if (curTemp < -900) {
//                sumNum--;
//            } else {
//                sumT += curTemp;
//                if (curTemp > max) {
//                    max = curTemp;
//                }
//                if (curTemp < min) {
//                    min = curTemp;
//                }
//            }
            if (curTemp > max  && curTemp < 40) {
                max = curTemp;
            }
            if (curTemp < min && curTemp > 3) {
                min = curTemp;
            }
            if(curTemp > 3 &&  curTemp < 40){
                sumT += curTemp;
                if (curTemp > max) {
                    max = curTemp;
                }
                if (curTemp < min) {
                    min = curTemp;
                }
                sumNum++;
            }
        }
@@ -321,7 +359,133 @@
        outPut.setTemperature(temperature);
        outPut.setAvgTemperature(NumberUtil.keepPrecision((sumT / sumNum), 1) + "");
        outPut.setMinTemperature(min + "");
        outPut.setMaxTemperature(min + "");
        outPut.setMaxTemperature(max + "");
        JSONObject properties = new JSONObject();
        properties.put("data", outPut);
        properties.put("timestamp", grain.getTimestamp());
        String height = this.getCacheHeight(device);
        if (StringUtils.isEmpty(height)) height = "0.0";
        properties.put("liquidHeight", height);
        grain.setProperties(properties);
        //封装好的数据
        log.info("---浅圆仓封装完成----开始执行推送");
        reqData.setData(JSONObject.toJSONString(grain));
        doPushGrain(reqData,grain);
    }
    /**
     * 从REDIS中获取液位高度信息
     *
     * @param device
     * @return
     */
    private String getCacheHeight(GatewayDevice device) {
        //给其他软使用
        String key = RedisConst.KEY_DEPOT_HEIGHT + ":" + configData.getCompanyId() + "_" + device.getDepotIdSys();
        return (String) redisUtil.get(key);
    }
    /**
     * 平房仓,解析第二步,解析到坐标数据
     *
     * @param temps
     * @throws Exception
     */
    private void analysisAndPush1(List<Double> temps, BaseReqData reqData, THDto thDto, GrainCableData cableData) {
        GatewayDevice device = reqData.getDevice();
        int cableZ = cableData.getCableZ();
        int cableY = cableData.getCableY();
        int cableX = cableData.getCableX();
        //数据封装
        GrainData grain = new GrainData();
        grain.setMessageId(ScConstant.getMessageId());
        grain.setDeviceId(device.getDeviceId());
        grain.setTimestamp(System.currentTimeMillis() + "");
        ClientHeaders headers = new ClientHeaders();
        headers.setDeviceName(device.getDeviceName());
        headers.setProductId(device.getProductId());
        headers.setOrgId(device.getOrgId());
        headers.setMsgId(reqData.getMessageId());
        grain.setHeaders(headers);
        GrainOutPut outPut = new GrainOutPut();
        double max = ReMessageBuilder.MAX_TEMP, min = ReMessageBuilder.MIN_TEMP, sumT = 0.0, sumNum =0;
        List<GrainTemp> temperature = new ArrayList<>();
        //根号
        int cableNum = 1, position = 0;
        double curTemp;
        int x = 0, y = 0, z = 0;
        for (int i = 0; i < temps.size(); i++) {
            curTemp = temps.get(i);
            position = i;
            z = i % cableZ + 1;
            x = i / (cableZ * cableY);
            y = x * (cableZ * cableY);
            y = (i - y) / cableZ;
            //根号
            cableNum = (i / cableZ) + 1;
            temperature.add(new GrainTemp(cableNum + "", z + "", curTemp + "", position + ""));
            //求最大最小值
//            if (curTemp < -900) {
//                sumNum--;
//            } else {
//                sumT += curTemp;
//                if (curTemp > max) {
//                    max = curTemp;
//                }
//                if (curTemp < min) {
//                    min = curTemp;
//                }
//            }
            if (curTemp > max  && curTemp < 40) {
                max = curTemp;
            }
            if (curTemp < min && curTemp > 3) {
                min = curTemp;
            }
            if(curTemp > 3 &&  curTemp < 40){
                sumT += curTemp;
                sumNum++;
            }
        }
        if (sumNum == 0) {
            sumNum = 1;
            log.warn("---当前粮情采集异常--");
        }
        //过滤比较用的最大最小值
        if (max == ReMessageBuilder.MAX_TEMP) {
            max = 0.0;
        }
        if (min == ReMessageBuilder.MIN_TEMP) {
            min = 0.0;
        }
        outPut.setTemperature(temperature);
        outPut.setAvgTemperature(NumberUtil.keepPrecision((sumT / sumNum), 1) + "");
        outPut.setMinTemperature(min + "");
        outPut.setMaxTemperature(max + "");
        List<GrainTH> ths = new ArrayList<>();
        ths.add(new GrainTH(thDto.getTempIn() != null ? thDto.getTempIn() + "" : "", thDto.getHumidityIn() != null ? thDto.getHumidityIn() + "" : "", "1"));
@@ -344,7 +508,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());
@@ -360,10 +524,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) {
@@ -371,6 +535,8 @@
            return;
        }
        reportService.reportGrainData(reqData);
        reqData.setData(reportService.grainData2GatewayApiInfoKafka(grainData,reqData.getDevice()).getData());
        reportService.reportGrainDataByKafka(reqData);
    }
@@ -418,7 +584,7 @@
        } finally {
            String hexStr = CommandBuild.getMsgTHReply(message.getAddr());
            log.info("平台--------->>>主机,返回仓温仓湿收到信息,报文={}", hexStr);
            BhznGrainV0ServerEngine.push(message.getIp(), message.getPort(), BytesUtil.hexStrToBytes(hexStr));
            BhznGrainV0ServerEngine.pushByMin(hexStr);
        }
    }