diff --git a/src/main/java/com/xydl/cac/entity/Iec104Point.java b/src/main/java/com/xydl/cac/entity/Iec104Point.java index e6b930f..4753002 100644 --- a/src/main/java/com/xydl/cac/entity/Iec104Point.java +++ b/src/main/java/com/xydl/cac/entity/Iec104Point.java @@ -10,6 +10,7 @@ import lombok.Data; import lombok.NoArgsConstructor; import javax.persistence.*; +import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import java.util.Date; @@ -28,9 +29,11 @@ public class Iec104Point { @Column(name = "id") private Integer id; + @NotNull(message = "eqmid不能为空") @Column(name = "eqmid") - private String eqmid; + private Integer eqmid; + @NotBlank(message = "字段不能为空") @ApiModelProperty("字段") @Column(name = "field") private String field; diff --git a/src/main/java/com/xydl/cac/iec104/Iec104Client.java b/src/main/java/com/xydl/cac/iec104/Iec104Client.java index e8490de..0b0055f 100644 --- a/src/main/java/com/xydl/cac/iec104/Iec104Client.java +++ b/src/main/java/com/xydl/cac/iec104/Iec104Client.java @@ -74,6 +74,7 @@ public class Iec104Client implements ConnectionEventListener { public void newASdu(Connection connection, ASdu aSdu) { if (aSdu == null) return; + log.info("Got new ASdu, " + aSdu.toString()); InformationObject[] objs = aSdu.getInformationObjects(); if (objs != null && objs.length > 0) { for (InformationObject obj : objs) { @@ -119,7 +120,7 @@ public class Iec104Client implements ConnectionEventListener { public static void main(String[] args) { Iec104Client client = new Iec104Client(null); try { - client.connect("192.168.1.177", 2404); + client.connect("192.168.1.190", 2404); client.interrogation(); while (true) { Thread.sleep(1000); diff --git a/src/main/java/com/xydl/cac/iec104/ServerConnectionEventListener.java b/src/main/java/com/xydl/cac/iec104/ServerConnectionEventListener.java index 7f80b5d..e86eb3b 100644 --- a/src/main/java/com/xydl/cac/iec104/ServerConnectionEventListener.java +++ b/src/main/java/com/xydl/cac/iec104/ServerConnectionEventListener.java @@ -1,11 +1,17 @@ package com.xydl.cac.iec104; +import com.xydl.cac.entity.Iec104Point; +import com.xydl.cac.model.StaticVariable; +import com.xydl.cac.util.DateUtil; import lombok.extern.slf4j.Slf4j; import org.openmuc.j60870.*; import org.openmuc.j60870.ie.*; import java.io.EOFException; import java.io.IOException; +import java.util.Date; +import java.util.HashMap; +import java.util.List; @Slf4j public class ServerConnectionEventListener implements ConnectionEventListener { @@ -20,7 +26,7 @@ public class ServerConnectionEventListener implements ConnectionEventListener { @Override public void newASdu(Connection connection, ASdu aSdu) { - log.debug("Got new ASdu:" + aSdu.toString()); + log.info("Got new ASdu, type=" + aSdu.getTypeIdentification()); InformationObject informationObject = null; try { switch (aSdu.getTypeIdentification()) { @@ -28,14 +34,9 @@ public class ServerConnectionEventListener implements ConnectionEventListener { case C_IC_NA_1: log.debug("Got interrogation command (100). Will send scaled measured values."); connection.sendConfirmation(aSdu); - // example GI response values - connection.send(new ASdu(ASduType.M_ME_NB_1, true, CauseOfTransmission.INTERROGATED_BY_STATION, - false, false, 0, aSdu.getCommonAddress(), - new InformationObject(1, new InformationElement[][]{ - {new IeScaledValue(-32768), new IeQuality(false, false, false, false, false)}, - {new IeScaledValue(10), new IeQuality(false, false, false, false, false)}, - {new IeScaledValue(-5), - new IeQuality(false, false, false, false, false)}}))); + + this.sendAll104(connection); + connection.sendActivationTermination(aSdu); break; case C_SC_NA_1: @@ -76,12 +77,40 @@ public class ServerConnectionEventListener implements ConnectionEventListener { } catch (EOFException e) { log.debug("Will quit listening for commands on connection (" + connectionId, ") because socket was closed."); - } catch (IOException e) { + } catch (Exception e) { log.debug("Will quit listening for commands on connection (" + connectionId, ") because of error: ", e.getMessage()); } } + private void sendAll104(Connection conn) throws Exception { + List list = StaticVariable.point104_Cache; + for (Iec104Point item : list) { + this.sendOne104(conn, item); + } + } + + private void sendOne104(Connection conn, Iec104Point item) throws Exception { + HashMap map = StaticVariable.sensorLastDataMap.get(item.getEqmid()); + String value = map.get(item.getField()); + String time = map.get("acquisitionTime"); + if (value != null && time != null) { + Date date = DateUtil.parse(time); + Float f = Float.valueOf(value); + InformationObject infoObj = new InformationObject(item.getSadr(), new InformationElement[][]{ + {new IeShortFloat(f), + new IeQuality(false, false, false, false, false), + new IeTime56(date.getTime())} + }); + ASdu obj = new ASdu(ASduType.M_ME_TF_1, true, CauseOfTransmission.INTERROGATED_BY_STATION, + false, false, 0, 1, infoObj); + conn.send(obj); + log.info("104服务端发送: sadr=" + item.getSadr() + ", value=" + value + ", time=" + time); + } else { + log.info("数据不全,104服务端不发送: sadr=" + item.getSadr() + ", value=" + value + ", time=" + time); + } + } + @Override public void connectionClosed(Connection connection, IOException e) { diff --git a/src/main/java/com/xydl/cac/model/StaticVariable.java b/src/main/java/com/xydl/cac/model/StaticVariable.java index 3e64788..a67010c 100644 --- a/src/main/java/com/xydl/cac/model/StaticVariable.java +++ b/src/main/java/com/xydl/cac/model/StaticVariable.java @@ -7,6 +7,7 @@ import com.xydl.cac.iec.IecServer; import com.xydl.cac.iec104.Iec104Client; import com.xydl.cac.iec104.Iec104Server; import com.xydl.cac.util.DateUtil; +import org.apache.commons.lang3.StringUtils; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -53,7 +54,7 @@ public class StaticVariable { StaticVariable.sensorLastDataMap.put(eqmid, map); } map.put(colname, value); - if (time != null) { + if (StringUtils.isNotBlank(time)) { map.put("acquisitionTime", time); }