[fix]优化工控机重连功能,主动断开的连接不发送failedMsg
如果ipcConnectionStatus==Constants.IPC_CONNECTION_STATUS.DISCONNECTED&&failedMsg==null 表示主动断开连接
This commit is contained in:
@@ -9,6 +9,14 @@ import java.lang.ref.WeakReference;
|
||||
|
||||
public abstract class BaseActivity extends AppCompatActivity {
|
||||
private BaseHandler mBaseHandler;
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (getHandler() != null)
|
||||
getHandler().removeCallbacksAndMessages(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化一个Handler,如果需要使用Handler,先调用此方法,
|
||||
* 然后可以使用postRunnable(Runnable runnable),
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.zhidao.adas.client.bean;
|
||||
|
||||
import androidx.annotation.ColorRes;
|
||||
|
||||
public class IPCConnectState {
|
||||
public final String status;
|
||||
@ColorRes
|
||||
public final int color;
|
||||
|
||||
public IPCConnectState(String status, @ColorRes int color) {
|
||||
this.status = status;
|
||||
this.color = color;
|
||||
}
|
||||
}
|
||||
@@ -4,25 +4,15 @@ import android.os.Environment;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
|
||||
import com.zhidao.support.adas.high.common.CupidLogUtils;
|
||||
import com.zhidao.support.adas.high.common.ThreadPoolManager;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.LineNumberReader;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
@@ -32,21 +22,18 @@ import java.util.concurrent.LinkedBlockingQueue;
|
||||
public class LogSave {
|
||||
private static final String TAG = LogSave.class.getSimpleName();
|
||||
private static final String ROOT_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "IPCMonitoring" + File.separator;//程序外部存储跟目录
|
||||
private static final String LOG_FILE_NAME = "data%s.log";//文件名称
|
||||
private static final String LOG_FILE_NAME = "%s.log";//文件名称
|
||||
private volatile static LogSave INSTANCE;
|
||||
private static final long MAX_CAPACITY = 5 * 1024 * 1024L;//单文件最大存储容量 kb
|
||||
private static final long MAX_CAPACITY = 20 * 1024 * 1024L;//单文件最大存储容量 kb
|
||||
private final LinkedBlockingQueue<String> queue;
|
||||
private BufferedWriter buff = null;
|
||||
private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault());
|
||||
private final SimpleDateFormat FILE_SDF = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss-SSS", Locale.getDefault());
|
||||
private File file;
|
||||
private boolean isLogSwitch = false;
|
||||
private long capacity = MAX_CAPACITY;
|
||||
private volatile long capacity = MAX_CAPACITY;
|
||||
private Future future;
|
||||
|
||||
private LogSave() {
|
||||
queue = new LinkedBlockingQueue<>();
|
||||
start();
|
||||
}
|
||||
|
||||
public static LogSave getInstance() {
|
||||
@@ -61,15 +48,6 @@ public class LogSave {
|
||||
}
|
||||
|
||||
|
||||
public void setIsLogSwitch(boolean isLogSwitch) {
|
||||
this.isLogSwitch = isLogSwitch;
|
||||
}
|
||||
|
||||
public boolean isLogSwitch() {
|
||||
return isLogSwitch;
|
||||
}
|
||||
|
||||
|
||||
public boolean isSdcardUse() {
|
||||
boolean bl = false;
|
||||
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
|
||||
@@ -80,7 +58,8 @@ public class LogSave {
|
||||
|
||||
private void getFile() throws IOException {
|
||||
if (isSdcardUse()) {
|
||||
String time = FILE_SDF.format(new Date());
|
||||
String time = sdf.format(new Date());
|
||||
time = time.replace(" ", "_").replace(":", "-").replace(".", "-");
|
||||
String childPath = time.split("_")[0] + File.separator;
|
||||
file = new File(ROOT_PATH + childPath + String.format(LOG_FILE_NAME, time));
|
||||
if (!file.exists()) {
|
||||
@@ -96,12 +75,14 @@ public class LogSave {
|
||||
|
||||
|
||||
public void saveLog(String action, String data) {
|
||||
if (isLogSwitch) {
|
||||
long nowTime = System.currentTimeMillis();
|
||||
String time = sdf.format(new Date(nowTime));
|
||||
String builder = time + " [action]:" + action + " [data]:" + data;
|
||||
queue.add(builder);
|
||||
}
|
||||
long nowTime = System.currentTimeMillis();
|
||||
String time = sdf.format(new Date(nowTime));
|
||||
String builder = time + " [action]:" + action + " [data]:" + data;
|
||||
queue.add(builder);
|
||||
}
|
||||
|
||||
public boolean isStart() {
|
||||
return future != null;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
|
||||
@@ -153,8 +153,6 @@ public class AutopilotConfigActivity extends BaseActivity {
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
EventBus.getDefault().post(new UpdateDataEvent());
|
||||
if (getHandler() != null)
|
||||
getHandler().removeCallbacksAndMessages(null);
|
||||
EventBus.getDefault().unregister(this);
|
||||
}
|
||||
|
||||
|
||||
@@ -103,9 +103,9 @@ public class InfoFragment extends Fragment {
|
||||
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onWarnEvent(Warn info) {
|
||||
EventBus.getDefault().removeStickyEvent(info);
|
||||
|
||||
if (title.equals(MainActivity.TITLE.RECEIVE_WARN)) {
|
||||
if (data.size() > 9) {
|
||||
data.remove(0);
|
||||
@@ -115,9 +115,9 @@ public class InfoFragment extends Fragment {
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onRectEvent(TrackedObjects info) {
|
||||
EventBus.getDefault().removeStickyEvent(info);
|
||||
|
||||
if (title.equals(MainActivity.TITLE.RECEIVE_TRACKED_OBJECTS)) {
|
||||
if (data.size() > 4) {
|
||||
data.remove(0);
|
||||
@@ -127,9 +127,9 @@ public class InfoFragment extends Fragment {
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onErrorEvent(ErrorData info) {
|
||||
EventBus.getDefault().removeStickyEvent(info);
|
||||
|
||||
if (title.equals(MainActivity.TITLE.RECEIVE_ERROR)) {
|
||||
if (data.size() > 19) {
|
||||
data.remove(0);
|
||||
@@ -184,11 +184,10 @@ public class InfoFragment extends Fragment {
|
||||
return turnLight;
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
|
||||
public void onCarEvent(VehicleState info) {
|
||||
EventBus.getDefault().removeStickyEvent(info);
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onVehicleStateEvent(VehicleState info) {
|
||||
if (title.equals(MainActivity.TITLE.RECEIVE_VEHICLE_STATE)) {
|
||||
if (data.size() > 5) {
|
||||
if (data.size() > 9) {
|
||||
data.remove(0);
|
||||
}
|
||||
data.add(info.toString());
|
||||
@@ -199,9 +198,8 @@ public class InfoFragment extends Fragment {
|
||||
}
|
||||
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
|
||||
public void onAutoEvent(GnssInfo info) {
|
||||
EventBus.getDefault().removeStickyEvent(info);
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onGnssInfoEvent(GnssInfo info) {
|
||||
if (title.equals(MainActivity.TITLE.RECEIVE_GNSS_INFO)) {
|
||||
if (data.size() > 9) {
|
||||
data.remove(0);
|
||||
@@ -211,9 +209,8 @@ public class InfoFragment extends Fragment {
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
|
||||
public void onAutoEvent(AutopilotState info) {
|
||||
EventBus.getDefault().removeStickyEvent(info);
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onAutopilotStateEvent(AutopilotState info) {
|
||||
if (title.equals(MainActivity.TITLE.RECEIVE_AUTOPILOT_STATE)) {
|
||||
if (data.size() > 9) {
|
||||
data.remove(0);
|
||||
@@ -224,10 +221,53 @@ public class InfoFragment extends Fragment {
|
||||
}
|
||||
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onTrajectoryEvent(Trajectory info) {
|
||||
EventBus.getDefault().removeStickyEvent(info);
|
||||
if (title.equals(MainActivity.TITLE.RECEIVE_TRAJECTORY)) {
|
||||
if (data.size() > 4) {
|
||||
data.remove(0);
|
||||
}
|
||||
data.add(info.toString());
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onArrivalNotificationEvent(ArrivalNotification info) {
|
||||
if (title.equals(MainActivity.TITLE.RECEIVE_ARRIVAL_NOTIFICATION)) {
|
||||
if (data.size() > 9) {
|
||||
data.remove(0);
|
||||
}
|
||||
data.add(info.toString());
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onGlobalPathEvent(GlobalPathResp info) {
|
||||
if (title.equals(MainActivity.TITLE.RECEIVE_GLOBAL_PATH_RESP)) {
|
||||
if (data.size() > 9) {
|
||||
data.remove(0);
|
||||
}
|
||||
data.add(info.toString());
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onRecordPanelEvent(RecordPanel info) {
|
||||
if (title.equals(MainActivity.TITLE.RECEIVE_RECORD_RESULT)) {
|
||||
if (data.size() > 9) {
|
||||
data.remove(0);
|
||||
}
|
||||
data.add(info.toString());
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onMogoReportMessageEvent(MogoReportMessage info) {
|
||||
if (title.equals(MainActivity.TITLE.RECEIVE_REPORT_MESSAGE)) {
|
||||
if (data.size() > 9) {
|
||||
data.remove(0);
|
||||
}
|
||||
@@ -236,46 +276,12 @@ public class InfoFragment extends Fragment {
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
|
||||
public void onAutopilotWayArriveEvent(ArrivalNotification info) {
|
||||
EventBus.getDefault().removeStickyEvent(info);
|
||||
if (title.equals(MainActivity.TITLE.RECEIVE_ARRIVAL_NOTIFICATION)) {
|
||||
data.add(info.toString());
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
|
||||
public void onAutopilotRouteEvent(GlobalPathResp info) {
|
||||
EventBus.getDefault().removeStickyEvent(info);
|
||||
if (title.equals(MainActivity.TITLE.RECEIVE_GLOBAL_PATH_RESP)) {
|
||||
data.add(info.toString());
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
|
||||
public void onAutopilotRecordResultEvent(RecordPanel info) {
|
||||
EventBus.getDefault().removeStickyEvent(info);
|
||||
if (title.equals(MainActivity.TITLE.RECEIVE_RECORD_RESULT)) {
|
||||
data.add(info.toString());
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
|
||||
public void onAutopilotGuardianInfoEvent(MogoReportMessage info) {
|
||||
EventBus.getDefault().removeStickyEvent(info);
|
||||
if (title.equals(MainActivity.TITLE.RECEIVE_REPORT_MESSAGE)) {
|
||||
data.add(info.toString());
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
|
||||
public void onPerceptionTrafficLightEvent(PerceptionTrafficLight info) {
|
||||
EventBus.getDefault().removeStickyEvent(info);
|
||||
if (title.equals(MainActivity.TITLE.RECEIVE_PERCEPTION_TRAFFIC_LIGHT)) {
|
||||
if (data.size() > 9) {
|
||||
data.remove(0);
|
||||
}
|
||||
data.add(info.toString());
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@ import static com.mogo.telematic.MogoProtocolMsg.NORMAL_DATA;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Configuration;
|
||||
import android.os.Bundle;
|
||||
import android.os.Message;
|
||||
import android.text.Editable;
|
||||
import android.text.TextUtils;
|
||||
import android.text.TextWatcher;
|
||||
@@ -14,17 +14,19 @@ import android.util.Log;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ListPopupWindow;
|
||||
import android.widget.PopupWindow;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.RadioGroup;
|
||||
import android.widget.Switch;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.AppCompatButton;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentTransaction;
|
||||
@@ -33,10 +35,6 @@ import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import androidx.recyclerview.widget.SimpleItemAnimator;
|
||||
|
||||
import com.zhidao.adas.client.bean.Base;
|
||||
import com.zhidao.adas.client.log.LogSave;
|
||||
import com.zhidao.support.adas.high.common.Constants.IPC_CONNECTION_STATUS;
|
||||
import com.google.gson.Gson;
|
||||
import com.mogo.telematic.MogoProtocolMsg;
|
||||
import com.mogo.telematic.NSDNettyManager;
|
||||
import com.mogo.telematic.client.listener.NettyClientListener;
|
||||
@@ -48,13 +46,13 @@ import com.zhidao.adas.client.adapter.InfoTitleAdapter;
|
||||
import com.zhidao.adas.client.base.BaseActivity;
|
||||
import com.zhidao.adas.client.base.BaseAdapter;
|
||||
import com.zhidao.adas.client.bean.ArrivalNotification;
|
||||
import com.zhidao.adas.client.bean.AutoPilotMode;
|
||||
import com.zhidao.adas.client.bean.AutopilotState;
|
||||
import com.zhidao.adas.client.bean.BasicInfoReq;
|
||||
import com.zhidao.adas.client.bean.CarConfigResp;
|
||||
import com.zhidao.adas.client.bean.ErrorData;
|
||||
import com.zhidao.adas.client.bean.GlobalPathResp;
|
||||
import com.zhidao.adas.client.bean.GnssInfo;
|
||||
import com.zhidao.adas.client.bean.IPCConnectState;
|
||||
import com.zhidao.adas.client.bean.MogoReportMessage;
|
||||
import com.zhidao.adas.client.bean.PerceptionTrafficLight;
|
||||
import com.zhidao.adas.client.bean.RecordPanel;
|
||||
@@ -62,16 +60,16 @@ import com.zhidao.adas.client.bean.TrackedObjects;
|
||||
import com.zhidao.adas.client.bean.Trajectory;
|
||||
import com.zhidao.adas.client.bean.VehicleState;
|
||||
import com.zhidao.adas.client.bean.Warn;
|
||||
import com.zhidao.adas.client.log.LogSave;
|
||||
import com.zhidao.adas.client.utils.Constants;
|
||||
import com.zhidao.support.adas.high.AdasManager;
|
||||
import com.zhidao.support.adas.high.AdasOptions;
|
||||
import com.zhidao.support.adas.high.OnAdasListener;
|
||||
import com.zhidao.support.adas.high.OnAdasConnectStatusListener;
|
||||
import com.zhidao.support.adas.high.OnAdasListener;
|
||||
import com.zhidao.support.adas.high.OnMultiDeviceListener;
|
||||
|
||||
import com.zhidao.support.adas.high.bean.IPCUpgradeStateInfo;
|
||||
import com.zhidao.support.adas.high.bean.SSHResult;
|
||||
|
||||
import com.zhidao.support.adas.high.common.Constants.IPC_CONNECTION_STATUS;
|
||||
import com.zhidao.support.adas.high.common.CupidLogUtils;
|
||||
import com.zhidao.support.adas.high.common.ProtocolStatus;
|
||||
import com.zhidao.support.recorder.RecordDataManager;
|
||||
@@ -86,7 +84,6 @@ import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import chassis.VehicleStateOuterClass;
|
||||
import io.netty.channel.Channel;
|
||||
@@ -97,6 +94,9 @@ import record_cache.RecordPanelOuterClass;
|
||||
|
||||
public class MainActivity extends BaseActivity implements OnAdasListener, OnAdasConnectStatusListener, BaseAdapter.OnItemClickListener<String> {
|
||||
private final static String TAG = MainActivity.class.getSimpleName();
|
||||
private static final int WHAT_IPC_IP = 0x00;
|
||||
private static final int WHAT_DRIVER_IP = 0x01;
|
||||
private static final int WHATIPC_CONNECT_STATE = 0x02;
|
||||
private EditText etIp;
|
||||
private TextView role;
|
||||
private TextView tvIp;
|
||||
@@ -145,6 +145,7 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
|
||||
private String recordFileName;
|
||||
private int connectStatus;
|
||||
private AutoPilotModeDialog autoPilotModeDialog;
|
||||
private ListPopupWindow listPopupWindow;
|
||||
|
||||
public interface TITLE {
|
||||
String RECEIVE_TRAJECTORY = "车前引导线";
|
||||
@@ -181,6 +182,7 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
initHandler();
|
||||
isPad = isPad(this);
|
||||
if (!isPad)
|
||||
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||
@@ -190,7 +192,7 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
|
||||
initAdas();
|
||||
connectStatus = AdasManager.getInstance().getIpcConnectionStatus();
|
||||
onUpdateConnectStateView();
|
||||
showIPCIP(AdasManager.getInstance().getIpcConnectedIp(), AdasManager.getInstance().getIpcConnectedPort());
|
||||
showIPCIP();
|
||||
}
|
||||
|
||||
|
||||
@@ -236,7 +238,7 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
|
||||
localIp = findViewById(R.id.local_ip);
|
||||
|
||||
|
||||
role.setText(BuildConfig.IS_CLIENT ? "乘客" : "司机");
|
||||
role.setText(BuildConfig.IS_CLIENT ? "乘客端" : "司机端");
|
||||
if (BuildConfig.IS_CLIENT) {
|
||||
line.setVisibility(View.GONE);
|
||||
connectionType.setVisibility(View.GONE);
|
||||
@@ -284,7 +286,7 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
|
||||
AdasManager.getInstance().setEnableLog(isChecked);
|
||||
}
|
||||
});
|
||||
cb_save.setChecked(LogSave.getInstance().isLogSwitch());
|
||||
cb_save.setChecked(LogSave.getInstance().isStart());
|
||||
cb_save.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
@@ -293,7 +295,6 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
|
||||
} else {
|
||||
LogSave.getInstance().stop();
|
||||
}
|
||||
LogSave.getInstance().setIsLogSwitch(isChecked);
|
||||
}
|
||||
});
|
||||
connectionType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
|
||||
@@ -344,6 +345,45 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
|
||||
showLocalIP();
|
||||
}
|
||||
});
|
||||
|
||||
findViewById(R.id.tv_ip).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (listPopupWindow == null)
|
||||
showListPopupWindow();
|
||||
else {
|
||||
listPopupWindow.dismiss();
|
||||
listPopupWindow = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void showListPopupWindow() {
|
||||
List<String> ips = Constants.getIpcUsedIps(this);
|
||||
if (ips != null && !ips.isEmpty()) {
|
||||
listPopupWindow = new ListPopupWindow(this);
|
||||
listPopupWindow.setAdapter(new ArrayAdapter<String>(this, R.layout.item_pop, ips));
|
||||
listPopupWindow.setAnchorView(etIp);//以哪个控件为基准,在该处以mEditText为基准
|
||||
listPopupWindow.setModal(true);
|
||||
listPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
|
||||
@Override
|
||||
public void onDismiss() {
|
||||
listPopupWindow = null;
|
||||
}
|
||||
});
|
||||
listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
||||
String text = ips.get(i);
|
||||
etIp.setText(text);
|
||||
etIp.setSelection(text.length());
|
||||
listPopupWindow.dismiss();
|
||||
listPopupWindow = null;
|
||||
}
|
||||
});
|
||||
listPopupWindow.show();
|
||||
}
|
||||
}
|
||||
|
||||
private void initListData() {
|
||||
@@ -534,22 +574,23 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
|
||||
});
|
||||
}
|
||||
|
||||
public void showIPCIP(final String ip, final int port) {
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (!BuildConfig.IS_CLIENT) {
|
||||
String temp = "";
|
||||
ipcIp.setVisibility(View.VISIBLE);
|
||||
if (!TextUtils.isEmpty(ip)) {
|
||||
temp = "IPC IP:" + ip + ":" + port;
|
||||
}
|
||||
ipcIp.setText(temp);
|
||||
} else {
|
||||
ipcIp.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
});
|
||||
private String getIPCIP() {
|
||||
final String ip = AdasManager.getInstance().getIpcConnectedIp();
|
||||
final int port = AdasManager.getInstance().getIpcConnectedPort();
|
||||
String temp = "";
|
||||
if (!TextUtils.isEmpty(ip)) {
|
||||
temp = ip + ":" + port;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
public void showIPCIP() {
|
||||
if (!BuildConfig.IS_CLIENT) {
|
||||
ipcIp.setVisibility(View.VISIBLE);
|
||||
ipcIp.setText("IPC IP:" + getIPCIP());
|
||||
} else {
|
||||
ipcIp.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -579,38 +620,17 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
|
||||
color = R.color.connect_status_disconnecting;
|
||||
break;
|
||||
}
|
||||
CupidLogUtils.i(TAG, "connectStatus=" + status);
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
tvConnectState.setText(status);
|
||||
tvConnectState.setTextColor(getResources().getColor(color));
|
||||
}
|
||||
});
|
||||
Message msg = Message.obtain();
|
||||
msg.obj = new IPCConnectState(status, color);
|
||||
msg.what = WHATIPC_CONNECT_STATE;
|
||||
getHandler().sendMessage(msg);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
private void updateText(final TextView tv, final String text) {
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
tv.setText(text);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onSSHResult(final SSHResult info) {
|
||||
LogSave.getInstance().saveLog("接收", info.toString());
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
showToastCenter("IPC命令下发结果:" + info.code + " 命令:" + info.cmd + " 信息:" + info.msg);
|
||||
}
|
||||
});
|
||||
showToastCenter("IPC命令下发结果:" + info.code + " 命令:" + info.cmd + " 信息:" + info.msg);
|
||||
CupidLogUtils.w(TAG, "IPC命令下发结果:" + info.code + " 命令:" + info.cmd + " 信息:" + info.msg);
|
||||
}
|
||||
|
||||
@@ -618,56 +638,56 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
|
||||
public void onError(ProtocolStatus status, byte[] bytes) {
|
||||
ErrorData base = new ErrorData(status, bytes);
|
||||
LogSave.getInstance().saveLog("接收", base.toString());
|
||||
EventBus.getDefault().postSticky(base);
|
||||
EventBus.getDefault().post(base);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTrajectory(MessagePad.Header header, MessagePad.Trajectory trajectory) {
|
||||
Trajectory base = new Trajectory(header, trajectory);
|
||||
LogSave.getInstance().saveLog("接收", base.toString());
|
||||
EventBus.getDefault().postSticky(base);
|
||||
EventBus.getDefault().post(base);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTrackedObjects(MessagePad.Header header, MessagePad.TrackedObjects trackedObjects) {
|
||||
TrackedObjects base = new TrackedObjects(header, trackedObjects);
|
||||
LogSave.getInstance().saveLog("接收", base.toString());
|
||||
EventBus.getDefault().postSticky(base);
|
||||
EventBus.getDefault().post(base);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGnssInfo(MessagePad.Header header, MessagePad.GnssInfo gnssInfo) {
|
||||
GnssInfo base = new GnssInfo(header, gnssInfo);
|
||||
LogSave.getInstance().saveLog("接收", base.toString());
|
||||
EventBus.getDefault().postSticky(base);
|
||||
EventBus.getDefault().post(base);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onVehicleState(MessagePad.Header header, VehicleStateOuterClass.VehicleState vehicleState) {
|
||||
VehicleState base = new VehicleState(header, vehicleState);
|
||||
LogSave.getInstance().saveLog("接收", base.toString());
|
||||
EventBus.getDefault().postSticky(base);
|
||||
EventBus.getDefault().post(base);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAutopilotState(MessagePad.Header header, MessagePad.AutopilotState autopilotState) {
|
||||
AutopilotState base = new AutopilotState(header, autopilotState);
|
||||
LogSave.getInstance().saveLog("接收", base.toString());
|
||||
EventBus.getDefault().postSticky(base);
|
||||
EventBus.getDefault().post(base);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReportMessage(MessagePad.Header header, MogoReportMsg.MogoReportMessage mogoReportMessage) {
|
||||
MogoReportMessage base = new MogoReportMessage(header, mogoReportMessage);
|
||||
LogSave.getInstance().saveLog("接收", base.toString());
|
||||
EventBus.getDefault().postSticky(base);
|
||||
EventBus.getDefault().post(base);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPerceptionTrafficLight(MessagePad.Header header, TrafficLightOuterClass.TrafficLights trafficLights) {
|
||||
PerceptionTrafficLight base = new PerceptionTrafficLight(header, trafficLights);
|
||||
LogSave.getInstance().saveLog("接收", base.toString());
|
||||
EventBus.getDefault().postSticky(base);
|
||||
EventBus.getDefault().post(base);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -675,13 +695,8 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
|
||||
BasicInfoReq info = new BasicInfoReq(header, basicInfoReq);
|
||||
LogSave.getInstance().saveLog("接收", info.toString());
|
||||
AdasManager.getInstance().sendBasicInfoResp("X202021111192N41VY", 1);
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
toastMsg("收到车机基础信息请求:" + info.toString());
|
||||
}
|
||||
});
|
||||
// EventBus.getDefault().postSticky(new BasicInfoReq(header, basicInfoReq));
|
||||
showToastCenter("收到车机基础信息请求:" + info.toString());
|
||||
// EventBus.getDefault().post(new BasicInfoReq(header, basicInfoReq));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -697,48 +712,53 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
|
||||
LogSave.getInstance().saveLog("接收", base.toString());
|
||||
recordKey = recordPanel.getKey();
|
||||
recordFileName = recordPanel.getFilename();
|
||||
EventBus.getDefault().postSticky(base);
|
||||
EventBus.getDefault().post(base);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGlobalPathResp(MessagePad.Header header, MessagePad.GlobalPathResp globalPathResp) {
|
||||
GlobalPathResp base = new GlobalPathResp(header, globalPathResp);
|
||||
LogSave.getInstance().saveLog("接收", base.toString());
|
||||
EventBus.getDefault().postSticky(base);
|
||||
EventBus.getDefault().post(base);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWarn(MessagePad.Header header, MessagePad.Warn warn) {
|
||||
Warn base = new Warn(header, warn);
|
||||
LogSave.getInstance().saveLog("接收", base.toString());
|
||||
EventBus.getDefault().postSticky(base);
|
||||
EventBus.getDefault().post(base);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onArrivalNotification(MessagePad.Header header, MessagePad.ArrivalNotification arrivalNotification) {
|
||||
ArrivalNotification base = new ArrivalNotification(header, arrivalNotification);
|
||||
LogSave.getInstance().saveLog("接收", base.toString());
|
||||
EventBus.getDefault().postSticky(base);
|
||||
EventBus.getDefault().post(base);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgradeStateInfo(final IPCUpgradeStateInfo info) {
|
||||
LogSave.getInstance().saveLog("接收", info.toString());
|
||||
EventBus.getDefault().postSticky(info);
|
||||
EventBus.getDefault().post(info);
|
||||
}
|
||||
|
||||
|
||||
private Toast toast;
|
||||
|
||||
public void showToastCenter(String msg) {
|
||||
if (toast != null) {
|
||||
toast.cancel();
|
||||
toast = null;
|
||||
}
|
||||
toast = Toast.makeText(this, "", Toast.LENGTH_SHORT); //如果有居中显示需求
|
||||
toast.setGravity(Gravity.CENTER, 0, 0);
|
||||
toast.setText(msg);
|
||||
toast.show();
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (toast != null) {
|
||||
toast.cancel();
|
||||
toast = null;
|
||||
}
|
||||
toast = Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT); //如果有居中显示需求
|
||||
toast.setGravity(Gravity.CENTER, 0, 0);
|
||||
toast.setText(msg);
|
||||
toast.show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -763,13 +783,7 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
|
||||
} else {
|
||||
connectStatus = IPC_CONNECTION_STATUS.DISCONNECTED;
|
||||
}
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ipcIp.setVisibility(View.VISIBLE);
|
||||
ipcIp.setText("司机IP:" + NSDNettyManager.getInstance().getConnServerIp());
|
||||
}
|
||||
});
|
||||
getHandler().sendEmptyMessage(WHAT_DRIVER_IP);
|
||||
onUpdateConnectStateView();
|
||||
}
|
||||
});
|
||||
@@ -868,29 +882,26 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
|
||||
public void onConnectionIPCStatus(int ipcConnectionStatus, String failedMsg) {
|
||||
connectStatus = ipcConnectionStatus;
|
||||
String status = onUpdateConnectStateView();
|
||||
status += "failedMsg=" + failedMsg;
|
||||
LogSave.getInstance().saveLog("连接状态", status);
|
||||
if (connectStatus == IPC_CONNECTION_STATUS.CONNECTED) {
|
||||
CupidLogUtils.w(TAG, "===>MainActivity onWebSocketConnectSuccess");
|
||||
showIPCIP(AdasManager.getInstance().getIpcConnectedIp(), AdasManager.getInstance().getIpcConnectedPort());
|
||||
} else if (connectStatus == IPC_CONNECTION_STATUS.DISCONNECTED) {
|
||||
toastMsg("连接失败:" + failedMsg);
|
||||
CupidLogUtils.w(TAG, "===>MainActivity onWebSocketConnectFailed");
|
||||
showIPCIP(AdasManager.getInstance().getIpcConnectedIp(), AdasManager.getInstance().getIpcConnectedPort());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void toastMsg(final String msg) {
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
|
||||
getHandler().sendEmptyMessage(WHAT_IPC_IP);
|
||||
String tem = getIPCIP();
|
||||
if (!TextUtils.isEmpty(tem)) {
|
||||
List<String> ips = Constants.getIpcUsedIps(this);
|
||||
Constants.addIpcUsedIps(this, ips, tem);
|
||||
}
|
||||
});
|
||||
} else if (connectStatus == IPC_CONNECTION_STATUS.DISCONNECTED) {
|
||||
if (!TextUtils.isEmpty(failedMsg)) {
|
||||
status += " failedMsg=" + failedMsg;
|
||||
showToastCenter("连接失败:" + failedMsg);
|
||||
}
|
||||
getHandler().sendEmptyMessage(WHAT_IPC_IP);
|
||||
}
|
||||
LogSave.getInstance().saveLog("连接状态", status);
|
||||
CupidLogUtils.i(TAG, "connectStatus=" + status);
|
||||
}
|
||||
|
||||
private void showLocalIP() {
|
||||
showToastCenter("已刷新本机IP");
|
||||
localIp.setText("本机IP:" + getIpAddressString());
|
||||
}
|
||||
|
||||
@@ -936,10 +947,10 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
|
||||
|
||||
@Override
|
||||
public void onItemClick(int position, String data) {
|
||||
// if (connectStatus != IPC_CONNECTION_STATUS.CONNECTED) {
|
||||
// toastMsg("IPC 未连接");
|
||||
// return;
|
||||
// }
|
||||
if (connectStatus != IPC_CONNECTION_STATUS.CONNECTED) {
|
||||
showToastCenter("IPC 未连接");
|
||||
return;
|
||||
}
|
||||
switch (position) {
|
||||
case 0:
|
||||
if (autoPilotModeDialog == null) {
|
||||
@@ -1056,4 +1067,24 @@ public class MainActivity extends BaseActivity implements OnAdasListener, OnAdas
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void handleMessage(Message msg) {
|
||||
super.handleMessage(msg);
|
||||
switch (msg.what) {
|
||||
case WHAT_IPC_IP:
|
||||
showIPCIP();
|
||||
break;
|
||||
case WHAT_DRIVER_IP:
|
||||
ipcIp.setVisibility(View.VISIBLE);
|
||||
ipcIp.setText("司机IP:" + NSDNettyManager.getInstance().getConnServerIp());
|
||||
break;
|
||||
case WHATIPC_CONNECT_STATE:
|
||||
IPCConnectState status = (IPCConnectState) msg.obj;
|
||||
tvConnectState.setText(status.status);
|
||||
tvConnectState.setTextColor(getResources().getColor(status.color));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.zhidao.adas.client.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.zhidao.adas.client.bean.AutoPilotMode;
|
||||
@@ -85,4 +86,43 @@ public class Constants {
|
||||
public static boolean delIPCIp(Context context) {
|
||||
return PreferencesUtils.delete(context, IPC_IP);
|
||||
}
|
||||
|
||||
|
||||
/***********************保存使用过的IP列表******************/
|
||||
|
||||
|
||||
private static final String IPC_USED_IP = "ipc_used_ip";
|
||||
|
||||
public static List<String> getIpcUsedIps(Context context) {
|
||||
String json = PreferencesUtils.getString(context, IPC_USED_IP, null);
|
||||
if (TextUtils.isEmpty(json)) return null;
|
||||
List<String> list = JsonUtil.fromJson(json, new TypeToken<List<String>>() {
|
||||
}.getType());
|
||||
return list;
|
||||
}
|
||||
|
||||
public static boolean addIpcUsedIps(Context context, List<String> list, String mode) {
|
||||
if (list == null)
|
||||
list = new ArrayList<>();
|
||||
list.remove(mode);
|
||||
list.add(0, mode);
|
||||
if (list.size() > 10) {
|
||||
list.remove(list.size() - 1);
|
||||
}
|
||||
return PreferencesUtils.putString(context, IPC_USED_IP, JsonUtil.toJson(list));
|
||||
}
|
||||
|
||||
public static boolean delIpcUsedIps(Context context, List<String> list, String mode) {
|
||||
if (list == null) {
|
||||
return PreferencesUtils.delete(context, IPC_USED_IP);
|
||||
} else {
|
||||
if (list.contains(mode)) {
|
||||
list.remove(mode);
|
||||
return PreferencesUtils.putString(context, IPC_USED_IP, JsonUtil.toJson(list));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginStart="10dp"
|
||||
android:gravity="center_vertical"
|
||||
android:text="工控机"
|
||||
android:text="@string/app_name"
|
||||
android:textColor="@color/colorWhile"
|
||||
android:textSize="18dp"
|
||||
android:textStyle="bold" />
|
||||
@@ -215,9 +215,9 @@
|
||||
android:layoutDirection="ltr"
|
||||
android:maxLength="21"
|
||||
android:maxLines="1"
|
||||
android:minWidth="100dp"
|
||||
android:minWidth="166dp"
|
||||
android:textColor="@color/colorWhile"
|
||||
android:textSize="16dp" />
|
||||
android:textSize="16sp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_ip"
|
||||
|
||||
13
app_ipc_monitoring/src/main/res/layout/item_pop.xml
Normal file
13
app_ipc_monitoring/src/main/res/layout/item_pop.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@android:id/text1"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:maxLines="1"
|
||||
android:minWidth="100dp"
|
||||
android:paddingTop="10dp"
|
||||
android:paddingBottom="10dp"
|
||||
android:textAppearance="?android:attr/textAppearanceListItemSmall"
|
||||
android:textColor="@color/colorBlock"
|
||||
android:textSize="16sp" />
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.zhidao.support.adas.high;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.zhidao.support.adas.high.common.Constants;
|
||||
import com.zhidao.support.adas.high.common.Define;
|
||||
|
||||
@@ -15,7 +17,8 @@ public interface OnAdasConnectStatusListener {
|
||||
*
|
||||
* @param ipcConnectionStatus {@link Constants.IPC_CONNECTION_STATUS}
|
||||
* @param failedMsg 连接异常信息 需要判null
|
||||
* 如果ipcConnectionStatus==Constants.IPC_CONNECTION_STATUS.DISCONNECTED&&failedMsg==null 表示主动断开连接
|
||||
*/
|
||||
void onConnectionIPCStatus(@Define.IPCConnectionStatus int ipcConnectionStatus, String failedMsg);
|
||||
void onConnectionIPCStatus(@Define.IPCConnectionStatus int ipcConnectionStatus, @Nullable String failedMsg);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.zhidao.support.adas.high.common;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
* 与工控机链接成功后,工控机断开连接超时监测管理器
|
||||
*/
|
||||
public class TimeoutManager {
|
||||
private static volatile TimeoutManager INSTANCE;
|
||||
private volatile long lastReceiveTime;
|
||||
private volatile Future future;
|
||||
private OnTimeoutListener listener;
|
||||
|
||||
public interface OnTimeoutListener {
|
||||
void onTimeout();
|
||||
}
|
||||
|
||||
public void setListener(OnTimeoutListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
private TimeoutManager() {
|
||||
}
|
||||
|
||||
public static TimeoutManager getInstance() {
|
||||
if (INSTANCE == null) {
|
||||
synchronized (TimeoutManager.class) {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new TimeoutManager();
|
||||
}
|
||||
}
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新最后一次接收时间
|
||||
*/
|
||||
public void refreshLase() {
|
||||
lastReceiveTime = System.currentTimeMillis();
|
||||
Log.i("Fpga", "lastReceiveTime=" + lastReceiveTime);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if (future == null) {
|
||||
future = ThreadPoolManager.getsInstance().submit(new DetectionThread());
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void stop() {
|
||||
if (future != null && !future.isCancelled()) {
|
||||
future.cancel(true);
|
||||
}
|
||||
future = null;
|
||||
}
|
||||
|
||||
private class DetectionThread implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
synchronized (this) {
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
if (System.currentTimeMillis() - lastReceiveTime > 5 * 1000L) {
|
||||
if (listener != null) {
|
||||
listener.onTimeout();
|
||||
}
|
||||
stop();
|
||||
}
|
||||
try {
|
||||
Thread.sleep(5 * 1000L);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -58,6 +58,7 @@ public class FpgaSocket implements IWebSocket {
|
||||
private String wsHost;
|
||||
private String ipAddress;
|
||||
private int port;
|
||||
|
||||
/**
|
||||
* 是否是用户主动关闭socket
|
||||
*/
|
||||
@@ -77,9 +78,9 @@ public class FpgaSocket implements IWebSocket {
|
||||
}
|
||||
if (okBuilder == null) {
|
||||
okBuilder = new OkHttpClient.Builder();
|
||||
okBuilder.writeTimeout(5, TimeUnit.SECONDS);
|
||||
okBuilder.readTimeout(5, TimeUnit.SECONDS);
|
||||
okBuilder.connectTimeout(5, TimeUnit.SECONDS);
|
||||
okBuilder.writeTimeout(5, TimeUnit.SECONDS)
|
||||
.readTimeout(5, TimeUnit.SECONDS)
|
||||
.connectTimeout(5, TimeUnit.SECONDS);
|
||||
}
|
||||
if (client == null) {
|
||||
client = okBuilder.build();
|
||||
@@ -137,10 +138,9 @@ public class FpgaSocket implements IWebSocket {
|
||||
}
|
||||
isUserCloseWebSocket = true;
|
||||
if (mWebSocket != null) {
|
||||
boolean isClose = mWebSocket.close(1000, null);
|
||||
CupidLogUtils.i(TAG, "WebSocket 主动断开连接,是否成功= " + isClose);
|
||||
mWebSocket.cancel();
|
||||
mWebSocket = null;
|
||||
close(true, 1000);
|
||||
} else {
|
||||
onConnectFailed(null);
|
||||
}
|
||||
listener = null;
|
||||
client = null;
|
||||
@@ -260,6 +260,8 @@ public class FpgaSocket implements IWebSocket {
|
||||
public void onClosing(@NonNull WebSocket webSocket, int code, @NonNull String reason) {
|
||||
super.onClosing(webSocket, code, reason);
|
||||
CupidLogUtils.e(TAG, "WebSocket onClosing= " + reason);
|
||||
if (TextUtils.isEmpty(reason))
|
||||
reason = "onClosing";
|
||||
onClose(reason);
|
||||
}
|
||||
|
||||
@@ -267,6 +269,8 @@ public class FpgaSocket implements IWebSocket {
|
||||
public void onClosed(@NonNull WebSocket webSocket, int code, @NonNull String reason) {
|
||||
super.onClosed(webSocket, code, reason);
|
||||
CupidLogUtils.e(TAG, "WebSocket onClosed= " + reason);
|
||||
if (TextUtils.isEmpty(reason))
|
||||
reason = "onClosed";
|
||||
onClose(reason);
|
||||
}
|
||||
|
||||
@@ -274,30 +278,45 @@ public class FpgaSocket implements IWebSocket {
|
||||
public void onFailure(@NonNull WebSocket webSocket, @NonNull Throwable t, Response response) {
|
||||
super.onFailure(webSocket, t, response);
|
||||
CupidLogUtils.e(TAG, "WebSocket onFailure= " + t);
|
||||
t.printStackTrace();
|
||||
String reason = t.toString();
|
||||
if (TextUtils.isEmpty(reason))
|
||||
reason = "onFailure";
|
||||
if (mWebSocket != null) {
|
||||
mWebSocket.close(1000, null);
|
||||
mWebSocket.cancel();
|
||||
mWebSocket = null;
|
||||
close(false, 1001);
|
||||
}
|
||||
if (mWebSocketConnectListener != null)
|
||||
mWebSocketConnectListener.onWebSocketConnectFailed(t.toString());
|
||||
reconnect();
|
||||
|
||||
onConnectFailed(reason);
|
||||
}
|
||||
}
|
||||
|
||||
private void onConnectFailed(String reason) {
|
||||
if (mWebSocketConnectListener != null) {
|
||||
if (isUserCloseWebSocket)
|
||||
reason = null;
|
||||
mWebSocketConnectListener.onWebSocketConnectFailed(reason);
|
||||
}
|
||||
reconnect();
|
||||
}
|
||||
|
||||
private void onClose(String reason) {
|
||||
if (mWebSocket != null) {
|
||||
mWebSocket.close(1000, null);
|
||||
mWebSocket.cancel();
|
||||
mWebSocket = null;
|
||||
close(false, 1001);
|
||||
} else {
|
||||
if (mWebSocketConnectListener != null)
|
||||
mWebSocketConnectListener.onWebSocketConnectFailed(reason);
|
||||
reconnect();
|
||||
onConnectFailed(reason);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param isInitiative 是否是主动断开
|
||||
* @param code code
|
||||
*/
|
||||
private void close(boolean isInitiative, int code) {
|
||||
boolean isClose = mWebSocket.close(code, null);
|
||||
CupidLogUtils.i(TAG, "WebSocket " + (isInitiative ? "主动" : "被动") + "断开连接是否成功= " + isClose);
|
||||
mWebSocket.cancel();
|
||||
mWebSocket = null;
|
||||
}
|
||||
|
||||
public WebSocket getWebSocket() {
|
||||
return mWebSocket;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user