writeFile method

Future<void> writeFile(
  1. int deviceId,
  2. int fileInstance,
  3. List<int> data, {
  4. bool truncate = false,
  5. int? chunkSize,
  6. BacnetFileProgress? onProgress,
  7. Duration? timeout,
  8. bool background = false,
  9. BacnetCancelToken? cancelToken,
})

Writes data to File object fileInstance from position 0 (stream access), in chunks that fit into one APDU of the device.

With truncate File_Size is set to the length of data afterwards, removing a longer previous content (the device must allow writing File_Size).

Implementation

Future<void> writeFile(
  int deviceId,
  int fileInstance,
  List<int> data, {
  bool truncate = false,
  int? chunkSize,
  BacnetFileProgress? onProgress,
  Duration? timeout,
  bool background = false,
  BacnetCancelToken? cancelToken,
}) async {
  // binds the device and checks that the file exists
  await _fileSize(deviceId, fileInstance, timeout);
  final size = chunkSize ?? await _chunkSize(deviceId);
  final bytes = Uint8List.fromList(data);
  var done = 0;
  onProgress?.call(0, bytes.length);
  while (done < bytes.length) {
    final end = done + size < bytes.length ? done + size : bytes.length;
    await writeFileStream(
      deviceId,
      fileInstance,
      Uint8List.sublistView(bytes, done, end),
      start: done,
      timeout: timeout,
      background: background,
      cancelToken: cancelToken,
    );
    done = end;
    onProgress?.call(done, bytes.length);
  }
  if (truncate) {
    await writeProperty(
      deviceId,
      BacnetObjectType.file,
      fileInstance,
      BacnetPropertyId.fileSize,
      BacnetUnsigned(bytes.length),
      timeout: timeout,
    );
  }
}