From 18aa3675725b767b3663513f9545320881ba5d26 Mon Sep 17 00:00:00 2001 From: Miguel Matey Sanz Date: Thu, 23 Jun 2022 11:09:29 +0200 Subject: [PATCH] refactor: rename plugin records and move deviceId to top-level record --- demo/app/home/device/device-view-model.ts | 8 ++-- demo/app/tests/internal/index.spec.ts | 8 ++-- .../internal/sensor-callback-manager.spec.ts | 41 +++++++++-------- .../record-messaging-service.android.spec.ts | 36 +++++++-------- .../record-messaging-service.android.spec.ts | 44 ++++++++----------- ...r-record-messaging-service.android.spec.ts | 22 ++++------ ...e-record-messaging-service.android.spec.ts | 22 ++++------ .../internal/sensors/triaxial/index.spec.ts | 18 ++++---- ...r-record-messaging-service.android.spec.ts | 22 ++++------ ...l-record-messaging-service.android.spec.ts | 44 +++++++------------ ...stract-record-messaging-service.android.ts | 8 ++-- src/internal/sensor-callback-manager.ts | 14 +++--- .../record-messaging-service.android.ts | 14 +++--- src/internal/sensors/heart-rate/record.ts | 5 --- src/internal/sensors/heart-rate/sample.ts | 5 +++ .../record-messaging-service.android.ts | 14 +++--- src/internal/sensors/location/record.ts | 7 --- src/internal/sensors/location/sample.ts | 7 +++ src/internal/sensors/sample.ts | 3 ++ src/internal/sensors/sensor-record.ts | 11 ++--- .../record-messaging-service.android.ts | 11 ++--- .../record-messaging-service.android.ts | 11 ++--- .../record-messaging-service.android.ts | 11 ++--- src/internal/sensors/triaxial/record.ts | 7 --- src/internal/sensors/triaxial/sample.ts | 7 +++ ...iaxial-record-messaging-service.android.ts | 14 +++--- src/internal/store/store.ts | 6 +-- src/internal/tasks/start-sensor-task.ts | 2 +- src/sensors/records/index.ts | 9 ++-- 29 files changed, 198 insertions(+), 233 deletions(-) delete mode 100644 src/internal/sensors/heart-rate/record.ts create mode 100644 src/internal/sensors/heart-rate/sample.ts delete mode 100644 src/internal/sensors/location/record.ts create mode 100644 src/internal/sensors/location/sample.ts create mode 100644 src/internal/sensors/sample.ts delete mode 100644 src/internal/sensors/triaxial/record.ts create mode 100644 src/internal/sensors/triaxial/sample.ts diff --git a/demo/app/home/device/device-view-model.ts b/demo/app/home/device/device-view-model.ts index dbc2e1e..89b9ae9 100644 --- a/demo/app/home/device/device-view-model.ts +++ b/demo/app/home/device/device-view-model.ts @@ -140,10 +140,10 @@ export class DeviceViewModel extends Observable { private handleOnStartTap(sensorDescription: SensorDescription) { const collector = sensorDescription.collector; - const listener = collector.listenSensorUpdates((sensorCallback) => { - const records = sensorCallback.records; - const deviceId = records[0].deviceId; - getLogger().logResultForNode(deviceId, JSON.stringify(records)); + const listener = collector.listenSensorUpdates((sensorRecord) => { + const samples = sensorRecord.samples; + const deviceId = sensorRecord.deviceId; + getLogger().logResultForNode(deviceId, JSON.stringify(samples)); }); this.listeners.set(sensorDescription.sensor, listener); collector.startCollecting( diff --git a/demo/app/tests/internal/index.spec.ts b/demo/app/tests/internal/index.spec.ts index 0a41522..5978353 100644 --- a/demo/app/tests/internal/index.spec.ts +++ b/demo/app/tests/internal/index.spec.ts @@ -1,9 +1,10 @@ import { wearOS, WearOsNode } from "nativescript-wearos-sensors/internal/utils/android/wear-os-types.android"; import { MessagingProtocol } from "nativescript-wearos-sensors/internal/communication/messaging"; import { SensorType } from "nativescript-wearos-sensors/internal/sensors/sensor-type"; -import { SensorRecord, SensorRecords } from "nativescript-wearos-sensors/internal/sensors/sensor-record"; +import { SensorRecord } from "nativescript-wearos-sensors/internal/sensors/sensor-record"; import { MessagingResult } from "nativescript-wearos-sensors/internal/communication/messaging/android/messaging-result-service.android"; import { encodeMessage } from "nativescript-wearos-sensors/internal/communication/encoder-decoder"; +import { SensorSample } from "nativescript-wearos-sensors/internal/sensors/sample"; export function buildFakeNode(id: string, name: string, nearby: boolean): WearOsNode { return new wearOS.Node({ @@ -74,9 +75,10 @@ export function buildFakeMessageEvent( }); } -export function buildFakeSensorRecords(type: SensorType, records: SensorRecord[]): SensorRecords { +export function buildFakeSensorRecords(type: SensorType, deviceId: string, samples: SensorSample[]): SensorRecord { return { type, - records, + deviceId, + samples, } } diff --git a/demo/app/tests/internal/sensor-callback-manager.spec.ts b/demo/app/tests/internal/sensor-callback-manager.spec.ts index 49cec35..243aa7f 100644 --- a/demo/app/tests/internal/sensor-callback-manager.spec.ts +++ b/demo/app/tests/internal/sensor-callback-manager.spec.ts @@ -1,23 +1,22 @@ import { SensorCallbackManager } from "nativescript-wearos-sensors/internal/sensor-callback-manager"; -import { SensorRecord, SensorRecords } from "nativescript-wearos-sensors/internal/sensors/sensor-record"; +import { SensorRecord } from "nativescript-wearos-sensors/internal/sensors/sensor-record"; import { buildFakeSensorRecords } from "~/tests/internal/index.spec"; import { SensorType } from "nativescript-wearos-sensors/internal/sensors/sensor-type"; +import { SensorSample } from "nativescript-wearos-sensors/internal/sensors/sample"; describe("Sensor callback manager", () => { + const deviceId = "test"; const callbackManager = new SensorCallbackManager(); const sensorTypeA = SensorType.ACCELEROMETER; const sensorTypeB = SensorType.LOCATION; - const sensorRecordA1: SensorRecord = { - deviceId: "test", + const sensorRecordA1: SensorSample = { timestamp: Date.now() }; - const sensorRecordA2: SensorRecord = { - deviceId: "test", + const sensorRecordA2: SensorSample = { timestamp: Date.now() }; - const sensorRecordB1: SensorRecord = { - deviceId: "test", + const sensorRecordB1: SensorSample = { timestamp: Date.now() }; const noNotificationTimeout = 100; @@ -37,13 +36,13 @@ describe("Sensor callback manager", () => { (callbackId) => null ); - const sensorRecords = buildFakeSensorRecords(sensorTypeA,[sensorRecordA1, sensorRecordA2]); + const sensorRecord = buildFakeSensorRecords(sensorTypeA, deviceId, [sensorRecordA1, sensorRecordA2]); callbackManager.notifyAll( - sensorRecords + sensorRecord ); const sensorUpdates = await toBeNotified; - expect(sensorUpdates).toEqual(sensorRecords); + expect(sensorUpdates).toEqual(sensorRecord); await notToBeNotified; }); @@ -56,7 +55,7 @@ describe("Sensor callback manager", () => { ); callbackManager.notifyAll( - buildFakeSensorRecords(sensorTypeA,[sensorRecordA1, sensorRecordA2]) + buildFakeSensorRecords(sensorTypeA, deviceId, [sensorRecordA1, sensorRecordA2]) ); await notToBeNotified; }); @@ -71,14 +70,14 @@ describe("Sensor callback manager", () => { const toBeNotified = listenSensorUpdates(callbackManager, sensorTypeA); await notToBeNotified; - const sensorRecords = buildFakeSensorRecords(sensorTypeA,[sensorRecordA1, sensorRecordA2]); - callbackManager.notifyAll(sensorRecords); + const sensorRecord = buildFakeSensorRecords(sensorTypeA, deviceId,[sensorRecordA1, sensorRecordA2]); + callbackManager.notifyAll(sensorRecord); callbackManager.notifyAll( - buildFakeSensorRecords(sensorTypeB, [sensorRecordB1]) + buildFakeSensorRecords(sensorTypeB, deviceId, [sensorRecordB1]) ); const sensorUpdates = await toBeNotified; - expect(sensorUpdates).toBe(sensorRecords); + expect(sensorUpdates).toBe(sensorRecord); }); it("removes all registered callbacks for a specific event, while others can listen updates", async () => { @@ -98,13 +97,13 @@ describe("Sensor callback manager", () => { callbackManager.removeAllForEvent(sensorTypeA); callbackManager.notifyAll( - buildFakeSensorRecords(sensorTypeA,[sensorRecordA1, sensorRecordA2]) + buildFakeSensorRecords(sensorTypeA, deviceId, [sensorRecordA1, sensorRecordA2]) ); - const sensorRecords = buildFakeSensorRecords(sensorTypeB,[sensorRecordB1]); - callbackManager.notifyAll(sensorRecords); + const sensorRecord = buildFakeSensorRecords(sensorTypeB, deviceId, [sensorRecordB1]); + callbackManager.notifyAll(sensorRecord); const sensorUpdates = await toBeNotified; - expect(sensorUpdates).toBe(sensorRecords) + expect(sensorUpdates).toBe(sensorRecord) }) it("removes all registered callbacks, so no one is notified", async () => { @@ -123,7 +122,7 @@ describe("Sensor callback manager", () => { callbackManager.removeAll(); callbackManager.notifyAll( - buildFakeSensorRecords(sensorTypeA,[sensorRecordA1, sensorRecordA2]) + buildFakeSensorRecords(sensorTypeA, deviceId, [sensorRecordA1, sensorRecordA2]) ); const success = await Promise.all([notToBeNotified1, notToBeNotified2]); @@ -136,7 +135,7 @@ describe("Sensor callback manager", () => { function listenSensorUpdates( callbackManager: SensorCallbackManager, eventName: string -): Promise> { +): Promise> { return new Promise((resolve) => callbackManager.add(sensorRecords => resolve(sensorRecords), eventName)) } diff --git a/demo/app/tests/internal/sensors/heart-rate/record-messaging-service.android.spec.ts b/demo/app/tests/internal/sensors/heart-rate/record-messaging-service.android.spec.ts index 2fec5d5..768b8d5 100644 --- a/demo/app/tests/internal/sensors/heart-rate/record-messaging-service.android.spec.ts +++ b/demo/app/tests/internal/sensors/heart-rate/record-messaging-service.android.spec.ts @@ -1,6 +1,6 @@ import { buildFakeMessageEvent, getFakeMessagingProtocol } from "~/tests/internal/index.spec"; import { HeartRateRecordMessagingService } from "nativescript-wearos-sensors/internal/sensors/heart-rate/android/record-messaging-service.android"; -import { HeartRateSensorRecord } from "nativescript-wearos-sensors/internal/sensors/heart-rate/record"; +import { HeartRateSensorSample } from "nativescript-wearos-sensors/internal/sensors/heart-rate/sample"; import { SensorType } from "nativescript-wearos-sensors/internal/sensors/sensor-type"; describe("Heart rate record messaging service", () => { @@ -9,28 +9,22 @@ describe("Heart rate record messaging service", () => { it("decodes the message data building an array of heart rate records", () => { const recordMessagingService = new HeartRateRecordMessagingService(); - const expectedRecords: HeartRateSensorRecord[] = [ - { - deviceId: nodeId, - ...getFakeHeartRateData(), - }, - { - deviceId: nodeId, - ...getFakeHeartRateData(), - } + const expectedSamples: HeartRateSensorSample[] = [ + { ...getFakeHeartRateData() }, + { ...getFakeHeartRateData() } ]; const messageEvent = buildFakeMessageEvent( nodeId, protocol.newRecordMessagePath, - buildFakeEncodedMessage(expectedRecords) + buildFakeEncodedMessage(expectedSamples) ); - const decodedRecord = recordMessagingService.decodeRecords(messageEvent); + const decodedRecord = recordMessagingService.decodeSamples(messageEvent); expect(decodedRecord.type).toBe(SensorType.HEART_RATE); - const { records } = decodedRecord; - records.forEach((record, i) => { - expect(record.value).toEqual(expectedRecords[i].value); + const { samples } = decodedRecord; + samples.forEach((sample, i) => { + expect(sample.value).toEqual(expectedSamples[i].value); }) }); }); @@ -42,16 +36,16 @@ function getFakeHeartRateData() { }; } -function buildFakeEncodedMessage(expectedRecords: HeartRateSensorRecord[]) { - const bytes = Array.create("byte", (4 + (12) * expectedRecords.length)); +function buildFakeEncodedMessage(expectedSamples: HeartRateSensorSample[]) { + const bytes = Array.create("byte", (4 + (12) * expectedSamples.length)); for (let i = 0; i < bytes.length; i++) { bytes[i] = 0; } const buff = java.nio.ByteBuffer.wrap(bytes); - buff.putInt(expectedRecords.length); - expectedRecords.forEach((record) => { - buff.putInt(record.value); - buff.putLong(record.timestamp); + buff.putInt(expectedSamples.length); + expectedSamples.forEach((sample) => { + buff.putInt(sample.value); + buff.putLong(sample.timestamp); }) return bytes; diff --git a/demo/app/tests/internal/sensors/location/record-messaging-service.android.spec.ts b/demo/app/tests/internal/sensors/location/record-messaging-service.android.spec.ts index 27a93ab..91b74f9 100644 --- a/demo/app/tests/internal/sensors/location/record-messaging-service.android.spec.ts +++ b/demo/app/tests/internal/sensors/location/record-messaging-service.android.spec.ts @@ -1,5 +1,5 @@ import { buildFakeMessageEvent, getFakeMessagingProtocol } from "~/tests/internal/index.spec"; -import { LocationSensorRecord } from "nativescript-wearos-sensors/internal/sensors/location/record"; +import { LocationSensorSample } from "nativescript-wearos-sensors/internal/sensors/location/sample"; import { LocationRecordMessagingService } from "nativescript-wearos-sensors/internal/sensors/location/android/record-messaging-service.android"; import { SensorType } from "nativescript-wearos-sensors/internal/sensors/sensor-type"; @@ -9,30 +9,24 @@ describe("Location record messaging service", () => { it("decodes the message data building an array of location records", () => { const recordMessagingService = new LocationRecordMessagingService(); - const expectedRecords: LocationSensorRecord[] = [ - { - deviceId: nodeId, - ...getFakeLocationData(), - }, - { - deviceId: nodeId, - ...getFakeLocationData(), - } + const expectedSamples: LocationSensorSample[] = [ + { ...getFakeLocationData() }, + { ...getFakeLocationData() } ]; const messageEvent = buildFakeMessageEvent( nodeId, protocol.newRecordMessagePath, - buildFakeEncodedMessage(expectedRecords) + buildFakeEncodedMessage(expectedSamples) ); - const decodedRecord = recordMessagingService.decodeRecords(messageEvent); + const decodedRecord = recordMessagingService.decodeSamples(messageEvent); expect(decodedRecord.type).toBe(SensorType.LOCATION); - const { records } = decodedRecord; - records.forEach((record, i) => { - expect(record.latitude).toEqual(expectedRecords[i].latitude); - expect(record.longitude).toEqual(expectedRecords[i].longitude); - expect(record.altitude).toEqual(expectedRecords[i].altitude); + const { samples } = decodedRecord; + samples.forEach((samples, i) => { + expect(samples.latitude).toEqual(expectedSamples[i].latitude); + expect(samples.longitude).toEqual(expectedSamples[i].longitude); + expect(samples.altitude).toEqual(expectedSamples[i].altitude); }) }); }); @@ -46,18 +40,18 @@ function getFakeLocationData() { }; } -function buildFakeEncodedMessage(expectedRecords: LocationSensorRecord[]) { - const bytes = Array.create("byte", (4 + (32) * expectedRecords.length)); +function buildFakeEncodedMessage(expectedSamples: LocationSensorSample[]) { + const bytes = Array.create("byte", (4 + (32) * expectedSamples.length)); for (let i = 0; i < bytes.length; i++) { bytes[i] = 0; } const buff = java.nio.ByteBuffer.wrap(bytes); - buff.putInt(expectedRecords.length); - expectedRecords.forEach((record) => { - buff.putDouble(record.latitude); - buff.putDouble(record.longitude); - buff.putDouble(record.altitude); - buff.putLong(record.timestamp); + buff.putInt(expectedSamples.length); + expectedSamples.forEach((sample) => { + buff.putDouble(sample.latitude); + buff.putDouble(sample.longitude); + buff.putDouble(sample.altitude); + buff.putLong(sample.timestamp); }) return bytes; diff --git a/demo/app/tests/internal/sensors/triaxial/accelerometer/android/accelerometer-record-messaging-service.android.spec.ts b/demo/app/tests/internal/sensors/triaxial/accelerometer/android/accelerometer-record-messaging-service.android.spec.ts index dfe8d25..89c22ef 100644 --- a/demo/app/tests/internal/sensors/triaxial/accelerometer/android/accelerometer-record-messaging-service.android.spec.ts +++ b/demo/app/tests/internal/sensors/triaxial/accelerometer/android/accelerometer-record-messaging-service.android.spec.ts @@ -1,7 +1,7 @@ import { AccelerometerRecordMessagingService } from "nativescript-wearos-sensors/internal/sensors/triaxial/accelerometer/android/record-messaging-service.android"; import { buildFakeEncodedMessage, getFakeTriAxialData } from "../../index.spec"; import { buildFakeMessageEvent, getFakeMessagingProtocol } from "~/tests/internal/index.spec"; -import { TriAxialSensorRecord } from "nativescript-wearos-sensors/internal/sensors/triaxial/record"; +import { TriAxialSensorSample } from "nativescript-wearos-sensors/internal/sensors/triaxial/sample"; import { SensorType } from "nativescript-wearos-sensors/internal/sensors/sensor-type"; describe("Accelerometer record messaging service", () => { @@ -11,24 +11,18 @@ describe("Accelerometer record messaging service", () => { it("decodes the message data building an array of accelerometer records", () => { const recordMessagingService = new AccelerometerRecordMessagingService(); - const expectedRecords: TriAxialSensorRecord[] = [ - { - deviceId: nodeId, - ...getFakeTriAxialData() - }, - { - deviceId: nodeId, - ...getFakeTriAxialData() - } + const expectedSamples: TriAxialSensorSample[] = [ + { ...getFakeTriAxialData() }, + { ...getFakeTriAxialData() } ] const messageEvent = buildFakeMessageEvent( nodeId, protocol.newRecordMessagePath, - buildFakeEncodedMessage(expectedRecords) + buildFakeEncodedMessage(expectedSamples) ) - const decodedRecords = recordMessagingService.decodeRecords(messageEvent); - expect(decodedRecords.type).toBe(SensorType.ACCELEROMETER); - expect(decodedRecords.records.length).toBe(2); + const decodedRecord = recordMessagingService.decodeSamples(messageEvent); + expect(decodedRecord.type).toBe(SensorType.ACCELEROMETER); + expect(decodedRecord.samples.length).toBe(2); }); }); diff --git a/demo/app/tests/internal/sensors/triaxial/gyroscope/android/gyroscope-record-messaging-service.android.spec.ts b/demo/app/tests/internal/sensors/triaxial/gyroscope/android/gyroscope-record-messaging-service.android.spec.ts index 478d5c5..8253581 100644 --- a/demo/app/tests/internal/sensors/triaxial/gyroscope/android/gyroscope-record-messaging-service.android.spec.ts +++ b/demo/app/tests/internal/sensors/triaxial/gyroscope/android/gyroscope-record-messaging-service.android.spec.ts @@ -1,7 +1,7 @@ import { buildFakeMessageEvent, getFakeMessagingProtocol } from "~/tests/internal/index.spec"; import { buildFakeEncodedMessage, getFakeTriAxialData } from "~/tests/internal/sensors/triaxial/index.spec"; import { GyroscopeRecordMessagingService } from "nativescript-wearos-sensors/internal/sensors/triaxial/gyroscope/android/record-messaging-service.android"; -import { TriAxialSensorRecord } from "nativescript-wearos-sensors/internal/sensors/triaxial/record"; +import { TriAxialSensorSample } from "nativescript-wearos-sensors/internal/sensors/triaxial/sample"; import { SensorType } from "nativescript-wearos-sensors/internal/sensors/sensor-type"; describe("Gyroscope record messaging service", () => { @@ -11,24 +11,18 @@ describe("Gyroscope record messaging service", () => { it("decodes the message data building an array of gyroscope records", () => { const recordMessagingService = new GyroscopeRecordMessagingService(); - const expectedRecords: TriAxialSensorRecord[] = [ - { - deviceId: nodeId, - ...getFakeTriAxialData() - }, - { - deviceId: nodeId, - ...getFakeTriAxialData() - } + const expectedSamples: TriAxialSensorSample[] = [ + { ...getFakeTriAxialData() }, + { ...getFakeTriAxialData() } ] const messageEvent = buildFakeMessageEvent( nodeId, protocol.newRecordMessagePath, - buildFakeEncodedMessage(expectedRecords) + buildFakeEncodedMessage(expectedSamples) ) - const decodedRecords = recordMessagingService.decodeRecords(messageEvent); - expect(decodedRecords.type).toBe(SensorType.GYROSCOPE); - expect(decodedRecords.records.length).toBe(2); + const decodedRecord = recordMessagingService.decodeSamples(messageEvent); + expect(decodedRecord.type).toBe(SensorType.GYROSCOPE); + expect(decodedRecord.samples.length).toBe(2); }); }); diff --git a/demo/app/tests/internal/sensors/triaxial/index.spec.ts b/demo/app/tests/internal/sensors/triaxial/index.spec.ts index b04efae..13ef44d 100644 --- a/demo/app/tests/internal/sensors/triaxial/index.spec.ts +++ b/demo/app/tests/internal/sensors/triaxial/index.spec.ts @@ -1,4 +1,4 @@ -import { TriAxialSensorRecord } from "nativescript-wearos-sensors/internal/sensors/triaxial/record"; +import { TriAxialSensorSample } from "nativescript-wearos-sensors/internal/sensors/triaxial/sample"; export function getFakeTriAxialData() { return { @@ -9,18 +9,18 @@ export function getFakeTriAxialData() { }; } -export function buildFakeEncodedMessage(expectedRecords: TriAxialSensorRecord[]) { - const bytes = Array.create("byte", (4 + (20) * expectedRecords.length)); +export function buildFakeEncodedMessage(expectedSamples: TriAxialSensorSample[]) { + const bytes = Array.create("byte", (4 + (20) * expectedSamples.length)); for (let i = 0; i < bytes.length; i++) { bytes[i] = 0; } const buff = java.nio.ByteBuffer.wrap(bytes); - buff.putInt(expectedRecords.length); - expectedRecords.forEach((record) => { - buff.putFloat(record.x); - buff.putFloat(record.y); - buff.putFloat(record.z); - buff.putLong(record.timestamp); + buff.putInt(expectedSamples.length); + expectedSamples.forEach((sample) => { + buff.putFloat(sample.x); + buff.putFloat(sample.y); + buff.putFloat(sample.z); + buff.putLong(sample.timestamp); }) return bytes; diff --git a/demo/app/tests/internal/sensors/triaxial/magnetometer/android/magnetometer-record-messaging-service.android.spec.ts b/demo/app/tests/internal/sensors/triaxial/magnetometer/android/magnetometer-record-messaging-service.android.spec.ts index 65974e8..843b847 100644 --- a/demo/app/tests/internal/sensors/triaxial/magnetometer/android/magnetometer-record-messaging-service.android.spec.ts +++ b/demo/app/tests/internal/sensors/triaxial/magnetometer/android/magnetometer-record-messaging-service.android.spec.ts @@ -1,7 +1,7 @@ import { buildFakeMessageEvent, getFakeMessagingProtocol } from "~/tests/internal/index.spec"; import { buildFakeEncodedMessage, getFakeTriAxialData } from "~/tests/internal/sensors/triaxial/index.spec"; import { MagnetometerRecordMessagingService } from "nativescript-wearos-sensors/internal/sensors/triaxial/magnetometer/android/record-messaging-service.android"; -import { TriAxialSensorRecord } from "nativescript-wearos-sensors/internal/sensors/triaxial/record"; +import { TriAxialSensorSample } from "nativescript-wearos-sensors/internal/sensors/triaxial/sample"; import { SensorType } from "nativescript-wearos-sensors/internal/sensors/sensor-type"; describe("Magnetometer record messaging service", () => { @@ -11,24 +11,18 @@ describe("Magnetometer record messaging service", () => { it("decodes the message data building an array of gyroscope records", () => { const recordMessagingService = new MagnetometerRecordMessagingService(); - const expectedRecords: TriAxialSensorRecord[] = [ - { - deviceId: nodeId, - ...getFakeTriAxialData() - }, - { - deviceId: nodeId, - ...getFakeTriAxialData() - } + const expectedSamples: TriAxialSensorSample[] = [ + { ...getFakeTriAxialData() }, + { ...getFakeTriAxialData() } ] const messageEvent = buildFakeMessageEvent( nodeId, protocol.newRecordMessagePath, - buildFakeEncodedMessage(expectedRecords) + buildFakeEncodedMessage(expectedSamples) ) - const decodedRecords = recordMessagingService.decodeRecords(messageEvent); - expect(decodedRecords.type).toBe(SensorType.MAGNETOMETER); - expect(decodedRecords.records.length).toBe(2); + const decodedRecord = recordMessagingService.decodeSamples(messageEvent); + expect(decodedRecord.type).toBe(SensorType.MAGNETOMETER); + expect(decodedRecord.samples.length).toBe(2); }); }); diff --git a/demo/app/tests/internal/sensors/triaxial/triaxial-record-messaging-service.android.spec.ts b/demo/app/tests/internal/sensors/triaxial/triaxial-record-messaging-service.android.spec.ts index e17113e..0ef76d5 100644 --- a/demo/app/tests/internal/sensors/triaxial/triaxial-record-messaging-service.android.spec.ts +++ b/demo/app/tests/internal/sensors/triaxial/triaxial-record-messaging-service.android.spec.ts @@ -1,6 +1,6 @@ import { buildFakeMessageEvent, getFakeMessagingProtocol } from "../../index.spec"; import { TriAxialRecordMessagingService } from "nativescript-wearos-sensors/internal/sensors/triaxial/triaxial-record-messaging-service.android"; -import { TriAxialSensorRecord } from "nativescript-wearos-sensors/internal/sensors/triaxial/record"; +import { TriAxialSensorSample } from "nativescript-wearos-sensors/internal/sensors/triaxial/sample"; import { buildFakeEncodedMessage, getFakeTriAxialData } from "~/tests/internal/sensors/triaxial/index.spec"; describe("TriAxial record messaging service", () => { @@ -15,14 +15,14 @@ describe("TriAxial record messaging service", () => { recordMessagingService = new TriAxialRecordMessagingService(); recordMessagingService.setProtocol(protocol); recordMessagingService.setCallbackManager(callbackManager); - spyOn(recordMessagingService, "decodeRecords").and.callThrough(); + spyOn(recordMessagingService, "decodeSamples").and.callThrough(); }); it("does nothing when receives a messages with an unknown protocol", () => { const messageEvent = buildFakeMessageEvent(nodeId, "unknownProtocol"); recordMessagingService.onMessageReceived(messageEvent); - expect(recordMessagingService.decodeRecords).not.toHaveBeenCalled(); + expect(recordMessagingService.decodeSamples).not.toHaveBeenCalled(); expect(callbackManager.notifyAll).not.toHaveBeenCalled(); }); @@ -30,35 +30,28 @@ describe("TriAxial record messaging service", () => { const messageEvent = buildFakeMessageEvent(nodeId, protocol.newRecordMessagePath); recordMessagingService.onMessageReceived(messageEvent); - expect(recordMessagingService.decodeRecords).not.toHaveBeenCalled(); + expect(recordMessagingService.decodeSamples).not.toHaveBeenCalled(); expect(callbackManager.notifyAll).not.toHaveBeenCalled(); }); it("decodes the message data building a new record", () => { - const expectedRecords: TriAxialSensorRecord[] = [ - { - deviceId: nodeId, - ...getFakeTriAxialData() - }, - { - deviceId: nodeId, - ...getFakeTriAxialData() - }, + const expectedSamples: TriAxialSensorSample[] = [ + { ...getFakeTriAxialData() }, + { ...getFakeTriAxialData() }, ]; const messageEvent = buildFakeMessageEvent( nodeId, protocol.newRecordMessagePath, - buildFakeEncodedMessage(expectedRecords) + buildFakeEncodedMessage(expectedSamples) ); - const { records } = recordMessagingService.decodeRecords(messageEvent); - expect(records.length).toBe(2); - records.forEach((record, i) => { - expect(record.deviceId).toEqual(expectedRecords[i].deviceId); - expect(record.timestamp).toEqual(expectedRecords[i].timestamp); - expect(record.x).toBeCloseTo(expectedRecords[i].x, 6); - expect(record.y).toBeCloseTo(expectedRecords[i].y, 6); - expect(record.z).toBeCloseTo(expectedRecords[i].z, 6); + const { samples } = recordMessagingService.decodeSamples(messageEvent); + expect(samples.length).toBe(2); + samples.forEach((sample, i) => { + expect(sample.timestamp).toEqual(expectedSamples[i].timestamp); + expect(sample.x).toBeCloseTo(expectedSamples[i].x, 6); + expect(sample.y).toBeCloseTo(expectedSamples[i].y, 6); + expect(sample.z).toBeCloseTo(expectedSamples[i].z, 6); }) }); @@ -68,16 +61,13 @@ describe("TriAxial record messaging service", () => { nodeId, protocol.newRecordMessagePath, buildFakeEncodedMessage([ - { - deviceId: nodeId, - ...getFakeTriAxialData() - } + { ...getFakeTriAxialData() } ]) ); recordMessagingService.onMessageReceived(messageEvent); - expect(recordMessagingService.decodeRecords).toHaveBeenCalledWith(messageEvent); + expect(recordMessagingService.decodeSamples).toHaveBeenCalledWith(messageEvent); expect(callbackManager.notifyAll).toHaveBeenCalled(); }); }) diff --git a/src/internal/communication/messaging/android/abstract-record-messaging-service.android.ts b/src/internal/communication/messaging/android/abstract-record-messaging-service.android.ts index f006eeb..467a94a 100644 --- a/src/internal/communication/messaging/android/abstract-record-messaging-service.android.ts +++ b/src/internal/communication/messaging/android/abstract-record-messaging-service.android.ts @@ -1,6 +1,6 @@ import { wearOS } from "../../../utils/android/wear-os-types.android"; import { MessagingProtocol } from "../index"; -import { SensorRecords } from "../../../sensors/sensor-record"; +import { SensorRecord } from "../../../sensors/sensor-record"; import { SensorCallbackManager } from "../../../sensor-callback-manager"; import WearableListenerServiceDelegate = es.uji.geotec.wearos_sensors.messaging.WearableListenerServiceDelegate; @@ -28,9 +28,9 @@ export abstract class AbstractRecordMessagingService implements WearableListener return; } - const records = this.decodeRecords(message); - this.callbackManager.notifyAll(records); + const record = this.decodeSamples(message); + this.callbackManager.notifyAll(record); } - abstract decodeRecords(messageEvent: wearOS.MessageEvent): SensorRecords; + abstract decodeSamples(messageEvent: wearOS.MessageEvent): SensorRecord; } diff --git a/src/internal/sensor-callback-manager.ts b/src/internal/sensor-callback-manager.ts index 858e0c2..f05e9d7 100644 --- a/src/internal/sensor-callback-manager.ts +++ b/src/internal/sensor-callback-manager.ts @@ -3,17 +3,17 @@ import { EventData as NSEventData, } from "@nativescript/core"; import { fromObject } from "@nativescript/core/data/observable"; -import { SensorRecords } from "./sensors/sensor-record"; +import { SensorRecord } from "./sensors/sensor-record"; interface InternalEventData extends NSEventData { - data: SensorRecords; + data: SensorRecord; } type InternalEventCallback = (eventData: InternalEventData) => void; interface CallbackDescriptor { eventName: string; callback: InternalEventCallback; } -export type SensorCallback = (sensorRecords: SensorRecords) => void; +export type SensorCallback = (sensorRecord: SensorRecord) => void; export class SensorCallbackManager { @@ -38,14 +38,14 @@ export class SensorCallbackManager { return this.listenerCounter++; } - notifyAll(records: SensorRecords): void { - if (!this.notificationCenter.hasListeners(records.type)) { + notifyAll(record: SensorRecord): void { + if (!this.notificationCenter.hasListeners(record.type)) { return; } const eventData: InternalEventData = { - eventName: records.type, + eventName: record.type, object: this.notificationCenter, - data: records, + data: record, }; this.notificationCenter.notify(eventData); } diff --git a/src/internal/sensors/heart-rate/android/record-messaging-service.android.ts b/src/internal/sensors/heart-rate/android/record-messaging-service.android.ts index da7f668..4a22a8b 100644 --- a/src/internal/sensors/heart-rate/android/record-messaging-service.android.ts +++ b/src/internal/sensors/heart-rate/android/record-messaging-service.android.ts @@ -1,24 +1,23 @@ import { AbstractRecordMessagingService } from "../../../communication/messaging/android/abstract-record-messaging-service.android"; import { wearOS } from "../../../utils/android/wear-os-types.android"; -import { SensorRecords } from "../../sensor-record"; -import { HeartRateSensorRecord } from "../record"; +import { SensorRecord } from "../../sensor-record"; +import { HeartRateSensorSample } from "../sample"; import { SensorType } from "../../sensor-type"; import ByteBuffer = java.nio.ByteBuffer; export class HeartRateRecordMessagingService extends AbstractRecordMessagingService { - decodeRecords(messageEvent: wearOS.MessageEvent): SensorRecords { + decodeSamples(messageEvent: wearOS.MessageEvent): SensorRecord { const buff = ByteBuffer.wrap(messageEvent.getData()); const size = buff.getInt(); let value, time; - const records: HeartRateSensorRecord[] = []; + const samples: HeartRateSensorSample[] = []; for (let i = 0; i < size; i++) { value = buff.getInt(); time = buff.getLong(); - records.push({ - deviceId: messageEvent.getSourceNodeId(), + samples.push({ timestamp: time, value, }); @@ -26,7 +25,8 @@ export class HeartRateRecordMessagingService extends AbstractRecordMessagingServ return { type: SensorType.HEART_RATE, - records, + deviceId: messageEvent.getSourceNodeId(), + samples: samples, }; } } diff --git a/src/internal/sensors/heart-rate/record.ts b/src/internal/sensors/heart-rate/record.ts deleted file mode 100644 index c53f53e..0000000 --- a/src/internal/sensors/heart-rate/record.ts +++ /dev/null @@ -1,5 +0,0 @@ -import {SensorRecord} from "../sensor-record"; - -export interface HeartRateSensorRecord extends SensorRecord { - value: number; -} diff --git a/src/internal/sensors/heart-rate/sample.ts b/src/internal/sensors/heart-rate/sample.ts new file mode 100644 index 0000000..7d3475d --- /dev/null +++ b/src/internal/sensors/heart-rate/sample.ts @@ -0,0 +1,5 @@ +import { SensorSample } from "../sample"; + +export interface HeartRateSensorSample extends SensorSample { + value: number; +} diff --git a/src/internal/sensors/location/android/record-messaging-service.android.ts b/src/internal/sensors/location/android/record-messaging-service.android.ts index c7e50e8..292d076 100644 --- a/src/internal/sensors/location/android/record-messaging-service.android.ts +++ b/src/internal/sensors/location/android/record-messaging-service.android.ts @@ -1,26 +1,25 @@ import { AbstractRecordMessagingService } from "../../../communication/messaging/android/abstract-record-messaging-service.android"; import { wearOS } from "../../../utils/android/wear-os-types.android"; -import { SensorRecords } from "../../sensor-record"; -import { LocationSensorRecord } from "../record"; +import { SensorRecord } from "../../sensor-record"; +import { LocationSensorSample } from "../sample"; import { SensorType } from "../../sensor-type"; import ByteBuffer = java.nio.ByteBuffer; export class LocationRecordMessagingService extends AbstractRecordMessagingService { - decodeRecords(messageEvent: wearOS.MessageEvent): SensorRecords { + decodeSamples(messageEvent: wearOS.MessageEvent): SensorRecord { const buff = ByteBuffer.wrap(messageEvent.getData()); const size = buff.getInt(); let lat, lon, alt, time; - const records: LocationSensorRecord[] = []; + const samples: LocationSensorSample[] = []; for (let i = 0; i < size; i++) { lat = buff.getDouble(); lon = buff.getDouble(); alt = buff.getDouble(); time = buff.getLong(); - records.push({ - deviceId: messageEvent.getSourceNodeId(), + samples.push({ timestamp: time, latitude: lat, longitude: lon, @@ -30,7 +29,8 @@ export class LocationRecordMessagingService extends AbstractRecordMessagingServi return { type: SensorType.LOCATION, - records, + deviceId: messageEvent.getSourceNodeId(), + samples: samples, }; } } diff --git a/src/internal/sensors/location/record.ts b/src/internal/sensors/location/record.ts deleted file mode 100644 index a76eb9e..0000000 --- a/src/internal/sensors/location/record.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { SensorRecord } from "../sensor-record"; - -export interface LocationSensorRecord extends SensorRecord { - latitude: number; - longitude: number; - altitude: number; -} diff --git a/src/internal/sensors/location/sample.ts b/src/internal/sensors/location/sample.ts new file mode 100644 index 0000000..4bcd7a8 --- /dev/null +++ b/src/internal/sensors/location/sample.ts @@ -0,0 +1,7 @@ +import { SensorSample } from "../sample"; + +export interface LocationSensorSample extends SensorSample { + latitude: number; + longitude: number; + altitude: number; +} diff --git a/src/internal/sensors/sample.ts b/src/internal/sensors/sample.ts new file mode 100644 index 0000000..f2bbec7 --- /dev/null +++ b/src/internal/sensors/sample.ts @@ -0,0 +1,3 @@ +export interface SensorSample { + timestamp: number; +} \ No newline at end of file diff --git a/src/internal/sensors/sensor-record.ts b/src/internal/sensors/sensor-record.ts index e64826c..432a23d 100644 --- a/src/internal/sensors/sensor-record.ts +++ b/src/internal/sensors/sensor-record.ts @@ -1,11 +1,8 @@ +import { SensorSample } from "./sample"; import { SensorType } from "./sensor-type"; -export interface SensorRecord { - deviceId: string; - timestamp: number; -} - -export interface SensorRecords { +export interface SensorRecord { type: SensorType; - records: T[]; + deviceId: string; + samples: T[]; } diff --git a/src/internal/sensors/triaxial/accelerometer/android/record-messaging-service.android.ts b/src/internal/sensors/triaxial/accelerometer/android/record-messaging-service.android.ts index 7a96734..8c5d365 100644 --- a/src/internal/sensors/triaxial/accelerometer/android/record-messaging-service.android.ts +++ b/src/internal/sensors/triaxial/accelerometer/android/record-messaging-service.android.ts @@ -1,16 +1,17 @@ import { TriAxialRecordMessagingService } from "../../triaxial-record-messaging-service.android"; import { AbstractRecordMessagingService } from "../../../../communication/messaging/android/abstract-record-messaging-service.android"; import { SensorType } from "../../../sensor-type"; -import { SensorRecords } from "../../../sensor-record"; -import { TriAxialSensorRecord } from "../../record"; +import { SensorRecord } from "../../../sensor-record"; +import { TriAxialSensorSample } from "../../sample"; export class AccelerometerRecordMessagingService extends TriAxialRecordMessagingService { - decodeRecords(messageEvent: com.google.android.gms.wearable.MessageEvent): SensorRecords { - const { records } = super.decodeRecords(messageEvent); + decodeSamples(messageEvent: com.google.android.gms.wearable.MessageEvent): SensorRecord { + const { deviceId, samples } = super.decodeSamples(messageEvent); return { type: SensorType.ACCELEROMETER, - records, + deviceId: deviceId, + samples: samples, }; } } diff --git a/src/internal/sensors/triaxial/gyroscope/android/record-messaging-service.android.ts b/src/internal/sensors/triaxial/gyroscope/android/record-messaging-service.android.ts index ad0cf41..12edc75 100644 --- a/src/internal/sensors/triaxial/gyroscope/android/record-messaging-service.android.ts +++ b/src/internal/sensors/triaxial/gyroscope/android/record-messaging-service.android.ts @@ -1,16 +1,17 @@ import { TriAxialRecordMessagingService } from "../../triaxial-record-messaging-service.android"; import { AbstractRecordMessagingService } from "../../../../communication/messaging/android/abstract-record-messaging-service.android"; -import { SensorRecords } from "../../../sensor-record"; +import { SensorRecord } from "../../../sensor-record"; import { SensorType } from "../../../sensor-type"; -import {TriAxialSensorRecord} from "../../record"; +import {TriAxialSensorSample} from "../../sample"; export class GyroscopeRecordMessagingService extends TriAxialRecordMessagingService { - decodeRecords(messageEvent: com.google.android.gms.wearable.MessageEvent): SensorRecords { - const { records } = super.decodeRecords(messageEvent); + decodeSamples(messageEvent: com.google.android.gms.wearable.MessageEvent): SensorRecord { + const { deviceId, samples } = super.decodeSamples(messageEvent); return { type: SensorType.GYROSCOPE, - records, + deviceId: deviceId, + samples: samples, }; } } diff --git a/src/internal/sensors/triaxial/magnetometer/android/record-messaging-service.android.ts b/src/internal/sensors/triaxial/magnetometer/android/record-messaging-service.android.ts index 0d2676f..a6ee4cc 100644 --- a/src/internal/sensors/triaxial/magnetometer/android/record-messaging-service.android.ts +++ b/src/internal/sensors/triaxial/magnetometer/android/record-messaging-service.android.ts @@ -1,16 +1,17 @@ import { TriAxialRecordMessagingService } from "../../triaxial-record-messaging-service.android"; -import { SensorRecords } from "../../../sensor-record"; -import { TriAxialSensorRecord } from "../../record"; +import { SensorRecord } from "../../../sensor-record"; +import { TriAxialSensorSample } from "../../sample"; import { SensorType } from "../../../sensor-type"; import { AbstractRecordMessagingService } from "../../../../communication/messaging/android/abstract-record-messaging-service.android"; export class MagnetometerRecordMessagingService extends TriAxialRecordMessagingService { - decodeRecords(messageEvent: com.google.android.gms.wearable.MessageEvent): SensorRecords { - const { records } = super.decodeRecords(messageEvent); + decodeSamples(messageEvent: com.google.android.gms.wearable.MessageEvent): SensorRecord { + const { deviceId, samples } = super.decodeSamples(messageEvent); return { type: SensorType.MAGNETOMETER, - records, + deviceId: deviceId, + samples: samples, }; } } diff --git a/src/internal/sensors/triaxial/record.ts b/src/internal/sensors/triaxial/record.ts deleted file mode 100644 index ef1eee8..0000000 --- a/src/internal/sensors/triaxial/record.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { SensorRecord } from "../sensor-record"; - -export interface TriAxialSensorRecord extends SensorRecord { - x: number; - y: number; - z: number; -} diff --git a/src/internal/sensors/triaxial/sample.ts b/src/internal/sensors/triaxial/sample.ts new file mode 100644 index 0000000..f9a8814 --- /dev/null +++ b/src/internal/sensors/triaxial/sample.ts @@ -0,0 +1,7 @@ +import { SensorSample } from "../sample"; + +export interface TriAxialSensorSample extends SensorSample { + x: number; + y: number; + z: number; +} diff --git a/src/internal/sensors/triaxial/triaxial-record-messaging-service.android.ts b/src/internal/sensors/triaxial/triaxial-record-messaging-service.android.ts index 2f4b399..384a0ed 100644 --- a/src/internal/sensors/triaxial/triaxial-record-messaging-service.android.ts +++ b/src/internal/sensors/triaxial/triaxial-record-messaging-service.android.ts @@ -1,25 +1,24 @@ import { AbstractRecordMessagingService } from "../../communication/messaging/android/abstract-record-messaging-service.android"; import { wearOS } from "../../utils/android/wear-os-types.android"; -import { TriAxialSensorRecord } from "./record"; -import { SensorRecords } from "../sensor-record"; +import { TriAxialSensorSample } from "./sample"; +import { SensorRecord } from "../sensor-record"; import ByteBuffer = java.nio.ByteBuffer; export class TriAxialRecordMessagingService extends AbstractRecordMessagingService { - decodeRecords(messageEvent: wearOS.MessageEvent): SensorRecords { + decodeSamples(messageEvent: wearOS.MessageEvent): SensorRecord { const buff = ByteBuffer.wrap(messageEvent.getData()); const size = buff.getInt(); let x, y, z, time; - const records: TriAxialSensorRecord[] = []; + const samples: TriAxialSensorSample[] = []; for (let i = 0; i < size; i++) { x = buff.getFloat(); y = buff.getFloat(); z = buff.getFloat(); time = buff.getLong(); - records.push({ - deviceId: messageEvent.getSourceNodeId(), + samples.push({ timestamp: time, x, y, @@ -29,7 +28,8 @@ export class TriAxialRecordMessagingService extends AbstractRecordMessagingServi return { type: null, - records, + deviceId: messageEvent.getSourceNodeId(), + samples: samples, }; } } diff --git a/src/internal/store/store.ts b/src/internal/store/store.ts index 11621b7..ebd4eff 100644 --- a/src/internal/store/store.ts +++ b/src/internal/store/store.ts @@ -1,11 +1,11 @@ -import { SensorRecords } from "../sensors/sensor-record"; +import { SensorRecord } from "../sensors/sensor-record"; import { knownFolders } from "@nativescript/core"; export class Store { - private records: SensorRecords[] = []; + private records: SensorRecord[] = []; - public addRecord(record: SensorRecords): void { + public addRecord(record: SensorRecord): void { this.records.push(record); } diff --git a/src/internal/tasks/start-sensor-task.ts b/src/internal/tasks/start-sensor-task.ts index f5538fb..40f0411 100644 --- a/src/internal/tasks/start-sensor-task.ts +++ b/src/internal/tasks/start-sensor-task.ts @@ -36,7 +36,7 @@ export class StartSensorTask extends Task { taskDispatcher.emitEvent( `${camelCase(this.sensorType)}RecordsAcquired`, { - deviceId: records.records[0].deviceId, + deviceId: records.samples[0].deviceId, records: records }); }); diff --git a/src/sensors/records/index.ts b/src/sensors/records/index.ts index 03c388f..0e7ada2 100644 --- a/src/sensors/records/index.ts +++ b/src/sensors/records/index.ts @@ -1,4 +1,5 @@ -export { SensorRecord, SensorRecords } from "../../internal/sensors/sensor-record"; -export { HeartRateSensorRecord } from "../../internal/sensors/heart-rate/record"; -export { LocationSensorRecord } from "../../internal/sensors/location/record"; -export { TriAxialSensorRecord } from "../../internal/sensors/triaxial/record"; +export { SensorRecord } from "../../internal/sensors/sensor-record"; +export { SensorSample } from "../../internal/sensors/sample"; +export { HeartRateSensorSample } from "../../internal/sensors/heart-rate/sample"; +export { LocationSensorSample } from "../../internal/sensors/location/sample"; +export { TriAxialSensorSample } from "../../internal/sensors/triaxial/sample";