package com.ifpdos.factorytest;

import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.Log;
import com.ifpdos.Utils.JsonUtils;
import com.ifpdos.Utils.UdiUtil;
import com.ifpdos.Utils.okhttp.OkHttpHelper;
import com.ifpdos.factorytest.entity.EmptyBody;
import com.ifpdos.factorytest.entity.EnableBody;
import com.ifpdos.factorytest.entity.ListIntValueBody;
import com.ifpdos.factorytest.entity.Rs232NotifyBody;
import com.ifpdos.udi.sdk.IEventHandler;
import com.ifpdos.udi.sdk.UdiSdk;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes.dex */
public class Rs232Test extends TestItem {
    private static final String TAG = "Rs232Test";
    private CountDownLatch countDownLatch;
    private IEventHandler eventHandler;
    private boolean lastRs232Enable;
    private Handler mHandler;
    private Handler mWriteHandler;
    private HandlerThread mWriteThread;
    private List<Integer> rs232Testdata;
    private boolean testSuccess;

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$new$0(String str, String str2) {
        Log.d(TAG, "event: " + str + ",data:" + str2);
        if (this.countDownLatch != null && UdiUtil.URL_RS232_DATA.equals(str)) {
            Rs232NotifyBody rs232NotifyBody = (Rs232NotifyBody) JsonUtils.fromJson(str2, Rs232NotifyBody.class);
            if (rs232NotifyBody == null) {
                this.countDownLatch.countDown();
                return;
            }
            Log.d(TAG, "rs232NotifyBody: " + rs232NotifyBody);
            if (rs232NotifyBody.mBufferData.size() == 1 && rs232NotifyBody.mBufferData.get(0).intValue() == 66) {
                this.testSuccess = true;
                this.countDownLatch.countDown();
            }
        }
    }

    public Rs232Test(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.rs232Testdata = Collections.singletonList(66);
        this.testSuccess = false;
        this.mWriteThread = new HandlerThread("rs232");
        this.eventHandler = new IEventHandler() { // from class: com.ifpdos.factorytest.Rs232Test$$ExternalSyntheticLambda1
            @Override // com.ifpdos.udi.sdk.IEventHandler
            public final void onEvent(String str, String str2) {
                this.f$0.lambda$new$0(str, str2);
            }
        };
        setStatus(this.mContext.getString(R.string.insert_rs232_test_fixture));
        this.mHandler = new Handler(Looper.getMainLooper());
        this.lastRs232Enable = getRs232SwitchEnable();
        UdiSdk.registerEvents((List<String>) Collections.singletonList(UdiUtil.URL_RS232_DATA), this.eventHandler);
    }

    @Override // com.ifpdos.factorytest.TestItem
    boolean doTest() {
        if (this.countDownLatch != null) {
            Log.d(TAG, "testing,ignore doTest");
            return true;
        }
        setStatus(this.mContext.getString(R.string.testing));
        if (this.mWriteHandler == null) {
            this.mWriteThread.start();
            this.mWriteHandler = new Handler(this.mWriteThread.getLooper());
        }
        this.mWriteHandler.post(new Runnable() { // from class: com.ifpdos.factorytest.Rs232Test$$ExternalSyntheticLambda0
            @Override // java.lang.Runnable
            public final void run() {
                this.f$0.testRs232();
            }
        });
        return true;
    }

    private boolean getRs232SwitchEnable() {
        try {
            return ((EnableBody) OkHttpHelper.getInstance().get(UdiUtil.URL_RS232_ENABLE, EnableBody.class)).enable;
        } catch (Exception e) {
            Log.e(TAG, "getRs232SwitchEnable: error", e);
            return false;
        }
    }

    private boolean setRs232SwitchEnable(boolean z) {
        return ((EmptyBody) OkHttpHelper.getInstance().postSync(UdiUtil.URL_RS232_ENABLE, JsonUtils.toJson(new EnableBody(z)), EmptyBody.class)) != null;
    }

    private boolean sendRs232Data(List<Integer> list) {
        return ((EnableBody) OkHttpHelper.getInstance().postSync(UdiUtil.URL_RS232_DATA, JsonUtils.toJson(new ListIntValueBody(list)), EnableBody.class)).enable;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void testRs232() {
        this.testSuccess = false;
        this.countDownLatch = new CountDownLatch(1);
        if (!this.lastRs232Enable) {
            boolean rs232SwitchEnable = setRs232SwitchEnable(true);
            this.testSuccess = rs232SwitchEnable;
            if (!rs232SwitchEnable) {
                Log.e(TAG, "setRs232SwitchEnable error");
                Rs232testDone(false, "");
            }
        }
        boolean zSendRs232Data = sendRs232Data(this.rs232Testdata);
        this.testSuccess = zSendRs232Data;
        if (!zSendRs232Data) {
            Log.e(TAG, "sendRs232Data error");
            Rs232testDone(false, "");
        } else {
            try {
                this.testSuccess = false;
                this.countDownLatch.await(3L, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Rs232testDone(this.testSuccess, "");
        }
        this.countDownLatch = null;
    }

    public void Rs232testDone(final boolean z, String str) {
        this.mHandler.post(new Runnable() { // from class: com.ifpdos.factorytest.Rs232Test.1
            @Override // java.lang.Runnable
            public void run() {
                Rs232Test.this.testDone(z, "");
            }
        });
    }

    @Override // com.ifpdos.factorytest.TestItem
    void onDestroy() {
        super.onDestroy();
        UdiSdk.unregisterEvents(this.eventHandler);
        setRs232SwitchEnable(this.lastRs232Enable);
        if (this.mWriteHandler != null) {
            this.mWriteThread.quit();
            this.mWriteHandler.removeCallbacksAndMessages(null);
        }
    }
}
