Extending CAMS
Extend CARP Mobile Sensing with custom triggers, sampling packages, data managers and data or privacy transformers.
CARP Mobile Sensing (CAMS) is designed to be extended with domain-specific studies, triggers, tasks, measures, probes and data types. You extend it by implementing classes that inherit from the abstract classes in the library and registering them with CAMS.
| Extension | What you implement | Where you register it |
|---|---|---|
| Trigger | TriggerConfiguration, TriggerExecutor, TriggerFactory | ExecutorFactory().registerTriggerFactory() |
| Sampling package | SamplingPackage, Data, Probe, optionally DeviceManager | SamplingPackageRegistry().register() |
| Data manager | DataEndPoint, DataManager, DataManagerFactory | DataManagerRegistry().register() |
| Data transformer | DataTransformerSchema, DataTransformer functions | DataTransformerSchemaRegistry().register() |
| Privacy transformer | Transformer functions that privacy-protect Data properties | The PrivacySchema.DEFAULT schema in the DataTransformerSchemaRegistry |
| User task | UserTask, UserTaskFactory | AppTaskController().registerUserTaskFactory() |
JSON serialization
Most extensions rely on serialization to and from JSON. This applies to triggers, measures, sampling schemas, data and more. CAMS uses polymorphic serialization as supported by the carp_serializable package, an extension of the standard json_serializable package.
Polymorphic JSON serialization requires four steps when you design your classes:
Define a serializable class
Extend from
Serializableand annotate the class:Implement the JSON methods
Implement the three JSON serialization methods:
Register the class
Register the class's
fromJsonFunctionin theFromJsonFactory, for example in theonRegister()callback of a sampling package:Run the code generator
When all JSON classes are implemented, generate the JSON serialization helper classes using
build_runner. Run this command in the root of your Flutter project:This generates the necessary
.g.dartfiles.
For details, see the carp_serializable and json_serializable packages.
Adding new triggers
Triggers are a central part of a StudyProtocol, and you can create your own triggers and add them to CAMS:
- Define one or more new triggers.
- Define a
TriggerExecutorfor each new trigger. - Define and register a
TriggerFactorythat creates the correctTriggerExecutorfor a specific trigger.
Define a trigger
A trigger extends the TriggerConfiguration class and adds the domain-specific fields that configure it. The RemoteTrigger below listens to a resource on a server identified by a URI and triggers when this resource is available.
Trigger configurations are part of a study protocol, so they must support JSON serialization.
Define a trigger executor
Each trigger needs a corresponding TriggerExecutor that executes the trigger at sampling runtime.
- Every
Executorin CAMS can implement runtime behavior on init, start, stop, restart and dispose. - The abstract
TriggerExecutorclass has default implementations of all these methods, so you only override the lifecycle methods where something needs to happen. - Typically you override
onStart(), which is called when sensing starts, and implement the trigger logic there. Call theonTrigger()callback to fire the trigger.
The RemoteTriggerExecutor starts a periodic timer that checks the resource at the URI in the trigger configuration. If the resource is available, it calls onTrigger().
Warning
This trigger executor is very simplistic and does not cover any edge cases, such as exceptions from network errors.
Define and register a trigger factory
A TriggerFactory maps triggers to their executors at runtime.
- A factory can handle multiple triggers, as listed in the set of trigger
typesit supports. - The
create()method creates the correct trigger executor for the specified trigger. - The
onRegister()callback adds the trigger configurations to theFromJsonFactory(), so trigger configurations in a study protocol can be serialized to and from JSON.
Register the trigger factory with CAMS:
Do this when you initialize CAMS, before any protocol is created or loaded.
Adding new sampling capabilities
A SamplingPackage adds measures to CAMS. It defines the measure types and their data classes and creates a probe for each type. For an external device or an online service, it also provides the device manager. Building sampling packages walks through every part with a complete, tested example package:
Building sampling packages
Build, test and publish a sampling package of your own.
Devices and services
Add a device manager for a wearable or an online service.
You can release a sampling package as a Dart package on pub.dev. The existing sampling packages cover onboard phone sensors, external wearable devices and online services; see Measure types.
Adding a new data manager
CAMS comes with a set of built-in and external data managers. See Data managers for an overview.
To save or upload data to a custom data backend, implement three interfaces:
Create a data endpoint
Implement the
DataEndPointinterface, which is used in the study protocol.Create a data manager
Implement the
DataManagerinterface, which saves or uploads measurements as they are sampled.Create a data manager factory
Create a
DataManagerFactorythat creates a data manager based on the data endpoint configuration.
Data endpoint
A data endpoint is part of the study protocol. It specifies which data manager stores or forwards data, and how that data manager is configured. Every new data manager needs a corresponding data endpoint that extends the DataEndPoint class.
For example, the FileDataEndPoint specifies that data is saved to a file with the FileDataManager. It configures the data manager with a buffer size and whether the file is zipped or encrypted.
Warning
A DataEndPoint must support JSON serialization to be part of a study protocol.
Data manager
A data manager stores or forwards the collected measurements. Every new data manager implements the DataManager interface:
All of these methods must be implemented. The AbstractDataManager class is a useful starting point. For example, the ConsoleDataManager is a minimal data manager that prints a JSON-encoded version of the data to the console:
Data manager factory
Each new data manager needs a corresponding DataManagerFactory, which creates a data manager of a specific type when the study protocol is loaded. The interface:
The factory for the ConsoleDataManager:
Register the factory in the DataManagerRegistry singleton:
Do this at app start-up, before the protocol is loaded and sensing is started.
Adding data and privacy transformers
For how to use transformers and privacy schemas in a study protocol, see Data transformation and privacy.
Creating data transformer schemas
To create a DataTransformerSchema, implement the namespace getter and the onRegister() callback. This is the Open mHealth transformer schema:
Register each transformer schema in the DataTransformerSchemaRegistry singleton as part of your app setup:
Once the schema is registered, you can create transformers for each data type. A data transformer transforms one type of data into another, as defined by the DataTransformer typedef:
For each Data type you want to transform, define a new class that also extends Data. For example, the OMHGeopositionDataPoint class represents an OMH Geoposition data point:
- The most important member is
transformer, a function that transforms one type ofDatainto another. The mapping between the two data types happens in thefromLocationDatafactory. - The class must support JSON serialization. Here the superclass
OMHContextDataPointhandles it. ThetoJsonmethod is needed to serialize and store the data. - The namespace of the
DataTypeofOMHGeopositionDataPointisOMH.
Add the transformer to the schema:
In carp_context_package, this happens in the onRegister() method of the ContextSamplingPackage.
Using data transformer schemas
You can transform data manually by looking up a transformer schema and applying its transformer. This code transforms a Location into an OMHGeopositionDatum:
More commonly, you set the data format of the DataEndPoint with its dataFormat property, which specifies the namespace, such as NameSpace.OMH. This data endpoint saves measurements to a file in the Open mHealth (OMH) data format:
Note
Not all data types in CAMS have an OMH data format. If no OMH transformer is found, the data is stored in the original CARP data format.
Adding privacy transformers
A PrivacySchema is a special data transformer schema that takes a piece of data and protects its relevant properties. CAMS has a built-in privacy schema with the transformer namespace PrivacySchema.DEFAULT. To add privacy protection:
- Write a transformer function that transforms a
Dataobject so that its properties are privacy-protected. - Register this function in the
DataTransformerSchemaRegistryas part of thePrivacySchema.DEFAULTschema.
These privacy functions anonymize text messages and phone calls:
You can put these functions anywhere in the sampling package. Typically you put them in a separate file if you have many, or in the package Dart file if you have only a few.
Register the anonymizer functions in the PrivacySchema.DEFAULT schema, typically in the package's onRegister() callback:
This example is implemented in the carp_communication_package.
Tip
Consider whether your sampling package needs to supply privacy-protecting functions, and if so, add them to the default privacy schema.