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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package com.mm.android.deviceaddmodule.presenter;
 
import android.os.Message;
import android.text.TextUtils;
 
import com.mm.android.deviceaddmodule.R;
import com.mm.android.deviceaddmodule.contract.DispatchContract;
import com.mm.android.deviceaddmodule.event.DeviceAddEvent;
import com.mm.android.deviceaddmodule.helper.DeviceAddHelper;
import com.mm.android.deviceaddmodule.mobilecommon.AppConsume.ProviderManager;
import com.mm.android.deviceaddmodule.mobilecommon.annotation.DeviceAbility;
import com.mm.android.deviceaddmodule.mobilecommon.base.LCBusinessHandler;
import com.mm.android.deviceaddmodule.mobilecommon.businesstip.HandleMessageCode;
import com.mm.android.deviceaddmodule.mobilecommon.common.LCConfiguration;
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 org.greenrobot.eventbus.EventBus;
 
import java.lang.ref.WeakReference;
 
import static com.mm.android.deviceaddmodule.helper.Utils4AddDevice.isDeviceTypeBox;
 
public class DispatchPresenter implements DispatchContract.Presenter {
    WeakReference<DispatchContract.View> mView;
 
    public DispatchPresenter(DispatchContract.View view) {
        mView = new WeakReference<>(view);
    }
 
    private void getDevIntroductionInfoSync(String deviceModel, final boolean isOnlineAction) {
        LCBusinessHandler getDevIntroductionHandler = new LCBusinessHandler() {
            @Override
            public void handleBusiness(Message msg) {
                if (mView.get() == null
                        || (mView.get() != null && !mView.get().isViewActive())) {
                    return;
                }
                dispatchIntroductionResult(isOnlineAction);
            }
        };
        DeviceAddModel.newInstance().getDevIntroductionInfo(deviceModel, getDevIntroductionHandler);
    }
 
    private void checkDevIntroductionInfo(final String deviceModelName, final boolean isOnlineAction) {
 
        mView.get().showProgressDialog();
        LCBusinessHandler checkDevIntroductionHandler = new LCBusinessHandler() {
            @Override
            public void handleBusiness(Message msg) {
                if (mView.get() == null
                        || (mView.get() != null && !mView.get().isViewActive())) {
                    return;
                }
                DeviceIntroductionInfo deviceIntroductionInfo = null;
                if (msg.what == HandleMessageCode.HMC_SUCCESS) {
                    deviceIntroductionInfo = (DeviceIntroductionInfo) msg.obj;
                }
                if (deviceIntroductionInfo == null) {        //表示需要更新
                    getDevIntroductionInfoSync(deviceModelName, isOnlineAction);
                } else {
                    dispatchIntroductionResult(isOnlineAction);
                }
            }
        };
        DeviceAddModel.newInstance().checkDevIntroductionInfo(deviceModelName, checkDevIntroductionHandler);
    }
 
    private void dispatchIntroductionResult(boolean isOnlineAction) {
        mView.get().cancelProgressDialog();
        if(isOnlineAction){
            gotoPage();
        }else {
            EventBus.getDefault().post(new DeviceAddEvent(DeviceAddEvent.CONFIG_PAGE_NAVIGATION_ACTION));
        }
    }
 
    //扫描出的二维码是否有效
    @Override
    public boolean isSnInValid(String sn) {
        if (ProviderManager.getAppProvider().getAppType() == LCConfiguration.APP_LECHANGE_OVERSEA) {
            return (sn.length() == 0
                    || sn.getBytes().length > 64);
        } else {
            return TextUtils.isEmpty(sn);
        }
    }
 
    @Override
    public boolean isScCodeInValid(String scCode) {
        return false;
    }
 
 
    @Override
    public boolean isManualInputPage() {
        return false;
    }
 
    protected void updateDeviceAddInfo(final String deviceSn, final String model, String regCode, String nc, String sc) {
        DeviceAddInfo deviceAddInfo = DeviceAddModel.newInstance().getDeviceInfoCache();
        deviceAddInfo.setDeviceSn(deviceSn);
        deviceAddInfo.setDeviceCodeModel(model);
        deviceAddInfo.setDeviceModel(model);
        deviceAddInfo.setRegCode(regCode);
        deviceAddInfo.setSc(sc);
        deviceAddInfo.setNc(nc);  // 将16进制的字符串转换为数字
        // 支持SC码的设备,使用SC码作为设备密码
        if(DeviceAddHelper.isSupportAddBySc(deviceAddInfo)) {
            deviceAddInfo.setDevicePwd(sc);
        }
    }
 
    /**
     * 处理服务返回的设备信息
     */
    public void dispatchResult() {
        DeviceAddInfo deviceAddInfo = DeviceAddModel.newInstance().getDeviceInfoCache();
        if (!deviceAddInfo.isSupport()) {
            mView.get().goNotSupportBindTipPage();
        }else if (DeviceAddInfo.BindStatus.bindByMe.name().equals(deviceAddInfo.getBindStatus())) {                     //设备被当前帐户绑定
            mView.get().showToastInfo(R.string.add_device_device_bind_by_yourself);
        } else if (DeviceAddInfo.BindStatus.bindByOther.name().equals(deviceAddInfo.getBindStatus())) {           //设备被其他帐户绑定
            mView.get().goOtherUserBindTipPage();
        } else if (DeviceAddInfo.DeviceType.ap.name().equals(deviceAddInfo.getType())) {        //配件
            checkDevIntroductionInfo(deviceAddInfo.getDeviceModel(),false);
        } else {    // 设备
           if (isManualInputPage()  // 若二维码中无sc码处理成与ios一致
                    && deviceAddInfo.hasAbility(DeviceAbility.SCCode) && (deviceAddInfo.getSc() == null || deviceAddInfo.getSc().length() != 8)) {   // 已上平台有sc码能力但sc码输入错误
                mView.get().showToastInfo(R.string.add_device_input_corrent_sc_tip);
            } else if (!deviceAddInfo.isDeviceInServer()) {                                                            //设备未在平台上注册
                //走设备离线添加流程
                deviceOfflineAction();
            } else if (DeviceAddInfo.Status.offline.name().equals(deviceAddInfo.getStatus())) {                        //设备离线
                deviceOfflineAction();
            } else if (DeviceAddInfo.Status.online.name().equals(deviceAddInfo.getStatus())
                    || DeviceAddInfo.Status.sleep.name().equals(deviceAddInfo.getStatus())
                    || DeviceAddInfo.Status.upgrading.name().equals(deviceAddInfo.getStatus())) {                         //设备在线/休眠/升级中
                deviceOnlineAction();
            }
        }
 
        if(isManualInputPage()) {
            deviceAddInfo.setStartTime(System.currentTimeMillis());
        }
    }
 
    /**
     * <p>
     * 获取设备信息失败,或者设备离线状态下,需要对结果进行处理
     * </p>
     */
    private void deviceOfflineAction() {
        DeviceAddInfo deviceAddInfo = DeviceAddModel.newInstance().getDeviceInfoCache();
        if (isDeviceTypeBox(deviceAddInfo.getDeviceCodeModel())) {// 如果是乐盒设备,直接提示设备不在线
            mView.get().showToastInfo(R.string.add_device_box_is_offline);
        } else {
            if ((!TextUtils.isEmpty(deviceAddInfo.getDeviceCodeModel())
                    || !TextUtils.isEmpty(deviceAddInfo.getDeviceModel()))) { //扫码信息中存在设备类型
                String deviceModel = deviceAddInfo.getDeviceModel();
                if (TextUtils.isEmpty(deviceModel)) {
                    deviceModel = deviceAddInfo.getDeviceCodeModel();
                }
                checkDevIntroductionInfo(deviceModel,false);
            } else {
                mView.get().goTypeChoosePage();                 //设备选择
            }
        }
    }
 
    /**
     * <p>
     * 获取到设备信息,并且设备在线,对结果进行处理
     * </p>
     */
    private void deviceOnlineAction() {
        DeviceAddInfo deviceAddInfo = DeviceAddModel.newInstance().getDeviceInfoCache();
        if (isDeviceTypeBox(deviceAddInfo.getDeviceCodeModel())) {
            // 盒子,不支持
            mView.get().showToastInfo(R.string.add_device_not_support_to_bind);
            return;
        } else {// 其他设备
            if (!TextUtils.isEmpty(deviceAddInfo.getDeviceCodeModel())
                    || !TextUtils.isEmpty(deviceAddInfo.getDeviceModel())) { //扫码信息中存在设备类型
                String deviceModel = deviceAddInfo.getDeviceModel();
                if (TextUtils.isEmpty(deviceModel)) {
                    deviceModel = deviceAddInfo.getDeviceCodeModel();
                }
                checkDevIntroductionInfo(deviceModel,true);
            } else {
                gotoPage();
            }
        }
    }
 
    private void gotoPage() {
        DeviceAddInfo deviceAddInfo = DeviceAddModel.newInstance().getDeviceInfoCache();
        deviceAddInfo.setCurDeviceAddType(DeviceAddInfo.DeviceAddType.ONLINE);
 
        if (DeviceAddHelper.isSupportAddBySc(deviceAddInfo)) {
            mView.get().goCloudConnectPage();
        } else {
            if (ProviderManager.getAppProvider().getAppType() == LCConfiguration.APP_LECHANGE_OVERSEA) { // 海外
                mView.get().goDeviceLoginPage();
            } else {
                if (deviceAddInfo.hasAbility(DeviceAbility.Auth)) {
                    if (TextUtils.isEmpty(deviceAddInfo.getDevicePwd())) {
                        mView.get().goDeviceLoginPage();//输入设备密码
                    } else {
                        mView.get().goDeviceBindPage();//直接绑定
                    }
                } else if (deviceAddInfo.hasAbility(DeviceAbility.RegCode)) {
                    if (TextUtils.isEmpty(deviceAddInfo.getRegCode())) {
                        mView.get().goSecCodePage();//输入安全码
                    } else {
                        mView.get().goDeviceBindPage();//直接绑定
                    }
                }
            }
        }
    }
}