package br;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
import java.util.concurrent.Delayed;

import java.awt.*;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

public class TwoWaySerialComm
{	
    public TwoWaySerialComm()
    {
        super();
    }
    
    //adicionado parametro "Mouse ms" a funcao "connect"
    void connect ( String portName) throws Exception
    {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if ( portIdentifier.isCurrentlyOwned() )
        {
            System.out.println("Erro: Porta esta em uso!");
        }
        else
        {
            CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
            
            if ( commPort instanceof SerialPort )
            {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                
                InputStream in = serialPort.getInputStream();
                //OutputStream out = serialPort.getOutputStream();
                //adicionado parametro "Mouse ms" a funcao "SerialReader"
                (new Thread(new SerialReader(in))).start();
                //(new Thread(new SerialWriter(out))).start();
            }
            else
            {
                System.out.println("Erro: Somente portas seriais so suportadas por este exemplo!");
            }
        }     
    }
    
    /** */
    //adicionado parametro "Mouse ms" a "SerialReader" 
    public static class SerialReader implements Runnable 
    {
        InputStream in;
        //Mouse ms = new Mouse("n");
        AutoVetores tela = new AutoVetores("Anlise");
        String dir = "";
        
        public SerialReader ( InputStream in )
        {
            this.in = in;          
            
        }
        
        public void run ()
        {
            byte[] buffer = new byte[1024];
            int len = -1;
            
            
            try
            {
                while ( ( len = this.in.read(buffer)) > -1 )
                {
                	dir = new String(buffer, 0, len);
                	
                	while (( len = this.in.read(buffer)) > 0 ){
                		dir += new String(buffer, 0, len);
                		//System.out.println("entrou");
                	}
                	if (dir.length() > 0){
                		//System.out.println(dir);
                		if (dir.equals("f")){
                			tela.mostrarTela();
                			tela.addValueXYseries(0, true);
                		}else{
                			tela.addValueXYseries(Float.parseFloat(dir), false);
                		}
                		dir = "";
                	}
                	
                	/*try{
                    	ms.moveMouse(dir);
                    }catch(AWTException e){
                    	e.printStackTrace();
                    }*/
                }
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }            
        }
    }

    /** */
    public static class SerialWriter implements Runnable 
    {
        OutputStream out;
        
        public SerialWriter ( OutputStream out )
        {
            this.out = out;
        }
        
        public void run ()
        {
            try
            {                
                int c = 0;
                while ( ( c = System.in.read()) > -1 )
                {
                    this.out.write(c);
                }                
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }            
        }
    }
}
