From d6f1c42e4f06494557f6253b4f946cc477145375 Mon Sep 17 00:00:00 2001
From: jiazx0107@163.com <jiazx0107@163.com>
Date: 星期一, 28 八月 2023 16:32:19 +0800
Subject: [PATCH] 更新MODBUS-TCP协议,协议解析3

---
 igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/AnalysisService.java      |   87 +++++++++++++++++++++++++++++
 igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ModbusUtil2.java                  |   30 ---------
 igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/RemoteGasServiceImpl.java |   46 +++++++++++++++
 igds-protocol-fzzy3/src/main/java/com/ld/igds/protocol/fzzy/analysis/AnalysisGas.java            |    2 
 igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ServerUtil.java                   |    4 +
 5 files changed, 137 insertions(+), 32 deletions(-)

diff --git a/igds-protocol-fzzy3/src/main/java/com/ld/igds/protocol/fzzy/analysis/AnalysisGas.java b/igds-protocol-fzzy3/src/main/java/com/ld/igds/protocol/fzzy/analysis/AnalysisGas.java
index b62e2ba..a3274b1 100644
--- a/igds-protocol-fzzy3/src/main/java/com/ld/igds/protocol/fzzy/analysis/AnalysisGas.java
+++ b/igds-protocol-fzzy3/src/main/java/com/ld/igds/protocol/fzzy/analysis/AnalysisGas.java
@@ -48,8 +48,6 @@
     private CoreGasService gasService;
     @Autowired
     private CoreCommonService commonService;
-    @Autowired
-    private ExeOrderService exeOrderService;
 
     @Autowired
     private NotifyWebInvoker notifyInvoker;
diff --git a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ModbusUtil2.java b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ModbusUtil2.java
index 56a5d85..e7d4dbe 100644
--- a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ModbusUtil2.java
+++ b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ModbusUtil2.java
@@ -56,7 +56,7 @@
             modbusMaster.init();
 
             //鍒濆鍖栦箣鍚庣瓑寰咃紝閬垮厤鍑虹幇杩炴帴鏈垱寤�
-            Thread.sleep(2000);
+            Thread.sleep(2500);
 
             masterMap.put(key, modbusMaster);
 
@@ -164,34 +164,6 @@
         WriteCoilResponse coilResponse = (WriteCoilResponse) getMaster(ip, port).send(coilRequest);
         return !coilResponse.isException();
     }
-
-
-    /**
-     * 鍐欑嚎鍦堝紑鍏崇姸鎬佹暟鎹�  0x05
-     *
-     * @param offset
-     * @param status
-     * @return
-     * @throws ModbusTransportException
-     * @throws ModbusInitException
-
-    public static Boolean writeCoilStatus(String ip, int port, int offset, boolean status) throws ModbusTransportException, ModbusInitException, InterruptedException {
-    //        boolean coilValue = status;
-    //        WriteCoilRequest coilRequest = new WriteCoilRequest(slaveId, offset, coilValue);
-    //        WriteCoilResponse coilResponse = (WriteCoilResponse) getMaster(ip, port).send(coilRequest);
-    //        return !coilResponse.isException();
-
-    WriteCoilRequest coilRequest = new WriteCoilRequest(slaveId, offset, status);
-    // Thread.sleep(3000);
-    WriteCoilResponse coilResponse = (WriteCoilResponse) getMaster(ip, port).send(coilRequest);
-
-    //鍙戦�佹竻闄ゅ懡浠�
-    WriteCoilRequest coilRequest1 = new WriteCoilRequest(slaveId, offset, !status);
-    getMaster(ip, port).send(coilRequest1);
-
-    return !coilResponse.isException();
-    }
-     */
 
 
     /**
diff --git a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ServerUtil.java b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ServerUtil.java
index 84352e8..11a7979 100644
--- a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ServerUtil.java
+++ b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ServerUtil.java
@@ -13,6 +13,10 @@
 
 
 
+    public static  final String RUN_TAG = "RUN_TAG";
+
+
+
     /**
      * 璁惧鐘舵�佽繑鍥炵殑缁撴灉 KEY= companyId + serId + deviceCode value = 缁撴灉鐘舵��
      */
diff --git a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/AnalysisService.java b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/AnalysisService.java
index e069867..0cff6e1 100644
--- a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/AnalysisService.java
+++ b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/AnalysisService.java
@@ -1,17 +1,24 @@
 package com.ld.igds.protocol.modbus.command;
 
 import com.ld.igds.common.CoreDeviceService;
+import com.ld.igds.constant.BizType;
+import com.ld.igds.gas.CoreGasService;
 import com.ld.igds.io.constant.OrderRespEnum;
 import com.ld.igds.io.notify.NotifyWebInvoker;
 import com.ld.igds.io.request.CheckGasRequest;
 import com.ld.igds.io.request.DeviceControlRequest;
+import com.ld.igds.models.Gas;
 import com.ld.igds.protocol.modbus.ServerUtil;
 import com.ld.igds.protocol.modbus.data.ModbusGasResult;
+import com.ld.igds.util.ContextUtil;
+import com.ld.igds.util.NumberUtil;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -25,6 +32,8 @@
     private CoreDeviceService coreDeviceService;
     @Resource
     private NotifyWebInvoker notifyInvoker;
+    @Autowired
+    private CoreGasService gasService;
 
 
     /**
@@ -58,5 +67,83 @@
      * @param results
      */
     public void analysisGas(CheckGasRequest request, List<ModbusGasResult> results) {
+
+        log.debug("----------寮�濮嬫墽琛屾皵浣撶粨鏋滆В鏋�----{}", request.getDepotId());
+
+        String batchId = ContextUtil.getDefaultBatchId();
+        //涓讳綋淇℃伅
+        Gas gas = new Gas(batchId, request.getCompanyId(), request.getDepotId(), new Date());
+        gas.setCheckNum(results.size());
+        gas.setGasEnd(results.size());
+        gas.setGasStart(1);
+        gas.setReceiveDate(new Date());
+
+        int sumNum = results.size();
+        double co2, o2, ph3, n2;
+        double sumO2 = 0.0, sumCo2 = 0.0, sumPh3 = 0.0, sumN2 = 0.0;
+        StringBuffer sb = new StringBuffer();
+        for (ModbusGasResult gasResult : results) {
+            co2 = gasResult.getCo2().doubleValue();
+            o2 = gasResult.getO2().doubleValue();
+            ph3 = gasResult.getPh3().doubleValue();
+            n2 = -100;
+
+            //TODO >>>>> 鏍规嵁杩斿洖鍊艰繘琛屽垽鏂拰杞崲
+
+
+            if (gas.getPerCo2Max() < co2) {
+                gas.setPerCo2Max(co2);
+            }
+            if (gas.getPerCo2Min() > co2) {
+                gas.setPerCo2Min(co2);
+            }
+            if (gas.getPerO2Max() < o2) {
+                gas.setPerO2Max(o2);
+            }
+            if (gas.getPerO2Min() > o2) {
+                gas.setPerO2Min(o2);
+            }
+            if (gas.getPerPh3Max() < ph3) {
+                gas.setPerPh3Max(ph3);
+            }
+            if (gas.getPerPh3Min() > ph3) {
+                gas.setPerPh3Min(ph3);
+            }
+            if (gas.getPerN2Max() < n2) {
+                gas.setPerN2Max(n2);
+            }
+            if (gas.getPerN2Min() > n2) {
+                gas.setPerN2Min(n2);
+            }
+
+            //鍥哄畾涓猴細passCode,co2,o2,ph3,n2;passCode,co2,o2,ph3,n2;"
+            sb.append(gasResult.getPasscode());
+            sb.append(",");
+            sb.append(co2);
+            sb.append(",");
+            sb.append(o2);
+            sb.append(",");
+            sb.append(ph3);
+            sb.append(",");
+            sb.append(n2);
+            sb.append(";");
+
+            sumCo2 += co2;
+            sumO2 += o2;
+            sumPh3 += ph3;
+            sumN2 += n2;
+
+        }
+        gas.setPoints(sb.toString());
+        gas.setPerCo2(NumberUtil.keepPrecision(sumCo2 / sumNum, 2));
+        gas.setPerO2(NumberUtil.keepPrecision(sumO2 / sumNum, 2));
+        gas.setPerN2(NumberUtil.keepPrecision(sumN2 / sumNum, 2));
+        gas.setPerPh3(NumberUtil.keepPrecision(sumPh3 / sumNum, 2));
+
+        gasService.saveOrUpdateData(gas);
+
+        // 璋冪敤閫氱煡鍓嶇
+        notifyInvoker.notifyWeb(gas.getCompanyId(), OrderRespEnum.MSG_SUCCESS, BizType.GAS, request.getDepotId() + " 姘斾綋妫�娴嬶細缁撴灉杩斿洖鎴愬姛.");
+        log.info("鎺у埗鏌�----->>>骞冲彴锛氭皵浣撹В鏋愬畬鎴�-浠撳簱={}", request.getDepotId());
     }
 }
diff --git a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/RemoteGasServiceImpl.java b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/RemoteGasServiceImpl.java
index f475b24..cece11d 100644
--- a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/RemoteGasServiceImpl.java
+++ b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/RemoteGasServiceImpl.java
@@ -1,6 +1,7 @@
 package com.ld.igds.protocol.modbus.command;
 
 import com.ld.igds.constant.BizType;
+import com.ld.igds.constant.RedisConst;
 import com.ld.igds.io.RemoteGasService;
 import com.ld.igds.io.constant.OrderRespEnum;
 import com.ld.igds.io.constant.ProtocolEnum;
@@ -14,6 +15,7 @@
 import com.ld.igds.protocol.modbus.ServerUtil;
 import com.ld.igds.protocol.modbus.data.ModbusGasResult;
 import com.ld.igds.protocol.modbus.data.ModbusTcp;
+import com.ld.igds.util.RedisUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Component;
@@ -39,6 +41,9 @@
     private HModbusService modbusService;
     @Resource
     private AnalysisService analysisService;
+    @Resource
+    private RedisUtil redisUtil;
+
 
     @Override
     public String getProtocol() {
@@ -47,6 +52,12 @@
 
     @Override
     public GasResponse checkGas(CheckGasRequest request) {
+
+        boolean isRun = isRun(request);
+        if (isRun) {
+            return new GasResponse(OrderRespEnum.ORDER_ERROR.getCode(), "鎻愰啋锛氬綋鍓嶄粨姝e湪鎵ц妫�娴�");
+        }
+
         String passCode = request.getDepotId();
 
         try {
@@ -64,6 +75,8 @@
 
             boolean exeResult = ModbusUtil2.writeCoilStatus(modbusTcp.getIp(), modbusTcp.getPort(), modbusTcp.getAddrExe(), true);
             if (exeResult) {
+
+                addRun(request);
                 //璋冪敤寮傛鑾峰彇缁撴灉
                 progressGasResult(request, modbusTcp);
                 log.debug("--------鍚姩娴嬫皵---{}-{}", modbusTcp.getSerId(), modbusTcp.getBizCode());
@@ -76,6 +89,27 @@
         return new GasResponse(OrderRespEnum.ORDER_SUCCESS);
     }
 
+    private void addRun(CheckGasRequest request) {
+        String key = RedisConst.buildKey(request.getCompanyId(), ServerUtil.RUN_TAG, request.getDepotId());
+
+        redisUtil.set(key, request, 6 * 60);
+    }
+
+    private void delRun(CheckGasRequest request) {
+        String key = RedisConst.buildKey(request.getCompanyId(), ServerUtil.RUN_TAG, request.getDepotId());
+
+        redisUtil.del(key);
+    }
+
+    private boolean isRun(CheckGasRequest request) {
+        String key = RedisConst.buildKey(request.getCompanyId(), ServerUtil.RUN_TAG, request.getDepotId());
+
+        Object obj = redisUtil.get(key);
+        if (null == obj) return false;
+
+        return true;
+    }
+
     /**
      * 寮傛鎵ц杩涘害鏌ヨ
      *
@@ -83,6 +117,7 @@
      */
     @Async
     public void progressGasResult(CheckGasRequest request, ModbusTcp modbusTcp) {
+
 
         log.debug("--------------寮傛鎵ц姘斾綋妫�娴嬭杩囪幏鍙�-------------");
 
@@ -97,6 +132,12 @@
             //鏍规嵁閫氶亾鏁伴噺绾跨▼绛夊緟锛屾瘡涓�氶亾绛夊緟50绉�
             Thread.sleep(list.size() * 50 * 1000);
             log.debug("--------------绛夊緟鏃堕棿瀹屾垚锛屽紑濮嬭幏鍙栨娴嬬粨鏋�-------------{}", modbusTcp.getBizCode());
+
+            boolean isRun = isRun(request);
+            if (!isRun) {
+                log.info("--------------褰撳墠浠撳簱妫�娴嬪凡缁忓仠姝紝鍙栨秷缁撴灉鏌ヨ-------------{}", modbusTcp.getBizCode());
+                return;
+            }
 
             ModbusTcp modbusTcpTemp = modbusTcp;
             List<ModbusGasResult> results = new ArrayList<>();
@@ -124,16 +165,18 @@
                 results.add(gasResult);
             }
 
+            //鍒犻櫎杩愯鏍囪
+            delRun(request);
 
             log.debug("----鑾峰彇鐨勬皵浣撴娴嬬粨鏋�---{}--{}", request.getDepotId(), results.toString());
             //閫氱煡瑙f瀽
             analysisService.analysisGas(request, results);
-
         } catch (Exception e) {
             log.error("------------鏇存柊姘斾綋妫�娴嬬粨鏋滆繘搴﹀け璐ワ紝鎵ц寮傚父-----{}--{}", modbusTcp.getBizCode(), e.getMessage());
         }
 
     }
+
 
     @Override
     public GasResponse checkGasByPoint(CheckGasRequest request) {
@@ -162,6 +205,7 @@
                 log.debug("--------鍏抽棴娴嬫皵---{}-{}", modbusTcp.getSerId(), modbusTcp.getBizCode());
 
                 //閫氱煡寮傛缁撴潫缁撴灉閲囬泦
+                delRun(request);
             }
 
         } catch (Exception e) {

--
Gitblit v1.9.3