diff --git a/modules/mogo-realtime/src/main/java/com/mogo/realtime/core/SnapshotUploadInTime.java b/modules/mogo-realtime/src/main/java/com/mogo/realtime/core/SnapshotUploadInTime.java index 9c7a0b2..ad850f9 100644 --- a/modules/mogo-realtime/src/main/java/com/mogo/realtime/core/SnapshotUploadInTime.java +++ b/modules/mogo-realtime/src/main/java/com/mogo/realtime/core/SnapshotUploadInTime.java @@ -3,22 +3,23 @@ package com.mogo.realtime.core; import android.content.Context; import com.mogo.cloud.passport.MoGoAiCloudClient; -import com.mogo.realtime.entity.CloudLocationInfo; +import com.mogo.realtime.Imp.RealTimeProviderImp; import com.mogo.realtime.location.MogoRTKLocation; import com.mogo.realtime.socket.IMogoCloudOnMsgListener; import com.mogo.realtime.socket.SocketHandler; -import java.util.List; - /** * 上报坐标服务 */ -public class SnapshotUploadInTime implements MogoRTKLocation.RTKLocationListener { +public class SnapshotUploadInTime implements UploadInTimeHandler.IUploadInTimeListener { private static final String TAG = "SnapshotUploadInTime"; private static volatile SnapshotUploadInTime sInstance; + //是否使用外部定位数据 + private boolean isUseExternalLocation = false; private SnapshotUploadInTime() { + isUseExternalLocation = MoGoAiCloudClient.getInstance().getAiCloudClientConfig().getIsUseExternalLocation(); } public static SnapshotUploadInTime getInstance() { @@ -36,26 +37,36 @@ public class SnapshotUploadInTime implements MogoRTKLocation.RTKLocationListener sInstance = null; } - //todo 调用 public void start(Context context, String appId, IMogoCloudOnMsgListener listener) { - if (MoGoAiCloudClient.getInstance().getAiCloudClientConfig().getIsUseExternalLocation()) {//todo 区分外部内部 + //开启内部定位 + if (!isUseExternalLocation) { MogoRTKLocation.getInstance().init(); - MogoRTKLocation.getInstance().registerRTKLocationListener(this); } + //开启长链服务 SocketHandler.getInstance().initSocket(context, appId, listener); SocketHandler.getInstance().resetUploadDelayListener(resetTime -> - MogoRTKLocation.getInstance().resetUploadDelay(resetTime) + UploadInTimeHandler.getInstance().resetUploadDelay(resetTime) ); + //上传数据服务启动,定时上报 + UploadInTimeHandler.getInstance().start(); + UploadInTimeHandler.getInstance().setUploadInTimeListener(this); + } - //todo 调用 public void stop() { + UploadInTimeHandler.getInstance().stop(); SocketHandler.getInstance().stop(); + if (!isUseExternalLocation) { + MogoRTKLocation.getInstance().stop(); + } } @Override - public void onLocationChanged(List cloudLocationInfos) { - SocketHandler.getInstance().sendMsg(cloudLocationInfos); + public void sendLocationData() { + if (!isUseExternalLocation) { + SocketHandler.getInstance().sendMsg(MogoRTKLocation.getInstance().sendLocationData()); + } else { + SocketHandler.getInstance().sendMsg(RealTimeProviderImp.getInstance().getLocationMsg()); + } } - } diff --git a/modules/mogo-realtime/src/main/java/com/mogo/realtime/core/UploadInTimeHandler.java b/modules/mogo-realtime/src/main/java/com/mogo/realtime/core/UploadInTimeHandler.java new file mode 100644 index 0000000..f9ce08a --- /dev/null +++ b/modules/mogo-realtime/src/main/java/com/mogo/realtime/core/UploadInTimeHandler.java @@ -0,0 +1,77 @@ +package com.mogo.realtime.core; + +import android.os.Handler; +import android.os.Message; + +import com.mogo.utils.WorkThreadHandler; + +public class UploadInTimeHandler { + + private static final String TAG = "UploadInTimeHandler"; + private static final int MSG_DATA_CHANGED = 0x100; + private static final long MSG_DATA_INTERNAL = 500L; + private final long uploadDelay = MSG_DATA_INTERNAL; + + private Handler mHandler; + private static volatile UploadInTimeHandler uploadInTimeHandler; + + private IUploadInTimeListener iUploadInTimeListener; + + private UploadInTimeHandler(){ + + } + + public static UploadInTimeHandler getInstance(){ + if(uploadInTimeHandler == null){ + synchronized (UploadInTimeHandler.class){ + if(uploadInTimeHandler == null){ + uploadInTimeHandler = new UploadInTimeHandler(); + } + } + } + return uploadInTimeHandler; + } + + public void start(){ + mHandler = new Handler(WorkThreadHandler.newInstance(TAG).getLooper()) { + @Override + public void handleMessage(Message msg) { + super.handleMessage(msg); + if (msg.what == MSG_DATA_CHANGED) { + mHandler.sendEmptyMessageDelayed(MSG_DATA_CHANGED, uploadDelay); + if(iUploadInTimeListener != null){ + iUploadInTimeListener.sendLocationData(); + } + } + } + }; + mHandler.sendEmptyMessage(MSG_DATA_CHANGED); + } + + public void setUploadInTimeListener(IUploadInTimeListener uploadInTimeListener){ + this.iUploadInTimeListener = uploadInTimeListener; + } + + /** + * 默认保持{@link #uploadDelay}间隔进行位置上报,如遇服务端控制,进行上报间隔修改 + * + * @param delay 上报间隔 + */ + public void resetUploadDelay(long delay) { + if (mHandler != null && mHandler.hasMessages(MSG_DATA_CHANGED)) { + mHandler.removeMessages(MSG_DATA_CHANGED); + mHandler.sendEmptyMessageDelayed(MSG_DATA_CHANGED, delay); + } + } + + public void stop(){ + mHandler.removeMessages(MSG_DATA_CHANGED); + mHandler = null; + } + + public interface IUploadInTimeListener { + + void sendLocationData(); + } + +} diff --git a/modules/mogo-realtime/src/main/java/com/mogo/realtime/location/MogoRTKLocation.java b/modules/mogo-realtime/src/main/java/com/mogo/realtime/location/MogoRTKLocation.java index 480a291..23a77ef 100644 --- a/modules/mogo-realtime/src/main/java/com/mogo/realtime/location/MogoRTKLocation.java +++ b/modules/mogo-realtime/src/main/java/com/mogo/realtime/location/MogoRTKLocation.java @@ -1,35 +1,28 @@ package com.mogo.realtime.location; -import android.content.BroadcastReceiver; import android.content.Context; -import android.content.Intent; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; -import android.os.Handler; -import android.os.Message; import com.mogo.cloud.passport.MoGoAiCloudClient; import com.mogo.realtime.entity.CloudLocationInfo; -import com.mogo.utils.WorkThreadHandler; import com.mogo.utils.logger.Logger; import java.util.ArrayList; import java.util.List; -//todo 改造 +/** + * AI云SDK内部定位服务 + */ public class MogoRTKLocation { private static final String TAG = "MogoRTKLocation"; - private static final int MSG_DATA_CHANGED = 0x100; - private static final long MSG_DATA_INTERNAL = 500L; - private Handler mHandler; private LocationManager locationManager; - private RTKLocationListener rtkLocationListener; - private List cacheList = new ArrayList<>(); + private final List cacheList = new ArrayList<>(); public static MogoRTKLocation getInstance() { return RTKHolder.rtkLoc; @@ -40,41 +33,15 @@ public class MogoRTKLocation { } private MogoRTKLocation() { - mHandler = new Handler(WorkThreadHandler.newInstance(TAG).getLooper()) { - @Override - public void handleMessage(Message msg) { - super.handleMessage(msg); - if (msg.what == MSG_DATA_CHANGED) { - mHandler.sendEmptyMessageDelayed(MSG_DATA_CHANGED, uploadDelay); - sendLocationData(); - } - } - }; - mHandler.sendEmptyMessage(MSG_DATA_CHANGED); - Logger.d(TAG, "构造方法开始发送消息"); + } - public interface RTKLocationListener { - void onLocationChanged(List cloudLocationInfos); - } - - private void sendLocationData() { - - if (rtkLocationListener != null) { - List list = new ArrayList<>(cacheList); - rtkLocationListener.onLocationChanged(list); - } + public List sendLocationData() { + List list = new ArrayList<>(cacheList); if (cacheList != null && cacheList.size() > 0) { cacheList.clear(); } - } - - public void registerRTKLocationListener(RTKLocationListener locationListener) { - rtkLocationListener = locationListener; - } - - public void unregisterRTKLocationListener() { - rtkLocationListener = null; + return list; } public void init() { @@ -152,26 +119,4 @@ public class MogoRTKLocation { } } - private long uploadDelay = MSG_DATA_INTERNAL; - - private FixUploadDelayReceiver fixUploadDelayReceiver = new FixUploadDelayReceiver(); - - private class FixUploadDelayReceiver extends BroadcastReceiver { - @Override - public void onReceive(Context context, Intent intent) { - uploadDelay = intent.getIntExtra("fixTime", 0); - } - } - - /** - * 默认保持{@link #uploadDelay}间隔进行位置上报,如遇服务端控制,进行上报间隔修改 - * - * @param delay 上报间隔 - */ - public void resetUploadDelay(long delay) { - if (mHandler != null && mHandler.hasMessages(MSG_DATA_CHANGED)) { - mHandler.removeMessages(MSG_DATA_CHANGED); - mHandler.sendEmptyMessageDelayed(MSG_DATA_CHANGED, delay); - } - } } diff --git a/modules/mogo-realtime/src/main/java/com/mogo/realtime/socket/SocketHandler.java b/modules/mogo-realtime/src/main/java/com/mogo/realtime/socket/SocketHandler.java index f8b60c7..9f0c032 100644 --- a/modules/mogo-realtime/src/main/java/com/mogo/realtime/socket/SocketHandler.java +++ b/modules/mogo-realtime/src/main/java/com/mogo/realtime/socket/SocketHandler.java @@ -106,6 +106,10 @@ public class SocketHandler { }; public void sendMsg(List cloudLocationInfo) { + if(cloudLocationInfo == null){ + Log.e(TAG,"请检查传入数组对象为Null"); + return; + } CloudLocationInfo lastInfo = null; // 如果数组内容不为空,就用数组最后一个值 if (!cloudLocationInfo.isEmpty()) {