fetchAll method
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.
token
is the remote access token.
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? token,
}) 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,
token: token,
);
} 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, bridges: bridgesWithResourceType, token: token);
}
}