4. Write the SamplingPackage
The data, probe and device manager are useless until CAMS can find them. The SamplingPackage is the table of contents:
class MugSamplingPackage implements SamplingPackage { static const String MUG_NAMESPACE = '${NameSpace.CARP}.mug'; static const String SIP = '$MUG_NAMESPACE.sip';
final DeviceManager _deviceManager = MugDeviceManager(MugDevice.DEVICE_TYPE);
@override DataTypeSamplingSchemeMap get samplingSchemes => DataTypeSamplingSchemeMap.from([ DataTypeSamplingScheme( DataTypeMetaData( type: SIP, displayName: 'Coffee sips', timeType: DataTimeType.POINT, ), ), ]);
@override List<DataTypeMetaData> get dataTypes => samplingSchemes.dataTypes;
@override Probe? create(String type) => switch (type) { SIP => SipProbe(), _ => null, };
@override void onRegister() { FromJsonFactory().registerAll([ MugDevice(), BLEDeviceRegistration(bleAddress: ''), MugSip(temperature: 0, volume: 0), ]); }
@override String get deviceType => MugDevice.DEVICE_TYPE;
@override DeviceManager get deviceManager => _deviceManager;}| Member | Used by CAMS for |
|---|---|
SIP |
The measure type string. A protocol says Measure(type: MugSamplingPackage.SIP). |
samplingSchemes / dataTypes |
The list of measure types this package offers, with a display name, and whether it is a point in time or a time span. |
create(type) |
Makes a probe for a measure type. Return null for types the package does not own. |
onRegister() |
Runs once on registration. Register every class that is read from JSON: the device, the registration and the data. |
deviceType / deviceManager |
Which DeviceConfiguration type this package handles, and the manager that connects to it. |
If you forget a class in onRegister(), the protocol serializes fine but fails when it is read back, because FromJsonFactory does not know the __type.
Sampling configurations (optional)
Section titled “Sampling configurations (optional)”A measure can carry a sampling configuration: how often or how long to sample. Give a default in samplingSchemes, and a protocol can override it per measure with overrideSamplingConfiguration.
| Configuration | Meaning |
|---|---|
IntervalSamplingConfiguration |
Wait interval between measurements. |
PeriodicSamplingConfiguration |
Sample for duration every interval. |
PersistentSamplingConfiguration |
Keeps its state across app restarts. |
HistoricSamplingConfiguration |
Collects past data. |
The mug pushes sips, so it needs none. A package that polled the mug’s temperature would add an IntervalSamplingConfiguration and read it in an IntervalProbe.