package sjtukc3c.smallcar.Modules;

import android.util.Log;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * Created by Administrator on 2016/12/20.
 */
public class CommandManager {

    public final static String CMD_FOWARD = "F";
    public final static String CMD_BACK = "B";
    public final static String CMD_LEFT = "L";
    public final static String CMD_RIGHT = "R";


    private static String mSlaveHost = "192.168.31.248";
    private static int mSlavePort = 15536;

    private Socket mSocket;

    public CommandManager(String h, int p){
        setSlaveHost(h);
        setSlavePort(p);
        buildUpConnection();
    }

    public void sendCommand(String cmd){
        switch (cmd){
            case CMD_FOWARD:
            case CMD_BACK:
            case CMD_LEFT:
            case CMD_RIGHT:
                Log.e("Lyy", "Command: " + cmd);
                sendCommandToSlave(cmd);
                break;
            default:
                break;
        }
    }

    private void buildUpConnection(){
        Thread th = new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    mSocket = new Socket(mSlaveHost, mSlavePort);
                    mSocket.sendUrgentData(0);
                } catch (IOException e){

                    e.printStackTrace();
                }
            }
        });
        th.start();
    }

    private void closeConnection(){
        if (mSocket != null){
            try {
                mSocket.close();
            } catch (IOException e){
                e.printStackTrace();
            }
        }
    }


    private void sendCommandToSlave(final String cmd) {
        if (mSocket == null || !mSocket.isConnected()) {
            buildUpConnection();
        }
        Thread th = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    mSocket = new Socket(mSlaveHost, mSlavePort);
                    DataOutputStream writer = new DataOutputStream(mSocket.getOutputStream());
                    writer.writeChars(cmd);
                    writer.flush();
                } catch (IOException e){
                    e.printStackTrace();
                }
            }
        });
        th.start();


    }

    public void setSlaveHost(String newHost){
        if (newHost != null) {
            mSlaveHost = newHost;
        }
    }

    public void setSlavePort(int newPort){
        if (newPort != -1) {
            mSlavePort = newPort;
        }
    }

    public void flush(){
        closeConnection();
        buildUpConnection();
    }
}

@@ -415,16 +415,159 @@
 = %22R%22;%0A%0A
+    private final static int MAX_RECONNECT_AMOUNT = 10;%0A    private static int reconnect_count = 0;%0A%0A    private static boolean mutex = false;%0A
 %0A    pri
@@ -1197,32 +1197,630 @@
 UpConnection()%7B%0A
+        reconnect_count += 1;%0A        if (reconnect_count %3E MAX_RECONNECT_AMOUNT && !mutex)%7B%0A            mutex = true;%0A            Thread th = new Thread(new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A                    try %7B%0A                        Thread.sleep(10000);%0A                        reconnect_count = 0;%0A                        mutex = false;%0A                    %7D catch (InterruptedException e) %7B%0A                        e.printStackTrace();%0A                    %7D%0A                %7D%0A            %7D);%0A            th.start();%0A            return;%0A        %7D%0A
         Thread t
@@ -2035,24 +2035,65 @@
 entData(0);%0A
+                    reconnect_count = 0;%0A
             
@@ -2121,17 +2121,16 @@
 ion e)%7B%0A
-%0A
         

Add reconnect limit on CommandManager