getDeviceInfo method

Future<DeviceInfo> getDeviceInfo()

Get device information from platform

Implementation

Future<DeviceInfo> getDeviceInfo() async {
  DebugLogger.log('[DeviceInfoDataSource] Calling platform.getDeviceInfo()...');

  // Detect platform
  PlatformType platformType;
  if (Platform.isIOS) {
    platformType = PlatformType.ios;
  } else if(Platform.isMacOS){
    platformType = PlatformType.macOS;
  }else if (Platform.isWindows) {
    platformType = PlatformType.windows;
  } else if (Platform.isLinux) {
    platformType = PlatformType.linux;
  } else {
    platformType = PlatformType.android;
  }
  DebugLogger.log('[DeviceInfoDataSource] Platform detected: $platformType');

  // For Linux, read from environment file directly
  if (platformType == PlatformType.linux) {
    DebugLogger.log(
      '[DeviceInfoDataSource] Linux platform - reading from environment file',
    );
    final envData = await LinuxEnvDataSource.getDeviceInfoFromEnv();

    final deviceId = envData['deviceId'] ?? '';
    final baseUrl = envData['baseUrl'];

    DebugLogger.log('[DeviceInfoDataSource] Environment data:');
    DebugLogger.log('[DeviceInfoDataSource]   - deviceId: $deviceId');
    DebugLogger.log('[DeviceInfoDataSource]   - baseUrl: $baseUrl');

    if (deviceId.isEmpty) {
      throw Exception('Device ID not found in Linux environment file');
    }

    return DeviceInfo(
      deviceId: deviceId,
      deviceType: DeviceType.mobile,
      platformType: platformType,
      mdmConfig: baseUrl != null ? {'API_URL': baseUrl} : null,
    );
  }

  // For Windows, read from registry directly
  if (platformType == PlatformType.windows) {
    DebugLogger.log('[DeviceInfoDataSource] Windows platform - reading from registry');
    final registryData =
        await WindowsRegistryDataSource.getDeviceInfoFromRegistry();

    final deviceId = registryData['deviceId'] ?? '';
    final baseUrl = registryData['baseUrl'];

    DebugLogger.log('[DeviceInfoDataSource] Registry data:');
    DebugLogger.log('[DeviceInfoDataSource]   - deviceId: $deviceId');
    DebugLogger.log('[DeviceInfoDataSource]   - baseUrl: $baseUrl');

    if (deviceId.isEmpty) {
      throw Exception('Device ID not found in Windows registry');
    }

    return DeviceInfo(
      deviceId: deviceId,
      deviceType: DeviceType.mobile,
      platformType: platformType,
      mdmConfig: baseUrl != null ? {'API_URL': baseUrl} : null,
    );
  }

  final result = await platform.getDeviceInfo();
  DebugLogger.log('[DeviceInfoDataSource] Platform result received:');
  DebugLogger.log('[DeviceInfoDataSource]   - Keys: ${result.keys.toList()}');
  DebugLogger.log('[DeviceInfoDataSource]   - deviceType: ${result['deviceType']}');
  DebugLogger.log('[DeviceInfoDataSource]   - deviceId: ${result['deviceId']}');
  DebugLogger.log(
    '[DeviceInfoDataSource]   - token: ${result['token'] != null ? "Present" : "NULL"}',
  );
  DebugLogger.log(
    '[DeviceInfoDataSource]   - refreshToken: ${result['refreshToken'] != null ? "Present" : "NULL"}',
  );

  final deviceType = result['deviceType'] == 'tv'
      ? DeviceType.tv
      : DeviceType.mobile;

  // Get MDM config for iOS
  Map<String, dynamic>? mdmConfig;
  String? certId;
  if (platformType == PlatformType.ios || platformType == PlatformType.macOS) {
    try {
      mdmConfig = await platform.getMDMConfig();
      DebugLogger.log(
        '[DeviceInfoDataSource] MDM Config retrieved: ${mdmConfig != null}',
      );
      if (mdmConfig != null) {
        DebugLogger.log(
          '[DeviceInfoDataSource] MDM Config keys: ${mdmConfig.keys.toList()}',
        );
        certId = mdmConfig['CERT_ID'] as String?;
        DebugLogger.log('[DeviceInfoDataSource] CERT_ID: $certId');

        // Check if CERT_ID is missing or empty
        if (certId == null || certId.isEmpty) {
          DebugLogger.log('[DeviceInfoDataSource] ❌ CERT_ID not found in MDM config');
          throw Exception('CERT_ID not found in iOS MDM configuration. Device is not enrolled.');
        }
      } else {
        DebugLogger.log('[DeviceInfoDataSource] ❌ MDM Config is null');
        throw Exception('MDM configuration not found. Device is not enrolled.');
      }
    } catch (e) {
      DebugLogger.log('[DeviceInfoDataSource] ⚠️ Failed to get MDM config or CERT_ID: $e');
      rethrow;
    }
  }

  DebugLogger.log('[DeviceInfoDataSource] Parsed device type: $deviceType');

  final deviceInfo = DeviceInfo(
    deviceId:
        result['deviceId'] as String? ?? result['token'] as String? ?? '',
    deviceType: deviceType,
    platformType: platformType,
    mdmConfig: mdmConfig,
  );

  DebugLogger.log('[DeviceInfoDataSource] Created DeviceInfo:');
  DebugLogger.log('[DeviceInfoDataSource]   - deviceId: ${deviceInfo.deviceId}');
  DebugLogger.log('[DeviceInfoDataSource]   - deviceType: ${deviceInfo.deviceType}');
  DebugLogger.log(
    '[DeviceInfoDataSource]   - platformType: ${deviceInfo.platformType}',
  );

  return deviceInfo;
}