package com.example.iot_controlhost.ui.fragment;


import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.example.iot_controlhost.base.BasePresenter;
import com.example.iot_controlhost.utils.global.RoomController;
import com.example.iot_controlhost.utils.global.RoomSensor;
import com.example.iot_controlhost.utils.global.RxTag;
import com.xuexiang.rxutil2.rxbus.RxBusUtils;
import com.xuexiang.rxutil2.rxjava.DisposablePool;
import com.xuexiang.rxutil2.rxjava.RxJavaUtils;
import com.xuexiang.rxutil2.rxjava.task.RxIOTask;
import com.xuexiang.rxutil2.rxjava.task.RxUITask;

public class ManualFragmentPresenter extends BasePresenter {
    public MutableLiveData<Void> refreshRelay = new MutableLiveData<>();
    public MutableLiveData<String> floorT = new MutableLiveData<>();

    public MutableLiveData<Void> getRefreshRelay() {
        return refreshRelay;
    }

    public ManualFragmentPresenter(@NonNull Application application) {
        super(application);
        //更新界面UI
        RxBusUtils.get().onMainThread(RxTag.UPDATE_MANUAL, Integer.class, controlCommand -> refreshRelay.setValue(null));
        //更新数据
        if (!RoomSensor.dynamicFloorTSensorList.isEmpty() || RoomController.relay.getModel().equals(RoomController.relay.getModels()[2])) {
            pollingTask.startPollingTaskOnIOThread(RxTag.MANUAL_UI, 5, () -> {
                if (!RoomSensor.dynamicFloorTSensorList.isEmpty()) {
                    floorT.postValue(RoomSensor.getFloorTemperature() + "℃");
                }
                if (RoomController.relay.getModel().equals(RoomController.relay.getModels()[2])) {
                    refreshRelay.postValue(null);
                }
            });
        }
    }
}
