| New file | 
|  |  |  | 
|---|
|  |  |  | package com.dy.rtuMw.web.comResult; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.dy.common.contant.Constant; | 
|---|
|  |  |  | import com.dy.common.mw.protocol.Command; | 
|---|
|  |  |  | import com.dy.common.mw.protocol.Data; | 
|---|
|  |  |  | import com.dy.common.mw.protocol4Mqtt.MqttSubMsg; | 
|---|
|  |  |  | import com.dy.rtuMw.server.ServerProperties; | 
|---|
|  |  |  | 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 通信中间件对外进行web调用,把命令结果发送出去 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @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 ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * RTU设备数据 | 
|---|
|  |  |  | * @param data | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | 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")); | 
|---|
|  |  |  | headers.set(Constant.TokenKeyInHeader, ServerProperties.orgTag); | 
|---|
|  |  |  | HttpEntity<?> httpEntity = new HttpEntity<>(data, headers); | 
|---|
|  |  |  | ResponseEntity<WebResponseVo> response = null; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | // 通过Post方式调用接口 | 
|---|
|  |  |  | response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, WebResponseVo.class); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | log.error("命令结果回调发生异常", e); | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //assert response != null; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | log.error("严重错误,在com.dy.aceMw.web.comResult.CommandResultDeal里,处理的是RTU命令结果Node,但数据中rtuResultSendWebUrl为空"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * Mqtt消息数据 | 
|---|
|  |  |  | * @param subMsg | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void deal(MqttSubMsg subMsg) { | 
|---|
|  |  |  | if (subMsg.mqttResultSendWebUrl != null | 
|---|
|  |  |  | && !subMsg.mqttResultSendWebUrl.trim().equals("") | 
|---|
|  |  |  | && !subMsg.mqttResultSendWebUrl.trim().equals(Command.ignoreRtuResultSendWebUrl)) { | 
|---|
|  |  |  | String url = UriComponentsBuilder.fromUriString(subMsg.mqttResultSendWebUrl) | 
|---|
|  |  |  | .build() | 
|---|
|  |  |  | .toUriString(); | 
|---|
|  |  |  | restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8)); | 
|---|
|  |  |  | HttpHeaders headers = new HttpHeaders(); | 
|---|
|  |  |  | headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8")); | 
|---|
|  |  |  | headers.set(Constant.TokenKeyInHeader, ServerProperties.orgTag); | 
|---|
|  |  |  | HttpEntity<?> httpEntity = new HttpEntity<>(subMsg, headers); | 
|---|
|  |  |  | ResponseEntity<WebResponseVo> response = null; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | // 通过Post方式调用接口 | 
|---|
|  |  |  | response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, WebResponseVo.class); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | log.error("命令结果回调发生异常", e); | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //assert response != null; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | log.error("严重错误,在com.dy.aceMw.web.comResult.CommandResultDeal里,处理的是RTU命令结果Node,但数据中rtuResultSendWebUrl为空"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|