FileDataManager
A data manager that writes Measurements as JSON to files on the phone.
Used when the protocol's data endpoint is a FileDataEndPoint. Each file holds a JSON array of measurements. When a file grows beyond FileDataEndPoint.bufferSize bytes, it is closed, optionally zipped, and a new file is started.
Key points:
- Emits FileDataManagerEvents on events when files are created and closed, so e.g. an upload manager can pick up closed files.
- Encryption (FileDataEndPoint.encrypt) is not implemented yet. Only the FileDataManagerEventTypes.fileEncrypted event is emitted.
- If a write fails, the file is reset and the write is retried.
The path and filename format is
~/carp/deployments/<study_deployment_id>/data/carp-data-yyyy-mm-dd-hh-mm-ss-ms.json.zip
where ~ is the folder where an application can place files that are private to the application (see Settings.getDataBasePath).
On iOS, this is the NSDocumentsDirectory and the files can be accessed via the MacOS Finder.
On Android, Flutter files are stored in the AppData directory, which is located in the data/data/<package_name>/app_flutter folder. Files can be accessed via AndroidStudio.
- Inheritance
-
- Object
- AbstractDataManager
- FileDataManager
Constructors
FileDataManager
Properties
file
The current file being written to.
Created on first access, which also emits a FileDataManagerEventTypes.fileCreated event.
fileDataEndPoint
The dataEndPoint cast to a FileDataEndPoint. Only valid after configure has been called.
filename
Full path and filename of the current file, on the format
~/carp/deployments/<study_deployment_id>/data/carp-data-yyyy-mm-dd-hh-mm-ss-ms.json
where the date is the creation time in UTC (zulu time). A new name is generated each time a file is flushed.
path
The full path where data files are stored on the device.
sink
The IOSink used to append to the current file.
Opened on first access, which also writes the opening [ of the JSON array.
type
The type of this data manager as enumerated in DataEndPointTypes.
Methods
close
Flushes any buffered data and closes this data manager.
After calling close the data manager can no longer be used.
configure
- required DataEndPoint dataEndPoint,
- required SmartphoneDeployment deployment,
- required Stream<
Measurement> measurements,
Configures the data manager with the study deployment, the dataEndPoint, and the stream of measurements to handle.
Call this before any data is handled.
flush
Closes flushFile and its flushSink, and zips the file if FileDataEndPoint.zip is true.
Resets the current file, so the next write starts a new file. Emits a FileDataManagerEventTypes.fileClosed event with the final path (ending in .zip if zipped). Calls for a sink that is already being flushed are ignored.
onDone
When the data stream closes, the onDone handler is called. Default implementation is a no-op function. If another behavior is wanted, implementations of this abstract data manager should handle closing of the data stream.
onMeasurement
- Measurement measurement
On each measurement collected, the onMeasurement handler is called.
Implementations of this interface should handle how to save or upload the measurement.
write
- Measurement measurement
Writes a JSON encoded measurement to the current file.
If the sink is not ready, the write is retried after 2 seconds. Calls flush when the file size exceeds FileDataEndPoint.bufferSize.