import com.medcosm.primitives.GraphData;
import com.medcosm.datalogger.ImporterInterface;
import java.util.GregorianCalendar;

import java.io.*;
import java.util.StringTokenizer;

/**
 * Simple demonstration class to show how automated datalogging can be performed on the MedCosm
 * DataLogger with almost no effort.  This class records the disk free space from
 * the main drive.  Unfortunately, for now, determining free space on disk requires
 * some sort of native call (JNI, native class, or Runtime.exec()), so this Class
 * is, as coded, specific to the North American version of MicroSoft Windows (yeeaach!).
 *
 * But hopefully this demonstrates the concept of an Importer class which directly
 * reads some loggable data for use by the DataLogger.
 * Could be uptime, number of users, hits/day, data from your proprietary widget counter,
 * or anything else you might dream up!
 *
 * <p>Title: DataLogger</p>
 * <p>Description: Generic Data Logging Package</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: MedCosm</p>
 * @author Alan Stein
 * @version 1.0
 */
class DiskFreeImporter implements ImporterInterface
{
  GraphData readData(String fileName)
  {
    GraphData graphData=new GraphData();   //Instantiate a new GraphData object

    GregorianCalendar gc=new GregorianCalendar();

    String timeStampStr=""+gc.get(GregorianCalendar.YEAR);
    timeStampStr=timeStampStr+zeroPad(gc.get(GregorianCalendar.MONTH)+1,2);
    timeStampStr=timeStampStr+zeroPad(gc.get(GregorianCalendar.DATE),2);
    timeStampStr=timeStampStr+zeroPad(gc.get(GregorianCalendar.HOUR_OF_DAY),2);
    timeStampStr=timeStampStr+zeroPad(gc.get(GregorianCalendar.MINUTE),2);

    long tStamp=Long.parseLong(timeStampStr);

    String comment="Disk Usage";

    //call "dir c:" to get num bytes free
    BufferedInputStream bis=Runtime.getRuntime().exec("cmd /c dir c:").getInputStream();
//    BufferedInputStream bis=Runtime.getRuntime().exec("df -k").getInputStream;    //something like this for Unix
//  alternately, could use the JConfig library from http://www.tolstoy.com/samizdat/jconfig.html for more Java-like library

    //get output of command
    BufferedReader in = new BufferedReader(new InputStreamReader(bis));
    String line;
    while((line = in.readLine()) != null)
    {
      if(line.indexOf("bytes free")!= -1)
        break;
    }
    int idx0=line.indexOf("(s)");
    int idx1=line.indexOf("bytes free");
    String bFreeStr=line.substring(idx0+3, idx1);   //parse out number of byte in "10 Dir(s)  123,456,789 bytes free" output

    int i=0;
    do
    {
      if(bFreeStr.charAt(i)==',')    //delete any commas
        bFreeStr=bFreeStr.substring(0,i)+bFreeStr.substring(i+1, bFreeStr.length());
      i++;
    }while(i<bFreeStr.length());

    long bFree=Long.parseLong(bFreeStr.trim());

    System.out.println("Disk Free: "+line+" ("+bFree+")");

    System.out.println("Datapoint:  x="+tStamp+" y="+bFree+" comment="+comment);
    graphData.addVal(tStamp, (double)bFree, comment);    //add our new data value to the GraphData object

    return graphData;   //return the GraphData object for use in the MedCosm DataLogger
  }



  String zeroPad(int value, int charLength)
  {
    String retStr=String.valueOf(value);
    retStr=new String("0000000000000000").substring(0,charLength-retStr.length())+retStr;
    return retStr;
  }

}