package com.dy.pipIrrMwTestWeb.test; import com.dy.common.mw.protocol.Command; import com.dy.common.mw.protocol.CommandType; import com.dy.common.mw.protocol.p206V1_0_0.ProtocolConstantV206V1_0_0; import com.dy.common.webUtil.BaseResponse; import org.springframework.beans.factory.annotation.Autowired; 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; import org.springframework.web.util.UriComponentsBuilder; /** * @Author: liurunyu * @Date: 2024/5/14 17:32 * @Description */ public class ComSupportP206V100 { protected static String mwUrlTest = "http://127.0.0.1:8070/rtuMw/com/test" ; protected static String mwUrlSendCom = "http://127.0.0.1:8070/rtuMw/com/send" ; protected static String rtuAddr = "620201000029" ; protected static String rtuResultSendWebUrl = "http://127.0.0.1:65535/test/comRes/receive" ; protected static String icCardNo = "37142501020500001" ;//IC卡编号(用户卡序列号) @Autowired private RestTemplate restTemplate; protected Command command(String code, Object param, String comId){ Command com = new Command() ; com.id = comId==null?Command.defaultId:(comId.trim().equals("")?Command.defaultId:comId) ;//实际应用中,替换成数据库记录id com.protocol = ProtocolConstantV206V1_0_0.protocolName ; com.code = code ; com.rtuAddr = rtuAddr ; com.type = CommandType.outerCommand ; com.rtuResultSendWebUrl = rtuResultSendWebUrl ; com.param = param ; return com ; } /** * 连接通信中间件测试 * @return */ protected BaseResponse sendTest(){ String url = UriComponentsBuilder.fromUriString(mwUrlTest) .build() .toUriString(); HttpHeaders headers = new HttpHeaders(); HttpEntity httpEntity = new HttpEntity<>(headers); ResponseEntity response = null; try { // 通过Get方式调用接口 response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, BaseResponse.class); } catch (Exception e) { e.printStackTrace(); } return response.getBody(); } /** * 发送命令 * @return */ protected BaseResponse sendCom2Mw(Command com){ String url = UriComponentsBuilder.fromUriString(mwUrlSendCom) .build() .toUriString(); HttpHeaders headers = new HttpHeaders(); HttpEntity httpEntity = new HttpEntity<>(com, headers); ResponseEntity response = null; try { // 通过Post方式调用接口 response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, BaseResponse.class); } catch (Exception e) { e.printStackTrace(); } return response.getBody(); } }