Administrator
2024-05-27 f5a7f6c30bc5fb13eb538b5856a663b1ba0667b6
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
package com.dy.pipIrrRemote.common;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.dy.common.mw.protocol.Command;
import com.dy.common.mw.protocol.CommandType;
import com.dy.common.mw.protocol.Data;
import com.dy.common.mw.protocol.p206V202404.CodeV202404;
import com.dy.common.mw.protocol.p206V202404.ProtocolConstantV206V202404;
import com.dy.common.mw.protocol.p206V202404.downVos.ComCdXyVo;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import com.dy.pipIrrGlobal.daoBa.BaSettingsMapper;
import com.dy.pipIrrGlobal.pojoRm.RmCommandHistory;
import com.dy.pipIrrRemote.result.RemoteResultCode;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
 
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
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-21 15:30
 * @LastEditTime 2024-05-21 15:30
 * @Description
 */
 
@Component
public class ComSupport {
    //@NotNull
    //private final CommandSv commandSv;
 
    protected static String mwUrlTest = "http://127.0.0.1:8070/rtuMw/com/test" ;
    protected static String mwUrlSendCom = "http://127.0.0.1:8070/rtuMw/com/send" ;
 
    //protected static String rtuAddr = "37142501020100215" ;
    protected static String rtuResultSendWebUrl = "http://127.0.0.1:8081/remote/comRes/receive" ;
 
    protected static String controllerType = "57" ;//控制器类型
    protected static Integer projectNo = 100 ;//项目编码
 
    protected static String icCardAddr = "04BEA5BB" ;//IC卡地址
    protected static String icCardNo = "37142501020500001" ;//IC卡编号(用户卡序列号)
 
    protected String protocolName = ProtocolConstantV206V202404.protocolName;
 
    protected String commandTypeOuter = CommandType.outerCommand;
 
    // 存储实例化的 CompletableFuture<Data> 对象
    protected static Map<String, Object> features = new HashMap<>();
    protected static Boolean setuped = false;
 
    @Autowired
    private RestTemplate restTemplate;
 
    @Autowired
    private BaSettingsMapper baSettingsMapper;
    public static ComSupport comSupport;
 
    /**
     * 引入BaSettingsMapper
     */
    @PostConstruct
    public void init() {
        comSupport = this;
        comSupport.baSettingsMapper = this.baSettingsMapper;
    }
 
    /**
     * 获取系统配置参数
     */
    public void setUp() {
        protocolName = comSupport.baSettingsMapper.getItemValue("protocolName");
        controllerType = comSupport.baSettingsMapper.getItemValue("controllerType");
        projectNo = Integer.parseInt(comSupport.baSettingsMapper.getItemValue("projectNo"));
        setuped = true;
    }
 
    protected ComCdXyVo comCdXyVo(){
        ComCdXyVo comVo = new ComCdXyVo() ;
        comVo.controllerType = controllerType ;
        comVo.projectNo = projectNo  ;
        return comVo ;
    }
 
    /**
     * 创建命令日志对象
     * @param commandCode 功能码
     * @param rtuAddr 阀控器地址
     * @param param 参数数据
     * @param operator 操作员
     * @return 零零日志对象
     */
    protected RmCommandHistory getComHistory(String commandCode, String rtuAddr, Object param, Long operator ) {
        RmCommandHistory rmCommandHistory = new RmCommandHistory();
        rmCommandHistory.setCommandCode(commandCode);
        rmCommandHistory.setCommandName(CodeV202404.getCodeName(commandCode));
        rmCommandHistory.setRtuaddr(rtuAddr);
        rmCommandHistory.setProtocol(protocolName);
        rmCommandHistory.setParam((JSONObject) JSON.toJSON(param));
        rmCommandHistory.setSendTime(new Date());
        rmCommandHistory.setOperator(operator);
        return rmCommandHistory;
    }
 
    /**
     * 构造命令对象
     * @param comId 命令ID
     * @param commandCode 功能码
     * @param rtuAddr RTU地址
     * @param param 参数数据
     * @return 构造好的命令对象
     */
    protected Command command(String comId, String commandCode, String rtuAddr, Object param){
        Command com = new Command() ;
        com.id = comId==null?Command.defaultId:(comId.trim().equals("")?Command.defaultId:comId);
        com.code = commandCode ;
        com.rtuAddr = rtuAddr ;
 
        com.protocol = protocolName;
        com.type = commandTypeOuter;
        com.rtuResultSendWebUrl = rtuResultSendWebUrl ;
 
        com.param = param ;
 
        return com ;
    }
 
    /**
     * 处理回调内容
     * @param comId
     * @return
     */
    protected BaseResponse<Data> dealWithCallBack(String comId) {
        CompletableFuture<Data> featureObject = new CompletableFuture<>();
        features.put(comId, featureObject);
        try {
            CompletableFuture<Data> feature = (CompletableFuture<Data>) features.get(comId);
            System.out.println("receive result ID:" + comId);
            Data resultData = feature.get(30, TimeUnit.SECONDS);
            features.remove(comId);
            String commandId = resultData.getCommandId();
            if(commandId.equals(comId)) {
                return BaseResponseUtils.buildSuccess(resultData);
            }else {
                return BaseResponseUtils.buildSuccess();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            return BaseResponseUtils.buildFail(RemoteResultCode.GET_RESULT_ERROR.getMessage());
        } catch (ExecutionException e) {
            e.printStackTrace();
            return BaseResponseUtils.buildFail(RemoteResultCode.GET_RESULT_ERROR.getMessage());
        } catch (TimeoutException e) {
            return BaseResponseUtils.buildFail(RemoteResultCode.GET_RESULT_IN_ONE_MINUTE.getMessage());
        }
    }
 
    /**
     * 连接通信中间件测试
     * @return
     */
    protected BaseResponse sendTest(){
        String url = UriComponentsBuilder.fromUriString(mwUrlTest)
                .build()
                .toUriString();
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<?> httpEntity = new HttpEntity<>(headers);
        ResponseEntity<BaseResponse> response = null;
        try {
            // 通过Get方式调用接口
            response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, BaseResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response.getBody();
    }
 
    /**
     * 发送命令
     * @return
     */
    protected BaseResponse sendCom2Mw(Command com){
        String url = UriComponentsBuilder.fromUriString(mwUrlSendCom)
                .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();
        }
        return response.getBody();
    }
 
}