package com.mm.android.deviceaddmodule.presenter; import android.os.Looper; import android.os.Message; import android.text.TextUtils; import com.dahua.mobile.utility.music.DHMusicPlayer; import com.mm.android.deviceaddmodule.R; import com.mm.android.deviceaddmodule.contract.ScanContract; import com.mm.android.deviceaddmodule.event.DeviceAddEvent; import com.mm.android.deviceaddmodule.helper.DeviceAddHelper; import com.mm.android.deviceaddmodule.mobilecommon.AppConsume.BusinessException; import com.mm.android.deviceaddmodule.mobilecommon.AppConsume.ProviderManager; import com.mm.android.deviceaddmodule.mobilecommon.AppConsume.ScanResult; import com.mm.android.deviceaddmodule.mobilecommon.AppConsume.ScanResultFactory; import com.mm.android.deviceaddmodule.mobilecommon.annotation.DeviceAbility; 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.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.mobilecommon.utils.LogUtil; 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 ScanPresenter implements ScanContract.Presenter { WeakReference<ScanContract.View> mView; DHMusicPlayer mDHMusicPlayer; public ScanPresenter(ScanContract.View view) { mView = new WeakReference<>(view); mDHMusicPlayer = new DHMusicPlayer(mView.get().getContextInfo(), false, true, R.raw.beep); mDHMusicPlayer.setSupportVibrate(true); } @Override public ScanResult parseScanStr(String scanStr, String sc) { if (!isManualInputPage()) { mDHMusicPlayer.playRing(false); } ScanResult scanResult = ScanResultFactory.scanResult(scanStr.trim()); LogUtil.debugLog("ScanPresenter", "scanResult : " + scanResult); // 手动输入的安全验è¯ç if (!TextUtils.isEmpty(sc)) { scanResult.setSc(sc); } if (!TextUtils.isEmpty(scanResult.getSn())) { updateDeviceAddInfo(scanResult.getSn().trim(), scanResult.getMode(), scanResult.getRegcode(), scanResult.getNc(), scanResult.getSc()); // EventBus.getDefault().post(new DeviceAddEvent(DeviceAddEvent.GET_DEVICE_SN)); } return scanResult; } /** * æ ¹æ®æ‰«ç 的数æ®å޻平尿Ÿ¥è¯¢è®¾å¤‡ä¿¡æ¯ * * @param deviceSn * @param deviceCodeModel */ @Override public void getDeviceInfo(final String deviceSn, final String deviceCodeModel) { if (isSnInValid(deviceSn)) { mView.get().showToastInfo(R.string.add_device_scan_lechange_device_qr_code); } else { mView.get().showProgressDialog(); LCBusinessHandler getDeviceHandler = 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) { } else { String errorDesp = ((BusinessException) msg.obj).errorDescription; if (errorDesp.contains("DV1037")) { mView.get().showToastInfo(R.string.add_device_device_sn_or_imei_not_match); return; } if (errorDesp.contains("DV1003")){ // mView.get().showToastInfo(R.string.device_has_binding); addDeviceToPolicy(deviceSn); return; } mView.get().showToastInfo(BusinessErrorTip.getErrorTip(msg)); return; } dispatchResult(); } }; DeviceAddModel.newInstance().getDeviceInfo(deviceSn, deviceCodeModel, "", getDeviceHandler); } } 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); } 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 void recyle() { if (mDHMusicPlayer != null) { mDHMusicPlayer.release(); } } @Override public void resetCache() { DeviceAddModel.newInstance().getDeviceInfoCache().clearCache(); } @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); } } /** * å¤„ç†æœåŠ¡è¿”å›žçš„è®¾å¤‡ä¿¡æ¯ */ private 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();//直接绑定 } } } } } }