提交 | 用户 | 时间
|
a6de49
|
1 |
package com.iailab.module.data.channel.kio.collector; |
H |
2 |
|
|
3 |
import com.alibaba.fastjson.JSON; |
|
4 |
import com.alibaba.fastjson.JSONArray; |
|
5 |
import com.alibaba.fastjson.JSONObject; |
|
6 |
import com.iailab.module.data.common.utils.HttpRequest; |
|
7 |
import com.iailab.module.data.channel.kio.dto.*; |
|
8 |
import org.slf4j.Logger; |
|
9 |
import org.slf4j.LoggerFactory; |
|
10 |
|
|
11 |
import java.util.ArrayList; |
|
12 |
import java.util.HashMap; |
|
13 |
import java.util.List; |
|
14 |
import java.util.Map; |
|
15 |
|
|
16 |
/** |
|
17 |
* @author PanZhibao |
|
18 |
* @Description |
|
19 |
* @createTime 2024年06月04日 |
|
20 |
*/ |
|
21 |
public class KingIOClient { |
|
22 |
|
|
23 |
private boolean bLogin = false; |
|
24 |
|
|
25 |
private boolean bConnect = false; |
|
26 |
|
|
27 |
private String ip; |
|
28 |
|
|
29 |
private int port; |
|
30 |
|
|
31 |
private String username; |
|
32 |
|
|
33 |
private String password; |
|
34 |
|
|
35 |
private String instanceName; |
|
36 |
|
|
37 |
private String authorization; |
|
38 |
|
|
39 |
private String url; |
|
40 |
|
|
41 |
private String chartset = "utf-8"; |
|
42 |
|
|
43 |
private final String R_CODE = "code"; |
|
44 |
|
|
45 |
private final String R_MSG = "message"; |
|
46 |
|
|
47 |
private final String R_DATA = "data"; |
|
48 |
|
|
49 |
private final int S_CODE = 0; |
|
50 |
|
|
51 |
private final String DATA_V = "V"; |
|
52 |
|
|
53 |
private long exp_login = 1000 * 60; |
|
54 |
|
|
55 |
private long logint_time = 0; |
|
56 |
|
|
57 |
private long heartbeat_last = 0; |
|
58 |
|
|
59 |
private int Q_192 = 192; |
|
60 |
|
|
61 |
private Logger logger = LoggerFactory.getLogger(getClass()); |
|
62 |
|
|
63 |
KingIOClient(String instanceName) { |
|
64 |
this.instanceName = instanceName; |
|
65 |
} |
|
66 |
|
|
67 |
/** |
|
68 |
* 登录设备 |
|
69 |
* |
|
70 |
* @param ip |
|
71 |
* @param port |
|
72 |
* @param username |
|
73 |
* @param password |
|
74 |
*/ |
|
75 |
public boolean login(String ip, int port, String username, String password) { |
|
76 |
if ((System.currentTimeMillis() - this.logint_time) < exp_login) { |
|
77 |
return bLogin; |
|
78 |
} |
|
79 |
this.ip = ip; |
|
80 |
this.port = port; |
|
81 |
this.username = username; |
|
82 |
this.password = password; |
|
83 |
this.url = "http://" + ip + ":" + port + "/api/v1"; |
|
84 |
Map<String, String> form = new HashMap<>(2); |
|
85 |
form.put("username", username); |
|
86 |
form.put("password", password); |
|
87 |
this.logint_time = System.currentTimeMillis(); |
|
88 |
String responseStr = HttpRequest.sendPost(this.url + "/login", JSON.toJSONString(form)); |
|
89 |
JSONObject responseObj = JSON.parseObject(responseStr); |
|
90 |
if (S_CODE != Integer.parseInt(responseObj.get(R_CODE).toString())) { |
|
91 |
bLogin = false; |
|
92 |
return false; |
|
93 |
} |
|
94 |
this.authorization = responseObj.getJSONObject(R_DATA).get("Authorization").toString(); |
|
95 |
bLogin = true; |
|
96 |
return bLogin; |
|
97 |
} |
|
98 |
|
|
99 |
public boolean reLogin() { |
|
100 |
return login(this.ip, this.port, this.username, this.password); |
|
101 |
} |
|
102 |
|
|
103 |
public boolean isExpLogin() { |
|
104 |
/*if ((System.currentTimeMillis() - logint_time) > exp_login) { |
|
105 |
return true; |
|
106 |
} else { |
|
107 |
return false; |
|
108 |
}*/ |
|
109 |
if ((System.currentTimeMillis() - logint_time) > exp_login) { |
|
110 |
this.bLogin = false; |
|
111 |
return true; |
|
112 |
}return false; |
|
113 |
|
|
114 |
} |
|
115 |
|
|
116 |
public boolean isConnect() { |
|
117 |
try { |
|
118 |
if ((System.currentTimeMillis() - heartbeat_last) < 10000) { |
|
119 |
return bConnect; |
|
120 |
} |
|
121 |
Map<String, String> map = new HashMap<>(); |
|
122 |
String responseStr = HttpRequest.sendPost(this.url + "/heartbeat", "", this.authorization); |
|
123 |
JSONObject responseObj = JSON.parseObject(responseStr); |
|
124 |
if (S_CODE != Integer.parseInt(responseObj.get(R_CODE).toString())) { |
|
125 |
bConnect = false; |
|
126 |
} else { |
|
127 |
logger.info("KIO心跳正常。"); |
|
128 |
bConnect = true; |
|
129 |
} |
|
130 |
heartbeat_last = System.currentTimeMillis(); |
|
131 |
} catch (Exception ex) { |
|
132 |
logger.info("KIO心跳异常!" + ex.getMessage()); |
|
133 |
bConnect = false; |
|
134 |
} |
|
135 |
return bConnect; |
|
136 |
} |
|
137 |
|
|
138 |
public String getTagValue(String tagName) { |
|
139 |
Map<String, String> map = new HashMap<>(); |
|
140 |
map.put("projectInstanceName", this.instanceName); |
|
141 |
map.put("TagName", tagName); |
|
142 |
String responseStr = HttpRequest.sendGet(this.url + "/realvalue", map, this.authorization); |
|
143 |
JSONObject responseObj = JSON.parseObject(responseStr); |
|
144 |
if (S_CODE != Integer.parseInt(responseObj.get(R_CODE).toString())) { |
|
145 |
return null; |
|
146 |
} |
|
147 |
JSONObject data = responseObj.getJSONObject(R_DATA); |
|
148 |
Object dataV = data.get(DATA_V); |
|
149 |
return dataV.toString(); |
|
150 |
} |
|
151 |
|
|
152 |
public Map<String, String> getTagsValue(List<String> tagNames) { |
|
153 |
Map<String, String> result = new HashMap<>(); |
|
154 |
KIOWriteObjDTO queryDto = new KIOWriteObjDTO(); |
|
155 |
List<KIOWriteDTO> objs = new ArrayList<>(); |
|
156 |
tagNames.forEach(item -> { |
|
157 |
KIOWriteDTO dto = new KIOWriteDTO(); |
|
158 |
dto.setN(item); |
|
159 |
objs.add(dto); |
|
160 |
}); |
|
161 |
queryDto.setObjs(objs); |
|
162 |
String responseStr = HttpRequest.sendPost(this.url + "/batchrealvalue", JSONObject.toJSONString(queryDto), this.authorization); |
|
163 |
JSONObject responseObj = JSON.parseObject(responseStr); |
|
164 |
if (S_CODE != Integer.parseInt(responseObj.get(R_CODE).toString())) { |
|
165 |
return null; |
|
166 |
} |
|
167 |
JSONArray data = responseObj.getJSONArray(R_DATA); |
|
168 |
for (int i = 0; i < data.size(); i++) { |
|
169 |
JSONObject obj = data.getJSONObject(i); |
|
170 |
if (obj.getInteger("Q") != Q_192) { |
|
171 |
continue; |
|
172 |
} |
|
173 |
result.put(obj.getString("N"), obj.get(DATA_V).toString()); |
|
174 |
} |
|
175 |
return result; |
|
176 |
} |
|
177 |
|
|
178 |
public void writeFloatValue(String tagName, Float newValue) throws Exception { |
|
179 |
KIOWriteObjDTO writeObj = new KIOWriteObjDTO(); |
|
180 |
List<KIOWriteDTO> objs = new ArrayList<>(); |
|
181 |
KIOWriteFloatDTO writeDTO = new KIOWriteFloatDTO(); |
|
182 |
writeDTO.setN(tagName); |
|
183 |
writeDTO.setV(newValue); |
|
184 |
objs.add(writeDTO); |
|
185 |
writeObj.setObjs(objs); |
|
186 |
String writeStr = JSONObject.toJSONString(writeObj); |
|
187 |
String responseStr = HttpRequest.sendPost(this.url + "/realvariables", writeStr, this.authorization); |
|
188 |
JSONObject responseObj = JSON.parseObject(responseStr); |
|
189 |
if (S_CODE != Integer.parseInt(responseObj.get(R_CODE).toString())) { |
|
190 |
throw new Exception(responseObj.getString(R_MSG)); |
|
191 |
} |
|
192 |
|
|
193 |
} |
|
194 |
|
|
195 |
public void writeIntValue(String tagName, Integer newValue) throws Exception { |
|
196 |
KIOWriteObjDTO writeObj = new KIOWriteObjDTO(); |
|
197 |
List<KIOWriteDTO> objs = new ArrayList<>(); |
|
198 |
KIOWriteIntDTO writeDTO = new KIOWriteIntDTO(); |
|
199 |
writeDTO.setN(tagName); |
|
200 |
writeDTO.setV(newValue); |
|
201 |
objs.add(writeDTO); |
|
202 |
writeObj.setObjs(objs); |
|
203 |
String responseStr = HttpRequest.sendPost(this.url + "/realvariables", JSONObject.toJSONString(writeObj), this.authorization); |
|
204 |
JSONObject responseObj = JSON.parseObject(responseStr); |
|
205 |
if (S_CODE != Integer.parseInt(responseObj.get(R_CODE).toString())) { |
|
206 |
throw new Exception(responseObj.getString(R_MSG)); |
|
207 |
} |
|
208 |
} |
|
209 |
|
|
210 |
public void writeBooleanValue(String tagName, Boolean newValue) throws Exception { |
|
211 |
KIOWriteObjDTO writeObj = new KIOWriteObjDTO(); |
|
212 |
List<KIOWriteDTO> objs = new ArrayList<>(); |
|
213 |
KIOWriteBooleanDTO writeDTO = new KIOWriteBooleanDTO(); |
|
214 |
writeDTO.setN(tagName); |
|
215 |
writeDTO.setV(newValue); |
|
216 |
objs.add(writeDTO); |
|
217 |
writeObj.setObjs(objs); |
|
218 |
String responseStr = HttpRequest.sendPost(this.url + "/realvariables", JSONObject.toJSONString(writeObj), this.authorization); |
|
219 |
JSONObject responseObj = JSON.parseObject(responseStr); |
|
220 |
if (S_CODE != Integer.parseInt(responseObj.get(R_CODE).toString())) { |
|
221 |
throw new Exception(responseObj.getString(R_MSG)); |
|
222 |
} |
|
223 |
} |
|
224 |
} |