feat: add tag picker in sensor calibration flow

This commit is contained in:
2026-05-21 21:46:08 +02:00
parent cf3019f484
commit 12772bcd8f
6 changed files with 260 additions and 11 deletions
+39 -4
View File
@@ -25,22 +25,22 @@ class CalibrationNotifier extends StateNotifier<CalibrationState> {
/// Channel subscription is set up first to guarantee no reading events are
/// dropped before the server acknowledges the stage start.
Future<void> beginCalibration() async {
// Subscribe first, then call REST.
_sub = repo
.calibrationEvents(sensorDeviceId)
.listen(_onEvent, onError: _onError);
try {
final samplesNeeded = await repo.beginCalibration(sensorId);
final firstUncompleted = calibrationDistances.first;
state = state.copyWith(
phase: CalibrationPhase.ready,
phase: CalibrationPhase.selectingTag,
samplesNeeded: samplesNeeded,
samplesCollected: 0,
selectedDistance: firstUncompleted,
selectedDistance: calibrationDistances.first,
completedDistances: {},
waveform: [],
clearLatestRssi: true,
clearTagRssiReadings: true,
clearSelectedTagId: true,
);
} catch (e) {
_sub?.cancel();
@@ -52,6 +52,28 @@ class CalibrationNotifier extends StateNotifier<CalibrationState> {
}
}
void selectTag(String tagId) {
if (state.phase != CalibrationPhase.selectingTag) return;
state = state.copyWith(selectedTagId: tagId);
}
Future<void> commitTag() async {
final tagId = state.selectedTagId;
if (tagId == null || state.phase != CalibrationPhase.selectingTag) return;
try {
final samplesNeeded = await repo.setCalibrationTag(sensorId, tagId);
state = state.copyWith(
phase: CalibrationPhase.ready,
samplesNeeded: samplesNeeded,
);
} catch (e) {
state = state.copyWith(
phase: CalibrationPhase.error,
error: e.toString(),
);
}
}
void selectDistance(double distance) {
if (state.phase != CalibrationPhase.ready) return;
if (state.completedDistances.contains(distance)) return;
@@ -111,6 +133,11 @@ class CalibrationNotifier extends StateNotifier<CalibrationState> {
void _onEvent(({String event, Map<String, dynamic> payload}) msg) {
switch (msg.event) {
case 'scan_reading':
_handleScanReading(msg.payload);
case 'tag_set':
final tagId = msg.payload['tag_id'] as String;
state = state.copyWith(selectedTagId: tagId);
case 'reading':
_handleReading(msg.payload);
case 'stage_complete':
@@ -122,6 +149,14 @@ class CalibrationNotifier extends StateNotifier<CalibrationState> {
}
}
void _handleScanReading(Map<String, dynamic> payload) {
final tagId = payload['tag_id'] as String;
final rssi = (payload['rssi'] as num).toDouble();
state = state.copyWith(
tagRssiReadings: {...state.tagRssiReadings, tagId: rssi},
);
}
void _handleReading(Map<String, dynamic> payload) {
final rssi = (payload['rssi'] as num).toDouble();
final progress = payload['stage_progress'] as Map<String, dynamic>;