This commit is contained in:
wangcongtao
2020-04-14 11:27:04 +08:00
parent dccc9be8be
commit 00dcc08075
8 changed files with 98 additions and 63 deletions

View File

@@ -20,7 +20,7 @@ public class JWebSocketClient extends WebSocketClient {
@Override
public void onOpen( ServerHandshake handshakedata ) {
Logger.e( "JWebSocketClient", "onOpen()" );
Logger.d( "JWebSocketClient", "onOpen()" );
bindSN();
}
@@ -36,12 +36,11 @@ public class JWebSocketClient extends WebSocketClient {
@Override
public void onClose( int code, String reason, boolean remote ) {
Logger.e( "JWebSocketClient", "onClose()" );
Logger.d( "JWebSocketClient", "onClose()" );
}
@Override
public void onError( Exception ex ) {
Logger.e( "JWebSocketClient", "onError()" );
ex.printStackTrace();
Logger.d( "JWebSocketClient", "onError()" );
}
}

View File

@@ -3,6 +3,7 @@ package com.mogo.module.gps.simulator;
import android.content.Context;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.mogo.utils.UiThreadHandler;
/**
@@ -18,12 +19,15 @@ public class MogoGpsSimulatorManagerDebug implements IMogoGpsSimulatorManager {
@Override
public void open() {
WebSocketManager.getInstance().connect( true );
WebSocketManager.getInstance().connect();
}
@Override
public void close() {
WebSocketManager.getInstance().disConnect();
UiThreadHandler.postDelayed( () -> {
WebSocketManager.destroy();
}, 1_000L );
}
@Override

View File

@@ -2,12 +2,17 @@ package com.mogo.module.gps.simulator;
import com.alibaba.android.arouter.launcher.ARouter;
import com.mogo.commons.AbsMogoApplication;
import com.mogo.map.MogoLatLng;
import com.mogo.map.navi.IMogoCarLocationChangedListener;
import com.mogo.map.navi.IMogoNavi;
import com.mogo.service.IMogoServiceApis;
import com.mogo.service.MogoServicePaths;
import com.mogo.utils.TipToast;
import com.mogo.utils.UiThreadHandler;
import com.mogo.utils.logger.Logger;
import com.mogo.utils.network.utils.GsonUtil;
import org.java_websocket.handshake.ServerHandshake;
import org.json.JSONObject;
import java.net.URI;
@@ -29,7 +34,7 @@ public class WebSocketManager {
private JWebSocketClient mClient;
private URI mUri;
private ExecutorService mService = Executors.newSingleThreadExecutor();
private ExecutorService mService;
public static WebSocketManager getInstance() {
if ( sWebSocketManager == null ) {
@@ -55,7 +60,25 @@ public class WebSocketManager {
Logger.e( TAG, e, "error." );
}
}
@Override
public void onOpen( ServerHandshake handshakedata ) {
super.onOpen( handshakedata );
UiThreadHandler.post( () -> {
TipToast.shortTip( "模拟GPS开启成功" );
} );
}
@Override
public void onClose( int code, String reason, boolean remote ) {
super.onClose( code, reason, remote );
UiThreadHandler.post( () -> {
TipToast.shortTip( "模拟GPS关闭成功" );
} );
shutdownServiceQuietly();
}
};
initThreadService();
}
private void parseMessage( String jsonStr ) throws Exception {
@@ -67,56 +90,56 @@ public class WebSocketManager {
} else if ( jsonObject.has( "realTimeLocationVo" ) ) {
Logger.d( TAG, "收到定位消息" );
CationVo realTimeLocationVo = GsonUtil.objectFromJson( jsonObject.getString( "realTimeLocationVo" ), CationVo.class );
mNavi.setExtraGPSData(
realTimeLocationVo.getLon(),
realTimeLocationVo.getLat(),
realTimeLocationVo.getVehicleSpeed(),
1,
realTimeLocationVo.getDirection(),
realTimeLocationVo.getLocationTime()
);
UiThreadHandler.post( () -> {
mNavi.setExtraGPSData(
realTimeLocationVo.getLon(),
realTimeLocationVo.getLat(),
realTimeLocationVo.getVehicleSpeed(),
1,
realTimeLocationVo.getDirection(),
realTimeLocationVo.getLocationTime()
);
} );
}
}
public void connect( final boolean block ) {
public void connect() {
if ( mClient == null ) {
return;
}
if ( mClient.isOpen() ) {
return;
}
mService.execute( () -> {
if ( block ) {
try {
if ( mClient.isClosed() ) {
mClient.reconnectBlocking();
} else {
mClient.connectBlocking();
}
} catch ( InterruptedException e ) {
Logger.e( TAG, e, "error." );
}
} else {
if ( mClient.isClosed() ) {
mClient.reconnect();
} else {
mClient.connect();
}
try {
mClient.connectBlocking();
} catch ( InterruptedException e ) {
Logger.e( TAG, e, "error." );
}
} );
}
private void initThreadService() {
if ( mService == null || mService.isShutdown() ) {
mService = Executors.newSingleThreadExecutor();
}
}
public void disConnect() {
if ( mClient == null ) {
return;
}
if ( mClient.isClosing() || mClient.isClosed() || !mClient.isOpen() ) {
return;
}
try {
mService.execute( () -> { mClient.close(); } );
mService.execute( () -> {
mClient.close();
} );
} catch ( Exception e ) {
Logger.e( TAG, e, "error." );
}
}
public static void destroy() {
sWebSocketManager = null;
}
private void shutdownServiceQuietly() {
if ( mService != null && !mService.isShutdown() ) {
mService.shutdown();
}
mService = null;
}
}