diff --git a/src/main/java/com/xydl/cac/iec/IEDService.java b/src/main/java/com/xydl/cac/iec/IEDService.java new file mode 100644 index 0000000..a7c0037 --- /dev/null +++ b/src/main/java/com/xydl/cac/iec/IEDService.java @@ -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 rptList) { + List 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 configTypeList, List rptList) throws Exception { + for (IcdConfigType configType : configTypeList) { + List instList = _instRepository.findByIcdConfigTypeId(configType.getId()); + List 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 rptList, String paramindex) { + for (Rptparamindex rpt : rptList) { + if (rpt.getParamindex().equals(paramindex)) { + return rpt; + } + } + return null; + } +} diff --git a/src/main/java/com/xydl/cac/repository/NSensorRepository.java b/src/main/java/com/xydl/cac/repository/NSensorRepository.java index 6aeedac..df691d7 100644 --- a/src/main/java/com/xydl/cac/repository/NSensorRepository.java +++ b/src/main/java/com/xydl/cac/repository/NSensorRepository.java @@ -21,6 +21,8 @@ public interface NSensorRepository extends JpaRepository, JpaS List findByTypeId(Integer typeId); + List findByTypeIdIn(List typeIdList); + List findByDevId(Integer devId); Long countByZsbIdIn(List list); diff --git a/src/main/java/com/xydl/cac/repository/RptparamindexRepository.java b/src/main/java/com/xydl/cac/repository/RptparamindexRepository.java index 45ae4fd..0a4fa6a 100644 --- a/src/main/java/com/xydl/cac/repository/RptparamindexRepository.java +++ b/src/main/java/com/xydl/cac/repository/RptparamindexRepository.java @@ -13,6 +13,8 @@ public interface RptparamindexRepository extends JpaRepository findAllByEqmid(Integer eqmid); + List findAllByEqmidInAndColnameIsNotNull(List eqmidList); + List findAllByParamindexStartingWith(String param); List findAllByParamindexEquals(String param); diff --git a/src/main/java/com/xydl/cac/task/Client61850Task.java b/src/main/java/com/xydl/cac/task/Client61850Task.java index 3d5e1d6..68893e1 100644 --- a/src/main/java/com/xydl/cac/task/Client61850Task.java +++ b/src/main/java/com/xydl/cac/task/Client61850Task.java @@ -1,15 +1,18 @@ package com.xydl.cac.task; 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.service.DataService; +import com.xydl.cac.service.ModevTypeService; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.Calendar; import java.util.List; @Service @@ -30,73 +33,72 @@ public class Client61850Task { RptparamindexRepository rptparamindexRepository; @Resource DataService dataService; + @Resource + ModevTypeService modevTypeService; + @Resource + NSensorRepository sensorRepository; - @Scheduled(cron = "0 30 * * * ?") + @Scheduled(cron = "0 * * * * ?") public void collectAll() { - List icdFileList = fileRepository.findAll(); - if (!CollectionUtils.isEmpty(icdFileList)) { - for (IcdFile icdFile : icdFileList) { - this.collectIcdFile(icdFile); + Calendar cal = Calendar.getInstance(); + int h = cal.get(Calendar.HOUR_OF_DAY); + int m = cal.get(Calendar.MINUTE); + m = h * 60 + m; + + List typeIdList = new ArrayList<>(); + List 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 iedList = iedRepository.findByIcdFileId(icdFile.getId()); - if (!CollectionUtils.isEmpty(iedList)) { - for (IcdIed ied : iedList) { - this.collectIed(icdFile.getXml(), ied); + List sensorList = sensorRepository.findByTypeIdIn(typeIdList); + List eqmidList = new ArrayList<>(); + for (NSensor sensor : sensorList) { + if (sensor.getDevId() != null) { + eqmidList.add(sensor.getDevId()); } } - } - - private void collectIed(String xml, IcdIed ied) { - List configTypeList = configRepository.findByIcdIedId(ied.getId()); - if (CollectionUtils.isEmpty(configTypeList)) { + if (CollectionUtils.isEmpty(eqmidList)) { return; } - // 开始连iec61850采集数据 - IecClient iecClient = new IecClient(); - try { - iecClient.connect(ied.getIp(), 102, ied.getApTitle(), xml); - this.collectIedValue(configTypeList, iecClient); + List rptList = rptparamindexRepository.findAllByEqmidInAndColnameIsNotNull(eqmidList); + if (CollectionUtils.isEmpty(rptList)) { + return; + } - } catch (Exception ex) { - log.error("61850采集数据异常, ied=" + ied.getName() + ", ip=" + ied.getIp(), ex); - } finally { - iecClient.disconnect(); + List icdFileList = fileRepository.findAll(); + if (!CollectionUtils.isEmpty(icdFileList)) { + for (IcdFile icdFile : icdFileList) { + this.collectIcdFile(icdFile, rptList); + } } } - private void collectIedValue(List configTypeList, IecClient iecClient) throws Exception { - for (IcdConfigType configType : configTypeList) { - List instList = instRepository.findByIcdConfigTypeId(configType.getId()); - List 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"; - - List rptList = rptparamindexRepository.findAllByParamindexEqualsAndEqmidIsNotNullAndTablenameIsNotNullAndColnameIsNotNull(paramindexOld); - if (!CollectionUtils.isEmpty(rptList)) { - this.getOneValue(paramindexNew, paramindexT, att.getFc(), rptList.get(0), iecClient); - } - } - } + private void collectIcdFile(IcdFile icdFile, List rptList) { + List iedList = iedRepository.findByIcdFileId(icdFile.getId()); + if (!CollectionUtils.isEmpty(iedList)) { + for (IcdIed ied : iedList) { + this.collectIed(icdFile.getXml(), ied, rptList); } } } - private void getOneValue(String paramindexNew, String paramindexT, String fc, Rptparamindex rpt, IecClient iecClient) throws Exception { - String value = iecClient.getValue(paramindexNew, fc); - String time = iecClient.getValue(paramindexT, fc); - time = time.replace("T", " ").replace("Z", "").replace("z", ""); - dataService.insertData(rpt.getTablename(), rpt.getEqmid(), time, rpt.getColname(), value); + private void collectIed(String xml, IcdIed ied, List rptList) { + IEDService iedService = new IEDService(configRepository, attRepository, instRepository, + rptparamindexRepository, dataService, xml, ied); + + iedService.collectAndSave(rptList); } } diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 82a7311..2f4369f 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -27,7 +27,7 @@ spring: task: scheduling: pool: - size: 5 + size: 10 cac: datapath: /home/xydl/ncac/data diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 681a7fb..d50d8a8 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -27,7 +27,7 @@ spring: task: scheduling: pool: - size: 5 + size: 10 cac: datapath: /home/xydl/ncac/data