panlili2024
2025-03-05 134209ad70f82051da3ce63471df0cc8f778e57d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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();
        }
    }
 
 
}