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 8faee9e669

@ -34,4 +34,6 @@ CREATE TABLE `ied_dl_config` (
`todel` int(11) DEFAULT NULL COMMENT '下载后删除',
`active` int(11) DEFAULT NULL COMMENT '0:停用; 1:启用;',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `i2sync_field` ADD COLUMN `attach` VARCHAR(45) NULL COMMENT 'sensorid尾部附加' AFTER `dest_field_name`;

@ -2,10 +2,7 @@ package com.xydl.cac.controller;
import com.alibaba.excel.util.StringUtils;
import com.xydl.cac.adapter.BusiAdapter;
import com.xydl.cac.entity.I2syncConfig;
import com.xydl.cac.entity.I2syncField;
import com.xydl.cac.entity.I2syncRecord;
import com.xydl.cac.entity.ModevType;
import com.xydl.cac.entity.*;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.Response;
import com.xydl.cac.model.i2sync.Attr;
@ -13,7 +10,9 @@ import com.xydl.cac.model.i2sync.Datanode;
import com.xydl.cac.model.i2sync.Monitordata;
import com.xydl.cac.model.i2sync.Request;
import com.xydl.cac.service.I2syncService;
import com.xydl.cac.service.ModevTypePointService;
import com.xydl.cac.service.ModevTypeService;
import com.xydl.cac.transform.I2DataTransformer;
import com.xydl.cac.util.DateUtil;
import com.xydl.cac.util.JSONUtil;
import io.swagger.annotations.Api;
@ -37,7 +36,11 @@ public class I2syncController extends BasicController {
@Resource
ModevTypeService modevTypeService;
@Resource
ModevTypePointService modevTypePointService;
@Resource
BusiAdapter busiAdapter;
@Resource
I2DataTransformer dataTransformer;
@GetMapping("status")
@ApiOperation("查询状态")
@ -107,37 +110,33 @@ public class I2syncController extends BasicController {
@ApiOperation("预览xml结构")
public Response<String> prewXml(Integer modevtypeId) throws Exception {
ModevType modevType = modevTypeService.detail(modevtypeId);
List<ModevTypePoint> points = modevTypePointService.listAll(modevtypeId);
List<I2syncField> fieldList = service.listFieldConfig(modevType.getTablename());
List<Attr> attrs = new ArrayList<>();
Attr attr = new Attr();
attr.setName("Phase");
attr.setValue("A相");
attr.setAlarm("FALSE");
attrs.add(attr);
for (I2syncField field : fieldList) {
attr = new Attr();
attr.setName(field.getDestFieldName());
attr.setValue("100.0");
attr.setAlarm("FALSE");
attrs.add(attr);
NSensor sensor = NSensor.builder()
.id(12)
.typeId(modevtypeId)
.sensorCode("HNHFT001Q1W000104")
.equipmentId("HNHFT001Q1W000104")
.phase("ABC相")
.build();
I2syncRecord record = I2syncRecord.builder()
.build();
I2syncConfig config = I2syncConfig.builder()
.typeCode(modevType.getTypeCode())
.build();
List<Map<String, Object>> dataList = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
map.put("acquisitionTime", DateUtil.format(new Date()));
for (ModevTypePoint point : points) {
map.put(point.getField(), 1.25);
}
Datanode node = new Datanode();
node.setSensorid("HNHFT001Q1W000104");
node.setType(modevType.getTypeCode());
node.setEquipmentid("HNHFT001Q1W000104");
node.setTimestamp(DateUtil.format(new Date()));
node.setAttr(attrs);
List<Datanode> nodeList = new ArrayList<>();
nodeList.add(node);
Monitordata monitordata = new Monitordata();
monitordata.setCacid("HNHFT001Q1W000104");
monitordata.setDatanodenum(1);
monitordata.setDatanode(nodeList);
Request request = new Request();
request.setMonitordata(monitordata);
dataList.add(map);
Request request = dataTransformer.transform(sensor, record, config, fieldList, dataList);
String xml = JSONUtil.object2Xml(request);
Response<String> resp = Response.success(xml);
if (StringUtils.isBlank(node.getType())) {
if (StringUtils.isBlank(modevType.getTypeCode())) {
resp.setWarnMsg("该类型还未配置对应的类型编码将导致type为空");
}
return resp;

@ -7,9 +7,9 @@ import com.xydl.cac.entity.constants.Constants;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.iec.IecServerService;
import com.xydl.cac.model.Response;
import com.xydl.cac.model.StaticVariable;
import com.xydl.cac.service.IcdFileConfigService;
import com.xydl.cac.service.IcdTransformService;
import com.xydl.cac.service.impl.IcdTransformServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
@ -128,11 +128,15 @@ public class IecServerController extends BasicController {
public Response<List<IcdTransform>> listTransform() throws Exception {
List<IcdTransform> result = transformService.listTransform();
for (IcdTransform item : result) {
if (IcdTransformServiceImpl.rptFromActiveMap.containsKey(item.getRptFrom())) {
if (StaticVariable.rptFromActiveMap.containsKey(item.getRptFrom())) {
item.setFromActive(true);
} else {
item.setFromActive(false);
}
if (IcdTransformServiceImpl.rptToActiveMap.containsKey(item.getRptTo())) {
if (StaticVariable.rptToActiveMap.containsKey(item.getRptTo())) {
item.setToActive(true);
} else {
item.setToActive(false);
}
}
return Response.success(result);

@ -65,6 +65,24 @@ public class TestController extends BasicController {
return Response.success(StaticVariable.sensorLastDataMap);
}
@GetMapping("iecServerMap")
@ApiOperation("iecServerMap")
public Response<HashMap> iecServerMap() {
return Response.success(StaticVariable.iecServerMap);
}
@GetMapping("rptFromActiveMap")
@ApiOperation("rptFromActiveMap")
public Response<HashMap> rptFromActiveMap() {
return Response.success(StaticVariable.rptFromActiveMap);
}
@GetMapping("rptToActiveMap")
@ApiOperation("rptToActiveMap")
public Response<HashMap> rptToActiveMap() {
return Response.success(StaticVariable.rptToActiveMap);
}
@GetMapping("clearMap")
@ApiOperation("clearMap")
public void clearMap() {

@ -33,4 +33,7 @@ public class I2syncField {
@Column(name = "dest_field_name")
private String destFieldName;
@Column(name = "attach")
private String attach;
}

@ -116,23 +116,33 @@ public class IEDCollectService {
}
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);
// 更新关联关系缓存
String key = paramindexNew + "_" + fc;
String value = rpt.getEqmid() + "," + rpt.getColname();
StaticVariable.paramRelationMap.put(key, value);
key = paramindexT + "_" + fc;
value = rpt.getEqmid() + ",acquisitionTime";
StaticVariable.paramRelationMap.put(key, value);
// 采集数据
BasicDataAttribute valueNode = iecClient.getNode(paramindexNew, fc);
BasicDataAttribute timeNode = iecClient.getNode(paramindexT, fc);
value = valueNode.getValueString();
String time = timeNode.getValueString();
log.info("采集到" + fc + " " + paramindexNew + "=" + value + ", t=" + time);
time = DateUtil.fromZoneUTCToLocal(time);
if (time == null) {
return;
}
time = this.makeSameTime(rpt.getEqmid(), time);
_dataService.insertData(rpt.getTablename(), rpt.getEqmid(), time, rpt.getColname(), value);
// 更新最新数据缓存
updateLastData(rpt.getEqmid(), rpt.getColname(), value, time);
StaticVariable.updateLastData(rpt.getEqmid(), rpt.getColname(), value, time);
// 更新关联关系缓存
String key = paramindexNew + "_" + fc;
value = rpt.getEqmid() + "," + rpt.getColname();
StaticVariable.paramRelationMap.put(key, value);
key = paramindexT + "_" + fc;
value = rpt.getEqmid() + ",acquisitionTime";
StaticVariable.paramRelationMap.put(key, value);
// 更新服务端
StaticVariable.updateServerNodeValue(valueNode);
StaticVariable.updateServerNodeValue(timeNode);
}
private String makeSameTime(Integer eqmid, String time) {
@ -229,34 +239,4 @@ public class IEDCollectService {
}
}
public static void updateLastData(Integer eqmid, String colname, String value, String time) {
HashMap<String, String> map = StaticVariable.sensorLastDataMap.get(eqmid);
if (map == null) {
map = new HashMap<>();
StaticVariable.sensorLastDataMap.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 (StaticVariable.paramRelationMap.containsKey(key)) {
String value = StaticVariable.paramRelationMap.get(key);
String[] str = value.split(",");
Integer eqmid = Integer.parseInt(str[0]);
String colname = str[1];
value = bda.getValueString();
if ("acquisitionTime".equals(colname)) {
value = DateUtil.fromZoneUTCToLocal(value);
}
updateLastData(eqmid, colname, value, null);
}
} catch (Exception ignore) {
}
}
}

@ -95,14 +95,21 @@ public class IecClient implements ClientEventListener {
}
public String getValue(String paramindex, String fc) throws Exception {
BasicDataAttribute node = this.getNode(paramindex, fc);
if (node != null) {
return node.getValueString();
}
return null;
}
public BasicDataAttribute getNode(String paramindex, String fc) throws Exception {
FcModelNode node = (FcModelNode) serverModel.findModelNode(paramindex, Fc.valueOf(fc));
if (node == null) {
throw new BusinessException("icd文件里未找到该节点, " + fc + " " + paramindex);
}
clientAssociation.getDataValues(node);
if (node instanceof BasicDataAttribute) {
String value = ((BasicDataAttribute) node).getValueString();
return value;
return (BasicDataAttribute) node;
}
return null;
}

@ -2,6 +2,7 @@ package com.xydl.cac.iec;
import com.beanit.iec61850bean.*;
import com.xydl.cac.entity.IcdTransform;
import com.xydl.cac.model.StaticVariable;
import com.xydl.cac.service.IcdTransformService;
import com.xydl.cac.service.impl.IcdTransformServiceImpl;
import lombok.extern.slf4j.Slf4j;
@ -57,7 +58,7 @@ public class IecServer implements ServerEventListener {
if (ref.startsWith(item.getRptFrom())) {
ref = ref.replace(item.getRptFrom(), item.getRptTo());
rptTo = item.getRptTo();
IcdTransformServiceImpl.rptFromActiveMap.put(item.getRptFrom(), "1");
StaticVariable.rptFromActiveMap.put(item.getRptFrom(), "1");
break;
}
}
@ -67,8 +68,10 @@ public class IecServer implements ServerEventListener {
node.setValueFrom(bda);
List<BasicDataAttribute> bdas = new ArrayList<>();
bdas.add(node);
IcdTransformServiceImpl.rptToActiveMap.put(rptTo, "1");
StaticVariable.rptToActiveMap.put(rptTo, "1");
serverSap.setValues(bdas);
} else {
StaticVariable.rptToActiveMap.remove(rptTo);
}
}

@ -131,14 +131,16 @@ public class RealTimeDataService {
for (ModelNode child : fcnode.getChildren()) {
if (child instanceof BasicDataAttribute) {
BasicDataAttribute bda = (BasicDataAttribute) child;
processBdaNodeValue(bda);
StaticVariable.updateServerNodeValue(bda);
StaticVariable.updateLastData(bda);
} else if (child instanceof ConstructedDataAttribute) {
ConstructedDataAttribute cda = (ConstructedDataAttribute) child;
if (!CollectionUtils.isEmpty(cda.getChildren())) {
for (ModelNode cchild : cda.getChildren()) {
if (cchild instanceof BasicDataAttribute) {
BasicDataAttribute bda = (BasicDataAttribute) cchild;
processBdaNodeValue(bda);
StaticVariable.updateServerNodeValue(bda);
StaticVariable.updateLastData(bda);
}
}
}
@ -148,13 +150,4 @@ public class RealTimeDataService {
}
}
private static void processBdaNodeValue(BasicDataAttribute bda) {
Iterator<Integer> it = StaticVariable.iecServerMap.keySet().iterator();
while (it.hasNext()) {
Integer key = it.next();
IecServer iecServer = StaticVariable.iecServerMap.get(key);
iecServer.updateBda(bda);
}
IEDCollectService.updateLastData(bda);
}
}

@ -1,15 +1,18 @@
package com.xydl.cac.model;
import com.fazecast.jSerialComm.SerialPort;
import com.beanit.iec61850bean.BasicDataAttribute;
import com.xydl.cac.entity.Jg;
import com.xydl.cac.entity.ModevType;
import com.xydl.cac.entity.WarnRule;
import com.xydl.cac.entity.Zsb;
import com.xydl.cac.iec.IecClient;
import com.xydl.cac.iec.IecServer;
import com.xydl.cac.util.DateUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
@ -21,6 +24,8 @@ public class StaticVariable {
public static HashMap<String, String> doneWarnMap = new HashMap<>();
public static HashMap<Integer, IecClient> realTimeClientMap = new HashMap<>();
public static int shutdown = 0;
public static HashMap<String, String> rptFromActiveMap = new HashMap<>();
public static HashMap<String, String> rptToActiveMap = new HashMap<>();
public static HashMap<String, String> unit_Cache = new HashMap<>();
public static List<ModevType> modevType_Cache = null;
@ -38,4 +43,50 @@ public class StaticVariable {
Thread.sleep(1000);
}
}
// 更新服务端
public static void updateServerNodeValue(BasicDataAttribute bda) {
Iterator<Integer> it = StaticVariable.iecServerMap.keySet().iterator();
while (it.hasNext()) {
Integer key = it.next();
IecServer iecServer = StaticVariable.iecServerMap.get(key);
iecServer.updateBda(bda);
}
}
// 更新最新数据缓存
public static void updateLastData(Integer eqmid, String colname, String value, String time) {
HashMap<String, String> map = StaticVariable.sensorLastDataMap.get(eqmid);
if (map == null) {
map = new HashMap<>();
StaticVariable.sensorLastDataMap.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 (StaticVariable.paramRelationMap.containsKey(key)) {
String value = StaticVariable.paramRelationMap.get(key);
String[] str = value.split(",");
Integer eqmid = Integer.parseInt(str[0]);
String colname = str[1];
value = bda.getValueString();
if ("acquisitionTime".equals(colname)) {
value = DateUtil.fromZoneUTCToLocal(value);
if (value == null) {
return;
}
}
updateLastData(eqmid, colname, value, null);
}
} catch (Exception ignore) {
}
}
}

@ -244,7 +244,7 @@ public class DataServiceImpl implements DataService {
@Override
public List<Map<String, Object>> getLatestData(String tableName, Integer devId, List<ModevTypePoint> points, Date start) throws Exception {
ConditionModel model = new ConditionModel();
model.setPageSize(1000);
model.setPageSize(500);
model.setPageNum(1);
model.setStartTime(start);
model.setExcludeStartTime(true);

@ -11,8 +11,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
@ -24,8 +22,6 @@ public class IcdTransformServiceImpl implements IcdTransformService {
@Resource
IcdTransformRepository transformRepository;
public static HashMap<String, String> rptFromActiveMap = new HashMap<>();
public static HashMap<String, String> rptToActiveMap = new HashMap<>();
@Override
@CacheEvict(cacheNames = {"listTransform"}, allEntries = true)

@ -1,6 +1,5 @@
package com.xydl.cac.transform;
import com.alibaba.excel.util.StringUtils;
import com.xydl.cac.entity.*;
import com.xydl.cac.model.StaticVariable;
import com.xydl.cac.model.i2sync.Attr;
@ -9,12 +8,10 @@ import com.xydl.cac.model.i2sync.Monitordata;
import com.xydl.cac.model.i2sync.Request;
import com.xydl.cac.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.*;
@Component
@Slf4j
@ -34,23 +31,42 @@ public class I2DataTransformer {
}
}
LinkedHashMap<String, List<I2syncField>> fieldMap = this.sortFieldList(fieldList);
List<Datanode> nodeList = new ArrayList<>();
for (Map<String, Object> map : dataList) {
String date = (String) map.get("acquisitionTime");
Datanode node = new Datanode();
node.setSensorid(sensor.getSensorCode());
node.setEquipmentid(sensor.getEquipmentId());
node.setTimestamp(date);
List<Attr> attrs = new ArrayList<>();
Attr attr = new Attr();
attr.setName("Phase");
attr.setValue(sensor.getPhase());
attr.setAlarm("FALSE");
attrs.add(attr);
for (I2syncField field : fieldList) {
Object value = map.get(field.getFieldName());
attr = new Attr();
Iterator<String> fit = fieldMap.keySet().iterator();
while (fit.hasNext()) {
String attach = fit.next();
List<I2syncField> fieldListNew = fieldMap.get(attach);
Datanode node = this.buildDatanode(sensor, config.getTypeCode(), attach,
fieldListNew, map, ruleList);
if (node != null) {
nodeList.add(node);
record.setLastDTime(DateUtil.parse(node.getTimestamp()));
}
}
}
Monitordata monitordata = new Monitordata();
monitordata.setCacid(sensor.getSensorCode());
monitordata.setDatanodenum(nodeList.size());
monitordata.setDatanode(nodeList);
Request request = new Request();
request.setMonitordata(monitordata);
return request;
}
private Datanode buildDatanode(NSensor sensor, String typeCode, String attach,
List<I2syncField> fieldList, Map<String, Object> dataMap,
List<WarnRule> ruleList) {
List<Attr> attrs = new ArrayList<>();
for (I2syncField field : fieldList) {
Object value = dataMap.get(field.getFieldName());
if (value != null) {
Attr attr = new Attr();
attr.setName(field.getDestFieldName());
attr.setValue(String.valueOf(value));
if (this.triggerRule(ruleList, field.getFieldName(), value)) {
@ -58,24 +74,63 @@ public class I2DataTransformer {
} else {
attr.setAlarm("FALSE");
}
if (StringUtils.isBlank(node.getType())) {
node.setType(config.getTypeCode());
}
attrs.add(attr);
}
node.setAttr(attrs);
}
if (attrs.size() < 1) {
return null;
}
String date = (String) dataMap.get("acquisitionTime");
Datanode node = new Datanode();
node.setType(typeCode);
node.setTimestamp(date);
node.setAttr(attrs);
nodeList.add(node);
record.setLastDTime(DateUtil.parse(date));
Attr attr = new Attr();
attr.setName("Phase");
attr.setAlarm("FALSE");
attrs.add(attr);
if (StringUtils.isBlank(attach)) {
attr.setValue(sensor.getPhase());
node.setSensorid(sensor.getSensorCode());
node.setEquipmentid(sensor.getEquipmentId());
} else {
attr.setValue("");
String code = sensor.getSensorCode();
node.setSensorid(code);
if (code != null && code.length() > attach.length() + 2) {
int len = code.length() - 2;
String tail = code.substring(len);
String first = code.substring(0, len - attach.length());
node.setSensorid(first + attach + tail);
}
String equipmentid = sensor.getEquipmentId();
node.setEquipmentid(equipmentid);
if (equipmentid != null && equipmentid.length() > attach.length() + 2) {
int len = equipmentid.length() - 2;
String tail = equipmentid.substring(len);
String first = equipmentid.substring(0, len - attach.length());
node.setEquipmentid(first + attach + tail);
}
}
return node;
}
Monitordata monitordata = new Monitordata();
monitordata.setCacid(sensor.getSensorCode());
monitordata.setDatanodenum(nodeList.size());
monitordata.setDatanode(nodeList);
Request request = new Request();
request.setMonitordata(monitordata);
return request;
private LinkedHashMap<String, List<I2syncField>> sortFieldList(List<I2syncField> fieldList) {
LinkedHashMap<String, List<I2syncField>> map = new LinkedHashMap<>();
for (I2syncField item : fieldList) {
String attach = item.getAttach();
if (attach == null) {
attach = "";
}
List<I2syncField> list = map.get(attach);
if (list == null) {
list = new ArrayList<>();
map.put(attach, list);
}
list.add(item);
}
return map;
}
private boolean triggerRule(List<WarnRule> ruleList, String fieldName, Object value) {

@ -1,5 +1,7 @@
package com.xydl.cac.util;
import lombok.extern.slf4j.Slf4j;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
@ -8,6 +10,7 @@ import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
@Slf4j
public class DateUtil {
public final static String defaultDatePattern = "yyyy-MM-dd HH:mm:ss";
public static DateTimeFormatter defaultFormatter = DateTimeFormatter.ofPattern(defaultDatePattern);
@ -109,6 +112,10 @@ public class DateUtil {
Date date = parse(time);
time = format(date);
LocalDateTime localtime = LocalDateTime.parse(time, defaultFormatter);
if (localtime.getYear() < 2000) {
log.warn("该时间小于2000年: " + str);
return null;
}
ZonedDateTime zonedDateTime = localtime.atZone(ZoneOffset.UTC);
ZonedDateTime convertedDateTime = zonedDateTime.withZoneSameInstant(ZoneId.systemDefault());
String result = convertedDateTime.format(defaultFormatter);

Loading…
Cancel
Save