package ir.ac.aut.ceit.aolab.jlamp.rpi.serial;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
 * Created by iman on 5/9/16.
 */
public class Serial {

	private PrintWriter printWriter;
	private BufferedReader bufferedReader;
	private static Serial serialInstance;
	private static final Logger LOG = LoggerFactory.getLogger(Serial.class);


	private Serial() {
		super();
		serialInstance = this;
	}

	public static Serial getSerialInstance() {
		if (serialInstance == null)
			new Serial();
		return serialInstance;
	}


	public void connect(String portName) throws Exception {
		CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
		LOG.info("Found the " + portName + " port.");
		if (portIdentifier.isCurrentlyOwned()) {
			LOG.error("Port is currently in use");
		} else {
			CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
			LOG.info("Opened port " + portName);

			if (commPort instanceof SerialPort) {
				SerialPort serialPort = (SerialPort) commPort;
				serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.PARITY_EVEN, SerialPort.FLOWCONTROL_NONE);

				printWriter = new PrintWriter(serialPort.getOutputStream());
				bufferedReader = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));

			} else {
				LOG.error("Only serial ports are handled by this example.");
			}
		}
	}

	public char readFromInput() {
		try {
			return (char) bufferedReader.read();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public void write(String string) {
		printWriter.write(string);
		printWriter.flush();
	}

	public void close() throws IOException {
		printWriter.close();
		bufferedReader.close();
	}

}

@@ -1624,17 +1624,12 @@
 read
-FromInput
+Char
 () %7B
@@ -1712,46 +1712,284 @@
 %0A%09%09%09
-e.printStackTrace();%0A%09%09%7D%0A%09%09return 0;%0A%09
+LOG.error(%22Failed to read a char.%22);%0A%09%09%7D%0A%09%09return 0;%0A%09%7D%0A%0A    public String readLine() %7B%0A        try %7B%0A            return (String) bufferedReader.readLine();%0A        %7D catch (IOException e) %7B%0A            LOG.error(%22Failed to read a line.%22);%0A        %7D%0A        return null;%0A%0A    
 %7D%0A%0A%09
@@ -2174,10 +2174,67 @@
 ();%0A%09%7D%0A%0A
+    public void skipInput() %7B%0A        readLine();%0A    %7D%0A%0A
 %7D%0A

Generates an skip input function