package com.dss.ups.sp51a.ble;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.util.Log;
import com.cvte.dss.ups.plugin.IUpsPlugin;
import com.dss.ups.sp51a.ble.data.ProtocolDefineKt;
import com.dss.ups.sp51a.ble.manager.BleDeviceWrapper;
import com.dss.ups.sp51a.ble.manager.BleSdkManager;
import com.dss.ups.sp51a.ble.manager.BleUtils;
import g.a.a.a.a;
import g.h.a.i;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import kotlin.Metadata;
import kotlin.NotImplementedError;
import kotlin.collections.j;
import kotlin.jvm.internal.o;
import kotlin.text.Charsets;

/* JADX INFO: loaded from: classes.dex */
@Metadata(bv = {1, 0, 3}, d1 = {"\u00004\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010 \n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0010\u000b\n\u0000\n\u0002\u0018\u0002\n\u0002\b\b\u0018\u0000 \u00162\u00020\u0001:\u0001\u0016B\u0007¢\u0006\u0004\b\u0014\u0010\u0015J\u0019\u0010\u0005\u001a\u0004\u0018\u00010\u00042\u0006\u0010\u0003\u001a\u00020\u0002H\u0016¢\u0006\u0004\b\u0005\u0010\u0006J\u001d\u0010\b\u001a\b\u0012\u0004\u0012\u00020\u00040\u00072\u0006\u0010\u0003\u001a\u00020\u0002H\u0016¢\u0006\u0004\b\b\u0010\tJ7\u0010\u0011\u001a\u00020\r2\u0006\u0010\u0003\u001a\u00020\u00022\u0006\u0010\u000b\u001a\u00020\n2\u0006\u0010\f\u001a\u00020\n2\u0006\u0010\u000e\u001a\u00020\r2\u0006\u0010\u0010\u001a\u00020\u000fH\u0016¢\u0006\u0004\b\u0011\u0010\u0012J/\u0010\u0011\u001a\u00020\r2\u0006\u0010\u0003\u001a\u00020\u00022\u0006\u0010\f\u001a\u00020\n2\u0006\u0010\u000e\u001a\u00020\r2\u0006\u0010\u0010\u001a\u00020\u000fH\u0016¢\u0006\u0004\b\u0011\u0010\u0013¨\u0006\u0017"}, d2 = {"Lcom/dss/ups/sp51a/ble/Plugin;", "Lcom/cvte/dss/ups/plugin/IUpsPlugin;", "Landroid/content/Context;", "context", "Lcom/cvte/dss/ups/plugin/IUpsPlugin$FirmwareInfo;", "getFirmwareInfo", "(Landroid/content/Context;)Lcom/cvte/dss/ups/plugin/IUpsPlugin$FirmwareInfo;", "", "getFirmwareInfos", "(Landroid/content/Context;)Ljava/util/List;", "", "id", "path", "", "force", "Lcom/cvte/dss/ups/plugin/IUpsPlugin$IUpgradeStatusListener;", "listener", "upgradeFirmware", "(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;ZLcom/cvte/dss/ups/plugin/IUpsPlugin$IUpgradeStatusListener;)Z", "(Landroid/content/Context;Ljava/lang/String;ZLcom/cvte/dss/ups/plugin/IUpsPlugin$IUpgradeStatusListener;)Z", "<init>", "()V", "Companion", "lib_release"}, k = 1, mv = {1, 5, 1})
public final class Plugin implements IUpsPlugin {
    private static final String TAG = "Plugin-SP51A-BLE";
    private static final String UUID_DESCRIPTOR_CLIENT_CHARACTERISTIC_CONFIGURATION = "00002902-0000-1000-8000-00805f9b34fb";

    @Override // com.cvte.dss.ups.plugin.IUpsPlugin
    public IUpsPlugin.FirmwareInfo getFirmwareInfo(Context context) {
        o.f(context, "context");
        throw new NotImplementedError(a.t("An operation is not implemented: ", "Not yet implemented"));
    }

    @Override // com.cvte.dss.ups.plugin.IUpsPlugin
    public List<IUpsPlugin.FirmwareInfo> getFirmwareInfos(Context context) {
        String str;
        String str2;
        o.f(context, "context");
        ArrayList arrayList = new ArrayList();
        BleUtils bleUtils = BleUtils.INSTANCE;
        if (bleUtils.getBluetoothState()) {
            List<BluetoothDevice> pairAndConnectDevice = bleUtils.getPairAndConnectDevice();
            ArrayList<BluetoothDevice> arrayList2 = new ArrayList();
            for (Object obj : pairAndConnectDevice) {
                if (o.a(((BluetoothDevice) obj).getName(), "SP51A")) {
                    arrayList2.add(obj);
                }
            }
            ArrayList arrayList3 = new ArrayList(i.H(arrayList2, 10));
            for (BluetoothDevice bluetoothDevice : arrayList2) {
                StringBuilder sbF = a.F("address:");
                sbF.append((Object) bluetoothDevice.getAddress());
                sbF.append(", name:");
                sbF.append((Object) bluetoothDevice.getName());
                sbF.append(", type:");
                sbF.append(bluetoothDevice.getType());
                Log.i(TAG, sbF.toString());
                arrayList3.add(bluetoothDevice);
            }
            if (!arrayList3.isEmpty()) {
                int i2 = 0;
                for (Object obj2 : arrayList3) {
                    int i3 = i2 + 1;
                    if (i2 < 0) {
                        j.e0();
                        throw null;
                    }
                    BluetoothDevice bluetoothDevice2 = (BluetoothDevice) obj2;
                    BleDeviceWrapper bleDeviceWrapper = new BleDeviceWrapper(bluetoothDevice2);
                    if (bleDeviceWrapper.connect(context, true)) {
                        byte[] characteristic = bleDeviceWrapper.readCharacteristic(ProtocolDefineKt.DEVICE_SOFTWARE_UUID);
                        if (characteristic == null) {
                            str = "Read Characteristic fail";
                        } else {
                            String str3 = new String(characteristic, Charsets.f9181b);
                            Log.i(TAG, o.l("version: ", str3));
                            if (bleDeviceWrapper.disconnect()) {
                                Log.i(TAG, o.l("get sp51a ble ota_key :", ConstantKt.SP51A_BLE_OTA_KEY));
                                if (!(str3.length() == 0)) {
                                    if (!(ConstantKt.SP51A_BLE_OTA_KEY.length() == 0)) {
                                        arrayList.add(new IUpsPlugin.FirmwareInfo("SP51A-BLE", "RF487A", "SP51A-BLE", str3, ConstantKt.SP51A_BLE_OTA_KEY, false, false, bluetoothDevice2.getAddress().toString()));
                                    }
                                }
                            }
                            i2 = i3;
                        }
                    } else {
                        str = "Connect gatt fail";
                    }
                    Log.e(TAG, str);
                    i2 = i3;
                }
                return arrayList;
            }
            str2 = "SP51A is not connect...";
        } else {
            str2 = "bluetooth is not enable...";
        }
        Log.e(TAG, str2);
        return arrayList;
    }

    @Override // com.cvte.dss.ups.plugin.IUpsPlugin
    public boolean upgradeFirmware(Context context, String id, String path, boolean force, IUpsPlugin.IUpgradeStatusListener listener) {
        o.f(context, "context");
        o.f(id, "id");
        o.f(path, "path");
        o.f(listener, "listener");
        if (!new File(path).exists()) {
            Log.e(TAG, "file not found");
            listener.onFailed("file not found");
            return false;
        }
        BleUtils bleUtils = BleUtils.INSTANCE;
        if (!bleUtils.getBluetoothState()) {
            Log.e(TAG, "bluetooth is not enable");
            listener.onFailed("bluetooth is not enable");
            return false;
        }
        List<BluetoothDevice> pairAndConnectDevice = bleUtils.getPairAndConnectDevice();
        ArrayList<BluetoothDevice> arrayList = new ArrayList();
        Iterator<T> it = pairAndConnectDevice.iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            Object next = it.next();
            BluetoothDevice bluetoothDevice = (BluetoothDevice) next;
            if (o.a(bluetoothDevice.getName(), "SP51A") && o.a(bluetoothDevice.getAddress(), id)) {
                arrayList.add(next);
            }
        }
        ArrayList arrayList2 = new ArrayList(i.H(arrayList, 10));
        bluetoothDevice = null;
        for (BluetoothDevice bluetoothDevice2 : arrayList) {
            StringBuilder sbF = a.F("address:");
            sbF.append((Object) bluetoothDevice2.getAddress());
            sbF.append(", name:");
            sbF.append((Object) bluetoothDevice2.getName());
            sbF.append(", type:");
            sbF.append(bluetoothDevice2.getType());
            Log.i(TAG, sbF.toString());
            arrayList2.add(bluetoothDevice2);
        }
        if (arrayList2.isEmpty()) {
            Log.e(TAG, "SP51A is not connect...");
            listener.onFailed("SP51A is not connect");
            return false;
        }
        Log.e(TAG, "sdk initialize");
        BleSdkManager bleSdkManager = new BleSdkManager();
        if (bluetoothDevice2 == null) {
            Log.e(TAG, "SP51A device not found");
            listener.onFailed("SP51A device not found");
            return false;
        }
        if (bleSdkManager.startOta(context, bluetoothDevice2, path, listener)) {
            Log.e(TAG, "upgradeFirmware quit");
            return true;
        }
        Log.e(TAG, "ota fail...");
        return false;
    }

    @Override // com.cvte.dss.ups.plugin.IUpsPlugin
    public boolean upgradeFirmware(Context context, String path, boolean force, IUpsPlugin.IUpgradeStatusListener listener) {
        o.f(context, "context");
        o.f(path, "path");
        o.f(listener, "listener");
        throw new NotImplementedError(a.t("An operation is not implemented: ", "Not yet implemented"));
    }
}
