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"; /** //fixme S2站点固定地址,不可修改 */ public static final String S2_SITE_NO = "192.168.2.42"; /** * 请求头 */ private static final HttpHeaders headers = new HttpHeaders(); static { headers.add("Content-Type", "application/json"); headers.add("api-version", "v2.0"); } /** * 通知输送线流动 */ 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 参数类型范化 * @return 返回结果 */ public static 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 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 + "返回结果解析异常!!"); } } } }