package com.example.demo;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresPermission;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.demo.R;
import com.example.administrator.demo.RootShellCmd;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import android_serialport_api.SerialPort;
import android_serialport_api.hyperlcd.LogInterceptorSerialPort;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView textView, getSerialport3;
    SerialPort SerialPortttyMT1, mSerialPort3;
    RootShellCmd rootShellCmd = new RootShellCmd();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initSerial();
        SerialPortttyMT1.setReadThread(new LogInterceptorSerialPort() {
            @Override
            public void log(final String port, final String log) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText(log +"\n");
                    }
                });
            }
        },200);

        mSerialPort3.setReadThread(new LogInterceptorSerialPort() {
            @Override
            public void log(final String port, final String log) {
                Log.d("SerialPortLog",log.toString());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        getSerialport3.setText(log +"\n");
                    }
                });
            }
        },200);
    }


    private void initSerial() {
        try {
            SerialPortttyMT1 = new SerialPort("/dev/ttyMT1", 9600, 0);
            rootShellCmd.setRs485RevMode();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            mSerialPort3 = new SerialPort("/dev/ttyMT0", 9600, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void initView() {
        findViewById(R.id.send1).setOnClickListener(this);
        findViewById(R.id.send3).setOnClickListener(this);
        findViewById(R.id.showNavBar).setOnClickListener(this);
        findViewById(R.id.hideNavBar).setOnClickListener(this);

        textView = (TextView) findViewById(R.id.textView);
        getSerialport3 = (TextView) findViewById(R.id.get3);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.send1:
                try {
                    String str = "test";
                    rootShellCmd.setRs485SendMode();
                    SerialPortttyMT1.write(str,200);
                    rootShellCmd.setRs485RevMode();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case R.id.send3:
                try {
                    String str = "test";
                    rootShellCmd.setRs485RevMode();
                    mSerialPort3.write(str,200);
                    Log.e("login activity", "File not found h2: ");

                } catch (Exception e) {
                    Log.e("login activity", "File not found h1: " + e.toString());
                    e.printStackTrace();
                }
                break;
            case R.id.showNavBar:
                rootShellCmd.showNavigation();
                break;
            case R.id.hideNavBar:
                    rootShellCmd.hideNavigation();
                break;
        }
    }






}
