111
hxb
2022-11-24 0a3e07f10937484145f33c7560607b4b2353cb81
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package com.mm.android.deviceaddmodule.presenter;
 
import android.media.MediaPlayer;
import android.net.wifi.ScanResult;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
 
import com.company.NetSDK.DEVICE_NET_INFO_EX;
import com.dahua.mobile.utility.network.DHWifiUtil;
import com.lechange.common.configwifi.LCSmartConfig;
import com.lechange.opensdk.configwifi.LCOpenSDK_ConfigWifi;
import com.lechange.opensdk.media.DeviceInitInfo;
import com.mm.android.deviceaddmodule.LCDeviceEngine;
import com.mm.android.deviceaddmodule.R;
import com.mm.android.deviceaddmodule.SearchDeviceManager;
import com.mm.android.deviceaddmodule.contract.SmartConfigConstract;
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.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.utils.LogUtil;
import com.mm.android.deviceaddmodule.mobilecommon.utils.PreferencesHelper;
import com.mm.android.deviceaddmodule.model.DeviceAddModel;
 
import org.greenrobot.eventbus.EventBus;
 
import java.io.File;
import java.lang.ref.WeakReference;
 
public class SmartConfigPresenter implements SmartConfigConstract.Presenter, SearchDeviceManager.ISearchDeviceListener {
    private int TIMER_TIMEOUT = 120 * 1000;// 120秒 连接路由
    private static final int TIME_OUT = 60 * 1000;                            //接收超时时间
    private static final int SEARCH_SUCCESS = 1;                            //搜索成功
    private static final int SEARCH_FAILED = 2;                            //搜索不到设备
    private static final int NOT_INIT = 3;                            //不需要初始化
    private static final int NEED_INIT = 4;                            //需要初始化
 
    private static final String VOICE_CONFIG_FILE_NAME = "Audiopair.wav";            // 声波配对的音频文件
 
    WeakReference<SmartConfigConstract.View> mView;
    DHWifiUtil mDHWifiUtil;
    DeviceInitInfo mDeviceNetInfoEx;
    String mDeviceSnCode;
    boolean mIsSupportSoundWave;
    boolean mIsSupportSoundWaveV2;    // 是否支持声波新方案
    private boolean mIsRunningAudio = false;
    private MediaPlayer mMediaPlayer = null;
    private String tempDirectory;   //smartconfig、声波文件临时存放路径
 
    public SmartConfigPresenter(SmartConfigConstract.View view/*, boolean isQRCodeConfig*/) {
        mView = new WeakReference<>(view);
        mDHWifiUtil = new DHWifiUtil(mView.get().getContextInfo().getApplicationContext());
        mDeviceSnCode = DeviceAddModel.newInstance().getDeviceInfoCache().getDeviceSn();
 
        initInfo();
    }
 
    private void initInfo() {
        String configMode = DeviceAddModel.newInstance().getDeviceInfoCache().getConfigMode();
        mIsSupportSoundWave = configMode != null && configMode.contains(DeviceAddInfo.ConfigMode.SoundWave.name());
        mIsSupportSoundWaveV2 = DeviceAddHelper.isSupportSoundWaveV2(DeviceAddModel.newInstance().getDeviceInfoCache());
 
        mView.get().updateTip2Txt(mIsSupportSoundWave, mIsSupportSoundWaveV2);
 
        mView.get().hideTipWifiPwdErrorTxt(ProviderManager.getAppProvider().getAppType() == LCConfiguration.APP_LECHANGE_OVERSEA);
 
        String userId = LCDeviceEngine.newInstance().userId;
        tempDirectory = mView.get().getContextInfo().getFilesDir() + File.separator + userId + File.separator;
    }
 
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (mView.get() != null && mView.get().isViewActive()) {
                switch (msg.what) {
                    case SEARCH_SUCCESS:        //搜索设备成功
                        stopAudio();
                        recyle();
                        isDeviceNeedInit();
                        break;
                    case NOT_INIT:              //设备不需初始化
                        mView.get().goConnectCloudPage();
                        break;
                    case NEED_INIT:             //设备未初始化
                        mView.get().goDevInitPage(mDeviceNetInfoEx);
                        break;
                }
 
            }
        }
    };
 
    private void isDeviceNeedInit() {
        DeviceAddInfo deviceAddInfo = DeviceAddModel.newInstance().getDeviceInfoCache();
        if(DeviceAddHelper.isSupportAddBySc(deviceAddInfo) || !DeviceAddHelper.isDeviceNeedInit(mDeviceNetInfoEx)) {
            mHandler.obtainMessage(NOT_INIT).sendToTarget();
        } else {
            mHandler.obtainMessage(NEED_INIT).sendToTarget();
        }
    }
 
    @Override
    public void recyle() {
        //关闭
        LCOpenSDK_ConfigWifi.configWifiStop();
        mView.get().stopCountDown();
        DeviceAddModel.newInstance().setLoop(false);
    }
 
    @Override
    public String getConfigMode() {
        return DeviceAddModel.newInstance().getDeviceInfoCache().getConfigMode();
    }
 
    //开始smartconfig配对
    @Override
    public void startSmartConfig() {
        startConfig();
        initAudioPair();
        startSearchDevices();
    }
 
    /**
     * SmartConfig
     */
    private void startConfig() {
        ScanResult scanResult = mDHWifiUtil.getScanResult();
        String encryptionCap = "";
        if (scanResult != null) {
            encryptionCap = scanResult.capabilities == null ? "" : scanResult.capabilities;
        }
        DeviceAddInfo.WifiInfo wifiInfo = DeviceAddModel.newInstance().getDeviceInfoCache().getWifiInfo();
        String ssid = wifiInfo.getSsid() == null ? "" : wifiInfo.getSsid();
        String ssid_pwd = wifiInfo.getPwd() == null ? "" : wifiInfo.getPwd();
 
        /**
         * cfgType:配网方式,1--组播,2--广播,4--声波,可按位或运算组合使用;
         * voiceConfigFilePath:声波配对的音频文件的生成路径,由上层调用者传入;
         * enableBgMusic:声波配对是否使用背景声,默认是布谷鸟叫声;
         * freq:声波配对收发信号的基带频率;
         * fsk_tx_mode:fsk发送方式,0--新的fsk发送方式,1--老的fsk发送方式,2--新的和老的fsk波形发送方式;
         */
        int fskTxMode = 1;
        boolean enableBgMusic = true;
        if(mIsSupportSoundWaveV2) {
            fskTxMode = 0;
            enableBgMusic = false;
        }
 
        LCOpenSDK_ConfigWifi.configWifiStart(mDeviceSnCode, ssid, ssid_pwd, encryptionCap, LCSmartConfig.ConfigType.LCConfigWifi_Type_ALL,enableBgMusic,11000, fskTxMode);
 
 
    }
 
    /**
     * 声波配对
     */
    private void initAudioPair() {
        if(!mIsSupportSoundWave && !mIsSupportSoundWaveV2) {
            return;
        }
 
        String directory = ProviderManager.getDeviceAddCustomProvider().getDevAddCachePath();
        File fileDir = new File(directory);
        if (!fileDir.exists()) {
            if (!fileDir.mkdirs()) {
                return;
            }
        }
        File fileAudio = new File(fileDir, VOICE_CONFIG_FILE_NAME);
        if (fileAudio.exists() && !TextUtils.isEmpty(fileAudio.getAbsolutePath())) {
            initAudio(fileAudio.getAbsolutePath());
        }
    }
 
 
    private void initAudio(String path) {
        mMediaPlayer = new MediaPlayer();
 
        try {
            LogUtil.debugLog("AudioConfig   : ", "setDataSource MediaPlayer : " + path);
            mMediaPlayer.setDataSource(path);
            LogUtil.debugLog("AudioConfig   : ", "prepare MediaPlayer");
            mMediaPlayer.prepare();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 播放新声波方案的音频文件
     */
    @Override
    public void playAudio() {
        if(!mIsRunningAudio && mMediaPlayer != null) {
            try {
                mMediaPlayer.setLooping(true);
                LogUtil.debugLog("AudioConfig   : ", "start MediaPlayer");
                mMediaPlayer.start();
                mIsRunningAudio = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    @Override
    public void pauseAudio() {
        if(mMediaPlayer != null) {
            LogUtil.debugLog("AudioConfig   : ", "pause MediaPlayer");
            mMediaPlayer.pause();
            mIsRunningAudio = false;
        }
    }
 
    /**
     * 停止播放新声波方案的音频文件
     */
    @Override
    public void stopAudio() {
        if(mIsRunningAudio && mMediaPlayer != null) {
            try {
                mMediaPlayer.stop();
                LogUtil.debugLog("AudioConfig   : ", "prepare MediaPlayer");
                mMediaPlayer.prepare();
                mIsRunningAudio = false;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    @Override
    public void releaseAudio() {
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            LogUtil.debugLog("AudioConfig   : ", "release MediaPlayer");
            mMediaPlayer.release();
            mMediaPlayer = null;
            mIsRunningAudio = false;
        }
    }
 
    @Override
    public void wifiPwdErrorClick() {
        DeviceAddInfo.WifiInfo info =  DeviceAddModel.newInstance().getDeviceInfoCache().getWifiInfo();
        info.setPwd("");
        DHWifiUtil mDHWifiUtil=new DHWifiUtil(mView.get().getContextInfo().getApplicationContext());
        String curWifiName = mDHWifiUtil.getCurrentWifiInfo().getSSID().replaceAll("\"", "");
        String WIFI_SAVE_PREFIX = LCDeviceEngine.newInstance().userId + "_WIFI_ADD_";
        PreferencesHelper.getInstance(mView.get().getContextInfo()).set(WIFI_SAVE_PREFIX + curWifiName,"");
        mView.get().goWfiPwdPage();
    }
 
    /**
     * 搜索设备
     */
    private void startSearchDevices() {
        final SearchDeviceManager manager = SearchDeviceManager.getInstance();
        mDeviceNetInfoEx = manager.getDeviceNetInfo(mDeviceSnCode);
        if (mDeviceNetInfoEx != null) {
            // 已经搜到
            mHandler.obtainMessage(SEARCH_SUCCESS).sendToTarget();
        } else {
            manager.registerListener(this);
            manager.startSearch();
            getDeviceInfo();
        }
    }
 
    private void getDeviceInfo() {
        DeviceAddInfo deviceAddInfo = DeviceAddModel.newInstance().getDeviceInfoCache();
        // 第一次添加时使用二维码或用输入的sc码来判断,离线配网或者设备已在平台上线过则可以通过能力集进行判断
        if(DeviceAddHelper.isSupportAddBySc(deviceAddInfo)) {
            String deviceSn = DeviceAddModel.newInstance().getDeviceInfoCache().getDeviceSn();
            String model = DeviceAddModel.newInstance().getDeviceInfoCache().getModelName();
            LCBusinessHandler getDeviceHandler = new LCBusinessHandler() {
                @Override
                public void handleBusiness(Message msg) {
                    if (mView.get() == null
                            || (mView.get() != null && !mView.get().isViewActive())) {
                        return;
                    }
                    if (msg.what == HandleMessageCode.HMC_SUCCESS) {
                        dispatchResult();
                    } else {//发生异常且倒计时未完成,重新查询
                        getDeviceInfo();
                    }
 
                }
            };
            DeviceAddModel.newInstance().getDeviceInfoLoop(deviceSn, model, TIME_OUT, getDeviceHandler);
        }
    }
 
    private void dispatchResult() {
        stopAudio();
        recyle();
 
        DeviceAddInfo deviceAddInfo = DeviceAddModel.newInstance().getDeviceInfoCache();
        String status=deviceAddInfo.getStatus();
        if(TextUtils.isEmpty(status)){
            status=DeviceAddInfo.Status.offline.name();
        }
        if (DeviceAddInfo.Status.online.name().equals(status)) {
            if (isWifiOfflineConfiMode()) {//wifi离线配置
                EventBus.getDefault().post(new DeviceAddEvent(DeviceAddEvent.DESTROY_ACTION));
                ProviderManager.getDeviceAddCustomProvider().goHomePage(mView.get().getContextInfo());
                mView.get().showToastInfo(R.string.add_device_wifi_config_success);
            } else {
                mView.get().goBindDevicePage();
            }
        } else {
            if (isWifiOfflineConfiMode()) {//wifi离线配置
                mView.get().showToastInfo(R.string.add_device_config_failed);
                mView.get().completeAction();
            } else {
                mView.get().goConfigTimeoutPage();
            }
        }
    }
 
    public boolean isWifiOfflineConfiMode() {
        return DeviceAddModel.newInstance().getDeviceInfoCache().isWifiOfflineMode();
    }
 
    @Override
    public void onDeviceSearched(String sncode, DeviceInitInfo info) {
        if (info != null && mDeviceSnCode.equalsIgnoreCase(sncode)) {
            mDeviceNetInfoEx = info;
            mHandler.obtainMessage(SEARCH_SUCCESS).sendToTarget();
            SearchDeviceManager.getInstance().unRegisterListener(this);
        }
    }
}