vince
2024-04-26 b1c572949997a5d82d9b609163ff280a1c49627d
src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisGrain.java
@@ -1,11 +1,22 @@
package com.fzzy.protocol.zldz.analysis;
import com.alibaba.fastjson.JSONObject;
import com.fzzy.api.data.GatewayDeviceType;
import com.fzzy.api.utils.BytesUtil;
import com.fzzy.api.utils.ContextUtil;
import com.fzzy.api.utils.NumberUtil;
import com.fzzy.gateway.GatewayUtils;
import com.fzzy.gateway.api.GatewayDeviceReportService;
import com.fzzy.gateway.api.GatewayRemoteManager;
import com.fzzy.gateway.data.BaseReqData;
import com.fzzy.gateway.data.WeatherWebDto;
import com.fzzy.gateway.entity.GatewayDevice;
import com.fzzy.gateway.hx2023.ScConstant;
import com.fzzy.gateway.hx2023.data.*;
import com.fzzy.io.request.BaseRequest;
import com.fzzy.protocol.ProtocolUtils;
import com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder;
import com.fzzy.protocol.data.THDto;
import com.fzzy.protocol.zldz.data.ReMessage;
import com.fzzy.protocol.zldz.service.ZldzGatewayGrainService;
import lombok.extern.slf4j.Slf4j;
@@ -14,6 +25,7 @@
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -32,6 +44,10 @@
    @Resource
    private ZldzGatewayGrainService zldzGatewayGrainService;
    @Resource
    private GatewayRemoteManager gatewayRemoteManager;
    public static Map<String, Map<String, String>> contextMap = new HashMap<>();
@@ -248,8 +264,6 @@
            // 备用值
            if (tempValue == ERROR_CHECK_TAG) {
                tempValue = ProtocolUtils.ERROR_TEMP;
                //验证是不是锥形仓补偿值
                curLay = (i % layMax) + 1;
                curRoot = (i / layMax) + 1;
@@ -294,16 +308,10 @@
    /**
     * 平方仓的解析,解析需要考虑当前是否启用的一分机多仓
     *
     * @param depotConf
     * @param msg
     * @param ser
     * @param exeRequest
     * @param packetMap
     * @param sumPackets
     */
    private void analysisStep1(ReMessage msg,
                               GatewayDevice device, BaseReqData reqData,
                               Map<String, String> packetMap, int sumPackets) {
    private void analysisStep1(ReMessage msg, GatewayDevice device, BaseReqData reqData, Map<String, String> packetMap, int sumPackets) {
        // 获取完整的粮情包信息
        String strPoints = "";
        for (int i = 1; i <= sumPackets; i++) {
@@ -316,7 +324,7 @@
        int cableX = Integer.valueOf(attCable[2]);
        // 根据层行列获取指定长度
        int start = 4 * (device.getCableStart() - 1) * cableZ * cableY;
        int start = 4 * (Integer.valueOf(device.getCableStart()) - 1) * cableZ * cableY;
        int len = 4 * cableZ * cableY * cableX;
        log.info("分机------>>>平台:返回粮情完整信息,分机={}", device.getDeviceName());
@@ -346,7 +354,7 @@
        // 将集合解析成坐标数据
        addPoint1(temps, msg, device, reqData);
        addPoint1(temps, device, reqData, cableX, cableY, cableZ);
    }
    private String buildCurKey(ReMessage msg, int curPacket) {
@@ -363,11 +371,133 @@
     * @param temps
     * @throws Exception
     */
    private void addPoint1(List<Double> temps, ReMessage msg,
                           GatewayDevice device, BaseReqData reqData) {
        //TODO
    private void addPoint1(List<Double> temps, GatewayDevice device, BaseReqData reqData, int cableX, int cableY, int cableZ) {
        //数据封装
        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 = cableX * cableY * cableZ;
        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 (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(min + "");
        List<GrainTH> ths = new ArrayList<>();
        THDto thDto = ProtocolUtils.getCacheTh(device.getDeviceId());
        if (null == thDto) thDto = new THDto();
        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());
        //系统气象站信息
        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("---粮情机械封装完成----开始执行推送");
        reqData.setData(JSONObject.toJSONString(grain));
        doPushGrain(reqData,grain);
    }
    private void doPushGrain(BaseReqData reqData,GrainData grainData) {
        GatewayDeviceReportService reportService = gatewayRemoteManager.getDeviceReportService(reqData.getDevice().getPushProtocol());
        if (null == reportService) {
            log.error("------------粮情推送失败,系统不存在当前协议执行类----{}", reqData.getDevice().getDeviceName());
            return;
        }
        reportService.reportGrainData(reqData);
        reqData.setData(reportService.grainData2GatewayApiInfoKafka(grainData,reqData.getDevice()).getData());
        reportService.reportGrainDataByKafka(reqData);
    }
    /**
     * 圆筒仓解析步骤
     *