package com.dy.pipIrrMwTestWeb.rtuLog; 
 | 
  
 | 
import com.dy.common.webUtil.BaseResponse; 
 | 
import com.dy.common.webUtil.BaseResponseUtils; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.http.*; 
 | 
import org.springframework.web.client.RestTemplate; 
 | 
import org.springframework.web.util.UriComponentsBuilder; 
 | 
  
 | 
import java.util.List; 
 | 
  
 | 
/** 
 | 
 * @Author: liurunyu 
 | 
 * @Date: 2024/8/28 14:58 
 | 
 * @Description 
 | 
 */ 
 | 
public class RtuLogSupport { 
 | 
    protected static String mwUrlRtuLogFile = "http://127.0.0.1:8070/rtuMw/com/rtuLogFile" ; 
 | 
    protected static String mwUrlRtuLogText = "http://127.0.0.1:8070/rtuMw/com/rtuLogText" ; 
 | 
  
 | 
  
 | 
    @Autowired 
 | 
    private RestTemplate restTemplate; 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 发送命令 
 | 
     * @return 
 | 
     */ 
 | 
    protected byte[] requestMw4File(String rtuAddr, String mwUrl) throws Exception{ 
 | 
        String url = UriComponentsBuilder.fromUriString(mwUrl) 
 | 
                .build() 
 | 
                .toUriString(); 
 | 
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url) 
 | 
                //.queryParam("paramTest", "test") 
 | 
                .queryParam("rtuAddr", rtuAddr); 
 | 
        String fullUrl = builder.toUriString(); 
 | 
        byte[] bs = restTemplate.getForObject(fullUrl, byte[].class); 
 | 
        return bs ; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 发送命令 
 | 
     * @return 
 | 
     */ 
 | 
    protected BaseResponse<List<String>> requestMw4Text(String rtuAddr, String mwUrl){ 
 | 
        String url = UriComponentsBuilder.fromUriString(mwUrl) 
 | 
                .build() 
 | 
                .toUriString(); 
 | 
  
 | 
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url) 
 | 
                //.queryParam("paramTest", "test") 
 | 
                .queryParam("rtuAddr", rtuAddr); 
 | 
  
 | 
        String fullUrl = builder.toUriString(); 
 | 
  
 | 
        HttpHeaders headers = new HttpHeaders(); 
 | 
        HttpEntity<?> httpEntity = new HttpEntity<>(headers); 
 | 
        ResponseEntity<BaseResponse> response = null; 
 | 
        try { 
 | 
            // 通过Get方式调用接口 
 | 
            response = restTemplate.exchange(fullUrl, HttpMethod.GET, httpEntity, BaseResponse.class); 
 | 
        } catch (Exception e) { 
 | 
            e.printStackTrace(); 
 | 
            return BaseResponseUtils.buildError("后端系统出错,中间件调用异常"); 
 | 
        } 
 | 
        if(response == null){ 
 | 
            return BaseResponseUtils.buildError("后端系统出错,中间件调用异常"); 
 | 
        }else{ 
 | 
            return response.getBody(); 
 | 
        } 
 | 
    } 
 | 
} 
 |