feat: 61850服务端可启动多个

dev
huangfeng 8 months ago
parent 6e3b1b3495
commit 299d781187

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

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

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

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

@ -27,7 +27,7 @@ public class RealTimeDataService {
@Resource
IcdIedRepository iedRepository;
public static IecServer iecServer = null;
public static HashMap<Integer, IecServer> iecServerMap = new HashMap<>();
public static boolean inDoing = false;
HashMap<Integer, IecClient> clientMap = new HashMap<>();
@ -65,6 +65,8 @@ public class RealTimeDataService {
iedRepository.save(ied);
clientMap.put(ied.getId(), iecClient);
} catch (Exception ex) {
ied.setStart(Constants.FALSE);
iedRepository.save(ied);
String err = "61850订阅异常, ied=" + ied.getName() + ", ip=" + ied.getIp();
log.error(err, ex);
throw new BusinessException(err);
@ -135,7 +137,10 @@ public class RealTimeDataService {
}
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);
}
IEDCollectService.updateLastData(bda);

Loading…
Cancel
Save