|
@@ -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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|