1. Define the data
Start with the answer, not the Bluetooth. One sip is: how hot, how much.
/// One sip from the smart mug.@JsonSerializable(includeIfNull: false)class MugSip extends Data { /// How hot the coffee was, in degrees Celsius. double temperature;
/// How much was drunk, in milliliters. int volume;
MugSip({required this.temperature, required this.volume});
@override String get jsonType => MugSamplingPackage.SIP;
@override Function get fromJsonFunction => _$MugSipFromJson; factory MugSip.fromJson(Map<String, dynamic> json) => FromJsonFactory().fromJson(json) as MugSip; @override Map<String, dynamic> toJson() => _$MugSipToJson(this);}What each line is for:
extends Data: everything a probe measures is aDataobject. It ends up inside aMeasurement.@JsonSerializable: the data is written to JSON (JSON serialization).build_runnergenerates_$MugSipFromJsonand_$MugSipToJson.jsonType: the name of the measure type,dk.cachet.carp.mug.sip. It becomes__typein the JSON and is the same string a protocol uses inMeasure(type: ...).fromJsonFunctionandfromJson: how CAMS creates aMugSipfrom JSON. It finds it throughFromJsonFactory, where you register it in step 4.
What a stored sip looks like
Section titled “What a stored sip looks like”{ "sensorStartTime": 1790788396315955, "data": { "__type": "dk.cachet.carp.mug.sip", "temperature": 61.0, "volume": 25 }}sensorStartTime is microseconds since epoch (UTC). The data part is your class.
Generate the JSON code
Section titled “Generate the JSON code”dart run build_runner build