feat: 重构ied服务并根据定时间隔采集数据

dev
huangfeng 8 months ago
parent 70b7b0c7a5
commit b4f6df1914

@ -0,0 +1,102 @@
package com.xydl.cac.iec;
import com.xydl.cac.entity.*;
import com.xydl.cac.repository.IcdConfigTypeAttRepository;
import com.xydl.cac.repository.IcdConfigTypeInstRepository;
import com.xydl.cac.repository.IcdConfigTypeRepository;
import com.xydl.cac.repository.RptparamindexRepository;
import com.xydl.cac.service.DataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import java.util.List;
@Slf4j
public class IEDService {
IcdConfigTypeRepository _configRepository;
IcdConfigTypeAttRepository _attRepository;
IcdConfigTypeInstRepository _instRepository;
RptparamindexRepository _rptparamindexRepository;
DataService _dataService;
IecClient iecClient;
IcdIed ied;
String xml;
public IEDService(IcdConfigTypeRepository configRepository, IcdConfigTypeAttRepository attRepository,
IcdConfigTypeInstRepository instRepository, RptparamindexRepository rptparamindexRepository,
DataService dataService, String xml, IcdIed ied) {
_configRepository = configRepository;
_attRepository = attRepository;
_instRepository = instRepository;
_rptparamindexRepository = rptparamindexRepository;
_dataService = dataService;
this.xml = xml;
this.ied = ied;
iecClient = new IecClient();
}
public void connect() throws Exception {
iecClient.connect(ied.getIp(), 102, ied.getApTitle(), xml);
}
public void disconnect() {
iecClient.disconnect();
}
public void collectAndSave(List<Rptparamindex> rptList) {
List<IcdConfigType> configTypeList = _configRepository.findByIcdIedId(ied.getId());
if (CollectionUtils.isEmpty(configTypeList)) {
return;
}
// 开始连iec61850采集数据
try {
log.info("61850开始采集数据, ied=" + ied.getName() + ", ip=" + ied.getIp());
this.connect();
this.doCollectAndSave(configTypeList, rptList);
} catch (Exception ex) {
log.error("61850采集数据异常, ied=" + ied.getName() + ", ip=" + ied.getIp(), ex);
} finally {
iecClient.disconnect();
}
}
private void doCollectAndSave(List<IcdConfigType> configTypeList, List<Rptparamindex> rptList) throws Exception {
for (IcdConfigType configType : configTypeList) {
List<IcdConfigTypeInst> instList = _instRepository.findByIcdConfigTypeId(configType.getId());
List<IcdConfigTypeAtt> attList = _attRepository.findByIcdConfigTypeId(configType.getId());
for (IcdConfigTypeInst inst : instList) {
String param = configType.getIedName() + configType.getLdeviceInst() + "/" + configType.getLnClass() + inst.getInst();
for (IcdConfigTypeAtt att : attList) {
if (att.containInst(inst.getInst())) {
String paramindexOld = param + "$" + att.getParam();
String paramindexNew = param + "." + att.getDoName() + "." + att.getLastName().replace("$", ".");
String paramindexT = param + "." + att.getDoName() + ".t";
Rptparamindex rpt = this.findRpt(rptList, paramindexOld);
if (rpt != null) {
this.collectAndSaveValue(paramindexNew, paramindexT, att.getFc(), rpt);
}
}
}
}
}
}
private void collectAndSaveValue(String paramindexNew, String paramindexT, String fc, Rptparamindex rpt) throws Exception {
String value = iecClient.getValue(paramindexNew, fc);
String time = iecClient.getValue(paramindexT, fc);
log.info("采集到" + fc + " " + paramindexNew + "=" + value + ", t=" + time);
time = time.replace("T", " ").replace("Z", "").replace("z", "");
_dataService.insertData(rpt.getTablename(), rpt.getEqmid(), time, rpt.getColname(), value);
}
private Rptparamindex findRpt(List<Rptparamindex> rptList, String paramindex) {
for (Rptparamindex rpt : rptList) {
if (rpt.getParamindex().equals(paramindex)) {
return rpt;
}
}
return null;
}
}

@ -21,6 +21,8 @@ public interface NSensorRepository extends JpaRepository<NSensor, Integer>, JpaS
List<NSensor> findByTypeId(Integer typeId); List<NSensor> findByTypeId(Integer typeId);
List<NSensor> findByTypeIdIn(List<Integer> typeIdList);
List<NSensor> findByDevId(Integer devId); List<NSensor> findByDevId(Integer devId);
Long countByZsbIdIn(List<Integer> list); Long countByZsbIdIn(List<Integer> list);

@ -13,6 +13,8 @@ public interface RptparamindexRepository extends JpaRepository<Rptparamindex, St
List<Rptparamindex> findAllByEqmid(Integer eqmid); List<Rptparamindex> findAllByEqmid(Integer eqmid);
List<Rptparamindex> findAllByEqmidInAndColnameIsNotNull(List<Integer> eqmidList);
List<Rptparamindex> findAllByParamindexStartingWith(String param); List<Rptparamindex> findAllByParamindexStartingWith(String param);
List<Rptparamindex> findAllByParamindexEquals(String param); List<Rptparamindex> findAllByParamindexEquals(String param);

@ -1,15 +1,18 @@
package com.xydl.cac.task; package com.xydl.cac.task;
import com.xydl.cac.entity.*; import com.xydl.cac.entity.*;
import com.xydl.cac.iec.IecClient; import com.xydl.cac.iec.IEDService;
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.ModevTypeService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List; import java.util.List;
@Service @Service
@ -30,73 +33,72 @@ public class Client61850Task {
RptparamindexRepository rptparamindexRepository; RptparamindexRepository rptparamindexRepository;
@Resource @Resource
DataService dataService; DataService dataService;
@Resource
ModevTypeService modevTypeService;
@Resource
NSensorRepository sensorRepository;
@Scheduled(cron = "0 30 * * * ?") @Scheduled(cron = "0 * * * * ?")
public void collectAll() { public void collectAll() {
List<IcdFile> icdFileList = fileRepository.findAll(); Calendar cal = Calendar.getInstance();
if (!CollectionUtils.isEmpty(icdFileList)) { int h = cal.get(Calendar.HOUR_OF_DAY);
for (IcdFile icdFile : icdFileList) { int m = cal.get(Calendar.MINUTE);
this.collectIcdFile(icdFile); m = h * 60 + m;
List<Integer> typeIdList = new ArrayList<>();
List<ModevType> modevTypeList = modevTypeService.listAll();
for (ModevType type : modevTypeList) {
int intervals = 30;
if (type.getIntervals() != null) {
intervals = type.getIntervals();
}
int x = m % intervals;
if (x == 0) {
typeIdList.add(type.getId());
} }
} }
} if (CollectionUtils.isEmpty(typeIdList)) {
return;
}
private void collectIcdFile(IcdFile icdFile) { List<NSensor> sensorList = sensorRepository.findByTypeIdIn(typeIdList);
List<IcdIed> iedList = iedRepository.findByIcdFileId(icdFile.getId()); List<Integer> eqmidList = new ArrayList<>();
if (!CollectionUtils.isEmpty(iedList)) { for (NSensor sensor : sensorList) {
for (IcdIed ied : iedList) { if (sensor.getDevId() != null) {
this.collectIed(icdFile.getXml(), ied); eqmidList.add(sensor.getDevId());
} }
} }
} if (CollectionUtils.isEmpty(eqmidList)) {
private void collectIed(String xml, IcdIed ied) {
List<IcdConfigType> configTypeList = configRepository.findByIcdIedId(ied.getId());
if (CollectionUtils.isEmpty(configTypeList)) {
return; return;
} }
// 开始连iec61850采集数据 List<Rptparamindex> rptList = rptparamindexRepository.findAllByEqmidInAndColnameIsNotNull(eqmidList);
IecClient iecClient = new IecClient(); if (CollectionUtils.isEmpty(rptList)) {
try { return;
iecClient.connect(ied.getIp(), 102, ied.getApTitle(), xml); }
this.collectIedValue(configTypeList, iecClient);
} catch (Exception ex) { List<IcdFile> icdFileList = fileRepository.findAll();
log.error("61850采集数据异常, ied=" + ied.getName() + ", ip=" + ied.getIp(), ex); if (!CollectionUtils.isEmpty(icdFileList)) {
} finally { for (IcdFile icdFile : icdFileList) {
iecClient.disconnect(); this.collectIcdFile(icdFile, rptList);
}
} }
} }
private void collectIedValue(List<IcdConfigType> configTypeList, IecClient iecClient) throws Exception { private void collectIcdFile(IcdFile icdFile, List<Rptparamindex> rptList) {
for (IcdConfigType configType : configTypeList) { List<IcdIed> iedList = iedRepository.findByIcdFileId(icdFile.getId());
List<IcdConfigTypeInst> instList = instRepository.findByIcdConfigTypeId(configType.getId()); if (!CollectionUtils.isEmpty(iedList)) {
List<IcdConfigTypeAtt> attList = attRepository.findByIcdConfigTypeId(configType.getId()); for (IcdIed ied : iedList) {
for (IcdConfigTypeInst inst : instList) { this.collectIed(icdFile.getXml(), ied, rptList);
String param = configType.getIedName() + configType.getLdeviceInst() + "/" + configType.getLnClass()
+ inst.getInst();
for (IcdConfigTypeAtt att : attList) {
if (att.containInst(inst.getInst())) {
String paramindexOld = param + "$" + att.getParam();
String paramindexNew = param + "." + att.getDoName() + "." + att.getLastName().replace("$", ".");
String paramindexT = param + "." + att.getDoName() + ".t";
List<Rptparamindex> rptList = rptparamindexRepository.findAllByParamindexEqualsAndEqmidIsNotNullAndTablenameIsNotNullAndColnameIsNotNull(paramindexOld);
if (!CollectionUtils.isEmpty(rptList)) {
this.getOneValue(paramindexNew, paramindexT, att.getFc(), rptList.get(0), iecClient);
}
}
}
} }
} }
} }
private void getOneValue(String paramindexNew, String paramindexT, String fc, Rptparamindex rpt, IecClient iecClient) throws Exception { private void collectIed(String xml, IcdIed ied, List<Rptparamindex> rptList) {
String value = iecClient.getValue(paramindexNew, fc); IEDService iedService = new IEDService(configRepository, attRepository, instRepository,
String time = iecClient.getValue(paramindexT, fc); rptparamindexRepository, dataService, xml, ied);
time = time.replace("T", " ").replace("Z", "").replace("z", "");
dataService.insertData(rpt.getTablename(), rpt.getEqmid(), time, rpt.getColname(), value); iedService.collectAndSave(rptList);
} }
} }

@ -27,7 +27,7 @@ spring:
task: task:
scheduling: scheduling:
pool: pool:
size: 5 size: 10
cac: cac:
datapath: /home/xydl/ncac/data datapath: /home/xydl/ncac/data

@ -27,7 +27,7 @@ spring:
task: task:
scheduling: scheduling:
pool: pool:
size: 5 size: 10
cac: cac:
datapath: /home/xydl/ncac/data datapath: /home/xydl/ncac/data

Loading…
Cancel
Save