vince
2025-06-04 0d9a7c8974add02c61d2471642e4e5a04013ab7b
优化粮温检测
已修改3个文件
已添加1个文件
319 ■■■■■ 文件已修改
src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java 36 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/gateway/GatewayTimerScheduled.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/gateway/hx2023/data/KafkaGrainDataDetail1.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/protocol/fzzydatabase/FzzyDatabaseGatewayGrainService.java 275 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java
@@ -17,6 +17,7 @@
    GRAIN_BHZN_V0_WEB("GRAIN_BHZN_V0_WEB", "粮情-BHZN-V0无线协议"),
    GRAIN_BHZN_V01_WEB("GRAIN_BHZN_V01_WEB", "粮情-BHZN-V01无线协议带开头结尾符号"),
    GRAIN_FZZY_ZLDZ_WEB("GRAIN_FZZY_ZLDZ_WEB", "粮情-FZZY-ZLDZ网口协议"),
    GRAIN_FZZY_DATABASE("GRAIN_FZZY_DATABASE", "粮情-FZZY-数据库协议"),
    GRAIN_YOUXIAN0_2023("GRAIN_YOUXIAN0_2023", "粮情-游仙主库协议"),
    GRAIN_YOUXIAN1_2023("GRAIN_YOUXIAN1_2023", "粮情-游仙分库协议"),
    GRAIN_WUJIA_2023("GRAIN_WUJIA_2023", "粮情-吴家粮库协议"),
@@ -34,22 +35,25 @@
    public static List<ApiTrigger> listTrigger() {
        List<ApiTrigger> list = new ArrayList<>();
        list.add(new ApiTrigger(TCP_FZZY_V3.getCode(), TCP_FZZY_V3.getName()));
        list.add(new ApiTrigger(GRAIN_FZZY_IGDS_V40.getCode(), GRAIN_FZZY_IGDS_V40.getName()));
        list.add(new ApiTrigger(GRAIN_BHZN_V0_WEB.getCode(), GRAIN_BHZN_V0_WEB.getName()));
        list.add(new ApiTrigger(GRAIN_BHZN_V01_WEB.getCode(), GRAIN_BHZN_V01_WEB.getName()));
        list.add(new ApiTrigger(GRAIN_FZZY_BHZN_WEB.getCode(), GRAIN_FZZY_BHZN_WEB.getName()));
        list.add(new ApiTrigger(GRAIN_FZZY_ZLDZ_WEB.getCode(), GRAIN_FZZY_ZLDZ_WEB.getName()));
        list.add(new ApiTrigger(GRAIN_YOUXIAN0_2023.getCode(), GRAIN_YOUXIAN0_2023.getName()));
        list.add(new ApiTrigger(GRAIN_YOUXIAN1_2023.getCode(), GRAIN_YOUXIAN1_2023.getName()));
        list.add(new ApiTrigger(GRAIN_WUJIA_2023.getCode(), GRAIN_WUJIA_2023.getName()));
        list.add(new ApiTrigger(GRAIN_XSYK_2023.getCode(), GRAIN_XSYK_2023.getName()));
        list.add(new ApiTrigger(DEVICE_WEIGHT_HTTP.getCode(), DEVICE_WEIGHT_HTTP.getName()));
        list.add(new ApiTrigger(DEVICE_WEIGHT_TCP_YH.getCode(), DEVICE_WEIGHT_TCP_YH.getName()));
        list.add(new ApiTrigger(DEVICE_IDCARD_HTTP.getCode(), DEVICE_IDCARD_HTTP.getName()));
        list.add(new ApiTrigger(DEVICE_LED_HTTP.getCode(), DEVICE_LED_HTTP.getName()));
        list.add(new ApiTrigger(DEVICE_LPR_SDK_HK.getCode(), DEVICE_LPR_SDK_HK.getName()));
        list.add(new ApiTrigger(DEVICE_TEST.getCode(), DEVICE_TEST.getName()));
//        list.add(new ApiTrigger(TCP_FZZY_V3.getCode(), TCP_FZZY_V3.getName()));
//        list.add(new ApiTrigger(GRAIN_FZZY_IGDS_V40.getCode(), GRAIN_FZZY_IGDS_V40.getName()));
//        list.add(new ApiTrigger(GRAIN_BHZN_V0_WEB.getCode(), GRAIN_BHZN_V0_WEB.getName()));
//        list.add(new ApiTrigger(GRAIN_BHZN_V01_WEB.getCode(), GRAIN_BHZN_V01_WEB.getName()));
//        list.add(new ApiTrigger(GRAIN_FZZY_BHZN_WEB.getCode(), GRAIN_FZZY_BHZN_WEB.getName()));
//        list.add(new ApiTrigger(GRAIN_FZZY_ZLDZ_WEB.getCode(), GRAIN_FZZY_ZLDZ_WEB.getName()));
//        list.add(new ApiTrigger(GRAIN_YOUXIAN0_2023.getCode(), GRAIN_YOUXIAN0_2023.getName()));
//        list.add(new ApiTrigger(GRAIN_YOUXIAN1_2023.getCode(), GRAIN_YOUXIAN1_2023.getName()));
//        list.add(new ApiTrigger(GRAIN_WUJIA_2023.getCode(), GRAIN_WUJIA_2023.getName()));
//        list.add(new ApiTrigger(GRAIN_XSYK_2023.getCode(), GRAIN_XSYK_2023.getName()));
//        list.add(new ApiTrigger(DEVICE_WEIGHT_HTTP.getCode(), DEVICE_WEIGHT_HTTP.getName()));
//        list.add(new ApiTrigger(DEVICE_WEIGHT_TCP_YH.getCode(), DEVICE_WEIGHT_TCP_YH.getName()));
//        list.add(new ApiTrigger(DEVICE_IDCARD_HTTP.getCode(), DEVICE_IDCARD_HTTP.getName()));
//        list.add(new ApiTrigger(DEVICE_LED_HTTP.getCode(), DEVICE_LED_HTTP.getName()));
//        list.add(new ApiTrigger(DEVICE_LPR_SDK_HK.getCode(), DEVICE_LPR_SDK_HK.getName()));
//        list.add(new ApiTrigger(DEVICE_TEST.getCode(), DEVICE_TEST.getName()));
        for (int i = 0; i < GatewayDeviceProtocol.values().length; i++) {
            list.add(new ApiTrigger(GatewayDeviceProtocol.values()[i].getCode(),GatewayDeviceProtocol.values()[i].getName()));
        }
        return list;
    }
src/main/java/com/fzzy/gateway/GatewayTimerScheduled.java
@@ -109,9 +109,9 @@
     */
//    @Scheduled(cron = "0 30 5 ? * *")
//    @Scheduled(cron = "0 32 11 ? * 1,4")
    @Scheduled(cron = "0 32 5 ? * *")
    @Scheduled(cron = "0 32 7 ? * *")
    public void scheduledGrain2() {
        //获取设备配置,只针对粮情设备进行执行
        //获取设备配置,只针对粮情设备进行执行vie
        log.info("定时粮情采集kafka推送");
        List<GatewayDevice> list = gatewayDeviceTestPR.listAll();
src/main/java/com/fzzy/gateway/hx2023/data/KafkaGrainDataDetail1.java
@@ -27,10 +27,10 @@
    private String rowy;
    //浅圆仓、筒仓必填,示例:{\"totalCircle\":3,\"smallCircle\":\"4,10,16\"},totalCircle:总圈数,smallCircle:每圈有几根缆
    private String totalCircle;
    private String totalCircle = "{\"totalCircle\":1,\"smallCircle\":\"1\"}";
    //具体圈数--浅圆仓、筒仓必填
    private String circle;
    private String circle = "1";
    public KafkaGrainDataDetail1() {
    }
src/main/java/com/fzzy/protocol/fzzydatabase/FzzyDatabaseGatewayGrainService.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,275 @@
package com.fzzy.protocol.fzzydatabase;
import com.alibaba.fastjson.JSONObject;
import com.fzzy.api.data.GatewayDeviceProtocol;
import com.fzzy.api.data.GatewayDeviceType;
import com.fzzy.api.utils.BytesUtil;
import com.fzzy.api.utils.DateUtil;
import com.fzzy.api.utils.NumberUtil;
import com.fzzy.async.fzzy40.Fzzy40CommonService;
import com.fzzy.data.ConfigData;
import com.fzzy.gateway.GatewayUtils;
import com.fzzy.gateway.api.GatewayDeviceReportService;
import com.fzzy.gateway.api.GatewayRemoteManager;
import com.fzzy.gateway.api.GatewaySyncGranService;
import com.fzzy.gateway.data.BaseReqData;
import com.fzzy.gateway.data.BaseResp;
import com.fzzy.gateway.data.GrainCableData;
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.gateway.service.GatewayConfService;
import com.fzzy.gateway.service.GatewayDeviceTestPR;
import com.fzzy.protocol.ProtocolUtils;
import com.fzzy.protocol.bhzn.cmd.ReMessageBuilder;
import com.fzzy.protocol.data.THDto;
import com.fzzy.protocol.youxian1.client.ClientEngine;
import com.ld.io.api.InvokeResult;
import io.netty.channel.Channel;
import com.fzzy.async.fzzy40.entity.Fz40Grain;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * æ¸¸ä»™åˆ†åº“协议-粮情采集入口
 */
@Slf4j
@Component
public class FzzyDatabaseGatewayGrainService implements GatewaySyncGranService {
    @Resource
    private GatewayConfService confService;
    @Resource
    private GatewayRemoteManager gatewayRemoteManager;
    @Resource
    private ConfigData configData;
    @Resource
    private Fzzy40CommonService fzzy40CommonService;
    @Resource
    private GatewayDeviceTestPR gatewayDeviceTestPR;
    @Override
    public String getGrainProtocol() {
        return GatewayDeviceProtocol.GRAIN_FZZY_DATABASE.getCode();
    }
    /**
     * @param reqData
     * @return
     */
    @Override
    public synchronized BaseResp syncGrain(BaseReqData reqData) {
        BaseResp resp = new BaseResp();
        GatewayDevice device = reqData.getDevice();
        if (null == device) {
            resp.setCode(500);
            resp.setMsg("系统未获取到下行连接设备信息,无法执行");
            log.error("----------------系统未获取到下行连接设备信息,无法执行---------");
            return resp;
        }
        try {
         //从数据库查询最新的数据,通过mqtt推送上去
            //reqData = new BaseReqData();
            reqData.setProductId(device.getProductId());
            reqData.setDeviceName(device.getDeviceName());
            reqData.setDeviceId(device.getDeviceId());
            reqData.setDayTime(new Date());
            reqData.setDevice(device);
            Date start = DateUtil.getCurZero(reqData.getDayTime());
            Date end = DateUtil.getNextZero(reqData.getDayTime());
            //获取到数据
            String depotIdSys = device.getDepotIdSys();
            if (StringUtils.isEmpty(depotIdSys)) {
                log.error("--------设备--{}-未配置系统相关仓库编码,无法执行当前操作", device.getDeviceName());
                return new BaseResp(BaseResp.CODE_500, "未配置系统相关仓库编码,无法执行当前操作");
            }
            List<Fz40Grain> listGrain = fzzy40CommonService.listGrain(depotIdSys, start, end);
            if (null == listGrain || listGrain.isEmpty()) {
                log.error("---------设备-{}-系统仓库编码-{}-未同步到粮情信息,请确认当前条件下是否有数据", device.getDeviceName(), device.getDepotIdSys());
                return new BaseResp(BaseResp.CODE_500, "未同步到粮情信息,请确认当前条件下是否有数据");
            }
            //获取第一条数据执行推送
            Fz40Grain lastData = listGrain.get(0);
            log.info(lastData.toString());
            lastData.setReceiveDate(new Date());
            GrainData grain = fz40Grain2GrainData(reqData, lastData, device);
            //推送
            Thread.sleep(3000);
            doPushGrain(reqData,grain);
        } catch (Exception e) {
            log.error("粮情检测异常:{}", e);
            resp.setCode(500);
            resp.setMsg("平台------>>>>控制柜:发送粮情检测命令:" + e.getMessage());
            return resp;
        }
        return resp;
    }
    private GrainData fz40Grain2GrainData(BaseReqData reqData ,Fz40Grain grainData, GatewayDevice device) {
        GrainCableData cableData = GatewayUtils.getCableData(device);
        GrainData grain = new GrainData();
        //层行列
        int cableZ = cableData.getCableZ();
        int cableY = cableData.getCableY();
        int cableX = cableData.getCableX();
        //温度集合
        String[] attr = grainData.getPoints().split(",");
        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 < attr.length; i++) {
            curTemp = Double.valueOf(attr[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 > 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(grainData.getTempIn() != null ? grainData.getTempIn() + "" : "", grainData.getHumidityIn() != null ? grainData.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().replaceAll("%",""));
        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));
        return 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);
    }
    @Override
    public BaseResp syncGrainTh(BaseReqData reqData) {
        return new BaseResp();
    }
    @Override
    public BaseResp syncConf(BaseReqData reqData) {
        return new BaseResp();
    }
    @Override
    public BaseResp writeConf(BaseReqData reqData) {
        return new BaseResp();
    }
    @Override
    public BaseResp initCable(BaseReqData reqData) {
        return new BaseResp();
    }
    @Override
    public BaseResp disconnect(BaseReqData reqData) {
        return new BaseResp();
    }
    @Override
    public BaseResp transparent(BaseReqData reqData) {
        return new BaseResp();
    }
}