From bf34444f482223d291830c13cb147392298d99ee Mon Sep 17 00:00:00 2001
From: vince <757871790@qq.com>
Date: 星期五, 15 三月 2024 09:39:25 +0800
Subject: [PATCH] 粮情协议优化

---
 src/main/java/com/fzzy/protocol/bhzn/analysis/AnalysisService.java |  124 +++++++++++++++++++++++-----------------
 1 files changed, 71 insertions(+), 53 deletions(-)

diff --git a/src/main/java/com/fzzy/protocol/bhzn/analysis/AnalysisService.java b/src/main/java/com/fzzy/protocol/bhzn/analysis/AnalysisService.java
index ffc5f7b..28668ed 100644
--- a/src/main/java/com/fzzy/protocol/bhzn/analysis/AnalysisService.java
+++ b/src/main/java/com/fzzy/protocol/bhzn/analysis/AnalysisService.java
@@ -3,6 +3,7 @@
 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;
@@ -21,7 +22,7 @@
 import com.fzzy.protocol.bhzn.cmd.CommandBuild;
 import com.fzzy.protocol.bhzn.cmd.ReMessageBuilder;
 import com.fzzy.protocol.bhzn.data.IoMessage;
-import com.fzzy.protocol.data.GrainCableData;
+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;
@@ -124,7 +125,7 @@
 
 
             //鑾峰彇璇锋眰淇℃伅
-            BaseReqData reqData = ProtocolUtils.getSyncReq(gatewayDevice.getDepotIdSys());
+            BaseReqData reqData = ProtocolUtils.getSyncReq(gatewayDevice.getDeviceSn());
             if (null == reqData) {
                 replayGrain(message);
                 log.error("涓绘満-------->>骞冲彴锛岃В鏋愮伯鎯呭け璐ワ紝鏈幏鍙栧埌绮儏璇锋眰淇℃伅锛�" + message.getAddr());
@@ -132,7 +133,7 @@
             }
 
             // 鍒ゆ柇鏁版嵁鏈夋病鏈夋敹鍙栧畬鏁�
-            GrainCableData cableData = this.getCableData(gatewayDevice);
+            GrainCableData cableData = GatewayUtils.getCableData(gatewayDevice);
             int sumPoint = cableData.getSumNum();
 
             //鑾峰彇褰撳墠绮儏娓╁害鎶ユ枃
@@ -181,39 +182,6 @@
         }
     }
 
-    private GrainCableData getCableData(GatewayDevice gatewayDevice) {
-        String cableRule = gatewayDevice.getCableRule();
-        String cableCir = gatewayDevice.getCableCir();
-
-        GrainCableData result = new GrainCableData();
-
-        int cableY, cableX;
-        String[] attCable = cableRule.split("-");
-        int cableZ = Integer.valueOf(attCable[0]);
-        if (StringUtils.isEmpty(cableCir)) {
-            cableY = Integer.valueOf(attCable[1]);
-            cableX = Integer.valueOf(attCable[2]);
-        } else {
-            String[] attCir = cableCir.split("-");
-            cableZ = Integer.valueOf(attCir[0]);
-            cableY = 1;
-            cableX = 0;
-            //閽堝澶氬湀璁$畻
-            for (int i = 0; i < cableCir.length(); i++) {
-                cableX += Integer.valueOf(attCir[i]);
-            }
-
-            result.setCir(true);
-        }
-
-        result.setCableY(cableY);
-        result.setCableZ(cableZ);
-        result.setCableX(cableX);
-        result.setSumNum(cableZ * cableY * cableX);
-
-        return result;
-    }
-
     /**
      * 杩斿洖绮儏鏀跺埌鎶ユ枃淇℃伅锛岄渶瑕佹敞鎰忥細濡傛灉瀛樺湪鍒嗗寘鎯呭喌涓嬶紝闇�瑕佺瓑鎵�鏈夊寘鏀跺埌鍚庤繑鍥�
      *
@@ -230,19 +198,16 @@
     private void analysisGrain2(IoMessage message, BaseReqData reqData, String grainStr, GrainCableData cableData) {
         GatewayDevice device = reqData.getDevice();
         // 鑾峰彇瀹屾暣鐨勭伯鎯呭寘淇℃伅
-        int cableZ = cableData.getCableZ();
-        int cableY = cableData.getCableY();
-        int cableX = cableData.getCableX();
         int sumNum = cableData.getSumNum();
 
         // 鏍规嵁灞傝鍒楄幏鍙栨寚瀹氶暱搴�
-        int start = 0;
+        int start = (Integer.valueOf(device.getCableStart()) - 1) *  cableData.getCableZ()  * 4;
         int len = 4 * sumNum;
 
         log.info("鍒嗘満------>>>骞冲彴锛氳繑鍥炵伯鎯呭畬鏁翠俊鎭紝鍒嗘満={}", device.getDeviceName());
 
         String strPoints = grainStr.substring(start, start + len);
-
+        log.info("strPoints = "+ strPoints);
         // 灏嗙伯鎯呰В鏋愭垚鏁扮粍
         List<Double> temps = new ArrayList<>();
         double tempValue;
@@ -273,9 +238,13 @@
         String key = "GRAIN_" + message.getAddr();
         contextMapGrain.put(key, null);
 
-        if (cableData.isCir()) {
+
+        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);
@@ -283,21 +252,33 @@
     }
 
     /**
-     * 绛掍粨瑙f瀽锛岃В鏋愮浜屾锛岃В鏋愬埌鍧愭爣鏁版嵁
+     * 绛掍粨鎺ㄩ��
+     *
+     * @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 analysisAndPush2(List<Double> temps, BaseReqData reqData, THDto thDto, GrainCableData cableData) {
+    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 cableX = cableData.getCableX();
 
         int sumNum = temps.size();
-
+            log.info("temps:" + temps.toString());
         //鏁版嵁灏佽
         GrainData grain = new GrainData();
         grain.setMessageId(ScConstant.getMessageId());
@@ -369,18 +350,46 @@
 
 
         JSONObject properties = new JSONObject();
-        properties.put("data", JSONObject.toJSONString(outPut));
-        properties.put("timestamp", grain.getTimestamp());
 
+        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);
 
-        grain.setProperties(properties.toJSONString());
+        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("---娴呭渾浠撳皝瑁呭畬鎴�----寮�濮嬫墽琛屾帹閫�");
-
+        log.info("---娌圭綈浠撲俊鎭皝瑁呭畬鎴�----寮�濮嬫墽琛屾帹閫�");
+        grain.setProperties(properties);
         reqData.setData(JSONObject.toJSONString(grain));
 
         doPushGrain(reqData);
@@ -394,8 +403,17 @@
      */
     private String getCacheHeight(GatewayDevice device) {
         //缁欏叾浠栬蒋浣跨敤
-        String key = RedisConst.KEY_DEPOT_HEIGHT + ":" + configData.getCompanyId() + "_" + device.getDepotIdSys();
-        return (String) redisUtil.get(key);
+        //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";
+        }
+
     }
 
 

--
Gitblit v1.9.3