From 0d9a7c8974add02c61d2471642e4e5a04013ab7b Mon Sep 17 00:00:00 2001
From: vince <757871790@qq.com>
Date: 星期三, 04 六月 2025 11:37:07 +0800
Subject: [PATCH] 优化粮温检测

---
 src/main/java/com/fzzy/protocol/fzzydatabase/FzzyDatabaseGatewayGrainService.java |  275 +++++++++++++++++++++++++++++++++++++++++++++
 src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java                        |   36 +++--
 src/main/java/com/fzzy/gateway/GatewayTimerScheduled.java                         |    4 
 src/main/java/com/fzzy/gateway/hx2023/data/KafkaGrainDataDetail1.java             |    4 
 4 files changed, 299 insertions(+), 20 deletions(-)

diff --git a/src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java b/src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java
index d9c0f79..b916f5d 100644
--- a/src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java
+++ b/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;
     }
 
diff --git a/src/main/java/com/fzzy/gateway/GatewayTimerScheduled.java b/src/main/java/com/fzzy/gateway/GatewayTimerScheduled.java
index 83b3646..78a0dad 100644
--- a/src/main/java/com/fzzy/gateway/GatewayTimerScheduled.java
+++ b/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();
 
diff --git a/src/main/java/com/fzzy/gateway/hx2023/data/KafkaGrainDataDetail1.java b/src/main/java/com/fzzy/gateway/hx2023/data/KafkaGrainDataDetail1.java
index 0fc7c81..04ad557 100644
--- a/src/main/java/com/fzzy/gateway/hx2023/data/KafkaGrainDataDetail1.java
+++ b/src/main/java/com/fzzy/gateway/hx2023/data/KafkaGrainDataDetail1.java
@@ -27,10 +27,10 @@
     private String rowy;
 
     //娴呭渾浠撱�佺瓛浠撳繀濉紝绀轰緥锛歿\"totalCircle\":3,\"smallCircle\":\"4,10,16\"}锛宼otalCircle锛氭�诲湀鏁帮紝smallCircle锛氭瘡鍦堟湁鍑犳牴缂�
-    private String totalCircle;
+    private String totalCircle = "{\"totalCircle\":1,\"smallCircle\":\"1\"}";
 
     //鍏蜂綋鍦堟暟--娴呭渾浠撱�佺瓛浠撳繀濉�
-    private String circle;
+    private String circle = "1";
 
     public KafkaGrainDataDetail1() {
     }
diff --git a/src/main/java/com/fzzy/protocol/fzzydatabase/FzzyDatabaseGatewayGrainService.java b/src/main/java/com/fzzy/protocol/fzzydatabase/FzzyDatabaseGatewayGrainService.java
new file mode 100644
index 0000000..e95d295
--- /dev/null
+++ b/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();
+    }
+}

--
Gitblit v1.9.3