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/youxian0/analysis/AnalysisService.java |  141 +++++++++++++++++++++++++++++++---------------
 1 files changed, 94 insertions(+), 47 deletions(-)

diff --git a/src/main/java/com/fzzy/protocol/youxian0/analysis/AnalysisService.java b/src/main/java/com/fzzy/protocol/youxian0/analysis/AnalysisService.java
index 5e1d840..4eae202 100644
--- a/src/main/java/com/fzzy/protocol/youxian0/analysis/AnalysisService.java
+++ b/src/main/java/com/fzzy/protocol/youxian0/analysis/AnalysisService.java
@@ -1,7 +1,6 @@
 package com.fzzy.protocol.youxian0.analysis;
 
 import com.alibaba.fastjson.JSONObject;
-import com.fzzy.api.Constant;
 import com.fzzy.api.data.GatewayDeviceType;
 import com.fzzy.api.utils.BytesUtil;
 import com.fzzy.api.utils.NumberUtil;
@@ -39,7 +38,6 @@
 
     @Resource
     private GatewayRemoteManager gatewayRemoteManager;
-
 
     private static Map<String, GrainRoot> contextGrainRoot = new HashMap<>();
 
@@ -80,26 +78,54 @@
 
         //绮儏杩斿洖
         if (ServiceUtils.FUNCTION_66.equalsIgnoreCase(funId)) {
-            this.analysisGrainStep1(device, msgId, strMsg);
+
+            log.info("---------寮�濮嬭В鏋愮伯鎯呬俊鎭�---------");
+            try{
+                this.analysisGrainStep1(device, msgId, strMsg);
+            }catch (Exception e){
+                log.error(e.getMessage(),e);
+            }
+
+            log.info("---------瑙f瀽绮儏淇℃伅缁撴潫---------");
         }
 
         //娓╂箍搴﹁繑鍥�
         if (ServiceUtils.FUNCTION_68.equalsIgnoreCase(funId)) {
-            this.analysisGrainTh(device, strMsg);
+            log.info("---------寮�濮嬭В鏋愪粨娓╂箍搴︿俊鎭�---------");
+            try{
+                this.analysisGrainTh(device, strMsg);
+            }catch (Exception e){
+                log.error(e.getMessage(),e);
+            }
+
         }
 
     }
 
     private void analysisGrainTh(GatewayDevice device, String strMsg) {
-        THDto th = new THDto();
+        try{
+            THDto th = new THDto();
 
-        //TODO----->>> 寰呰В鏋愯皟鏁达紝鍏堢敤澶栭儴姘旇薄淇℃伅
-        //绯荤粺姘旇薄绔欎俊鎭�
-        WeatherWebDto weather = WeatherWebDto.contextMap.get("default");
-        th.setTempIn(Double.valueOf(weather.getTem()) - 1);
-        th.setHumidityIn(Double.valueOf(weather.getHumidity()) - 1);
+            //TODO----->>> 寰呰В鏋愯皟鏁达紝鍏堢敤澶栭儴姘旇薄淇℃伅
+            //7E 00 01 01 00 06 00 00 A0 FF FF 68 1A 05 CC 16 3A 62 36 7E
+            //绯荤粺姘旇薄绔欎俊鎭�
+//        WeatherWebDto weather = WeatherWebDto.contextMap.get("default");
+//        th.setTempIn(Double.valueOf(weather.getTem()) - 2);
+//        th.setHumidityIn(Double.valueOf(weather.getHumidity()) - 10);
+            double t,h;
+            String temp = strMsg.substring(30,32);
+            t =  BytesUtil.hexToInt(temp)/2;
+            log.info("娓╁害锛歿}",t);
+            temp = strMsg.substring(32,34);
+            h = BytesUtil.hexToInt(temp);
+            log.info("婀垮害锛歿}",h);
+            th.setTempIn(t);
+            th.setHumidityIn(h);
+            this.add2ThMap(device.getDepotIdSys(), th);
+        }catch (Exception e){
+            log.error(e.getMessage(),e);
+        }
 
-        this.add2ThMap(device.getDepotIdSys(), th);
     }
 
 
@@ -113,52 +139,57 @@
      * @param msgId  鍛戒护ID
      */
     private void analysisGrainStep1(GatewayDevice device, int msgId, String strMsg) {
-
+        String[] attCable = device.getCableRule().split("-");
+        int cableZ = Integer.valueOf(attCable[0]);
+        int cableY = Integer.valueOf(attCable[1]);
+        int cableX = Integer.valueOf(attCable[2]);
+        log.info("z={},x={},y={}",cableZ,cableX,cableY);
         //鑾峰彇璇锋眰淇℃伅
         BaseReqData reqData = ProtocolUtils.getSyncReq(device.getDepotIdSys());
         if (null == reqData) {
             log.error("---------娌℃湁鑾峰彇鍒拌姹備俊鎭紝涓嶆墽琛岃В鏋�------{}", device.getDeviceName());
             return;
         }
-
         //鍙繚鐣欑伯鎯呬俊鎭�
-        int start = 22 * 2;
+        int start = 15 * 2;
         strMsg = strMsg.substring(start);
 
-
-        //瀵嗛挜鍜岀偣鏁�
-        String kyeNumHex = strMsg.substring(2, 4);
-        String kyeNumBin = BytesUtil.toBinary8String(BytesUtil.hexToInt(kyeNumHex));
-
-        String key = kyeNumBin.substring(0, 3);
-        int keyValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key));
-        key = kyeNumBin.substring(4);
-        int numValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key));
-
-        //02 A4 BB BA BA B4
-        start = 2 * 2;
-        String tempHex;
         GrainRoot grainRoot = new GrainRoot();
         grainRoot.setKey(buildGrainRootKey(device.getDeviceSn(), msgId));
         grainRoot.setNum(msgId);
-        double point = 0;
-        for (int i = 0; i < numValue; i++) {
-            start = start + i * 2;
-            tempHex = strMsg.substring(start, start + 2);
-            //瀹為檯娓╁害锛濆瘑閽�*瀵嗛挜*37(婧㈠嚭涓烘棤绗﹀彿瀛楄妭)鍐嶅紓鎴栧姞瀵嗗悗鐨勬俯搴�/2銆�
-            point = this.getGrainTemp(keyValue, tempHex);
-            log.debug("--------瑙f瀽鍚庣殑娓╁害鐐�----{}---{}", tempHex, point);
-            grainRoot.getPoints().add(point);
-        }
 
+        String tempStr = "";
+        for (int j = 0;j<cableY;j++){
+            tempStr = strMsg.substring((6+2*cableZ) * j,(6+2*cableZ)  * j + (6+2*cableZ) );
+            //瀵嗛挜鍜岀偣鏁� 02 A4 BB BA BA B4
+            String kyeNumHex = tempStr.substring(2, 4);
+            String kyeNumBin = BytesUtil.toBinary8String(BytesUtil.hexToInt(kyeNumHex));
+
+            String key = "00000" + kyeNumBin.substring(0, 3);
+            int keyValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key));
+            key = "0000" + kyeNumBin.substring(4);
+            int numValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key));
+
+            //02 A4 BB BA BA B4
+            start = 2 * 2;
+            String tempHex;
+
+            double point = 0;
+            for (int i = 0; i < numValue; i++) {
+                start = start + i * 2;
+                tempHex = tempStr.substring(start, start + 2);
+                //瀹為檯娓╁害锛濆瘑閽�*瀵嗛挜*37(婧㈠嚭涓烘棤绗﹀彿瀛楄妭)鍐嶅紓鎴栧姞瀵嗗悗鐨勬俯搴�/2銆�
+                point = this.getGrainTemp(keyValue, tempHex);
+                log.info("--------瑙f瀽鍚庣殑娓╁害鐐�----{}---{}", tempHex, point);
+                grainRoot.getPoints().add(point);
+                start = 2 * 2;
+            }
+        }
 
         this.add2GrainMap(grainRoot);
 
         //鍒ゆ柇鏄笉鏄渶鍚庝竴鍖呮暟鎹紝濡傛灉鏄渶鍚庝竴鍖呮墽琛岃В鏋�
-        String[] attCable = device.getCableRule().split("-");
-        int cableZ = Integer.valueOf(attCable[0]);
-        int cableY = Integer.valueOf(attCable[1]);
-        int cableX = Integer.valueOf(attCable[2]);
+
         if (grainRoot.getNum() == cableX) {
             analysisGrainStep2(reqData, cableZ, cableY, cableX);
         }
@@ -176,14 +207,24 @@
 
         List<Double> points = new ArrayList<>();
         GrainRoot root;
+        List<Double> t = null;
         for (int i = 1; i <= cableX; i++) {
             root = this.getGrainRoot(buildGrainRootKey(reqData.getDevice().getDeviceSn(), i));
 
             if (null == root || null == root.getPoints()) {
-                log.error("-----------瑙f瀽鑾峰彇鎵�鏈夌伯鎯呮娴嬬偣澶辫触锛屽彇娑堟墽琛�---------{}", reqData.getDevice().getDeviceName());
-                return;
+                log.error("-----------瑙f瀽鑾峰彇鎵�鏈夌伯鎯呮娴嬬偣澶辫触锛屽彇娑堟墽琛�---------{}---{}", reqData.getDevice().getDeviceName(),i);
+                ;
+                t = new ArrayList<>();
+                for (int x = 0;x<cableY*cableZ;x++
+                     ) {
+                    t.add(-100.00);
+                }
+                //return;
+                points.addAll(t);
+            }else{
+                points.addAll(root.getPoints());
             }
-            points.addAll(root.getPoints());
+
         }
 
         //鎵ц灏佽瑙f瀽
@@ -297,10 +338,10 @@
 
         reqData.setData(JSONObject.toJSONString(grain));
 
-        doPushGrain(reqData);
+        doPushGrain(reqData,grain);
     }
 
-    private void doPushGrain(BaseReqData reqData) {
+    private void doPushGrain(BaseReqData reqData,GrainData grainData) {
 
         GatewayDeviceReportService reportService = gatewayRemoteManager.getDeviceReportService(reqData.getDevice().getPushProtocol());
         if (null == reportService) {
@@ -308,9 +349,11 @@
             return;
         }
         reportService.reportGrainData(reqData);
+        reqData.setData(reportService.grainData2GatewayApiInfoKafka(grainData,reqData.getDevice()).getData());
+        reportService.reportGrainDataByKafka(reqData);
     }
 
-    private void add2GrainMap(GrainRoot grainRoot) {
+    private synchronized  void add2GrainMap(GrainRoot grainRoot) {
         contextGrainRoot.put(grainRoot.getKey(), grainRoot);
     }
 
@@ -339,8 +382,12 @@
 
         int num1 = BytesUtil.hexToInt(valueHex);
         int num2 = BytesUtil.hexToInt(tempHex);
+        if((num1 ^ num2) / 2.0 > 35){
+            return  -100.00;
+        }else {
+            return  (num1 ^ num2) / 2.0;
+        }
 
-        return (num1 ^ num2) / 2.0;
     }
 
     private void add2ThMap(String depotIdSys, THDto th) {

--
Gitblit v1.9.3