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

import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import structures.Constants;

/* class SerialComm
 * Sets up ODB serial communication.
 *
 */
public class SerialComm {

    private static InputStream inputStream;
    private static OutputStream outputStream;
    private static SerialPort serialPort;
    private static boolean waitingforData = false;
    private static byte[] CAN_response = null;

    /**
     * @return the inputStream
     */
    public static InputStream getInputStream() {
        return inputStream;
    }

    /**
     * @param aInputStream the inputStream to set
     */
    public static void setInputStream(InputStream aInputStream) {
        inputStream = aInputStream;
    }

    /**
     * @return the outputStream
     */
    public static OutputStream getOutputStream() {
        return outputStream;
    }

    /**
     * @param aOutputStream the outputStream to set
     */
    public static void setOutputStream(OutputStream aOutputStream) {
        outputStream = aOutputStream;
    }

    /**
     * @return the serialPort
     */
    public static SerialPort getSerialPort() {
        return serialPort;
    }

    /**
     * @param aSerialPort the serialPort to set
     */
    public static void setSerialPort(SerialPort aSerialPort) {
        serialPort = aSerialPort;
    }

    /**
     * @return the waitingforData
     */
    public static boolean isWaitingforData() {
        return waitingforData;
    }

    /**
     * @param aWaitingforData the waitingforData to set
     */
    public static void setWaitingforData(boolean aWaitingforData) {
        waitingforData = aWaitingforData;
    }

    /**
     * @return the CAN_response
     */
    public static byte[] getCAN_response() {
        return CAN_response;
    }

    /**
     * @param aCAN_response the CAN_response to set
     */
    public static void setCAN_response(byte[] aCAN_response) {
        CAN_response = aCAN_response;
    }

    public void writeToSerialPort(String cmd) {


        try {
            // write string to serial port
            //System.out.println("COMMAND: " + cmd);
            outputStream.write(cmd.getBytes());

            SerialComm.setWaitingforData(true);
            waitingforData = true;

        } catch (IOException e) {
        }

    }
    ;

    public void initializeOutputStream() {

        try {
            // get the outputstream
            outputStream = serialPort.getOutputStream();


        } catch (IOException e) {
        }

        try {
        } catch (Exception e) {
            System.out.println("Error setting event notification");
            System.out.println(e.toString());
            System.exit(-1);
        }

    }

    public int setupSerialPort(String port, String baud) {


        boolean portFound = false;
        CommPortIdentifier portId = null;
        Enumeration portList;

        // parse ports and if the default port is found, initialized the reader
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {

                if (portId.getName().equals(port)) {
                    portFound = true;
                    break;
                }
            }

        }
        if (portFound) { //we found the port we specified


            try { //open the serial port
                serialPort = (SerialPort) portId.open("OBDComm", 2000);
            } catch (PortInUseException e) {
                System.out.println(e.toString());
            }

            try { //get the input stream
                inputStream = serialPort.getInputStream();
            } catch (IOException e) {
                System.out.println(e.toString());
            }

            try {//add listener for data
                serialPort.addEventListener(new serial.SerialPortListener());

            } catch (TooManyListenersException e) {
                System.out.println(e.toString());
            }

            // activate the DATA_AVAILABLE notifier
            serialPort.notifyOnDataAvailable(true);

            try {
                // set port parameters
                serialPort.setSerialPortParams(Integer.parseInt(baud), Constants.DATA_BITS_8,
                        Constants.STOP_BITS_1,
                        Constants.PARITY_NONE);

                serialPort.setFlowControlMode(Constants.FLOW_CONTROL_NONE);
            } catch (UnsupportedCommOperationException e) {
                System.out.println(e.toString());
            }

            return 0;
        } else {
            System.out.println("port " + Constants.COM_PORT + " not found.");
            return -1;
        }
    }
    ;
}
