hfxc226 2 жил өмнө
parent
commit
b2d48430bb

+ 228 - 232
platform-iec/src/main/java/com/ydl/iec/iec104/core/ScheduledTaskPool.java

@@ -15,242 +15,238 @@ import java.time.LocalTime;
 
 
 /**
- *  这是一个定时任务管理池
- * @ClassName:  ScheduledTaskPool   
- * @Description: 
+ * 这是一个定时任务管理池
+ *
+ * @ClassName: ScheduledTaskPool
+ * @Description:
  * @author: YDL
- * @date:   2020年5月19日 上午10:47:15
+ * @date: 2020年5月19日 上午10:47:15
  */
 @Slf4j
 public class ScheduledTaskPool {
 
-	private static final Logger LOGGER = LoggerFactory.getLogger(ChannelInboundHandlerAdapter.class);
-
-	/**
-	 * 发送指令
-	 */
-	private ChannelHandlerContext ctx;
-	/**
-	 * 发送启动指令的线程
-	 */
-	private Thread sendStatrThread;
-	/**
-	 * 循环发送启动指令线程的状态
-	 */
-	private Boolean sendStatrStatus = false;
-	/**
-	 * 发送测试指令的线程 类似心跳
-	 */
-	private Thread sendTestThread;
-	/**
-	 * 发送测试指令线程的状态
-	 */
-	private Boolean sendTestStatus = false;
-	/**
-	 * 发送测试指令线程的状态-累计数量,超过十次没响应后,需要重启监听
-	 */
-	private int sendTestNum = 0;
-	/**
-	 * 发送总召唤指令状态
-	 */
-	private Boolean senGeneralCallStatus = false;
-	/**
-	 * 发送总召唤指令状态
-	 */
-	private Boolean senGeneralCallStatusDegree = false;
-	/**
-	 * 启动指令收到确认后固定时间内发送总召唤指令
-	 */
-	private Thread generalCallTThread;
-	/**
-	 * 启动指令收到确认后固定时间内发送电度总召唤指令
-	 */
-	private Thread generalCallTThreadDegree;
-
-	public Thread getGeneralCallTThread(){
-		return generalCallTThread;
-	}
-
-	public Thread getGeneralCallTThreadDegree(){
-		return generalCallTThreadDegree;
-	}
-
-	public ScheduledTaskPool(ChannelHandlerContext ctx) {
-		this.ctx = ctx;
-	}
-
-	/**
-	 *
-	* @Title: sendStatrFrame
-	* @Description: 发送启动帧
-	 */
-	public void sendStartFrame() {
-		synchronized (sendStatrStatus) {
-			if (sendStatrThread != null) {
-				sendStatrStatus = true;
-				sendStatrThread.start();
-			} else if (sendStatrThread == null) {
-				sendStatrStatus = true;
-				sendStatrThread = new Thread(new Runnable() {
-					@Override
-					public void run() {
-						while (sendStatrStatus) {
-							try {
-								ctx.channel().writeAndFlush(BasicInstruction104.STARTDT);
-								LOGGER.info("发送启动链路指令");
-								Thread.sleep(5000);
-							} catch (Exception e) {
-								e.printStackTrace();
-							}
-						}
-					}
-				});
-				sendStatrThread.start();
-			}
-		}
-	}
-
-	/**
-	 *
-	* @Title: stopSendStatrFrame
-	* @Description: 停止发送确认帧
-	 */
-	public void stopSendStatrFrame() {
-		if (sendStatrThread != null) {
-			sendStatrStatus = false;
-		}
-	}
-	
-	/**
-	 * 
-	* @Title: sendTestFrame
-	* @Description: 发送测试帧
-	 */
-	public void sendTestFrame() {
-		synchronized (sendTestStatus) {
-			if (sendTestThread != null && sendTestThread.getState() == Thread.State.TERMINATED) {
-				sendTestStatus = true;
-				sendTestThread.start();
-			} else if (sendTestThread == null) {
-				sendTestStatus = true;
-				sendTestThread = new Thread(() -> {
-					while (sendTestStatus) {
-						try {
-							LOGGER.info("发送测试链路指令,次数:" + sendTestNum);
-							ctx.channel().writeAndFlush(BasicInstruction104.TESTFR);
-							sendTestNum++;
-							Thread.sleep(5000);
-						} catch (Exception e) {
-							e.printStackTrace();
-						}
-					}
-				});
-				sendTestThread.start();
-			}
-		}
-	}
-	
-	/**
-	 *
-	* @Title: stopSendTestFrame
-	* @Description: 停止发送测试帧
-	 */
-	public void stopSendTestFrame() {
-		if (sendTestThread != null) {
-			LOGGER.info("停止发送测试帧");
-			sendTestStatus = false;
-			sendTestNum = 0;
-		}
-	}
-
-	/**
-	 *
-	* @Title: sendGeneralCall
-	* @Description: 发送总召唤
-	 */
-	public void sendGeneralCall() {
-		synchronized (senGeneralCallStatus) {
-			if (generalCallTThread != null && generalCallTThread.getState() == Thread.State.TERMINATED) {
-				senGeneralCallStatus = true;
-				generalCallTThread.start();
-			} else if (generalCallTThread == null) {
-				senGeneralCallStatus = true;
-				generalCallTThread = new Thread(() -> {
-					LOGGER.info("sendTestStatus: " + sendTestStatus);
-					// 每次启动发起一次总召唤,后面不发了,后面只发电度的
-					LOGGER.info("总召唤指令" );
-					if(sendTestNum == 1){// 第一次发生总召唤
-						ctx.channel().writeAndFlush(BasicInstruction104.getGeneralCallRuleDetail104());
-					}
-					while (sendTestStatus) {
-						try {
-							// ctx.channel().writeAndFlush(BasicInstruction104.getGeneralCallRuleDetail104());
-							Thread.sleep(1000 * 60 * 2);// 2分钟判断一次是否发送一次电度总召唤
-							// 判断是否需要发送电度
-							//if(isNeedSendDegree()){
-							if(sendTestNum > 1){
-								LOGGER.info("电度召唤指令" );
-								ctx.channel().writeAndFlush(BasicInstruction104.getGeneralCallRuleDetail104Degree());
-							}
-							//}
-						} catch (Exception e) {
-							e.printStackTrace();
-						}
-					}
-				});
-				generalCallTThread.start();
-			}
-		}
-	}
-
-	/**
-	 * 判断是否需要发送电度召唤
-	 * 每两分钟发起一次,判断时间是否在0,3分内,在此期间内,必然发起一次
-	 * @return
-	 */
-	private static boolean isNeedSendDegree() {
-		LocalTime time = LocalTime.now();
-		int minute = time.getMinute();
-		System.out.println(minute);
-		if(minute>=0 && minute<=3){
-			return true;
-		}
-		return false;
-	}
-
-	public static void main(String[] args) {
-		// MessageDetail messageDetail = BasicInstruction104.getGeneralCallRuleDetail104();
-		// System.out.println(JSON.toJSONString(messageDetail).getBytes());
-		System.out.println(isNeedSendDegree());
-	}
-
-	/**
-	 *
-	 * @Title: sendGeneralCall
-	 * @Description: 发送电度总召唤
-	 */
-	public void sendGeneralCallDegree() {
-		synchronized (senGeneralCallStatusDegree) {
-			if (generalCallTThreadDegree != null && generalCallTThreadDegree.getState() == Thread.State.TERMINATED) {
-				senGeneralCallStatusDegree = true;
-				generalCallTThreadDegree.start();
-			} else if (generalCallTThreadDegree == null) {
-				senGeneralCallStatusDegree = true;
-				generalCallTThreadDegree = new Thread(() -> {
-					LOGGER.error("sendTestStatus: " + sendTestStatus);
-					while (sendTestStatus) {
-						try {
-							LOGGER.error("发送电度总召唤指令");
-							ctx.channel().writeAndFlush(BasicInstruction104.getGeneralCallRuleDetail104Degree());
-							Thread.sleep(1000 * 60 * 15);// 15分钟
-						} catch (Exception e) {
-							e.printStackTrace();
-						}
-					}
-				});
-				generalCallTThreadDegree.start();
-			}
-		}
-	}
+    private static final Logger LOGGER = LoggerFactory.getLogger(ChannelInboundHandlerAdapter.class);
+
+    /**
+     * 发送指令
+     */
+    private ChannelHandlerContext ctx;
+    /**
+     * 发送启动指令的线程
+     */
+    private Thread sendStatrThread;
+    /**
+     * 循环发送启动指令线程的状态
+     */
+    private Boolean sendStatrStatus = false;
+    /**
+     * 发送测试指令的线程 类似心跳
+     */
+    private Thread sendTestThread;
+    /**
+     * 发送测试指令线程的状态
+     */
+    private Boolean sendTestStatus = false;
+    /**
+     * 发送测试指令线程的状态-累计数量,超过十次没响应后,需要重启监听
+     */
+    private int sendTestNum = 0;
+    /**
+     * 发送总召唤指令状态
+     */
+    private Boolean senGeneralCallStatus = false;
+    /**
+     * 发送总召唤指令状态
+     */
+    private Boolean senGeneralCallStatusDegree = false;
+    /**
+     * 启动指令收到确认后固定时间内发送总召唤指令
+     */
+    private Thread generalCallTThread;
+    /**
+     * 启动指令收到确认后固定时间内发送电度总召唤指令
+     */
+    private Thread generalCallTThreadDegree;
+
+    public Thread getGeneralCallTThread() {
+        return generalCallTThread;
+    }
+
+    public Thread getGeneralCallTThreadDegree() {
+        return generalCallTThreadDegree;
+    }
+
+    public ScheduledTaskPool(ChannelHandlerContext ctx) {
+        this.ctx = ctx;
+    }
+
+    /**
+     * @Title: sendStatrFrame
+     * @Description: 发送启动帧
+     */
+    public void sendStartFrame() {
+        synchronized (sendStatrStatus) {
+            if (sendStatrThread != null) {
+                sendStatrStatus = true;
+                sendStatrThread.start();
+            } else if (sendStatrThread == null) {
+                sendStatrStatus = true;
+                sendStatrThread = new Thread(new Runnable() {
+                    @Override
+                    public void run() {
+                        while (sendStatrStatus) {
+                            try {
+                                ctx.channel().writeAndFlush(BasicInstruction104.STARTDT);
+                                LOGGER.info("发送启动链路指令");
+                                Thread.sleep(5000);
+                            } catch (Exception e) {
+                                e.printStackTrace();
+                            }
+                        }
+                    }
+                });
+                sendStatrThread.start();
+            }
+        }
+    }
+
+    /**
+     * @Title: stopSendStatrFrame
+     * @Description: 停止发送确认帧
+     */
+    public void stopSendStatrFrame() {
+        if (sendStatrThread != null) {
+            sendStatrStatus = false;
+        }
+    }
+
+    /**
+     * @Title: sendTestFrame
+     * @Description: 发送测试帧
+     */
+    public void sendTestFrame() {
+        synchronized (sendTestStatus) {
+            if (sendTestThread != null && sendTestThread.getState() == Thread.State.TERMINATED) {
+                sendTestStatus = true;
+                sendTestThread.start();
+            } else if (sendTestThread == null) {
+                sendTestStatus = true;
+                sendTestThread = new Thread(() -> {
+                    while (sendTestStatus) {
+                        try {
+                            LOGGER.info("发送测试链路指令,次数:" + sendTestNum);
+                            ctx.channel().writeAndFlush(BasicInstruction104.TESTFR);
+                            sendTestNum++;
+                            Thread.sleep(5000);
+                        } catch (Exception e) {
+                            e.printStackTrace();
+                        }
+                    }
+                });
+                sendTestThread.start();
+            }
+        }
+    }
+
+    /**
+     * @Title: stopSendTestFrame
+     * @Description: 停止发送测试帧
+     */
+    public void stopSendTestFrame() {
+        if (sendTestThread != null) {
+            LOGGER.info("停止发送测试帧");
+            sendTestStatus = false;
+            sendTestNum = 0;
+        }
+    }
+
+    /**
+     * @Title: sendGeneralCall
+     * @Description: 发送总召唤
+     */
+    public void sendGeneralCall() {
+        synchronized (senGeneralCallStatus) {
+            if (generalCallTThread != null && generalCallTThread.getState() == Thread.State.TERMINATED) {
+                senGeneralCallStatus = true;
+                generalCallTThread.start();
+            } else if (generalCallTThread == null) {
+                senGeneralCallStatus = true;
+                generalCallTThread = new Thread(() -> {
+                    LOGGER.info("sendTestStatus: " + sendTestStatus);
+                    // 每次启动发起一次总召唤,后面不发了,后面只发电度的
+                    LOGGER.info("总召唤指令");
+                    if (sendTestNum == 1) {// 第一次发生总召唤
+                        ctx.channel().writeAndFlush(BasicInstruction104.getGeneralCallRuleDetail104());
+                    }
+                    while (sendTestStatus) {
+                        try {
+                            // ctx.channel().writeAndFlush(BasicInstruction104.getGeneralCallRuleDetail104());
+                            Thread.sleep(1000 * 60 * 2);// 2分钟判断一次是否发送一次电度总召唤
+                            // 判断是否需要发送电度
+                            if (isNeedSendDegree()) {
+                                if (sendTestNum > 1) {
+                                    LOGGER.info("电度召唤指令");
+                                    ctx.channel().writeAndFlush(BasicInstruction104.getGeneralCallRuleDetail104Degree());
+                                }
+                            }
+                        } catch (Exception e) {
+                            e.printStackTrace();
+                        }
+                    }
+                });
+                generalCallTThread.start();
+            }
+        }
+    }
+
+    /**
+     * 判断是否需要发送电度召唤
+     * 每两分钟发起一次,判断时间是否在0,3分内,在此期间内,必然发起一次
+     *
+     * @return
+     */
+    private static boolean isNeedSendDegree() {
+        LocalTime time = LocalTime.now();
+        int minute = time.getMinute();
+        System.out.println(minute);
+        if (minute >= 0 && minute <= 3) {
+            return true;
+        }
+        return false;
+    }
+
+    public static void main(String[] args) {
+        // MessageDetail messageDetail = BasicInstruction104.getGeneralCallRuleDetail104();
+        // System.out.println(JSON.toJSONString(messageDetail).getBytes());
+        System.out.println(isNeedSendDegree());
+    }
+
+    /**
+     * @Title: sendGeneralCall
+     * @Description: 发送电度总召唤
+     */
+    public void sendGeneralCallDegree() {
+        synchronized (senGeneralCallStatusDegree) {
+            if (generalCallTThreadDegree != null && generalCallTThreadDegree.getState() == Thread.State.TERMINATED) {
+                senGeneralCallStatusDegree = true;
+                generalCallTThreadDegree.start();
+            } else if (generalCallTThreadDegree == null) {
+                senGeneralCallStatusDegree = true;
+                generalCallTThreadDegree = new Thread(() -> {
+                    LOGGER.error("sendTestStatus: " + sendTestStatus);
+                    while (sendTestStatus) {
+                        try {
+                            LOGGER.error("发送电度总召唤指令");
+                            ctx.channel().writeAndFlush(BasicInstruction104.getGeneralCallRuleDetail104Degree());
+                            Thread.sleep(1000 * 60 * 15);// 15分钟
+                        } catch (Exception e) {
+                            e.printStackTrace();
+                        }
+                    }
+                });
+                generalCallTThreadDegree.start();
+            }
+        }
+    }
 
 }

+ 3 - 1
platform-iec/src/main/java/com/ydl/iec/util/MySqlUtil.java

@@ -220,7 +220,9 @@ public class MySqlUtil {
         bytes[3] = 0;
         messageInfo.setMessageInfos(bytes);
         list.add(messageInfo);
-        addBatch(list, time, year, month, day, hour, minute);
+        // addBatch(list, time, year, month, day, hour, minute);
+
+        System.out.println(ByteUtil.byteArrayToInt(messageInfo.getMessageInfos()));
     }
 
     /**