From d5dab07dc674de7edbbc1bd6ade0486f54fbdb26 Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期二, 06 五月 2025 19:19:35 +0800
Subject: [PATCH] 1、实现万用token(0000-0000-1234-9876-5); 2、web端单独实现命令结果等待器,并修改相关部分; 3、web端实现透传命令; 4、修改一些不当注释; 5、优化一些代码。
---
pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/resource/rtuLog/RtuLogManager.java | 244 ++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 244 insertions(+), 0 deletions(-)
diff --git a/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/resource/rtuLog/RtuLogManager.java b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/resource/rtuLog/RtuLogManager.java
new file mode 100644
index 0000000..ae3b050
--- /dev/null
+++ b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/resource/rtuLog/RtuLogManager.java
@@ -0,0 +1,244 @@
+package com.dy.rtuMw.resource.rtuLog;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+
+import com.dy.rtuMw.resource.ResourceUnit;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import com.dy.common.util.DateTime;
+import com.dy.common.queue.Queue;
+import com.dy.common.threadPool.ThreadPool;
+import com.dy.common.threadPool.TreadPoolFactory;
+
+public class RtuLogManager {
+
+ private static final Logger log = LogManager.getLogger(RtuLogManager.class) ;
+
+ private static final RtuLogManager instance = new RtuLogManager() ;
+
+ private static final Queue logQueue = new Queue("rtuLogQueue") ;
+
+ private static final Integer maxNodeAddWorkThread = 500 ;//缂撳瓨杈惧埌杩欎釜鏁伴噺锛屽皢澧炲姞绾跨▼
+ private static final Integer maxWorkThread = 5 ;//鏈�澶у伐浣滅嚎绋嬫暟
+ private static final Object maxWorkThreadSynObj = new Object() ;//鏈�澶у伐浣滅嚎绋嬫暟
+ private static int workThread = 0 ;//宸ヤ綔绾跨▼鏁�
+
+ private RtuLogManager(){
+ }
+ @SuppressWarnings("unused")
+ public static RtuLogManager getInstance(){
+ return instance ;
+ }
+
+ /**
+ * 璁剧疆闃熷垪闄愬埗
+ * @param warnSize 闃熷垪鑺傜偣鏁伴噺 鎶ヨ鍊�
+ * @param maxSize 闃熷垪鑺傜偣鏁伴噺 鏈�澶у��
+ */
+ @SuppressWarnings("unused")
+ public void setQueueLimit(int warnSize, int maxSize){
+ logQueue.setLimit(warnSize, maxSize);
+ }
+
+ /**
+ * 鍔犲叆Rtu鏃ュ織鏁版嵁
+ * 鍦ㄥ崟绾跨▼涓墽琛�
+ * @param logNodeObj 鏃ュ織闃熷垪鑺傜偣瀵硅薄
+ * @throws Exception 寮傚父
+ */
+ public void pushRtuLog(RtuLogNode logNodeObj) throws Exception{
+ logQueue.pushTail(logNodeObj);
+ //鍦ㄥ崟绾跨▼涓墽琛�
+ if(workThread <= 0){
+ startWorkThread() ;
+ }
+ }
+
+ /**
+ * 鍚姩宸ヤ綔绾跨▼宸ヤ綔
+ */
+ private void startWorkThread(){
+ workThread ++ ;
+ try {
+ TreadPoolFactory.getThreadPoolShort().putJob(new ThreadPool.Job() {
+ public void execute() {
+ while(logQueue.size() > 0){
+ RtuLogNode node ;
+ if(workThread > 1){
+ synchronized(logQueue){
+ node = (RtuLogNode)logQueue.pop() ;
+ }
+ }else{
+ node = (RtuLogNode)logQueue.pop() ;
+ }
+ if(node != null){
+ //鏃ュ織鏂囦欢瀛樺偍
+ log(node.rtuAddr, node.content) ;
+ }
+
+ if(RtuLogManager.logQueue.size() > maxNodeAddWorkThread){
+ if(workThread < maxWorkThread){
+ synchronized(maxWorkThreadSynObj){
+ if(workThread < maxWorkThread){
+ startWorkThread() ;
+ }
+ }
+ }
+ }
+ }
+ workThread -- ;
+ if(workThread < 0){
+ workThread = 0 ;
+ }
+ }
+
+
+ /*
+ * 寮哄埗鍋滄execute鏂规硶涓墽琛岀殑姝诲惊鐜伐浣�
+ */
+ @Override
+ public void destroy() {
+ //鍐嶅皢鍚姩锛岄槻姝㈡棤绾跨▼澶勭悊浜�
+ startWorkThread() ;
+ }
+
+ /**
+ * execute鏂规硶涓皟鐢ㄦ鏂规硶鍒ゆ柇鏄惁缁撴潫姝诲惊鐜伐浣�
+ */
+ @Override
+ public boolean isDestroy() {
+ return false;
+ }
+ }) ;
+ }catch(Exception e){
+ log.error(e);
+ workThread -- ;
+ if(workThread < 0){
+ workThread = 0 ;
+ }
+ }
+ }
+
+ /**
+ * 璁板綍Rtu鏃ュ織
+ * @param rtuAddr 鎺у埗鍣ㄥ湴鍧�
+ * @param content 鏃ュ織鍐呭
+ */
+ private void log(String rtuAddr , String content){
+ Object[] res = getFileForWrite(ResourceUnit.confVo.rtuLogDir , rtuAddr.trim()) ;
+ if(res[0] != null){
+ writeLog((File)res[0] , DateTime.yyyy_MM_dd_HH_mm_ss() + " " + content) ;
+ }else{
+ log.error("涓嶈兘寰楀埌鍦板潃涓�:" + rtuAddr + "鐨凴tu鏃ュ織鏂囦欢锛侊紙淇℃伅缂栫爜" + res[1] + "锛�") ;
+ }
+ }
+
+ /**
+ * 寰楀埌鏃ュ織鏂囦欢
+ * @param path 鏂囦欢璺緞
+ * @param rtuAddr 鎺у埗鍣ㄥ湴鍧�
+ * @return 鏃ュ織鏂囦欢
+ */
+ private Object[] getFileForWrite(String path , String rtuAddr) {
+ File dir = new File(path) ;
+ if(!dir.exists()){
+ if(!dir.mkdirs()){
+ return new Object[]{null, 1} ;
+ }
+ }
+ File f = new File(path + rtuAddr + ".log");
+ FileInputStream in = null;
+ try {
+ if (f.exists()) {
+ in = new FileInputStream(f);
+ if(in.available() >= ResourceUnit.confVo.rtuLogFileMaxSize){
+ in.close() ;//蹇呴』鍔犱笂姝よ鍙�
+ File oldestLog = new File(path + rtuAddr + ".log." + (ResourceUnit.confVo.rtuLogFileMaxCount - 1)) ;
+ if(oldestLog.exists()){
+ if(!oldestLog.delete()){
+ return new Object[]{null, 2} ;
+ }
+ }
+ for(int i = (ResourceUnit.confVo.rtuLogFileMaxCount - 2) ; i > 0 ; i--){
+ File oldLog = new File(path + rtuAddr + ".log." + i) ;
+ if(oldLog.exists()){
+ if(!oldLog.renameTo(new File(path + rtuAddr + ".log." + (i + 1)))){
+ return new Object[]{null, 3} ;
+ }
+ if(!oldLog.delete()){
+ return new Object[]{null, 4} ;
+ }
+ }
+ }
+ if(!f.renameTo(new File(path + rtuAddr + ".log." + 1))){
+ return new Object[]{null, 5} ;
+ }
+ //2024-10-11 涓婇潰rename浜嗭紝搴旇涓嶇敤delete浜�
+ //if(!f.delete()){
+ // return new Object[]{null, 6} ;
+ //}
+
+ f = new File(path + rtuAddr + ".log");
+ if(!f.exists()){
+ if(f.createNewFile()){
+ return new Object[]{f, 7} ;
+ }else{
+ return new Object[]{null, 8} ;
+ }
+ }
+ }else{
+ return new Object[]{f, 9} ;
+ }
+ }else{
+ if(f.createNewFile()){
+ return new Object[]{f, 10} ;
+ }else{
+ return new Object[]{null, 11} ;
+ }
+ }
+ } catch (Exception e) {
+ log.error("寰楀埌Rtu(" + rtuAddr + ")鏃ュ織鏂囦欢鏃跺嚭閿�" , e) ;
+ }finally{
+ try {
+ if (in != null) {
+ in.close();
+ }
+ } catch (IOException e) {
+ log.error("鍏抽棴Rtu鏃ュ織鏂囦欢(" + f.getName() + ")璇诲叆娴佹椂鍑洪敊" , e) ;
+ }
+ }
+ return new Object[]{null, 12} ;
+ }
+
+ /**
+ * 鍚戞枃浠朵腑鍐欏叆鏃ュ織
+ * @param f 鏂囦欢
+ * @param conent 鏃ュ織鍐呭
+ */
+ private void writeLog(File f, String conent) {
+ if (f.exists()) {
+ BufferedWriter out = null;
+ try {
+ out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f, true))); // 鍙傛暟涓簍rue锛岃〃绀鸿拷鍔犲唴瀹�
+ out.write(conent + "\n");
+ } catch (Exception e) {
+ log.error("鍐欏叆Rtu鏃ュ織鏂囦欢(" + f.getName() + ")鏃跺嚭閿�" , e) ;
+ } finally {
+ try {
+ if (out != null) {
+ out.close();
+ }
+ } catch (IOException e) {
+ log.error("鍏抽棴Rtu鏃ュ織鏂囦欢(" + f.getName() + ")鍐欏嚭娴佹椂鍑洪敊" , e) ;
+ }
+ }
+ }
+ }
+
+}
--
Gitblit v1.8.0