createObject method

  1. @override
Future<BacnetObject> createObject(
  1. int deviceId, {
  2. BacnetObjectType? type,
  3. BacnetObject? object,
  4. List<BacnetPropertyValue> initialValues = const [],
  5. Duration? timeout,
})
override

Creates an object in deviceId (CreateObject) and returns it: an object of type whose instance the device chooses, or object.

initialValues are written while creating the object. When the device rejects one, the BacnetProtocolException names its position in BacnetProtocolException.firstFailedElement (1 based).

final setpoint = await client.createObject(1234,
    type: BacnetObjectType.analogValue,
    initialValues: const [
      BacnetPropertyValue(
        propertyIdentifier: BacnetPropertyId.objectName,
        value: BacnetCharacterString('Setpoint'),
      ),
    ]);

Implementation

@override
Future<BacnetObject> createObject(
  int deviceId, {
  BacnetObjectType? type,
  BacnetObject? object,
  List<BacnetPropertyValue> initialValues = const [],
  Duration? timeout,
}) => Future.sync(() {
  if ((type == null) == (object == null)) {
    throw ArgumentError('give either type or object');
  }
  requests.add(
    FakeBacnetRequest('createObject', deviceId: deviceId, object: object),
  );
  return _request(BacnetConfirmedService.createObject, deviceId, null, (
    device,
  ) {
    final objectType = object?.type ?? type!;
    if (objectType == BacnetObjectType.device) {
      throw device._error(
        BacnetErrorClass.object,
        BacnetErrorCode.dynamicCreationNotSupported,
        firstFailedElement: 0,
      );
    }
    final instance = object?.instance ?? device._freeInstance(objectType);
    if (device.object(objectType, instance) != null) {
      throw device._error(
        BacnetErrorClass.object,
        BacnetErrorCode.objectIdentifierAlreadyExists,
        firstFailedElement: 0,
      );
    }
    for (final (index, value) in initialValues.indexed) {
      if (value.propertyIdentifier == BacnetPropertyId.objectIdentifier ||
          value.propertyIdentifier == BacnetPropertyId.objectType) {
        throw device._error(
          BacnetErrorClass.property,
          BacnetErrorCode.writeAccessDenied,
          firstFailedElement: index + 1,
        );
      }
    }
    final created = device.addObject(objectType, instance);
    for (final value in initialValues) {
      created._write(value.propertyIdentifier, value.value, value.priority);
    }
    return created.identifier;
  });
});