package com.ifpdos.factorytest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import com.cvte.tv.api.gson.JsonParser;
import com.ifpdos.Utils.okhttp.OkHttpHelper;
import com.ifpdos.factorytest.util.SensorLayout;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import okhttp3.Response;

/* JADX INFO: loaded from: classes.dex */
public class MultifunSensorTestActivity extends Activity {
    private static final String TAG = "MultifunSensor";
    private Disposable mDisposable;
    private LinearLayout sensorLayoutContainer;
    private List<SensorLayout> sensorLayoutList;
    private boolean testResult;
    List<Sensor> sensorList = new ArrayList();
    private final JsonParser mJsonParser = new JsonParser();
    Random random = new Random();

    @Override // android.app.Activity
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.multi_fun_sensor_test);
        initSensorList();
        initUi();
        startPollingSensor();
    }

    private void initSensorList() {
        if (ConfigParser.hasCo2Sensor()) {
            this.sensorList.add(new Sensor(getString(R.string.carbon_dioxide), "/v1/device/sensor/co2"));
        }
        if (ConfigParser.hasPM25Sensor()) {
            this.sensorList.add(new Sensor("PM2.5 : ", "/v1/device/sensor/pm25"));
        }
        if (ConfigParser.hasFormaldehydeSensor()) {
            this.sensorList.add(new Sensor(getString(R.string.formaldehyde), "/v1/device/sensor/formaldehyde"));
        }
        if (ConfigParser.hasTemperatureSensor()) {
            this.sensorList.add(new Sensor(getString(R.string.temperature), "/v1/device/sensor/temperature"));
        }
        if (ConfigParser.hasHumiditySensor()) {
            this.sensorList.add(new Sensor(getString(R.string.humidity), "/v1/device/sensor/humidity"));
        }
        if (ConfigParser.hasVocSensor()) {
            this.sensorList.add(new Sensor(" VOC : ", "/v1/device/sensor/voc"));
        }
        if (ConfigParser.hasPm10Sensor()) {
            this.sensorList.add(new Sensor(getString(R.string.pm10), "/v1/device/sensor/pm10"));
        }
    }

    private void startPollingSensor() {
        Log.d(TAG, "startPollingSensor");
        this.mDisposable = Observable.interval(0L, 1L, TimeUnit.SECONDS).flatMap(new Function() { // from class: com.ifpdos.factorytest.MultifunSensorTestActivity$$ExternalSyntheticLambda0
            @Override // io.reactivex.functions.Function
            public final Object apply(Object obj) {
                return this.f$0.lambda$startPollingSensor$0((Long) obj);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer() { // from class: com.ifpdos.factorytest.MultifunSensorTestActivity$$ExternalSyntheticLambda1
            @Override // io.reactivex.functions.Consumer
            public final void accept(Object obj) throws Exception {
                this.f$0.lambda$startPollingSensor$1((List) obj);
            }
        }, new Consumer() { // from class: com.ifpdos.factorytest.MultifunSensorTestActivity$$ExternalSyntheticLambda2
            @Override // io.reactivex.functions.Consumer
            public final void accept(Object obj) {
                ((Throwable) obj).printStackTrace();
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ ObservableSource lambda$startPollingSensor$0(Long l) throws Exception {
        List<Sensor> sensorValue = getSensorValue();
        this.sensorList = sensorValue;
        return Observable.just(sensorValue);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$startPollingSensor$1(List list) throws Exception {
        updateSensorValue();
    }

    private void updateSensorValue() {
        Log.d(TAG, "updateSensorValue");
        for (SensorLayout sensorLayout : this.sensorLayoutList) {
            for (Sensor sensor : this.sensorList) {
                if (sensor.getName().equals(sensorLayout.getSensorName())) {
                    sensorLayout.updateSensorValue(sensor.getSensorValue());
                }
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public boolean getAllTestResult() {
        Log.d(TAG, "updateSensorValue");
        Iterator<SensorLayout> it = this.sensorLayoutList.iterator();
        while (it.hasNext()) {
            if (!it.next().getTestResult()) {
                return false;
            }
        }
        return true;
    }

    private List<Sensor> getSensorValue() {
        Log.d(TAG, "getSensorValue");
        for (Sensor sensor : this.sensorList) {
            try {
                Response responseExecuteSync = OkHttpHelper.getInstance().executeSync(sensor.getUrl());
                Log.d(TAG, "response code = " + responseExecuteSync.code());
                if (responseExecuteSync.code() == 200) {
                    String strString = responseExecuteSync.body().string();
                    Log.d(TAG, "response.body = " + strString);
                    float asFloat = this.mJsonParser.parse(strString).getAsJsonObject().get("value").getAsFloat();
                    Log.d(TAG, "sensorValue= " + asFloat);
                    sensor.setSensorValue(Float.toString(asFloat));
                }
            } catch (Exception e) {
                Log.e(TAG, "getSensorValue err", e);
            }
        }
        return this.sensorList;
    }

    private void initUi() {
        this.sensorLayoutContainer = (LinearLayout) findViewById(R.id.sensor_layout_container);
        this.sensorLayoutList = new ArrayList();
        for (Sensor sensor : this.sensorList) {
            SensorLayout sensorLayout = new SensorLayout(this, null);
            sensorLayout.setSensorName(sensor.getName());
            this.sensorLayoutList.add(sensorLayout);
            this.sensorLayoutContainer.addView(sensorLayout);
        }
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(0);
        linearLayout.setGravity(1);
        linearLayout.setPadding(650, 0, 650, 0);
        Button button = new Button(this);
        button.setText(getString(R.string.test_finish));
        button.setBackgroundResource(R.color.blue);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(5, -2, 1.0f);
        layoutParams.setMargins(16, 0, 16, 0);
        button.setLayoutParams(layoutParams);
        linearLayout.addView(button);
        this.sensorLayoutContainer.addView(linearLayout);
        button.setOnClickListener(new View.OnClickListener() { // from class: com.ifpdos.factorytest.MultifunSensorTestActivity.1
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                MultifunSensorTestActivity.this.getAllTestResult();
                MultifunSensorTestActivity multifunSensorTestActivity = MultifunSensorTestActivity.this;
                multifunSensorTestActivity.setResult(multifunSensorTestActivity.getAllTestResult() ? 1 : 2);
                MultifunSensorTestActivity.this.finish();
            }
        });
    }

    private class Sensor {
        private String name;
        private String sensorValue;
        private String url;

        public Sensor(String str, String str2) {
            this.name = str;
            this.url = str2;
        }

        public String getName() {
            return this.name;
        }

        public String getUrl() {
            return this.url;
        }

        public void setName(String str) {
            this.name = str;
        }

        public void setSensorValue(String str) {
            this.sensorValue = str;
        }

        public String getSensorValue() {
            return this.sensorValue;
        }
    }
}
