You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
backend/src/main/java/com/xydl/cac/iec/IEDCollectService.java

156 lines
6.3 KiB
Java

package com.xydl.cac.iec;
import com.beanit.iec61850bean.BasicDataAttribute;
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 com.xydl.cac.socket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import java.util.HashMap;
import java.util.List;
@Slf4j
public class IEDCollectService {
IcdConfigTypeRepository _configRepository;
IcdConfigTypeAttRepository _attRepository;
IcdConfigTypeInstRepository _instRepository;
RptparamindexRepository _rptparamindexRepository;
DataService _dataService;
IecClient iecClient;
IcdIed ied;
String xml;
WebSocketServer _webSocketServer;
public static HashMap<Integer, HashMap<String, String>> lastDataMap = new HashMap<>();
public static HashMap<String, String> relationMap = new HashMap<>();
public IEDCollectService(IcdConfigTypeRepository configRepository, IcdConfigTypeAttRepository attRepository,
IcdConfigTypeInstRepository instRepository, RptparamindexRepository rptparamindexRepository,
DataService dataService, String xml, IcdIed ied,
WebSocketServer webSocketServer) {
_configRepository = configRepository;
_attRepository = attRepository;
_instRepository = instRepository;
_rptparamindexRepository = rptparamindexRepository;
_dataService = dataService;
this.xml = xml;
this.ied = ied;
_webSocketServer = webSocketServer;
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) {
String err = "61850采集数据异常, ied=" + ied.getName() + ", ip=" + ied.getIp();
log.error(err, ex);
_webSocketServer.sendMessage(err, null);
} 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);
// 更新最新数据缓存
updateLastData(rpt.getEqmid(), rpt.getColname(), value, time);
// 更新关联关系缓存
String key = paramindexNew + "_" + fc;
value = rpt.getEqmid() + "," + rpt.getColname();
relationMap.put(key, value);
key = paramindexT + "_" + fc;
value = rpt.getEqmid() + ",acquisitionTime";
relationMap.put(key, value);
}
private Rptparamindex findRpt(List<Rptparamindex> rptList, String paramindex) {
for (Rptparamindex rpt : rptList) {
if (rpt.getParamindex().equals(paramindex)) {
return rpt;
}
}
return null;
}
public static void updateLastData(Integer eqmid, String colname, String value, String time) {
HashMap<String, String> map = lastDataMap.get(eqmid);
if (map == null) {
map = new HashMap<>();
lastDataMap.put(eqmid, map);
}
map.put(colname, value);
if (time != null) {
map.put("acquisitionTime", time);
}
}
public static void updateLastData(BasicDataAttribute bda) {
try {
String ref = bda.getReference().toString();
String key = ref + "_" + bda.getFc().toString();
if (IEDCollectService.relationMap.containsKey(key)) {
String value = IEDCollectService.relationMap.get(key);
String[] str = value.split(",");
Integer eqmid = Integer.parseInt(str[0]);
String colname = str[1];
value = bda.getValueString();
if ("acquisitionTime".equals(colname)) {
value = value.replace("T", " ").replace("Z", "").replace("z", "");
}
updateLastData(eqmid, colname, value, null);
}
} catch (Exception ignore) {
}
}
}