feat: add tag picker in sensor calibration flow
This commit is contained in:
@@ -80,6 +80,12 @@ class PhoenixSensorRepository implements SensorRepository {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<int> setCalibrationTag(int id, String tagId) async {
|
||||||
|
final json = await client.setCalibrationTag(id, tagId);
|
||||||
|
return json['samples_needed'] as int;
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> cancelCalibration(int id) => client.cancelCalibration(id);
|
Future<void> cancelCalibration(int id) => client.cancelCalibration(id);
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,9 @@ abstract class SensorRepository {
|
|||||||
/// result. Returns the fitted (rssiRef, pathLossExp) pair.
|
/// result. Returns the fitted (rssiRef, pathLossExp) pair.
|
||||||
Future<({double rssiRef, double pathLossExp})> finishCalibration(int id);
|
Future<({double rssiRef, double pathLossExp})> finishCalibration(int id);
|
||||||
|
|
||||||
|
/// Commits a specific tag for calibration stages. Returns samples_needed.
|
||||||
|
Future<int> setCalibrationTag(int id, String tagId);
|
||||||
|
|
||||||
/// Aborts calibration and discards all accumulated stage data.
|
/// Aborts calibration and discards all accumulated stage data.
|
||||||
Future<void> cancelCalibration(int id);
|
Future<void> cancelCalibration(int id);
|
||||||
|
|
||||||
|
|||||||
@@ -52,6 +52,12 @@ class SensorClient extends LocaliserdClient {
|
|||||||
'/api/sensors/$id/calibration/finish',
|
'/api/sensors/$id/calibration/finish',
|
||||||
) as Map<String, dynamic>;
|
) as Map<String, dynamic>;
|
||||||
|
|
||||||
|
Future<Map<String, dynamic>> setCalibrationTag(int id, String tagId) async =>
|
||||||
|
await post('/api/sensors/$id/calibration/tag', {
|
||||||
|
'tag_id': tagId,
|
||||||
|
})
|
||||||
|
as Map<String, dynamic>;
|
||||||
|
|
||||||
Future<void> cancelCalibration(int id) =>
|
Future<void> cancelCalibration(int id) =>
|
||||||
delete('/api/sensors/$id/calibration');
|
delete('/api/sensors/$id/calibration');
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
enum CalibrationPhase { idle, ready, collecting, done, error }
|
enum CalibrationPhase { idle, selectingTag, ready, collecting, done, error }
|
||||||
|
|
||||||
const calibrationDistances = [0.5, 1.0, 2.0, 3.0];
|
const calibrationDistances = [0.5, 1.0, 2.0, 3.0];
|
||||||
|
|
||||||
@@ -14,6 +14,8 @@ class CalibrationState {
|
|||||||
this.avgRssi,
|
this.avgRssi,
|
||||||
this.resultRssiRef,
|
this.resultRssiRef,
|
||||||
this.resultPathLossExp,
|
this.resultPathLossExp,
|
||||||
|
this.tagRssiReadings = const {},
|
||||||
|
this.selectedTagId,
|
||||||
this.error,
|
this.error,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -28,6 +30,10 @@ class CalibrationState {
|
|||||||
final double? avgRssi;
|
final double? avgRssi;
|
||||||
final double? resultRssiRef;
|
final double? resultRssiRef;
|
||||||
final double? resultPathLossExp;
|
final double? resultPathLossExp;
|
||||||
|
/// Latest scan RSSI per tag device ID, updated during selectingTag phase.
|
||||||
|
final Map<String, double> tagRssiReadings;
|
||||||
|
/// The tag device ID committed for this calibration session.
|
||||||
|
final String? selectedTagId;
|
||||||
final String? error;
|
final String? error;
|
||||||
|
|
||||||
bool get canFinish =>
|
bool get canFinish =>
|
||||||
@@ -47,10 +53,14 @@ class CalibrationState {
|
|||||||
double? avgRssi,
|
double? avgRssi,
|
||||||
double? resultRssiRef,
|
double? resultRssiRef,
|
||||||
double? resultPathLossExp,
|
double? resultPathLossExp,
|
||||||
|
Map<String, double>? tagRssiReadings,
|
||||||
|
String? selectedTagId,
|
||||||
String? error,
|
String? error,
|
||||||
bool clearSelectedDistance = false,
|
bool clearSelectedDistance = false,
|
||||||
bool clearLatestRssi = false,
|
bool clearLatestRssi = false,
|
||||||
bool clearAvgRssi = false,
|
bool clearAvgRssi = false,
|
||||||
|
bool clearTagRssiReadings = false,
|
||||||
|
bool clearSelectedTagId = false,
|
||||||
bool clearError = false,
|
bool clearError = false,
|
||||||
}) =>
|
}) =>
|
||||||
CalibrationState(
|
CalibrationState(
|
||||||
@@ -66,6 +76,11 @@ class CalibrationState {
|
|||||||
avgRssi: clearAvgRssi ? null : avgRssi ?? this.avgRssi,
|
avgRssi: clearAvgRssi ? null : avgRssi ?? this.avgRssi,
|
||||||
resultRssiRef: resultRssiRef ?? this.resultRssiRef,
|
resultRssiRef: resultRssiRef ?? this.resultRssiRef,
|
||||||
resultPathLossExp: resultPathLossExp ?? this.resultPathLossExp,
|
resultPathLossExp: resultPathLossExp ?? this.resultPathLossExp,
|
||||||
|
tagRssiReadings: clearTagRssiReadings
|
||||||
|
? const {}
|
||||||
|
: tagRssiReadings ?? this.tagRssiReadings,
|
||||||
|
selectedTagId:
|
||||||
|
clearSelectedTagId ? null : selectedTagId ?? this.selectedTagId,
|
||||||
error: clearError ? null : error ?? this.error,
|
error: clearError ? null : error ?? this.error,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,22 +25,22 @@ class CalibrationNotifier extends StateNotifier<CalibrationState> {
|
|||||||
/// Channel subscription is set up first to guarantee no reading events are
|
/// Channel subscription is set up first to guarantee no reading events are
|
||||||
/// dropped before the server acknowledges the stage start.
|
/// dropped before the server acknowledges the stage start.
|
||||||
Future<void> beginCalibration() async {
|
Future<void> beginCalibration() async {
|
||||||
// Subscribe first, then call REST.
|
|
||||||
_sub = repo
|
_sub = repo
|
||||||
.calibrationEvents(sensorDeviceId)
|
.calibrationEvents(sensorDeviceId)
|
||||||
.listen(_onEvent, onError: _onError);
|
.listen(_onEvent, onError: _onError);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final samplesNeeded = await repo.beginCalibration(sensorId);
|
final samplesNeeded = await repo.beginCalibration(sensorId);
|
||||||
final firstUncompleted = calibrationDistances.first;
|
|
||||||
state = state.copyWith(
|
state = state.copyWith(
|
||||||
phase: CalibrationPhase.ready,
|
phase: CalibrationPhase.selectingTag,
|
||||||
samplesNeeded: samplesNeeded,
|
samplesNeeded: samplesNeeded,
|
||||||
samplesCollected: 0,
|
samplesCollected: 0,
|
||||||
selectedDistance: firstUncompleted,
|
selectedDistance: calibrationDistances.first,
|
||||||
completedDistances: {},
|
completedDistances: {},
|
||||||
waveform: [],
|
waveform: [],
|
||||||
clearLatestRssi: true,
|
clearLatestRssi: true,
|
||||||
|
clearTagRssiReadings: true,
|
||||||
|
clearSelectedTagId: true,
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
_sub?.cancel();
|
_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) {
|
void selectDistance(double distance) {
|
||||||
if (state.phase != CalibrationPhase.ready) return;
|
if (state.phase != CalibrationPhase.ready) return;
|
||||||
if (state.completedDistances.contains(distance)) 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) {
|
void _onEvent(({String event, Map<String, dynamic> payload}) msg) {
|
||||||
switch (msg.event) {
|
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':
|
case 'reading':
|
||||||
_handleReading(msg.payload);
|
_handleReading(msg.payload);
|
||||||
case 'stage_complete':
|
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) {
|
void _handleReading(Map<String, dynamic> payload) {
|
||||||
final rssi = (payload['rssi'] as num).toDouble();
|
final rssi = (payload['rssi'] as num).toDouble();
|
||||||
final progress = payload['stage_progress'] as Map<String, dynamic>;
|
final progress = payload['stage_progress'] as Map<String, dynamic>;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import 'package:flutter/services.dart';
|
|||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
|
||||||
import '../../domain/models/calibration.dart';
|
import '../../domain/models/calibration.dart';
|
||||||
|
import '../../domain/models/tag.dart';
|
||||||
import '../../providers.dart';
|
import '../../providers.dart';
|
||||||
|
|
||||||
void showCalibrationSheet(
|
void showCalibrationSheet(
|
||||||
@@ -71,11 +72,22 @@ class _CalibrationSheetState extends ConsumerState<CalibrationSheet> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _commitTag() async {
|
||||||
|
await ref.read(calibrationProvider(_key).notifier).commitTag();
|
||||||
|
if (mounted) {
|
||||||
|
_pageController.animateToPage(
|
||||||
|
2,
|
||||||
|
duration: const Duration(milliseconds: 300),
|
||||||
|
curve: Curves.easeInOut,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _finish() async {
|
Future<void> _finish() async {
|
||||||
await ref.read(calibrationProvider(_key).notifier).finishCalibration();
|
await ref.read(calibrationProvider(_key).notifier).finishCalibration();
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
_pageController.animateToPage(
|
_pageController.animateToPage(
|
||||||
2,
|
3,
|
||||||
duration: const Duration(milliseconds: 300),
|
duration: const Duration(milliseconds: 300),
|
||||||
curve: Curves.easeInOut,
|
curve: Curves.easeInOut,
|
||||||
);
|
);
|
||||||
@@ -115,6 +127,11 @@ class _CalibrationSheetState extends ConsumerState<CalibrationSheet> {
|
|||||||
onBegin: _begin,
|
onBegin: _begin,
|
||||||
onCancel: () => _cancelAndClose(context),
|
onCancel: () => _cancelAndClose(context),
|
||||||
),
|
),
|
||||||
|
_TagSelectionPage(
|
||||||
|
state: state,
|
||||||
|
sensorKey: _key,
|
||||||
|
onNext: _commitTag,
|
||||||
|
),
|
||||||
_CollectingPage(
|
_CollectingPage(
|
||||||
state: state,
|
state: state,
|
||||||
sensorKey: _key,
|
sensorKey: _key,
|
||||||
@@ -214,7 +231,170 @@ class _IntroPage extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Page 2 — Collecting
|
// Page 2 — Tag selection
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class _TagSelectionPage extends ConsumerWidget {
|
||||||
|
const _TagSelectionPage({
|
||||||
|
required this.state,
|
||||||
|
required this.sensorKey,
|
||||||
|
required this.onNext,
|
||||||
|
});
|
||||||
|
|
||||||
|
final CalibrationState state;
|
||||||
|
final ({int id, String deviceId}) sensorKey;
|
||||||
|
final Future<void> Function() onNext;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
|
final theme = Theme.of(context);
|
||||||
|
final notifier = ref.read(calibrationProvider(sensorKey).notifier);
|
||||||
|
final tagsAsync = ref.watch(tagsProvider);
|
||||||
|
|
||||||
|
final tags = tagsAsync.whenOrNull(data: (t) => t) ?? const [];
|
||||||
|
final readings = state.tagRssiReadings;
|
||||||
|
|
||||||
|
// Sort: tags with readings by RSSI descending (nearest first),
|
||||||
|
// then tags without readings by id.
|
||||||
|
final sorted = [...tags]..sort((a, b) {
|
||||||
|
final ra = readings[a.tagId];
|
||||||
|
final rb = readings[b.tagId];
|
||||||
|
if (ra != null && rb != null) return rb.compareTo(ra);
|
||||||
|
if (ra != null) return -1;
|
||||||
|
if (rb != null) return 1;
|
||||||
|
return a.id.compareTo(b.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
return SafeArea(
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.fromLTRB(24, 16, 24, 24),
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
|
children: [
|
||||||
|
Center(
|
||||||
|
child: Container(
|
||||||
|
width: 32,
|
||||||
|
height: 4,
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: theme.colorScheme.outlineVariant,
|
||||||
|
borderRadius: BorderRadius.circular(2),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 20),
|
||||||
|
const Text(
|
||||||
|
'Select your tag',
|
||||||
|
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500),
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 4),
|
||||||
|
Text(
|
||||||
|
'Hold each tag near the sensor — the one you\'re using will show a stronger signal.',
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 13,
|
||||||
|
color: theme.colorScheme.onSurfaceVariant,
|
||||||
|
),
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
Expanded(
|
||||||
|
child: tags.isEmpty
|
||||||
|
? Center(
|
||||||
|
child: Text(
|
||||||
|
'No tags enrolled',
|
||||||
|
style: TextStyle(
|
||||||
|
color: theme.colorScheme.onSurfaceVariant),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
: ListView.builder(
|
||||||
|
itemCount: sorted.length,
|
||||||
|
itemBuilder: (context, i) {
|
||||||
|
final tag = sorted[i];
|
||||||
|
final rssi = readings[tag.tagId];
|
||||||
|
final selected = tag.tagId == state.selectedTagId;
|
||||||
|
return _TagListTile(
|
||||||
|
key: ValueKey(tag.tagId),
|
||||||
|
tag: tag,
|
||||||
|
rssi: rssi,
|
||||||
|
selected: selected,
|
||||||
|
onTap: () => notifier.selectTag(tag.tagId),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
_AsyncButton(
|
||||||
|
onPressed: onNext,
|
||||||
|
enabled: state.selectedTagId != null,
|
||||||
|
label: 'Next',
|
||||||
|
icon: Icons.arrow_forward,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _TagListTile extends StatelessWidget {
|
||||||
|
const _TagListTile({
|
||||||
|
super.key,
|
||||||
|
required this.tag,
|
||||||
|
required this.rssi,
|
||||||
|
required this.selected,
|
||||||
|
required this.onTap,
|
||||||
|
});
|
||||||
|
|
||||||
|
final Tag tag;
|
||||||
|
final double? rssi;
|
||||||
|
final bool selected;
|
||||||
|
final VoidCallback onTap;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final cs = Theme.of(context).colorScheme;
|
||||||
|
return ListTile(
|
||||||
|
selected: selected,
|
||||||
|
selectedTileColor: cs.primaryContainer,
|
||||||
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||||
|
leading: Icon(
|
||||||
|
selected ? Icons.radio_button_checked : Icons.radio_button_unchecked,
|
||||||
|
color: selected ? cs.primary : cs.outline,
|
||||||
|
),
|
||||||
|
title: Text(tag.name),
|
||||||
|
subtitle: Text(
|
||||||
|
tag.tagId,
|
||||||
|
style: TextStyle(fontSize: 12, color: cs.onSurfaceVariant),
|
||||||
|
),
|
||||||
|
trailing: rssi != null
|
||||||
|
? TweenAnimationBuilder<double>(
|
||||||
|
tween: Tween(begin: rssi, end: rssi),
|
||||||
|
duration: const Duration(milliseconds: 300),
|
||||||
|
builder: (context, value, child) => Chip(
|
||||||
|
label: Text(
|
||||||
|
'${value.round()} dBm',
|
||||||
|
style: const TextStyle(fontFamily: 'monospace', fontSize: 12),
|
||||||
|
),
|
||||||
|
backgroundColor: cs.primaryContainer,
|
||||||
|
side: BorderSide.none,
|
||||||
|
padding: const EdgeInsets.symmetric(horizontal: 4),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
: Chip(
|
||||||
|
label: Text(
|
||||||
|
'—',
|
||||||
|
style: TextStyle(fontSize: 12, color: cs.onSurfaceVariant),
|
||||||
|
),
|
||||||
|
side: BorderSide.none,
|
||||||
|
padding: const EdgeInsets.symmetric(horizontal: 4),
|
||||||
|
),
|
||||||
|
onTap: onTap,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Page 3 — Collecting
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
class _CollectingPage extends ConsumerWidget {
|
class _CollectingPage extends ConsumerWidget {
|
||||||
@@ -772,12 +952,14 @@ class _AsyncButton extends StatefulWidget {
|
|||||||
required this.label,
|
required this.label,
|
||||||
this.icon,
|
this.icon,
|
||||||
this.compact = false,
|
this.compact = false,
|
||||||
|
this.enabled = true,
|
||||||
});
|
});
|
||||||
|
|
||||||
final Future<void> Function() onPressed;
|
final Future<void> Function() onPressed;
|
||||||
final String label;
|
final String label;
|
||||||
final IconData? icon;
|
final IconData? icon;
|
||||||
final bool compact;
|
final bool compact;
|
||||||
|
final bool enabled;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<_AsyncButton> createState() => _AsyncButtonState();
|
State<_AsyncButton> createState() => _AsyncButtonState();
|
||||||
@@ -806,12 +988,14 @@ class _AsyncButtonState extends State<_AsyncButton> {
|
|||||||
Icon(widget.icon, size: 18),
|
Icon(widget.icon, size: 18),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
: Text(widget.label);
|
: Text(widget.label, style: TextStyle(fontSize: 36, color: Theme.of(context).colorScheme.primary));
|
||||||
|
|
||||||
|
final canPress = !_loading && widget.enabled;
|
||||||
|
|
||||||
if (widget.compact) {
|
if (widget.compact) {
|
||||||
return TextButton(
|
return TextButton(
|
||||||
style: TextButton.styleFrom(shape: shape, textStyle: const .new(fontSize: 32)),
|
style: TextButton.styleFrom(shape: shape, textStyle: const TextStyle(fontSize: 36, color: Colors.black)),
|
||||||
onPressed: _loading ? null : _run,
|
onPressed: canPress ? _run : null,
|
||||||
child: child,
|
child: child,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -821,7 +1005,7 @@ class _AsyncButtonState extends State<_AsyncButton> {
|
|||||||
shape: shape,
|
shape: shape,
|
||||||
minimumSize: const Size.fromHeight(48),
|
minimumSize: const Size.fromHeight(48),
|
||||||
),
|
),
|
||||||
onPressed: _loading ? null : _run,
|
onPressed: canPress ? _run : null,
|
||||||
child: child,
|
child: child,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user