package com.xixun.communicate;
import com.xixun.communicate.Protocol.Modbus;
import com.xixun.display.MyLog;
import com.xixun.display.MyService;

/**
 * Created by Administrator on 2018/06/08.
 */

public class SerialExtension {
    LedCardCommunication ledCardCommunication=null;

    public AidlSerialPort aidlSerialPort=null;

    Modbus modbus=null;


    SerialExtension(LedCardCommunication ledCardCommunication)
    {   this.ledCardCommunication=ledCardCommunication;

        new Thread() {
            public void run() {
                Process();
            }
        }.start();
    }

    void Process()
    {
        try {
            MyLog.i("12SerialExtension-》Process");
            modbus = new Modbus((byte)0x01,new IFunctionCallBack() {
                @Override
                public void LastSendDataProcess(byte[] DataBuffer, int iLength) { // 协议包数据最终发送。数据打包完后最终发送
                    TransProcess(DataBuffer,iLength);
                }
                @Override
                public void LastReceDataProcess(byte[] DataBuffer, int iLength) { // 协议包数据最终接收。数据解包完后最终处理。
                    ReceProcess();
                }
            });
            MyLog.i("------------------AidlSerialPort");
            aidlSerialPort = new AidlSerialPort(ledCardCommunication, LedCardCommunication.serialDeviceName_Extention, 9600);
            bReady=true;

            aidlSerialPort.SetCallFunc(new IFunctionCallBack() {
                @Override
                public void LastSendDataProcess(byte[] DataBuffer, int iLength) { // AIDL 裸数据发送。暂时不用。
                    TransProcess(DataBuffer,iLength);
                }

                @Override
                public void LastReceDataProcess(byte[] DataBuffer, int iLength) { // AIDL 裸数据接收。每个字节都会调用
                    if (NeedExit()) return;
                    if (GetReady()==false) return;

                    if (DataBuffer==null)
                    {
                        ledCardCommunication.Request_Exit();
                        return;
                    }

                    if (iLength<=0)
                    {
                        ledCardCommunication.Request_Exit();
                        return;
                    }

                    //MyLog.i("iLength= "+iLength);
                    //if (false)
                    for (int i=0;i<iLength;i++ )
                    {
                        //MyLog.i("" + Integer.toHexString(DataBuffer[i] & 0xFF) + " ");
                        modbus.ReceFrame(DataBuffer[i]);
                    }
                }
            });
        } catch (Exception e) {
            MyLog.i(" " + Thread.currentThread().getStackTrace()[2].getFileName() + "-->"
                    +   "Function: " +Thread.currentThread().getStackTrace()[2].getMethodName() + "-->"
                    +   "Line" +Thread.currentThread().getStackTrace()[2].getLineNumber() + ":\r\n"
                    +   "    Error:" + e.getMessage() + "\r\n\r\n"
            );
            ledCardCommunication.Request_Exit();
        }

        while (NeedExit()==false){
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                ledCardCommunication.Request_Exit();
                break;
            }
        }
    }


    public void ReceProcess() {
        int iLength = 0;
        if (NeedExit()) return;
        if (GetReady()==false) return;

        if (ledCardCommunication==null) return;
        if (ledCardCommunication.clientsManage==null) return;
        if (ledCardCommunication.clientsManage.ms==null) return;

        MyLog.i("modbus.ucCommandType="+modbus.ucCommandType);

        switch (modbus.ucCommandType)
        {
            case Modbus.MODBUS_COMMAND_CARRIAGE_NUMBER:
                ledCardCommunication.clientsManage.ms.workParams.iDirection=modbus.ReceBuffer[0];
                ledCardCommunication.clientsManage.ms.workParams.iCarriageNumber=modbus.ReceBuffer[1];
                break;
            case Modbus.MODBUS_COMMAND_TRAIN_NUMBER:
                if (ledCardCommunication.clientsManage.ms.workParams.strStationNumber==null) return;
                ledCardCommunication.clientsManage.ms.workParams.strStationNumber=
                        new String(modbus.ReceBuffer,0,modbus.iReceLength-1);
                break;
            default:
                break;
        }
        ledCardCommunication.clientsManage.ms.workParams.pushBaseParams();
        ledCardCommunication.clientsManage.ms.SendMessage(MyService.MSG_CHANGE_NONE,"");
    }

    void TransProcess(byte[] DataBuffer, int iLength) {
        if (NeedExit()) return;
        if (GetReady() == false) return;
        if (aidlSerialPort == null) return;

        aidlSerialPort.TransData_Byte(DataBuffer,iLength);
    }


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

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

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

        if (aidlSerialPort != null) aidlSerialPort.Exit();
        aidlSerialPort=null;

        MyLog.i(" "+ Thread.currentThread().getStackTrace()[2].getFileName() + "-->"
                +   "Function: " +Thread.currentThread().getStackTrace()[2].getMethodName() + "-->"
                +   "Line" +Thread.currentThread().getStackTrace()[2].getLineNumber() + ":\r\n"
        );
    }
}
