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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package com.mm.android.deviceaddmodule.presenter;
 
import android.os.Handler;
import android.os.Message;
import com.dahua.mobile.utility.music.DHMusicPlayer;
import com.lechange.opensdk.device.LCOpenSDK_DeviceInit;
import com.lechange.opensdk.media.DeviceInitInfo;
import com.mm.android.deviceaddmodule.R;
import com.mm.android.deviceaddmodule.SearchDeviceManager;
import com.mm.android.deviceaddmodule.contract.InitContract;
import com.mm.android.deviceaddmodule.mobilecommon.AppConsume.ProviderManager;
import com.mm.android.deviceaddmodule.mobilecommon.common.LCConfiguration;
import com.mm.android.deviceaddmodule.mobilecommon.entity.deviceadd.DeviceAddInfo;
import com.mm.android.deviceaddmodule.mobilecommon.utils.LogUtil;
import com.mm.android.deviceaddmodule.mobilecommon.utils.PasswordCheckRules;
import com.mm.android.deviceaddmodule.model.DeviceAddModel;
import java.lang.ref.WeakReference;
import static com.mm.android.deviceaddmodule.helper.Utils4AddDevice.checkDeviceVersion;
import static com.mm.android.deviceaddmodule.helper.Utils4AddDevice.checkEffectiveIP;
 
public class InitPresenter implements InitContract.Presenter, SearchDeviceManager.ISearchDeviceListener {
    private static final String TAG = "InitPresenter";
    private int SEARCH_DEVICE_WAIT_TIME = 5 * 1000;                          // 搜索设备时间
    private int NEW_V_SEARCH_DEVICE_WAIT_TIME = 10 * 1000;                          // 搜索设备时间
    private static final int SEARCH_SUCCESS = 1;                            //搜索成功
    private static final int SEARCH_FAILED = 2;                            //搜索不到设备
 
    WeakReference<InitContract.View> mView;
    DHMusicPlayer mDHMusicPlayer;
    DeviceInitInfo mDeviceNetInfoEx;
    String mDeviceSn;
    boolean isBeginInit;                                        //是否开始初始化,即是否点击初始化按钮
    private boolean isDeviceSearching = false;              //是否在开始搜索设备
    boolean mIsDeviceNewVersion;                              //新程序设备,直接走单播
    boolean mIsHasInitbyMulicast = false;                    //是否已经尝试过组播
    boolean isSoftApAddType = false;                          //当前软AP添加设备(门铃、铃铛等)版本,不管新老设备,IP会一直无效,故软AP添加初始化走组播+单播流程,待后续设备程序更改后再进行优化。
    boolean mIsHasInitbyIp = false;                         //是否已经尝试过单播
 
    public InitPresenter(InitContract.View view) {
        mView = new WeakReference<>(view);
        mDHMusicPlayer = new DHMusicPlayer(mView.get().getContextInfo(), true, mView.get().getMusicRes());
        mDeviceSn = DeviceAddModel.newInstance().getDeviceInfoCache().getDeviceSn();
        if (DeviceAddInfo.DeviceAddType.SOFTAP.equals(DeviceAddModel.newInstance().getDeviceInfoCache().getCurDeviceAddType())) {
            isSoftApAddType = true;
        }
    }
 
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (mView.get() != null &&
                    mView.get().isViewActive()) {
                switch (msg.what) {
                    case SEARCH_SUCCESS:
                        searchDevicesSuccess();
                        break;
                    case SEARCH_FAILED:
                        stopSearchDevice();
                        if (isBeginInit) {
                            errorAction();
                        }
                        break;
 
                    default:
                        break;
                }
            }
        }
    };
 
    private void searchDevicesSuccess() {
        stopSearchDevice();
        if (mDeviceNetInfoEx == null) {
            return;
        }
        if (isOnlyNeedUnicast() || mIsHasInitbyMulicast) {
            if (isBeginInit) {
                LogUtil.debugLog(TAG, "searched device: initDevByIp");
                initDevAccountByIp(mDeviceNetInfoEx, mView.get().getInitPwd());
            }
        } else {
            LogUtil.debugLog(TAG, "searched device: initDev");
            startDevInit();
        }
    }
 
    private Runnable mTimeOutRunnable = new Runnable() {
 
        @Override
        public void run() {
            mHandler.sendEmptyMessage(SEARCH_FAILED);
        }
    };
 
    @Override
    public void checkDevice() {
        if (isOnlyNeedUnicast()) {//新设备,需检测ip是否有效
            //检测设备Ip是否有效,无效需要重新搜索设备
            boolean isValidIp = checkEffectiveIP(mDeviceNetInfoEx);
            if (!isValidIp) {
                startSearchDevice();
            }
        }
 
    }
 
    private void startSearchDevice() {
        isDeviceSearching = true;
        SearchDeviceManager searchDeviceManager = SearchDeviceManager.getInstance();
        searchDeviceManager.registerListener(this);
        searchDeviceManager.startSearch();
        mHandler.postDelayed(mTimeOutRunnable, isOnlyNeedUnicast() ? NEW_V_SEARCH_DEVICE_WAIT_TIME : SEARCH_DEVICE_WAIT_TIME);
    }
 
    private void stopSearchDevice() {
        isDeviceSearching = false;
        SearchDeviceManager.getInstance().unRegisterListener(this);
        mHandler.removeCallbacks(mTimeOutRunnable);
    }
 
    private void errorAction() {
        mView.get().cancelProgressDialog();
        mView.get().goErrorTipPage();
    }
 
    private void initDevAccountByIp(DeviceInitInfo devicNetInfo, final String password) {
        mView.get().showProgressDialog();
        LCOpenSDK_DeviceInit.getInstance().initDeviceByIpEx(devicNetInfo, password, new LCOpenSDK_DeviceInit.IInitDeviceListener() {
            @Override
            public void onDeviceInit(boolean isInit) {
                if (isInit) {
                    dispatchInitResult();
                } else {
                    // 初始化失败页面
                    errorAction();
                }
            }
        });
    }
 
    @Override
    public void startDevInit() {
        mView.get().showProgressDialog();
 
        LCOpenSDK_DeviceInit.getInstance().initDeviceEx(mDeviceNetInfoEx, mView.get().getInitPwd(), new LCOpenSDK_DeviceInit.IInitDeviceListener() {
            @Override
            public void onDeviceInit(boolean isInit) {
                if (isInit) {
                    dispatchInitResult();
                } else {
                    if(mIsHasInitbyIp) {
                        // 初始化失败页面
                        errorAction();
                    } else {
                        //组播失败,直接走单播
                        DeviceInitInfo deviceNetInfoEx = SearchDeviceManager.getInstance().getDeviceNetInfo(mDeviceSn);
                        if (deviceNetInfoEx != null) {
                            mDeviceNetInfoEx = deviceNetInfoEx;
                        }
                        LogUtil.debugLog(TAG, "initDev failed,then try initDevByIp");
                        initDevAccountByIp(mDeviceNetInfoEx, mView.get().getInitPwd());
                    }
                }
            }
        });
    }
 
    public void dispatchInitResult() {
        mView.get().cancelProgressDialog();
        isBeginInit = false;
        DeviceAddInfo.DeviceAddType deviceAddType = DeviceAddModel.newInstance().getDeviceInfoCache().getCurDeviceAddType();
        DeviceAddModel.newInstance().getDeviceInfoCache().setDevicePwd(mView.get().getInitPwd());
 
        if (DeviceAddInfo.DeviceAddType.SOFTAP.equals(deviceAddType)) {
            mView.get().goSoftAPWifiListPage();
        } else {
            // 初始化成功 开始接入乐橙云
            mView.get().goConnectCloudPage();
        }
    }
 
 
    @Override
    public void setDeviceEX(DeviceInitInfo deviceEX) {
        mDeviceNetInfoEx = deviceEX;
        mIsDeviceNewVersion = checkDeviceVersion(mDeviceNetInfoEx);
    }
 
    @Override
    public void playTipSound() {
        if (ProviderManager.getAppProvider().getAppType() != LCConfiguration.APP_LECHANGE_OVERSEA)//海外版本不提示语音
            mDHMusicPlayer.playRing(false);
    }
 
    @Override
    public void startDevInitByIp() {
        isBeginInit = true;
        LogUtil.debugLog(TAG, "click init button...");
        if (isOnlyNeedUnicast()) {
            boolean isEffectiveIp = checkEffectiveIP(mDeviceNetInfoEx);
            if (isDeviceSearching) {//正在搜索
                LogUtil.debugLog(TAG, "please be wait, searching device ip now...");
                mView.get().showProgressDialog();
            } else if(isEffectiveIp) {//设备ip有效,不需重新搜索
                LogUtil.debugLog(TAG, "ip is effective: initDevByIp: " +  new String(mDeviceNetInfoEx.mIp).trim());
                initDevAccountByIp(mDeviceNetInfoEx, mView.get().getInitPwd());
            } else {
                LogUtil.debugLog(TAG, "ip is not effective: initDev");
                startDevInit();
            }
        } else {
            LogUtil.debugLog(TAG, "old device or DHCP is closed: initDev");
            startDevInit();
        }
    }
 
    @Override
    public boolean isPwdValid() {
        int res = PasswordCheckRules.checkPasswordValidation(mView.get().getInitPwd(), mView.get().getInitPwd(), mView.get().getContextInfo());
        if (res == PasswordCheckRules.PASSWORD_NOT_MATCH) {
            // mView.get().showToastInfo(R.string.add_device_pwd_rule_tip1);
            return false;
        } else if (res == PasswordCheckRules.PASSWORD_INVALID
                || res == PasswordCheckRules.PASSWORD_NOT_SAFY
                || res == PasswordCheckRules.PASSWORD_INVALID_LENGTH
                || res == PasswordCheckRules.PASSWORD_INVALID_COMBINATION) {
            mView.get().showToastInfo(R.string.mobile_common_password_too_simple);
            return false;
        }
 
        return true;
    }
 
 
    @Override
    public void recyle() {
        mDHMusicPlayer.release();
        stopSearchDevice();
    }
 
    @Override
    public void onDeviceSearched(String sncode, DeviceInitInfo device_net_info_ex) {
        if (device_net_info_ex == null) {
            return;
        }
        String szSerialNo = device_net_info_ex.mSerialNo;
        if (isOnlyNeedUnicast()) {
            if (szSerialNo.equalsIgnoreCase(mDeviceSn)) {
                boolean isEffectiveIp = checkEffectiveIP(device_net_info_ex);
                mDeviceNetInfoEx = device_net_info_ex;
                if (isEffectiveIp) {
                    mHandler.obtainMessage(SEARCH_SUCCESS).sendToTarget();
                }
            }
        } else {
            if (device_net_info_ex != null && szSerialNo.equalsIgnoreCase(mDeviceSn) && device_net_info_ex.mIPVersion == 4) {
                mDeviceNetInfoEx = device_net_info_ex;
                mHandler.obtainMessage(SEARCH_SUCCESS).sendToTarget();
            }
        }
    }
 
    //是否只需要进行单播
    private boolean isOnlyNeedUnicast() {
        return mIsDeviceNewVersion && !isSoftApAddType;
    }
}