zhubaomin
6 天以前 00d54db361d697cf8fdf2f15940f4650b21b3185
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.dy.pipIrrRemote.monitor;
 
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.dy.common.mw.protocol.Command;
import com.dy.common.mw.protocol.CommandBackParam;
import com.dy.common.mw.protocol.rtuState.RtuStatus;
import com.dy.common.util.DateTime;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.QueryResultVo;
import com.dy.pipIrrGlobal.daoPr.PrIntakeMapper;
import com.dy.pipIrrGlobal.rtuMw.CodeLocal;
import com.dy.pipIrrGlobal.rtuMw.Web2RtuMw;
import com.dy.pipIrrGlobal.voPr.VoOnLineIntake;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.PojoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import java.text.DateFormat;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @Author: liurunyu
 * @Date: 2024/10/23 11:32
 * @Description
 */
@Slf4j
@Service
public class MonitorSv extends Web2RtuMw {
 
    @Autowired
    private PrIntakeMapper prIntakeMapper;
 
    @Autowired
    private Environment env;
 
    @Autowired
    private RestTemplate restTemplate;
 
    /**
     * 获取已经绑定控制器的取水口列表(并标记在线与不在线)
     *
     * @return
     */
    public QueryResultVo<List<VoOnLineIntake>> selectOnLineIntakes(QueryVo vo) {
        if(vo.getIsOnLine() == null){
            //不区分在线与离线
            return selectIntakesIgnoreOnOffLine(vo) ;
        }else{
            //在线 或 离线
            return selectIntakesOnOrOffLine(vo) ;
        }
    }
 
    /**
     * 查询RTU在通信中间件中的状态
     * @param rtuAddr
     * @return
     */
    public RtuStatus rtuStatus(String rtuAddr) throws Exception{
        //向通信中间件发关命令,查询部分RTU在线情况
        RtuStatus rtuStatus = null ;
        Command com = this.createInnerCommand(CodeLocal.oneRtuStates);
        com.setParam(rtuAddr) ;
        String rqUrl = this.get2MwRequestUrl(this.env, ContextComSend) ;
        BaseResponse res = sendPostRequest2Mw(restTemplate, rqUrl, com) ;
        if(res != null){
            if(res.isSuccess()){
                Command reCom = JSON.parseObject(res.getContent() == null ? null : JSON.toJSONString(res.getContent()), Command.class) ;
                CommandBackParam bakParam = JSON.parseObject((reCom== null || reCom.param == null) ? null : JSON.toJSONString(reCom.param), CommandBackParam.class) ;
                if(bakParam != null){
                    if(bakParam.getSuccess().booleanValue()){
                        //通信中间件成功返回命令结果
                        if(reCom.getAttachment() != null){
                            rtuStatus = JSON.parseObject(JSON.toJSONString(reCom.getAttachment()), RtuStatus.class);
                        }else{
                            throw new Exception("通信中间件未掌握该RTU的状态,可能该RTU未上线") ;
                        }
                    }
                }else{
                    log.error("通信中间件返回内部命令结果中不包含CommandBackParam类型参数");
                    throw new Exception("通信中间件执行命令出错") ;
                }
            }else{
                log.error("通信中间件返回内部命令执行失败" + (res.getMsg() == null? "" : ("," + res.getMsg()))) ;
                throw new Exception("通信中间件执行命令出错" + (res.getMsg() == null? "" : ("," + res.getMsg()))) ;
            }
        }else{
            log.error("通信中间件返回内部命令结果为null");
            throw new Exception("通信中间件执行命令出错,返回内部命令结果为null") ;
        }
        return rtuStatus ;
    }
 
    /**
     * 查询取水口,不限制在线与离线状态
     * @param vo
     * @return
     */
    private QueryResultVo<List<VoOnLineIntake>> selectIntakesIgnoreOnOffLine(QueryVo vo) {
        QueryResultVo<List<VoOnLineIntake>> rsVo = this.queryDb(vo) ;
        if(rsVo != null && rsVo.obj != null && (rsVo.obj.size() > 0)){
            String rtuAddrs = "" ;
            boolean first = true ;
            for(VoOnLineIntake rVo : rsVo.obj){
                if(first){
                    first = false ;
                    rtuAddrs += rVo.getRtuAddr() ;
                }else{
                    rtuAddrs += "," + rVo.getRtuAddr() ;
                }
            }
            //向通信中间件发关命令,查询部分RTU在线情况
            Command com = this.createInnerCommand(CodeLocal.onLinePart);
            com.setParam(rtuAddrs) ;
            String rqUrl = this.get2MwRequestUrl(this.env, ContextComSend) ;
            BaseResponse res = sendPostRequest2Mw(restTemplate, rqUrl, com) ;
            if(res != null){
                if(res.isSuccess()){
                    Command reCom = JSON.parseObject(res.getContent() == null ? null : JSON.toJSONString(res.getContent()), Command.class) ;
                    CommandBackParam bakParam = JSON.parseObject((reCom== null || reCom.param == null) ? null : JSON.toJSONString(reCom.param), CommandBackParam.class) ;
                    if(bakParam != null){
                        if(bakParam.getSuccess().booleanValue()){
                            //通信中间件成功返回命令结果
                            HashMap<String, Boolean> onLineMap = JSON.parseObject(JSON.toJSONString(reCom.getAttachment()), HashMap.class);
                            for(VoOnLineIntake rVo : rsVo.obj){
                                if(onLineMap.containsKey(rVo.getRtuAddr())) {
                                    rVo.setIsOnLine(onLineMap.get(rVo.getRtuAddr()));
                                }
                            }
                        }
                    }else{
                        log.error("通信中间件返回内部命令结果中不包含CommandBackParam类型参数");
                    }
                }else{
                    log.error("通信中间件返回内部命令执行失败" + (res.getMsg() == null? "" : ("," + res.getMsg()))) ;
                }
            }else{
                log.error("通信中间件返回内部命令结果为null");
            }
        }
        return rsVo;
    }
 
 
    /**
     * 查询取水口,在线或离线状态
     * @param vo
     * @return
     */
    private QueryResultVo<List<VoOnLineIntake>> selectIntakesOnOrOffLine(QueryVo vo) {
        //向通信中间件发关命令,查询部分RTU在线情况
        Command com = this.createInnerCommand(CodeLocal.onLineAll);
        String rqUrl = this.get2MwRequestUrl(this.env, ContextComSend) ;
        BaseResponse res = sendPostRequest2Mw(restTemplate, rqUrl, com) ;
        if(res != null){
            if(res.isSuccess()){
                Command reCom = JSON.parseObject(res.getContent() == null ? null : JSON.toJSONString(res.getContent()), Command.class) ;
                CommandBackParam bakParam = JSON.parseObject((reCom== null || reCom.param == null) ? null : JSON.toJSONString(reCom.param), CommandBackParam.class) ;
                if(bakParam != null){
                    if(bakParam.getSuccess().booleanValue()){
                        //通信中间件成功返回命令结果
                        HashMap<String, Boolean> onLineMap = JSON.parseObject(JSON.toJSONString(reCom.getAttachment()), HashMap.class);
                        JSONArray jsonArray = new JSONArray();
                        for (Map.Entry<String, Boolean> entry : onLineMap.entrySet()) {
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put("rtuAddr", entry.getKey());
                            jsonObject.put("isOnLine", entry.getValue());
                            jsonArray.add(jsonObject);
                        }
                        vo.setOnLineMap(jsonArray.toJSONString());
                    }
                }else{
                    log.error("通信中间件返回内部命令结果中不包含CommandBackParam类型参数");
                }
            }else{
                log.error("通信中间件返回内部命令执行失败" + (res.getMsg() == null? "" : ("," + res.getMsg()))) ;
            }
        }else{
            log.error("通信中间件返回内部命令结果为null");
        }
 
        return this.queryDb(vo) ;
    }
 
    /**
     * 查询数据库
     * @param vo
     * @return
     */
    private QueryResultVo<List<VoOnLineIntake>> queryDb(QueryVo vo){
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(vo);
        Long itemTotal = prIntakeMapper.selectIntakesCountForOnLine(params);
 
        QueryResultVo<List<VoOnLineIntake>> rsVo = new QueryResultVo<>();
        rsVo.pageSize = vo.pageSize;
        rsVo.pageCurr = vo.pageCurr;
        rsVo.calculateAndSet(itemTotal, params);
        List<VoOnLineIntake> records = prIntakeMapper.selectIntakesForOnLine(params) ;
        rsVo.obj = records ;
        for(VoOnLineIntake rVo : records){
            if(rVo.getAlarm() != null && !rVo.getAlarm().trim().equals("") && rVo.getAlarm().endsWith(",")){
                if(rVo.getAlarmDt() != null){
                    rVo.setAlarm(rVo.getAlarm().substring(0, rVo.getAlarm().length() - 1) + "\n" + DateTime.yyyy_MM_dd_HH_mm_ss(rVo.getAlarmDt()) ) ;
                }else{
                    rVo.setAlarm(rVo.getAlarm().substring(0, rVo.getAlarm().length() - 1)) ;
                }
            }
        }
        return rsVo ;
    }
 
}