whyclj
2019-10-23 ed1a24a5bf58e4bc5166e768177946b1cf62ba8b
platforms/android/app/src/main/java/com/socket/MySocketClientThread.java
@@ -44,23 +44,28 @@
    private ByteBuffer bytebuffer_for_socket_RX = ByteBuffer.allocate(1500);
    public Handler mHandler = null;
    public static Socket socket;
    public static Socket socket = null;
    private Date lastConTime;                       //上一次通讯成功的时间
    private boolean thread_run_flag = true;
    public boolean isRuning = false;
    ServiceModel returnmodel = new ServiceModel();
    public MySocketClientThread(String server_ip){
    private static MySocketClientThread thread ;
    private MySocketClientThread(String server_ip){
        this.server_ip = server_ip;
        battData = new BattRTData();
        testParam = new BattTestParam();
        testRtData = new BattTestRtData();
        new Thread(new Runnable() {
            @Override
            public void run() {
                reConnectDevice();
            }
        }).start();
    }
    public static MySocketClientThread createThread(String server_ip){
        if(thread == null){
           thread = new MySocketClientThread(server_ip);
        }
        return thread;
    }
    /*********************************************************************************************/
@@ -69,7 +74,7 @@
        //初始化消息循环队列,需要在Handler创建之前
        Looper.prepare();
        isRuning = true;
        mHandler = new Handler()
        {
            public void handleMessage(Message msg)
@@ -112,7 +117,15 @@
        Log.e(TAG, "Loop start at "+ Com.getDateTimeFormat(new Date(),Com.DTF_YMDhms) );
        //启动子线程消息循环队列
        Looper.loop();
        isRuning = false;
        Log.e(TAG, "Loop stop at "+ Com.getDateTimeFormat(new Date(),Com.DTF_YMDhms) );
        if(socket != null){
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
@@ -130,6 +143,10 @@
//            socket = new Socket();
//            SocketAddress socAddress = new InetSocketAddress(server_ip, server_port);
//            socket.connect(socAddress, 2000);
            if(socket == null || socket.isClosed()){
                Log.e(TAG, "SocketComm:!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " );
                reConnectDevice();
            }
            if(this.socket != null)
            {
                in = this.socket.getInputStream();
@@ -233,6 +250,7 @@
                    res_t = false;
                    model.code = 0;
                }
                thread_run_flag = true;
            } else {
                //System.out.println(this.getName() + "-------socket  异常关闭-------" + Com.getNowTimeWithAt());
                thread_run_flag = false;
@@ -240,20 +258,6 @@
        } catch (IOException | InterruptedException e) {
            thread_run_flag = false;
        } finally {
            if(in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(!thread_run_flag){
                reConnectDevice();
            }
@@ -278,6 +282,12 @@
            Gson gson = new Gson();
            BattTestParam param = gson.fromJson(model.msg,BattTestParam.class);
            buffer = param.getByteBuffer();
        }else if(FBS_ComBase.CMD_RETURNMAIN == model.cmd){
            buffer = ByteBuffer.allocate(1);
            buffer.order(ByteOrder.LITTLE_ENDIAN);
            buffer.position(0);
            buffer.put(FBS_ComBase.changeIntToByte(Integer.parseInt(model.msg)));
            buffer.flip();
        }
        return buffer;
    }
@@ -333,6 +343,7 @@
        if(true == m_FBS_Cmd.putByteBuffer(bf))
        {
            model.errcode = m_FBS_Cmd.RecState;
            lastConTime = new Date();
            //---------------------  心跳包测试    ----------------------------------
@@ -364,7 +375,8 @@
            else if(FBS_ComBase.CMD_CHANGEBATT == m_FBS_Cmd.CMD) {
                if(FBS_ComBase.RETURN_SUCCESS == m_FBS_Cmd.RecState) {
                    isSuccess = true;
                    //System.out.println("切换电池组成功");
                    Log.e(TAG, "getDataFromCommBuf: 切换电池组成功");
                }
            }
@@ -387,8 +399,9 @@
            }
            //---------------------  获取放电参数    ----------------------------------------------
            else if(FBS_ComBase.CMD_GETDISCHARGEPARAM == m_FBS_Cmd.CMD) {
                Log.e(TAG, "getDataFromCommBuf: 获取放电参数" );
                if(testParam.putByteBuffer(bf)) {
                    //System.out.println("获取放电参数成功");
                    System.out.println("获取放电参数成功");
                    //System.out.println(testParam);
                    isSuccess = true;
                    model.setData(testParam);
@@ -477,31 +490,72 @@
                    System.out.println("进入Android模式成功");
                }
            }
            //---------------------  进入界面同步切换   ----------------------------------
            else if(FBS_ComBase.CMD_RETURNMAIN == m_FBS_Cmd.CMD) {
                if(FBS_ComBase.RETURN_SUCCESS == m_FBS_Cmd.RecState) {
                    isSuccess = true;
                    System.out.println("切换界面成功");
                }
            }
            //---------------------  退出上位机控制   ----------------------------------
            else if(FBS_ComBase.CMD_EXITMACHINECONTROL == m_FBS_Cmd.CMD) {
                if(FBS_ComBase.RETURN_SUCCESS == m_FBS_Cmd.RecState) {
                    isSuccess = true;
                    System.out.println("退出上位机控制");
                }
            }
        }
        return isSuccess;
    }
    public boolean setUpDeviceIp(String server_ip){
        boolean flag  = false;
        synchronized (socket){
            if (server_ip != null && server_ip.length() > 0){
                flag = reConnectDevice();
        if(this.socket == null){
            flag = reConnectDevice();
        }else{
            synchronized (socket){
                if (this.server_ip != null && server_ip.length() > 0){
                    flag = reConnectDevice();
                }
            }
        }
        return false;
    }
    //关闭socket连接
    public void closeConn(){
        if(this.socket != null){
            try {
                this.socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //测试当前ip是否可以连接
    public boolean testSockConn(String server_ip){
        boolean flag = false;
        Socket socket1 = null;
        try {
            Socket socket1 = new Socket();
            socket1 = new Socket();
            Log.e(TAG, "testSockConn: "+server_ip );
            SocketAddress socAddress = new InetSocketAddress(server_ip, this.server_port);
            socket1.connect(socAddress, 3000);
            flag = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
           if(socket1 != null){
               try {
                   socket1.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
        }
        return flag;
    }