package com.cvte.dss.ups.camera;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import com.cvte.dss.ups.camera.model.UpgradeResult;
import com.cvte.dss.ups.camera.utils.CameraHelper;
import com.cvte.dss.ups.camera.utils.CommonUtils;
import com.cvte.dss.ups.plugin.IUpsPlugin;
import com.cvte.vman.VmanMiddleWare;
import com.cvte.vman.instance.InvokeCmd;
import g.a.a.a.a;
import g.h.a.i;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import kotlin.Metadata;
import kotlin.Result;
import kotlin.collections.j;
import kotlin.jvm.internal.c0;
import kotlin.jvm.internal.o;

/* JADX INFO: loaded from: classes.dex */
@Metadata(bv = {1, 0, 3}, d1 = {"\u0000X\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0010\u000e\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0010\u0007\n\u0002\b\u0003\n\u0002\u0010\u0005\n\u0000\n\u0002\u0010\u0012\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0002\b\t\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0007\n\u0002\u0018\u0002\n\u0002\b\u0006\u0018\u0000 /2\u00020\u0001:\u0001/B\u000f\u0012\u0006\u0010+\u001a\u00020*¢\u0006\u0004\b-\u0010.J!\u0010\u0007\u001a\u00020\u00062\u0006\u0010\u0003\u001a\u00020\u00022\b\u0010\u0005\u001a\u0004\u0018\u00010\u0004H\u0002¢\u0006\u0004\b\u0007\u0010\bJ\u001f\u0010\u000b\u001a\u00020\t2\u0006\u0010\n\u001a\u00020\t2\u0006\u0010\u0005\u001a\u00020\u0004H\u0002¢\u0006\u0004\b\u000b\u0010\fJ\u0019\u0010\u0010\u001a\u0004\u0018\u00010\u000f2\u0006\u0010\u000e\u001a\u00020\rH\u0002¢\u0006\u0004\b\u0010\u0010\u0011J\u001f\u0010\u0013\u001a\u00020\u00122\u0006\u0010\n\u001a\u00020\t2\u0006\u0010\u0005\u001a\u00020\u0004H\u0002¢\u0006\u0004\b\u0013\u0010\u0014J\u0017\u0010\u0015\u001a\u00020\u00122\u0006\u0010\u0003\u001a\u00020\u0002H\u0002¢\u0006\u0004\b\u0015\u0010\u0016J\u0017\u0010\u0018\u001a\u00020\u00122\u0006\u0010\u0017\u001a\u00020\u0002H\u0002¢\u0006\u0004\b\u0018\u0010\u0016J\r\u0010\u0019\u001a\u00020\u0002¢\u0006\u0004\b\u0019\u0010\u001aJ\r\u0010\u001b\u001a\u00020\u0002¢\u0006\u0004\b\u001b\u0010\u001aJ%\u0010\u001e\u001a\u00020\u00122\u0006\u0010\u0003\u001a\u00020\u00022\u0006\u0010\u0005\u001a\u00020\u00042\u0006\u0010\u001d\u001a\u00020\u001c¢\u0006\u0004\b\u001e\u0010\u001fJ\u0015\u0010\u001e\u001a\u00020 2\u0006\u0010\u0003\u001a\u00020\u0002¢\u0006\u0004\b\u001e\u0010!R\"\u0010#\u001a\u00020\"8\u0006@\u0006X\u0086\u000e¢\u0006\u0012\n\u0004\b#\u0010$\u001a\u0004\b%\u0010&\"\u0004\b'\u0010(R\u0018\u0010\u001d\u001a\u0004\u0018\u00010\u001c8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b\u001d\u0010)R\u0016\u0010+\u001a\u00020*8\u0002@\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b+\u0010,¨\u00060"}, d2 = {"Lcom/cvte/dss/ups/camera/CameraManager;", "", "", "path", "Lcom/cvte/dss/ups/plugin/IUpsPlugin$IUpgradeStatusListener;", "listener", "", "startUploadFile", "(Ljava/lang/String;Lcom/cvte/dss/ups/plugin/IUpsPlugin$IUpgradeStatusListener;)Z", "", "process", "startReadFileProgress", "(FLcom/cvte/dss/ups/plugin/IUpsPlugin$IUpgradeStatusListener;)F", "", "sum", "", "checkSum", "(B)[B", "", "checkCameraVersion", "(FLcom/cvte/dss/ups/plugin/IUpsPlugin$IUpgradeStatusListener;)V", "deleteTempFile", "(Ljava/lang/String;)V", "upgradePath", "finishUpgradeCamera", "getVersion", "()Ljava/lang/String;", "getOTAKey", "Landroid/content/Context;", "context", "upgradeCamera", "(Ljava/lang/String;Lcom/cvte/dss/ups/plugin/IUpsPlugin$IUpgradeStatusListener;Landroid/content/Context;)V", "Lcom/cvte/dss/ups/camera/model/UpgradeResult;", "(Ljava/lang/String;)Lcom/cvte/dss/ups/camera/model/UpgradeResult;", "Landroid/os/Handler;", "mHandler", "Landroid/os/Handler;", "getMHandler", "()Landroid/os/Handler;", "setMHandler", "(Landroid/os/Handler;)V", "Landroid/content/Context;", "Lcom/cvte/dss/ups/camera/CameraHdiApi;", "cameraHdiApi", "Lcom/cvte/dss/ups/camera/CameraHdiApi;", "<init>", "(Lcom/cvte/dss/ups/camera/CameraHdiApi;)V", "Companion", "lib_release"}, k = 1, mv = {1, 4, 0})
public final class CameraManager {
    private static final byte CAMERA_MAIN_START_UPGRADE = 34;
    private static final byte CAMERA_SUB_CODE_GET_OTA_BACK = 15;
    private static final byte CAMERA_SUB_CODE_GET_OTA_KEY = 14;
    private static final byte CAMERA_SUB_CODE_GET_VERSION = 6;
    private static final byte CAMERA_SUB_CODE_GET_VERSION_BACK = 7;
    private static final byte CAMERA_SUB_START_UPGRADE = 0;
    private static final byte CAMERA_SUB_UPLOAD_FINISH = 2;
    private static final int COMMAND_DATA_BEGIN_INDEX = 7;
    private static final int HID_UPLOAD_DATA = 510;
    private static final int MAX_TRY_COUNT = 3;
    public static final int NORMAL_TRY_SLEEP_TIME = 500;
    private static final String TAG = "CameraServiceCore";
    public static final String TEMP_CAMERA_FILE_DIR = "/sdcard/tmp/";
    private static final int TIME_SECOND_CAMERA_REBOOT_FINISH = 16;
    private static final long TIME_SECOND_CAMERA_REBOOT_FINISH_INTERNAL = 1000;
    private final CameraHdiApi cameraHdiApi;
    private Context context;
    private Handler mHandler;

    public CameraManager(CameraHdiApi cameraHdiApi) {
        o.g(cameraHdiApi, "cameraHdiApi");
        this.cameraHdiApi = cameraHdiApi;
        this.mHandler = new Handler(Looper.getMainLooper());
    }

    private final void checkCameraVersion(float process, IUpsPlugin.IUpgradeStatusListener listener) {
        Object objQ0;
        CommonUtils.INSTANCE.safeSleep(InvokeCmd.CMD_PMSCTRL_SET_PMS_WAKEUP_SOURCES);
        for (int i2 = 0; i2 < 10; i2++) {
            Plugin plugin = new Plugin();
            Context context = this.context;
            if (context == null) {
                o.m();
                throw null;
            }
            IUpsPlugin.FirmwareInfo firmwareInfo = plugin.getFirmwareInfo(context);
            String version = firmwareInfo != null ? firmwareInfo.getVersion() : null;
            Log.i(TAG, "checkCameraVersion: " + version);
            if (!TextUtils.isEmpty(version)) {
                listener.onProcess(1.0f);
                listener.onComplete();
                try {
                    VmanMiddleWare vmanMiddleWare = VmanMiddleWare.getInstance();
                    o.b(vmanMiddleWare, "VmanMiddleWare.getInstance()");
                    objQ0 = Integer.valueOf(Log.i(TAG, "setCameraStatusUpdate: result = " + vmanMiddleWare.getAvManager().setCameraStatusUpdate(true)));
                } catch (Throwable th) {
                    objQ0 = i.q0(th);
                }
                if (Result.a(objQ0) != null) {
                    Log.e(TAG, "setCameraStatusUpdate error");
                    return;
                }
                return;
            }
            CommonUtils.INSTANCE.safeSleep(7000);
            if (((double) process) + 0.1d < 0.99d) {
                process += 0.1f;
            }
            listener.onProcess(process);
        }
        listener.onFailed(" checkCameraVersion fail");
    }

    private final byte[] checkSum(byte sum) {
        return this.cameraHdiApi.sendCameraCommand((byte) 34, (byte) 2, new byte[]{sum});
    }

    private final void deleteTempFile(String path) {
        Log.e(TAG, "deleteTempFile path = " + path);
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
    }

    private final void finishUpgradeCamera(String upgradePath) {
        deleteTempFile(upgradePath);
        CameraHelper.INSTANCE.removeUpgradingCamera(this.cameraHdiApi.getCameraInfo());
    }

    private final float startReadFileProgress(float process, final IUpsPlugin.IUpgradeStatusListener listener) {
        final c0 c0Var = new c0();
        c0Var.f6163f = process;
        this.mHandler.postDelayed(new Runnable() { // from class: com.cvte.dss.ups.camera.CameraManager.startReadFileProgress.1
            @Override // java.lang.Runnable
            public void run() {
                c0 c0Var2 = c0Var;
                float f2 = c0Var2.f6163f;
                if (((double) f2) + 0.001d < 0.5f) {
                    float f3 = f2 + 0.001f;
                    c0Var2.f6163f = f3;
                    listener.onProcess(f3);
                }
                CameraManager.this.getMHandler().postDelayed(this, 2000L);
            }
        }, 2000L);
        return c0Var.f6163f;
    }

    private final boolean startUploadFile(String path, IUpsPlugin.IUpgradeStatusListener listener) throws IOException {
        File file = new File(path);
        if (!file.exists()) {
            Log.i(TAG, "startUploadFile: file not exist");
            if (listener != null) {
                listener.onFailed("file not exist!");
            }
            return false;
        }
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] bArr = new byte[HID_UPLOAD_DATA];
        this.cameraHdiApi.connect();
        byte b2 = 0;
        while (true) {
            try {
                try {
                    if (fileInputStream.read(bArr) == -1) {
                        break;
                    }
                    this.cameraHdiApi.startUpgradeDataTransfer(bArr);
                    for (int i2 = 0; i2 < HID_UPLOAD_DATA; i2++) {
                        b2 = (byte) (b2 + bArr[i2]);
                    }
                    j.q(bArr, (byte) 0, 0, 0, 6);
                } catch (Exception e2) {
                    Log.i(TAG, "startUploadFile: ", e2);
                    if (listener != null) {
                        listener.onFailed("startUploadFile fail");
                    }
                }
            } finally {
                fileInputStream.close();
                this.mHandler.removeCallbacksAndMessages(null);
                this.cameraHdiApi.disconnect();
            }
        }
        byte[] bArrCheckSum = checkSum(b2);
        if (bArrCheckSum != null && bArrCheckSum[7] == 0) {
            return true;
        }
        if (listener == null) {
            return false;
        }
        listener.onFailed("check sum fail");
        return false;
    }

    public final Handler getMHandler() {
        return this.mHandler;
    }

    public final String getOTAKey() {
        int i2 = 0;
        String cameraMessage = "";
        while (TextUtils.isEmpty(cameraMessage) && i2 < 3) {
            i2++;
            cameraMessage = this.cameraHdiApi.getCameraMessage((byte) 14, (byte) 15);
            CommonUtils.INSTANCE.safeSleep(500);
        }
        a.g0(" getVersion ota key", cameraMessage, TAG);
        return cameraMessage != null ? cameraMessage : "";
    }

    public final String getVersion() {
        String cameraMessage = this.cameraHdiApi.getCameraMessage((byte) 6, (byte) 7);
        int i2 = 0;
        while (TextUtils.isEmpty(cameraMessage) && i2 < 3) {
            i2++;
            cameraMessage = this.cameraHdiApi.getCameraMessage((byte) 6, (byte) 7);
            CommonUtils.INSTANCE.safeSleep(500);
        }
        a.g0(" getVersion version=", cameraMessage, TAG);
        return cameraMessage != null ? cameraMessage : "";
    }

    public final void setMHandler(Handler handler) {
        o.g(handler, "<set-?>");
        this.mHandler = handler;
    }

    public final UpgradeResult upgradeCamera(String path) {
        o.g(path, "path");
        CameraHelper.INSTANCE.addUpgradingCamera(this.cameraHdiApi.getCameraInfo());
        long length = new File(path).length();
        int i2 = (int) length;
        if (i2 == -1) {
            return new UpgradeResult(false, "file length is -1");
        }
        CommonUtils commonUtils = CommonUtils.INSTANCE;
        String strCopyFileToTemp = commonUtils.copyFileToTemp(path, TEMP_CAMERA_FILE_DIR);
        Log.i(TAG, "upgradeCamera:length " + length + "upgradeCamera:path" + path);
        byte[] bArrSendCameraCommand = this.cameraHdiApi.sendCameraCommand((byte) 34, (byte) 0, commonUtils.changeFileLengthToByteArray(i2));
        if (bArrSendCameraCommand == null || bArrSendCameraCommand[7] != 0) {
            deleteTempFile(strCopyFileToTemp);
            return new UpgradeResult(false, "check file fail");
        }
        Log.i(TAG, "upgradeCamera:发送完文件长度 开始升级，收到升级回复");
        if (!startUploadFile(strCopyFileToTemp, null)) {
            Log.i(TAG, "upload file fail");
            deleteTempFile(strCopyFileToTemp);
            this.cameraHdiApi.disconnect();
            return new UpgradeResult(false, "upload file fail");
        }
        Log.i(TAG, "upload file success");
        float upgradeProgress = 0.0f;
        while (upgradeProgress >= 0 && upgradeProgress < 100) {
            upgradeProgress = this.cameraHdiApi.getUpgradeProgress();
            Log.i(TAG, "upgradeCamera: " + upgradeProgress);
        }
        this.cameraHdiApi.disconnect();
        for (int i3 = 0; i3 < 16; i3++) {
            CommonUtils.INSTANCE.safeSleep(1000L);
        }
        deleteTempFile(strCopyFileToTemp);
        Log.i(TAG, "upgradeCamera: finish");
        return new UpgradeResult(true, null, 2, null);
    }

    public final void upgradeCamera(String path, IUpsPlugin.IUpgradeStatusListener listener, Context context) {
        o.g(path, "path");
        o.g(listener, "listener");
        o.g(context, "context");
        CameraHelper.INSTANCE.addUpgradingCamera(this.cameraHdiApi.getCameraInfo());
        long length = new File(path).length();
        int i2 = (int) length;
        if (i2 == -1) {
            listener.onFailed("file length is -1");
            finishUpgradeCamera(path);
            return;
        }
        this.context = context;
        Log.i(TAG, "upgradeCamera:length " + length + "upgradeCamera:path" + path);
        byte[] bArrSendCameraCommand = this.cameraHdiApi.sendCameraCommand((byte) 34, (byte) 0, CommonUtils.INSTANCE.changeFileLengthToByteArray(i2));
        if (bArrSendCameraCommand == null || bArrSendCameraCommand[7] != 0) {
            listener.onFailed("check file fail");
            finishUpgradeCamera(path);
            return;
        }
        Log.i(TAG, "upgradeCamera:发送完文件长度 开始升级，收到升级回复");
        listener.onProcess(0.1f);
        startReadFileProgress(0.1f, listener);
        if (!startUploadFile(path, listener)) {
            Log.i(TAG, "upload file fail");
            finishUpgradeCamera(path);
            listener.onFailed("upload file fail");
            this.cameraHdiApi.disconnect();
            return;
        }
        Log.i(TAG, "upload file success");
        float upgradeProgress = 0.0f;
        float f2 = 0.5f;
        while (true) {
            float f3 = 0;
            if (upgradeProgress < f3 || upgradeProgress >= 100) {
                break;
            }
            upgradeProgress = this.cameraHdiApi.getUpgradeProgress();
            Log.i(TAG, "upgradeCamera: " + upgradeProgress);
            if (upgradeProgress > f3) {
                f2 = (upgradeProgress / 400) + 0.5f;
                listener.onProcess(f2);
            }
        }
        this.cameraHdiApi.disconnect();
        for (int i3 = 0; i3 < 16; i3++) {
            CommonUtils.INSTANCE.safeSleep(1000L);
            f2 += 0.01f;
            listener.onProcess(f2);
        }
        Log.i(TAG, "upgradeCamera: finish");
        checkCameraVersion(f2, listener);
        finishUpgradeCamera(path);
    }
}
