wuzeyu
2024-08-28 1e567af8bbdda31ce3342cd41bb40157168b3da9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.dy.rtuMw.resource;
 
import java.io.File;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.TreeMap;
 
import com.dy.rtuMw.resource.rtuLog.RtuLogNode;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext;
 
import com.dy.common.mw.UnitAdapterInterface;
import com.dy.common.mw.UnitInterface;
import com.dy.common.mw.UnitStartedCallbackInterface;
import com.dy.rtuMw.resource.rtuLog.RtuLogManager;
import com.dy.common.util.DateTime;
 
public class ResourceUnit implements UnitInterface {
 
    private static final Logger log = LogManager.getLogger(ResourceUnit.class) ;
    
    private static final ResourceUnit instance = new ResourceUnit() ;
    
    //private static SupportUnit supUnit ;
    public static ResourceUnitAdapter adapter ;
    public static ResourceUnitConfigVo confVo ;
    
    public static ApplicationContext springContext ;
 
    private ResourceUnit(){}
 
    @SuppressWarnings("unused")
    public static ResourceUnit getInstance(){
        return instance ;
    }
    
    @Override
    public void setAdapter(UnitAdapterInterface adapter) throws Exception {
        if(adapter == null){
            throw new Exception("资源模块适配器对象不能为空!") ;
        }
        ResourceUnit.adapter = (ResourceUnitAdapter)adapter ; 
        ResourceUnit.confVo = ResourceUnit.adapter.getConfig() ;
        if(ResourceUnit.confVo == null){
            throw new Exception("资源模块配置对象不能为空!") ;
        }
    }
    
    /*
     * 设置支持单元
     * @param supUnit
    public void setSupportUnit(SupportUnit supUnit){
        ResourceUnit.supUnit = supUnit ;
    }*/
    
    /**
     * 设置spring上下文
     * @param springContext Spring容器上下文对象
     */
    @SuppressWarnings("unused")
    public void setSpringContext(ApplicationContext springContext) {
        ResourceUnit.springContext = springContext ;
    }
    @Override
    public void start(UnitStartedCallbackInterface callback) throws Exception {
        if(ResourceUnit.springContext == null){
            throw new Exception("Spring上下文对象未设置!") ;
        }
        callback.call(null) ;
        System.out.println("资源模块成功启动");
    }
 
    @Override
    public void stop(UnitStartedCallbackInterface callback) {
    }
 
    /**
     * 记录Rtu日志
     * @param node 日志队列中的节点对象
     */
    @SuppressWarnings("unused")
    public void rtuLog(RtuLogNode node){
        if(node != null && node.rtuAddr != null && node.content != null){
            try {
                RtuLogManager.getInstance().pushRtuLog(node);
            } catch (Exception e) {
                log.error(e);
            }
        }
    }
 
    
    /**
     * 得到Rtu所有日志文件
     * @param rtuAddr 控制器地址
     * @return 文件列表(文件名称,文件大小,文件最后修改时间)
     */
    @SuppressWarnings("unused")
    public TreeMap<String, String[]> listLogFilesName(String rtuAddr){
        File f = new File(ResourceUnit.confVo.rtuLogDir) ;
        TreeMap<String, String[]> map = new TreeMap<>() ;
        if(f.isDirectory()){
            File[] fs = f.listFiles() ;
            if(fs != null && fs.length > 0){
                String fileName ;
                for(File ff : fs){
                    if(ff != null){
                        fileName = ff.getName() ;
                        if(fileName.startsWith(rtuAddr)){
                            String[] ss = new String[3] ;
                            ss[0] = ff.getName() ;
                            ss[1] = formatFileSize(ff.length()) ;
                            ss[2] = DateTime.yyyy_MM_dd_HH_mm_ss(new Date(ff.lastModified())) ;
                            map.put(ss[0], ss) ;
                        }
                    }
                }
            }
        }
        return map ;
    }
    
    /**
     * 得到一个日志文件
     * @param fileName 文件名称([rtuAddr].log)
     */
    @SuppressWarnings("unused")
    public File getLogFile(String fileName){
        return new File(ResourceUnit.confVo.rtuLogDir + fileName);
    }
 
    private String formatFileSize(long fileS) {//通过File的 length()方法数值传入
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString;
        if (fileS < 1024) {
            fileSizeString = df.format((double) fileS) + " B";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) fileS / 1024) + " K";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) fileS / 1048576) + " M";
        } else {
            fileSizeString = df.format((double) fileS / 1073741824) + " G";
        }
        return fileSizeString;
    }
 
 
 
}