package com.teravation.labjack.demo;

/**
 * This is a demonstration of the LabJackJava API, printing the current/max/min temperatures and humidity readings
 * from the temperature probe.
 *
 * <p />Copyright &copy; 2003 <a href="http://www.teravation.com">Teravation</a>. All rights reserved.
 *
 * @created (2/8/2003 1:40:51 AM)
 * @version 2.0
 * @author 
 */
import com.teravation.labjack.*;
public class LabJackEnvironmentalDemo {
    private int delayMs = 5000;

    private float minTempF = Float.MAX_VALUE;
    private float minTempC = Float.MAX_VALUE;
    private float minHumidity = Float.MAX_VALUE;

    private float maxTempF = Float.MIN_VALUE;
    private float maxTempC = Float.MIN_VALUE;
    private float maxHumidity = Float.MIN_VALUE;
    
    /**
     * LabJackEnvironmentalDemo constructor (empty).
     */
    private LabJackEnvironmentalDemo() {
        super();
    }
    
    /**
     * LabJackEnvironmentalDemo constructor (specifies delay period)
     *
     * @created (2/8/2003 1:42:46 AM)
     * @param delayMs the number of milliseconds to delay during each loop
     */
    public LabJackEnvironmentalDemo(int delayMs) {
        super();
        this.setDelayMs(delayMs);
    }
    
    /**
     * Returns the current loop delay value.
     *
     * @created (2/8/2003 1:41:49 AM)
     * @return the current loop delay value.
     */
    private int getDelayMs() {
        return delayMs;
    }
    
    /**
     * Insert the method's description here.
     * @created (2/8/2003 1:41:05 AM)
     * @param args java.lang.String[]
     */
    public static void main(String[] args) {
        int delay = 60000;
        if (args.length > 0) {
            try {
                delay = Integer.parseInt(args[0]);
            } catch (NumberFormatException nfe) {
                System.out.println("Invalid delay...using delay of " + delay + "ms");
            }
            if (delay < 0) {
                System.out.println("Invalid delay...using delay of " + delay + "ms");
            } else {
                System.out.println("Delay = " + delay + "ms");
            }
        }
        LabJackEnvironmentalDemo demo = new LabJackEnvironmentalDemo(delay);
        demo.run();
    }
    
    /**
     * The main demo application--it uses the first labjack found and
     * prints the current/maximum/minimum temperature and humidity readings
     * every <code>delayMs</code> milliseconds.
     *
     * @created (2/8/2003 1:41:22 AM)
     */
    public void run() {
        LabJack[] labjacks = null;
        try {
            // Get the list of all available LabJacks
            labjacks = new LabJackFactory().getLabJacks();

            if (labjacks == null || labjacks.length == 0) {
                // If there are no LabJacks found, print a message and exit
                System.out.println("No LabJacks found!");
                System.exit(0);
            }
            // Use the first labjack found
            LabJack lj = labjacks[0];
            System.out.println("Using " + lj);

            while (true) {
                // Update the LabJack object with all of the environmental information from the device
                lj.updateEnvironmentals();

                // Set the minimums if the current readings are lower
                this.minTempC = Math.min(this.minTempC, lj.getTempC());
                this.minTempF = Math.min(this.minTempF, lj.getTempF());
                this.minHumidity = Math.min(this.minHumidity, lj.getHumidity());

                // Set the maximums if the current readings are higher
                this.maxTempC = Math.max(this.maxTempC, lj.getTempC());
                this.maxTempF = Math.max(this.maxTempF, lj.getTempF());
                this.maxHumidity = Math.max(this.maxHumidity, lj.getHumidity());

                System.out.println();
                // Print out the current date/time
                System.out.println(new java.util.Date());
                // Print out the current/max//min information
                System.out.println("Current:  TempF = " + lj.getTempF() + ", TempC = " + lj.getTempC() + ", RH = " + lj.getHumidity());
                System.out.println("    Max:  TempF = " + maxTempF + ", TempC = " + maxTempC + ", RH = " + maxHumidity);
                System.out.println("    Min:  TempF = " + minTempF + ", TempC = " + minTempC + ", RH = " + minHumidity);
                // Sleep for the specified period of time
                try {
                    Thread.sleep(this.getDelayMs());
                } catch (InterruptedException ie) {
                }
            }
        } catch (LabJackException lje) {
            // If an exception is found, print the stack trace and exit
            lje.printStackTrace(System.err);
        }
    }
    
    /**
     * Sets the number of milliseconds to delay during each loop.
     * 
     * @created (2/8/2003 1:41:49 AM)
     * @param newDelayMs the number of milliseconds to delay during each loop.
     */
    private void setDelayMs(int newDelayMs) {
        delayMs = newDelayMs;
    }
}