zhubaomin
19 小时以前 6f2c512bffe6faaa8536fa98fbf2b2e552b6a2ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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();
    }
}