RxJava / Android Observable对状态更改没有反应

时间:2019-06-11 23:18:16

标签: android rx-java rx-android

当我在主题上调用onNext时,可观察对象没有发生任何变化,并且没有做出适当的反应。

因此,我们的产品使用canBus将数据从控制器发送到硬件,并且我们正在使用rxJava处理数据更改。我们已经使rxJava与UI完美配合,但是对于我们的Alexa新功能,由于某种原因,当我直接调用模型函数以更改值(然后又调用subject.onNext)时,什么也没发生。我似乎找不到错误。流程如下:

Alexa回复:

case "ShowerState":
            //TODO figure out shower state alexa case
            Log.d(TAG, "Intent: ShowerIntent");
            if(action.equals("on")){
                Log.d(TAG, "Action: On");
                digitalShowerModel.changeShowerStateValue(DigitalShowerModel.DigitalShowerState.ON);
            }else if(action.equals("off")){
                Log.d(TAG, "Action: Off");
                digitalShowerModel.changeShowerStateValue(DigitalShowerModel.DigitalShowerState.OFF);
            }
            break;

数字淋浴器型号:

public void changeShowerStateValue(DigitalShowerState digitalShowerState) {
    Log.d(TAG, "Change shower state value");
    currentShowerState = digitalShowerState;
    currentShowerStateSubject.onNext(currentShowerState);
}

观察者流:

private void initDigitalShowerCanMsgs() {
    Observable.combineLatest(
            digitalShowerRepository.getShowerStateChanges(),
            digitalShowerRepository.getWaterTempChanges(),
            digitalShowerRepository.getWaterOutputChanges(),
            digitalShowerRepository.getWaterFlowChanges(),
            Observable.interval(TIME_BETWEEN_DIGITAL_SHOWER_MSGS, TimeUnit.SECONDS),
            new Function5<DigitalShowerModel.DigitalShowerState,
                                    Integer, DigitalShowerModel.WaterOutput,
                                    DigitalShowerModel.WaterFlow, Long, ArrayList<Integer>>() {
                @Override
                public ArrayList<Integer> apply(DigitalShowerModel.DigitalShowerState digitalShowerState, Integer temp, DigitalShowerModel.WaterOutput waterOutput, DigitalShowerModel.WaterFlow waterFlow, Long aLong) throws Exception {
                    ArrayList<Integer> dataBytes = new ArrayList<>();
                    dataBytes.add(digitalShowerState.ordinal());
                    dataBytes.add(temp);
                    dataBytes.add(waterOutput.ordinal());
                    dataBytes.add(waterFlow.ordinal());
                    return dataBytes;
                }
            })
            .observeOn(Schedulers.io())
            .subscribe(new Observer<ArrayList<Integer>>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(ArrayList<Integer> dataBytes) {
                    Log.d(TAG, "SHOWER CAN MESSAGE OBSERVER");
                    FC_Cmd[FlexDefs.CTLESVSTATE].fflxout.execute(dataBytes.get(0), dataBytes.get(1),
                            dataBytes.get(2), dataBytes.get(3), 0, 0);
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                }

                @Override
                public void onComplete() {

                }
            });

数字淋浴存储库:

public Observable<DigitalShowerModel.DigitalShowerState> getShowerStateChanges() {
    Log.d(TAG, "Get shower state changes DigitalShowerRepository");
    return digitalShowerModel.getCurrentShowerStateSubject();
}

数字淋浴模型(观察者):

    public Subject<DigitalShowerState> getCurrentShowerStateSubject() {
    Log.d(TAG, "Get shower state value");
    return currentShowerStateSubject;
}

我不明白为什么未处理更改。正在记录日志消息“更改淋浴状态值”。此外,观察者正在按“ Shower can观察者日志”进行观察。我还认为可能是这个观察者还调用了getShowerStateChanges,从而以某种方式影响了它:

数字淋浴屏碎片:

 Observable.combineLatest(
        digitalShowerRepository.getShowerStateChanges(),
        digitalShowerRepository.getWaterOutputChanges(), new BiFunction<DigitalShowerModel.DigitalShowerState, DigitalShowerModel.WaterOutput, Pair>() {
            @Override
            public Pair<DigitalShowerModel.DigitalShowerState, DigitalShowerModel.WaterOutput>
                    apply(DigitalShowerModel.DigitalShowerState digitalShowerState, DigitalShowerModel.WaterOutput waterOutput) throws Exception {
                return new Pair<>(digitalShowerState, waterOutput);
            }
        })
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<Pair>() {
            @Override
            public void onSubscribe(Disposable d) {
                digitalShowerDisposables.add(d);
            }

            @Override
            public void onNext(Pair pair) {
                if(!wasInitiatedByUser) {
                    rainHeadRdoBtn.setOnTouchListener(null);
                    showerHeadRdoBtn.setOnTouchListener(null);
                    wandRdoBtn.setOnTouchListener(null);
                    showerOptions.setOnCheckedChangeListener(null);

                    if (pair.first == DigitalShowerModel.DigitalShowerState.ON) {
                        switch ((DigitalShowerModel.WaterOutput) pair.second) {
                            case RAINHEAD:
                                rainHeadRdoBtn.setChecked(true);
                                break;
                            case SHOWERHEAD:
                                showerHeadRdoBtn.setChecked(true);
                                break;
                            case WAND:
                                wandRdoBtn.setChecked(true);
                                break;
                        }
                    } else if (pair.first == DigitalShowerModel.DigitalShowerState.OFF) {
                        showerOptions.clearCheck();
                    }

                    rainHeadRdoBtn.setOnTouchListener(DigitalShowerScreenFragment.this);
                    showerHeadRdoBtn.setOnTouchListener(DigitalShowerScreenFragment.this);
                    wandRdoBtn.setOnTouchListener(DigitalShowerScreenFragment.this);
                    showerOptions.setOnCheckedChangeListener(DigitalShowerScreenFragment.this);
                }
                wasInitiatedByUser = false;
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onComplete() {

            }
    });

但这似乎主要是UI更改观察者。重要提示:我没有写观察者,但是我确实知道发生了什么,减去了这个问题。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

所以我忘了将此添加到alexa响应类中,并且在添加后它可以工作:

TouchSteamApplication.getApplicationComponent().inject(this);