Merge branch 'iec61850' of http://61.169.135.146:8081/git/cac/backend into iec61850

 Conflicts:
	src/main/java/com/xydl/cac/model/StaticVariable.java
main
liuguijing 6 months ago
commit 2d5491876b

@ -0,0 +1 @@
ALTER TABLE `warn_rule` ADD COLUMN `notify_com` INT(11) NULL DEFAULT NULL COMMENT '0:不通知; 1:通知' AFTER `active`;

@ -10,6 +10,7 @@ import com.xydl.cac.iec.RealTimeDataService;
import com.xydl.cac.model.ColumnModel; import com.xydl.cac.model.ColumnModel;
import com.xydl.cac.model.IcdAttUpdateModel; import com.xydl.cac.model.IcdAttUpdateModel;
import com.xydl.cac.model.Response; import com.xydl.cac.model.Response;
import com.xydl.cac.repository.IcdIedRepository;
import com.xydl.cac.service.DataService; import com.xydl.cac.service.DataService;
import com.xydl.cac.service.IcdFileConfigService; import com.xydl.cac.service.IcdFileConfigService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
@ -25,6 +26,7 @@ import javax.annotation.Resource;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.List; import java.util.List;
import java.util.Optional;
@RestController @RestController
@Api(tags = {"IcdConfig相关接口"}) @Api(tags = {"IcdConfig相关接口"})
@ -38,6 +40,8 @@ public class IcdConfigController extends BasicController {
DataService dataService; DataService dataService;
@Resource @Resource
RealTimeDataService realTimeDataService; RealTimeDataService realTimeDataService;
@Resource
IcdIedRepository iedRepository;
@PostMapping("upload") @PostMapping("upload")
@ApiOperation("上传客户端icd文件") @ApiOperation("上传客户端icd文件")
@ -161,8 +165,22 @@ public class IcdConfigController extends BasicController {
if (RealTimeDataService.inDoing) { if (RealTimeDataService.inDoing) {
throw new BusinessException("请稍后再操作"); throw new BusinessException("请稍后再操作");
} }
realTimeDataService.startCollect(iedId); Optional<IcdIed> optional = iedRepository.findById(iedId);
return Response.success("OK"); if (!optional.isPresent()) {
throw new BusinessException("未找到该IED");
}
IcdIed ied = optional.get();
try {
realTimeDataService.startCollect(ied);
ied.setStart(Constants.TRUE);
iedRepository.save(ied);
return Response.success("OK");
} catch (Exception ex) {
realTimeDataService.stopCollect(ied.getId());
ied.setStart(Constants.FALSE);
iedRepository.save(ied);
return Response.fail(ex.getMessage());
}
} }
@PostMapping("stopCollect") @PostMapping("stopCollect")
@ -174,7 +192,14 @@ public class IcdConfigController extends BasicController {
if (RealTimeDataService.inDoing) { if (RealTimeDataService.inDoing) {
throw new BusinessException("请稍后再操作"); throw new BusinessException("请稍后再操作");
} }
realTimeDataService.stopCollect(iedId); Optional<IcdIed> optional = iedRepository.findById(iedId);
if (!optional.isPresent()) {
throw new BusinessException("未找到该IED");
}
IcdIed ied = optional.get();
ied.setStart(Constants.FALSE);
iedRepository.save(ied);
realTimeDataService.stopCollect(ied.getId());
return Response.success("OK"); return Response.success("OK");
} }

@ -46,4 +46,11 @@ public class IcdIed {
@ApiModelProperty(name = "端口") @ApiModelProperty(name = "端口")
@Column(name = "port") @Column(name = "port")
private Integer port; private Integer port;
@Transient
private boolean connected;
@Transient
private int retry;
@Transient
private int seconds;
} }

@ -77,6 +77,10 @@ public class WarnRule {
@Column(name = "active") @Column(name = "active")
private Integer active; private Integer active;
@ApiModelProperty("状态 0:不通知 1:通知")
@Column(name = "notify_com")
private Integer notifyCom;
@ApiModelProperty("最后数据采集时间") @ApiModelProperty("最后数据采集时间")
@Column(name = "last_d_time") @Column(name = "last_d_time")
private Date lastDTime; private Date lastDTime;

@ -15,6 +15,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.net.SocketException;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -55,7 +56,8 @@ public class IEDCollectService {
} }
public void connect() throws Exception { public void connect() throws Exception {
iecClient.connect(ied, xml); iecClient.init(ied, xml);
iecClient.connect();
} }
public void disconnect() { public void disconnect() {
@ -87,7 +89,11 @@ public class IEDCollectService {
StaticVariable.doneWarnMap.put(key, "1"); StaticVariable.doneWarnMap.put(key, "1");
_webSocketServer.sendMessage(err, null); _webSocketServer.sendMessage(err, null);
} }
RealTimeDataService.onErrorCheck(ied.getId()); if (ex instanceof SocketException) {
NetErrorThread thread = new NetErrorThread();
thread.iedId = ied.getId();
thread.run();
}
} finally { } finally {
iecClient.disconnect(); iecClient.disconnect();
} }

@ -29,9 +29,12 @@ public class IecClient implements ClientEventListener {
ServerModel serverModel; ServerModel serverModel;
public boolean keep = false; public boolean keep = false;
public boolean connected = false;
private RealTimeDataService realTimeDataService; private RealTimeDataService realTimeDataService;
private WebSocketServer webSocketServer; private WebSocketServer webSocketServer;
public int retry = 10; public int retry = 0;
public int seconds = 0;
private boolean inRetry = false;
public Date lastReportTime; public Date lastReportTime;
public IecClient() { public IecClient() {
@ -42,16 +45,20 @@ public class IecClient implements ClientEventListener {
webSocketServer = _webSocketServer; webSocketServer = _webSocketServer;
} }
public void connect(IcdIed _ied, String xml) throws Exception { public void init(IcdIed _ied, String xml) throws Exception {
InputStream in = IOUtils.toInputStream(xml, StandardCharsets.UTF_8); InputStream in = IOUtils.toInputStream(xml, StandardCharsets.UTF_8);
this.connect(_ied, in); this.init(_ied, in);
} }
public void connect(IcdIed _ied, InputStream in) throws Exception { public void init(IcdIed _ied, InputStream in) throws Exception {
ied = _ied; ied = _ied;
serverModel = SclParser.parse(in).get(0);
}
public void connect() throws Exception {
title = new int[]{1, 3, 9999, 33}; title = new int[]{1, 3, 9999, 33};
if (StringUtils.isNotBlank(_ied.getApTitle())) { if (StringUtils.isNotBlank(ied.getApTitle())) {
String[] strs = _ied.getApTitle().replaceAll(" ", ",").split(","); String[] strs = ied.getApTitle().replaceAll(" ", ",").split(",");
if (strs.length == 4) { if (strs.length == 4) {
title[0] = Integer.parseInt(strs[0]); title[0] = Integer.parseInt(strs[0]);
title[1] = Integer.parseInt(strs[1]); title[1] = Integer.parseInt(strs[1]);
@ -69,14 +76,15 @@ public class IecClient implements ClientEventListener {
clientSap.setTSelRemote(new byte[]{0, 1}); clientSap.setTSelRemote(new byte[]{0, 1});
clientSap.setTSelLocal(new byte[]{0, 0}); clientSap.setTSelLocal(new byte[]{0, 0});
clientSap.setApTitleCalled(title); clientSap.setApTitleCalled(title);
serverModel = SclParser.parse(in).get(0);
clientAssociation = clientSap.associate(InetAddress.getByName(ied.getIp()), ied.getPort(), null, this); clientAssociation = clientSap.associate(InetAddress.getByName(ied.getIp()), ied.getPort(), null, this);
clientAssociation.setServerModel(serverModel); clientAssociation.setServerModel(serverModel);
connected = true;
} }
public void disconnect() { public void disconnect() {
try { try {
connected = false;
clientAssociation.disconnect(); clientAssociation.disconnect();
clientAssociation = null; clientAssociation = null;
} catch (Exception ignore) { } catch (Exception ignore) {
@ -87,11 +95,13 @@ public class IecClient implements ClientEventListener {
if (StaticVariable.shutdown == 1) { if (StaticVariable.shutdown == 1) {
return; return;
} }
retry++;
seconds = 0;
clientAssociation = clientSap.associate(InetAddress.getByName(ied.getIp()), ied.getPort(), null, this); clientAssociation = clientSap.associate(InetAddress.getByName(ied.getIp()), ied.getPort(), null, this);
clientAssociation.setServerModel(serverModel); clientAssociation.setServerModel(serverModel);
log.info("61850订阅断线重连成功, ied=" + ied.getName() + ", ip=" + ied.getIp() + ", port=" + ied.getPort()); log.info("61850订阅断线重连成功, ied=" + ied.getName() + ", ip=" + ied.getIp() + ", port=" + ied.getPort());
retry = 10; connected = true;
keep = true; retry = 0;
} }
public String getValue(String paramindex, String fc) throws Exception { public String getValue(String paramindex, String fc) throws Exception {
@ -126,7 +136,6 @@ public class IecClient implements ClientEventListener {
} }
} }
log.info("61850订阅成功, ied=" + ied.getName() + ", ip=" + ied.getIp() + ", port=" + ied.getPort()); log.info("61850订阅成功, ied=" + ied.getName() + ", ip=" + ied.getIp() + ", port=" + ied.getPort());
keep = true;
} }
public void disableReporting() { public void disableReporting() {
@ -151,7 +160,6 @@ public class IecClient implements ClientEventListener {
} else { } else {
log.info("61850停止订阅, ied=" + ied.getName() + ", ip=" + ied.getIp() + ", port=" + ied.getPort()); log.info("61850停止订阅, ied=" + ied.getName() + ", ip=" + ied.getIp() + ", port=" + ied.getPort());
} }
keep = false;
} }
public List<FileInformation> listFile(String path) throws Exception { public List<FileInformation> listFile(String path) throws Exception {
@ -177,36 +185,31 @@ public class IecClient implements ClientEventListener {
@Override @Override
public void associationClosed(IOException e) { public void associationClosed(IOException e) {
if (keep) { if (keep && !inRetry) {
inRetry = true;
retry = 0;
seconds = 0;
this.disableReporting(); this.disableReporting();
this.disconnect(); this.disconnect();
while (retry > 0 && StaticVariable.shutdown == 0) { while (!connected && StaticVariable.shutdown == 0) {
retry--;
try { try {
if (retry >= 5) { Thread.sleep(1000);
StaticVariable.wait(10); seconds++;
this.reconnect(); if (retry < 10 && seconds > 60) {
} else if (retry == 4) {
log.warn("61850订阅断线重连已失败5次, ied=" + ied.getName() + ", ip=" + ied.getIp() + ", port=" + ied.getPort());
StaticVariable.wait(60);
this.reconnect(); this.reconnect();
} else if (retry >= 1) { } else if (seconds > 60 * 60) {
StaticVariable.wait(60);
this.reconnect(); this.reconnect();
} else {
String err = "61850订阅断线重连已失败多次不再重连. ied=" + ied.getName() + ", ip=" + ied.getIp() + ", port=" + ied.getPort();
log.warn(err);
if (realTimeDataService != null && ied != null) {
realTimeDataService.stopCollect(ied.getId());
}
if (webSocketServer != null) {
webSocketServer.sendMessage(err, null);
}
} }
break;
} catch (Exception ignore) { } catch (Exception ignore) {
String err = "61850订阅断线重连失败" + retry + "次, ied="
+ ied.getName() + ", ip=" + ied.getIp() + ", port=" + ied.getPort();
log.warn(err);
if (webSocketServer != null && retry >= 10) {
webSocketServer.sendMessage(err, null);
}
} }
} }
inRetry = false;
} }
} }
@ -218,7 +221,8 @@ public class IecClient implements ClientEventListener {
.port(102) .port(102)
.apTitle("1 3 9999 33") .apTitle("1 3 9999 33")
.build(); .build();
iecClient.connect(ied, new FileInputStream("C:/资料/om.SCD")); iecClient.init(ied, new FileInputStream("C:/资料/om.SCD"));
iecClient.connect();
String str = iecClient.getValue("OMDLMONT/SPDC1.MaxDsch.mag.f", "MX"); String str = iecClient.getValue("OMDLMONT/SPDC1.MaxDsch.mag.f", "MX");
System.out.println(str); System.out.println(str);
str = iecClient.getValue("OMDLMONT/SPDC1.MaxDsch.t", "MX"); str = iecClient.getValue("OMDLMONT/SPDC1.MaxDsch.t", "MX");

@ -83,6 +83,7 @@ public class IecServerService {
@PreDestroy @PreDestroy
private void stop() { private void stop() {
StaticVariable.shutdown = 1;
Iterator<Integer> it = StaticVariable.iecServerMap.keySet().iterator(); Iterator<Integer> it = StaticVariable.iecServerMap.keySet().iterator();
while (it.hasNext()) { while (it.hasNext()) {
Integer key = it.next(); Integer key = it.next();

@ -0,0 +1,15 @@
package com.xydl.cac.iec;
import com.xydl.cac.model.StaticVariable;
public class NetErrorThread extends Thread {
public int iedId;
@Override
public void run() {
IecClient iecClient = StaticVariable.realTimeClientMap.get(iedId);
if (iecClient != null) {
iecClient.associationClosed(null);
}
}
}

@ -36,36 +36,30 @@ public class RealTimeDataService {
if (!CollectionUtils.isEmpty(list)) { if (!CollectionUtils.isEmpty(list)) {
for (IcdIed ied : list) { for (IcdIed ied : list) {
try { try {
this.startCollect(ied.getId()); this.startCollect(ied);
} catch (Exception ignore) { } catch (Exception ignore) {
} }
} }
} }
} }
public void startCollect(Integer iedId) throws BusinessException { public void startCollect(IcdIed ied) throws BusinessException {
Optional<IcdIed> optional = iedRepository.findById(iedId);
if (!optional.isPresent()) {
throw new BusinessException("未找到该IED");
}
IcdIed ied = optional.get();
Optional<IcdFile> optionalFile = fileRepository.findById(ied.getIcdFileId()); Optional<IcdFile> optionalFile = fileRepository.findById(ied.getIcdFileId());
if (!optionalFile.isPresent()) { if (!optionalFile.isPresent()) {
throw new BusinessException("未找到该icd文件"); throw new BusinessException("未找到该icd文件");
} }
inDoing = true; inDoing = true;
IcdFile icdFile = optionalFile.get(); IcdFile icdFile = optionalFile.get();
IecClient iecClient = new IecClient(this, webSocketServer);
try { try {
IecClient iecClient = new IecClient(this, webSocketServer); iecClient.init(ied, icdFile.getXml());
iecClient.connect(ied, icdFile.getXml()); iecClient.keep = true;
iecClient.enableReporting();
ied.setStart(Constants.TRUE);
iedRepository.save(ied);
StaticVariable.realTimeClientMap.put(ied.getId(), iecClient); StaticVariable.realTimeClientMap.put(ied.getId(), iecClient);
iecClient.connect();
iecClient.enableReporting();
} catch (Exception ex) { } catch (Exception ex) {
String err = "61850订阅异常, ied=" + ied.getName() + ", ip=" + ied.getIp() + ", port=" + ied.getPort(); String err = "61850订阅异常, ied=" + ied.getName() + ", ip=" + ied.getIp() + ", port=" + ied.getPort();
log.error(err, ex); log.error(err, ex);
this.stopCollect(iedId);
throw new BusinessException(err); throw new BusinessException(err);
} finally { } finally {
inDoing = false; inDoing = false;
@ -73,29 +67,12 @@ public class RealTimeDataService {
} }
public void stopCollect(Integer iedId) { public void stopCollect(Integer iedId) {
this.onlyStop(iedId);
Optional<IcdIed> optional = iedRepository.findById(iedId);
if (optional.isPresent()) {
IcdIed ied = optional.get();
ied.setStart(Constants.FALSE);
iedRepository.save(ied);
}
}
public static void onErrorCheck(Integer iedId) {
IecClient iecClient = StaticVariable.realTimeClientMap.get(iedId);
if (iecClient != null) {
iecClient.associationClosed(null);
}
}
private void onlyStop(Integer iedId) {
IecClient iecClient = StaticVariable.realTimeClientMap.get(iedId); IecClient iecClient = StaticVariable.realTimeClientMap.get(iedId);
if (iecClient != null) { if (iecClient != null) {
StaticVariable.realTimeClientMap.remove(iedId);
iecClient.keep = false; iecClient.keep = false;
iecClient.disableReporting(); iecClient.disableReporting();
iecClient.disconnect(); iecClient.disconnect();
StaticVariable.realTimeClientMap.remove(iedId);
} }
} }
@ -109,7 +86,7 @@ public class RealTimeDataService {
idList.add(it.next()); idList.add(it.next());
} }
for (Integer iedId : idList) { for (Integer iedId : idList) {
this.onlyStop(iedId); this.stopCollect(iedId);
} }
} }

@ -3,7 +3,9 @@ package com.xydl.cac.service.impl;
import com.xydl.cac.entity.*; import com.xydl.cac.entity.*;
import com.xydl.cac.entity.constants.Constants; import com.xydl.cac.entity.constants.Constants;
import com.xydl.cac.exception.BusinessException; import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.iec.IecClient;
import com.xydl.cac.model.IcdAttUpdateModel; import com.xydl.cac.model.IcdAttUpdateModel;
import com.xydl.cac.model.StaticVariable;
import com.xydl.cac.repository.*; import com.xydl.cac.repository.*;
import com.xydl.cac.service.DataService; import com.xydl.cac.service.DataService;
import com.xydl.cac.service.IcdFileConfigService; import com.xydl.cac.service.IcdFileConfigService;
@ -40,6 +42,8 @@ public class IcdFileConfigServiceImpl implements IcdFileConfigService {
RptparamindexRepository rptparamindexRepository; RptparamindexRepository rptparamindexRepository;
@Resource @Resource
DataService dataService; DataService dataService;
@Resource
IedDlConfigRepository iedDlConfigRepository;
@Override @Override
public void upload(String xml, String filename, int srv) throws Exception { public void upload(String xml, String filename, int srv) throws Exception {
@ -108,6 +112,15 @@ public class IcdFileConfigServiceImpl implements IcdFileConfigService {
List<IcdIed> iedList = iedRepository.findByIcdFileId(icdFile.getId()); List<IcdIed> iedList = iedRepository.findByIcdFileId(icdFile.getId());
for (IcdIed ied : iedList) { for (IcdIed ied : iedList) {
if (StringUtils.isNotBlank(ied.getIp())) { if (StringUtils.isNotBlank(ied.getIp())) {
ied.setConnected(false);
ied.setRetry(0);
ied.setSeconds(0);
IecClient iecClient = StaticVariable.realTimeClientMap.get(ied.getId());
if (iecClient != null) {
ied.setRetry(iecClient.retry);
ied.setConnected(iecClient.connected);
ied.setSeconds(iecClient.seconds);
}
result.add(ied); result.add(ied);
} }
} }
@ -220,6 +233,7 @@ public class IcdFileConfigServiceImpl implements IcdFileConfigService {
configRepository.deleteAll(); configRepository.deleteAll();
fileRepository.deleteAll(); fileRepository.deleteAll();
iedRepository.deleteAll(); iedRepository.deleteAll();
iedDlConfigRepository.deleteAll();
} }
@Override @Override

@ -100,7 +100,8 @@ public class IedDlConfigServiceImpl implements IedDlConfigService {
IcdFile icdFile = optionalFile.get(); IcdFile icdFile = optionalFile.get();
IecClient iecClient = new IecClient(); IecClient iecClient = new IecClient();
try { try {
iecClient.connect(ied, icdFile.getXml()); iecClient.init(ied, icdFile.getXml());
iecClient.connect();
return iecClient.listFile(path); return iecClient.listFile(path);
} catch (Exception ex) { } catch (Exception ex) {
throw new BusinessException(ex.getMessage()); throw new BusinessException(ex.getMessage());

@ -86,6 +86,7 @@ public class WarnRuleServiceImpl implements WarnRuleService {
rule.setThreshold(item.getThreshold()); rule.setThreshold(item.getThreshold());
rule.setLevel(item.getLevel()); rule.setLevel(item.getLevel());
rule.setActive(item.getActive()); rule.setActive(item.getActive());
rule.setNotifyCom(item.getNotifyCom());
repository.save(rule); repository.save(rule);
StaticVariable.rule_Cache.remove(rule.getId()); StaticVariable.rule_Cache.remove(rule.getId());
} }

@ -4,6 +4,7 @@ import com.xydl.cac.entity.ModevTypePoint;
import com.xydl.cac.entity.NSensor; import com.xydl.cac.entity.NSensor;
import com.xydl.cac.entity.WarnRule; import com.xydl.cac.entity.WarnRule;
import com.xydl.cac.entity.Warning; import com.xydl.cac.entity.Warning;
import com.xydl.cac.entity.constants.Constants;
import com.xydl.cac.model.StaticVariable; import com.xydl.cac.model.StaticVariable;
import com.xydl.cac.model.TriggerModel; import com.xydl.cac.model.TriggerModel;
import com.xydl.cac.repository.WarnRuleRepository; import com.xydl.cac.repository.WarnRuleRepository;
@ -73,6 +74,10 @@ public class RuleCheckTask {
for (TriggerModel model : warnList) { for (TriggerModel model : warnList) {
this.sendWarning(rule, model); this.sendWarning(rule, model);
} }
if (rule.getNotifyCom() != null && rule.getNotifyCom().intValue() == Constants.TRUE) {
// 通知串口
}
} }
} catch (Exception e) { } catch (Exception e) {
log.error("RuleCheckTask.ruleCheck error.", e); log.error("RuleCheckTask.ruleCheck error.", e);

@ -113,7 +113,6 @@ public class DateUtil {
time = format(date); time = format(date);
LocalDateTime localtime = LocalDateTime.parse(time, defaultFormatter); LocalDateTime localtime = LocalDateTime.parse(time, defaultFormatter);
if (localtime.getYear() < 2000) { if (localtime.getYear() < 2000) {
log.warn("该时间小于2000年: " + str);
return null; return null;
} }
ZonedDateTime zonedDateTime = localtime.atZone(ZoneOffset.UTC); ZonedDateTime zonedDateTime = localtime.atZone(ZoneOffset.UTC);

Loading…
Cancel
Save