Explorar el Código

测气数据计算落库

hanqingsong hace 4 años
padre
commit
20e4ed509a

+ 10 - 2
src/main/java/com/chinaitop/depot/intelligent/common/ConstantClass.java

@@ -10,12 +10,20 @@ public class ConstantClass {
10 10
     /**
11 11
      * 用于字符串拼接类型判定
12 12
      */
13
-    public static final int TYPE_BUILDER = 1;
14
-    public static final int TYPE_BUFFER = 2;
13
+    public static final int TYPE_ZRO = 0;
14
+    public static final int TYPE_ONE = 1;
15
+    public static final int TYPE_TWO = 2;
16
+
17
+    // 公用休眠时间 1000 毫秒
18
+    public static final long TYPE_SLEEP = 0x3E8L;
19
+
15 20
     // 清除控制柜已发送指令(PLC断开操作-控制柜下-指令为3位0x)
16 21
     public static final String CLEAN_NLL_DOWN = "00 00 00";
17 22
     public static final String CLEAN_NLL_UP = "00 00 00 00 00";
23
+    // 测气指令
18 24
     public static final String TYPE_ON = "00 00 00 00 02"; // 测气必须先开启电源
25
+    public static final String TYPE_SC = "00 00 00 00 08"; // 测气指令
26
+
19 27
     // 通风口 操作状态
20 28
     public static final String TYPE_NO = "1"; // 开启
21 29
     public static final String TYPE_OFF = "0"; // 关闭

+ 2 - 2
src/main/java/com/chinaitop/depot/intelligent/factoryObject/StringJoinFactory.java

@@ -21,9 +21,9 @@ public class StringJoinFactory implements StringFactory {
21 21
     @Override
22 22
     public String joinString(int type, String... value) {
23 23
         switch (type) {
24
-            case ConstantClass.TYPE_BUILDER:
24
+            case ConstantClass.TYPE_ONE:
25 25
                 return stringBuilderJoin(value);
26
-            case ConstantClass.TYPE_BUFFER:
26
+            case ConstantClass.TYPE_TWO:
27 27
                 return stringBufferJoin(value);
28 28
             default:
29 29
                 return stringBuilderJoin(value);

+ 2 - 2
src/main/java/com/chinaitop/depot/intelligent/factoryObject/factoryInterface/StringFactory.java

@@ -9,8 +9,8 @@ public interface StringFactory {
9 9
 
10 10
     /**
11 11
      * 字符串拼接接口
12
-     * @param type 1(ConstantClass.TYPE_BUILDER): StringBuilder
13
-     *             2(ConstantClass.TYPE_BUFFER): StringBuffer
12
+     * @param type 1(ConstantClass.TYPE_ONE): StringBuilder
13
+     *             2(ConstantClass.TYPE_TWO): StringBuffer
14 14
      * @param value 传入参数需按拼接先后顺序传入 (例: a b c => abc)
15 15
      * @return string
16 16
      */

+ 1 - 1
src/main/java/com/chinaitop/depot/intelligent/grainsituation/controller/GasConfigController.java

@@ -41,7 +41,7 @@ public class GasConfigController {
41 41
             if (null != pageNum && null != pageSize) {
42 42
                 PageHelper.startPage(pageNum, pageSize);
43 43
             }
44
-            PageInfo<TGasConfig> pageInfo = gasConfigService.getList(storeCode);
44
+            PageInfo<TGasConfig> pageInfo = gasConfigService.getList(storeCode, null);
45 45
             return ResponseEntity.ok(pageInfo);
46 46
         } catch (Exception e) {
47 47
             e.printStackTrace();

+ 40 - 6
src/main/java/com/chinaitop/depot/intelligent/grainsituation/controller/GasDetectionController.java

@@ -1,8 +1,11 @@
1 1
 package com.chinaitop.depot.intelligent.grainsituation.controller;
2 2
 
3
+import com.chinaitop.depot.intelligent.common.ConstantClass;
4
+import com.chinaitop.depot.intelligent.grainsituation.model.TGasConfig;
3 5
 import com.chinaitop.depot.intelligent.grainsituation.model.TGasData;
4 6
 import com.chinaitop.depot.intelligent.grainsituation.model.TQtdevinfoValue;
5 7
 import com.chinaitop.depot.intelligent.grainsituation.model.TTestdata;
8
+import com.chinaitop.depot.intelligent.grainsituation.service.GasConfigService;
6 9
 import com.chinaitop.depot.intelligent.grainsituation.service.GasDetectionService;
7 10
 import com.chinaitop.depot.intelligent.socket.SocketClient;
8 11
 import com.chinaitop.depot.intelligent.socket.SocketServer;
@@ -151,13 +154,16 @@ public class GasDetectionController {
151 154
     private SocketServer socketServer;
152 155
     @Autowired
153 156
     private ConstUtils constUtils;
157
+    @Resource
158
+    private GasConfigService gasConfigService;
159
+
154 160
     /**
155 161
      * 单仓检测接口
156 162
      *
157 163
      * @param vCfCode
158 164
      * @return
159 165
      */
160
-    @RequestMapping(value = "/sendOnlyGasDetection", method = RequestMethod.POST)
166
+    @RequestMapping(value = "/sendOnlyGasDetection", method = RequestMethod.GET)
161 167
     @ApiOperation(value = "单仓气体实时检测", notes = "单仓气体实时检测")
162 168
     @ApiImplicitParams({
163 169
             @ApiImplicitParam(name = "storeCode", defaultValue = "001", value = "仓库编号", paramType = "query"),
@@ -165,9 +171,21 @@ public class GasDetectionController {
165 171
     })
166 172
     public ResponseEntity sendOnlyInsectPestDetection(String storeCode, String orgId) {
167 173
         String result = "";
168
-        // 1.获取仓房ip和指令
169
-//        socketServer.service()
170
-        return ResponseEntity.ok(result);
174
+        try {
175
+            // 1.获取仓房ip
176
+            String rangeIp = gasConfigService.getList(storeCode,null).getList().get(ConstantClass.TYPE_ZRO).getRangeIp();
177
+            // 2.发送测气指令指令
178
+            String service = socketServer.service(rangeIp, ConstantClass.TYPE_ON, ConstantClass.TYPE_ONE);// 先开启测气电源
179
+            if (service.equals(ConstantClass.C_SOH_z)) { // "00" 未开启远程模式
180
+                return ResponseEntity.ok(TypeEnum.S_2D_SOH.getDesc()); // 本地模式,无法远程控制
181
+            }
182
+            Thread.sleep(ConstantClass.TYPE_SLEEP);
183
+            result = socketServer.service(rangeIp, ConstantClass.TYPE_SC, ConstantClass.TYPE_ONE); // 发送测气指令
184
+        } catch (InterruptedException e) {
185
+            e.printStackTrace();
186
+            return ResponseEntity.failed(TypeEnum.S_NUL.getDesc());
187
+        }
188
+        return ResponseEntity.ok(socketServer.responseResultForPLC(result));
171 189
     }
172 190
 
173 191
     /**
@@ -181,11 +199,27 @@ public class GasDetectionController {
181 199
     public ResponseEntity sendTemperatureDetectionAll(String orgId) {
182 200
         String result = "";
183 201
         try {
202
+            // 获取所有仓房测气配置
203
+            List<TGasConfig> list = gasConfigService.getList(null, null).getList();
204
+            // 发送单仓检测
205
+            for (TGasConfig gasConfig : list) {
206
+                String rangeIp = gasConfig.getRangeIp();
207
+                // 2.发送测气指令指令
208
+                String service = socketServer.service(gasConfig.getRangeIp(), ConstantClass.TYPE_ON, ConstantClass.TYPE_ONE);// 先开启测气电源
209
+                if (service.equals(ConstantClass.C_SOH_z)) { // "00" 未开启远程模式
210
+                    return ResponseEntity.ok(TypeEnum.S_2D_SOH.getDesc()); // 本地模式,无法远程控制
211
+                }
212
+                Thread.sleep(ConstantClass.TYPE_SLEEP);
213
+                result = socketServer.service(rangeIp, ConstantClass.TYPE_SC, ConstantClass.TYPE_ONE); // 发送测气指令
214
+            }
215
+        } catch (InterruptedException e) {
216
+            e.printStackTrace();
217
+            return ResponseEntity.failed(TypeEnum.S_EOT.getDesc());
184 218
         } catch (Exception e) {
185 219
             e.printStackTrace();
186
-            return ResponseEntity.ok("设备连接异常!");
220
+            return ResponseEntity.failed(TypeEnum.S_STX.getDesc());
187 221
         }
188
-        return ResponseEntity.ok(result);
222
+        return ResponseEntity.ok(socketServer.responseResultForPLC(result));
189 223
     }
190 224
 
191 225
     /**

+ 1 - 1
src/main/java/com/chinaitop/depot/intelligent/grainsituation/service/GasConfigService.java

@@ -9,7 +9,7 @@ import com.github.pagehelper.PageInfo;
9 9
  * @create 2021-10-15 14:27
10 10
  */
11 11
 public interface GasConfigService {
12
-    PageInfo<TGasConfig> getList(String storeCode);
12
+    PageInfo<TGasConfig> getList(String storeCode, String ip);
13 13
 
14 14
     TGasConfig getById(String id);
15 15
 

+ 3 - 2
src/main/java/com/chinaitop/depot/intelligent/grainsituation/service/GasDetectionService.java

@@ -1,10 +1,8 @@
1 1
 package com.chinaitop.depot.intelligent.grainsituation.service;
2 2
 
3
-import com.chinaitop.depot.intelligent.basicdata.model.TDcsqtinfo;
4 3
 import com.chinaitop.depot.intelligent.basicdata.model.TQtdevinfo;
5 4
 import com.chinaitop.depot.intelligent.grainsituation.model.TDcsdata;
6 5
 import com.chinaitop.depot.intelligent.grainsituation.model.TGasData;
7
-import com.chinaitop.depot.intelligent.grainsituation.model.TQtdevinfoValue;
8 6
 
9 7
 import java.util.Date;
10 8
 import java.util.List;
@@ -43,4 +41,7 @@ public interface GasDetectionService {
43 41
     List<TGasData> getGasList(String storeCode, String startTime, String endTime);
44 42
 
45 43
     List<TGasData> getGasCheckGroupList(String storeCode);
44
+
45
+
46
+    void saveGasData(List<List<Float>> objects1, String ip);
46 47
 }

+ 4 - 1
src/main/java/com/chinaitop/depot/intelligent/grainsituation/service/impl/GasConfigServiceImpl.java

@@ -26,12 +26,15 @@ public class GasConfigServiceImpl implements GasConfigService {
26 26
     }
27 27
 
28 28
     @Override
29
-    public PageInfo<TGasConfig> getList(String storeCode) {
29
+    public PageInfo<TGasConfig> getList(String storeCode, String ip) {
30 30
         TGasConfigExample example = new TGasConfigExample();
31 31
         TGasConfigExample.Criteria criteria = example.createCriteria();
32 32
         if (StringUtils.isNotBlank(storeCode)) {
33 33
             criteria.andStoreCodeEqualTo(storeCode);
34 34
         }
35
+        if (StringUtils.isNotBlank(ip)) {
36
+            criteria.andRangeIpEqualTo(ip);
37
+        }
35 38
         return new PageInfo<>(gasConfigMapper.selectByExample(example));
36 39
     }
37 40
 

+ 71 - 5
src/main/java/com/chinaitop/depot/intelligent/grainsituation/service/impl/GasDetectionServiceImpl.java

@@ -6,25 +6,27 @@ import com.chinaitop.depot.intelligent.basicdata.model.TDcsqtinfo;
6 6
 import com.chinaitop.depot.intelligent.basicdata.model.TDcsqtinfoExample;
7 7
 import com.chinaitop.depot.intelligent.basicdata.model.TQtdevinfo;
8 8
 import com.chinaitop.depot.intelligent.basicdata.model.TQtdevinfoExample;
9
+import com.chinaitop.depot.intelligent.common.ConstantClass;
9 10
 import com.chinaitop.depot.intelligent.factoryObject.factoryInterface.StringFactory;
10 11
 import com.chinaitop.depot.intelligent.grainsituation.mapper.TDcsdataMapper;
11 12
 import com.chinaitop.depot.intelligent.grainsituation.mapper.TGasDataMapper;
12 13
 import com.chinaitop.depot.intelligent.grainsituation.mapper.TQtdevinfoValueMapper;
13 14
 import com.chinaitop.depot.intelligent.grainsituation.model.*;
15
+import com.chinaitop.depot.intelligent.grainsituation.service.GasConfigService;
14 16
 import com.chinaitop.depot.intelligent.grainsituation.service.GasDetectionService;
15 17
 import com.chinaitop.depot.intelligent.utils.DateUtils;
16 18
 import com.chinaitop.depot.intelligent.utils.ParameterUtil;
19
+import com.chinaitop.depot.intelligent.utils.UuidUtils;
20
+import com.google.common.collect.Lists;
17 21
 import com.google.common.collect.Maps;
18 22
 import org.apache.commons.lang3.StringUtils;
19 23
 import org.springframework.beans.factory.annotation.Autowired;
20 24
 import org.springframework.stereotype.Service;
21 25
 import org.springframework.transaction.annotation.Transactional;
26
+import reactor.util.annotation.Nullable;
22 27
 
23 28
 import javax.annotation.Resource;
24
-import java.util.Date;
25
-import java.util.HashMap;
26
-import java.util.List;
27
-import java.util.Map;
29
+import java.util.*;
28 30
 
29 31
 @Service
30 32
 public class GasDetectionServiceImpl implements GasDetectionService {
@@ -117,7 +119,7 @@ public class GasDetectionServiceImpl implements GasDetectionService {
117 119
         if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
118 120
             Date startDate = DateUtils.stringToData(stringFactory.joinString(1, startTime, DateUtils.startTime));
119 121
             Date endDate = DateUtils.stringToData(stringFactory.joinString(1, endTime, DateUtils.endTime));
120
-            criteria.andResultTimeBetween(startDate,endDate);
122
+            criteria.andResultTimeBetween(startDate, endDate);
121 123
         }
122 124
         return gasDataMapper.selectByExample(example);
123 125
     }
@@ -127,5 +129,69 @@ public class GasDetectionServiceImpl implements GasDetectionService {
127 129
         return gasDataMapper.getGasCheckGroupList(storeCode);
128 130
     }
129 131
 
132
+    @Resource
133
+    private GasConfigService gasConfigService;
134
+
135
+    @Override
136
+    public void saveGasData(List<List<Float>> objects1, String ip) {
137
+        if (objects1.size() > 0 && StringUtils.isNotBlank(ip)) {
138
+            // 根据ip获取仓房信息
139
+            TGasConfig gasConfig = gasConfigService.getList(null, ip).getList().get(ConstantClass.TYPE_ZRO);
140
+            String orgId = gasConfig.getOrgId();
141
+            String storeCode = gasConfig.getStoreCode();
142
+            // 数据list
143
+            ArrayList<TGasData> gases = Lists.newArrayList();
144
+            objects1.forEach(data -> {
145
+                if (data.size() > 12) {
146
+                    TGasData gasData = new TGasData();
147
+                    gasData.setNowO2(data.get(0)); // 氧气实时
148
+                    gasData.setNowPh3(data.get(1)); // 磷化氢实时
149
+                    gasData.setSouthO2(data.get(2)); // 南检测结果 氧气
150
+                    gasData.setSouthPh3(data.get(3)); // 南检测结果 磷化氢
151
+                    gasData.setEastO2(data.get(4)); // 东检测结果 氧气
152
+                    gasData.setEastPh3(data.get(5)); // 东检测结果 磷化氢
153
+                    gasData.setNorthO2(data.get(6)); // 北检测结果 氧气
154
+                    gasData.setNorthPh3(data.get(7)); // 北检测结果 磷化氢
155
+                    gasData.setWestO2(data.get(8)); // 西检测结果 氧气
156
+                    gasData.setWestPh3(data.get(9)); // 西检测结果 磷化氢
157
+                    gasData.setInO2(data.get(10)); // 中检测结果 氧气
158
+                    gasData.setInPh3(data.get(11)); // 中检测结果 磷化氢
159
+                    gases.add(gasData);
160
+                }
161
+            });
162
+            // 保存对象
163
+            TGasData saveOnlyGasData = new TGasData();
164
+            saveOnlyGasData.setId(UuidUtils.getCode()); // id
165
+            saveOnlyGasData.setOrgId(orgId); // 组织机构编码
166
+            saveOnlyGasData.setStoreCode(storeCode); // 仓房编码
167
+            // 计算平均值
168
+            float avgNowO2 = (float) gases.stream().mapToDouble(TGasData::getNowO2).average().getAsDouble();
169
+            float avgNowPh3 = (float) gases.stream().mapToDouble(TGasData::getNowPh3).average().getAsDouble();
170
+            float avgSouthO2 = (float) gases.stream().mapToDouble(TGasData::getSouthO2).average().getAsDouble();
171
+            float avgSouthPh3 = (float) gases.stream().mapToDouble(TGasData::getSouthPh3).average().getAsDouble();
172
+            float avgEastO2 = (float) gases.stream().mapToDouble(TGasData::getEastO2).average().getAsDouble();
173
+            float avgEastPh3 = (float) gases.stream().mapToDouble(TGasData::getEastPh3).average().getAsDouble();
174
+            float avgNorthO2 = (float) gases.stream().mapToDouble(TGasData::getNorthO2).average().getAsDouble();
175
+            float avgNorthPh3 = (float) gases.stream().mapToDouble(TGasData::getNorthPh3).average().getAsDouble();
176
+            float avgWestO2 = (float) gases.stream().mapToDouble(TGasData::getWestO2).average().getAsDouble();
177
+            float avgWestPh3 = (float) gases.stream().mapToDouble(TGasData::getWestPh3).average().getAsDouble();
178
+            float avgInO2 = (float) gases.stream().mapToDouble(TGasData::getInO2).average().getAsDouble();
179
+            float avgInPh3 = (float) gases.stream().mapToDouble(TGasData::getInPh3).average().getAsDouble();
180
+            saveOnlyGasData.setNowO2(avgNowO2);
181
+            saveOnlyGasData.setNowPh3(avgNowPh3);
182
+            saveOnlyGasData.setSouthO2(avgSouthO2);
183
+            saveOnlyGasData.setSouthPh3(avgSouthPh3);
184
+            saveOnlyGasData.setEastO2(avgEastO2);
185
+            saveOnlyGasData.setEastPh3(avgEastPh3);
186
+            saveOnlyGasData.setNorthO2(avgNorthO2);
187
+            saveOnlyGasData.setNorthPh3(avgNorthPh3);
188
+            saveOnlyGasData.setWestO2(avgWestO2);
189
+            saveOnlyGasData.setWestPh3(avgWestPh3);
190
+            saveOnlyGasData.setInO2(avgInO2);
191
+            saveOnlyGasData.setInPh3(avgInPh3);
192
+            // 保存
193
+            gasDataMapper.insert(saveOnlyGasData);
194
+        }
195
+    }
130 196
 
131 197
 }

+ 7 - 3
src/main/java/com/chinaitop/depot/intelligent/socket/ServerThread.java

@@ -1,10 +1,12 @@
1 1
 package com.chinaitop.depot.intelligent.socket;
2 2
 
3 3
 import com.alibaba.fastjson.JSON;
4
+import com.chinaitop.depot.intelligent.grainsituation.service.GasDetectionService;
4 5
 import com.google.common.collect.Lists;
5 6
 import org.slf4j.Logger;
6 7
 import org.slf4j.LoggerFactory;
7 8
 
9
+import javax.annotation.Resource;
8 10
 import java.io.*;
9 11
 import java.net.Socket;
10 12
 import java.text.NumberFormat;
@@ -19,6 +21,8 @@ public class ServerThread implements Runnable {
19 21
     private final static Logger log = LoggerFactory.getLogger(ServerThread.class);
20 22
 
21 23
     private Socket socket;
24
+    @Resource
25
+    private GasDetectionService gasDetectionService;
22 26
 
23 27
     ServerThread(Socket socket) {
24 28
         this.socket = socket;
@@ -82,7 +86,7 @@ public class ServerThread implements Runnable {
82 86
                  * 中检测结果 氧气 110-113
83 87
                  * 中检测结果 磷化氢 114-117
84 88
                  */
85
-                gasDataHandle(data); // 处理数据
89
+                gasDataHandle(data, socket.getInetAddress().getHostAddress()); // 处理数据
86 90
                 socket.shutdownInput(); // 关闭输入流
87 91
             }
88 92
         } catch (IOException e) {
@@ -102,7 +106,7 @@ public class ServerThread implements Runnable {
102 106
         }
103 107
     }
104 108
 
105
-    private void gasDataHandle(Integer[] data) {
109
+    private void gasDataHandle(Integer[] data, String ip) {
106 110
         // 1.将数据前16位排除
107 111
         Integer[] integers = Arrays.copyOfRange(data, 16, data.length);
108 112
         // 2.将数据分成51组,每组117个位
@@ -159,7 +163,7 @@ public class ServerThread implements Runnable {
159 163
         });
160 164
         log.info("处理后的PLC测气返回数据: {}", JSON.toJSON(objects1));
161 165
         // 将数据保存到数据库
162
-
166
+        gasDetectionService.saveGasData(objects1, ip);
163 167
     }
164 168
 
165 169
     /*public static void main(String[] args) {

+ 1 - 3
src/main/java/com/chinaitop/depot/intelligent/socket/SocketServer.java

@@ -35,8 +35,6 @@ public class SocketServer {
35 35
     private static final int backlog = 3;
36 36
     // 设置连接超时时间
37 37
     private static final int timeout = 0x2BF20;
38
-    // 休眠时间
39
-    private static final long sleepTime = 0x3E8L;
40 38
     // 计数防止死循环
41 39
     private int temp = 0;
42 40
     private static final int tempCount = 0x40;
@@ -83,7 +81,7 @@ public class SocketServer {
83 81
                     outByte(socket, orders); // 输出指令
84 82
                     log.info(LocalDateTime.now() + "***指令发送成功: {}", orders);
85 83
                     // 清除PLC控制柜指令,使其断开连接上一条指令(如有其它断开PLC指令使用else if 添加判断)
86
-                    Thread.sleep(sleepTime);
84
+                    Thread.sleep(ConstantClass.TYPE_SLEEP);
87 85
                     int length = orders.length();
88 86
                     if (length == ConstantClass.CLEAN_NLL_DOWN.length()) {
89 87
                         outByte(socket, ConstantClass.CLEAN_NLL_DOWN);