Skip to content

Commit cd986b0

Browse files
identify sensor streams by wearable and sensor to avoid collisions
1 parent 49694fb commit cd986b0

5 files changed

Lines changed: 46 additions & 10 deletions

File tree

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:collection';
23

34
import 'package:open_earable_flutter/open_earable_flutter.dart';
45

@@ -7,16 +8,38 @@ import 'package:open_earable_flutter/open_earable_flutter.dart';
78
class SensorStreams {
89
SensorStreams._();
910

10-
static final Map<Sensor, Stream<SensorValue>> _sharedStreams = {};
11+
static final Map<String, Map<Sensor, Stream<SensorValue>>>
12+
_sharedStreamsByDevice = {};
13+
static Map<Sensor, Stream<SensorValue>> _createIdentitySensorStreamMap() =>
14+
LinkedHashMap<Sensor, Stream<SensorValue>>.identity();
1115

12-
static Stream<SensorValue> shared(Sensor sensor) {
13-
return _sharedStreams.putIfAbsent(
16+
static Stream<SensorValue> shared({
17+
required Wearable wearable,
18+
required Sensor sensor,
19+
}) {
20+
final deviceStreams = _sharedStreamsByDevice.putIfAbsent(
21+
wearable.deviceId,
22+
// Identity map avoids collisions when Sensor overrides ==/hashCode
23+
// non-uniquely across different devices.
24+
_createIdentitySensorStreamMap,
25+
);
26+
return deviceStreams.putIfAbsent(
1427
sensor,
1528
() => sensor.sensorStream.asBroadcastStream(),
1629
);
1730
}
1831

19-
static void clearForSensor(Sensor sensor) {
20-
_sharedStreams.remove(sensor);
32+
static void clearForSensor({
33+
required Wearable wearable,
34+
required Sensor sensor,
35+
}) {
36+
final deviceStreams = _sharedStreamsByDevice[wearable.deviceId];
37+
if (deviceStreams == null) {
38+
return;
39+
}
40+
deviceStreams.remove(sensor);
41+
if (deviceStreams.isEmpty) {
42+
_sharedStreamsByDevice.remove(wearable.deviceId);
43+
}
2144
}
2245
}

open_wearable/lib/view_models/sensor_data_provider.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:open_earable_flutter/open_earable_flutter.dart';
77
import 'package:open_wearable/models/sensor_streams.dart';
88

99
class SensorDataProvider with ChangeNotifier {
10+
final Wearable wearable;
1011
final Sensor sensor;
1112
final int timeWindow; // seconds
1213

@@ -26,6 +27,7 @@ class SensorDataProvider with ChangeNotifier {
2627
DateTime? _lastSensorArrivalTime;
2728

2829
SensorDataProvider({
30+
required this.wearable,
2931
required this.sensor,
3032
this.timeWindow = 5,
3133
}) {
@@ -52,8 +54,10 @@ class SensorDataProvider with ChangeNotifier {
5254
}
5355

5456
void _listenToStream() {
55-
_sensorStreamSubscription =
56-
SensorStreams.shared(sensor).listen((sensorValue) {
57+
_sensorStreamSubscription = SensorStreams.shared(
58+
wearable: wearable,
59+
sensor: sensor,
60+
).listen((sensorValue) {
5761
sensorValues.add(sensorValue);
5862
_lastSensorTimestamp = sensorValue.timestamp;
5963
_lastSensorArrivalTime = DateTime.now();

open_wearable/lib/view_models/sensor_recorder_provider.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ class SensorRecorderProvider with ChangeNotifier {
148148

149149
File file = await recorder.start(
150150
filepath: filepath,
151-
inputStream: SensorStreams.shared(sensor),
151+
inputStream: SensorStreams.shared(
152+
wearable: wearable,
153+
sensor: sensor,
154+
),
152155
);
153156

154157
logger.i(

open_wearable/lib/widgets/sensors/sensor_page.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,10 @@ class _SensorPageState extends State<SensorPage>
189189
in wearable.requireCapability<SensorManager>().sensors) {
190190
_sensorDataProviders.putIfAbsent(
191191
(wearable, sensor),
192-
() => SensorDataProvider(sensor: sensor),
192+
() => SensorDataProvider(
193+
wearable: wearable,
194+
sensor: sensor,
195+
),
193196
);
194197
}
195198
}

open_wearable/lib/widgets/sensors/values/sensor_values_page.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ class _SensorValuesPageState extends State<SensorValuesPage>
144144
in wearable.requireCapability<SensorManager>().sensors) {
145145
_sensorDataProvider.putIfAbsent(
146146
(wearable, sensor),
147-
() => SensorDataProvider(sensor: sensor),
147+
() => SensorDataProvider(
148+
wearable: wearable,
149+
sensor: sensor,
150+
),
148151
);
149152
}
150153
}

0 commit comments

Comments
 (0)