package com.hdl.sdk.sourceos.bind;
|
|
import android.text.TextUtils;
|
|
import com.hdl.hdlhttp.HxHttp;
|
import com.hdl.sdk.common.utils.SPUtils;
|
import com.hdl.sdk.connect.bean.response.BindInfoBean;
|
import com.hdl.sdk.connect.cloud.HDLException;
|
import com.hdl.sdk.connect.cloud.HDLResponse;
|
import com.hdl.sdk.connect.cloud.HdlCloudApi;
|
import com.hdl.sdk.connect.config.HDLCloudConfig;
|
import com.hdl.sdk.connect.config.HDLLinkConfig;
|
import com.hdl.sdk.sourceos.Rk3566Manager;
|
import com.hdl.sdk.sourceos.qrcode.QRCode;
|
import com.hdl.sdk.sourceos.utils.DeviceUtils;
|
import com.hdl.sdk.sourceos.utils.SPKey;
|
import com.hdl.sdk.sourceos.utils.thread.ThreadUtils;
|
|
import org.greenrobot.eventbus.EventBus;
|
|
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.atomic.AtomicLong;
|
|
import io.reactivex.rxjava3.disposables.CompositeDisposable;
|
import io.reactivex.rxjava3.disposables.Disposable;
|
|
/**
|
* Created by Tong on 2021/11/11.
|
* on+是否绑定
|
* 1、绑定成功需要查询住宅信息
|
* 2、扫码3分钟超时
|
*/
|
public class BindHomeService {
|
|
private ScheduledExecutorService queryThread;
|
|
private final CompositeDisposable compositeDisposable = new CompositeDisposable();
|
|
//最大时间
|
private static final long MAX_TIME = 3 * 60 * 1000;
|
|
//上一次执行时间
|
private final AtomicLong lastTime = new AtomicLong(0);
|
|
private BindHomeService() {
|
}
|
|
private static class SingletonInstance {
|
private static final BindHomeService INSTANCE = new BindHomeService();
|
}
|
|
public static BindHomeService getInstance() {
|
return SingletonInstance.INSTANCE;
|
}
|
|
/**
|
* 轮询是否绑定
|
*
|
* @param timestamp 时间戳
|
*/
|
public void startQuery(String timestamp) {
|
stopQuery();
|
lastTime.set(System.currentTimeMillis());
|
String packageName = Rk3566Manager.getInstance().getContext().getPackageName();
|
String unique = DeviceUtils.getUniqueCode();
|
queryThread = ThreadUtils.newScheduledThreadPool(1);
|
queryThread.scheduleWithFixedDelay(new Runnable() {
|
@Override
|
public void run() {
|
long timeMillis = System.currentTimeMillis();
|
if (timeMillis - lastTime.get() < MAX_TIME) {
|
compositeDisposable.add(isBind(packageName, unique, timestamp));
|
} else {
|
//超时了
|
EventBus.getDefault().post(new BindAuthEvent(BindAuthEvent.ON_PLUS_BINDING_TIMEOUT_ACTION));
|
stopQuery();
|
}
|
|
}
|
}, 0L, 800L, TimeUnit.MILLISECONDS);
|
}
|
|
/**
|
* 停止查询
|
*/
|
public void stopQuery() {
|
lastTime.set(0);
|
if (queryThread != null && !queryThread.isShutdown()) {
|
queryThread.shutdownNow();
|
}
|
if (!compositeDisposable.isDisposed()) {
|
compositeDisposable.dispose();
|
}
|
}
|
|
|
/**
|
* 通过保存的code查询是否绑定
|
*
|
* @param bindCode 保存的code查询是否绑定
|
* @return
|
*/
|
public static Disposable isBindByCode(String bindCode) {
|
if (TextUtils.isEmpty(bindCode)) return null;
|
String[] code = bindCode.split("_");
|
if (code.length < 3) {
|
return null;
|
}
|
String packageName = code[0];
|
String unique = code[1];
|
String timestamp = code[2];
|
return isBind(packageName, unique, timestamp);
|
}
|
|
public static Disposable isBind(String timestamp) {
|
String packageName = Rk3566Manager.getInstance().getContext().getPackageName();
|
String unique = DeviceUtils.getUniqueCode();
|
return isBind(packageName, unique, timestamp);
|
}
|
|
/**
|
* @param timestamp 时间戳
|
* @param packageName 包名
|
* @param unique 序列号
|
* @return
|
*/
|
public static Disposable isBind(String packageName, String unique, String timestamp) {
|
return isBind(packageName, unique, timestamp, new HDLResponse<BindInfoBean>() {
|
@Override
|
public void onResponse(BindInfoBean response) {
|
handleBind(response, timestamp);
|
}
|
|
@Override
|
public void onFailure(HDLException e) {
|
//网络异常、或者接口报错
|
EventBus.getDefault().post(new BindAuthEvent(BindAuthEvent.ON_PLUS_BINDING_ERROR_ACTION));
|
}
|
});
|
}
|
|
|
/**
|
* @param timestamp 时间戳
|
* @param packageName 包名
|
* @param unique 序列号
|
* @return
|
*/
|
public static Disposable isBind(String packageName, String unique, String timestamp, HDLResponse<BindInfoBean> response) {
|
return HxHttp.builder()
|
.url(HdlCloudApi.IS_BIND_URL)
|
.params("packageName", packageName)
|
.params("unique", unique)
|
.params("qrcodeTimestamp", timestamp)
|
.build()
|
.executePost()
|
.subscribeWith(response);
|
}
|
|
|
/**
|
* 处理绑定
|
*
|
* @param response 返回
|
* @param timestamp 时间戳
|
*/
|
private static void handleBind(BindInfoBean response, String timestamp) {
|
if (!TextUtils.isEmpty(response.getHomeId())) {
|
|
SPUtils.saveSerializableEntity(SPKey.BIND_HOME_INFO, response);
|
SPUtils.put(SPKey.BIND_CODE, QRCode.createBindQRCodeInfo(timestamp));
|
|
HDLCloudConfig.getInstance().setToken(response.getToken());
|
HDLCloudConfig.getInstance().setRefreshToken(response.getRefreshToken());
|
|
EventBus.getDefault().post(new BindAuthEvent(BindAuthEvent.ON_PLUS_BINDING_SUCCEED_ACTION));
|
|
getInstance().stopQuery();
|
}
|
}
|
|
|
}
|