浏览代码

根据国能宁夏分公司对侧要求,调整了测试信号,召唤增量数据推送的方式

songwenbin 2 年之前
父节点
当前提交
ac71429cef

+ 2 - 0
gdnxfgs/src/main/java/com/gyee/edge/gddlly/config/Iec104Config.java

@@ -19,6 +19,8 @@ public class Iec104Config {
     private int fullPushInterval = 30000;
     //变化推送时间间隔
     private int changedPushInterval = 10000;
+    //心跳信号发送时间间隔
+    private int sendSysFrameInterval = 10000;
 
     //接收到帧的数量到该值就要发一个确认帧
     private short frameAmountMax;

+ 64 - 7
gdnxfgs/src/main/java/com/gyee/edge/gddlly/iec104/Iec104Session.java

@@ -28,6 +28,7 @@ public class Iec104Session {
     private short acceptSeq = 0;    //接收序列号
     private short sendSeq = 0;      //发送序列号
     private boolean isRunning = false;
+    private boolean isStarted = false;
 
     private ChannelHandlerContext channelHandlerContext;
 
@@ -69,7 +70,7 @@ public class Iec104Session {
                 byte fun = request.getControl()[0];
                 switch (fun) {
                     case 0x07: //启动命令
-                        if(state == SessionState.NEW || state == SessionState.INACTIVE)
+                        if(state == SessionState.NEW || state == SessionState.ACTIVE)
                             state = SessionState.START104;
                         acceptSeq = 0;
                         sendSeq = 0;
@@ -88,6 +89,9 @@ public class Iec104Session {
                 }
                 if (response != null)
                     sendMessage(response);
+
+                if (state == SessionState.START104)
+                    this.start();
                     //channelHandlerContext.writeAndFlush(response);
             } else {
                 //acceptSeq = (short)(request.getSendSeq() + 1);
@@ -120,16 +124,32 @@ public class Iec104Session {
     }
 
     public void start() {
+        if (isStarted) {
+            log.warn("104测试帧发送线程已启动!client = " +getId());
+        } else {
+            getTest104Thread().start();
+        }
+
         if (isRunning) {
             log.warn("数据轮询和转发线程已启动!client = " +getId());
-            return;
+        } else {
+            if (publicAddress > 0) {
+                //有意延迟3秒,错开测试帧和数据帧
+                try {
+                    Thread.sleep(3000);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+                getPushThread().start();
+            }
         }
 
-        getPushThread().start();
+
     }
 
     public void stop() {
         isRunning = false;
+        isStarted = false;
     }
 
     private Thread getPushThread() {
@@ -165,10 +185,18 @@ public class Iec104Session {
                             }
                         }
 
-                        if (state == SessionState.CALL_ALL_END)
-                            Thread.sleep(iec104Config.getChangedPushInterval());
-                        else
-                            Thread.sleep(iec104Config.getFullPushInterval());
+                        if (state == SessionState.CALL_ALL) {
+                            state = SessionState.CALL_ALL_END;
+                            //Thread.sleep(iec104Config.getFullPushInterval());
+                        }
+
+                        Thread.sleep(iec104Config.getChangedPushInterval());
+
+
+//                        if (state == SessionState.CALL_ALL_END)
+//                            Thread.sleep(iec104Config.getChangedPushInterval());
+//                        else
+//                            Thread.sleep(iec104Config.getFullPushInterval());
                     }
                 } catch (Exception e) {
                     //todo: 异常处理
@@ -269,4 +297,33 @@ public class Iec104Session {
         channelHandlerContext.writeAndFlush(msg);
     }
 
+
+    private Thread getTest104Thread() {
+        return new Thread(new Runnable() {
+            public void run() {
+                log.info("开始启动104测试信号发送...");
+                try {
+                    isStarted = true;
+                    while(isStarted) {
+                        try {
+                            //间隔发送104测试帧
+                            Iec104Message testMessage = BasicInstruction104.createSysMessage(UControlEnum.TESTFR_YES);
+                            sendMessage(testMessage);
+                            Thread.sleep(iec104Config.getSendSysFrameInterval());
+                        } catch (Exception e) {
+                            log.error(e.getMessage());
+                        }
+                    }
+                } catch (Exception e) {
+                    //todo: 异常处理
+                    log.error(e.getMessage());
+
+                } finally {
+                    isStarted = false;
+                }
+            }
+        });
+    }
+
+
 }

+ 16 - 9
gdnxfgs/src/main/java/com/gyee/edge/gddlly/iec104/handler/Iec104ServerHandler.java

@@ -31,15 +31,7 @@ public class Iec104ServerHandler extends SimpleChannelInboundHandler<Iec104Messa
 
 	@Override
 	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
-		// 清理Session
-		String sessionId = getSessionId(ctx);
-		if (sessionMap.containsKey(sessionId)) {
-			Iec104Session session = sessionMap.get(sessionId);
-			session.stop();
-			Thread.sleep(5000);
-			sessionMap.remove(sessionId);
-		}
-
+		closeSession(ctx);
 	}
 	
 	@Override
@@ -57,6 +49,10 @@ public class Iec104ServerHandler extends SimpleChannelInboundHandler<Iec104Messa
 	@Override
 	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
 		cause.printStackTrace();
+
+		closeSession(ctx);
+
+
 		//todo: 异常处理
 		//ctx.close();
 	}
@@ -67,4 +63,15 @@ public class Iec104ServerHandler extends SimpleChannelInboundHandler<Iec104Messa
 		int clientPort = ipSocket.getPort();
 		return clientIp + ":" + clientPort;
 	}
+
+	private void closeSession(ChannelHandlerContext ctx) throws InterruptedException {
+		// 清理Session
+		String sessionId = getSessionId(ctx);
+		if (sessionMap.containsKey(sessionId)) {
+			Iec104Session session = sessionMap.get(sessionId);
+			session.stop();
+			Thread.sleep(5000);
+			sessionMap.remove(sessionId);
+		}
+	}
 }

+ 1 - 0
gdnxfgs/src/main/resources/application.yaml

@@ -39,6 +39,7 @@ iec104:
   port: 2404
   fullPushInterval: 60000
   changedPushInterval: 10000
+  sendSysFrameInterval: 10000
   frameAiMax: 16
   frameDiMax: 22