wxr
2022-11-24 2af932533ef851bf983385244e9912976dbd4daa
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
package com.mm.android.deviceaddmodule.presenter;
 
import android.os.Message;
 
import com.mm.android.deviceaddmodule.contract.ApPairConstract;
import com.mm.android.deviceaddmodule.mobilecommon.base.LCBusinessHandler;
import com.mm.android.deviceaddmodule.mobilecommon.businesstip.HandleMessageCode;
import com.mm.android.deviceaddmodule.mobilecommon.entity.AddApResult;
import com.mm.android.deviceaddmodule.mobilecommon.entity.deviceadd.DeviceAddInfo;
import com.mm.android.deviceaddmodule.model.DeviceAddModel;
 
import java.lang.ref.WeakReference;
 
public class ApPairPresenter implements ApPairConstract.Presenter {
    WeakReference<ApPairConstract.View> mView;
 
    public ApPairPresenter(ApPairConstract.View view) {
        mView = new WeakReference<>(view);
    }
 
    @Override
    public void pair() {
        DeviceAddInfo deviceAddInfo = DeviceAddModel.newInstance().getDeviceInfoCache();
        DeviceAddInfo.GatewayInfo gatewayInfo = deviceAddInfo.getGatewayInfo();
        if(gatewayInfo == null){    //异常保护
            return;
        }
        String deviceId = gatewayInfo.getSn();
        String apId = deviceAddInfo.getDeviceSn();
        getPariResultSync(deviceId, apId);
    }
 
    @Override
    public void stopPair() {
        DeviceAddModel.newInstance().setLoop(false);
    }
 
    private void getPariResultSync(final String deviceId, final String apId) {
        LCBusinessHandler resultHandler = new LCBusinessHandler() {
            @Override
            public void handleBusiness(Message msg) {
                if (HandleMessageCode.HMC_SUCCESS == msg.what) {
                    AddApResult addApResult = (AddApResult) msg.obj;
                    apPairSucceed(addApResult);
                }else{
                    getPariResultSync(deviceId, apId);
                }
            }
        };
        DeviceAddModel.newInstance().getAddApResultAsync(deviceId, apId, resultHandler);
    }
 
    private void apPairSucceed(AddApResult addApResult) {
        if(mView.get() != null){
            mView.get().goApBindSuccessPage(addApResult);
        }
    }
 
}