package cn.myapps.runtime.system.monitor.util;

import cn.myapps.runtime.system.monitor.model.SystemMonitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Arrays;
import java.util.List;


public class SystemMonitorUtil {
	private static final Logger LOG = LoggerFactory.getLogger(SystemMonitorUtil.class);
	
	public static void writeFile(SystemMonitor monitor1, String filePath) {
		try {
			String toStr=monitor1.toString()+", ";
			File systemMonitorFile=new File(filePath);
			if(!systemMonitorFile.exists()) systemMonitorFile.createNewFile();
			FileWriter fw=new FileWriter(filePath,true);
			
			fw.write(toStr);
			fw.flush();
			fw.close();
		} catch (IOException e) {
			LOG.error("ERROR FILE:" + filePath);
			e.printStackTrace();
		}
		
	}
	
	public static List<String> readFile(String filePath){
		List<String> lists=null;
		try {
			File fileSystemMonitor=new File(filePath);
			if(!fileSystemMonitor.exists()){
				fileSystemMonitor.createNewFile();
			}
			BufferedReader br=new BufferedReader(new FileReader(filePath));
			StringBuffer stbrAll=new StringBuffer();
			StringBuffer stbr=new StringBuffer();
			stbrAll.append("[");
			char[] c=new char[1024*1024];
			int num;
			for(;;){
				if((num=br.read(c))!=-1){
					
					stbr.append(new String(c,0,num));
				}else{
					break;
				}
			}
			String str=stbr.toString();
			String[] strs=str.split(", ");
			lists=Arrays.asList(strs);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return lists;
	}
}
