socketManager and socketHandler fix
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
package com.mogo.realtime.socket;
|
||||
|
||||
import com.mogo.realtime.entity.MogoSnapshotSetData;
|
||||
|
||||
public interface IMogoCloudOnMsgListener {
|
||||
|
||||
void onMsgReceived(MogoSnapshotSetData mogoSnapshotSetData);
|
||||
}
|
||||
@@ -2,26 +2,42 @@ package com.mogo.realtime.socket;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.SystemClock;
|
||||
import android.util.Log;
|
||||
|
||||
import com.mogo.cloud.passport.MoGoAiCloudClient;
|
||||
import com.mogo.cloud.socket.IMogoCloudSocketOnMessageListener;
|
||||
import com.mogo.cloud.socket.MsgBody;
|
||||
import com.mogo.cloud.socket.SocketManager;
|
||||
import com.mogo.cloud.socket.WebSocketData;
|
||||
import com.mogo.realtime.Interface.RealTimeApisHandler;
|
||||
import com.mogo.realtime.constant.SimpleLocationCorrectStrategy;
|
||||
import com.mogo.realtime.entity.ADASRecognizedResult;
|
||||
import com.mogo.realtime.entity.CloudLocationInfo;
|
||||
import com.mogo.realtime.entity.LocationResult;
|
||||
import com.mogo.realtime.entity.MogoSnapshotSetData;
|
||||
import com.mogo.realtime.entity.OnePerSecondSendContent;
|
||||
import com.mogo.realtime.util.MortonCode;
|
||||
import com.mogo.utils.network.utils.GsonUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.mogo.cloud.socket.WebSocketMsgType.MSG_TYPE_ACK;
|
||||
import static com.mogo.cloud.socket.WebSocketMsgType.MSG_TYPE_DOWNLINK_CAR_DATA;
|
||||
import static com.mogo.cloud.socket.WebSocketMsgType.MSG_TYPE_UPLINK_CAR_DATA;
|
||||
|
||||
/**
|
||||
* @author liujing
|
||||
* @description 描述
|
||||
* @since: 2021/1/22
|
||||
*
|
||||
*/
|
||||
public class SocketHandler {
|
||||
|
||||
private static final String TAG = "SocketHandler";
|
||||
private static volatile SocketHandler mInstance;
|
||||
|
||||
private long serverTime = 0;
|
||||
private long receiveMsgTime = 0;
|
||||
private List<IMogoCloudSocketOnMessageListener> listenerList;
|
||||
private CloudLocationInfo mLastInfo;
|
||||
private final List<IMogoCloudOnMsgListener> listenerList = new ArrayList<>();
|
||||
|
||||
public static SocketHandler getInstance() {
|
||||
if (mInstance == null) {
|
||||
@@ -37,27 +53,23 @@ public class SocketHandler {
|
||||
/*
|
||||
* useInner: 是否注册自己内部的监听
|
||||
* */
|
||||
public void initSocket(Context context, boolean useInner, String appId, IMogoCloudSocketOnMessageListener listener) {
|
||||
public void initSocket(Context context, boolean useInner, String appId, IMogoCloudOnMsgListener listener) {
|
||||
SocketManager.getInstance().init(context, appId);
|
||||
if (useInner) {
|
||||
SocketManager.getInstance().registerOnMessageListener(0x040002, onMessageListener);
|
||||
SocketManager.getInstance().registerOnMessageListener(0x040003, onMessageListener);
|
||||
} else {
|
||||
SocketManager.getInstance().registerOnMessageListener(0x040002, listener);
|
||||
SocketManager.getInstance().registerOnMessageListener(0x040003, listener);
|
||||
SocketManager.getInstance().registerOnMessageListener(0x040002, onMessageListener);
|
||||
SocketManager.getInstance().registerOnMessageListener(0x040003, onMessageListener);
|
||||
if (listener != null && !listenerList.contains(listener)) {
|
||||
listenerList.add(listener);
|
||||
}
|
||||
}
|
||||
|
||||
private final IMogoCloudSocketOnMessageListener<String> onMessageListener = new IMogoCloudSocketOnMessageListener<String>() {
|
||||
private final IMogoCloudSocketOnMessageListener<WebSocketData> onMessageListener = new IMogoCloudSocketOnMessageListener<WebSocketData>() {
|
||||
@Override
|
||||
public Class<String> target() {
|
||||
return String.class;
|
||||
public Class<WebSocketData> target() {
|
||||
return WebSocketData.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMsgReceived(String message) {
|
||||
WebSocketData webSocketData = GsonUtil.objectFromJson(message, WebSocketData.class);
|
||||
public void onMsgReceived(WebSocketData webSocketData) {
|
||||
int msgType = webSocketData.getMsgType();
|
||||
if (msgType == MSG_TYPE_ACK.getMsgType()) {
|
||||
if (webSocketData.getUtcTime() > 0) {
|
||||
@@ -70,17 +82,93 @@ public class SocketHandler {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (msgType == MSG_TYPE_DOWNLINK_CAR_DATA.getMsgType()) {
|
||||
MogoSnapshotSetData data = GsonUtil.objectFromJson(webSocketData.getData(), MogoSnapshotSetData.class);
|
||||
if (data == null) {
|
||||
Log.e(TAG, "onMsgReceived MogoSnapshotSetData == null ");
|
||||
return;
|
||||
}
|
||||
for (IMogoCloudOnMsgListener listener : listenerList) {
|
||||
listener.onMsgReceived(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public void stop() {
|
||||
//todo
|
||||
// MogoRTKLocation.getInstance().unregisterRTKLocationListener();
|
||||
// MogoRTKLocation.getInstance().stop();
|
||||
for (int i = 0; i < listenerList.size(); i++) {
|
||||
SocketManager.getInstance().unregisterOnMessageListener(0x040002, listenerList.get(i));
|
||||
SocketManager.getInstance().unregisterOnMessageListener(0x040003, listenerList.get(i));
|
||||
public void sendMsg(List<CloudLocationInfo> cloudLocationInfo) {
|
||||
CloudLocationInfo lastInfo = null;
|
||||
// 如果数组内容不为空,就用数组最后一个值
|
||||
if (!cloudLocationInfo.isEmpty()) {
|
||||
lastInfo = cloudLocationInfo.get(cloudLocationInfo.size() - 1);
|
||||
mLastInfo = lastInfo;
|
||||
}
|
||||
if (lastInfo == null) {
|
||||
lastInfo = mLastInfo;
|
||||
}
|
||||
LocationResult locationResult = null;
|
||||
if (lastInfo != null) {
|
||||
// 定位点预测纠偏
|
||||
lastInfo = SimpleLocationCorrectStrategy.getInstance().correct(lastInfo);
|
||||
locationResult = new LocationResult();
|
||||
if (lastInfo != null) {
|
||||
locationResult.lastCoordinate = lastInfo;
|
||||
locationResult.mortonCode = MortonCode.wrapEncodeMorton(lastInfo.getLon(), lastInfo.getLat());
|
||||
}
|
||||
locationResult.coordinates = new ArrayList<>();
|
||||
locationResult.sn = MoGoAiCloudClient.getInstance().getAiCloudClientConfig().getSn();
|
||||
locationResult.coordinates.addAll(cloudLocationInfo);
|
||||
}
|
||||
List<ADASRecognizedResult> recognizedResults = RealTimeApisHandler.getInstance().getApis().getRecognizedResultManager().getLastADASRecognizedResult();//外显接口返回
|
||||
OnePerSecondSendContent content = new OnePerSecondSendContent();
|
||||
content.self = locationResult;
|
||||
content.adas = recognizedResults;
|
||||
|
||||
if (content.self == null &&
|
||||
(content.adas == null || content.adas.isEmpty())) {
|
||||
Log.d(TAG, "no information to sent");
|
||||
return;
|
||||
}
|
||||
WebSocketData webSocketData = new WebSocketData();
|
||||
webSocketData.setMsgType(MSG_TYPE_UPLINK_CAR_DATA.getMsgType());
|
||||
webSocketData.setSeq(computeSendMsgTime());
|
||||
webSocketData.setSn(MoGoAiCloudClient.getInstance().getAiCloudClientConfig().getSn());
|
||||
webSocketData.setData(GsonUtil.jsonFromObject(content));
|
||||
String msg = GsonUtil.jsonFromObject(webSocketData);
|
||||
|
||||
int msgType = 0x040003; //低频数据
|
||||
if (cloudLocationInfo.size() > 2) {
|
||||
msgType = 0x040002; //高频数据
|
||||
}
|
||||
MsgBody msgBody = new MsgBody();
|
||||
msgBody.msgType(msgType);
|
||||
msgBody.content(msg);
|
||||
SocketManager.getInstance().sendMsg(msgBody, msgId -> {
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
SocketManager.getInstance().unregisterOnMessageListener(0x040002, onMessageListener);
|
||||
SocketManager.getInstance().unregisterOnMessageListener(0x040003, onMessageListener);
|
||||
listenerList.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务端会在ack数据中增加服务端的时间戳{@link #serverTime}, 收到消息时,记录收到消息的时间{@link #receiveMsgTime},收消息的时间用{@link SystemClock#elapsedRealtime()}可以避免系统时间改变造成的影响
|
||||
* 在发送数据时,使用serverTime+(elapseRealtime()-receiveMsgTime)计算发送数据时的时间戳,这个时间戳是基本相对服务端的时间戳为基准的时间,忽略了ack下发时的时间延迟
|
||||
* <p>
|
||||
* 如果{@link #serverTime}或{@link #receiveMsgTime}数据有异常,则使用{@link System#currentTimeMillis()}当做当前时间,作为容错
|
||||
*
|
||||
* @return 基本相对服务端的时间戳为基准的时间
|
||||
*/
|
||||
private long computeSendMsgTime() {
|
||||
long sendMsgTime;
|
||||
if (serverTime > 0 && receiveMsgTime > 0) {
|
||||
sendMsgTime = serverTime + (SystemClock.elapsedRealtime() - receiveMsgTime);
|
||||
} else {
|
||||
sendMsgTime = System.currentTimeMillis();
|
||||
}
|
||||
return sendMsgTime;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user