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
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
package com.mm.android.deviceaddmodule.presenter;
 
import android.os.Message;
 
import com.mm.android.deviceaddmodule.R;
import com.mm.android.deviceaddmodule.contract.GatewayListConstract;
import com.mm.android.deviceaddmodule.helper.DeviceAddHelper;
import com.mm.android.deviceaddmodule.mobilecommon.base.LCBusinessHandler;
import com.mm.android.deviceaddmodule.mobilecommon.businesstip.BusinessErrorCode;
import com.mm.android.deviceaddmodule.mobilecommon.businesstip.HandleMessageCode;
import com.mm.android.deviceaddmodule.mobilecommon.entity.device.DHDevice;
import com.mm.android.deviceaddmodule.mobilecommon.entity.deviceadd.DeviceAddInfo;
import com.mm.android.deviceaddmodule.mobilecommon.entity.deviceadd.DeviceIntroductionInfo;
import com.mm.android.deviceaddmodule.model.DeviceAddModel;
 
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
 
public class GatewayListPresenter implements GatewayListConstract.Presenter {
    WeakReference<GatewayListConstract.View> mView;
    List<DHDevice> mGatewayData;
    int mSelectedPos=-1;
 
    public GatewayListPresenter(GatewayListConstract.View view) {
        mView = new WeakReference<>(view);
        mGatewayData = new ArrayList<>();
        initApView();
    }
 
    private void initApView() {
        DeviceAddInfo deviceAddInfo = DeviceAddModel.newInstance().getDeviceInfoCache();
        mView.get().setApSn(deviceAddInfo.getDeviceSn());
        DeviceIntroductionInfo deviceIntroductionInfo = deviceAddInfo.getDevIntroductionInfo();
        if (deviceIntroductionInfo != null) {
            String img = deviceIntroductionInfo.getImageInfos().get(DeviceAddHelper.OMSKey.ACCESSORY_MODE_FINISH_DEVICE_IMAGE);
            mView.get().setApImg(img);
        }
    }
 
    @Override
    public List<DHDevice> getGatewayData(boolean selectedGateway) {
        //TODO 网关列表选择不能依赖本地缓存,需要开新接口从服务拉数据
        return mGatewayData;
    }
 
    @Override
    public int gatewaySize() {
        return mGatewayData.size();
    }
 
    @Override
    public void dispatchCurSelect(int curPos) {
        if (curPos >= 0 && curPos < mGatewayData.size()) {
            DHDevice device = mGatewayData.get(curPos);
            DeviceAddInfo.GatewayInfo gatewayInfo = new DeviceAddInfo.GatewayInfo();
            gatewayInfo.setSn(device.getDeviceId());
            DeviceAddInfo deviceAddInfo = DeviceAddModel.newInstance().getDeviceInfoCache();
            deviceAddInfo.setGatewayInfo(gatewayInfo);
            String deviceId = deviceAddInfo.getGatewayInfo().getSn();
            String apId = deviceAddInfo.getDeviceSn();
            String apType = deviceAddInfo.getCatalog();
            String apModel = deviceAddInfo.getDeviceModel();
            pairSync(deviceId, apId, apType, apModel);
        } else {
            mView.get().goTipPage();
        }
    }
 
    @Override
    public int getSelectedpos() {
        return mSelectedPos;
    }
 
    private void pairSync(final String deviceId, final String apId, String apType, String apModel) {
        mView.get().showProgressDialog();
        LCBusinessHandler pairHandler = new LCBusinessHandler() {
            @Override
            public void handleBusiness(Message msg) {
                if (mView.get() != null && mView.get().isViewActive()) {
                    mView.get().cancelProgressDialog();
                    if (HandleMessageCode.HMC_SUCCESS == msg.what) {
                        boolean isSucceed = (boolean) msg.obj;
                        if (isSucceed) {
                            mView.get().goTipPage();
                            return;
                        }
                    }
                    apPairFailed(msg.what);
                }
            }
        };
        DeviceAddModel.newInstance().addApDevice(deviceId, apId, apType, apModel, pairHandler);
    }
 
    private void apPairFailed(int errorCode) {
        if(errorCode == BusinessErrorCode.BEC_DEVICE_ADD_AP_UPPER_LIMIT){
            mView.get().showToastInfo(R.string.add_device_add_ap_upper_limit);
        } else {
            mView.get().showToastInfo(R.string.add_device_config_failed);
        }
    }
}