feat: 61850服务端可启动多个

dev
huangfeng 8 months ago
parent 6e3b1b3495
commit 299d781187

@ -62,24 +62,33 @@ public class IecServerController extends BasicController {
@PostMapping("start") @PostMapping("start")
@ApiOperation("启动IEC服务端") @ApiOperation("启动IEC服务端")
public Response<String> start(@Validated @NotNull(message = "fileId不能为空!") Integer fileId) throws Exception { public Response<String> start(Integer fileId, Integer port) throws Exception {
iecServerService.startServer(fileId); if (fileId == null) {
throw new BusinessException("fileId不能为空!");
}
if (port == null) {
throw new BusinessException("port不能为空!");
}
iecServerService.startServer(fileId, port);
return Response.success("OK"); return Response.success("OK");
} }
@PostMapping("stop") @PostMapping("stop")
@ApiOperation("停止IEC服务端") @ApiOperation("停止IEC服务端")
public Response<String> stop() throws Exception { public Response<String> stop(Integer fileId) throws Exception {
iecServerService.stopServer(); if (fileId == null) {
throw new BusinessException("fileId不能为空!");
}
iecServerService.stopServer(fileId);
return Response.success("OK"); return Response.success("OK");
} }
@GetMapping("status") // @GetMapping("status")
@ApiOperation("查看IEC服务端状态") // @ApiOperation("查看IEC服务端状态")
public Response<HashMap<String, Object>> status() throws Exception { // public Response<HashMap<String, Object>> status() throws Exception {
HashMap<String, Object> map = iecServerService.status(); // HashMap<String, Object> map = iecServerService.status();
return Response.success(map); // return Response.success(map);
} // }
@GetMapping("listParamindex") @GetMapping("listParamindex")
@ApiOperation("查询该IED下的paramindex数据") @ApiOperation("查询该IED下的paramindex数据")

@ -46,6 +46,10 @@ public class IcdFile {
@Column(name = "start") @Column(name = "start")
private Integer start; private Integer start;
@ApiModelProperty(name = "端口")
@Column(name = "port")
private Integer port;
@Transient @Transient
private List<IcdConfigType> configList; private List<IcdConfigType> configList;
@Transient @Transient

@ -19,7 +19,7 @@ public class IecServer implements ServerEventListener {
ServerModel serversServerModel = null; ServerModel serversServerModel = null;
public boolean started = false; public boolean started = false;
public String filename = null; public String filename = null;
public Integer fileId = null; public Integer port = null;
IcdTransformService _transformService; IcdTransformService _transformService;
public IecServer(IcdTransformService transformService) { public IecServer(IcdTransformService transformService) {
@ -35,6 +35,7 @@ public class IecServer implements ServerEventListener {
serverSap.startListening(this); serverSap.startListening(this);
serversServerModel = serverSap.getModelCopy(); serversServerModel = serverSap.getModelCopy();
started = true; started = true;
this.port = port;
log.info("已启动IEC61850服务端在" + port + "端口"); log.info("已启动IEC61850服务端在" + port + "端口");
} }
} }

@ -24,22 +24,20 @@ public class IecServerService {
@Resource @Resource
IcdTransformService transformService; IcdTransformService transformService;
IecServer iecServer = null;
@PostConstruct @PostConstruct
private void init() { private void init() {
iecServer = new IecServer(transformService);
List<IcdFile> list = fileRepository.findBySrvAndStart(Constants.Server, Constants.TRUE); List<IcdFile> list = fileRepository.findBySrvAndStart(Constants.Server, Constants.TRUE);
if (!CollectionUtils.isEmpty(list)) { if (!CollectionUtils.isEmpty(list)) {
IcdFile icdFile = list.get(0); for (IcdFile icdFile : list) {
try { try {
this.startServer(icdFile.getId()); this.startServer(icdFile.getId(), icdFile.getPort());
} catch (Exception ignore) { } catch (Exception ignore) {
}
} }
} }
} }
public void startServer(Integer fileId) throws BusinessException { public void startServer(Integer fileId, int port) throws BusinessException {
Optional<IcdFile> optional = fileRepository.findById(fileId); Optional<IcdFile> optional = fileRepository.findById(fileId);
if (!optional.isPresent()) { if (!optional.isPresent()) {
throw new BusinessException("未找到该文件"); throw new BusinessException("未找到该文件");
@ -49,44 +47,57 @@ public class IecServerService {
throw new BusinessException("该文件不是服务端文件"); throw new BusinessException("该文件不是服务端文件");
} }
try { try {
iecServer.start(icdFile.getXml(), 102); IecServer iecServer = new IecServer(transformService);
iecServer.start(icdFile.getXml(), port);
iecServer.filename = icdFile.getFilename(); iecServer.filename = icdFile.getFilename();
iecServer.fileId = icdFile.getId(); RealTimeDataService.iecServerMap.put(fileId, iecServer);
RealTimeDataService.iecServer = iecServer;
icdFile.setStart(Constants.TRUE); icdFile.setStart(Constants.TRUE);
icdFile.setPort(port);
fileRepository.save(icdFile); fileRepository.save(icdFile);
} catch (Exception e) { } catch (Exception e) {
icdFile.setStart(Constants.FALSE);
fileRepository.save(icdFile);
log.error("启动IEC61850服务端异常.", e); log.error("启动IEC61850服务端异常.", e);
throw new BusinessException(e.getMessage());
} }
} }
public void stopServer() { public void stopServer(Integer fileId) {
this.stop(); this.onlyStopServer(fileId);
if (iecServer.fileId != null) { Optional<IcdFile> optional = fileRepository.findById(fileId);
Optional<IcdFile> optional = fileRepository.findById(iecServer.fileId); if (optional.isPresent()) {
if (optional.isPresent()) { IcdFile icdFile = optional.get();
IcdFile icdFile = optional.get(); icdFile.setStart(Constants.FALSE);
icdFile.setStart(Constants.FALSE); fileRepository.save(icdFile);
fileRepository.save(icdFile); }
} }
private void onlyStopServer(Integer fileId) {
IecServer iecServer = RealTimeDataService.iecServerMap.get(fileId);
if (iecServer != null) {
log.info("关闭IecServer服务, port=" + iecServer.port);
iecServer.close();
RealTimeDataService.iecServerMap.remove(fileId);
} }
} }
@PreDestroy @PreDestroy
private void stop() { private void stop() {
log.info("关闭IecServer服务."); Iterator<Integer> it = RealTimeDataService.iecServerMap.keySet().iterator();
RealTimeDataService.iecServer = null; while (it.hasNext()) {
iecServer.close(); Integer key = it.next();
this.onlyStopServer(key);
}
} }
public HashMap<String, Object> status() { // public HashMap<String, Object> status() {
HashMap<String, Object> map = new HashMap<>(); // HashMap<String, Object> map = new HashMap<>();
map.put("port", 102); // map.put("port", 102);
map.put("started", iecServer.started); // map.put("started", iecServer.started);
map.put("filename", iecServer.filename); // map.put("filename", iecServer.filename);
map.put("fileId", iecServer.fileId); // map.put("fileId", iecServer.fileId);
return map; // return map;
} // }
public List<IcdConfigTypeInst> listParamindex(Integer fileId) throws Exception { public List<IcdConfigTypeInst> listParamindex(Integer fileId) throws Exception {
Optional<IcdFile> optional = fileRepository.findById(fileId); Optional<IcdFile> optional = fileRepository.findById(fileId);

@ -27,7 +27,7 @@ public class RealTimeDataService {
@Resource @Resource
IcdIedRepository iedRepository; IcdIedRepository iedRepository;
public static IecServer iecServer = null; public static HashMap<Integer, IecServer> iecServerMap = new HashMap<>();
public static boolean inDoing = false; public static boolean inDoing = false;
HashMap<Integer, IecClient> clientMap = new HashMap<>(); HashMap<Integer, IecClient> clientMap = new HashMap<>();
@ -65,6 +65,8 @@ public class RealTimeDataService {
iedRepository.save(ied); iedRepository.save(ied);
clientMap.put(ied.getId(), iecClient); clientMap.put(ied.getId(), iecClient);
} catch (Exception ex) { } catch (Exception ex) {
ied.setStart(Constants.FALSE);
iedRepository.save(ied);
String err = "61850订阅异常, ied=" + ied.getName() + ", ip=" + ied.getIp(); String err = "61850订阅异常, ied=" + ied.getName() + ", ip=" + ied.getIp();
log.error(err, ex); log.error(err, ex);
throw new BusinessException(err); throw new BusinessException(err);
@ -135,7 +137,10 @@ public class RealTimeDataService {
} }
private static void processBdaNodeValue(BasicDataAttribute bda) { private static void processBdaNodeValue(BasicDataAttribute bda) {
if (iecServer != null) { Iterator<Integer> it = RealTimeDataService.iecServerMap.keySet().iterator();
while (it.hasNext()) {
Integer key = it.next();
IecServer iecServer = RealTimeDataService.iecServerMap.get(key);
iecServer.updateBda(bda); iecServer.updateBda(bda);
} }
IEDCollectService.updateLastData(bda); IEDCollectService.updateLastData(bda);

Loading…
Cancel
Save