From 6c6ffa45a770afa1edceadc58e07ea03173101eb Mon Sep 17 00:00:00 2001 From: Miguel Matey Sanz Date: Wed, 22 Jun 2022 13:03:17 +0200 Subject: [PATCH 01/31] feat: parameterize timeout for resolutions --- .../capabilities/android/capability-client.android.ts | 4 ++-- src/internal/communication/communication-client.android.ts | 4 ++-- .../messaging/android/messaging-client.android.ts | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/internal/communication/capabilities/android/capability-client.android.ts b/src/internal/communication/capabilities/android/capability-client.android.ts index 2ebd455..4b48291 100644 --- a/src/internal/communication/capabilities/android/capability-client.android.ts +++ b/src/internal/communication/capabilities/android/capability-client.android.ts @@ -15,8 +15,8 @@ export class CapabilityClient extends CommunicationClient { - const resolutionPromise = this.createResolutionPromise(this.protocol, node); + public async sendCapabilityAdvertisementRequest(node: Node, timeout: number): Promise { + const resolutionPromise = this.createResolutionPromise(this.protocol, node, timeout); await this.sendMessage(node, this.protocol.messagePath); return await resolutionPromise; diff --git a/src/internal/communication/communication-client.android.ts b/src/internal/communication/communication-client.android.ts index 359ca20..1234e77 100644 --- a/src/internal/communication/communication-client.android.ts +++ b/src/internal/communication/communication-client.android.ts @@ -27,14 +27,14 @@ export class CommunicationClient { ); } - protected createResolutionPromise(protocol: CommunicationProtocol, node: Node, timeout: boolean = true): Promise { + protected createResolutionPromise(protocol: CommunicationProtocol, node: Node, timeout?: number): Promise { this.communicationResultService.setProtocol(protocol); return new Promise(async (resolve, reject) => { let timeoutId; if (timeout) { timeoutId = setTimeout(() => { reject(`Timeout for communication request in node ${node.name} (${node.id})`); - }, 5000); + }, timeout); } this.communicationResultService.setResolutionCallbackForNode( diff --git a/src/internal/communication/messaging/android/messaging-client.android.ts b/src/internal/communication/messaging/android/messaging-client.android.ts index 30fd7de..de92312 100644 --- a/src/internal/communication/messaging/android/messaging-client.android.ts +++ b/src/internal/communication/messaging/android/messaging-client.android.ts @@ -16,7 +16,8 @@ export class MessagingClientImpl extends CommunicationClient im public async sendIsReadyMessage(node: Node): Promise { const resolutionPromise = this.createResolutionPromise( this.protocol.readyProtocol, - node + node, + 5000 ); await this.sendMessage(node, this.protocol.readyProtocol.messagePath); return await resolutionPromise; @@ -25,8 +26,7 @@ export class MessagingClientImpl extends CommunicationClient im public async sendPrepareMessage(node: Node): Promise { const resolutionPromise = this.createResolutionPromise( this.protocol.prepareProtocol, - node, - false + node ); await this.sendMessage(node, this.protocol.prepareProtocol.messagePath); return await resolutionPromise; From 6465277294a5026874d77998166166c5651f6a9d Mon Sep 17 00:00:00 2001 From: Miguel Matey Sanz Date: Wed, 22 Jun 2022 13:05:34 +0200 Subject: [PATCH 02/31] refactor!: new promise API for connected nodes --- .../device-list/device-list-view-model.ts | 37 +++++++++---------- .../discoverer/node-discoverer.android.ts | 15 ++++++-- .../node/discoverer/node-discoverer.ts | 3 +- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/demo/app/home/device-list/device-list-view-model.ts b/demo/app/home/device-list/device-list-view-model.ts index 6ea5dff..4ca6ef4 100644 --- a/demo/app/home/device-list/device-list-view-model.ts +++ b/demo/app/home/device-list/device-list-view-model.ts @@ -1,6 +1,6 @@ import { Observable } from "@nativescript/core"; import { getLogger } from "~/home/logger/logger-view-model"; -import { getNodeDiscoverer, NodeDiscovered } from "nativescript-wearos-sensors/node"; +import { getNodeDiscoverer } from "nativescript-wearos-sensors/node"; export class DeviceListViewModel extends Observable { @@ -29,32 +29,31 @@ export class DeviceListViewModel extends Observable { this.notifyPropertyChange("nodes", this.nodes); this.notifyPropertyChange("scanning", this.scanning); - nodeDiscoverer.getConnectedNodes().subscribe({ - next: (nodeDiscovered: NodeDiscovered) => { - if (nodeDiscovered.error) { - this.logger.logResult(nodeDiscovered.error); - return; - } + nodeDiscoverer.getConnectedNodes() + .then((nodesDiscovered) => { + nodesDiscovered.forEach((nodeDiscovered) => { + if (nodeDiscovered.error) { + this.logger.logResult(nodeDiscovered.error); + return; + } - const node = nodeDiscovered.node; - this.logger.logResult(`Connected node --> ${JSON.stringify(node)}`); + const node = nodeDiscovered.node; + this.logger.logResult(`Connected node --> ${JSON.stringify(node)}`); - const sensorsAvailability = {}; - node.capabilities.forEach((capability) => sensorsAvailability[capability.toLowerCase()] = true) + const sensorsAvailability = {}; + node.capabilities.forEach((capability) => sensorsAvailability[capability.toLowerCase()] = true) - this.nodes.push({ - id: node.id, - name: node.name, - sensorsAvailability: sensorsAvailability, + this.nodes.push({ + id: node.id, + name: node.name, + sensorsAvailability: sensorsAvailability, + }); }); this.notifyPropertyChange("nodes", this.nodes); - }, - complete: () => { this.logger.logInfo("Scan ended"); this.scanning = false; this.notifyPropertyChange("scanning", this.scanning); - } - }); + }); } } diff --git a/src/internal/node/discoverer/node-discoverer.android.ts b/src/internal/node/discoverer/node-discoverer.android.ts index d091b73..884d2f4 100644 --- a/src/internal/node/discoverer/node-discoverer.android.ts +++ b/src/internal/node/discoverer/node-discoverer.android.ts @@ -6,7 +6,7 @@ import { Node } from "../index"; import { CapabilityClient } from "../../communication/capabilities/android/capability-client.android"; import { getCapabilityAdvertiserResultService } from "../../communication/capabilities/android/capability-advertiser-result-service.android"; import { capabilityProtocol } from "../../communication/capabilities"; -import { EMPTY, from, Observable } from "rxjs"; +import { EMPTY, firstValueFrom, from, Observable, toArray } from "rxjs"; import { catchError, switchMap } from "rxjs/operators"; export class AndroidNodeDiscoverer implements NodeDiscoverer { @@ -37,7 +37,14 @@ export class AndroidNodeDiscoverer implements NodeDiscoverer { }); } - public getConnectedNodes(): Observable { + public getConnectedNodes(timeout: number = 5000): Promise { + return firstValueFrom( + this.scanConnectedNodes(timeout) + .pipe(toArray()) + ); + } + + private scanConnectedNodes(timeout: number): Observable { const connectedNodes = this.nodeClient.getConnectedNodes(); const connectedNodesPromise = new Promise>((resolve, reject) => { @@ -68,7 +75,7 @@ export class AndroidNodeDiscoverer implements NodeDiscoverer { nativeNode.getDisplayName(), ); - this.capabilityClient.sendCapabilityAdvertisementRequest(node) + this.capabilityClient.sendCapabilityAdvertisementRequest(node, timeout) .then((capabilityResult) => { node.capabilities = capabilityResult.capabilities; subscriber.next({ @@ -87,7 +94,7 @@ export class AndroidNodeDiscoverer implements NodeDiscoverer { }); } }); - }), catchError((err => EMPTY)) + }), catchError((() => EMPTY)) ); } } diff --git a/src/internal/node/discoverer/node-discoverer.ts b/src/internal/node/discoverer/node-discoverer.ts index ca14de0..cdfdcee 100644 --- a/src/internal/node/discoverer/node-discoverer.ts +++ b/src/internal/node/discoverer/node-discoverer.ts @@ -1,9 +1,8 @@ import { Node } from "../index"; -import { Observable } from "rxjs"; export interface NodeDiscoverer { getLocalNode(): Promise; - getConnectedNodes(): Observable; + getConnectedNodes(timeout?: number): Promise; } export interface NodeDiscovered { From 531e92798dbc4566131854758862ed698923c55b Mon Sep 17 00:00:00 2001 From: Miguel Matey Sanz Date: Wed, 22 Jun 2022 13:06:43 +0200 Subject: [PATCH 03/31] test: update tests with new connected nodes API --- .../android/capability-client.android.spec.ts | 6 +- .../node-discoverer.android.spec.ts | 72 +++++++++---------- 2 files changed, 38 insertions(+), 40 deletions(-) diff --git a/demo/app/tests/internal/communication/capabilities/android/capability-client.android.spec.ts b/demo/app/tests/internal/communication/capabilities/android/capability-client.android.spec.ts index b0aadb2..c03290e 100644 --- a/demo/app/tests/internal/communication/capabilities/android/capability-client.android.spec.ts +++ b/demo/app/tests/internal/communication/capabilities/android/capability-client.android.spec.ts @@ -21,7 +21,7 @@ describe("Capability client", () => { }) it("sends a capability advertisement request to a node and returns a response", async () => { - const response = capabilityClient.sendCapabilityAdvertisementRequest(node); + const response = capabilityClient.sendCapabilityAdvertisementRequest(node, 1000); capabilityAdvertiserService.onMessageReceived( buildFakeMessageEvent( node.id, @@ -39,8 +39,8 @@ describe("Capability client", () => { it("sends a capability advertisement request to a node but it not responds", async () => { jasmine.clock().install(); - const response = capabilityClient.sendCapabilityAdvertisementRequest(node); - jasmine.clock().tick(5000); + const response = capabilityClient.sendCapabilityAdvertisementRequest(node, 500); + jasmine.clock().tick(500); await expectAsync(response).toBeRejectedWith(`Timeout for communication request in node ${node.name} (${node.id})`); diff --git a/demo/app/tests/internal/node/discoverer/node-discoverer.android.spec.ts b/demo/app/tests/internal/node/discoverer/node-discoverer.android.spec.ts index f0a61bd..074818a 100644 --- a/demo/app/tests/internal/node/discoverer/node-discoverer.android.spec.ts +++ b/demo/app/tests/internal/node/discoverer/node-discoverer.android.spec.ts @@ -6,7 +6,8 @@ import { CapabilityAdvertisementResult } from "nativescript-wearos-sensors/inter import { AndroidNodeDiscoverer } from "nativescript-wearos-sensors/internal/node/discoverer/node-discoverer.android"; import { buildFakeNode } from "~/tests/internal/index.spec"; import { SensorType } from "nativescript-wearos-sensors/internal/sensors/sensor-type"; -import { toArray, isEmpty } from "rxjs/operators"; + +const TIMEOUT = 100; describe("Node discoverer", () => { @@ -31,7 +32,7 @@ describe("Node discoverer", () => { nodeDiscoverer = new AndroidNodeDiscoverer(capabilityClient, nodeClient); }) - it("returns an empty list when there are no connected nodes",(done) => { + it("returns an empty list when there are no connected nodes",async () => { const fakeConnectedNodesResult = buildFakeConnectedNodesResult(true, []); const fakeConnectedNodesRequest = buildFakeConnectedNodesRequest(fakeConnectedNodesResult); @@ -40,23 +41,20 @@ describe("Node discoverer", () => { ); spyOn(capabilityClient, "sendCapabilityAdvertisementRequest").and.callThrough(); - nodeDiscoverer.getConnectedNodes().pipe(isEmpty()).subscribe((res) => { - expect(res).toBeTrue(); - done(); - }); + const connectedNodesPromise = nodeDiscoverer.getConnectedNodes(TIMEOUT); fakeConnectedNodesRequest.complete(); + const connectedNodes = await connectedNodesPromise; + expect(connectedNodes.length).toBe(0); }); - it("throws an error when the connected nodes request has been not successful", (done) => { + it("returns an empty list when the connected nodes request has been not successful", async () => { spyOn(nodeClient, "getConnectedNodes").and.rejectWith(); - nodeDiscoverer.getConnectedNodes().pipe(isEmpty()).subscribe((res) => { - expect(res).toBeTrue(); - done(); - }); + const nodesDiscovered = await nodeDiscoverer.getConnectedNodes(); + expect(nodesDiscovered.length).toBe(0); }); - it("given two connected nodes which advertise capabilities, returns a list with them", (done) => { + it("given two connected nodes which advertise capabilities, returns a list with them", async () => { const fakeConnectedNodesResult = buildFakeConnectedNodesResult(true, [ buildFakeNode(node1.id, node1.name, true), buildFakeNode(node2.id, node2.name, true) @@ -66,22 +64,22 @@ describe("Node discoverer", () => { fakeConnectedNodesRequest ); spyOn(capabilityClient, "sendCapabilityAdvertisementRequest") - .withArgs(node1).and.returnValue(Promise.resolve({ nodeId: node1.id, capabilities: node1Capabilities})) - .withArgs(node2).and.returnValue(Promise.resolve({ nodeId: node2.id, capabilities: node2Capabilities})); - - nodeDiscoverer.getConnectedNodes().pipe(toArray()).subscribe((connectedNodes) => { - expect(capabilityClient.sendCapabilityAdvertisementRequest).toHaveBeenCalledTimes(2); - expect(connectedNodes.length).toBe(2); - expect(connectedNodes[0].node).toEqual(node1WithCapabilities); - expect(connectedNodes[0].error).toBeUndefined(); - expect(connectedNodes[1].node).toEqual(node2WithCapabilities); - expect(connectedNodes[1].error).toBeUndefined(); - done(); - }); + .withArgs(node1, TIMEOUT).and.returnValue(Promise.resolve({ nodeId: node1.id, capabilities: node1Capabilities})) + .withArgs(node2, TIMEOUT).and.returnValue(Promise.resolve({ nodeId: node2.id, capabilities: node2Capabilities})); + + const connectedNodesPromise = nodeDiscoverer.getConnectedNodes(TIMEOUT); fakeConnectedNodesRequest.complete(); + const connectedNodes = await connectedNodesPromise; + + expect(capabilityClient.sendCapabilityAdvertisementRequest).toHaveBeenCalledTimes(2); + expect(connectedNodes.length).toBe(2); + expect(connectedNodes[0].node).toEqual(node1WithCapabilities); + expect(connectedNodes[0].error).toBeUndefined(); + expect(connectedNodes[1].node).toEqual(node2WithCapabilities); + expect(connectedNodes[1].error).toBeUndefined(); }); - it("given two connected nodes which only one of them advertise capabilities, returns a list with that node", (done) => { + it("given two connected nodes which only one of them advertise capabilities, returns a list with that node", async () => { const fakeConnectedNodesResult = buildFakeConnectedNodesResult(true, [ buildFakeNode(node1.id, node1.name, true), buildFakeNode(node3.id, node3.name, true) @@ -91,19 +89,19 @@ describe("Node discoverer", () => { fakeConnectedNodesRequest ); spyOn(capabilityClient, "sendCapabilityAdvertisementRequest") - .withArgs(node1).and.returnValue(Promise.resolve({ nodeId: node1.id, capabilities: node1Capabilities})) - .withArgs(node3).and.returnValue(Promise.reject("TEST: timeout for capability advertisement response")); - - nodeDiscoverer.getConnectedNodes().pipe(toArray()).subscribe((connectedNodes) => { - expect(capabilityClient.sendCapabilityAdvertisementRequest).toHaveBeenCalledTimes(2); - expect(connectedNodes.length).toBe(2); - expect(connectedNodes[0].node).toEqual(node1WithCapabilities); - expect(connectedNodes[0].error).toBeUndefined(); - expect(connectedNodes[1].node).toEqual(node3); - expect(connectedNodes[1].error).toEqual("TEST: timeout for capability advertisement response"); - done(); - }); + .withArgs(node1, TIMEOUT).and.returnValue(Promise.resolve({ nodeId: node1.id, capabilities: node1Capabilities})) + .withArgs(node3, TIMEOUT).and.returnValue(Promise.reject("TEST: timeout for capability advertisement response")); + + const connectedNodesPromise = nodeDiscoverer.getConnectedNodes(TIMEOUT); fakeConnectedNodesRequest.complete(); + const connectedNodes = await connectedNodesPromise; + + expect(capabilityClient.sendCapabilityAdvertisementRequest).toHaveBeenCalledTimes(2); + expect(connectedNodes.length).toBe(2); + expect(connectedNodes[0].node).toEqual(node1WithCapabilities); + expect(connectedNodes[0].error).toBeUndefined(); + expect(connectedNodes[1].node).toEqual(node3); + expect(connectedNodes[1].error).toEqual("TEST: timeout for capability advertisement response"); }); }) From 0a14da9a91b375d74e19be518405dc40ad4a6e6d Mon Sep 17 00:00:00 2001 From: Miguel Matey Sanz Date: Wed, 22 Jun 2022 13:10:35 +0200 Subject: [PATCH 04/31] chore(demo): remove unused rxjs dependency --- demo/package-lock.json | 33 ++------------------------------- demo/package.json | 3 +-- 2 files changed, 3 insertions(+), 33 deletions(-) diff --git a/demo/package-lock.json b/demo/package-lock.json index 637cb11..b643757 100644 --- a/demo/package-lock.json +++ b/demo/package-lock.json @@ -11,8 +11,7 @@ "@nativescript/unit-test-runner": "~3.0.0", "nativescript-drop-down": "^6.0.0", "nativescript-task-dispatcher": "^3.1.1", - "nativescript-wearos-sensors": "file:../src", - "rxjs": "^7.5.5" + "nativescript-wearos-sensors": "file:../src" }, "devDependencies": { "@jsdevtools/coverage-istanbul-loader": "3.0.5", @@ -29,7 +28,7 @@ }, "../src": { "name": "nativescript-wearos-sensors", - "version": "1.0.1", + "version": "3.0.0", "license": "Apache-2.0", "dependencies": { "nativescript-task-dispatcher": "^3.1.1", @@ -5062,19 +5061,6 @@ "queue-microtask": "^1.2.2" } }, - "node_modules/rxjs": { - "version": "7.5.5", - "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-7.5.5.tgz", - "integrity": "sha512-sy+H0pQofO95VDmFLzyaw9xNJU4KTRSwQIGM6+iG3SypAtCiLDzpeG8sJrNCWn2Up9km+KhkvTdbkrdy+yzZdw==", - "dependencies": { - "tslib": "^2.1.0" - } - }, - "node_modules/rxjs/node_modules/tslib": { - "version": "2.4.0", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.0.tgz", - "integrity": "sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==" - }, "node_modules/safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", @@ -10196,21 +10182,6 @@ "queue-microtask": "^1.2.2" } }, - "rxjs": { - "version": "7.5.5", - "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-7.5.5.tgz", - "integrity": "sha512-sy+H0pQofO95VDmFLzyaw9xNJU4KTRSwQIGM6+iG3SypAtCiLDzpeG8sJrNCWn2Up9km+KhkvTdbkrdy+yzZdw==", - "requires": { - "tslib": "^2.1.0" - }, - "dependencies": { - "tslib": { - "version": "2.4.0", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.0.tgz", - "integrity": "sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==" - } - } - }, "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", diff --git a/demo/package.json b/demo/package.json index 369c935..5c77156 100644 --- a/demo/package.json +++ b/demo/package.json @@ -7,9 +7,8 @@ "@nativescript/theme": "~2.3.0", "@nativescript/unit-test-runner": "~3.0.0", "nativescript-drop-down": "^6.0.0", - "nativescript-wearos-sensors": "file:../src", "nativescript-task-dispatcher": "^3.1.1", - "rxjs": "^7.5.5" + "nativescript-wearos-sensors": "file:../src" }, "devDependencies": { "@jsdevtools/coverage-istanbul-loader": "3.0.5", From 8f1d6868603d5f39d01c3f3f143e4c3d4b7f8c4c Mon Sep 17 00:00:00 2001 From: Miguel Matey Sanz Date: Wed, 22 Jun 2022 14:41:40 +0200 Subject: [PATCH 05/31] feat: util functions for bridge app --- src/internal/setup/index.ts | 24 ++++++++++++++++++++++++ src/setup/index.ts | 1 + 2 files changed, 25 insertions(+) create mode 100644 src/internal/setup/index.ts create mode 100644 src/setup/index.ts diff --git a/src/internal/setup/index.ts b/src/internal/setup/index.ts new file mode 100644 index 0000000..b121fb7 --- /dev/null +++ b/src/internal/setup/index.ts @@ -0,0 +1,24 @@ +import { getNodeDiscoverer } from "../node/discoverer"; +import { Utils } from "@nativescript/core"; + +export async function isBridgeWearOsAppInstalled(): Promise { + const nodeDiscoverer = getNodeDiscoverer(); + + try { + await nodeDiscoverer.getLocalNode(); + return true; + } catch (ex) { + return false; + } +} + +export function openStoreToInstallBridgeApp() { + const context = Utils.android.getApplicationContext(); + const intent = new android.content.Intent( + android.content.Intent.ACTION_VIEW, + android.net.Uri.parse("market://details?id=com.google.android.wearable.app") + ); + intent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK); + + context.startActivity(intent); +} \ No newline at end of file diff --git a/src/setup/index.ts b/src/setup/index.ts new file mode 100644 index 0000000..db7d45e --- /dev/null +++ b/src/setup/index.ts @@ -0,0 +1 @@ +export { isBridgeWearOsAppInstalled, openStoreToInstallBridgeApp } from '../internal/setup'; \ No newline at end of file From 16404e44e7616d0adbb854c4d7e5cca4be2dfc1b Mon Sep 17 00:00:00 2001 From: Miguel Matey Sanz Date: Thu, 23 Jun 2022 09:55:28 +0200 Subject: [PATCH 06/31] refactor(demo): use plugin API instead of tasks --- demo/app/home/device/device-view-model.ts | 80 ++++++++++++----------- 1 file changed, 43 insertions(+), 37 deletions(-) diff --git a/demo/app/home/device/device-view-model.ts b/demo/app/home/device/device-view-model.ts index 7fafa0e..697eb39 100644 --- a/demo/app/home/device/device-view-model.ts +++ b/demo/app/home/device/device-view-model.ts @@ -5,7 +5,6 @@ import { Node } from "nativescript-wearos-sensors/node"; import { CollectorManager, PrepareError, SensorDelay } from "nativescript-wearos-sensors/collection"; import { getSensorCollector, SensorType } from "nativescript-wearos-sensors/sensors"; import { wearosSensors } from "nativescript-wearos-sensors"; -import { pascalCase } from "nativescript-wearos-sensors/internal/utils/strings"; export class DeviceViewModel extends Observable { private logger; @@ -39,6 +38,8 @@ export class DeviceViewModel extends Observable { private batchSize = 50; + private listeners = new Map(); + constructor( private node: Node ) { @@ -54,7 +55,7 @@ export class DeviceViewModel extends Observable { message: this.status["availableInDevice"], iconColorBg: this.bgColors["availableInDevice"] }, - icon: this.iconForSensor(sensor), + icon: iconForSensor(sensor), }; }); } @@ -137,26 +138,31 @@ export class DeviceViewModel extends Observable { } private handleOnStartTap(sensorDescription: SensorDescription) { - wearosSensors.emitEvent( - `start${pascalCase(sensorDescription.sensor)}Command`, + const collector = sensorDescription.collector; + const listener = collector.listenSensorUpdates((sensorCallback) => { + const records = sensorCallback.records; + const deviceId = records[0].deviceId; + getLogger().logResultForNode(deviceId, JSON.stringify(records)); + }); + this.listeners.set(sensorDescription.sensor, listener); + collector.startCollecting( + this.node, { - deviceId: this.node.id, - config: { - sensorDelay: this.sensorDelays.getValue(this.selectedDelayIndex), - batchSize: this.batchSize - } + sensorInterval: this.sensorDelays.getValue(this.selectedDelayIndex), + batchSize: this.batchSize } ); + this.updateSensorDescriptionStatus(sensorDescription, Status.LISTENING); } private handleOnStopTap(sensorDescription: SensorDescription) { - wearosSensors.emitEvent( - `stop${pascalCase(sensorDescription.sensor)}Command`, - { - deviceId: this.node.id - } - ); + const collector = sensorDescription.collector; + const listener = this.listeners.get(sensorDescription.sensor); + this.listeners.delete(sensorDescription.sensor); + collector.stopListenSensorUpdates(listener); + collector.stopCollecting(this.node); + this.updateSensorDescriptionStatus(sensorDescription, Status.READY); } @@ -169,28 +175,6 @@ export class DeviceViewModel extends Observable { } this.repeater.refresh(); } - - private iconForSensor(sensor): string { - let icon; - switch (sensor) { - case SensorType.ACCELEROMETER: - icon = "e89f"; - break; - case SensorType.GYROSCOPE: - icon = "e84d"; - break; - case SensorType.MAGNETOMETER: - icon = "e87a"; - break; - case SensorType.LOCATION: - icon = "e0c8"; - break; - case SensorType.HEART_RATE: - icon = "e87d"; - } - - return String.fromCharCode(parseInt(icon, 16)); - } } interface SensorDescription { @@ -214,3 +198,25 @@ enum Status { READY = "ready", LISTENING = "listening" } + +function iconForSensor(sensor: SensorType): string { + let icon; + switch (sensor) { + case SensorType.ACCELEROMETER: + icon = "e89f"; + break; + case SensorType.GYROSCOPE: + icon = "e84d"; + break; + case SensorType.MAGNETOMETER: + icon = "e87a"; + break; + case SensorType.LOCATION: + icon = "e0c8"; + break; + case SensorType.HEART_RATE: + icon = "e87d"; + } + + return String.fromCharCode(parseInt(icon, 16)); +} From 0f6ffc201ef51a66a6a2bfe77c3ad22836e27c2a Mon Sep 17 00:00:00 2001 From: Miguel Matey Sanz Date: Thu, 23 Jun 2022 10:05:02 +0200 Subject: [PATCH 07/31] refactor: allow custom value for sensors' reporting interval --- demo/app/home/device/device-page.xml | 37 +++++++++++-------- demo/app/home/device/device-view-model.ts | 15 ++++---- src/collection/index.ts | 5 ++- .../collection/collection-configuration.ts | 33 ++++++++++++----- 4 files changed, 57 insertions(+), 33 deletions(-) diff --git a/demo/app/home/device/device-page.xml b/demo/app/home/device/device-page.xml index 06c9a6f..6676518 100644 --- a/demo/app/home/device/device-page.xml +++ b/demo/app/home/device/device-page.xml @@ -7,46 +7,51 @@ + /> + /> - - - - - + + - + - + backgroundColor="{{ $value.status.iconColorBg }}" horizontalAlignment="center" verticalAlignment="center"/> +