fetchAll method

Future<void> fetchAll({
  1. List<Bridge>? bridges,
  2. String decrypter(
    1. String ciphertext
    )?,
})

Fetch all of the Philip's Hue devices on the network that this device has permission to fetch.

bridges If you only want to fetch resources from a specific bridge or bridges, you can provide a list of bridges. If this parameter is null, all bridges will be checked. If any of the bridges in the list are not already associated with this HueNetwork, they will be ignored.

decrypter When the old tokens are read from local storage, they are decrypted. This parameter allows you to provide your own decryption method. This will be used in addition to the default decryption method. This will be performed after the default decryption method.

If this method fails to fetch a resource with an error, the resource will be added to the failedFetches list.

Implementation

Future<void> fetchAll({
  List<Bridge>? bridges,
  String Function(String ciphertext)? decrypter,
}) async {
  final List<Bridge> bridgesToCheck;
  if (bridges == null) {
    bridgesToCheck = this.bridges;
  } else {
    bridgesToCheck = [];

    for (final Bridge bridge in bridges) {
      if (this.bridges.contains(bridge)) {
        bridgesToCheck.add(bridge);
      }
    }
  }

  if (bridgesToCheck.isEmpty) return;

  /// A map of all of the resource types on each bridge.
  ///
  /// No need to make calls for types the bridge doesn't have.
  final Map<Bridge, Set<ResourceType>> resourceTypesByBridge = {};

  for (final Bridge bridge in bridgesToCheck) {
    resourceTypesByBridge[bridge] = {};

    // Fetch all of the resource types on the bridge.
    final Map<String, dynamic>? dataMap;
    try {
      dataMap = await HueHttpRepo.get(
        bridgeIpAddr: bridge.ipAddress!,
        applicationKey: bridge.applicationKey!,
        resourceType: null,
        decrypter: decrypter,
      );
    } catch (e) {
      _addFailedFetch(
        FailedResource(
          id: '',
          type: ResourceType.device,
          bridge: bridge,
          error: ErrorType.unknown,
          additionalInfo: e.toString(),
        ),
      );
      continue;
    }

    if (dataMap == null) continue;

    /// Abbreviated raw data maps for the bridge's resources.
    List<Map<String, dynamic>>? abbrDataList =
        MiscTools.extractDataList(dataMap);

    // This would mean there is no useful data in the list.
    if (abbrDataList == null) continue;

    for (final Map<String, dynamic> rawMap in abbrDataList) {
      final ResourceType type =
          ResourceType.fromString(rawMap[ApiFields.type]);

      resourceTypesByBridge[bridge]!.add(type);
    }
  }

  // Go through all of the resource types on each bridge, and fetch all of
  // the resources of that type on the bridge.
  for (ResourceType type in ResourceType.values) {
    final List<Bridge> bridgesWithResourceType = [];

    for (final Bridge bridge in bridgesToCheck) {
      if (resourceTypesByBridge[bridge]!.contains(type)) {
        bridgesWithResourceType.add(bridge);
      }
    }

    if (bridgesWithResourceType.isEmpty) continue;

    await fetchAllType(type, decrypter: decrypter);
  }
}