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
104
105
106
107
package com.mm.android.deviceaddmodule.presenter;
 
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
 
import com.dahua.mobile.utility.network.DHWifiUtil;
import com.mm.android.deviceaddmodule.R;
import com.mm.android.deviceaddmodule.contract.DevSecCodeConstract;
import com.mm.android.deviceaddmodule.helper.DeviceAddHelper;
import com.mm.android.deviceaddmodule.mobilecommon.AppConsume.BusinessException;
import com.mm.android.deviceaddmodule.mobilecommon.base.LCBusinessHandler;
import com.mm.android.deviceaddmodule.mobilecommon.businesstip.BusinessErrorTip;
import com.mm.android.deviceaddmodule.mobilecommon.businesstip.HandleMessageCode;
import com.mm.android.deviceaddmodule.mobilecommon.entity.deviceadd.DeviceAddInfo;
import com.mm.android.deviceaddmodule.mobilecommon.utils.StringUtils;
import com.mm.android.deviceaddmodule.model.DeviceAddModel;
 
import java.lang.ref.WeakReference;
 
public class DevSecCodePresenter implements DevSecCodeConstract.Presenter {
    WeakReference<DevSecCodeConstract.View> mView;
    String mDeviceSn;
    boolean mIsWifiOfflineMode;
    DHWifiUtil mDHWifiUtil;
 
    public DevSecCodePresenter(DevSecCodeConstract.View view) {
        mView = new WeakReference<>(view);
        mDeviceSn = DeviceAddModel.newInstance().getDeviceInfoCache().getDeviceSn();
        mIsWifiOfflineMode = DeviceAddModel.newInstance().getDeviceInfoCache().isWifiOfflineMode();
        mDHWifiUtil = new DHWifiUtil(mView.get().getContextInfo());
    }
 
    @Override
    public void validate() {
        if (!checkInput(mView.get().getDeviceSecCode())) {
            mView.get().showToastInfo(R.string.mobile_common_bec_add_device_valid_error);
            return;
        }
        mView.get().showProgressDialog();
        bindDevice(mView.get().getDeviceSecCode());
    }
 
    private boolean checkInput(String regCode) {
        return !(TextUtils.isEmpty(regCode) || regCode.length() < 6);
    }
 
    // 无auth能力集的设备没有设备密码(进入该流程肯定无auth能力集)
    private void bindDevice(final String code) {
        final DeviceAddInfo deviceAddInfo = DeviceAddModel.newInstance().getDeviceInfoCache();
        String sn = deviceAddInfo.getDeviceSn();
        LCBusinessHandler bindHandler = new LCBusinessHandler() {
            @Override
            public void handleBusiness(Message msg) {
                if (mView.get() == null
                        || (mView.get() != null && !mView.get().isViewActive())) {
                    return;
                }
                mView.get().cancelProgressDialog();
                if (msg.what == HandleMessageCode.HMC_SUCCESS) {
                    if (DeviceAddInfo.BindStatus.bindByOther.name().equals(deviceAddInfo.getBindStatus())) {           //设备被其他帐户绑定
                        mView.get().goOtherUserBindTipPage();
                        return;
                    }
                    addDeviceToPolicy(code);
                } else {
                    String errorDesp = ((BusinessException) msg.obj).errorDescription;
                    if (errorDesp.contains("DV1014")) {  //设备绑定错误连续超过10次,限制绑定30分钟
                        mView.get().goErrorTipPage(DeviceAddHelper.ErrorCode.COMMON_ERROR_DEVICE_BIND_MROE_THAN_TEN);
                    } else if (errorDesp.contains("DV1015")) {  //设备绑定错误连续超过20次,限制绑定24小时
                        mView.get().goErrorTipPage(DeviceAddHelper.ErrorCode.COMMON_ERROR_DEVICE_MROE_THAN_TEN_TWICE);
                    } else if (errorDesp.contains("DV1045")) {  // 设备冲突
                        mView.get().goErrorTipPage(DeviceAddHelper.ErrorCode.COMMON_ERROR_DEVICE_SN_CODE_CONFLICT);
                    } else if (errorDesp.contains("DV1044")) {  //设备IP不在统一局域网内
                        mView.get().goErrorTipPage(DeviceAddHelper.ErrorCode.COMMON_ERROR_DEVICE_IP_ERROR);
                    } else if (errorDesp.contains("DV1027")) { // 设备安全码错误
                        mView.get().showToastInfo(BusinessErrorTip.getErrorTip(msg));
                    } else if (errorDesp.contains("DV1016")||errorDesp.contains("DV1025")) {  // 设备密码错误(环回认证失败)/设备SC码或设备密码错误
                        mView.get().showToastInfo(R.string.add_device_verify_device_pwd_failed);
                        mView.get().goDevLoginPage();
                        deviceAddInfo.setDevicePwd("");
                    } else if (errorDesp.contains("DV1037")) {  //NB设备的imei和device id 不匹配
                        mView.get().goErrorTipPage(DeviceAddHelper.ErrorCode.COMMON_ERROR_DEVICE_SN_OR_IMEI_NOT_MATCH);
                    } else {
                        mView.get().showToastInfo(BusinessErrorTip.getErrorTip(msg));
                    }
                }
            }
        };
        DeviceAddModel.newInstance().bindDevice(sn, code, bindHandler);
    }
 
    private void addDeviceToPolicy(String sn){
        LCBusinessHandler policyHandler = new LCBusinessHandler(Looper.getMainLooper()) {
 
            @Override
            public void handleBusiness(Message msg) {
                if (msg.what == HandleMessageCode.HMC_SUCCESS){
                    mView.get().goBindSuceesPage();
                }else{
                    mView.get().showToastInfo(BusinessErrorTip.getErrorTip(msg));
                }
            }
        };
        DeviceAddModel.newInstance().addPolicy(sn,policyHandler);
    }
}