From 6a9cf62f7ca4b45c48a571c63fd484c186070a0e Mon Sep 17 00:00:00 2001
From: jiazx0107@163.com <jiazx0107@163.com>
Date: 星期五, 19 一月 2024 23:53:12 +0800
Subject: [PATCH] 增加油罐仓的解析2

---
 src/main/java/com/fzzy/protocol/bhzn/analysis/AnalysisService.java |  175 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 150 insertions(+), 25 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 f44c036..423a4af 100644
--- a/src/main/java/com/fzzy/protocol/bhzn/analysis/AnalysisService.java
+++ b/src/main/java/com/fzzy/protocol/bhzn/analysis/AnalysisService.java
@@ -6,6 +6,9 @@
 import com.fzzy.api.data.GatewayDeviceType;
 import com.fzzy.api.utils.BytesUtil;
 import com.fzzy.api.utils.NumberUtil;
+import com.fzzy.api.utils.RedisConst;
+import com.fzzy.api.utils.RedisUtil;
+import com.fzzy.data.ConfigData;
 import com.fzzy.gateway.GatewayUtils;
 import com.fzzy.gateway.api.GatewayDeviceReportService;
 import com.fzzy.gateway.api.GatewayRemoteManager;
@@ -18,10 +21,12 @@
 import com.fzzy.protocol.bhzn.cmd.CommandBuild;
 import com.fzzy.protocol.bhzn.cmd.ReMessageBuilder;
 import com.fzzy.protocol.bhzn.data.IoMessage;
+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;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
@@ -41,6 +46,10 @@
 
     @Resource
     private GatewayRemoteManager gatewayRemoteManager;
+    @Resource
+    private ConfigData configData;
+    @Resource
+    private RedisUtil redisUtil;
 
     /**
      * 閽堝鍒嗗寘绮儏鎶ユ枃杩涜灏佽
@@ -114,7 +123,6 @@
             }
 
 
-
             //鑾峰彇璇锋眰淇℃伅
             BaseReqData reqData = ProtocolUtils.getSyncReq(gatewayDevice.getDepotIdSys());
             if (null == reqData) {
@@ -124,11 +132,8 @@
             }
 
             // 鍒ゆ柇鏁版嵁鏈夋病鏈夋敹鍙栧畬鏁�
-            String[] attCable = gatewayDevice.getCableRule().split("-");
-            int cableZ = Integer.valueOf(attCable[0]);
-            int cableY = Integer.valueOf(attCable[1]);
-            int cableX = Integer.valueOf(attCable[2]);
-            int sumPoint = cableZ * cableY * cableX;
+            GrainCableData cableData = GatewayUtils.getCableData(gatewayDevice);
+            int sumPoint = cableData.getSumNum();
 
             //鑾峰彇褰撳墠绮儏娓╁害鎶ユ枃
             String grainHex = message.getContent().substring(16);
@@ -141,7 +146,7 @@
                 log.info("鍒嗘満------>>>骞冲彴锛氱伯鎯呮暟鎹崟鍖�=" + grainHex);
                 //杩斿洖绮儏鎺ユ敹淇℃伅
                 replayGrain(message);
-                analysisGrain2(message, reqData, grainHex);
+                analysisGrain2(message, reqData, grainHex, cableData);
                 return;
             }
 
@@ -162,7 +167,7 @@
                 //杩斿洖绮儏鎺ユ敹淇℃伅
                 replayGrain(message);
                 log.info("鍒嗘満------>>>骞冲彴锛氱伯鎯呮暟鎹鍖咃紝瀹屾暣鏁版嵁=" + grainHex);
-                analysisGrain2(message, reqData, grainHex);
+                analysisGrain2(message, reqData, grainHex, cableData);
                 return;
 
             } else {
@@ -189,17 +194,17 @@
         BhznGrainV2ServerEngine.push(message.getIp(), message.getPort(), BytesUtil.hexStrToBytes(hexStr));
     }
 
-    private void analysisGrain2(IoMessage message, BaseReqData reqData, String grainStr) {
+    private void analysisGrain2(IoMessage message, BaseReqData reqData, String grainStr, GrainCableData cableData) {
         GatewayDevice device = reqData.getDevice();
         // 鑾峰彇瀹屾暣鐨勭伯鎯呭寘淇℃伅
-        String[] attCable = device.getCableRule().split("-");
-        int cableZ = Integer.valueOf(attCable[0]);
-        int cableY = Integer.valueOf(attCable[1]);
-        int cableX = Integer.valueOf(attCable[2]);
+        int cableZ = cableData.getCableZ();
+        int cableY = cableData.getCableY();
+        int cableX = cableData.getCableX();
+        int sumNum = cableData.getSumNum();
 
         // 鏍规嵁灞傝鍒楄幏鍙栨寚瀹氶暱搴�
         int start = 0;
-        int len = 4 * cableZ * cableY * cableX;
+        int len = 4 * sumNum;
 
         log.info("鍒嗘満------>>>骞冲彴锛氳繑鍥炵伯鎯呭畬鏁翠俊鎭紝鍒嗘満={}", device.getDeviceName());
 
@@ -229,14 +234,135 @@
 
         THDto thDto = this.getTH(message);
         if (null != thDto) {
-            log.debug("-------THDto--={}", thDto);
+            log.info("-------THDto--={}", thDto);
         }
         //娓呯┖
         String key = "GRAIN_" + message.getAddr();
         contextMapGrain.put(key, null);
 
-        // 灏嗛泦鍚堣В鏋愭垚鍧愭爣鏁版嵁
-        addPoint1(temps, reqData, thDto);
+        if (cableData.isCir()) {
+            //绛掍粨
+            analysisAndPush2(temps, reqData, thDto, cableData);
+        } else {
+            //骞虫埧浠�
+            analysisAndPush1(temps, reqData, thDto, cableData);
+        }
+    }
+
+    /**
+     * 绛掍粨瑙f瀽锛岃В鏋愮浜屾锛岃В鏋愬埌鍧愭爣鏁版嵁
+     *
+     * @param temps
+     * @throws Exception
+     */
+    private void analysisAndPush2(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();
+
+        //鏁版嵁灏佽
+        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 = com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder.MAX_TEMP, min = com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder.MIN_TEMP, sumT = 0.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 < sumNum; 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 == com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder.MAX_TEMP) {
+            max = 0.0;
+        }
+        if (min == com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder.MIN_TEMP) {
+            min = 0.0;
+        }
+
+        outPut.setTemperature(temperature);
+        outPut.setAvgTemperature(NumberUtil.keepPrecision((sumT / sumNum), 1) + "");
+        outPut.setMinTemperature(min + "");
+        outPut.setMaxTemperature(min + "");
+
+
+        JSONObject properties = new JSONObject();
+        properties.put("data", JSONObject.toJSONString(outPut));
+        properties.put("timestamp", grain.getTimestamp());
+
+        String height = this.getCacheHeight(device);
+        if (StringUtils.isEmpty(height)) height = "0.0";
+        properties.put("liquidHeight", height);
+
+        grain.setProperties(properties.toJSONString());
+
+        //灏佽濂界殑鏁版嵁
+        log.info("---娴呭渾浠撳皝瑁呭畬鎴�----寮�濮嬫墽琛屾帹閫�");
+
+        reqData.setData(JSONObject.toJSONString(grain));
+
+        doPushGrain(reqData);
+    }
+
+    /**
+     * 浠嶳EDIS涓幏鍙栨恫浣嶉珮搴︿俊鎭�
+     *
+     * @param device
+     * @return
+     */
+    private String getCacheHeight(GatewayDevice device) {
+        //缁欏叾浠栬蒋浣跨敤
+        String key = RedisConst.KEY_DEPOT_HEIGHT + ":" + configData.getCompanyId() + "_" + device.getDepotIdSys();
+        return (String) redisUtil.get(key);
     }
 
 
@@ -246,14 +372,13 @@
      * @param temps
      * @throws Exception
      */
-    private void addPoint1(List<Double> temps, BaseReqData reqData, THDto thDto) {
+    private void analysisAndPush1(List<Double> temps, BaseReqData reqData, THDto thDto, GrainCableData cableData) {
 
         GatewayDevice device = reqData.getDevice();
 
-        String[] attCable = device.getCableRule().split("-");
-        int cableZ = Integer.valueOf(attCable[0]);
-        int cableY = Integer.valueOf(attCable[1]);
-        int cableX = Integer.valueOf(attCable[2]);
+        int cableZ = cableData.getCableZ();
+        int cableY = cableData.getCableY();
+        int cableX = cableData.getCableX();
 
         //鏁版嵁灏佽
         GrainData grain = new GrainData();
@@ -325,7 +450,7 @@
         outPut.setMaxTemperature(min + "");
         List<GrainTH> ths = new ArrayList<>();
 
-        ths.add(new GrainTH(thDto.getTempIn()!=null?thDto.getTempIn()+"":"",thDto.getHumidityIn()!=null?thDto.getHumidityIn()+"":"","1"));
+        ths.add(new GrainTH(thDto.getTempIn() != null ? thDto.getTempIn() + "" : "", thDto.getHumidityIn() != null ? thDto.getHumidityIn() + "" : "", "1"));
         outPut.setTemperatureAndhumidity(ths);
         grain.setOutput(JSONObject.toJSONString(outPut));
 
@@ -379,8 +504,8 @@
         try {
             THDto th = new THDto();
             String data = message.getContent();
-            String houseNo = data.substring(0, 4);
-            int depotId = BytesUtil.hexToInt(BytesUtil.tran_LH(houseNo));
+            String houseNo = data.substring(0, 2);
+            int depotId = BytesUtil.hexToInt(houseNo);
             String t = data.substring(4, 8);
             String h = data.substring(8, 12);
             double humy;

--
Gitblit v1.9.3