From eb04d0ce899b3029c44f00c269aa110fdffa6383 Mon Sep 17 00:00:00 2001 From: huangfeng Date: Mon, 14 Oct 2024 11:20:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8B=86=E5=88=86=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=92=8C=E9=87=87=E9=9B=86=EF=BC=8C=E5=BC=80=E6=9C=BA=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=90=AF=E5=8A=A8=E6=9C=8D=E5=8A=A1=E5=92=8C=E9=87=87?= =?UTF-8?q?=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cac/controller/IcdConfigController.java | 24 ++++++ .../cac/controller/IecServerController.java | 15 +--- src/main/java/com/xydl/cac/iec/IecServer.java | 1 - .../com/xydl/cac/iec/IecServerService.java | 35 ++++++++- .../com/xydl/cac/iec/RealTimeDataService.java | 78 +++++++++++++------ .../xydl/cac/repository/IcdIedRepository.java | 2 + 6 files changed, 114 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/xydl/cac/controller/IcdConfigController.java b/src/main/java/com/xydl/cac/controller/IcdConfigController.java index 4cd1d59..81e8e5c 100644 --- a/src/main/java/com/xydl/cac/controller/IcdConfigController.java +++ b/src/main/java/com/xydl/cac/controller/IcdConfigController.java @@ -4,6 +4,7 @@ import com.xydl.cac.entity.IcdConfigType; import com.xydl.cac.entity.IcdFile; import com.xydl.cac.entity.IcdIed; import com.xydl.cac.exception.BusinessException; +import com.xydl.cac.iec.RealTimeDataService; import com.xydl.cac.model.ColumnModel; import com.xydl.cac.model.IcdAttUpdateModel; import com.xydl.cac.model.Response; @@ -32,6 +33,8 @@ public class IcdConfigController extends BasicController { IcdFileConfigService configService; @Resource DataService dataService; + @Resource + RealTimeDataService realTimeDataService; @PostMapping("upload") @ApiOperation("上传客户端icd文件") @@ -139,4 +142,25 @@ public class IcdConfigController extends BasicController { return Response.success("OK"); } + + @PostMapping("startCollect") + @ApiOperation("开始实时采集转发") + public Response startCollect(@Validated @NotNull(message = "iedId不能为空!") Integer iedId) throws Exception { + if (RealTimeDataService.inDoing) { + throw new BusinessException("请稍后再操作"); + } + realTimeDataService.startCollect(iedId); + return Response.success("OK"); + } + + @PostMapping("stopCollect") + @ApiOperation("停止实时采集转发") + public Response stopCollect(@Validated @NotNull(message = "iedId不能为空!") Integer iedId) throws Exception { + if (RealTimeDataService.inDoing) { + throw new BusinessException("请稍后再操作"); + } + realTimeDataService.stopCollect(iedId); + return Response.success("OK"); + } + } diff --git a/src/main/java/com/xydl/cac/controller/IecServerController.java b/src/main/java/com/xydl/cac/controller/IecServerController.java index ed34bf0..98dcf8f 100644 --- a/src/main/java/com/xydl/cac/controller/IecServerController.java +++ b/src/main/java/com/xydl/cac/controller/IecServerController.java @@ -1,9 +1,7 @@ package com.xydl.cac.controller; import com.xydl.cac.entity.IcdFile; -import com.xydl.cac.exception.BusinessException; import com.xydl.cac.iec.IecServerService; -import com.xydl.cac.iec.RealTimeDataService; import com.xydl.cac.model.Response; import com.xydl.cac.service.IcdFileConfigService; import io.swagger.annotations.Api; @@ -30,8 +28,6 @@ public class IecServerController extends BasicController { IcdFileConfigService configService; @Resource IecServerService iecServerService; - @Resource - RealTimeDataService realTimeDataService; @PostMapping("upload") @ApiOperation("上传服务端icd文件") @@ -59,22 +55,14 @@ public class IecServerController extends BasicController { @PostMapping("start") @ApiOperation("启动IEC服务端") public Response start(@Validated @NotNull(message = "fileId不能为空!") Integer fileId) throws Exception { - if (RealTimeDataService.inDoing) { - throw new BusinessException("请稍后再操作"); - } iecServerService.startServer(fileId); - realTimeDataService.start(); return Response.success("OK"); } @PostMapping("stop") @ApiOperation("停止IEC服务端") public Response stop() throws Exception { - if (RealTimeDataService.inDoing) { - throw new BusinessException("请稍后再操作"); - } - realTimeDataService.stop(); - iecServerService.stop(); + iecServerService.stopServer(); return Response.success("OK"); } @@ -84,4 +72,5 @@ public class IecServerController extends BasicController { HashMap map = iecServerService.status(); return Response.success(map); } + } diff --git a/src/main/java/com/xydl/cac/iec/IecServer.java b/src/main/java/com/xydl/cac/iec/IecServer.java index 60cb232..cb28648 100644 --- a/src/main/java/com/xydl/cac/iec/IecServer.java +++ b/src/main/java/com/xydl/cac/iec/IecServer.java @@ -36,7 +36,6 @@ public class IecServer implements ServerEventListener { log.info("已停止IEC61850服务端."); } started = false; - filename = null; } public void updateBda(BasicDataAttribute bda) { diff --git a/src/main/java/com/xydl/cac/iec/IecServerService.java b/src/main/java/com/xydl/cac/iec/IecServerService.java index 944efd3..c557b15 100644 --- a/src/main/java/com/xydl/cac/iec/IecServerService.java +++ b/src/main/java/com/xydl/cac/iec/IecServerService.java @@ -5,10 +5,13 @@ import com.xydl.cac.exception.BusinessException; import com.xydl.cac.repository.IcdFileRepository; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; +import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; import java.util.HashMap; +import java.util.List; import java.util.Optional; @Service @@ -20,6 +23,18 @@ public class IecServerService { IecServer iecServer = new IecServer(); + @PostConstruct + private void init() { + List list = fileRepository.findBySrvAndStart(1, 1); + if (!CollectionUtils.isEmpty(list)) { + IcdFile icdFile = list.get(0); + try { + this.startServer(icdFile.getId()); + } catch (Exception ignore) { + } + } + } + public void startServer(Integer fileId) throws BusinessException { Optional optional = fileRepository.findById(fileId); if (!optional.isPresent()) { @@ -34,17 +49,29 @@ public class IecServerService { iecServer.filename = icdFile.getFilename(); iecServer.fileId = icdFile.getId(); RealTimeDataService.iecServer = iecServer; + icdFile.setStart(1); + fileRepository.save(icdFile); } catch (Exception e) { log.error("启动IEC61850服务端异常.", e); } } + public void stopServer() { + this.stop(); + if (iecServer.fileId != null) { + Optional optional = fileRepository.findById(iecServer.fileId); + if (optional.isPresent()) { + IcdFile icdFile = optional.get(); + icdFile.setStart(0); + fileRepository.save(icdFile); + } + } + } + @PreDestroy - public void stop() { + private void stop() { RealTimeDataService.iecServer = null; - if (iecServer != null) { - iecServer.close(); - } + iecServer.close(); } public HashMap status() { diff --git a/src/main/java/com/xydl/cac/iec/RealTimeDataService.java b/src/main/java/com/xydl/cac/iec/RealTimeDataService.java index e355a15..f8f116b 100644 --- a/src/main/java/com/xydl/cac/iec/RealTimeDataService.java +++ b/src/main/java/com/xydl/cac/iec/RealTimeDataService.java @@ -3,17 +3,20 @@ package com.xydl.cac.iec; import com.beanit.iec61850bean.*; import com.xydl.cac.entity.IcdFile; import com.xydl.cac.entity.IcdIed; +import com.xydl.cac.exception.BusinessException; import com.xydl.cac.repository.IcdFileRepository; import com.xydl.cac.repository.IcdIedRepository; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; +import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Optional; @Service @Slf4j @@ -26,38 +29,67 @@ public class RealTimeDataService { public static HashMap dataMap = new HashMap<>(); public static IecServer iecServer = null; public static boolean inDoing = false; - HashMap clientMap = new HashMap<>(); + HashMap clientMap = new HashMap<>(); - public void start() { - inDoing = true; - List icdFileList = fileRepository.findAll(); - if (CollectionUtils.isEmpty(icdFileList)) { - return; - } - for (IcdFile icdFile : icdFileList) { - List iedList = iedRepository.findByIcdFileId(icdFile.getId()); - if (!CollectionUtils.isEmpty(iedList)) { - for (IcdIed ied : iedList) { - try { - IecClient iecClient = new IecClient(); - iecClient.connect(ied.getIp(), 102, ied.getApTitle(), icdFile.getXml()); - iecClient.enableReporting(); - log.info("61850订阅成功, ied=" + ied.getName() + ", ip=" + ied.getIp()); - clientMap.put(ied.getIp(), iecClient); - } catch (Exception ex) { - log.error("61850订阅异常, ied=" + ied.getName() + ", ip=" + ied.getIp(), ex); - } + @PostConstruct + private void init() { + List list = iedRepository.findByStart(1); + if (!CollectionUtils.isEmpty(list)) { + for (IcdIed ied : list) { + try { + this.startCollect(ied.getId()); + } catch (Exception ignore) { } } } + } + + public void startCollect(Integer iedId) throws BusinessException { + inDoing = true; + Optional optional = iedRepository.findById(iedId); + if (!optional.isPresent()) { + throw new BusinessException("未找到该IED"); + } + IcdIed ied = optional.get(); + Optional optionalFile = fileRepository.findById(ied.getIcdFileId()); + if (!optionalFile.isPresent()) { + throw new BusinessException("未找到该icd文件"); + } + IcdFile icdFile = optionalFile.get(); + try { + IecClient iecClient = new IecClient(); + iecClient.connect(ied.getIp(), 102, ied.getApTitle(), icdFile.getXml()); + iecClient.enableReporting(); + log.info("61850订阅成功, ied=" + ied.getName() + ", ip=" + ied.getIp()); + ied.setStart(1); + iedRepository.save(ied); + clientMap.put(ied.getId(), iecClient); + } catch (Exception ex) { + log.error("61850订阅异常, ied=" + ied.getName() + ", ip=" + ied.getIp(), ex); + } inDoing = false; } + public void stopCollect(Integer iedId) { + IecClient iecClient = clientMap.get(iedId); + if (iecClient != null) { + iecClient.disableReporting(); + iecClient.disconnect(); + clientMap.remove(iedId); + } + Optional optional = iedRepository.findById(iedId); + if (optional.isPresent()) { + IcdIed ied = optional.get(); + ied.setStart(0); + iedRepository.save(ied); + } + } + @PreDestroy - public void stop() { - Iterator it = clientMap.keySet().iterator(); + private void stop() { + Iterator it = clientMap.keySet().iterator(); while (it.hasNext()) { - String key = it.next(); + Integer key = it.next(); IecClient iecClient = clientMap.get(key); iecClient.disableReporting(); iecClient.disconnect(); diff --git a/src/main/java/com/xydl/cac/repository/IcdIedRepository.java b/src/main/java/com/xydl/cac/repository/IcdIedRepository.java index 910abab..01a9448 100644 --- a/src/main/java/com/xydl/cac/repository/IcdIedRepository.java +++ b/src/main/java/com/xydl/cac/repository/IcdIedRepository.java @@ -12,4 +12,6 @@ import java.util.List; public interface IcdIedRepository extends JpaRepository, JpaSpecificationExecutor { List findByIcdFileId(Integer fileId); + + List findByStart(Integer start); } \ No newline at end of file