add MoGoAiCloud sdk --> socket passport replace origin logic

This commit is contained in:
zhongchao
2021-02-23 11:20:39 +08:00
parent 024988b241
commit 632ececb99
45 changed files with 1285 additions and 873 deletions

View File

@@ -0,0 +1 @@
/build

View File

@@ -0,0 +1,42 @@
plugins {
id 'com.android.library'
}
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
defaultConfig {
minSdkVersion rootProject.ext.android.minSdkVersion
targetSdkVersion rootProject.ext.android.targetSdkVersion
versionCode Integer.valueOf(VERSION_CODE)
versionName getValueFromRootProperties("${project.name.replace("-", "_").toUpperCase()}_VERSION")
consumerProguardFiles "consumer-rules.pro"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation rootProject.ext.dependencies.mogoaicloudsocket
implementation rootProject.ext.dependencies.mogoaicloudpassport
if (Boolean.valueOf(RELEASE)) {
implementation rootProject.ext.dependencies.mogoutils
implementation rootProject.ext.dependencies.mogoserviceapi
} else {
implementation project(":foudations:mogo-utils")
implementation project(":services:mogo-service-api")
}
}
apply from: new File(rootProject.rootDir, "gradle/upload.gradle").toString()

View File

@@ -0,0 +1,3 @@
GROUP=com.mogo.aicloud.services
POM_ARTIFACT_ID=services-apk
VERSION_CODE=1

View File

@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mogo.aicloud.services">
</manifest>

View File

@@ -0,0 +1,63 @@
package com.mogo.aicloud.services.locationinfo;
import android.content.Context;
import androidx.annotation.Keep;
import com.mogo.map.location.MogoLocation;
import com.mogo.service.locationinfo.IMogoLocationInfoService;
import com.mogo.utils.logger.Logger;
public
/**
* @author congtaowang
* @since 2020/7/16
*
* 位置服务
*/
@Keep
class MogoLocationInfoServices implements IMogoLocationInfoService {
private static final String TAG = "MogoLocationInfoServices-apk";
private static volatile MogoLocationInfoServices sInstance;
private MogoLocationInfoServices() {
}
@Keep
public static MogoLocationInfoServices getInstance() {
if ( sInstance == null ) {
synchronized ( MogoLocationInfoServices.class ) {
if ( sInstance == null ) {
sInstance = new MogoLocationInfoServices();
}
}
}
return sInstance;
}
public synchronized void release() {
sInstance = null;
}
@Override
public void provideLocation( MogoLocation location ) {
Logger.d( TAG, "apk - provideLocation" );
}
@Override
public void start() {
Logger.d( TAG, "apk - start" );
}
@Override
public void stop() {
Logger.d( TAG, "apk - stop" );
}
@Override
public void init( Context context ) {
Logger.d( TAG, "apk - init" );
}
}

View File

@@ -0,0 +1,71 @@
package com.mogo.aicloud.services.passport;
import android.content.Context;
import androidx.annotation.Keep;
import com.mogo.cloud.passport.IMoGoTokenCallback;
import com.mogo.cloud.passport.MoGoAiCloudClient;
import com.mogo.service.passport.IMogoPassportManager;
import com.mogo.service.passport.IMogoTicketCallback;
public
/*
* @author congtaowang
* @since 2020/7/16
*
* 描述
*/
@Keep
class PassportManager implements IMogoPassportManager {
private static volatile PassportManager sInstance;
private IMogoTicketCallback mCallBack;
private PassportManager() {
MoGoAiCloudClient.getInstance().addTokenCallbacks(new IMoGoTokenCallback() {
@Override
public void onTokenGot(String token, String sn) {
if (mCallBack != null) {
mCallBack.onTicketGot(token);
}
}
@Override
public void onError(int code, String msg) {
if (mCallBack != null) {
mCallBack.onError(code, msg);
}
}
});
}
@Keep
public static PassportManager getInstance() {
if (sInstance == null) {
synchronized (PassportManager.class) {
if (sInstance == null) {
sInstance = new PassportManager();
}
}
}
return sInstance;
}
public synchronized void release() {
mCallBack = null;
sInstance = null;
}
@Override
public void requestTicket(final IMogoTicketCallback callback) {
this.mCallBack = callback;
MoGoAiCloudClient.getInstance().refreshToken();
}
@Override
public void init(Context context) {
}
}

View File

@@ -0,0 +1,153 @@
package com.mogo.aicloud.services.socket;
import android.content.Context;
import androidx.annotation.Keep;
import com.mogo.cloud.socket.IMogoCloudSocketMsgAckListener;
import com.mogo.cloud.socket.IMogoCloudSocketOnMessageListener;
import com.mogo.cloud.socket.SocketManager;
import com.mogo.service.connection.IMogoMsgAckListener;
import com.mogo.service.connection.IMogoOnMessageListener;
import com.mogo.service.connection.IMogoSocketManager;
import com.mogo.service.connection.MsgBody;
import com.mogo.utils.logger.Logger;
import com.zhidao.ptech.connsvr.commom.protocol.MogoCommon;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author arrow
* @since 2021-02-22
* <p>
* 长链实现:基于 netty
*/
@Keep
public class MogoAiCloudSocketManager implements IMogoSocketManager {
private static final String TAG = "SocketManager-apk";
private static volatile MogoAiCloudSocketManager sInstance;
private String mAppId;
private MogoAiCloudSocketManager(Context context) {
}
@Keep
public static MogoAiCloudSocketManager getInstance(Context context) {
if (sInstance == null) {
synchronized (MogoAiCloudSocketManager.class) {
if (sInstance == null) {
sInstance = new MogoAiCloudSocketManager(context);
}
}
}
return sInstance;
}
/**
* 管理消息分发
* <p>
* key - msgType
*/
private final Map<Integer, List<IMogoOnMessageListener>> mListeners = new ConcurrentHashMap<>();
/**
* 管理消息回执
* <p>
* key - msgId
*/
private final Map<Long, IMogoMsgAckListener> mAckListeners = new ConcurrentHashMap<>();
@Override
public void init(Context context, String appId) {
this.mAppId = appId;
SocketManager.getInstance().init(context);
}
@Override
public void registerOnMessageListener(int msgType, IMogoOnMessageListener listener) {
if (mListeners.containsKey(msgType)) {
com.mogo.cloud.utils.logger.Logger.w(TAG, "msgType %d is exist.", msgType);
return;
}
if (!mListeners.containsKey(msgType)) {
mListeners.put(msgType, new ArrayList<>());
}
mListeners.get(msgType).add(listener);
SocketManager.getInstance().registerOnMessageListener(msgType, mogoCloudSocketOnMessageListener);
}
@Override
public void unregisterOnMessageListener(int msgType) {
}
@Override
public void unregisterOnMessageListener(int msgType, IMogoOnMessageListener listener) {
if (listener == null) {
return;
}
if (!mListeners.containsKey(msgType)) {
return;
}
List<IMogoOnMessageListener> listeners = mListeners.get(msgType);
if (listeners != null) {
listeners.remove(listener);
}
SocketManager.getInstance().unregisterOnMessageListener(msgType, mogoCloudSocketOnMessageListener);
}
@Override
public void sendMsg(MsgBody body, IMogoMsgAckListener listener) {
Logger.d(TAG, "sendMsg.");
mAckListeners.put(body.getMsgId(), listener);
com.mogo.cloud.socket.MsgBody msgBody = new com.mogo.cloud.socket.MsgBody();
msgBody.msgType(body.getMsgType());
msgBody.ack(body.isAck());
msgBody.content(body.getContent());
SocketManager.getInstance().sendMsg(mAppId, MogoCommon.Product.mogoBussiness.getNumber(), msgBody, mogoCloudSocketMsgAckListener);
}
public synchronized void release() {
SocketManager.getInstance().release();
mListeners.clear();
sInstance = null;
}
@Override
public void init(Context context) {
}
private final IMogoCloudSocketMsgAckListener mogoCloudSocketMsgAckListener = msgId -> mAckListeners.get(msgId).onAck(msgId);
private final IMogoCloudSocketOnMessageListener mogoCloudSocketOnMessageListener = new IMogoCloudSocketOnMessageListener() {
@Override
public Class<Object> target(int msgType) {
List<IMogoOnMessageListener> listeners = mListeners.get(msgType);
if (listeners != null) {
return listeners.get(0).target();
} else {
return Object.class;
}
}
@Override
public void onMsgReceived(int msgType, Object obj) {
Logger.d(TAG,"onMsgReceived obj className : " + obj.getClass().getName());
List<IMogoOnMessageListener> listeners = mListeners.get(msgType);
if (listeners != null && !listeners.isEmpty()) {
for (IMogoOnMessageListener listener : listeners) {
if (listener != null) {
Logger.d(TAG, "received msgId = %s, content = %s", mAckListeners.get(msgType), obj);
listener.onMsgReceived(obj);
}
}
}
}
};
}

View File

@@ -0,0 +1 @@
/build

View File

@@ -0,0 +1,44 @@
plugins {
id 'com.android.library'
}
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
defaultConfig {
minSdkVersion rootProject.ext.android.minSdkVersion
targetSdkVersion rootProject.ext.android.targetSdkVersion
versionCode Integer.valueOf(VERSION_CODE)
versionName getValueFromRootProperties("${project.name.replace("-", "_").toUpperCase()}_VERSION")
consumerProguardFiles "consumer-rules.pro"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation rootProject.ext.dependencies.mogoaicloudsocket
implementation rootProject.ext.dependencies.mogoaicloudpassport
if (Boolean.valueOf(RELEASE)) {
implementation rootProject.ext.dependencies.mogoutils
implementation rootProject.ext.dependencies.mogocommons
implementation rootProject.ext.dependencies.mogoserviceapi
} else {
implementation project(":foudations:mogo-utils")
implementation project(":foudations:mogo-commons")
implementation project(":services:mogo-service-api")
}
}
apply from: new File(rootProject.rootDir, "gradle/upload.gradle").toString()

View File

@@ -0,0 +1,3 @@
GROUP=com.mogo.aicloud.services
POM_ARTIFACT_ID=services-sdk
VERSION_CODE=1

View File

@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mogo.aicloud.services">
</manifest>

View File

@@ -0,0 +1,79 @@
package com.mogo.aicloud.services.locationinfo;
import android.content.Context;
import androidx.annotation.Keep;
import com.mogo.commons.debug.DebugConfig;
import com.mogo.map.location.MogoLocation;
import com.mogo.service.locationinfo.IMogoLocationInfoService;
import com.mogo.utils.logger.Logger;
import com.zhidao.locupload.LocUploadConfig;
import com.zhidao.locupload.LocUploadManager;
public
/*
* @author congtaowang
* @since 2020/7/16
*
* 位置服务
*/
@Keep
class MogoLocationInfoServices implements IMogoLocationInfoService {
private static final String TAG = "MogoLocationInfoServices-sdk";
private static volatile MogoLocationInfoServices sInstance;
private MogoLocation mLocation;
private MogoLocationInfoServices() {
}
@Keep
public static MogoLocationInfoServices getInstance() {
if ( sInstance == null ) {
synchronized ( MogoLocationInfoServices.class ) {
if ( sInstance == null ) {
sInstance = new MogoLocationInfoServices();
}
}
}
return sInstance;
}
public synchronized void release() {
sInstance = null;
}
@Override
public void provideLocation( MogoLocation location ) {
mLocation = location;
Logger.d( TAG, "sdk - provideLocation" );
}
public MogoLocation getLocation() {
return mLocation;
}
@Override
public void start() {
LocUploadManager.getInstance().startUpload();
Logger.d( TAG, "sdk - start" );
}
@Override
public void stop() {
LocUploadManager.getInstance().stopUpload();
Logger.d( TAG, "sdk - stop" );
}
@Override
public void init( Context context ) {
LocUploadConfig.instance().
setAppId( DebugConfig.getSocketAppId() ).
setContext( context.getApplicationContext() ).
setLoggable( DebugConfig.isDebug() ).
setLocInterval( 2000L );
Logger.d( TAG, "sdk - init" );
}
}

View File

@@ -0,0 +1,133 @@
package com.mogo.aicloud.services.locationinfo;
import com.elegant.spi.annotations.Service;
import com.zhidao.locupload.location.LocationServiceProvider;
public
/*
* @author congtaowang
* @since 2020/7/16
*
* 描述
*/
@Service( value = LocationServiceProvider.class )
class MogoLocationSource extends LocationServiceProvider {
@Override
public float getBearing() {
if ( MogoLocationInfoServices.getInstance().getLocation() != null ) {
return MogoLocationInfoServices.getInstance().getLocation().getBearing();
}
return 0;
}
@Override
public float getAccuracy() {
if ( MogoLocationInfoServices.getInstance().getLocation() != null ) {
return MogoLocationInfoServices.getInstance().getLocation().getAccuracy();
}
return 0;
}
@Override
public String getProvider() {
if ( MogoLocationInfoServices.getInstance().getLocation() != null ) {
return MogoLocationInfoServices.getInstance().getLocation().getProvider();
}
return null;
}
@Override
public float getSpeed() {
if ( MogoLocationInfoServices.getInstance().getLocation() != null ) {
return MogoLocationInfoServices.getInstance().getLocation().getSpeed();
}
return 0;
}
@Override
public double getAltitude() {
if ( MogoLocationInfoServices.getInstance().getLocation() != null ) {
return MogoLocationInfoServices.getInstance().getLocation().getAltitude();
}
return 0;
}
@Override
public String getAdCode() {
if ( MogoLocationInfoServices.getInstance().getLocation() != null ) {
return MogoLocationInfoServices.getInstance().getLocation().getAdCode();
}
return null;
}
@Override
public int getLocType() {
if ( MogoLocationInfoServices.getInstance().getLocation() != null ) {
return MogoLocationInfoServices.getInstance().getLocation().getLocType();
}
return 0;
}
@Override
public double getLatitude() {
if ( MogoLocationInfoServices.getInstance().getLocation() != null ) {
return MogoLocationInfoServices.getInstance().getLocation().getLatitude();
}
return 0;
}
@Override
public double getLongitude() {
if ( MogoLocationInfoServices.getInstance().getLocation() != null ) {
return MogoLocationInfoServices.getInstance().getLocation().getLongitude();
}
return 0;
}
@Override
public long getTime() {
if ( MogoLocationInfoServices.getInstance().getLocation() != null ) {
return MogoLocationInfoServices.getInstance().getLocation().getTime();
}
return 0;
}
@Override
public String getCityCode() {
if ( MogoLocationInfoServices.getInstance().getLocation() != null ) {
return MogoLocationInfoServices.getInstance().getLocation().getCityCode();
}
return null;
}
@Override
public String getCityName() {
if ( MogoLocationInfoServices.getInstance().getLocation() != null ) {
return MogoLocationInfoServices.getInstance().getLocation().getCityName();
}
return null;
}
@Override
public int getGpsAccuracyStatus() {
if ( MogoLocationInfoServices.getInstance().getLocation() != null ) {
return MogoLocationInfoServices.getInstance().getLocation().getGpsAccuracyStatus();
}
return 0;
}
@Override
public int getSatellites() {
if ( MogoLocationInfoServices.getInstance().getLocation() != null ) {
return MogoLocationInfoServices.getInstance().getLocation().getSatellite();
}
return 0;
}
@Override
public int getCarStatus() {
// 常开状态
return 1;
}
}

View File

@@ -0,0 +1,71 @@
package com.mogo.aicloud.services.passport;
import android.content.Context;
import androidx.annotation.Keep;
import com.mogo.cloud.passport.IMoGoTokenCallback;
import com.mogo.cloud.passport.MoGoAiCloudClient;
import com.mogo.service.passport.IMogoPassportManager;
import com.mogo.service.passport.IMogoTicketCallback;
public
/*
* @author congtaowang
* @since 2020/7/16
*
* 描述
*/
@Keep
class PassportManager implements IMogoPassportManager {
private static volatile PassportManager sInstance;
private IMogoTicketCallback mCallBack;
private PassportManager() {
MoGoAiCloudClient.getInstance().addTokenCallbacks(new IMoGoTokenCallback() {
@Override
public void onTokenGot(String token, String sn) {
if (mCallBack != null) {
mCallBack.onTicketGot(token);
}
}
@Override
public void onError(int code, String msg) {
if (mCallBack != null) {
mCallBack.onError(code, msg);
}
}
});
}
@Keep
public static PassportManager getInstance() {
if (sInstance == null) {
synchronized (PassportManager.class) {
if (sInstance == null) {
sInstance = new PassportManager();
}
}
}
return sInstance;
}
public synchronized void release() {
mCallBack = null;
sInstance = null;
}
@Override
public void requestTicket(final IMogoTicketCallback callback) {
this.mCallBack = callback;
MoGoAiCloudClient.getInstance().refreshToken();
}
@Override
public void init(Context context) {
}
}

View File

@@ -0,0 +1,153 @@
package com.mogo.aicloud.services.socket;
import android.content.Context;
import androidx.annotation.Keep;
import com.mogo.cloud.socket.IMogoCloudSocketMsgAckListener;
import com.mogo.cloud.socket.IMogoCloudSocketOnMessageListener;
import com.mogo.cloud.socket.SocketManager;
import com.mogo.service.connection.IMogoMsgAckListener;
import com.mogo.service.connection.IMogoOnMessageListener;
import com.mogo.service.connection.IMogoSocketManager;
import com.mogo.service.connection.MsgBody;
import com.mogo.utils.logger.Logger;
import com.zhidao.ptech.connsvr.commom.protocol.MogoCommon;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author arrow
* @since 2021-02-22
* <p>
* 长链实现:基于 netty
*/
@Keep
public class MogoAiCloudSocketManager implements IMogoSocketManager {
private static final String TAG = "SocketManager-apk";
private static volatile MogoAiCloudSocketManager sInstance;
private String mAppId;
private MogoAiCloudSocketManager(Context context) {
}
@Keep
public static MogoAiCloudSocketManager getInstance(Context context) {
if (sInstance == null) {
synchronized (MogoAiCloudSocketManager.class) {
if (sInstance == null) {
sInstance = new MogoAiCloudSocketManager(context);
}
}
}
return sInstance;
}
/**
* 管理消息分发
* <p>
* key - msgType
*/
private final Map<Integer, List<IMogoOnMessageListener>> mListeners = new ConcurrentHashMap<>();
/**
* 管理消息回执
* <p>
* key - msgId
*/
private final Map<Long, IMogoMsgAckListener> mAckListeners = new ConcurrentHashMap<>();
@Override
public void init(Context context, String appId) {
this.mAppId = appId;
SocketManager.getInstance().init(context);
}
@Override
public void registerOnMessageListener(int msgType, IMogoOnMessageListener listener) {
if (mListeners.containsKey(msgType)) {
com.mogo.cloud.utils.logger.Logger.w(TAG, "msgType %d is exist.", msgType);
return;
}
if (!mListeners.containsKey(msgType)) {
mListeners.put(msgType, new ArrayList<>());
}
mListeners.get(msgType).add(listener);
SocketManager.getInstance().registerOnMessageListener(msgType, mogoCloudSocketOnMessageListener);
}
@Override
public void unregisterOnMessageListener(int msgType) {
}
@Override
public void unregisterOnMessageListener(int msgType, IMogoOnMessageListener listener) {
if (listener == null) {
return;
}
if (!mListeners.containsKey(msgType)) {
return;
}
List<IMogoOnMessageListener> listeners = mListeners.get(msgType);
if (listeners != null) {
listeners.remove(listener);
}
SocketManager.getInstance().unregisterOnMessageListener(msgType, mogoCloudSocketOnMessageListener);
}
@Override
public void sendMsg(MsgBody body, IMogoMsgAckListener listener) {
Logger.d(TAG, "sendMsg.");
mAckListeners.put(body.getMsgId(), listener);
com.mogo.cloud.socket.MsgBody msgBody = new com.mogo.cloud.socket.MsgBody();
msgBody.msgType(body.getMsgType());
msgBody.ack(body.isAck());
msgBody.content(body.getContent());
SocketManager.getInstance().sendMsg(mAppId, MogoCommon.Product.mogoBussiness.getNumber(), msgBody, mogoCloudSocketMsgAckListener);
}
public synchronized void release() {
SocketManager.getInstance().release();
mListeners.clear();
sInstance = null;
}
@Override
public void init(Context context) {
}
private final IMogoCloudSocketMsgAckListener mogoCloudSocketMsgAckListener = msgId -> mAckListeners.get(msgId).onAck(msgId);
private final IMogoCloudSocketOnMessageListener mogoCloudSocketOnMessageListener = new IMogoCloudSocketOnMessageListener() {
@Override
public Class<Object> target(int msgType) {
List<IMogoOnMessageListener> listeners = mListeners.get(msgType);
if (listeners != null) {
return listeners.get(0).target();
} else {
return Object.class;
}
}
@Override
public void onMsgReceived(int msgType, Object obj) {
Logger.d(TAG,"onMsgReceived obj className : " + obj.getClass().getName());
List<IMogoOnMessageListener> listeners = mListeners.get(msgType);
if (listeners != null && !listeners.isEmpty()) {
for (IMogoOnMessageListener listener : listeners) {
if (listener != null) {
Logger.d(TAG, "received msgId = %s, content = %s", mAckListeners.get(msgType), obj);
listener.onMsgReceived(obj);
}
}
}
}
};
}

View File

@@ -8,12 +8,11 @@ import com.mogo.commons.debug.DebugConfig;
import com.mogo.map.location.MogoLocation;
import com.mogo.service.locationinfo.IMogoLocationInfoService;
import com.mogo.utils.logger.Logger;
import com.zhidao.locupload.LocEnvironment;
import com.zhidao.locupload.LocUploadConfig;
import com.zhidao.locupload.LocUploadManager;
public
/**
/*
* @author congtaowang
* @since 2020/7/16
*