package com.iailab.module.data.channel.kio.collector;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.iailab.module.data.common.utils.HttpRequest;
|
import com.iailab.module.data.channel.kio.dto.*;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* @author PanZhibao
|
* @Description
|
* @createTime 2024年06月04日
|
*/
|
public class KingIOClient {
|
|
private boolean bLogin = false;
|
|
private boolean bConnect = false;
|
|
private String ip;
|
|
private int port;
|
|
private String username;
|
|
private String password;
|
|
private String instanceName;
|
|
private String authorization;
|
|
private String url;
|
|
private String chartset = "utf-8";
|
|
private final String R_CODE = "code";
|
|
private final String R_MSG = "message";
|
|
private final String R_DATA = "data";
|
|
private final int S_CODE = 0;
|
|
private final String DATA_V = "V";
|
|
private long exp_login = 1000 * 60;
|
|
private long logint_time = 0;
|
|
private long heartbeat_last = 0;
|
|
private int Q_192 = 192;
|
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
|
KingIOClient(String instanceName) {
|
this.instanceName = instanceName;
|
}
|
|
/**
|
* 登录设备
|
*
|
* @param ip
|
* @param port
|
* @param username
|
* @param password
|
*/
|
public boolean login(String ip, int port, String username, String password) {
|
if ((System.currentTimeMillis() - this.logint_time) < exp_login) {
|
return bLogin;
|
}
|
this.ip = ip;
|
this.port = port;
|
this.username = username;
|
this.password = password;
|
this.url = "http://" + ip + ":" + port + "/api/v1";
|
Map<String, String> form = new HashMap<>(2);
|
form.put("username", username);
|
form.put("password", password);
|
this.logint_time = System.currentTimeMillis();
|
String responseStr = HttpRequest.sendPost(this.url + "/login", JSON.toJSONString(form));
|
JSONObject responseObj = JSON.parseObject(responseStr);
|
if (S_CODE != Integer.parseInt(responseObj.get(R_CODE).toString())) {
|
bLogin = false;
|
return false;
|
}
|
this.authorization = responseObj.getJSONObject(R_DATA).get("Authorization").toString();
|
bLogin = true;
|
return bLogin;
|
}
|
|
public boolean reLogin() {
|
return login(this.ip, this.port, this.username, this.password);
|
}
|
|
public boolean isExpLogin() {
|
/*if ((System.currentTimeMillis() - logint_time) > exp_login) {
|
return true;
|
} else {
|
return false;
|
}*/
|
if ((System.currentTimeMillis() - logint_time) > exp_login) {
|
this.bLogin = false;
|
return true;
|
}return false;
|
|
}
|
|
public boolean isConnect() {
|
try {
|
if ((System.currentTimeMillis() - heartbeat_last) < 10000) {
|
return bConnect;
|
}
|
Map<String, String> map = new HashMap<>();
|
String responseStr = HttpRequest.sendPost(this.url + "/heartbeat", "", this.authorization);
|
JSONObject responseObj = JSON.parseObject(responseStr);
|
if (S_CODE != Integer.parseInt(responseObj.get(R_CODE).toString())) {
|
bConnect = false;
|
} else {
|
logger.info("KIO心跳正常。");
|
bConnect = true;
|
}
|
heartbeat_last = System.currentTimeMillis();
|
} catch (Exception ex) {
|
logger.info("KIO心跳异常!" + ex.getMessage());
|
bConnect = false;
|
}
|
return bConnect;
|
}
|
|
public String getTagValue(String tagName) {
|
Map<String, String> map = new HashMap<>();
|
map.put("projectInstanceName", this.instanceName);
|
map.put("TagName", tagName);
|
String responseStr = HttpRequest.sendGet(this.url + "/realvalue", map, this.authorization);
|
JSONObject responseObj = JSON.parseObject(responseStr);
|
if (S_CODE != Integer.parseInt(responseObj.get(R_CODE).toString())) {
|
return null;
|
}
|
JSONObject data = responseObj.getJSONObject(R_DATA);
|
Object dataV = data.get(DATA_V);
|
return dataV.toString();
|
}
|
|
public Map<String, String> getTagsValue(List<String> tagNames) {
|
Map<String, String> result = new HashMap<>();
|
KIOWriteObjDTO queryDto = new KIOWriteObjDTO();
|
List<KIOWriteDTO> objs = new ArrayList<>();
|
tagNames.forEach(item -> {
|
KIOWriteDTO dto = new KIOWriteDTO();
|
dto.setN(item);
|
objs.add(dto);
|
});
|
queryDto.setObjs(objs);
|
String responseStr = HttpRequest.sendPost(this.url + "/batchrealvalue", JSONObject.toJSONString(queryDto), this.authorization);
|
JSONObject responseObj = JSON.parseObject(responseStr);
|
if (S_CODE != Integer.parseInt(responseObj.get(R_CODE).toString())) {
|
return null;
|
}
|
JSONArray data = responseObj.getJSONArray(R_DATA);
|
for (int i = 0; i < data.size(); i++) {
|
JSONObject obj = data.getJSONObject(i);
|
if (obj.getInteger("Q") != Q_192) {
|
continue;
|
}
|
result.put(obj.getString("N"), obj.get(DATA_V).toString());
|
}
|
return result;
|
}
|
|
public void writeFloatValue(String tagName, Float newValue) throws Exception {
|
KIOWriteObjDTO writeObj = new KIOWriteObjDTO();
|
List<KIOWriteDTO> objs = new ArrayList<>();
|
KIOWriteFloatDTO writeDTO = new KIOWriteFloatDTO();
|
writeDTO.setN(tagName);
|
writeDTO.setV(newValue);
|
objs.add(writeDTO);
|
writeObj.setObjs(objs);
|
String writeStr = JSONObject.toJSONString(writeObj);
|
String responseStr = HttpRequest.sendPost(this.url + "/realvariables", writeStr, this.authorization);
|
JSONObject responseObj = JSON.parseObject(responseStr);
|
if (S_CODE != Integer.parseInt(responseObj.get(R_CODE).toString())) {
|
throw new Exception(responseObj.getString(R_MSG));
|
}
|
|
}
|
|
public void writeIntValue(String tagName, Integer newValue) throws Exception {
|
KIOWriteObjDTO writeObj = new KIOWriteObjDTO();
|
List<KIOWriteDTO> objs = new ArrayList<>();
|
KIOWriteIntDTO writeDTO = new KIOWriteIntDTO();
|
writeDTO.setN(tagName);
|
writeDTO.setV(newValue);
|
objs.add(writeDTO);
|
writeObj.setObjs(objs);
|
String responseStr = HttpRequest.sendPost(this.url + "/realvariables", JSONObject.toJSONString(writeObj), this.authorization);
|
JSONObject responseObj = JSON.parseObject(responseStr);
|
if (S_CODE != Integer.parseInt(responseObj.get(R_CODE).toString())) {
|
throw new Exception(responseObj.getString(R_MSG));
|
}
|
}
|
|
public void writeBooleanValue(String tagName, Boolean newValue) throws Exception {
|
KIOWriteObjDTO writeObj = new KIOWriteObjDTO();
|
List<KIOWriteDTO> objs = new ArrayList<>();
|
KIOWriteBooleanDTO writeDTO = new KIOWriteBooleanDTO();
|
writeDTO.setN(tagName);
|
writeDTO.setV(newValue);
|
objs.add(writeDTO);
|
writeObj.setObjs(objs);
|
String responseStr = HttpRequest.sendPost(this.url + "/realvariables", JSONObject.toJSONString(writeObj), this.authorization);
|
JSONObject responseObj = JSON.parseObject(responseStr);
|
if (S_CODE != Integer.parseInt(responseObj.get(R_CODE).toString())) {
|
throw new Exception(responseObj.getString(R_MSG));
|
}
|
}
|
}
|