Skip to content

Commit

Permalink
refactor: rename plugin records and move deviceId to top-level record
Browse files Browse the repository at this point in the history
  • Loading branch information
matey97 committed Jun 23, 2022
1 parent 0f6ffc2 commit 18aa367
Show file tree
Hide file tree
Showing 29 changed files with 198 additions and 233 deletions.
8 changes: 4 additions & 4 deletions demo/app/home/device/device-view-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 5 additions & 3 deletions demo/app/tests/internal/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -74,9 +75,10 @@ export function buildFakeMessageEvent(
});
}

export function buildFakeSensorRecords(type: SensorType, records: SensorRecord[]): SensorRecords<any> {
export function buildFakeSensorRecords(type: SensorType, deviceId: string, samples: SensorSample[]): SensorRecord<any> {
return {
type,
records,
deviceId,
samples,
}
}
41 changes: 20 additions & 21 deletions demo/app/tests/internal/sensor-callback-manager.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
});

Expand All @@ -56,7 +55,7 @@ describe("Sensor callback manager", () => {
);

callbackManager.notifyAll(
buildFakeSensorRecords(sensorTypeA,[sensorRecordA1, sensorRecordA2])
buildFakeSensorRecords(sensorTypeA, deviceId, [sensorRecordA1, sensorRecordA2])
);
await notToBeNotified;
});
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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]);
Expand All @@ -136,7 +135,7 @@ describe("Sensor callback manager", () => {
function listenSensorUpdates(
callbackManager: SensorCallbackManager,
eventName: string
): Promise<SensorRecords<any>> {
): Promise<SensorRecord<any>> {
return new Promise((resolve) =>
callbackManager.add(sensorRecords => resolve(sensorRecords), eventName))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand All @@ -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);
})
});
});
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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);
})
});
});
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand All @@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand All @@ -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);
});
});
Loading

0 comments on commit 18aa367

Please sign in to comment.