package com.cvte.dss.ups.u3hub;

import android.content.Context;
import android.util.Log;
import com.cvte.dss.ups.plugin.IUpsPlugin;
import com.cvte.dss.ups.u3hub.hub.HubDeviceManager;
import com.cvte.dss.ups.u3hub.hub.HubUpgradeApi;
import com.cvte.dss.ups.u3hub.listener.OnUpgradeResultListener;
import com.cvte.dss.ups.u3hub.model.UpgradeResult;
import g.a.a.a.a;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import kotlin.Metadata;
import kotlin.jvm.internal.c0;
import kotlin.jvm.internal.o;
import no.nordicsemi.android.dfu.DfuServiceInitiator;
import o.a.e.b;

/* JADX INFO: loaded from: classes.dex */
@Metadata(bv = {1, 0, 3}, d1 = {"\u00002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0000\n\u0002\u0010\u000b\n\u0002\b\u0007\u0018\u0000 \u00142\u00020\u0001:\u0001\u0014B\u0007¢\u0006\u0004\b\u0012\u0010\u0013J!\u0010\u0007\u001a\u0004\u0018\u00010\u00062\u0006\u0010\u0003\u001a\u00020\u00022\u0006\u0010\u0005\u001a\u00020\u0004H\u0002¢\u0006\u0004\b\u0007\u0010\bJ\u0019\u0010\n\u001a\u0004\u0018\u00010\t2\u0006\u0010\u0003\u001a\u00020\u0002H\u0016¢\u0006\u0004\b\n\u0010\u000bJ/\u0010\u0010\u001a\u00020\u000e2\u0006\u0010\u0003\u001a\u00020\u00022\u0006\u0010\r\u001a\u00020\f2\u0006\u0010\u000f\u001a\u00020\u000e2\u0006\u0010\u0005\u001a\u00020\u0004H\u0016¢\u0006\u0004\b\u0010\u0010\u0011¨\u0006\u0015"}, d2 = {"Lcom/cvte/dss/ups/u3hub/Plugin;", "Lcom/cvte/dss/ups/plugin/IUpsPlugin;", "Landroid/content/Context;", "context", "Lcom/cvte/dss/ups/plugin/IUpsPlugin$IUpgradeStatusListener;", "listener", "Lo/a/e/b;", "startUpgradeProgress", "(Landroid/content/Context;Lcom/cvte/dss/ups/plugin/IUpsPlugin$IUpgradeStatusListener;)Lo/a/e/b;", "Lcom/cvte/dss/ups/plugin/IUpsPlugin$FirmwareInfo;", "getFirmwareInfo", "(Landroid/content/Context;)Lcom/cvte/dss/ups/plugin/IUpsPlugin$FirmwareInfo;", "", "path", "", "force", "upgradeFirmware", "(Landroid/content/Context;Ljava/lang/String;ZLcom/cvte/dss/ups/plugin/IUpsPlugin$IUpgradeStatusListener;)Z", "<init>", "()V", "Companion", "lib_release"}, k = 1, mv = {1, 4, 0})
public final class Plugin implements IUpsPlugin {
    public static final String TAG = "Plugin";

    private final b startUpgradeProgress(Context context, final IUpsPlugin.IUpgradeStatusListener listener) {
        int deviceNum = HubUpgradeApi.INSTANCE.getDeviceNum(context);
        a.c0("found 817&103 num:", deviceNum, TAG);
        if (deviceNum < 0) {
            return null;
        }
        final float f2 = 1.0f / (deviceNum * 30);
        Log.d(TAG, "processPerSecond:" + f2);
        final c0 c0Var = new c0();
        c0Var.f6163f = 0.0f;
        return o.a.b.b(0L, 1L, TimeUnit.SECONDS).c(o.a.i.a.f6107b).d(new o.a.g.b<Long>() { // from class: com.cvte.dss.ups.u3hub.Plugin.startUpgradeProgress.1
            @Override // o.a.g.b
            public final void accept(Long l2) {
                c0 c0Var2 = c0Var;
                float f3 = c0Var2.f6163f;
                float f4 = f2;
                if (f3 + f4 < 0.98f) {
                    float f5 = f3 + f4;
                    c0Var2.f6163f = f5;
                    listener.onProcess(f5);
                }
            }
        }, new o.a.g.b<Throwable>() { // from class: com.cvte.dss.ups.u3hub.Plugin.startUpgradeProgress.2
            @Override // o.a.g.b
            public final void accept(Throwable th) {
                Log.i(Plugin.TAG, "startUpgradeProgress: ", th);
            }
        });
    }

    @Override // com.cvte.dss.ups.plugin.IUpsPlugin
    public IUpsPlugin.FirmwareInfo getFirmwareInfo(Context context) {
        o.g(context, "context");
        String version = new HubDeviceManager().getVersion(context);
        if (version != null) {
            return new IUpsPlugin.FirmwareInfo("front-usb-board", "VL817&103", "front-usb-board", version, "a8af0d370105aa246289ab83f4f2e346d5708fe7", true, false);
        }
        return null;
    }

    @Override // com.cvte.dss.ups.plugin.IUpsPlugin
    public boolean upgradeFirmware(final Context context, String path, boolean force, final IUpsPlugin.IUpgradeStatusListener listener) {
        o.g(context, "context");
        o.g(path, "path");
        o.g(listener, "listener");
        final b bVarStartUpgradeProgress = startUpgradeProgress(context, listener);
        if (bVarStartUpgradeProgress == null) {
            return false;
        }
        new HubDeviceManager().upgrade(context, path, new OnUpgradeResultListener() { // from class: com.cvte.dss.ups.u3hub.Plugin.upgradeFirmware.1
            @Override // com.cvte.dss.ups.u3hub.listener.OnUpgradeResultListener
            public final void onUpgradeResult(final UpgradeResult upgradeResult) throws InterruptedException, IOException {
                Log.i(Plugin.TAG, "upgradeFirmware: " + upgradeResult);
                bVarStartUpgradeProgress.b();
                if (upgradeResult != UpgradeResult.SUCCESS) {
                    listener.onFailed(upgradeResult.name());
                    return;
                }
                if (PackageUtil.INSTANCE.isRunningInDsu(context)) {
                    listener.onProcess(1.0f);
                    listener.onComplete();
                } else {
                    Thread.sleep(1000L);
                    UdiInvokeUtil.INSTANCE.startRebootDsu();
                    new Thread(new Runnable() { // from class: com.cvte.dss.ups.u3hub.Plugin.upgradeFirmware.1.1
                        @Override // java.lang.Runnable
                        public final void run() throws InterruptedException, IOException {
                            Thread.sleep(30000L);
                            int i2 = 0;
                            while (true) {
                                boolean zCheckDsuRebootReady = UdiInvokeUtil.INSTANCE.checkDsuRebootReady();
                                a.k0("checkDsuRebootReady:", zCheckDsuRebootReady, Plugin.TAG);
                                if (zCheckDsuRebootReady) {
                                    listener.onProcess(1.0f);
                                    listener.onComplete();
                                    return;
                                }
                                Log.d(Plugin.TAG, "checkDsuRebootReady fail,retry :" + i2);
                                Thread.sleep(DfuServiceInitiator.DEFAULT_SCAN_TIMEOUT);
                                int i3 = i2 + 1;
                                if (i2 >= 36) {
                                    listener.onFailed(upgradeResult.name());
                                    return;
                                }
                                i2 = i3;
                            }
                        }
                    }).start();
                }
            }
        });
        return true;
    }
}
