pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/config/RestTemplateConfig.java
New file @@ -0,0 +1,21 @@ package com.dy.pipIrrRemote.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; /** * @author ZhuBaoMin * @date 2024-05-07 17:09 * @LastEditTime 2024-05-07 17:09 * @Description */ @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } } pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/config/WebFilterConfiguration.java
New file @@ -0,0 +1,51 @@ package com.dy.pipIrrRemote.config; import com.dy.common.webFilter.DevOfDataSourceNameSetFilter; import com.dy.common.webFilter.UserTokenFilter; import jakarta.servlet.Filter; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author ZhuBaoMin * @date 2024-05-07 14:51 * @LastEditTime 2024-05-07 14:51 * @Description */ @Configuration public class WebFilterConfiguration { @Value("${pipIrr.global.dev}") public String isDevStage ;//是否为开发阶段 @Value("${pipIrr.global.dsName}") public String dsName ;//开发阶段的数据源名称 /** * DevOfDataSourceNameSetFilter与UserTokenFilter只能一个被配置上, * 所以他们的order都是1 */ private static final int order_UserTokenFilter = 1 ;//与下面 private static final int order_DevOfDataSourceNameSetFilter = 1 ; @Bean public FilterRegistrationBean<? extends Filter> RegFilter() { FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>(); if(this.isDevStage != null && !this.isDevStage.trim().equals("") && this.isDevStage.trim().equalsIgnoreCase("true")){ filterRegistrationBean.setFilter(new DevOfDataSourceNameSetFilter()); filterRegistrationBean.addUrlPatterns("/*");//配置过滤规则 filterRegistrationBean.addInitParameter("dataSourceName",dsName);//设置init参数 filterRegistrationBean.setName("DevOfDataSourceNameSetFilter");//设置过滤器名称 filterRegistrationBean.setOrder(order_DevOfDataSourceNameSetFilter);//执行次序 }else{ filterRegistrationBean.setFilter(new UserTokenFilter()); filterRegistrationBean.addUrlPatterns("/*");//配置过滤规则 filterRegistrationBean.setName("UserTokenFilter");//设置过滤器名称 filterRegistrationBean.setOrder(order_UserTokenFilter);//执行次序 } return filterRegistrationBean; } } pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/config/WebListenerConfiguration.java
New file @@ -0,0 +1,68 @@ package com.dy.pipIrrRemote.config; import com.dy.common.webListener.GenerateIdSetSuffixListener; import jakarta.servlet.ServletContextListener; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author ZhuBaoMin * @date 2024-05-07 14:52 * @LastEditTime 2024-05-07 14:52 * @Description */ @Configuration public class WebListenerConfiguration { /** * 启动顺序 */ //private static final int order_config = 0 ; private static final int order_idSetSuffix = 1 ; //private static final int order_init = 2 ; /* * 解析各种***.config配置的ConfigListener,暂时不采用此种配置方式 * @Bean public ConfigListener getGlConfigListener(){ return new ConfigListener() ; } /** * 外部提供Listener * @param listener 外部提供Listener * @return 注册Bean @Bean public ServletListenerRegistrationBean<? extends ServletContextListener> regConfigListener(ConfigListener listener) { ServletListenerRegistrationBean<ConfigListener> listenerRegistrationBean = new ServletListenerRegistrationBean<>(); listenerRegistrationBean.setListener(listener); listenerRegistrationBean.setOrder(order_config); return listenerRegistrationBean; } */ /** * 内部提供listener,该listener在系统启动时,根据配置 设置ID产生器的后缀 * @return 注册Bean */ @Bean public ServletListenerRegistrationBean<? extends ServletContextListener> regSsoListener() { ServletListenerRegistrationBean<GenerateIdSetSuffixListener> listenerRegistrationBean = new ServletListenerRegistrationBean<>(); listenerRegistrationBean.setListener(new GenerateIdSetSuffixListener()); listenerRegistrationBean.setOrder(order_idSetSuffix); return listenerRegistrationBean; } // /** // * 内部提供listener,该listener在系统启动时,初始化数据库数据 // * @return 注册Bean // */ // @Bean // public ServletListenerRegistrationBean<? extends ServletContextListener> regInitListener() { // ServletListenerRegistrationBean<InitListener> listenerRegistrationBean = new ServletListenerRegistrationBean<>(); // listenerRegistrationBean.setListener(new InitListener()); // listenerRegistrationBean.setOrder(order_init); // return listenerRegistrationBean; // } } pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/result/RemoteResultCode.java
New file @@ -0,0 +1,24 @@ package com.dy.pipIrrRemote.result; import lombok.AllArgsConstructor; import lombok.Getter; /** * @author ZhuBaoMin * @date 2024-05-07 14:54 * @LastEditTime 2024-05-07 14:54 * @Description */ @Getter @AllArgsConstructor public enum RemoteResultCode { /** * 远程操作 */ DIVIDE_FAIL(10001, "分水房添加失败"), DELETE_DIVIDE_FAIL(10001, "分水房删除失败"), NO_DIVIDES(10001, "无符合条件的分水房记录"); private final Integer code; private final String message; } pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/utils/RestTemplateUtils.java
New file @@ -0,0 +1,97 @@ package com.dy.pipIrrRemote.utils; import com.alibaba.fastjson2.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.*; import org.springframework.stereotype.Component; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** * @author ZhuBaoMin * @date 2024-05-07 17:07 * @LastEditTime 2024-05-07 17:07 * @Description */ @Component public class RestTemplateUtils { @Autowired private RestTemplate restTemplate; public JSONObject get(String url, Map<String, Object> queryParams) throws IOException { return get(url, queryParams, new HashMap<>(1)); } public JSONObject get(String url, Map<String, Object> queryParams, Map<String, String> headerParams) throws IOException { String tempUrl = setParamsByAppendUrl(queryParams, url); HttpHeaders headers = new HttpHeaders(); headerParams.forEach(headers::add); HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(null, headers); ResponseEntity<String> response = restTemplate.exchange(tempUrl, HttpMethod.GET, httpEntity, String.class); return JSONObject.parseObject(response.getBody()); } public JSONObject get2(String url, Map<String, Object> queryParams, Map<String, String> headerParams) throws IOException { String tempUrl = setParamsByPath(queryParams, url); HttpHeaders headers = new HttpHeaders(); headerParams.forEach(headers::add); HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(null, headers); ResponseEntity<String> response = restTemplate.exchange(tempUrl, HttpMethod.GET, httpEntity, String.class, queryParams); return JSONObject.parseObject(response.getBody()); } public JSONObject post(String url, String json, Map<String, String> headerParams) { HttpHeaders headers = new HttpHeaders(); headerParams.forEach(headers::add); headers.setContentType(MediaType.APPLICATION_JSON); headers.add("Accept", MediaType.APPLICATION_JSON.toString()); HttpEntity<String> httpEntity = new HttpEntity<>(json, headers); ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class); return JSONObject.parseObject(response.getBody()); } private String setParamsByPath(Map<String, Object> queryParams, String url) { // url?id={id}&name={name} if (queryParams == null || queryParams.isEmpty()) { return url; } StringBuilder sb = new StringBuilder(); try { for (Map.Entry<String, Object> entry : queryParams.entrySet()) { sb.append("&").append(entry.getKey()).append("=").append("{").append(entry.getKey()).append("}"); } if (!url.contains("?")) { sb.deleteCharAt(0).insert(0, "?"); } } catch (Exception e) { e.printStackTrace(); } return url + sb; } private String setParamsByAppendUrl(Map<String, Object> queryParams, String url) { // url?id=1&name=zzc if (queryParams == null || queryParams.isEmpty()) { return url; } StringBuilder sb = new StringBuilder(); try { for (Map.Entry<String, Object> entry : queryParams.entrySet()) { sb.append("&").append(entry.getKey()).append("="); sb.append(entry.getValue()); } if (!url.contains("?")) { sb.deleteCharAt(0).insert(0, "?"); } } catch (Exception e) { e.printStackTrace(); } return url + sb; } } pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/valve/ValveCtrl.java
New file @@ -0,0 +1,108 @@ package com.dy.pipIrrRemote.valve; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.dy.common.aop.SsoAop; import com.dy.common.webUtil.BaseResponse; import com.dy.common.webUtil.BaseResponseUtils; import com.dy.common.webUtil.ResultCodeMsg; import com.dy.pipIrrRemote.utils.RestTemplateUtils; import com.dy.pipIrrRemote.valve.dto.DTOValve; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.MediaType; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; /** * @author ZhuBaoMin * @date 2024-05-07 14:59 * @LastEditTime 2024-05-07 14:59 * @Description */ @Slf4j @Tag(name = "分水房管理", description = "分水房操作") @RestController @RequestMapping(path="valve") @RequiredArgsConstructor public class ValveCtrl { private final RestTemplateUtils restTemplateUtils; private CompletableFuture<String> futureValue = new CompletableFuture<>(); /** * 远程开关阀 * @param po 开关阀传入对象 * @param bindingResult * @return */ @Operation(summary = "远程开关阀", description = "远程开关阀") @ApiResponses(value = { @ApiResponse( responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, description = "操作结果:true:成功,false:失败(BaseResponse.content)", content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = Boolean.class))} ) }) @PostMapping(path = "operate", consumes = MediaType.APPLICATION_JSON_VALUE) @Transactional(rollbackFor = Exception.class) @SsoAop() public BaseResponse<Boolean> open(@RequestBody @Valid DTOValve po, BindingResult bindingResult) throws ExecutionException, InterruptedException { if(bindingResult != null && bindingResult.hasErrors()){ return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); } String a = null; try { a = futureValue.get(10, TimeUnit.SECONDS); } catch (TimeoutException e) { return BaseResponseUtils.buildFail("1分钟后去查看结果"); } futureValue = new CompletableFuture<>(); Map<String, Object> param = new HashMap<>(); param.put("controllerType", "01"); param.put("projectNo", 100); param.put("rtuNewAddr", "202405061656120001"); Map<String, Object> postParams = new HashMap<>(); postParams.put("id", 2024050616450001L); postParams.put("protocol", "p1"); postParams.put("rtuAddr", "20001"); postParams.put("type", "outerCommand"); postParams.put("code", "10"); postParams.put("noRtMwDealRes", false); postParams.put("rtuResultSendWebUrl", "127.0.0.1/remote/"); postParams.put("param", param); Map<String, String> headerParams = new HashMap<>(); JSONObject job_result = restTemplateUtils.post("http://localhost:8070/accMw/com/send", JSON.toJSONString(postParams), headerParams); return BaseResponseUtils.buildSuccess(a) ; } @GetMapping("/setValue") public String setValue(String name) { futureValue.complete(name); return "Value set"; } } pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/valve/dto/DTOValve.java
New file @@ -0,0 +1,48 @@ package com.dy.pipIrrRemote.valve.dto; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.NotNull; import lombok.Data; import org.hibernate.validator.constraints.Range; /** * @author ZhuBaoMin * @date 2024-05-07 15:05 * @LastEditTime 2024-05-07 15:05 * @Description 远程开阀、远程关阀传入对象 */ @Data @Schema(name = "开关阀传入对象") public class DTOValve { public static final long serialVersionUID = 202405071506001L; /** * 取水口ID */ @Schema(description = "取水口ID", requiredMode = Schema.RequiredMode.NOT_REQUIRED) @NotNull(message = "取水口不能为空") private Long intakeId; /** * 虚拟卡ID */ @Schema(description = "虚拟卡ID", requiredMode = Schema.RequiredMode.NOT_REQUIRED) @NotNull(message = "虚拟卡不能为空") private Long vcId; /** * 操作类型,1-开阀,2-关阀 */ @Schema(description = "操作类型", requiredMode = Schema.RequiredMode.NOT_REQUIRED) @NotNull(message = "操作类型不能为空") @Range(min = 1, max = 2) private Integer operateType; /** * 操作人 */ @Schema(description = "操作人", requiredMode = Schema.RequiredMode.NOT_REQUIRED) @NotNull(message = "操作人不能为空") private Long operator; } pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/resources/application.yml
@@ -6,7 +6,7 @@ management: server: port: ${pipIrr.remote.actutorPort} #web服务端口,tomcat默认是8080 #web服务端口,tomcat默认是8081 server: port: ${pipIrr.remote.webPort} servlet: pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/PipIrrSellApplication.java
@@ -28,6 +28,4 @@ } } pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/config/RestTemplateConfig.java
@@ -42,4 +42,10 @@ return restTemplate; } //简单RestTemplate实例 @Bean public RestTemplate simpleRestTemplate() { return new RestTemplate(); } }