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 文件名称 
 | 
     */ 
 | 
    @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; 
 | 
    } 
 | 
  
 | 
  
 | 
  
 | 
} 
 |