pjb
2025-03-13 19e6e3559842d23fc5bd4f28a688dd8c2c747d4b
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.zy.asrs.wms.apis.wcs.utils;
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
import com.zy.asrs.framework.common.SpringUtils;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wms.apis.wcs.entity.response.CommonReponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
 
/**
 * @author pang.jiabao
 * @description 调用ESS接口封装工具类
 * @createDate 2025/3/12 11:26
 */
@Slf4j
public class HttpEssUtils {
 
    /**
     * 基础ip+port
     */
    public static final String baseUrl = "http://192.168.2.200:9046";
 
    /**
     * 请求头
     */
    private static final HttpHeaders headers = new HttpHeaders();
 
    static {
        headers.add("Content-Type", "application/json");
    }
 
    /**
     * 通知输送线流动
     */
    public static final String CONVEYOR_START = baseUrl + "/conveyor/moveContainer";
 
    /**
     * 控制播种墙亮/灭/闪灯
     */
    public static final String PLT_SEND_COMMAND = baseUrl + "/expand/api/equipment/ptl/sendCommand";
 
    /**
     * 发送post请求调用ESS接口
     *
     * @param name  调用接口名
     * @param url   接口地址
     * @param param 参数
     * @param <T>   参数类型范化
     * @return 返回结果
     */
    public static <T> CommonReponse post(String name, String url, T param) {
        log.info("{},请求地址:{},请求参数:{}", name, url, param);
        HttpEntity httpEntity = new HttpEntity<>(param, headers);
        RestTemplate restTemplate = SpringUtils.getBean(RestTemplate.class);
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
        log.info("{},返回结果:{}", name, exchange);
        if (exchange.getBody() == null) {
            throw new CoolException(name + "返回结果异常!!");
        } else {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.coercionConfigDefaults()
                    .setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsEmpty);
            try {
                return objectMapper.readValue(exchange.getBody(), CommonReponse.class);
            } catch (JsonProcessingException e) {
                log.error("{},返回结果解析异常{}", name, e.getMessage());
                e.printStackTrace();
                throw new CoolException(name + "返回结果解析异常!!");
            }
        }
    }
}