feat: 增加61850断线重连

dev
huangfeng 8 months ago
parent 74c14169ac
commit f8fcabc730

@ -53,7 +53,7 @@ public class IEDCollectService {
} }
public void connect() throws Exception { public void connect() throws Exception {
iecClient.connect(ied.getIp(), ied.getPort(), ied.getApTitle(), xml); iecClient.connect(ied, xml);
} }
public void disconnect() { public void disconnect() {

@ -1,6 +1,7 @@
package com.xydl.cac.iec; package com.xydl.cac.iec;
import com.beanit.iec61850bean.*; import com.beanit.iec61850bean.*;
import com.xydl.cac.entity.IcdIed;
import com.xydl.cac.entity.constants.Constants; import com.xydl.cac.entity.constants.Constants;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
@ -17,19 +18,26 @@ import java.util.List;
@Slf4j @Slf4j
public class IecClient implements ClientEventListener { public class IecClient implements ClientEventListener {
IcdIed ied;
int[] title;
ClientSap clientSap = new ClientSap(); ClientSap clientSap = new ClientSap();
ClientAssociation clientAssociation = null; ClientAssociation clientAssociation = null;
ServerModel serverModel; ServerModel serverModel;
public void connect(String host, int port, String apTitle, String xml) throws Exception { public boolean keep = false;
public RealTimeDataService realTimeDataService;
int retry = 10;
public void connect(IcdIed _ied, String xml) throws Exception {
InputStream in = IOUtils.toInputStream(xml, StandardCharsets.UTF_8); InputStream in = IOUtils.toInputStream(xml, StandardCharsets.UTF_8);
this.connect(host, port, apTitle, in); this.connect(_ied, in);
} }
public void connect(String host, int port, String apTitle, InputStream in) throws Exception { public void connect(IcdIed _ied, InputStream in) throws Exception {
int[] title = new int[]{1, 3, 9999, 33}; ied = _ied;
if (StringUtils.isNotBlank(apTitle)) { title = new int[]{1, 3, 9999, 33};
String[] strs = apTitle.replaceAll(" ", ",").split(","); if (StringUtils.isNotBlank(_ied.getApTitle())) {
String[] strs = _ied.getApTitle().replaceAll(" ", ",").split(",");
if (strs.length == 4) { if (strs.length == 4) {
title[0] = Integer.parseInt(strs[0]); title[0] = Integer.parseInt(strs[0]);
title[1] = Integer.parseInt(strs[1]); title[1] = Integer.parseInt(strs[1]);
@ -47,9 +55,9 @@ public class IecClient implements ClientEventListener {
clientSap.setTSelRemote(new byte[]{0, 1}); clientSap.setTSelRemote(new byte[]{0, 1});
clientSap.setTSelLocal(new byte[]{0, 0}); clientSap.setTSelLocal(new byte[]{0, 0});
clientSap.setApTitleCalled(title); clientSap.setApTitleCalled(title);
clientAssociation = clientSap.associate(InetAddress.getByName(host), port, null, this);
serverModel = SclParser.parse(in).get(0); serverModel = SclParser.parse(in).get(0);
clientAssociation = clientSap.associate(InetAddress.getByName(ied.getIp()), ied.getPort(), null, this);
clientAssociation.setServerModel(serverModel); clientAssociation.setServerModel(serverModel);
} }
@ -60,6 +68,12 @@ public class IecClient implements ClientEventListener {
} }
} }
private void reconnect() throws Exception {
clientAssociation = clientSap.associate(InetAddress.getByName(ied.getIp()), ied.getPort(), null, this);
clientAssociation.setServerModel(serverModel);
log.info("61850断线重连成功, ied=" + ied.getName() + ", ip=" + ied.getIp() + ", port=" + ied.getPort());
}
public String getValue(String paramindex, String fc) throws Exception { public String getValue(String paramindex, String fc) throws Exception {
FcModelNode node = (FcModelNode) serverModel.findModelNode(paramindex, Fc.valueOf(fc)); FcModelNode node = (FcModelNode) serverModel.findModelNode(paramindex, Fc.valueOf(fc));
clientAssociation.getDataValues(node); clientAssociation.getDataValues(node);
@ -124,13 +138,49 @@ public class IecClient implements ClientEventListener {
@Override @Override
public void associationClosed(IOException e) { public void associationClosed(IOException e) {
if (keep) {
this.disableReporting();
this.disconnect();
try {
if (retry <= 0) {
log.info("61850断线重连失败10次, 已断开不再重连. ied=" + ied.getName() + ", ip=" + ied.getIp() + ", port=" + ied.getPort());
if (realTimeDataService != null && ied != null) {
realTimeDataService.stopCollect(ied.getId());
}
} else {
if (retry >= 5) {
Thread.sleep(10 * 1000);
retry--;
this.reconnect();
} else if (retry == 4) {
log.info("61850断线重连失败6次, ied=" + ied.getName() + ", ip=" + ied.getIp() + ", port=" + ied.getPort());
Thread.sleep(60 * 1000);
this.reconnect();
} else if (retry == 3) {
Thread.sleep(3 * 60 * 1000);
this.reconnect();
} else if (retry == 2) {
Thread.sleep(5 * 60 * 1000);
this.reconnect();
} else if (retry == 1) {
Thread.sleep(10 * 60 * 1000);
this.reconnect();
}
}
} catch (Exception ignore) {
}
}
} }
public static void main(String[] args) { public static void main(String[] args) {
IecClient iecClient = new IecClient(); IecClient iecClient = new IecClient();
try { try {
iecClient.connect("192.168.1.17", 102, "1 3 9999 33", new FileInputStream("C:/资料/om.SCD")); IcdIed ied = IcdIed.builder()
.ip("192.168.1.17")
.port(102)
.apTitle("1 3 9999 33")
.build();
iecClient.connect(ied, new FileInputStream("C:/资料/om.SCD"));
String str = iecClient.getValue("OMDLMONT/SPDC1.MaxDsch.mag.f", "MX"); String str = iecClient.getValue("OMDLMONT/SPDC1.MaxDsch.mag.f", "MX");
System.out.println(str); System.out.println(str);
str = iecClient.getValue("OMDLMONT/SPDC1.MaxDsch.t", "MX"); str = iecClient.getValue("OMDLMONT/SPDC1.MaxDsch.t", "MX");

@ -58,8 +58,10 @@ public class RealTimeDataService {
IcdFile icdFile = optionalFile.get(); IcdFile icdFile = optionalFile.get();
try { try {
IecClient iecClient = new IecClient(); IecClient iecClient = new IecClient();
iecClient.connect(ied.getIp(), ied.getPort(), ied.getApTitle(), icdFile.getXml()); iecClient.connect(ied, icdFile.getXml());
iecClient.enableReporting(); iecClient.enableReporting();
iecClient.keep = true;
iecClient.realTimeDataService = this;
log.info("61850订阅成功, ied=" + ied.getName() + ", ip=" + ied.getIp() + ", port=" + ied.getPort()); log.info("61850订阅成功, ied=" + ied.getName() + ", ip=" + ied.getIp() + ", port=" + ied.getPort());
ied.setStart(Constants.TRUE); ied.setStart(Constants.TRUE);
iedRepository.save(ied); iedRepository.save(ied);

@ -100,7 +100,7 @@ public class IedDlConfigServiceImpl implements IedDlConfigService {
IcdFile icdFile = optionalFile.get(); IcdFile icdFile = optionalFile.get();
IecClient iecClient = new IecClient(); IecClient iecClient = new IecClient();
try { try {
iecClient.connect(ied.getIp(), ied.getPort(), ied.getApTitle(), icdFile.getXml()); iecClient.connect(ied, icdFile.getXml());
return iecClient.listFile(path); return iecClient.listFile(path);
} catch (Exception ex) { } catch (Exception ex) {
throw new BusinessException(ex.getMessage()); throw new BusinessException(ex.getMessage());

Loading…
Cancel
Save