getRealDevice method

Future<Device?> getRealDevice()

Implementation

Future<Device?> getRealDevice() async {
  HttpClientResponse response;

  try {
    final request = await UpnpCommon.httpClient
        .getUrl(Uri.parse(location!))
        .timeout(const Duration(seconds: 5));

    response = await request.close();
  } catch (_) {
    return null;
  }

  if (response.statusCode != 200) {
    throw Exception('ERROR: Failed to fetch device description.'
        ' Status Code: ${response.statusCode}');
  }

  XmlDocument doc;

  try {
    final content =
        await response.cast<List<int>>().transform(utf8.decoder).join();
    doc = XmlDocument.parse(content);
  } on Exception catch (e) {
    throw FormatException('ERROR: Failed to parse'
        ' device description. $e');
  }

  if (doc.findAllElements('device').isEmpty) {
    throw ArgumentError('Not SCPD Compatible');
  }

  return Device()..loadFromXml(location, doc.rootElement);
}