From b1c572949997a5d82d9b609163ff280a1c49627d Mon Sep 17 00:00:00 2001
From: vince <757871790@qq.com>
Date: 星期五, 26 四月 2024 14:09:45 +0800
Subject: [PATCH] 粮情协议优化

---
 src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisGrain.java |  151 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 142 insertions(+), 9 deletions(-)

diff --git a/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisGrain.java b/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisGrain.java
index d2b07b1..0ac790d 100644
--- a/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisGrain.java
+++ b/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;
@@ -33,6 +44,10 @@
 
     @Resource
     private ZldzGatewayGrainService zldzGatewayGrainService;
+
+    @Resource
+    private GatewayRemoteManager gatewayRemoteManager;
+
 
     public static Map<String, Map<String, String>> contextMap = new HashMap<>();
 
@@ -249,8 +264,6 @@
             // 澶囩敤鍊�
             if (tempValue == ERROR_CHECK_TAG) {
                 tempValue = ProtocolUtils.ERROR_TEMP;
-
-
                 //楠岃瘉鏄笉鏄敟褰粨琛ュ伩鍊�
                 curLay = (i % layMax) + 1;
                 curRoot = (i / layMax) + 1;
@@ -298,9 +311,7 @@
      * @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++) {
@@ -343,7 +354,7 @@
 
 
         // 灏嗛泦鍚堣В鏋愭垚鍧愭爣鏁版嵁
-        addPoint1(temps, msg, device, reqData);
+        addPoint1(temps, device, reqData, cableX, cableY, cableZ);
     }
 
     private String buildCurKey(ReMessage msg, int curPacket) {
@@ -360,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);
+    }
+
+
     /**
      * 鍦嗙瓛浠撹В鏋愭楠�
      *

--
Gitblit v1.9.3