首页 > 代码库 > RxJava中的Observable,在smartConfig中的应用

RxJava中的Observable,在smartConfig中的应用

 

Observable: 事件源
subscriber: 观察者
 
public void smartConfig(final String password){

        Observable esptouch = Observable.create(new Observable.OnSubscribe<String>() {

            @Override
            public void call(Subscriber<? super String> subscriber) {

                EspWifiAdminSimple mWifiAdmin = new EspWifiAdminSimple(getActivity());
                String apSsid = mWifiAdmin.getWifiConnectedSsid();
                String apPassword = password;
                String apBssid = mWifiAdmin.getWifiConnectedBssid();

                EsptouchTask esptouchTask = new EsptouchTask(apSsid, apBssid, apPassword, false, getActivity());
                List<IEsptouchResult> resultList = esptouchTask.executeForResults(1);
                if (resultList.size() > 0 && resultList.get(0).isSuc()){
                    subscriber.onNext(getResources().getString(R.string.smart_config_register));
                    String mac = resultList.get(0).getBssid();
                    Log.e("felix", "mac:" + resultList.get(0).getBssid());
                    try {
                        List<Device> devices = null;
                        for (int i = 0; i < 10; i++){
                            Thread.sleep(1000);
                            Response<List<Device>> r  = RestApi.getRegisterDevices(mac).execute();
                            devices = r.body();    //  mac  18fe34fdace7
                            //if (devices != null){
                            if(devices.size()>0){
                                break;
                            }//18fe34d88345 mac adress 18fe34fda484
                        }
                        if (devices == null){
                            subscriber.onError(OnErrorThrowable.addValueAsLastCause(new Throwable(), getResources().getString(R.string.smart_no_device_found)));
                            return;
                        }
                        subscriber.onNext(getResources().getString(R.string.smart_config_register));
                        if(devices.size()>0){
                            for (Device device: devices){
                                //if(device.deviceMac.equals(mac)){
                                    mDevice = device;
                                    subscriber.onCompleted();
                                    return;
                                //}
                            }
                        }
                        subscriber.onError(OnErrorThrowable.addValueAsLastCause(new Throwable(), getResources().getString(R.string.smart_no_device_found)));
                    }catch (Exception e){
                        subscriber.onError(OnErrorThrowable.addValueAsLastCause(new Throwable(), getResources().getString(R.string.smart_network_error)));
                    }
                }else{
                    subscriber.onError(OnErrorThrowable.addValueAsLastCause(new Throwable(), getResources().getString(R.string.smart_config_fail)));
                }
                subscriber.onCompleted();
           }
        });

        esptouch.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {
//                mProgress.setVisibility(View.GONE);
//                mCancel.setText(R.string.close);
//                mCancel.setEnabled(true);
//                Response<Device> d = RestApi.registerDevices(device.id, device).execute();
//                subscriber.onNext(getResources().getString(R.string.smart_config_success));
//                mDevice = d.body();
                if (mDevice != null && mListener != null){
                    dismiss();
                    mListener.onDeviceDiscovered(mDevice);
                }
            }

            @Override
            public void one rror(Throwable e) {
                String message = (String)((OnErrorThrowable.OnNextValue)e.getCause()).getValue();
                mProgress.setVisibility(View.GONE);
                mCancel.setText(R.string.close);
                mCancel.setEnabled(true);
                mMessage.setText(message);
            }

            @Override
            public void onNext(String msg) {
                mMessage.setText(msg);

            }

        });


    }

 

RxJava中的Observable,在smartConfig中的应用