package cc.brino.Brpp.Utils;

/*
 * Copyright (c) 2016 StarFruitBrasil
 * 
 * Permission is hereby granted, free of charge, to any
 * person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the
 * Software without restriction, including without
 * limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software
 * is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice
 * shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
 * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import cc.brino.SerialMonitor.SerialMonitor;


/**
 * 
 * @author Mateus Berardo de Souza Terra
 */
public class CommPortUtils {

	private static volatile boolean isOpen = false;
	private BufferedReader stdout;
	private BufferedWriter stdin;
	private static ProcessBuilder pb;
	private static Process p;
	final Thread ioThread = new Thread() {

		@Override
		public void run() {
			while (isOpen)
				try {
					SerialMonitor.display(String.valueOf((char) stdout.read()));
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	};

	public ArrayList<String> getComPorts() {
		ArrayList<String> enu_ports;
		pb = new ProcessBuilder("bash", "-c",
				("python3 Python/Coms.py"));
		pb.redirectErrorStream(true);
		String c = "";
		try {
			p = pb.start();
			stdout = new BufferedReader(new InputStreamReader(
					p.getInputStream(), "UTF8"));
			c = stdout.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		c = c.replace("[", "");
		c = c.replace("]", "");
		c = c.replace(" ", "");
		c = c.replace("'", "");
		if (!c.equals("")) {
			String[] coms = c.split(",");
			enu_ports = new ArrayList<String>(Arrays.asList(coms));
		} else {
			enu_ports = new ArrayList<String>();
		}
		return enu_ports;
	}

	public boolean openPort(String com) {
		ArrayList<String> ports = getComPorts();
		if (ports.contains(com)) {
			pb = new ProcessBuilder("bash", "-c",
					("python3 Python/Monitor.py " + com));
			pb.redirectErrorStream(true);
			try {
				p = pb.start();
				stdout = new BufferedReader(
						new InputStreamReader(
								p.getInputStream(),
								"UTF8"));
				stdin = new BufferedWriter(
						new OutputStreamWriter(
								p.getOutputStream(),
								"UTF8"));
			} catch (IOException e) {
				// TODO Auto-generated catch
				// block
				e.printStackTrace();
				return false;
			}
			isOpen = true;
			ioThread.start();
			return true;
		}
		return false;
	}

	public void send(String msg) {
		try {
			stdin.write(msg);
			stdin.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void closePort() throws NullPointerException {
		isOpen = false;
		try {
			p.getErrorStream().close();
			p.getInputStream().close();
			p.getOutputStream().close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		p.destroy();
	}
}
@@ -1993,16 +1993,174 @@
 _ports;%0A
+%09%09if (System.getProperty(%22os.name%22).contains(%22Windows%22)) %7B%0A%09%09%09String proc = %22Python/Coms.exe%22;%0A%09%09%09pb = new ProcessBuilder(%22cmd.exe%22, %22/c%22, proc);%0A%09%09%7D else %7B%0A%09
 %09%09pb = n
@@ -2187,24 +2187,25 @@
 ash%22, %22-c%22,%0A
+%09
 %09%09%09%09(%22python
@@ -2225,16 +2225,20 @@
 .py%22));%0A
+%09%09%7D%0A
 %09%09pb.red
@@ -2885,16 +2885,181 @@
 com)) %7B%0A
+%09%09%09if (System.getProperty(%22os.name%22).contains(%22Windows%22)) %7B%0A%09%09%09%09String proc = %22Python/Monitor.exe%22;%0A%09%09%09%09pb = new ProcessBuilder(%22cmd.exe%22, %22/c%22, proc);%0A%09%09%09%7D else %7B%0A%09
 %09%09%09pb = 
@@ -3083,32 +3083,33 @@
 r(%22bash%22, %22-c%22,%0A
+%09
 %09%09%09%09%09(%22python3 P
@@ -3136,16 +3136,21 @@
  com));%0A
+%09%09%09%7D%0A
 %09%09%09pb.re

monitor serial p/ Windows