package org.bench4q.monitor.service;

import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.ExecutionException;
import java.text.SimpleDateFormat;
import java.io.*;
import org.apache.log4j.Logger;
import org.bench4q.monitor.model.*;
import org.hyperic.sigar.SigarException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class WriteSystemInfoToLocalDisk {
	private SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd-HH-mm-ss");
	private String savePath;
	private Logger logger = Logger.getLogger(WriteSystemInfoToLocalDisk.class);

	public static void main(String[] args) throws SigarException,
			InterruptedException, ExecutionException, IOException {
		WriteSystemInfoToLocalDisk testWrite = new WriteSystemInfoToLocalDisk();
		testWrite.setSavaPath("D:/sigartmp/");
		testWrite.writeCurrentSystemInfoToLocalDisk();
	}

	public void setSavaPath(String savePath) {
		this.savePath = savePath;
	}

	public void writeCurrentSystemInfoToLocalDisk() throws SigarException,
			InterruptedException, ExecutionException, IOException {
		dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		MonitorMain mainModel = new MonitorMain();
		FileWriter writer = null;
		try {
			writer = new FileWriter(savePath + dateFormat.format(new Date())
					+ ".json");
			Gson gson = new GsonBuilder()
					.excludeFieldsWithoutExposeAnnotation().create();
			gson.toJson(mainModel, writer);
		} catch (Exception e) {
			logger.error(e, e.fillInStackTrace());
		} finally {
			writer.close();
		}
	}
}