package com.dy.pipIrrIrrigate.irrigatePlan;
|
|
import com.alibaba.fastjson2.JSON;
|
import com.alibaba.fastjson2.JSONObject;
|
import com.dy.common.multiDataSource.DataSourceContext;
|
import com.dy.common.mw.protocol.Command;
|
import com.dy.common.mw.protocol.CommandType;
|
import com.dy.common.webUtil.BaseResponse;
|
import com.dy.common.webUtil.BaseResponseUtils;
|
import com.dy.pipIrrGlobal.rtuMw.CodeLocal;
|
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.stereotype.Service;
|
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.util.UriComponentsBuilder;
|
|
/**
|
* @author ZhuBaoMin
|
* @date 2025-07-02 11:08
|
* @LastEditTime 2025-07-02 11:08
|
* @Description
|
*/
|
|
@Slf4j
|
@Service
|
public class IrrigationSv {
|
@Autowired
|
private Environment env;
|
|
@Autowired
|
private RestTemplate restTemplate;
|
|
private static final String pro_mw = "mw";
|
private String key_mw = "comSendUrl";
|
|
public Boolean getOnlineStatus(String rtuAdd) {
|
Command com = new Command();
|
com.id = Command.defaultId;
|
com.code = CodeLocal.onLinePart;
|
com.type = CommandType.innerCommand;
|
com.setRtuAddr(rtuAdd);
|
com.setParam(rtuAdd);
|
JSONObject response = (JSONObject) JSON.toJSON(sendCom2Mw(com));
|
|
if (response == null || !response.getString("code").equals("0001") || response.getJSONObject("content").getJSONObject("attachment").size() == 0) {
|
return false;
|
}
|
|
if(response.getJSONObject("content").getJSONObject("attachment").getBoolean(rtuAdd)) {
|
return true;
|
}
|
else {
|
return false;
|
}
|
}
|
|
public Boolean getOpenCloseStatus(String rtuAdd) {
|
Command com = new Command();
|
com.id = Command.defaultId;
|
com.code = CodeLocal.oneRtuStates;
|
com.type = CommandType.innerCommand;
|
com.setRtuAddr(rtuAdd);
|
com.setParam(rtuAdd);
|
JSONObject response = (JSONObject) JSON.toJSON(sendCom2Mw(com));
|
|
if (response == null || !response.getString("code").equals("0001") || response.getJSONObject("content").getJSONObject("attachment") == null) {
|
return false;
|
}
|
|
// if(response.getJSONObject("content").getJSONObject("attachment").get("valveOpenTrueCloseFalse").equals("true")) {
|
if(response.getJSONObject("content").getJSONObject("attachment").getBoolean("valveOpenTrueCloseFalse")) {
|
return true;
|
}
|
else {
|
return false;
|
}
|
}
|
|
/**
|
* 发送命令
|
*
|
* @return
|
*/
|
protected BaseResponse sendCom2Mw(Command com) {
|
String url = UriComponentsBuilder.fromUriString(env.getProperty(pro_mw + "." + DataSourceContext.get() + "." + key_mw))
|
.build()
|
.toUriString();
|
HttpHeaders headers = new HttpHeaders();
|
HttpEntity<Command> httpEntity = new HttpEntity<>(com, headers);
|
ResponseEntity<BaseResponse> response = null;
|
try {
|
// 通过Post方式调用接口
|
response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, BaseResponse.class);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
if(response == null) {
|
return BaseResponseUtils.buildErrorMsg("中间件调用失败");
|
}
|
|
return response.getBody();
|
}
|
}
|