feat: 把订阅到的数据更新到服务端上

dev
huangfeng 8 months ago
parent 2ceaadb1e2
commit 711d9616cd

@ -2,6 +2,7 @@ package com.xydl.cac.controller;
import com.xydl.cac.iec.IecServerService;
import com.xydl.cac.model.Response;
import com.xydl.cac.service.RealTimeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
@ -20,17 +21,21 @@ public class IecServerController extends BasicController {
@Resource
IecServerService iecServerService;
@Resource
RealTimeService realTimeService;
@GetMapping("start")
@ApiOperation("启动IEC服务端")
public Response<String> start() throws Exception {
iecServerService.startServer();
realTimeService.start();
return Response.success("OK");
}
@GetMapping("stop")
@ApiOperation("停止IEC服务端")
public Response<String> stop() throws Exception {
realTimeService.stop();
iecServerService.stop();
return Response.success("OK");
}

@ -1,6 +1,7 @@
package com.xydl.cac.iec;
import com.beanit.iec61850bean.*;
import com.xydl.cac.service.impl.RealTimeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
@ -19,6 +20,7 @@ public class IecClient implements ClientEventListener {
ClientSap clientSap = new ClientSap();
ClientAssociation clientAssociation = null;
ServerModel serverModel;
Urcb urcb = null;
public void connect(String host, int port, String apTitle, String xml) throws Exception {
InputStream in = IOUtils.toInputStream(xml, StandardCharsets.UTF_8);
@ -64,21 +66,25 @@ public class IecClient implements ClientEventListener {
public void enableReporting() throws Exception {
if (!CollectionUtils.isEmpty(serverModel.getUrcbs())) {
Optional<Urcb> optional = serverModel.getUrcbs().stream().findFirst();
Urcb urcb = optional.get();
Optional<Urcb> optional = serverModel.getUrcbs().stream().findAny();
urcb = optional.get();
clientAssociation.enableReporting(urcb);
}
}
public void disableReporting() {
if (urcb != null) {
try {
clientAssociation.disableReporting(urcb);
} catch (Exception ignore) {
}
}
}
@Override
public void newReport(Report report) {
if (report != null && report.getValues() != null) {
for (FcModelNode node : report.getValues()) {
if (node instanceof FcDataObject) {
String str = ((FcDataObject) node).toString();
log.info(str);
}
}
if (report != null) {
RealTimeServiceImpl.processReport(report);
}
}

@ -6,22 +6,26 @@ import org.apache.commons.io.IOUtils;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class IecServer extends Thread implements ServerEventListener {
public class IecServer implements ServerEventListener {
ServerSap serverSap = null;
ServerModel serversServerModel = null;
public void startServer(String xml, int port) throws Exception {
InputStream in = IOUtils.toInputStream(xml, StandardCharsets.UTF_8);
serverSap = new ServerSap(port, 0, null, SclParser.parse(in).get(0), null);
serverSap.setPort(port);
serverSap.startListening(this);
serversServerModel = serverSap.getModelCopy();
this.start();
log.info("已启动IEC61850服务端.");
public boolean started = false;
public void start(String xml, int port) throws Exception {
if (!started) {
InputStream in = IOUtils.toInputStream(xml, StandardCharsets.UTF_8);
serverSap = new ServerSap(port, 0, null, SclParser.parse(in).get(0), null);
serverSap.setPort(port);
serverSap.startListening(this);
serversServerModel = serverSap.getModelCopy();
started = true;
log.info("已启动IEC61850服务端在" + port + "端口");
}
}
public void close() {
@ -29,6 +33,15 @@ public class IecServer extends Thread implements ServerEventListener {
serverSap.stop();
log.info("已停止IEC61850服务端.");
}
started = false;
}
public void updateBda(BasicDataAttribute bda) {
BasicDataAttribute node = (BasicDataAttribute) serversServerModel.findModelNode(bda.getReference(), bda.getFc());
node.setValueFrom(bda);
List<BasicDataAttribute> bdas = new ArrayList<>();
bdas.add(node);
serverSap.setValues(bdas);
}
@Override

@ -2,7 +2,7 @@ package com.xydl.cac.iec;
import com.xydl.cac.entity.IcdFile;
import com.xydl.cac.repository.IcdFileRepository;
import com.xydl.cac.service.RealTimeService;
import com.xydl.cac.service.impl.RealTimeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
@ -18,42 +18,34 @@ public class IecServerService {
@Resource
IcdFileRepository fileRepository;
@Resource
RealTimeService realTimeService;
IecServer iecServer = new IecServer();
boolean started = false;
public void startServer() {
if (!started) {
List<IcdFile> icdFileList = fileRepository.findAll();
if (!CollectionUtils.isEmpty(icdFileList)) {
IcdFile icdFile = icdFileList.get(0);
try {
iecServer.startServer(icdFile.getXml(), 102);
started = true;
realTimeService.start();
} catch (Exception e) {
log.error("启动IEC61850服务端异常.", e);
}
List<IcdFile> icdFileList = fileRepository.findAll();
if (!CollectionUtils.isEmpty(icdFileList)) {
IcdFile icdFile = icdFileList.get(0);
try {
iecServer.start(icdFile.getXml(), 102);
RealTimeServiceImpl.iecServer = iecServer;
} catch (Exception e) {
log.error("启动IEC61850服务端异常.", e);
}
}
}
@PreDestroy
public void stop() {
realTimeService.stop();
RealTimeServiceImpl.iecServer = null;
if (iecServer != null) {
iecServer.close();
started = false;
}
}
public HashMap<String, Object> status() {
HashMap<String, Object> map = new HashMap<>();
map.put("port", 102);
map.put("started", started);
map.put("started", iecServer.started);
return map;
}
}

@ -1,8 +1,10 @@
package com.xydl.cac.service.impl;
import com.beanit.iec61850bean.*;
import com.xydl.cac.entity.IcdFile;
import com.xydl.cac.entity.IcdIed;
import com.xydl.cac.iec.IecClient;
import com.xydl.cac.iec.IecServer;
import com.xydl.cac.repository.*;
import com.xydl.cac.service.RealTimeService;
import lombok.extern.slf4j.Slf4j;
@ -25,6 +27,8 @@ public class RealTimeServiceImpl implements RealTimeService {
@Resource
IcdIedRepository iedRepository;
public static HashMap<String, String> dataMap = new HashMap<>();
public static IecServer iecServer = null;
HashMap<String, IecClient> clientMap = new HashMap<>();
@Override
@ -57,8 +61,50 @@ public class RealTimeServiceImpl implements RealTimeService {
while (it.hasNext()) {
String key = it.next();
IecClient iecClient = clientMap.get(key);
iecClient.disableReporting();
iecClient.disconnect();
clientMap.remove(key);
}
}
public static void processReport(Report report) {
if (!CollectionUtils.isEmpty(report.getValues())) {
for (FcModelNode node : report.getValues()) {
processNodeValue(node);
}
}
}
private static void processNodeValue(FcModelNode node) {
if (node instanceof FcDataObject) {
FcDataObject fcnode = (FcDataObject) node;
if (!CollectionUtils.isEmpty(fcnode.getChildren())) {
for (ModelNode child : fcnode.getChildren()) {
if (child instanceof BasicDataAttribute) {
BasicDataAttribute bda = (BasicDataAttribute) child;
processBdaNodeValue(bda);
} else if (child instanceof ConstructedDataAttribute) {
ConstructedDataAttribute cda = (ConstructedDataAttribute) child;
if (!CollectionUtils.isEmpty(cda.getChildren())) {
for (ModelNode cchild : cda.getChildren()) {
if (cchild instanceof BasicDataAttribute) {
BasicDataAttribute bda = (BasicDataAttribute) cchild;
processBdaNodeValue(bda);
}
}
}
}
}
}
}
}
private static void processBdaNodeValue(BasicDataAttribute bda) {
String ref = bda.getReference().toString();
String value = bda.getValueString();
dataMap.put(ref, value);
if (iecServer != null) {
iecServer.updateBda(bda);
}
}
}

Loading…
Cancel
Save