getDeviceInfoFromRegistry static method

Future<Map<String, String?>> getDeviceInfoFromRegistry()

Get device ID and base URL from Windows registry

Reads from: .DEFAULT\Software\HaloAgent Returns: Map with 'deviceId' and 'baseUrl' (decoded from base64)

Implementation

static Future<Map<String, String?>> getDeviceInfoFromRegistry() async {
  DebugLogger.log('[WindowsRegistry] === getDeviceInfoFromRegistry() called ===');

  try {
    final registryValues = await readAllRegistryValues(
      r'.DEFAULT\Software\HaloAgent',
    );

    if (registryValues.isEmpty) {
      DebugLogger.log('[WindowsRegistry] ❌ Registry values are empty');
      return {'deviceId': null, 'baseUrl': null};
    }

    DebugLogger.log(
      '[WindowsRegistry] Registry values keys: ${registryValues.keys.toList()}',
    );

    // Get DEVICE_ID
    if (!registryValues.containsKey('DEVICE_ID')) {
      DebugLogger.log('[WindowsRegistry] ❌ DEVICE_ID not found in registry');
      return {'deviceId': null, 'baseUrl': null};
    }

    final deviceId = registryValues['DEVICE_ID']!;
    DebugLogger.log('[WindowsRegistry] ✅ DEVICE_ID found: $deviceId');

    // Get and decode API_URL
    String? baseUrl;
    if (registryValues.containsKey('API_URL')) {
      final encodedBaseUrl = registryValues['API_URL']!;
      DebugLogger.log('[WindowsRegistry] API_URL (encoded) found: $encodedBaseUrl');

      try {
        // Decode Base64 API_URL
        final decodedBytes = base64.decode(encodedBaseUrl);
        baseUrl = utf8.decode(decodedBytes);
        DebugLogger.log('[WindowsRegistry] ✅ API_URL decoded: $baseUrl');
      } catch (e) {
        DebugLogger.log('[WindowsRegistry] ❌ Failed to decode API_URL: $e');
      }
    } else {
      DebugLogger.log('[WindowsRegistry] ⚠️ API_URL not found in registry');
    }

    return {'deviceId': deviceId, 'baseUrl': baseUrl};
  } catch (e) {
    DebugLogger.log('[WindowsRegistry] ❌ Exception in getDeviceInfoFromRegistry: $e');
    return {'deviceId': null, 'baseUrl': null};
  }
}