Просмотр исходного кода

调用库端硬件设备代码

hanqingsong лет назад: 3
Родитель
Сommit
6e882c9295

+ 7 - 0
src/main/java/com/DepotIntelligentApplication.java

@@ -6,6 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
6 6
 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
7 7
 import org.springframework.context.annotation.Bean;
8 8
 import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
9
+import org.springframework.web.client.RestTemplate;
9 10
 
10 11
 @EnableEurekaClient
11 12
 @SpringBootApplication
@@ -20,4 +21,10 @@ public class DepotIntelligentApplication {
20 21
 	public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
21 22
 		return new ThreadPoolTaskScheduler();
22 23
 	}
24
+
25
+	@Bean
26
+	public RestTemplate restTemplate() {
27
+		return new RestTemplate();
28
+	}
29
+
23 30
 }

+ 21 - 45
src/main/java/com/chinaitop/depot/intelligent/grainsituation/controller/TemperatureRecordController.java

@@ -7,6 +7,7 @@ import com.chinaitop.depot.intelligent.grainsituation.service.TemperatureRecordS
7 7
 import com.chinaitop.depot.intelligent.socket.SocketClient;
8 8
 import com.chinaitop.depot.intelligent.utils.ConstUtils;
9 9
 import com.chinaitop.depot.intelligent.utils.HexConvertUtils;
10
+import com.chinaitop.depot.intelligent.utils.RemoteControlUtil;
10 11
 import com.github.pagehelper.PageHelper;
11 12
 import com.github.pagehelper.PageInfo;
12 13
 import com.unissoft.model.ResponseEntity;
@@ -19,12 +20,16 @@ import org.apache.commons.lang.StringUtils;
19 20
 import org.springframework.beans.factory.annotation.Autowired;
20 21
 import org.springframework.beans.factory.annotation.Value;
21 22
 import org.springframework.http.MediaType;
23
+import org.springframework.util.LinkedMultiValueMap;
24
+import org.springframework.util.MultiValueMap;
22 25
 import org.springframework.web.bind.annotation.RequestBody;
23 26
 import org.springframework.web.bind.annotation.RequestMapping;
24 27
 import org.springframework.web.bind.annotation.RequestMethod;
25 28
 import org.springframework.web.bind.annotation.RestController;
29
+import org.springframework.web.client.RestTemplate;
26 30
 
27 31
 import javax.annotation.Resource;
32
+import javax.validation.constraints.NotNull;
28 33
 import java.util.*;
29 34
 
30 35
 @RestController
@@ -50,7 +55,7 @@ public class TemperatureRecordController {
50 55
             @ApiImplicitParam(name = "vCfCode", value = "仓库名称", paramType = "query"),
51 56
             @ApiImplicitParam(name = "tType", value = "检测类型0测温,1粮油,2测水分", paramType = "query")
52 57
     })
53
-    public ResponseEntity<PageInfo<TTestdata>> getList(Integer pageNum, Integer pageSize, String vCfCode, String vDatatime, String startTime, String endTime, String tType,String orgId) {
58
+    public ResponseEntity<PageInfo<TTestdata>> getList(Integer pageNum, Integer pageSize, String vCfCode, String vDatatime, String startTime, String endTime, String tType, String orgId) {
54 59
         List<TTestdata> list = null;
55 60
         try {
56 61
             if (null != pageNum && null != pageSize) {
@@ -91,11 +96,10 @@ public class TemperatureRecordController {
91 96
     }
92 97
 
93 98
     /**
94
-     *
95 99
      * @param lqId
96 100
      * @param vCfCode
97 101
      * @param vDatatime
98
-     * @param viewFlag true: 表格数据,false: 3D三温图数据
102
+     * @param viewFlag  true: 表格数据,false: 3D三温图数据
99 103
      * @return
100 104
      */
101 105
     @RequestMapping(value = "/findByHouseAndTimes", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
@@ -232,54 +236,26 @@ public class TemperatureRecordController {
232 236
     private SocketClient socketClient;
233 237
     @Autowired
234 238
     private ConstUtils constUtils;
239
+    @Resource
240
+    private RemoteControlUtil remoteControlUtil;
235 241
 
236 242
     @RequestMapping(value = "/sendTemperatureDetection", method = RequestMethod.POST)
237 243
     @ApiOperation(value = "粮情实时检测", notes = "粮情实时检测")
238 244
     @ApiImplicitParams({
239 245
             @ApiImplicitParam(name = "storeCode", defaultValue = "001", value = "仓库编号", paramType = "query")
240 246
     })
241
-    public ResponseEntity<Map<String, String>> sendTemperatureDetection(String storeCode) {
242
-        Map<String, String> map = new LinkedHashMap<>();
243
-        if (StringUtils.isNotBlank(storeCode)) {
244
-            String sTemp = storeCode.substring(0, storeCode.length());
245
-            String[] sArray = sTemp.split(",");
246
-            for (String cfCode : sArray) {
247
-                // 0X02粮情数据采集,仓房号,设备编号若为0则控全部
248
-                String dir = constUtils.directiveJoint(constUtils.F_EXTENT, constUtils.F_SUB_P, constUtils.GRAIN_TYPE, cfCode, "");
249
-                // 16进制字符串转byte数组
250
-                byte[] bytes = HexConvertUtils.hexStringToByte(dir);
251
-                // 发送指令
252
-                String s = socketClient.startClient(bytes);
253
-                // 0:失败;1:成功;2:设备通讯异常;3:设备故障;4:未知原因; 5: 本地模式(有的设备有本地模式,即无法远程控制)
254
-                if (s.contains("7E")) {
255
-                    String substring = s.substring(s.length() - 2, s.length());
256
-                    if ("00".equals(substring))
257
-                        map.put(cfCode, TypeEnum.S_NUL.getDesc()); // 失败
258
-                    else if ("01".equals(substring))
259
-                        map.put(cfCode, TypeEnum.S_SOH.getDesc()); // 成功
260
-                    else if ("02".equals(substring))
261
-                        map.put(cfCode, TypeEnum.S_STX.getDesc()); // 设备通讯异常
262
-                    else if ("03".equals(substring))
263
-                        map.put(cfCode, TypeEnum.S_ETX.getDesc()); // 设备故障
264
-                    else if ("04".equals(substring))
265
-                        map.put(cfCode, TypeEnum.S_EOT.getDesc()); // 未知原因
266
-                    else if ("05".equals(substring))
267
-                        map.put(cfCode, TypeEnum.S_2D_SOH.getDesc()); // 本地模式,无法远程控制
268
-                    else if ("06".equals(substring))
269
-                        map.put(cfCode, TypeEnum.SM.getDesc()); // 设备正忙
270
-                } else {
271
-                    if (s.contains("323230")) { // 220-FileZilla Server 0.9.60 beta
272
-                        map.put("error", "无法连接到服务器!");
273
-                    } else {
274
-                        map.put("error", s);
275
-                    }
276
-                    return ResponseEntity.failed(s);
277
-                }
278
-            }
279
-        } else {
280
-            return ResponseEntity.failed(TypeEnum.S_EM.getDesc());  // 仓房号为空
281
-        }
282
-        return ResponseEntity.ok(map);
247
+    public ResponseEntity<Map<String, String>> sendTemperatureDetection(String storeCode, @NotNull String orgId) {
248
+        // 1.获取库端url
249
+        String url = temperatureRecordService.getRemoteUrl(orgId);
250
+        if (StringUtils.isBlank(url))
251
+            return ResponseEntity.failed("库端硬件地址未配置!");
252
+        // 2.封装请求数据
253
+        MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
254
+        requestEntity.add("storeCode", storeCode);
255
+        return remoteControlUtil.remoteScan(
256
+                String.format("%s%s", url, remoteControlUtil.chooseCFCode),
257
+                requestEntity, "POST", null
258
+        );
283 259
     }
284 260
 
285 261
     @RequestMapping(value = "/sendTemperatureDetectionAll", method = RequestMethod.GET)

+ 2 - 0
src/main/java/com/chinaitop/depot/intelligent/grainsituation/mapper/TTestdataMapper.java

@@ -49,4 +49,6 @@ public interface TTestdataMapper {
49 49
      * @return
50 50
      */
51 51
     List<Map<String,Object>> getGrainPointAll(Map<String, Object> map);
52
+
53
+    String getRemoteUrl(@Param("orgId") String orgId);
52 54
 }

+ 12 - 0
src/main/java/com/chinaitop/depot/intelligent/grainsituation/mapper/TTestdataMapper.xml

@@ -465,4 +465,16 @@
465 465
       </if>
466 466
     </where>
467 467
   </select>
468
+  <select id="getRemoteUrl" resultType="java.lang.String" parameterType="java.lang.String">
469
+    SELECT
470
+        intranet_url
471
+    FROM
472
+        t_url_config
473
+    WHERE
474
+        user_type = '1'
475
+        <if test="orgId != null">
476
+          AND org_id = #{orgId}
477
+        </if>
478
+  </select>
479
+
468 480
 </mapper>

+ 2 - 0
src/main/java/com/chinaitop/depot/intelligent/grainsituation/service/TemperatureRecordService.java

@@ -24,4 +24,6 @@ public interface TemperatureRecordService {
24 24
     String getOrgId (String cfCode);
25 25
 
26 26
     TTestdata getById(String id);
27
+
28
+    String getRemoteUrl(String orgId);
27 29
 }

+ 5 - 0
src/main/java/com/chinaitop/depot/intelligent/grainsituation/service/impl/TemperatureRecordServiceImpl.java

@@ -167,4 +167,9 @@ public class TemperatureRecordServiceImpl implements TemperatureRecordService {
167 167
         return tTestdata;
168 168
     }
169 169
 
170
+    @Override
171
+    public String getRemoteUrl(String orgId) {
172
+        return tTestdataMapper.getRemoteUrl(orgId);
173
+    }
174
+
170 175
 }

+ 113 - 0
src/main/java/com/chinaitop/depot/intelligent/utils/RemoteControlUtil.java

@@ -0,0 +1,113 @@
1
+package com.chinaitop.depot.intelligent.utils;
2
+
3
+import com.unissoft.model.ResponseEntity;
4
+import com.unissoft.model.TypeEnum;
5
+import org.springframework.stereotype.Component;
6
+import org.springframework.util.MultiValueMap;
7
+import org.springframework.util.StringUtils;
8
+import org.springframework.web.client.RestTemplate;
9
+
10
+import javax.annotation.Resource;
11
+import javax.validation.constraints.NotNull;
12
+import java.util.Iterator;
13
+import java.util.Map;
14
+
15
+/**
16
+ * @author qingsong.han
17
+ * @description: 云端远程检测控制工具类
18
+ * @create 2020-06-01 11:05
19
+ */
20
+@Component
21
+public class RemoteControlUtil {
22
+    /**
23
+     * rest接口注入
24
+     */
25
+    @Resource
26
+    private RestTemplate restTemplate;
27
+    /**
28
+     * url常量
29
+     */
30
+    // 数量检测,获取测量进度
31
+    public static final String scanProgress = "/intelligents/monitorWarning/getScanResult";
32
+    // 数量检测,获取扫描结果
33
+    public static final String measureWeight = "/intelligents/monitorWarning/getmeasureweight";
34
+    // 数量检测,监测指令接口
35
+    public static final String sendOrder = "/intelligents/monitorWarning/sendNumberMonitor";
36
+    // 粮温检测-手动选仓
37
+    public static final String chooseCFCode = "/intelligents/temperatureRecord/sendTemperatureDetection";
38
+    // 粮情全仓检测
39
+    public static final String foodAll = "/intelligents/temperatureRecord/sendTemperatureDetectionAll";
40
+    // 粮情单仓检测
41
+    public static final String foodOnly = "/intelligents/temperatureRecord/sendOnlyStoreDetection";
42
+    // 气体全仓检测
43
+    public static final String gasAll = "/intelligents/gasDetection/sendGasDetectionAll";
44
+    // 气体单仓检测
45
+    public static final String gasOnly = "/intelligents/gasDetection/sendOnlyGasDetection";
46
+    // 气象检测
47
+    public static final String weatherMonitored = "/intelligents/wertherinfo/sendNowWeatherDetection";
48
+    // 液位所有油罐检测
49
+    public static final String levelAll = "/intelligents/LevelDetection/sendLevelDetectionAll";
50
+    // 液位单油罐检测
51
+    public static final String onlyLevel = "/intelligents/LevelDetection/sendOnlyStoreDetection";
52
+    // 所有虫害检测
53
+    public static final String pestAll = "/intelligents/insectPestDetection/sendInsectPestDetectionAll";
54
+    // 单仓虫害检测
55
+    public static final String onlyPest = "/intelligents/insectPestDetection/sendOnlyInsectPestDetection";
56
+    // 能耗检测
57
+    public static final String energyOrder = "/intelligents/energyDetection/addHistoryData";
58
+
59
+    /**
60
+     * 远程库端请求方法
61
+     *
62
+     * @param url           库端地址
63
+     * @param requestEntity post请求参数 无参数传入 new LinkedMultiValueMap<>()
64
+     * @param httpType      http 类型
65
+     * @param requestMap    get请求参数 无参数传入 new HashMap<>()
66
+     * @return string
67
+     */
68
+    public ResponseEntity remoteScan(@NotNull String url, MultiValueMap requestEntity, @NotNull String httpType, Map requestMap) {
69
+        httpType = httpType.toUpperCase();
70
+        ResponseEntity responseEntity = null;
71
+        try {
72
+            if ("GET".equals(httpType)) {
73
+                responseEntity = restTemplate.getForObject(getForObject(url, requestMap), ResponseEntity.class);
74
+            } else {
75
+                responseEntity = restTemplate.postForObject(url, requestEntity, ResponseEntity.class);
76
+            }
77
+        } catch (Exception e) {
78
+            e.printStackTrace();
79
+        }
80
+        return responseEntity;
81
+    }
82
+
83
+    /**
84
+     * 封装的get请求,暂时只支持map传参,并且value只支持基本类型和String
85
+     *
86
+     * @param url    库端配置url
87
+     * @param object 接收数据(只支持map类型)
88
+     * @return string类型带参数get请求url
89
+     */
90
+    private static String getForObject(String url, Object object) {
91
+        StringBuilder stringBuilder = new StringBuilder(url);
92
+        if (object instanceof Map) {
93
+            Iterator iterator = ((Map) object).entrySet().iterator();
94
+            if (iterator.hasNext()) {
95
+                stringBuilder.append("?");
96
+                Object element;
97
+                while (iterator.hasNext()) {
98
+                    element = iterator.next();
99
+                    Map.Entry entry = (Map.Entry) element;
100
+                    //过滤value为null,value为null时进行拼接字符串会变成 "null"字符串
101
+                    if (entry.getValue() != null) {
102
+                        stringBuilder.append(element).append("&");
103
+                    }
104
+                    url = stringBuilder.substring(0, stringBuilder.length() - 1);
105
+                }
106
+            }
107
+        } else {
108
+            throw new RuntimeException("url请求:" + url + "请求参数有误不是map类型");
109
+        }
110
+        return url;
111
+    }
112
+
113
+}