From 8b3df17650769b913f64db48d2f56ab7a9a15616 Mon Sep 17 00:00:00 2001 From: Ortes Date: Fri, 26 Jul 2024 08:34:24 +0200 Subject: [PATCH] feat(sensors_plus)!: Add support for platform timestamp in event (#2506) Co-authored-by: Miguel Beltran --- .../plus/sensors/StreamHandlerImpl.kt | 9 +++- .../sensors_plus/example/lib/main.dart | 10 ++--- .../ios/Classes/FPPStreamHandlerPlus.swift | 32 +++++++++++--- .../sensors_plus/lib/src/web_sensors.dart | 15 +++++-- .../sensors_plus/test/sensors_test.dart | 43 ++++++++++++++++--- .../lib/src/accelerometer_event.dart | 13 +++++- .../lib/src/barometer_event.dart | 13 +++++- .../lib/src/gyroscope_event.dart | 13 +++++- .../lib/src/magnetometer_event.dart | 13 +++++- .../lib/src/method_channel_sensors.dart | 33 +++++++++++--- .../lib/src/user_accelerometer_event.dart | 16 ++++++- .../sensors_plus_platform_interface_test.dart | 43 ++++++++++++++++--- 12 files changed, 211 insertions(+), 42 deletions(-) diff --git a/packages/sensors_plus/sensors_plus/android/src/main/kotlin/dev/fluttercommunity/plus/sensors/StreamHandlerImpl.kt b/packages/sensors_plus/sensors_plus/android/src/main/kotlin/dev/fluttercommunity/plus/sensors/StreamHandlerImpl.kt index ffbfefee1f..f74d14b18d 100644 --- a/packages/sensors_plus/sensors_plus/android/src/main/kotlin/dev/fluttercommunity/plus/sensors/StreamHandlerImpl.kt +++ b/packages/sensors_plus/sensors_plus/android/src/main/kotlin/dev/fluttercommunity/plus/sensors/StreamHandlerImpl.kt @@ -4,6 +4,7 @@ import android.hardware.Sensor import android.hardware.SensorEvent import android.hardware.SensorEventListener import android.hardware.SensorManager +import android.os.SystemClock import io.flutter.plugin.common.EventChannel import io.flutter.plugin.common.EventChannel.EventSink @@ -15,6 +16,8 @@ internal class StreamHandlerImpl( private var sensor: Sensor? = null + private var timestampMicroAtBoot: Long = System.currentTimeMillis() * 1000 - SystemClock.elapsedRealtimeNanos() / 1000 + var samplingPeriod = 200000 set(value) { field = value @@ -65,10 +68,14 @@ internal class StreamHandlerImpl( override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {} override fun onSensorChanged(event: SensorEvent) { - val sensorValues = DoubleArray(event.values.size) + val sensorValues = DoubleArray(event.values.size + 1) event.values.forEachIndexed { index, value -> sensorValues[index] = value.toDouble() } + + val timestampMicro = timestampMicroAtBoot + (event.timestamp / 1000) + sensorValues[event.values.size] = timestampMicro.toDouble() + events.success(sensorValues) } } diff --git a/packages/sensors_plus/sensors_plus/example/lib/main.dart b/packages/sensors_plus/sensors_plus/example/lib/main.dart index 61635949d9..93ec4ad684 100644 --- a/packages/sensors_plus/sensors_plus/example/lib/main.dart +++ b/packages/sensors_plus/sensors_plus/example/lib/main.dart @@ -267,7 +267,7 @@ class _MyHomePageState extends State { _streamSubscriptions.add( userAccelerometerEventStream(samplingPeriod: sensorInterval).listen( (UserAccelerometerEvent event) { - final now = DateTime.now(); + final now = event.timestamp; setState(() { _userAccelerometerEvent = event; if (_userAccelerometerUpdateTime != null) { @@ -296,7 +296,7 @@ class _MyHomePageState extends State { _streamSubscriptions.add( accelerometerEventStream(samplingPeriod: sensorInterval).listen( (AccelerometerEvent event) { - final now = DateTime.now(); + final now = event.timestamp; setState(() { _accelerometerEvent = event; if (_accelerometerUpdateTime != null) { @@ -325,7 +325,7 @@ class _MyHomePageState extends State { _streamSubscriptions.add( gyroscopeEventStream(samplingPeriod: sensorInterval).listen( (GyroscopeEvent event) { - final now = DateTime.now(); + final now = event.timestamp; setState(() { _gyroscopeEvent = event; if (_gyroscopeUpdateTime != null) { @@ -354,7 +354,7 @@ class _MyHomePageState extends State { _streamSubscriptions.add( magnetometerEventStream(samplingPeriod: sensorInterval).listen( (MagnetometerEvent event) { - final now = DateTime.now(); + final now = event.timestamp; setState(() { _magnetometerEvent = event; if (_magnetometerUpdateTime != null) { @@ -383,7 +383,7 @@ class _MyHomePageState extends State { _streamSubscriptions.add( barometerEventStream(samplingPeriod: sensorInterval).listen( (BarometerEvent event) { - final now = DateTime.now(); + final now = event.timestamp; setState(() { _barometerEvent = event; if (_barometerUpdateTime != null) { diff --git a/packages/sensors_plus/sensors_plus/ios/Classes/FPPStreamHandlerPlus.swift b/packages/sensors_plus/sensors_plus/ios/Classes/FPPStreamHandlerPlus.swift index e7b335df2d..d5ad3624be 100644 --- a/packages/sensors_plus/sensors_plus/ios/Classes/FPPStreamHandlerPlus.swift +++ b/packages/sensors_plus/sensors_plus/ios/Classes/FPPStreamHandlerPlus.swift @@ -15,6 +15,8 @@ public protocol MotionStreamHandler: FlutterStreamHandler { var samplingPeriod: Int { get set } } +let timestampMicroAtBoot = (Date().timeIntervalSince1970 - ProcessInfo.processInfo.systemUptime) * 1000000 + func _initMotionManager() { if (_motionManager == nil) { _motionManager = CMMotionManager() @@ -31,14 +33,15 @@ func _initAltimeter() { } } -func sendTriplet(x: Float64, y: Float64, z: Float64, sink: @escaping FlutterEventSink) { +func sendFlutter(x: Float64, y: Float64, z: Float64, timestamp: TimeInterval, sink: @escaping FlutterEventSink) { if _isCleanUp { return } // Even after [detachFromEngineForRegistrar] some events may still be received // and fired until fully detached. DispatchQueue.main.async { - let triplet = [x, y, z] + let timestampSince1970Micro = timestampMicroAtBoot + (timestamp * 1000000) + let triplet = [x, y, z, timestampSince1970Micro] triplet.withUnsafeBufferPointer { buffer in sink(FlutterStandardTypedData.init(float64: Data(buffer: buffer))) } @@ -74,10 +77,11 @@ class FPPAccelerometerStreamHandlerPlus: NSObject, MotionStreamHandler { // Multiply by gravity, and adjust sign values to // align with Android. let acceleration = data!.acceleration - sendTriplet( + sendFlutter( x: -acceleration.x * GRAVITY, y: -acceleration.y * GRAVITY, z: -acceleration.z * GRAVITY, + timestamp: data!.timestamp, sink: sink ) } @@ -123,10 +127,11 @@ class FPPUserAccelStreamHandlerPlus: NSObject, MotionStreamHandler { // Multiply by gravity, and adjust sign values to // align with Android. let acceleration = data!.userAcceleration - sendTriplet( + sendFlutter( x: -acceleration.x * GRAVITY, y: -acceleration.y * GRAVITY, z: -acceleration.z * GRAVITY, + timestamp: data!.timestamp, sink: sink ) } @@ -170,7 +175,13 @@ class FPPGyroscopeStreamHandlerPlus: NSObject, MotionStreamHandler { return } let rotationRate = data!.rotationRate - sendTriplet(x: rotationRate.x, y: rotationRate.y, z: rotationRate.z, sink: sink) + sendFlutter( + x: rotationRate.x, + y: rotationRate.y, + z: rotationRate.z, + timestamp: data!.timestamp, + sink: sink + ) } return nil } @@ -212,7 +223,13 @@ class FPPMagnetometerStreamHandlerPlus: NSObject, MotionStreamHandler { return } let magneticField = data!.magneticField - sendTriplet(x: magneticField.x, y: magneticField.y, z: magneticField.z, sink: sink) + sendFlutter( + x: magneticField.x, + y: magneticField.y, + z: magneticField.z, + timestamp: data!.timestamp, + sink: sink + ) } return nil } @@ -257,7 +274,8 @@ class FPPBarometerStreamHandlerPlus: NSObject, MotionStreamHandler { } let pressure = data!.pressure.doubleValue * 10.0 // kPa to hPa (hectopascals) DispatchQueue.main.async { - let pressureArray: [Double] = [pressure] + let timestampSince1970Micro = timestampMicroAtBoot + (data!.timestamp * 1000000) + let pressureArray: [Double] = [pressure, timestampSince1970Micro] pressureArray.withUnsafeBufferPointer { buffer in sink(FlutterStandardTypedData.init(float64: Data(buffer: buffer))) } diff --git a/packages/sensors_plus/sensors_plus/lib/src/web_sensors.dart b/packages/sensors_plus/sensors_plus/lib/src/web_sensors.dart index d9b9c6badb..00cbd56b4b 100644 --- a/packages/sensors_plus/sensors_plus/lib/src/web_sensors.dart +++ b/packages/sensors_plus/sensors_plus/lib/src/web_sensors.dart @@ -85,6 +85,7 @@ class WebSensorsPlugin extends SensorsPlatform { accelerometer.x, accelerometer.y, accelerometer.z, + DateTime.now(), ), ); }.toJS; @@ -99,7 +100,8 @@ class WebSensorsPlugin extends SensorsPlatform { apiName: 'Accelerometer()', permissionName: 'accelerometer', onError: () { - _accelerometerStreamController!.add(AccelerometerEvent(0, 0, 0)); + _accelerometerStreamController! + .add(AccelerometerEvent(0, 0, 0, DateTime.now())); }, ); _accelerometerResultStream = @@ -138,6 +140,7 @@ class WebSensorsPlugin extends SensorsPlatform { gyroscope.x, gyroscope.y, gyroscope.z, + DateTime.now(), ), ); }.toJS; @@ -152,7 +155,8 @@ class WebSensorsPlugin extends SensorsPlatform { apiName: 'Gyroscope()', permissionName: 'gyroscope', onError: () { - _gyroscopeEventStreamController!.add(GyroscopeEvent(0, 0, 0)); + _gyroscopeEventStreamController! + .add(GyroscopeEvent(0, 0, 0, DateTime.now())); }, ); _gyroscopeEventResultStream = @@ -192,6 +196,7 @@ class WebSensorsPlugin extends SensorsPlatform { linearAccelerationSensor.x, linearAccelerationSensor.y, linearAccelerationSensor.z, + DateTime.now(), ), ); }.toJS; @@ -207,7 +212,7 @@ class WebSensorsPlugin extends SensorsPlatform { permissionName: 'accelerometer', onError: () { _userAccelerometerStreamController! - .add(UserAccelerometerEvent(0, 0, 0)); + .add(UserAccelerometerEvent(0, 0, 0, DateTime.now())); }, ); _userAccelerometerResultStream = @@ -247,6 +252,7 @@ class WebSensorsPlugin extends SensorsPlatform { magnetometerSensor.x, magnetometerSensor.y, magnetometerSensor.z, + DateTime.now(), ), ); }.toJS; @@ -261,7 +267,8 @@ class WebSensorsPlugin extends SensorsPlatform { apiName: 'Magnetometer()', permissionName: 'magnetometer', onError: () { - _magnetometerStreamController!.add(MagnetometerEvent(0, 0, 0)); + _magnetometerStreamController! + .add(MagnetometerEvent(0, 0, 0, DateTime.now())); }, ); _magnetometerResultStream = diff --git a/packages/sensors_plus/sensors_plus/test/sensors_test.dart b/packages/sensors_plus/sensors_plus/test/sensors_test.dart index a250c41531..bacca67d95 100644 --- a/packages/sensors_plus/sensors_plus/test/sensors_test.dart +++ b/packages/sensors_plus/sensors_plus/test/sensors_test.dart @@ -13,7 +13,13 @@ void main() { test('accelerometerEvents are streamed', () async { const channelName = 'dev.fluttercommunity.plus/sensors/accelerometer'; - const sensorData = [1.0, 2.0, 3.0]; + final date = DateTime.now(); + final sensorData = [ + 1.0, + 2.0, + 3.0, + date.microsecondsSinceEpoch.toDouble(), + ]; _initializeFakeMethodChannel('setAccelerationSamplingPeriod'); _initializeFakeSensorChannel(channelName, sensorData); @@ -22,11 +28,18 @@ void main() { expect(event.x, sensorData[0]); expect(event.y, sensorData[1]); expect(event.z, sensorData[2]); + expect(event.timestamp, date); }); test('gyroscopeEvents are streamed', () async { const channelName = 'dev.fluttercommunity.plus/sensors/gyroscope'; - const sensorData = [3.0, 4.0, 5.0]; + final date = DateTime.now(); + final sensorData = [ + 3.0, + 4.0, + 5.0, + date.microsecondsSinceEpoch.toDouble(), + ]; _initializeFakeMethodChannel('setGyroscopeSamplingPeriod'); _initializeFakeSensorChannel(channelName, sensorData); @@ -35,11 +48,18 @@ void main() { expect(event.x, sensorData[0]); expect(event.y, sensorData[1]); expect(event.z, sensorData[2]); + expect(event.timestamp, date); }); test('userAccelerometerEvents are streamed', () async { const channelName = 'dev.fluttercommunity.plus/sensors/user_accel'; - const sensorData = [6.0, 7.0, 8.0]; + final date = DateTime.now(); + final sensorData = [ + 6.0, + 7.0, + 8.0, + date.microsecondsSinceEpoch.toDouble(), + ]; _initializeFakeMethodChannel('setUserAccelerometerSamplingPeriod'); _initializeFakeSensorChannel(channelName, sensorData); @@ -48,11 +68,18 @@ void main() { expect(event.x, sensorData[0]); expect(event.y, sensorData[1]); expect(event.z, sensorData[2]); + expect(event.timestamp, date); }); test('magnetometerEvents are streamed', () async { const channelName = 'dev.fluttercommunity.plus/sensors/magnetometer'; - const sensorData = [8.0, 9.0, 10.0]; + final date = DateTime.now(); + final sensorData = [ + 8.0, + 9.0, + 10.0, + date.microsecondsSinceEpoch.toDouble(), + ]; _initializeFakeMethodChannel('setMagnetometerSamplingPeriod'); _initializeFakeSensorChannel(channelName, sensorData); @@ -61,17 +88,23 @@ void main() { expect(event.x, sensorData[0]); expect(event.y, sensorData[1]); expect(event.z, sensorData[2]); + expect(event.timestamp, date); }); test('barometerEvents are streamed', () async { const channelName = 'dev.fluttercommunity.plus/sensors/barometer'; - const sensorData = [1000.0]; + final date = DateTime.now(); + final sensorData = [ + 1000.0, + date.microsecondsSinceEpoch.toDouble(), + ]; _initializeFakeMethodChannel('setBarometerSamplingPeriod'); _initializeFakeSensorChannel(channelName, sensorData); final event = await barometerEventStream().first; expect(event.pressure, sensorData[0]); + expect(event.timestamp, date); }); } diff --git a/packages/sensors_plus/sensors_plus_platform_interface/lib/src/accelerometer_event.dart b/packages/sensors_plus/sensors_plus_platform_interface/lib/src/accelerometer_event.dart index 57d688a4ba..351bb76227 100644 --- a/packages/sensors_plus/sensors_plus_platform_interface/lib/src/accelerometer_event.dart +++ b/packages/sensors_plus/sensors_plus_platform_interface/lib/src/accelerometer_event.dart @@ -8,7 +8,7 @@ /// a particular direction. class AccelerometerEvent { /// Constructs an instance with the given [x], [y], and [z] values. - AccelerometerEvent(this.x, this.y, this.z); + AccelerometerEvent(this.x, this.y, this.z, this.timestamp); /// Acceleration force along the x axis (including gravity) measured in m/s^2. /// @@ -30,6 +30,15 @@ class AccelerometerEvent { /// towards the user and negative mean it is moving away from them. final double z; + /// timestamp of the event + /// + /// This is the timestamp of the event in microseconds, as provided by the + /// underlying platform. For Android, this is the uptimeMillis provided by + /// the SensorEvent. For iOS, this is the timestamp provided by the CMDeviceMotion. + + final DateTime timestamp; + @override - String toString() => '[AccelerometerEvent (x: $x, y: $y, z: $z)]'; + String toString() => + '[AccelerometerEvent (x: $x, y: $y, z: $z, timestamp: $timestamp)]'; } diff --git a/packages/sensors_plus/sensors_plus_platform_interface/lib/src/barometer_event.dart b/packages/sensors_plus/sensors_plus_platform_interface/lib/src/barometer_event.dart index c87722be2f..4d8adad35c 100644 --- a/packages/sensors_plus/sensors_plus_platform_interface/lib/src/barometer_event.dart +++ b/packages/sensors_plus/sensors_plus_platform_interface/lib/src/barometer_event.dart @@ -16,11 +16,20 @@ class BarometerEvent { /// Constructs a new instance with the given [pressure] value. /// /// See [BarometerEvent] for more information. - BarometerEvent(this.pressure); + BarometerEvent(this.pressure, this.timestamp); /// The atmospheric pressure surrounding the sensor in hectopascals ***hPa***. final double pressure; + /// timestamp of the event + /// + /// This is the timestamp of the event in microseconds, as provided by the + /// underlying platform. For Android, this is the uptimeMillis provided by + /// the SensorEvent. For iOS, this is the timestamp provided by the CMDeviceMotion. + + final DateTime timestamp; + @override - String toString() => '[BarometerEvent (pressure: $pressure hPa)]'; + String toString() => + '[BarometerEvent (pressure: $pressure hPa, timestamp: $timestamp)]'; } diff --git a/packages/sensors_plus/sensors_plus_platform_interface/lib/src/gyroscope_event.dart b/packages/sensors_plus/sensors_plus_platform_interface/lib/src/gyroscope_event.dart index 59c90e4b85..0deb917e07 100644 --- a/packages/sensors_plus/sensors_plus_platform_interface/lib/src/gyroscope_event.dart +++ b/packages/sensors_plus/sensors_plus_platform_interface/lib/src/gyroscope_event.dart @@ -6,7 +6,7 @@ /// the device in 3D space. class GyroscopeEvent { /// Constructs an instance with the given [x], [y], and [z] values. - GyroscopeEvent(this.x, this.y, this.z); + GyroscopeEvent(this.x, this.y, this.z, this.timestamp); /// Rate of rotation around the x axis measured in rad/s. /// @@ -30,6 +30,15 @@ class GyroscopeEvent { /// on. final double z; + /// timestamp of the event + /// + /// This is the timestamp of the event in microseconds, as provided by the + /// underlying platform. For Android, this is the uptimeMillis provided by + /// the SensorEvent. For iOS, this is the timestamp provided by the CMDeviceMotion. + + final DateTime timestamp; + @override - String toString() => '[GyroscopeEvent (x: $x, y: $y, z: $z)]'; + String toString() => + '[GyroscopeEvent (x: $x, y: $y, z: $z, timestamp: $timestamp)]'; } diff --git a/packages/sensors_plus/sensors_plus_platform_interface/lib/src/magnetometer_event.dart b/packages/sensors_plus/sensors_plus_platform_interface/lib/src/magnetometer_event.dart index b17eef1507..71e50e4ee2 100644 --- a/packages/sensors_plus/sensors_plus_platform_interface/lib/src/magnetometer_event.dart +++ b/packages/sensors_plus/sensors_plus_platform_interface/lib/src/magnetometer_event.dart @@ -12,12 +12,21 @@ class MagnetometerEvent { /// Constructs a new instance with the given [x], [y], and [z] values. /// /// See [MagnetometerEvent] for more information. - MagnetometerEvent(this.x, this.y, this.z); + MagnetometerEvent(this.x, this.y, this.z, this.timestamp); /// The ambient magnetic field in this axis surrounding the sensor in /// microteslas ***μT***. final double x, y, z; + /// timestamp of the event + /// + /// This is the timestamp of the event in microseconds, as provided by the + /// underlying platform. For Android, this is the uptimeMillis provided by + /// the SensorEvent. For iOS, this is the timestamp provided by the CMDeviceMotion. + + final DateTime timestamp; + @override - String toString() => '[MagnetometerEvent (x: $x, y: $y, z: $z)]'; + String toString() => + '[MagnetometerEvent (x: $x, y: $y, z: $z, timestamp: $timestamp)]'; } diff --git a/packages/sensors_plus/sensors_plus_platform_interface/lib/src/method_channel_sensors.dart b/packages/sensors_plus/sensors_plus_platform_interface/lib/src/method_channel_sensors.dart index b454096e1c..a604345fd0 100644 --- a/packages/sensors_plus/sensors_plus_platform_interface/lib/src/method_channel_sensors.dart +++ b/packages/sensors_plus/sensors_plus_platform_interface/lib/src/method_channel_sensors.dart @@ -56,7 +56,12 @@ class MethodChannelSensors extends SensorsPlatform { .receiveBroadcastStream() .map((dynamic event) { final list = event.cast(); - return AccelerometerEvent(list[0]!, list[1]!, list[2]!); + return AccelerometerEvent( + list[0]!, + list[1]!, + list[2]!, + DateTime.fromMicrosecondsSinceEpoch(list[3]!.toInt()), + ); }); return _accelerometerEvents!; } @@ -81,7 +86,12 @@ class MethodChannelSensors extends SensorsPlatform { _gyroscopeEvents ??= _gyroscopeEventChannel.receiveBroadcastStream().map((dynamic event) { final list = event.cast(); - return GyroscopeEvent(list[0]!, list[1]!, list[2]!); + return GyroscopeEvent( + list[0]!, + list[1]!, + list[2]!, + DateTime.fromMicrosecondsSinceEpoch(list[3]!.toInt()), + ); }); return _gyroscopeEvents!; } @@ -108,7 +118,12 @@ class MethodChannelSensors extends SensorsPlatform { .receiveBroadcastStream() .map((dynamic event) { final list = event.cast(); - return UserAccelerometerEvent(list[0]!, list[1]!, list[2]!); + return UserAccelerometerEvent( + list[0]!, + list[1]!, + list[2]!, + DateTime.fromMicrosecondsSinceEpoch(list[3]!.toInt()), + ); }); return _userAccelerometerEvents!; } @@ -133,7 +148,12 @@ class MethodChannelSensors extends SensorsPlatform { _magnetometerEvents ??= _magnetometerEventChannel.receiveBroadcastStream().map((dynamic event) { final list = event.cast(); - return MagnetometerEvent(list[0]!, list[1]!, list[2]!); + return MagnetometerEvent( + list[0]!, + list[1]!, + list[2]!, + DateTime.fromMicrosecondsSinceEpoch(list[3]!.toInt()), + ); }); return _magnetometerEvents!; } @@ -158,7 +178,10 @@ class MethodChannelSensors extends SensorsPlatform { _barometerEvents ??= _barometerEventChannel.receiveBroadcastStream().map((dynamic event) { final list = event.cast(); - return BarometerEvent(list[0]!); + return BarometerEvent( + list[0]!, + DateTime.fromMicrosecondsSinceEpoch(list[1]!.toInt()), + ); }); return _barometerEvents!; } diff --git a/packages/sensors_plus/sensors_plus_platform_interface/lib/src/user_accelerometer_event.dart b/packages/sensors_plus/sensors_plus_platform_interface/lib/src/user_accelerometer_event.dart index c8d2fa8ded..4d8f74a271 100644 --- a/packages/sensors_plus/sensors_plus_platform_interface/lib/src/user_accelerometer_event.dart +++ b/packages/sensors_plus/sensors_plus_platform_interface/lib/src/user_accelerometer_event.dart @@ -7,7 +7,7 @@ /// [AccelerometerEvent], this event does not include the effects of gravity. class UserAccelerometerEvent { /// Constructs an instance with the given [x], [y], and [z] values. - UserAccelerometerEvent(this.x, this.y, this.z); + UserAccelerometerEvent(this.x, this.y, this.z, this.timestamp); /// Acceleration force along the x axis (excluding gravity) measured in m/s^2. /// @@ -29,6 +29,18 @@ class UserAccelerometerEvent { /// towards the user and negative mean it is moving away from them. final double z; + /// timestamp of the event + /// + /// This is the timestamp of the event in microseconds, as provided by the + /// underlying platform. For Android, this is the timestamp in the event in + /// nanoseconds from the `android.os.SystemClock.elapsedRealtimeNanos` API, + /// and synchronized with SystemClock.uptimeMillis() to be in the same time. + /// For iOS, this is the TimeInterval in the event synchronized with + /// [NSDate timeIntervalSince1970] to be in the same time. + + final DateTime timestamp; + @override - String toString() => '[UserAccelerometerEvent (x: $x, y: $y, z: $z)]'; + String toString() => + '[UserAccelerometerEvent (x: $x, y: $y, z: $z, timestamp: $timestamp)]'; } diff --git a/packages/sensors_plus/sensors_plus_platform_interface/test/sensors_plus_platform_interface_test.dart b/packages/sensors_plus/sensors_plus_platform_interface/test/sensors_plus_platform_interface_test.dart index 05bb666499..e0376121dc 100644 --- a/packages/sensors_plus/sensors_plus_platform_interface/test/sensors_plus_platform_interface_test.dart +++ b/packages/sensors_plus/sensors_plus_platform_interface/test/sensors_plus_platform_interface_test.dart @@ -62,7 +62,13 @@ void main() { test('accelerometerEvents are streamed', () async { const channelName = 'dev.fluttercommunity.plus/sensors/accelerometer'; - const sensorData = [1.0, 2.0, 3.0]; + final date = DateTime.now(); + final sensorData = [ + 1.0, + 2.0, + 3.0, + date.microsecondsSinceEpoch.toDouble(), + ]; _initializeFakeMethodChannel('setAccelerationSamplingPeriod'); _initializeFakeSensorChannel(channelName, sensorData); @@ -71,11 +77,18 @@ void main() { expect(event.x, sensorData[0]); expect(event.y, sensorData[1]); expect(event.z, sensorData[2]); + expect(event.timestamp, date); }); test('gyroscopeEvents are streamed', () async { const channelName = 'dev.fluttercommunity.plus/sensors/gyroscope'; - const sensorData = [3.0, 4.0, 5.0]; + final date = DateTime.now(); + final sensorData = [ + 3.0, + 4.0, + 5.0, + date.microsecondsSinceEpoch.toDouble(), + ]; _initializeFakeMethodChannel('setGyroscopeSamplingPeriod'); _initializeFakeSensorChannel(channelName, sensorData); @@ -84,11 +97,18 @@ void main() { expect(event.x, sensorData[0]); expect(event.y, sensorData[1]); expect(event.z, sensorData[2]); + expect(event.timestamp, date); }); test('userAccelerometerEvents are streamed', () async { const channelName = 'dev.fluttercommunity.plus/sensors/user_accel'; - const sensorData = [6.0, 7.0, 8.0]; + final date = DateTime.now(); + final sensorData = [ + 6.0, + 7.0, + 8.0, + date.microsecondsSinceEpoch.toDouble(), + ]; _initializeFakeMethodChannel('setUserAccelerometerSamplingPeriod'); _initializeFakeSensorChannel(channelName, sensorData); @@ -97,11 +117,18 @@ void main() { expect(event.x, sensorData[0]); expect(event.y, sensorData[1]); expect(event.z, sensorData[2]); + expect(event.timestamp, date); }); test('magnetometerEvents are streamed', () async { const channelName = 'dev.fluttercommunity.plus/sensors/magnetometer'; - const sensorData = [8.0, 9.0, 10.0]; + final date = DateTime.now(); + final sensorData = [ + 8.0, + 9.0, + 10.0, + date.microsecondsSinceEpoch.toDouble(), + ]; _initializeFakeMethodChannel('setMagnetometerSamplingPeriod'); _initializeFakeSensorChannel(channelName, sensorData); @@ -110,17 +137,23 @@ void main() { expect(event.x, sensorData[0]); expect(event.y, sensorData[1]); expect(event.z, sensorData[2]); + expect(event.timestamp, date); }); test('barometerEvents are streamed', () async { const channelName = 'dev.fluttercommunity.plus/sensors/barometer'; - const sensorData = [1000.0]; + final date = DateTime.now(); + final sensorData = [ + 1000.0, + date.microsecondsSinceEpoch.toDouble(), + ]; _initializeFakeMethodChannel('setBarometerSamplingPeriod'); _initializeFakeSensorChannel(channelName, sensorData); final event = await barometerEventStream().first; expect(event.pressure, sensorData[0]); + expect(event.timestamp, date); }); }