001package ca.bc.webarts.tools.isy;
002
003import java.util.Arrays;
004import java.io.BufferedWriter;
005import java.io.File;
006import java.io.FileNotFoundException;
007import java.io.FileOutputStream;
008import java.io.FileReader;
009import java.io.FileWriter;
010import java.io.FileInputStream;
011import java.io.IOException;
012import java.io.InputStream;
013import java.io.OutputStream;
014
015import ca.bc.webarts.widgets.Util;
016
017
018/**
019  * This class wraps the communication to the REST interface oand queries the iMeterCurrentValue var and logs it to a file.
020  *
021  * @author  Tom B. Gutwin
022  **/
023public class PowerMeterLogger
024{
025  protected static final String CLASSNAME = "ca.bc.webarts.tools.isy.PowerMeterLogger";
026  static String outFilename_ = "./powerLog_"+Util.createCurrentDateStamp()+".txt";
027  //static FileOutputStream fos_ = new FileOutputStream(outFilename_);
028  static String nodeName_ = "iMeter1";
029  static String propName_ = "ST";
030  static String varName_ = "iMeterCurrentValue";
031
032
033  /**
034   * Class main commandLine entry method that has a test command and some convienience commands, as well as a pure rest command.
035   **/
036  public static void main(String [] args)
037  {
038    final String methodName = CLASSNAME + ": main()";
039    ISYRestRequester instance = new ISYRestRequester();
040    String currTimeStamp = Util.createCurrentDateTimeStamp();
041    //fos_ = new FileOutputStream(outFilename);
042    int varVal = 0;
043    String propVal = "0";
044    boolean breakOut = false;
045
046      while (!breakOut)
047      {
048        currTimeStamp = Util.createCurrentDateTimeStamp();
049        try
050        {
051          //varVal = instance.getVarCMD(varName_);
052          propVal = instance.getPropertyValue(nodeName_, propName_);
053          appendToDefaultFile(currTimeStamp,propVal);
054        }
055        catch(Exception ex)
056        {
057          System.out.println("Error retrieving VAR: "+varName_);
058          ex.printStackTrace();
059        }
060
061        Util.sleep(60*1000);
062      }  // while loop
063  } // main
064
065
066  private static boolean appendToDefaultFile(String timeStamp, String s) throws FileNotFoundException
067  {
068    return appendToFile(updateOutFilename(), timeStamp+"|"+s+"\n");
069  }
070
071
072  private static boolean appendToFile(String outFilename, String s) throws FileNotFoundException
073  {
074    BufferedWriter bw = null;
075    FileWriter fw = null;
076    boolean retVal = true;
077
078    try
079    {
080      String data = s;
081      File file = new File(outFilename);
082
083      // if file doesnt exists, then create it
084      if (!file.exists())
085      {
086        file.createNewFile();
087        appendToFile(outFilename, "CurrTimeStamp|Watts\n");
088      }
089
090      fw = new FileWriter(file.getAbsoluteFile(), true); // true = append file
091      bw = new BufferedWriter(fw);
092
093      bw.write(data);
094    }
095    catch (IOException e)
096    {
097      e.printStackTrace();
098        retVal = false;
099    }
100    finally
101    {
102      try
103      {
104        if (bw != null) bw.close();
105        if (fw != null) fw.close();
106        System.gc(); // this is required because a bug in Java won't release
107      }
108      catch (IOException ex)
109      {
110        ex.printStackTrace();
111        retVal = false;
112      }
113    }
114    return retVal;
115  }
116
117
118  private static String updateOutFilename()
119  {
120    outFilename_ = "./powerLog_"+Util.createCurrentDateStamp()+".txt";
121    return outFilename_;
122  }
123}