vince
2024-03-01 f7178996ca77a1bccc941c0e7a73b36803e508cb
src/main/java/com/fzzy/protocol/bhzn/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;
@@ -18,10 +22,12 @@
import com.fzzy.protocol.bhzn.cmd.CommandBuild;
import com.fzzy.protocol.bhzn.cmd.ReMessageBuilder;
import com.fzzy.protocol.bhzn.data.IoMessage;
import com.fzzy.gateway.data.GrainCableData;
import com.fzzy.protocol.data.THDto;
import com.fzzy.protocol.bhzn.server.BhznGrainV2ServerEngine;
import com.fzzy.protocol.bhzn.server.BhznGrainV2ServerUtils;
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;
    /**
     * 针对分包粮情报文进行封装
@@ -98,24 +108,24 @@
    private void analysisGrain(IoMessage message) {
        try {
            //根据分机SN获取设备配置信息
            GatewayDevice gatewayDevice = GatewayUtils.getCacheByDeviceSn(message.getAddr());
            if (null == gatewayDevice) {
                replayGrain(message);
                log.error("主机-------->>平台,解析粮情失败,未获取到系统设备配置信息:" + message.getAddr());
                return;
            }
            //根据分机地址获取分机信息
            ApiCommonDevice commonDevice = Constant.getCommonDeviceCache(message.getAddr());
            ApiCommonDevice commonDevice = Constant.getCommonDeviceCache(message.getIp());
            if (commonDevice == null) {
                replayGrain(message);
                log.error("主机-------->>平台,解析粮情失败,未获取到系统粮情主机配置:" + message.getAddr());
                return;
            }
            //根据分机SN获取设备配置信息
            GatewayDevice gatewayDevice = GatewayUtils.getCacheByDeviceSn(commonDevice.getSn());
            if (null == gatewayDevice) {
                replayGrain(message);
                log.error("主机-------->>平台,解析粮情失败,未获取到系统设备配置信息:" + message.getAddr());
                return;
            }
            //获取请求信息
            BaseReqData reqData = ProtocolUtils.getSyncReq(gatewayDevice.getDepotIdSys());
            BaseReqData reqData = ProtocolUtils.getSyncReq(gatewayDevice.getDeviceSn());
            if (null == reqData) {
                replayGrain(message);
                log.error("主机-------->>平台,解析粮情失败,未获取到粮情请求信息:" + message.getAddr());
@@ -123,11 +133,8 @@
            }
            // 判断数据有没有收取完整
            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);
            int sumPoint = cableData.getSumNum();
            //获取当前粮情温度报文
            String grainHex = message.getContent().substring(16);
@@ -140,7 +147,7 @@
                log.info("分机------>>>平台:粮情数据单包=" + grainHex);
                //返回粮情接收信息
                replayGrain(message);
                analysisGrain2(message, reqData, grainHex);
                analysisGrain2(message, reqData, grainHex, cableData);
                return;
            }
@@ -161,7 +168,7 @@
                //返回粮情接收信息
                replayGrain(message);
                log.info("分机------>>>平台:粮情数据多包,完整数据=" + grainHex);
                analysisGrain2(message, reqData, grainHex);
                analysisGrain2(message, reqData, grainHex, cableData);
                return;
            } else {
@@ -188,18 +195,14 @@
        BhznGrainV2ServerEngine.push(message.getIp(), message.getPort(), BytesUtil.hexStrToBytes(hexStr));
    }
    private void analysisGrain2(IoMessage message, BaseReqData reqData, String 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 sumNum = cableData.getSumNum();
        // 根据层行列获取指定长度
        int start = 0;
        int len = 4 * cableZ * cableY * cableX;
        int len = 4 * sumNum;
        log.info("分机------>>>平台:返回粮情完整信息,分机={}", device.getDeviceName());
@@ -229,14 +232,188 @@
        THDto thDto = this.getTH(message);
        if (null != thDto) {
            log.debug("-------THDto--={}", thDto);
            log.info("-------THDto--={}", thDto);
        }
        //清空
        String key = "GRAIN_" + message.getAddr();
        contextMapGrain.put(key, null);
        // 将集合解析成坐标数据
        addPoint1(temps, reqData, thDto);
        if (DepotType.TYPE_02.getCode().equals(device.getDepotType()) || DepotType.TYPE_04.getCode().equals(device.getDepotType())) {
            //筒仓
            analysisAndPush2(temps, reqData, thDto, cableData);
        } else if (DepotType.TYPE_03.getCode().equals(device.getDepotType())) {
            //储油罐
            analysisAndPush3(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 analysisAndPush3(List<Double> temps, BaseReqData reqData, THDto thDto, GrainCableData cableData) {
        GatewayDevice device = reqData.getDevice();
        int cableZ = cableData.getCableZ();
        int cableY = cableData.getCableY();
        int sumNum = temps.size();
        //数据封装
        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 = com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder.MAX_TEMP, min = com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder.MIN_TEMP, sumT = 0.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 < sumNum; 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 (sumNum == 0) {
            sumNum = 1;
            log.warn("---当前粮情采集异常--");
        }
        //过滤比较用的最大最小值
        if (max == com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder.MAX_TEMP) {
            max = 0.0;
        }
        if (min == com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder.MIN_TEMP) {
            min = 0.0;
        }
        outPut.setTemperature(temperature);
        outPut.setAvgTemperature(NumberUtil.keepPrecision((sumT / sumNum), 1) + "");
        outPut.setMinTemperature(min + "");
        outPut.setMaxTemperature(min + "");
        JSONObject properties = new JSONObject();
        properties.put("timestamp", grain.getTimestamp());
        outPut.setDetectTime( grain.getTimestamp());
        String height = this.getCacheHeight(device);
        if (StringUtils.isEmpty(height)) height = "0.0";
        properties.put("liquidHeight", height);
        outPut.setLiquidHeight(height);
        grain.setOutput(JSONObject.toJSONString(outPut));
        properties.put("output", outPut);
        GatewayDevice gatewayDeviceWeather = GatewayUtils.getCacheByDeviceTypeOne(GatewayDeviceType.TYPE_09.getCode());
        //系统气象站信息
        WeatherWebDto weather = WeatherWebDto.contextMap.get("default");
        //气象信息
        GrainWeather weatherStation = new GrainWeather();
        weatherStation.setMessageId(ScConstant.getMessageId());
        weatherStation.setMessgeId(weatherStation.getMessageId());
        if (null != gatewayDeviceWeather) {
            weatherStation.setId(gatewayDeviceWeather.getDeviceId());
        } else {
            weatherStation.setId(device.getDeviceId());
        }
        weatherStation.setAirPressure(weather.getPressure());
        weatherStation.setHumidity(weather.getHumidity());
        weatherStation.setPm(weather.getAir_pm25());
        weatherStation.setRadiation("0");
        weatherStation.setRainfallAmount(weather.getWea());
        weatherStation.setTemperature(weather.getTem());
        weatherStation.setWindDirection(weather.getWin());
        weatherStation.setWindPower(weather.getWin_meter());
        weatherStation.setWindSpeed(weather.getWin_speed());
        grain.setWeatherStation(JSONObject.toJSONString(weatherStation));
        //封装好的数据
        log.info("---油罐仓信息封装完成----开始执行推送");
        grain.setProperties(properties);
        reqData.setData(JSONObject.toJSONString(grain));
        doPushGrain(reqData);
    }
    /**
     * 从REDIS中获取液位高度信息
     *
     * @param device
     * @return
     */
    private String getCacheHeight(GatewayDevice device) {
        //给其他软使用
        //igds:5012:DEPOT_HEIGHT:0134
        String key = "igds:"+ device.getOrgId() + ":"  + RedisConst.KEY_DEPOT_HEIGHT+ ":" + device.getDepotIdSys();
        com.ld.igds.grain.dto.GrainData ht = (com.ld.igds.grain.dto.GrainData) redisUtil.get(key);
        log.info("KEY="+key);
        if(ht!=null){
            log.info("VALUE="+ht.getOilHeight());
            return ht.getOilHeight() ;
        }else {
            return "0.0";
        }
    }
@@ -246,14 +423,13 @@
     * @param temps
     * @throws Exception
     */
    private void addPoint1(List<Double> temps, BaseReqData reqData, THDto thDto) {
    private void analysisAndPush1(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();
        //数据封装
        GrainData grain = new GrainData();
@@ -323,7 +499,10 @@
        outPut.setAvgTemperature(NumberUtil.keepPrecision((sumT / sumNum), 1) + "");
        outPut.setMinTemperature(min + "");
        outPut.setMaxTemperature(min + "");
        List<GrainTH> ths = new ArrayList<>();
        ths.add(new GrainTH(thDto.getTempIn() != null ? thDto.getTempIn() + "" : "", thDto.getHumidityIn() != null ? thDto.getHumidityIn() + "" : "", "1"));
        outPut.setTemperatureAndhumidity(ths);
        grain.setOutput(JSONObject.toJSONString(outPut));
        GatewayDevice gatewayDeviceWeather = GatewayUtils.getCacheByDeviceTypeOne(GatewayDeviceType.TYPE_09.getCode());
@@ -376,8 +555,8 @@
        try {
            THDto th = new THDto();
            String data = message.getContent();
            String houseNo = data.substring(0, 4);
            int depotId = BytesUtil.hexToInt(BytesUtil.tran_LH(houseNo));
            String houseNo = data.substring(0, 2);
            int depotId = BytesUtil.hexToInt(houseNo);
            String t = data.substring(4, 8);
            String h = data.substring(8, 12);
            double humy;