readMultiple method

  1. @override
Future<Map<BacnetObject, Map<BacnetPropertyId, BacnetPropertyResult>>> readMultiple(
  1. int deviceId,
  2. List<BacnetReadAccessSpecification> specs, {
  3. Duration? timeout,
  4. bool background = false,
  5. BacnetCancelToken? cancelToken,
})
override

Reads multiple properties of multiple objects with ReadPropertyMultiple.

Returns object → property → result: a BacnetValue, or a BacnetError for a property the device could not return. A property may appear only once per object. Requests whose answer does not fit into one APDU are split automatically.

const sensor = BacnetObject(
  type: BacnetObjectType.analogInput,
  instance: 1,
);
final results = await client.readMultiple(1234, [
  const BacnetReadAccessSpecification(
    objectIdentifier: sensor,
    properties: [
      BacnetPropertyReference(
        propertyIdentifier: BacnetPropertyId.presentValue,
      ),
      BacnetPropertyReference(
        propertyIdentifier: BacnetPropertyId.objectName,
      ),
    ],
  ),
]);
final properties = results[sensor] ?? const {};
print(properties.valueOf(BacnetPropertyId.objectName)?.asString);
switch (properties[BacnetPropertyId.presentValue]) {
  case BacnetReal(:final value):
    print('$value °C');
  case BacnetError(:final errorCode):
    print('not readable: ${errorCode.label}');
  case _:
    print('missing or unexpected');
}

Implementation

@override
Future<Map<BacnetObject, Map<BacnetPropertyId, BacnetPropertyResult>>>
readMultiple(
  int deviceId,
  List<BacnetReadAccessSpecification> specs, {
  Duration? timeout,
  bool background = false,
  BacnetCancelToken? cancelToken,
}) {
  requests.add(FakeBacnetRequest('readMultiple', deviceId: deviceId));
  if (specs.isEmpty) return Future.value(const {});
  checkUniqueProperties(specs);
  return _request(
    BacnetConfirmedService.readPropertyMultiple,
    deviceId,
    cancelToken,
    (device) {
      final result =
          <BacnetObject, Map<BacnetPropertyId, BacnetPropertyResult>>{};
      for (final spec in specs) {
        final object = spec.objectIdentifier;
        final properties = result[object] ??= {};
        for (final reference in spec.properties) {
          final id = reference.propertyIdentifier;
          if (id == BacnetPropertyId.all ||
              id == BacnetPropertyId.required ||
              id == BacnetPropertyId.optional) {
            // every property the object has
            final fake = device.object(object.type, object.instance);
            if (fake == null) {
              properties[id] = const BacnetError(
                BacnetErrorClass.object,
                BacnetErrorCode.unknownObject,
              );
              continue;
            }
            for (final property in [
              ...fake.properties.keys,
              if (object.type == BacnetObjectType.device) ...[
                BacnetPropertyId.objectList,
                BacnetPropertyId.protocolServicesSupported,
              ],
            ]) {
              properties[property] = _readOrError(device, object, property);
            }
            continue;
          }
          properties[id] = _readOrError(device, object, id);
        }
      }
      return result;
    },
  );
}