/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serial;

import CommonFunctions.debugPrinter;
import java.util.Enumeration;
import mdot.Main;
import sensors.*;
import structures.Constants;

/*class SerialReadThread
 * Reads data from the serial port and calls the senor's calculate function.
 * Puts the result in the hashtable.
 */
public class SerialReadThread extends Thread {

    private static double tickCounter = 1;
    private static double sumMPG = 0;

    public SerialReadThread(String s) {

        super(s);
    }

    @Override
    public void run() {

        // first thing in the thread, we initialize the write operation
        SerialComm scomm = new SerialComm();
        scomm.initializeOutputStream();

        try {
            while (true) {
                // write string to port, the serialEvent will read it

                Enumeration<Sensor> en = SensorVector.sensorVect.elements();
                while (en.hasMoreElements()) {
                    Sensor s = en.nextElement(); //get the sensor
                    scomm.writeToSerialPort(s.getPid());    //get the command to send
                   
                    //wait for data
                    while (scomm.isWaitingforData()) {
                        Thread.sleep(1);            //this is required for linux
                      
                    }

                   
                    //handle the data
                    double result = s.calculate(s, scomm.getCAN_response());

                    if (result != -1) {
                        s.printSensorData(s, result);
                    }

                    scomm.setCAN_response(null);

                }
                //calculate derived values
                double calculatedMPG = 0;
                double averageMPG = 0;
                double MAF, VSS = 0;

                MAF = Main.getMe().GetObjectAsDouble(Constants.HASH_MAF);
                VSS = Main.getMe().GetObjectAsDouble(Constants.HASH_SPEED);

                //if there was an error, reset to 0
                if (MAF == -1 || VSS == -1) {
                    calculatedMPG = 0;
                } else {
                    //14.7 grams of air to 1 gram of gasoline - ideal air/fuel ratio
                    //6.17 pounds per gallon - density of gasoline
                    //454 grams per pound - conversion
                    //3600 seconds per hour conversion
                    calculatedMPG = (14.7 * 6.17 * 454 * VSS) / (3600 * MAF);
                }

                //If VSS is 0 or MAF is 0, calcaultedMPG will be 0.
                //This may not cause an accurate reading because you are using
                //fuel (very small amt) even if you are at an idle (VSS = 0)

                sumMPG += calculatedMPG;
                averageMPG = sumMPG/tickCounter;
                tickCounter++;

                //add it to my hash table
                debugPrinter.printLine("MPG: " + averageMPG);
                Main.getMe().setObjectAsDouble(Constants.HASH_MPG, averageMPG);


                //Sleep for 1 second
                Thread.sleep(Constants.THREAD_SLEEP);
                debugPrinter.printLine("\n");
            }
        } catch (InterruptedException e) {
        }
    }
}
