|  |  |  | 
|---|
|  |  |  | import com.dy.common.multiDataSource.DataSourceContext; | 
|---|
|  |  |  | import com.dy.common.mw.protocol.Command; | 
|---|
|  |  |  | import com.dy.common.mw.protocol.CommandType; | 
|---|
|  |  |  | import com.dy.common.softUpgrade.state.UpgradeTaskVo; | 
|---|
|  |  |  | import com.dy.common.webUtil.BaseResponse; | 
|---|
|  |  |  | import com.dy.common.webUtil.BaseResponseUtils; | 
|---|
|  |  |  | import com.dy.pipIrrGlobal.util.OrgListenerSupport; | 
|---|
|  |  |  | import org.springframework.core.env.Environment; | 
|---|
|  |  |  | import org.springframework.http.HttpEntity; | 
|---|
|  |  |  | import org.springframework.http.HttpHeaders; | 
|---|
|  |  |  | 
|---|
|  |  |  | import org.springframework.web.client.RestTemplate; | 
|---|
|  |  |  | import org.springframework.web.util.UriComponentsBuilder; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.Iterator; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Author: liurunyu | 
|---|
|  |  |  | * @Date: 2024/10/23 11:45 | 
|---|
|  |  |  | * @Description | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public abstract class Web2RtuMw { | 
|---|
|  |  |  | public abstract class Web2RtuMw extends OrgListenerSupport { | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * pro_mw:属性 | 
|---|
|  |  |  | * tag从控制器中获取 | 
|---|
|  |  |  | 
|---|
|  |  |  | private static final String pro_mw = "mw"; | 
|---|
|  |  |  | private static final String pro_url = "url"; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | protected static final String ContextComSend = "/rtuMw/com/send"; | 
|---|
|  |  |  | public static final String ContextComSend = "/rtuMw/com/send"; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | protected static final String ContextRtuLogFile = "/rtuMw/com/rtuLogFile"; | 
|---|
|  |  |  | protected static final String ContextRtuLogText = "/rtuMw/com/rtuLogText"; | 
|---|
|  |  |  | public static final String ContextRtuLogFile = "/rtuMw/com/rtuLogFile"; | 
|---|
|  |  |  | public static final String ContextRtuLogText = "/rtuMw/com/rtuLogText"; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | protected static final String ContextUgTaskSend = "/rtuMw/com/upgradeRtu"; | 
|---|
|  |  |  | protected static final String ContextUgForceOver = "/rtuMw/com/ugForceOver"; | 
|---|
|  |  |  | public static final String ContextRegisterMsReceiverWebUrl = "/rtuMw/com/registerMsReceiverWebUrl"; | 
|---|
|  |  |  | public static final String ContextUgTaskSend = "/rtuMw/com/upgradeRtu"; | 
|---|
|  |  |  | public static final String ContextUgForceOver = "/rtuMw/com/ugForceOver"; | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 得到向通信中间件发送数据的URL | 
|---|
|  |  |  | * @param env | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | protected String get2MwUrl(Environment env) { | 
|---|
|  |  |  | public String get2MwUrl(Environment env) { | 
|---|
|  |  |  | return env.getProperty(pro_mw + "." + DataSourceContext.get() + "." + pro_url); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 得到向通信中间件发送强制停止升级的命令URL | 
|---|
|  |  |  | * 得到向通信中间件发送数据的URL | 
|---|
|  |  |  | * @param env | 
|---|
|  |  |  | * @param orgTag | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | protected String get2MwRequestUrl(Environment env, String context) { | 
|---|
|  |  |  | public String get2MwUrl(Environment env, String orgTag) { | 
|---|
|  |  |  | return env.getProperty(pro_mw + "." + orgTag + "." + pro_url); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 得到向通信中间件发送数据的URL | 
|---|
|  |  |  | * @param env | 
|---|
|  |  |  | * @param context | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public String get2MwRequestUrl(Environment env, String context) { | 
|---|
|  |  |  | return get2MwUrl(env) + context; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 向通信中间件发送rtu远程升级任务 | 
|---|
|  |  |  | * @param restTemplate SpringBoot的RestTemplate | 
|---|
|  |  |  | * @param toMwUrl 到通信中间件的web请求Url | 
|---|
|  |  |  | * @param body 请求数据 | 
|---|
|  |  |  | * 得到向通信中间件发送数据的URL | 
|---|
|  |  |  | * @param env | 
|---|
|  |  |  | * @param orgTag | 
|---|
|  |  |  | * @param context | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | protected BaseResponse sendPostRequest2Mw(RestTemplate restTemplate, String toMwUrl, Object body) { | 
|---|
|  |  |  | public String get2MwRequestUrl(Environment env, String orgTag, String context) { | 
|---|
|  |  |  | return get2MwUrl(env, orgTag) + context; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 向通信中间件发送Post请求 | 
|---|
|  |  |  | * @param restTemplate SpringBoot的RestTemplate | 
|---|
|  |  |  | * @param toMwUrl 到通信中间件的web请求Url | 
|---|
|  |  |  | * @param body 数据 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public BaseResponse sendPostRequest2Mw(RestTemplate restTemplate, String toMwUrl, Object body) { | 
|---|
|  |  |  | String url = UriComponentsBuilder.fromUriString(toMwUrl) | 
|---|
|  |  |  | .build() | 
|---|
|  |  |  | .toUriString(); | 
|---|
|  |  |  | HttpHeaders headers = new HttpHeaders(); | 
|---|
|  |  |  | HttpEntity<?> httpEntity = new HttpEntity<>(body, headers); | 
|---|
|  |  |  | ResponseEntity<BaseResponse> response = null; | 
|---|
|  |  |  | ResponseEntity<BaseResponse> resEntity = null; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | // 通过Post方式调用接口 | 
|---|
|  |  |  | response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, BaseResponse.class); | 
|---|
|  |  |  | resEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, BaseResponse.class); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | return BaseResponseUtils.buildError("后端系统出错,中间件调用异常"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(response == null){ | 
|---|
|  |  |  | if(resEntity == null){ | 
|---|
|  |  |  | return BaseResponseUtils.buildError("后端系统出错,中间件调用异常"); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | return response.getBody(); | 
|---|
|  |  |  | return resEntity.getBody(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 向通信中间件发送rtu远程升级任务 | 
|---|
|  |  |  | * 向通信中间件发送Get请求 | 
|---|
|  |  |  | * @param restTemplate SpringBoot的RestTemplate | 
|---|
|  |  |  | * @param toMwUrl 到通信中间件的web请求Url | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | protected BaseResponse sendGetRequest2Mw(RestTemplate restTemplate, String toMwUrl) { | 
|---|
|  |  |  | public BaseResponse sendGetRequest2Mw(RestTemplate restTemplate, String toMwUrl) { | 
|---|
|  |  |  | return sendGetRequest2Mw(restTemplate, toMwUrl, null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 向通信中间件发送Get请求 | 
|---|
|  |  |  | * @param restTemplate SpringBoot的RestTemplate | 
|---|
|  |  |  | * @param toMwUrl 到通信中间件的web请求Url | 
|---|
|  |  |  | * @param paramName 参数名称 | 
|---|
|  |  |  | * @param paramValue 参数 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public BaseResponse sendGetRequest2Mw(RestTemplate restTemplate, String toMwUrl, String paramName, String paramValue) { | 
|---|
|  |  |  | String url = UriComponentsBuilder.fromUriString(toMwUrl) | 
|---|
|  |  |  | .build() | 
|---|
|  |  |  | .toUriString(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url) | 
|---|
|  |  |  | //        .queryParam("test", test); | 
|---|
|  |  |  | UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url) ; | 
|---|
|  |  |  | if(paramName != null && !paramName.trim().equals("")){ | 
|---|
|  |  |  | builder.queryParam(paramName, paramValue==null?"":paramValue) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return sendGetRequest2Mw(restTemplate, builder); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | ResponseEntity<BaseResponse> response = null; | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 向通信中间件发送Get请求 | 
|---|
|  |  |  | * @param restTemplate SpringBoot的RestTemplate | 
|---|
|  |  |  | * @param toMwUrl 到通信中间件的web请求Url | 
|---|
|  |  |  | * @param params 参数集合,参数名称是key,参数是value | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public BaseResponse sendGetRequest2Mw(RestTemplate restTemplate, String toMwUrl, Map<String, String> params) { | 
|---|
|  |  |  | String url = UriComponentsBuilder.fromUriString(toMwUrl) | 
|---|
|  |  |  | .build() | 
|---|
|  |  |  | .toUriString(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url) ; | 
|---|
|  |  |  | if(params != null && params.size() > 0){ | 
|---|
|  |  |  | Iterator<String> it = params.keySet().iterator() ; | 
|---|
|  |  |  | String key ; | 
|---|
|  |  |  | while (it.hasNext()){ | 
|---|
|  |  |  | key = it.next() ; | 
|---|
|  |  |  | builder.queryParam(key, params.get(key)) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return sendGetRequest2Mw(restTemplate, builder); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 向通信中间件发送Get请求 | 
|---|
|  |  |  | * @param restTemplate SpringBoot的RestTemplate | 
|---|
|  |  |  | * @param builder | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public BaseResponse sendGetRequest2Mw(RestTemplate restTemplate, UriComponentsBuilder builder) { | 
|---|
|  |  |  | ResponseEntity<BaseResponse> response; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | // 通过Get方式调用接口 | 
|---|
|  |  |  | response = restTemplate.exchange(builder.toUriString(), | 
|---|
|  |  |  | 
|---|
|  |  |  | new HttpEntity<>(new HttpHeaders()), | 
|---|
|  |  |  | BaseResponse.class); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | //e.printStackTrace(); | 
|---|
|  |  |  | return BaseResponseUtils.buildError("后端系统出错,中间件调用异常"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(response == null){ | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 创建外部命令(发给控制器) | 
|---|
|  |  |  | * @param comId 命令ID | 
|---|
|  |  |  | * @param code 命令code | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | protected Command createOuterCommand(String comId, String code) { | 
|---|
|  |  |  | public Command createOuterCommand(String comId, String code) { | 
|---|
|  |  |  | Command com = new Command(); | 
|---|
|  |  |  | com.id = comId; | 
|---|
|  |  |  | com.code = code ; | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 创建内部 | 
|---|
|  |  |  | * 创建外部命令(发给控制器) | 
|---|
|  |  |  | * @param rtuAddr 控制器地址 | 
|---|
|  |  |  | * @param comId 命令ID | 
|---|
|  |  |  | * @param code 命令code | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | protected Command createInnerCommand(String code) { | 
|---|
|  |  |  | public Command createOuterCommand(String rtuAddr, String comId, String code) { | 
|---|
|  |  |  | Command com = new Command(); | 
|---|
|  |  |  | com.id = comId; | 
|---|
|  |  |  | com.rtuAddr = rtuAddr; | 
|---|
|  |  |  | com.code = code ; | 
|---|
|  |  |  | com.type = CommandType.outerCommand; | 
|---|
|  |  |  | return com ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 创建外部透传命令(发给控制器) | 
|---|
|  |  |  | * @param comId 命令ID | 
|---|
|  |  |  | * @param code 命令code | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Command createOuterTransparentCommand(String comId, String code) { | 
|---|
|  |  |  | Command com = new Command(); | 
|---|
|  |  |  | com.id = comId; | 
|---|
|  |  |  | com.code = code ; | 
|---|
|  |  |  | com.type = CommandType.outerTransCommand; | 
|---|
|  |  |  | return com ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 创建外部透传命令(发给控制器) | 
|---|
|  |  |  | * @param rtuAddr 控制器地址 | 
|---|
|  |  |  | * @param comId 命令ID | 
|---|
|  |  |  | * @param code 命令code | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Command createOuterTransparentCommand(String rtuAddr, String comId, String code) { | 
|---|
|  |  |  | Command com = new Command(); | 
|---|
|  |  |  | com.id = comId; | 
|---|
|  |  |  | com.rtuAddr = rtuAddr; | 
|---|
|  |  |  | com.code = code ; | 
|---|
|  |  |  | com.type = CommandType.outerTransCommand; | 
|---|
|  |  |  | return com ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 创建内部命令 | 
|---|
|  |  |  | * @param code 命令code | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Command createInnerCommand(String code) { | 
|---|
|  |  |  | Command com = new Command(); | 
|---|
|  |  |  | com.id = Command.defaultId; | 
|---|
|  |  |  | com.code = code ; | 
|---|