Administrator
2024-07-18 66160634b3f6ee11af600cc17ef477aed657d28e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.dy.rtuMw.web.comResult;
 
import com.dy.common.mw.protocol.Command;
import com.dy.common.mw.protocol.Data;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
 
import java.nio.charset.StandardCharsets;
 
/**
 * @Author liurunyu
 * @Date 2023/12/21 20:28
 * @LastEditTime 2023/12/21 20:28
 * @Description
 */
@Component()
public class CommandResultDeal {
 
    private static final Logger log = LogManager.getLogger(CommandResultDeal.class.getName());
 
    private RestTemplate restTemplate;
 
    @Autowired
    public void setRestTemplate(RestTemplate restTemplate){
        this.restTemplate = restTemplate ;
    }
 
    public void deal(Data data) {
        if (data.rtuResultSendWebUrl != null
                && !data.rtuResultSendWebUrl.trim().equals("")
                && !data.rtuResultSendWebUrl.trim().equals(Command.ignoreRtuResultSendWebUrl)) {
            String url = UriComponentsBuilder.fromUriString(data.rtuResultSendWebUrl)
                    .build()
                    .toUriString();
            restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
            HttpEntity<?> httpEntity = new HttpEntity<>(data, headers);
            ResponseEntity<WebResponseVo> response = null;
            try {
                // 通过Post方式调用接口
                response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, WebResponseVo.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //assert response != null;
        } else {
            log.error("严重错误,在com.dy.aceMw.web.comResult.CommandResultDeal里,处理的是RTU命令结果Node,但数据中rtuResultSendWebUrl为空");
        }
    }
}