package com.dy.pipIrrRemote.common; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.dy.common.mw.protocol.Command; import com.dy.common.mw.protocol.CommandType; import com.dy.common.mw.protocol.p206V202404.CodeV202404; import com.dy.common.mw.protocol.p206V202404.ProtocolConstantV206V202404; import com.dy.common.mw.protocol.p206V202404.downVos.ComCdXyVo; import com.dy.common.webUtil.BaseResponse; import com.dy.pipIrrGlobal.pojoRm.RmCommandHistory; 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; import java.util.Date; /** * @author ZhuBaoMin * @date 2024-05-21 15:30 * @LastEditTime 2024-05-21 15:30 * @Description */ //@RequiredArgsConstructor public class ComSupport { //@NotNull //private final CommandSv commandSv; 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 = "37142501020100215" ; protected static String rtuResultSendWebUrl = "http://127.0.0.1:8081/remote/comRes/receive" ; protected static String controllerType = "57" ;//控制器类型 protected static Integer projectNo = 10 ;//项目编码 protected static String icCardAddr = "04BEA5BB" ;//IC卡地址 protected static String icCardNo = "37142501020500001" ;//IC卡编号(用户卡序列号) protected String protocolName = ProtocolConstantV206V202404.protocolName; protected String commandTypeInner = CommandType.innerCommand; protected String commandTypeOuter = CommandType.outerCommand; @Autowired private RestTemplate restTemplate; protected ComCdXyVo comCdXyVo(){ ComCdXyVo comVo = new ComCdXyVo() ; comVo.controllerType = controllerType ; comVo.projectNo = projectNo ; return comVo ; } /** * 创建命令日志对象 * @param commandCode 功能码 * @param rtuAddr 阀控器地址 * @param param 参数数据 * @param operator 操作员 * @return 零零日志对象 */ protected RmCommandHistory getComHistory(String commandCode, String rtuAddr, Object param, Long operator ) { RmCommandHistory rmCommandHistory = new RmCommandHistory(); rmCommandHistory.setCommandCode(commandCode); rmCommandHistory.setCommandName(CodeV202404.getCodeName(commandCode)); rmCommandHistory.setRtuaddr(rtuAddr); rmCommandHistory.setProtocol(protocolName); rmCommandHistory.setCommandType(commandTypeOuter); rmCommandHistory.setCallback(rtuResultSendWebUrl); rmCommandHistory.setParam((JSONObject) JSON.toJSON(param)); rmCommandHistory.setSendTime(new Date()); rmCommandHistory.setOperator(operator); return rmCommandHistory; } /** * 构造命令对象 * @param comId 命令ID * @param commandCode 功能码 * @param rtuAddr RTU地址 * @param param 参数数据 * @return 构造好的命令对象 */ protected Command command(String comId, String commandCode, String rtuAddr, Object param){ Command com = new Command() ; com.id = comId==null?Command.defaultId:(comId.trim().equals("")?Command.defaultId:comId); com.code = commandCode ; com.rtuAddr = rtuAddr ; com.protocol = protocolName; com.type = commandTypeOuter; 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(); } }