New file |
| | |
| | | package com.dy.pipIrrRemote.rtu; |
| | | |
| | | import com.dy.common.aop.SsoAop; |
| | | import com.dy.common.webUtil.BaseResponse; |
| | | import com.dy.common.webUtil.BaseResponseUtils; |
| | | import com.dy.pipIrrGlobal.rtuMw.Web2RtuMw; |
| | | import jakarta.servlet.ServletOutputStream; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.core.env.Environment; |
| | | import org.springframework.http.HttpEntity; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.HttpMethod; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.client.RestTemplate; |
| | | import org.springframework.web.util.UriComponentsBuilder; |
| | | |
| | | import java.net.URLEncoder; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2024/8/29 9:22 |
| | | * @Description |
| | | */ |
| | | |
| | | @Slf4j |
| | | @RestController |
| | | @RequestMapping(path="rtuLog") |
| | | public class RtuLogCtrl extends Web2RtuMw { |
| | | |
| | | private Environment env ; |
| | | private RestTemplate restTemplate; |
| | | |
| | | @Autowired |
| | | public RtuLogCtrl(Environment env, RestTemplate restTemplate) { |
| | | this.env = env; |
| | | this.restTemplate = restTemplate; |
| | | } |
| | | |
| | | /** |
| | | * 发送命令,请求下载rtu上下行数据日志文件 |
| | | * @param rtuAddr rtu地址 |
| | | * @return |
| | | */ |
| | | @GetMapping(path = "file") |
| | | @SsoAop() |
| | | public BaseResponse<List<String>> rtuLogFile(String rtuAddr, HttpServletRequest req, HttpServletResponse rep){ |
| | | String rqUrl = this.get2MwRequestUrl(this.env, ContextRtuLogFile) ; |
| | | ServletOutputStream out = null ; |
| | | try{ |
| | | byte[] bs = this.requestMw4File(rtuAddr, rqUrl) ; |
| | | if(bs != null && bs.length > 0){ |
| | | String fileReName = rtuAddr + ".log" ; |
| | | //URLEncoder.encode可以防止中文乱码 |
| | | fileReName = URLEncoder.encode(fileReName, "UTF-8").replaceAll("\\+", "%20"); |
| | | rep.addHeader("content-type", "application/octet-stream;charset=UTF-8"); |
| | | rep.addHeader("Content-Disposition", "attachment;fileName=" + fileReName); |
| | | out = rep.getOutputStream() ; |
| | | out.write(bs, 0, (bs==null?0:bs.length)); |
| | | out.flush(); |
| | | }else{ |
| | | return BaseResponseUtils.buildError("获取文件失败") ; |
| | | } |
| | | }catch (Exception e){ |
| | | }finally { |
| | | if(out != null){ |
| | | try{ |
| | | out.close(); |
| | | }catch(Exception e){ |
| | | } |
| | | } |
| | | } |
| | | return null ; |
| | | } |
| | | |
| | | /** |
| | | * 发送命令,请求rtu上下行数据日志文件内容 |
| | | * @param rtuAddr rtu地址 |
| | | * @return |
| | | */ |
| | | @GetMapping(path="text") |
| | | @SsoAop() |
| | | public BaseResponse<List<String>> rtuLogText(String rtuAddr){ |
| | | String rqUrl = this.get2MwRequestUrl(this.env, ContextRtuLogText) ; |
| | | //String mwUrlRtuLogText = env.getProperty(pro_mw + "." + DataSourceContext.get() + "." + key_mw_text); |
| | | BaseResponse<List<String>> text = this.requestMw4Text(rtuAddr, rqUrl); |
| | | if (text != null){ |
| | | if (text.getContent().get(0).contains("控制器")){ |
| | | return BaseResponseUtils.buildErrorMsg(""+text.getContent().get(0)+"") ; |
| | | } |
| | | return text ; |
| | | }else { |
| | | return BaseResponseUtils.buildErrorMsg("获取日志文件为null") ; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 发送命令,请求下载rtu上下行数据日志文件 |
| | | * @param rtuAddr rtu地址 |
| | | * @param mwUrl mw服务器地址 |
| | | * @return |
| | | */ |
| | | private byte[] requestMw4File(String rtuAddr, String mwUrl) throws Exception{ |
| | | String url = UriComponentsBuilder.fromUriString(mwUrl) |
| | | .build() |
| | | .toUriString(); |
| | | UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url) |
| | | .queryParam("rtuAddr", rtuAddr); |
| | | return restTemplate.getForObject(builder.toUriString(), byte[].class); |
| | | } |
| | | |
| | | /** |
| | | * 发送命令,请求rtu上下行数据日志文件内容 |
| | | * @param rtuAddr rtu地址 |
| | | * @param mwUrl mw服务器地址 |
| | | * @return |
| | | */ |
| | | private BaseResponse<List<String>> requestMw4Text(String rtuAddr, String mwUrl){ |
| | | String url = UriComponentsBuilder.fromUriString(mwUrl) |
| | | .build() |
| | | .toUriString(); |
| | | |
| | | UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url) |
| | | .queryParam("rtuAddr", rtuAddr); |
| | | |
| | | ResponseEntity<BaseResponse> response = restTemplate.exchange(builder.toUriString(), |
| | | HttpMethod.GET, |
| | | new HttpEntity<>(new HttpHeaders()), |
| | | BaseResponse.class); |
| | | |
| | | return (response==null?null:response.getBody()); |
| | | } |
| | | } |