package com.xixun.communicate;

import com.xixun.communicate.Protocol.NET_COMMU_TYPE;
import com.xixun.display.MyLog;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2018/01/22.
 *
 *
 Y卡：
 fpga： /dev/s3c2410_serial2
 sensor： /dev/s3c2410_serial1
 GPS/RF： /dev/s3c2410_serial3
 ----------------------------------------------
 E卡：
 /dev/ttyMT1

 */

public class Uart {
    LedCardCommunication ledCardCommunication=null;

    SerialPort serialPort=null;
    FileOutputStream mOutputStream=null;
    FileInputStream mInputStream=null;

    String strUartDeviceName=null;
    int iBaudRate=0;
    byte ucCommandType= NET_COMMU_TYPE.NONE.getValue();

    public Uart(LedCardCommunication ledCardCommunication,String strUartDeviceName,int iBaudRate)
    {
        this.ledCardCommunication=ledCardCommunication;
        this.strUartDeviceName=strUartDeviceName;
        this.iBaudRate=iBaudRate;


        if (strUartDeviceName==LedCardCommunication.serialDeviceName_Sensor) ucCommandType=NET_COMMU_TYPE.UART_SENSOR.getValue();
        if (strUartDeviceName==LedCardCommunication.serialDeviceName_Fpga) ucCommandType=NET_COMMU_TYPE.UART_FPGA.getValue();
        if (strUartDeviceName==LedCardCommunication.serialDeviceName_Extention) ucCommandType=NET_COMMU_TYPE.UART_EXTENSION.getValue();

        //android_serialport_api.SerialPortFinder serialPortFinder=new android_serialport_api.SerialPortFinder();
        //serialPortFinder.getAllDevices();
        new Thread() {
            public void run() {
                Process();
            }
        }.start();
    }


    void Process()
    {
        try {
            serialPort=new SerialPort(new File(strUartDeviceName),iBaudRate,0);//"/dev/ttySAC0" // "/dev/ttyMT3" // "/dev/s3c2410_serial3"
            if (serialPort==null)
            {   Stop();
                return;
            }

            mOutputStream=(FileOutputStream) serialPort.getOutputStream();
            mInputStream=(FileInputStream) serialPort.getInputStream();

            bReady=true;

        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        int iLength=0;
        byte[] DataBuffer =new byte[1024*8];

        while (NeedExit()==false) {
            if (mInputStream==null) break;

            try {
                iLength=mInputStream.read(DataBuffer);
                if (iLength==0) continue;
                if (iLength<0)
                {
                    Stop();
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            ReceDataProcess(DataBuffer,iLength);

            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Stop();
    }

    void ReceDataProcess(byte[] DataBuffer,int iLength)
    {
        ledCardCommunication.clientsManage.TransData_Byte(null,ucCommandType,(byte)0,(byte)0,DataBuffer,iLength);
        MyLog.i("Rece DataBuffer: "+DataBuffer[0]);
    }

    public void TransData_Byte(byte ucCommand,  byte ucIdence, byte ucPipeId,byte[] DataBuffer, int iLength)
    {   if (NeedExit()) return;
        if (GetReady()==false) return;

        try {
            mOutputStream.write(DataBuffer,0,iLength);
            mOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void Stop()
    {
        try {
            bReady=false;

            if (mInputStream!=null) mInputStream.close();
            if (mOutputStream!=null) mOutputStream.close();
            if (serialPort!=null) serialPort.close();

        } catch (Exception e) {}
        if (NeedExit()==false)
            MyLog.i(strUartDeviceName+ " "+ Thread.currentThread().getStackTrace()[2].getFileName() + "-->"
                    +   "Function: " +Thread.currentThread().getStackTrace()[2].getMethodName()
            );
    }

    boolean bReady=false;
    public  boolean GetReady() { return bReady; }

    Boolean bNeedExit=false;
    boolean NeedExit() { return bNeedExit; }

    public void Exit()
    {    if (bNeedExit) return;
        bNeedExit=true;

        Stop();
    }

}
