From f8fcabc730648922270975af978fd636cb546cd7 Mon Sep 17 00:00:00 2001 From: huangfeng Date: Thu, 31 Oct 2024 10:02:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A061850=E6=96=AD?= =?UTF-8?q?=E7=BA=BF=E9=87=8D=E8=BF=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xydl/cac/iec/IEDCollectService.java | 2 +- src/main/java/com/xydl/cac/iec/IecClient.java | 70 ++++++++++++++++--- .../com/xydl/cac/iec/RealTimeDataService.java | 4 +- .../service/impl/IedDlConfigServiceImpl.java | 2 +- 4 files changed, 65 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/xydl/cac/iec/IEDCollectService.java b/src/main/java/com/xydl/cac/iec/IEDCollectService.java index 9a87f66..6b4e18e 100644 --- a/src/main/java/com/xydl/cac/iec/IEDCollectService.java +++ b/src/main/java/com/xydl/cac/iec/IEDCollectService.java @@ -53,7 +53,7 @@ public class IEDCollectService { } public void connect() throws Exception { - iecClient.connect(ied.getIp(), ied.getPort(), ied.getApTitle(), xml); + iecClient.connect(ied, xml); } public void disconnect() { diff --git a/src/main/java/com/xydl/cac/iec/IecClient.java b/src/main/java/com/xydl/cac/iec/IecClient.java index 99f8b21..a0e3c75 100644 --- a/src/main/java/com/xydl/cac/iec/IecClient.java +++ b/src/main/java/com/xydl/cac/iec/IecClient.java @@ -1,6 +1,7 @@ package com.xydl.cac.iec; import com.beanit.iec61850bean.*; +import com.xydl.cac.entity.IcdIed; import com.xydl.cac.entity.constants.Constants; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.IOUtils; @@ -17,19 +18,26 @@ import java.util.List; @Slf4j public class IecClient implements ClientEventListener { + IcdIed ied; + int[] title; ClientSap clientSap = new ClientSap(); ClientAssociation clientAssociation = null; 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); - this.connect(host, port, apTitle, in); + this.connect(_ied, in); } - public void connect(String host, int port, String apTitle, InputStream in) throws Exception { - int[] title = new int[]{1, 3, 9999, 33}; - if (StringUtils.isNotBlank(apTitle)) { - String[] strs = apTitle.replaceAll(" ", ",").split(","); + public void connect(IcdIed _ied, InputStream in) throws Exception { + ied = _ied; + title = new int[]{1, 3, 9999, 33}; + if (StringUtils.isNotBlank(_ied.getApTitle())) { + String[] strs = _ied.getApTitle().replaceAll(" ", ",").split(","); if (strs.length == 4) { title[0] = Integer.parseInt(strs[0]); title[1] = Integer.parseInt(strs[1]); @@ -47,9 +55,9 @@ public class IecClient implements ClientEventListener { clientSap.setTSelRemote(new byte[]{0, 1}); clientSap.setTSelLocal(new byte[]{0, 0}); clientSap.setApTitleCalled(title); - - clientAssociation = clientSap.associate(InetAddress.getByName(host), port, null, this); serverModel = SclParser.parse(in).get(0); + + clientAssociation = clientSap.associate(InetAddress.getByName(ied.getIp()), ied.getPort(), null, this); 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 { FcModelNode node = (FcModelNode) serverModel.findModelNode(paramindex, Fc.valueOf(fc)); clientAssociation.getDataValues(node); @@ -124,13 +138,49 @@ public class IecClient implements ClientEventListener { @Override 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) { IecClient iecClient = new IecClient(); 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"); System.out.println(str); str = iecClient.getValue("OMDLMONT/SPDC1.MaxDsch.t", "MX"); diff --git a/src/main/java/com/xydl/cac/iec/RealTimeDataService.java b/src/main/java/com/xydl/cac/iec/RealTimeDataService.java index b546ce4..4423e3d 100644 --- a/src/main/java/com/xydl/cac/iec/RealTimeDataService.java +++ b/src/main/java/com/xydl/cac/iec/RealTimeDataService.java @@ -58,8 +58,10 @@ public class RealTimeDataService { IcdFile icdFile = optionalFile.get(); try { IecClient iecClient = new IecClient(); - iecClient.connect(ied.getIp(), ied.getPort(), ied.getApTitle(), icdFile.getXml()); + iecClient.connect(ied, icdFile.getXml()); iecClient.enableReporting(); + iecClient.keep = true; + iecClient.realTimeDataService = this; log.info("61850订阅成功, ied=" + ied.getName() + ", ip=" + ied.getIp() + ", port=" + ied.getPort()); ied.setStart(Constants.TRUE); iedRepository.save(ied); diff --git a/src/main/java/com/xydl/cac/service/impl/IedDlConfigServiceImpl.java b/src/main/java/com/xydl/cac/service/impl/IedDlConfigServiceImpl.java index 2dda516..0a80e7e 100644 --- a/src/main/java/com/xydl/cac/service/impl/IedDlConfigServiceImpl.java +++ b/src/main/java/com/xydl/cac/service/impl/IedDlConfigServiceImpl.java @@ -100,7 +100,7 @@ public class IedDlConfigServiceImpl implements IedDlConfigService { IcdFile icdFile = optionalFile.get(); IecClient iecClient = new IecClient(); try { - iecClient.connect(ied.getIp(), ied.getPort(), ied.getApTitle(), icdFile.getXml()); + iecClient.connect(ied, icdFile.getXml()); return iecClient.listFile(path); } catch (Exception ex) { throw new BusinessException(ex.getMessage());