Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce multiple types for sensor values #18

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions example/lib/widgets/sensor_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,18 @@ class SensorView extends StatelessWidget {
List<String> renderedValues = [];
if (snapshot.hasData) {
final sensorValue = snapshot.data!;
renderedValues = sensorValue.values
.map((v) => v.toStringAsFixed(2).padLeft(7, ' '))
.toList();
renderedValues = sensorValue.valueStrings;

if (sensorValue is SensorDoubleValue) {
renderedValues = sensorValue.values
.map((v) => v.toStringAsFixed(2).padLeft(7, ' '))
.toList();
} else if (sensorValue is SensorIntValue) {
renderedValues = sensorValue.values
.map((v) => v.toString().padLeft(7, ' '))
.toList();
}

} else {
renderedValues = List.generate(
sensor.axisCount, (_) => "#.##".padLeft(7, ' '));
Expand Down
46 changes: 42 additions & 4 deletions lib/src/models/capabilities/sensor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,51 @@ abstract class Sensor {
}

class SensorValue {
final List<double> values;
final List<String> _valuesStrings;
final int timestamp;

int get dimensions => values.length;
int get dimensions => _valuesStrings.length;

List<String> get valueStrings => _valuesStrings;

const SensorValue({
required this.values,
required List<String> valueStrings,
required this.timestamp,
});
}) : _valuesStrings = valueStrings;
}

class SensorDoubleValue extends SensorValue {
final List<double> values;

const SensorDoubleValue({
required this.values,
required int timestamp,
}) : super(
valueStrings: const [],
timestamp: timestamp,
);

@override
int get dimensions => values.length;

@override
List<String> get valueStrings => values.map((e) => e.toString()).toList();
}

class SensorIntValue extends SensorValue {
final List<int> values;

const SensorIntValue({
required this.values,
required int timestamp,
}) : super(
valueStrings: const [],
timestamp: timestamp,
);

@override
int get dimensions => values.length;

@override
List<String> get valueStrings => values.map((e) => e.toString()).toList();
}
8 changes: 4 additions & 4 deletions lib/src/models/devices/cosinuss_one.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class _CosinussOneSensor extends Sensor {
int accZ = bytes[18];

streamController.add(
SensorValue(
SensorDoubleValue(
values: [accX.toDouble(), accY.toDouble(), accZ.toDouble()],
timestamp: DateTime.now().millisecondsSinceEpoch - startTime,
),
Expand Down Expand Up @@ -228,7 +228,7 @@ class _CosinussOneSensor extends Sensor {
32; // ambient light sensor (e.g., if sensor is not placed correctly)

streamController.add(
SensorValue(
SensorDoubleValue(
values: [
ppgRed.toDouble(),
ppgGreen.toDouble(),
Expand Down Expand Up @@ -268,7 +268,7 @@ class _CosinussOneSensor extends Sensor {
}

streamController.add(
SensorValue(
SensorDoubleValue(
values: [temperature],
timestamp: DateTime.now().millisecondsSinceEpoch - startTime,
),
Expand Down Expand Up @@ -300,7 +300,7 @@ class _CosinussOneSensor extends Sensor {
}

streamController.add(
SensorValue(
SensorDoubleValue(
values: [bpm.toDouble()],
timestamp: DateTime.now().millisecondsSinceEpoch - startTime,
),
Expand Down
4 changes: 2 additions & 2 deletions lib/src/models/devices/open_earable_v1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ class _OpenEarableSensor extends Sensor {
_dataSubscription = _sensorManager.subscribeToSensorData(0).listen((data) {
int timestamp = data["timestamp"];

SensorValue sensorValue = SensorValue(
SensorValue sensorValue = SensorDoubleValue(
values: [
kalmanX.filtered(data[sensorName]["X"]),
kalmanY.filtered(data[sensorName]["Y"]),
Expand All @@ -436,7 +436,7 @@ class _OpenEarableSensor extends Sensor {
_dataSubscription = _sensorManager.subscribeToSensorData(1).listen((data) {
int timestamp = data["timestamp"];

SensorValue sensorValue = SensorValue(
SensorValue sensorValue = SensorDoubleValue(
values: [data[sensorName][componentName]],
timestamp: timestamp,
);
Expand Down
Loading