| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- package com.paul.drone.util;
- import android.util.Log;
- import com.paul.drone.keyvalue.DroneTypeEnum;
- import dji.sdk.keyvalue.key.CameraKey;
- import dji.sdk.keyvalue.key.DJIKey;
- import dji.sdk.keyvalue.key.DJIKeyInfo;
- import dji.sdk.keyvalue.key.FlightControllerKey;
- import dji.sdk.keyvalue.key.ProductKey;
- import dji.sdk.keyvalue.key.RemoteControllerKey;
- import dji.sdk.keyvalue.value.camera.CameraType;
- import dji.sdk.keyvalue.value.common.LocationCoordinate2D;
- import dji.sdk.keyvalue.value.product.ProductType;
- import dji.v5.manager.KeyManager;
- import dji.v5.et.*;
- /**
- * 设备信息集中管理工具类
- * 统一获取 serialNumber、productType、cameraType,并带缓存和异常兜底
- */
- public class DeviceInfoManager {
- private static final String TAG = "DeviceInfoManager";
- private static volatile DeviceInfoManager INSTANCE;
- // 缓存属性
- private String cachedSerialNumber;
- private String cachedProductType;
- private DeviceInfoManager() {
- // 私有构造函数
- }
- /**
- * 获取单例实例
- * @return DeviceInfoManager实例
- */
- public static DeviceInfoManager getInstance() {
- if (INSTANCE == null) {
- synchronized (DeviceInfoManager.class) {
- if (INSTANCE == null) {
- INSTANCE = new DeviceInfoManager();
- }
- }
- }
- return INSTANCE;
- }
- /**
- * 清理缓存
- */
- public void clearCache() {
- cachedSerialNumber = null;
- cachedProductType = null;
- }
- // 默认重试次数
- private static final int DEFAULT_RETRY_COUNT = 5;
- // 重试间隔时间(ms)
- private static final long RETRY_INTERVAL_MS = 1000;
- /**
- * 获取设备序列号
- * @return 序列号字符串
- */
- public String getFlySerialNumber() {
- if (cachedSerialNumber != null) {
- return cachedSerialNumber;
- }
- String serialNumber = retryOperation(this::getFlightSerialNumber, DEFAULT_RETRY_COUNT, RETRY_INTERVAL_MS);
- if (!serialNumber.isEmpty()) {
- cachedSerialNumber = serialNumber;
- return serialNumber;
- }
- cachedSerialNumber = "暂无";
- return cachedSerialNumber;
- }
- /**
- * 通用重试操作方法
- * @param operation 要执行的操作
- * @param maxRetries 最大重试次数
- * @param retryIntervalMs 重试间隔(毫秒)
- * @return 操作结果
- */
- private <T> T retryOperation(RetryableOperation<T> operation, int maxRetries, long retryIntervalMs) {
- int retryCount = 0;
- T result;
- while (true) {
- try {
- result = operation.execute();
- if (result != null && !isDefaultValue(result)) {
- return result;
- }
- } catch (Exception e) {
- Log.e(TAG, "Operation failed, retry count: " + retryCount, e);
- }
- retryCount++;
- if (retryCount >= maxRetries) {
- break;
- }
- try {
- Thread.sleep(retryIntervalMs);
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- Log.e(TAG, "Retry interrupted", e);
- break;
- }
- }
- return null;
- }
- /**
- * 检查结果是否为默认值
- * @param result 结果
- * @return 是否为默认值
- */
- private boolean isDefaultValue(Object result) {
- if (result instanceof String) {
- return ((String) result).isEmpty() || "UNKNOWN".equals(result);
- }
- return result == null;
- }
- /**
- * 可重试操作接口
- */
- private interface RetryableOperation<T> {
- T execute() throws Exception;
- }
- public LocationCoordinate2D getFightLocation(){
- LocationCoordinate2D value = KeyManager.getInstance().getValue(DJIKey.create(FlightControllerKey.KeyHomeLocation));
- // 先检查value是否为null
- if (value == null) {
- // 如果是null,创建一个新的LocationCoordinate2D实例
- value = new LocationCoordinate2D(0.0, 0.0);
- }
- // 然后安全地设置坐标值
- value.setLatitude(28.228079);
- value.setLongitude(112.938975);
- return value;
- }
- /**
- * 获取固件版本
- * @return 固件版本字符串
- */
- public String getFirmwareVersion() {
- // 实际应用中应该从设备获取
- // 这里返回模拟数据
- return "1.0.0";
- }
- /** 获取设备序列号,优先多种方式,带缓存 */
- private String getFlightSerialNumber() throws Exception {
- KeyManager keyManager = KeyManager.getInstance();
- if (keyManager == null) {
- throw new Exception("KeyManager is null");
- }
- //return "ADFD186ASDFLWIXUE";
- // 1. ProductKey.KeySerialNumber
- String productSN = keyManager.getValue(DJIKey.create(ProductKey.KeySerialNumber));
- if (productSN != null && !productSN.isEmpty()) {
- return productSN;
- }
- // // 2. FlightControllerKey.KeySerialNumber
- String fcSN = keyManager.getValue(DJIKey.create(FlightControllerKey.KeySerialNumber));
- if (fcSN != null && !fcSN.isEmpty()) {
- return fcSN;
- }
- throw new Exception("Failed to get serial number from all sources");
- }
- public String getRemoteSerialNumber() {
- KeyManager keyManager = KeyManager.getInstance();
- if (keyManager == null) {
- return "UNKNOWN";
- }
- // 1. RemoteControllerKey.KeyProductSerialNumber
- try {
- String productSN = keyManager.getValue(DJIKey.create(RemoteControllerKey.KeyProductSerialNumber));
- if (productSN != null && !productSN.isEmpty()) {
- return productSN;
- }
- } catch (Exception e) {
- Log.e(TAG, "RemoteControllerKey.KeyProductSerialNumber error", e);
- }
- return "UNKNOWN";
- }
- /** 获取产品类型,带缓存 */
- public String getProductType() {
- if (cachedProductType != null) {
- return cachedProductType;
- }
- KeyManager keyManager = KeyManager.getInstance();
- if (keyManager == null) {
- cachedProductType = "UNKNOWN";
- return cachedProductType;
- }
- try {
- ProductType value = keyManager.getValue(DJIKey.create(ProductKey.KeyProductType));
- String name = value.name();
- cachedProductType = name;
- return name;
- } catch (Exception e) {
- Log.e(TAG, "ProductType error", e);
- cachedProductType = "UNKNOWN";
- return cachedProductType;
- }
- }
- /** 获取相机类型 */
- public String getCameraType(int cameraIndex) {
- KeyManager keyManager = KeyManager.getInstance();
- if (keyManager == null) {
- return "UNKNOWN";
- }
- try {
- CameraType value = keyManager.getValue(DJIKey.create(CameraKey.KeyCameraType));
- String name = value.name();
- return name;
- } catch (Exception e) {
- Log.e(TAG, "CameraType error", e);
- return "UNKNOWN";
- }
- }
- /**
- * 获取产品型号或子型号(相机型号)
- * - 如果是 DJI_MATRICE_400、M300_RTK、M350_RTK,返回产品型号 name
- * - 否则,返回 CameraType 的 name(如果有),否则返回产品型号 name
- */
- public String getProductOrCameraType(int cameraIndex) {
- KeyManager keyManager = KeyManager.getInstance();
- if (keyManager == null) {
- return "UNKNOWN";
- }
- ProductType productType = null;
- try {
- productType = keyManager.getValue(DJIKey.create(ProductKey.KeyProductType));
- } catch (Exception e) {
- Log.e(TAG, "ProductType error", e);
- }
- String productTypeName = (productType != null) ? productType.name() : "UNKNOWN";
- if (productType == ProductType.DJI_MATRICE_400 ||
- productType == ProductType.M300_RTK ||
- productType == ProductType.M350_RTK) {
- return productTypeName;
- } else {
- String cameraTypeName = null;
- try {
- CameraType value = keyManager.getValue(DJIKey.create(CameraKey.KeyCameraType));
- cameraTypeName = value.name();
- } catch (Exception e) {
- Log.e(TAG, "CameraType error", e);
- }
- if (cameraTypeName != null && !cameraTypeName.isEmpty() && !cameraTypeName.equals("UNKNOWN")) {
- return cameraTypeName;
- } else {
- return productTypeName;
- }
- }
- }
- public String getModelByDroneTypeEnum(int cameraIndex) {
- KeyManager keyManager = KeyManager.getInstance();
- if (keyManager == null) {
- return "UNKNOWN";
- }
- ProductType productType = null;
- try {
- productType = keyManager.getValue(DJIKey.create(ProductKey.KeyProductType));
- } catch (Exception e) {
- Log.e(TAG, "ProductType error", e);
- }
- if (productType == ProductType.DJI_MATRICE_400 ||
- productType == ProductType.M300_RTK ||
- productType == ProductType.M350_RTK) {
- String productTypeName = productType.name();
- for (DroneTypeEnum droneType : DroneTypeEnum.values()) {
- if (droneType.getDroneType().equals(productTypeName)) {
- return droneType.getModel();
- }
- }
- return "";
- } else {
- try {
- CameraType value = keyManager.getValue(DJIKey.create(CameraKey.KeyCameraType));
- String cameraTypeName = value.name();
- for (DroneTypeEnum droneType : DroneTypeEnum.values()) {
- if (droneType.getDroneType().equals(cameraTypeName)) {
- return droneType.getModel();
- }
- }
- } catch (Exception e) {
- Log.e(TAG, "CameraType error", e);
- }
- return "UNKNOWN";
- }
- }
- public String getTypeByDroneTypeEnum(int cameraIndex) {
- if (cachedProductType != null) {
- // 使用缓存的产品类型进行查找
- for (DroneTypeEnum droneType : DroneTypeEnum.values()) {
- if (droneType.getDroneType().equals(cachedProductType)) {
- return droneType.getDroneType();
- }
- }
- }
- // 没有缓存或缓存不匹配,进行重试获取
- String result = retryOperation(() -> getTypeByDroneTypeEnumInternal(cameraIndex), DEFAULT_RETRY_COUNT, RETRY_INTERVAL_MS);
- return result != null ? result : "暂无";
- }
- /**
- * getTypeByDroneTypeEnum的内部实现
- */
- private String getTypeByDroneTypeEnumInternal(int cameraIndex) throws Exception {
- KeyManager keyManager = KeyManager.getInstance();
- if (keyManager == null) {
- throw new Exception("KeyManager is null");
- }
- ProductType productType = keyManager.getValue(DJIKey.create(ProductKey.KeyProductType));
- if (productType == null) {
- throw new Exception("ProductType is null");
- }
- if (productType == ProductType.DJI_MATRICE_400 ||
- productType == ProductType.M300_RTK ||
- productType == ProductType.M350_RTK) {
- String productTypeName = productType.name();
- for (DroneTypeEnum droneType : DroneTypeEnum.values()) {
- if (droneType.getDroneType().equals(productTypeName)) {
- cachedProductType = productTypeName;
- return droneType.getDroneType();
- }
- }
- throw new Exception("No matching DroneTypeEnum for productType: " + productTypeName);
- } else {
- CameraType cameraType = keyManager.getValue(DJIKey.create(CameraKey.KeyCameraType));
- if (cameraType == null) {
- throw new Exception("CameraType is null");
- }
- String cameraTypeName = cameraType.name();
- for (DroneTypeEnum droneType : DroneTypeEnum.values()) {
- if (droneType.getDroneType().equals(cameraTypeName)) {
- return droneType.getDroneType();
- }
- }
- throw new Exception("No matching DroneTypeEnum for cameraType: " + cameraTypeName);
- }
- }
- }
|