fetchAllType method

Future<void> fetchAllType(
  1. ResourceType type
)

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

Implementation

Future<void> fetchAllType(ResourceType type) async {
  // Eliminate the possibility of duplicates.
  List<Resource>? resourceList = _getListType(type);
  if (resourceList == null) return;
  resourceList.clear();

  // Go through each bridge.
  for (Bridge bridge in bridges) {
    Map<String, dynamic>? dataMap = await HueHttpRepo.get(
      bridgeIpAddr: bridge.ipAddress!,
      applicationKey: bridge.applicationKey!,
      resourceType: type,
    );

    // This would mean there was an error in the GET request.
    if (dataMap == null) continue;

    /// Abbreviated raw data maps for all of the resources of the current
    /// type.
    List<Map<String, dynamic>>? abbrDataList =
        MiscTools.extractDataList(dataMap);

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

    List<String?> ids = abbrDataList
        .map((rawMap) => rawMap[ApiFields.id] as String?)
        .toList();

    for (String? id in ids) {
      if (id == null) continue;

      Map<String, dynamic>? data = await HueHttpRepo.get(
        bridgeIpAddr: bridge.ipAddress!,
        applicationKey: bridge.applicationKey!,
        resourceType: type,
        pathToResource: id,
      );

      if (data == null) continue;

      switch (type) {
        case ResourceType.device:
          _devices.add(Device.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.bridgeHome:
          _bridgeHomes.add(BridgeHome.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.room:
          _rooms.add(Room.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.zone:
          _zones.add(Zone.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.light:
          _lights.add(Light.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.button:
          _buttons.add(Button.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.relativeRotary:
          _relativeRotaries.add(RelativeRotary.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.temperature:
          _temperatures.add(Temperature.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.lightLevel:
          _lightLevels.add(LightLevel.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.motion:
          _motions.add(Motion.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.entertainment:
          _entertainments.add(Entertainment.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.groupedLight:
          _groupedLights.add(GroupedLight.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.devicePower:
          _devicePowers.add(DevicePower.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.zigbeeConnectivity:
          _zigbeeConnectivities.add(ZigbeeConnectivity.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.zgpConnectivity:
          _zgpConnectivities.add(ZgpConnectivity.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.zigbeeDeviceDiscovery:
          _zigbeeDeviceDiscoveries.add(ZigbeeDeviceDiscovery.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.homekit:
          _homekits.add(Homekit.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.matter:
          _matters.add(Matter.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.matterFabric:
          _matterFabrics.add(MatterFabric.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.scene:
          _scenes.add(Scene.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.entertainmentConfiguration:
          _entertainmentConfigurations
              .add(EntertainmentConfiguration.fromJson(data)
                ..bridge = bridge
                ..hueNetwork = this);
          break;
        case ResourceType.behaviorScript:
          _behaviorScripts.add(BehaviorScript.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.behaviorInstance:
          _behaviorInstances.add(BehaviorInstance.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.geofenceClient:
          _geofenceClients.add(GeofenceClient.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.geolocation:
          _geolocations.add(Geolocation.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        case ResourceType.smartScene:
          _smartScenes.add(SmartScene.fromJson(data)
            ..bridge = bridge
            ..hueNetwork = this);
          break;
        default:
        // Do nothing
      }
    }
  }
}