From 7f67c31c90ace221e348fa725ccabd90fadb700d Mon Sep 17 00:00:00 2001 From: Miguel Matey Sanz Date: Thu, 7 Jul 2022 10:46:03 +0200 Subject: [PATCH] feat(plugin): add listener for wear-triggered free messages and improvements --- src/free-message/index.ts | 12 ++++++ .../android/free-message-client.android.ts | 21 ++++++++++- .../free-message-result-service.android.ts | 37 +++++++++++++------ .../free-message/free-message-client.ts | 9 +++++ .../communication/free-message/index.ts | 16 +++++++- 5 files changed, 80 insertions(+), 15 deletions(-) create mode 100644 src/free-message/index.ts create mode 100644 src/internal/communication/free-message/free-message-client.ts diff --git a/src/free-message/index.ts b/src/free-message/index.ts new file mode 100644 index 0000000..9c2ac37 --- /dev/null +++ b/src/free-message/index.ts @@ -0,0 +1,12 @@ +export { getFreeMessageClient } from "../internal/communication/free-message"; +import { + FreeMessage as FM, + FreeMessageListener as FL, + ReceivedMessage as RM +} from "../internal/communication/free-message"; +export type FreeMessage = FM; +export type FreeMessageListener = FL; +export type ReceivedMessage = RM; + +import { FreeMessageClient as FMC } from "../internal/communication/free-message/free-message-client"; +export type FreeMessageClient = FMC; \ No newline at end of file diff --git a/src/internal/communication/free-message/android/free-message-client.android.ts b/src/internal/communication/free-message/android/free-message-client.android.ts index 3e431c8..9fabb40 100644 --- a/src/internal/communication/free-message/android/free-message-client.android.ts +++ b/src/internal/communication/free-message/android/free-message-client.android.ts @@ -1,10 +1,11 @@ import { CommunicationClient } from "../../communication-client.android"; -import { freeMessageProtocol, FreeMessage, ReceivedMessage } from "../index"; +import { freeMessageProtocol, FreeMessage, ReceivedMessage, FreeMessageListener } from "../index"; +import { FreeMessageClient } from "../free-message-client"; import { Node } from "../../../node"; import { getFreeMessageResultService } from "./free-message-result-service.android"; import { encodeFreeMessage } from "../encoder-decoder"; -export class FreeMessageClient extends CommunicationClient { +export class FreeMessageAndroidClient extends CommunicationClient implements FreeMessageClient { constructor( private protocol = freeMessageProtocol, @@ -14,6 +15,14 @@ export class FreeMessageClient extends CommunicationClient { freeMessageResultService.setProtocol(protocol); } + public registerListener(listener: FreeMessageListener): void { + this.freeMessageResultService.setDefaultListener(listener); + } + + public unregisterListener(): void { + this.freeMessageResultService.clearDefaultListener(); + } + public async send(node: Node, freeMessage: FreeMessage): Promise { const message = encodeFreeMessage(freeMessage); await this.sendMessage(node, this.protocol.withoutResponse, message); @@ -30,4 +39,12 @@ export class FreeMessageClient extends CommunicationClient { await this.sendMessage(node, this.protocol.expectingResponse, message); return await resolutionPromise; } +} + +let _instance: FreeMessageClient; +export function getFreeMessageAndroidClient(): FreeMessageClient { + if (!_instance) { + _instance = new FreeMessageAndroidClient(); + } + return _instance; } \ No newline at end of file diff --git a/src/internal/communication/free-message/android/free-message-result-service.android.ts b/src/internal/communication/free-message/android/free-message-result-service.android.ts index 4112101..ca99708 100644 --- a/src/internal/communication/free-message/android/free-message-result-service.android.ts +++ b/src/internal/communication/free-message/android/free-message-result-service.android.ts @@ -1,6 +1,6 @@ import { CommunicationResultService } from "../../communication-result-service"; import WearableListenerServiceDelegate = es.uji.geotec.wearos_sensors.messaging.WearableListenerServiceDelegate; -import { FreeMessage, FreeMessageProtocol, ReceivedMessage } from "../index"; +import { FreeMessage, FreeMessageListener, FreeMessageProtocol, ReceivedMessage } from "../index"; import { CommunicationProtocol } from "../../communication-protocol"; import { wearOS } from "../../../utils/android/wear-os-types.android"; import { decodeMessage } from "../../encoder-decoder"; @@ -9,7 +9,8 @@ import { decodeFreeMessage } from "../encoder-decoder"; export class FreeMessageResultService implements CommunicationResultService, WearableListenerServiceDelegate { private protocol: FreeMessageProtocol; - private resolutionCallbacks = new Map void>(); + private resolutionCallbacks = new Map(); + private defaultListener: FreeMessageListener; setProtocol(protocol: CommunicationProtocol): void { this.protocol = protocol as FreeMessageProtocol; @@ -19,9 +20,17 @@ export class FreeMessageResultService implements CommunicationResultService, Wea this.resolutionCallbacks.set(nodeId, callback); } + setDefaultListener(listener: FreeMessageListener): void { + this.defaultListener = listener; + } + + clearDefaultListener(): void { + this.defaultListener = undefined; + } + onMessageReceived(message: wearOS.MessageEvent): void { const path = message.getPath(); - const sourceNodeId = message.getSourceNodeId(); + const senderNodeId = message.getSourceNodeId(); // For now, smartphone only accepts without-response messages if (path !== this.protocol.withoutResponse) { @@ -34,19 +43,23 @@ export class FreeMessageResultService implements CommunicationResultService, Wea const stringMessage: string = decodeMessage(message.getData()); const freeMessage: FreeMessage = decodeFreeMessage(stringMessage); + const receivedMessage: ReceivedMessage = { + senderNodeId, + freeMessage + }; - if (freeMessage.inResponseTo && this.resolutionCallbacks.has(sourceNodeId)) { - const callback = this.resolutionCallbacks.get(sourceNodeId); - callback({ - senderNodeId: sourceNodeId, - freeMessage: freeMessage - }); - this.resolutionCallbacks.delete(sourceNodeId); + if (freeMessage.inResponseTo && this.resolutionCallbacks.has(senderNodeId)) { + const callback = this.resolutionCallbacks.get(senderNodeId); + callback(receivedMessage); + this.resolutionCallbacks.delete(senderNodeId); return; } - // TODO: listener callback - console.log(`[FreeMessageResultService]: ${JSON.stringify(freeMessage)}`); + if (!this.defaultListener) { + throw new Error(`received message ${JSON.stringify(receivedMessage)} but there are no callbacks set`); + } + + this.defaultListener(receivedMessage); } } diff --git a/src/internal/communication/free-message/free-message-client.ts b/src/internal/communication/free-message/free-message-client.ts new file mode 100644 index 0000000..644c918 --- /dev/null +++ b/src/internal/communication/free-message/free-message-client.ts @@ -0,0 +1,9 @@ +import { FreeMessage, FreeMessageListener, ReceivedMessage } from "./index"; +import { Node } from "../../node"; + +export interface FreeMessageClient { + registerListener(listener: FreeMessageListener): void; + unregisterListener(): void; + send(node: Node, freeMessage: FreeMessage): Promise; + sendExpectingResponse(node: Node, freeMessage: FreeMessage, timeout?: number): Promise; +} \ No newline at end of file diff --git a/src/internal/communication/free-message/index.ts b/src/internal/communication/free-message/index.ts index c05ca60..4912e62 100644 --- a/src/internal/communication/free-message/index.ts +++ b/src/internal/communication/free-message/index.ts @@ -1,3 +1,15 @@ +import { FreeMessageClient } from "./free-message-client"; +import { Application } from "@nativescript/core"; +import { getFreeMessageAndroidClient } from "./android/free-message-client.android"; + +export function getFreeMessageClient(): FreeMessageClient { + if (Application.android) { + return getFreeMessageAndroidClient(); + } else { + return null; + } +} + export interface FreeMessageProtocol { withoutResponse: string; expectingResponse: string; @@ -16,4 +28,6 @@ export interface FreeMessage { export interface ReceivedMessage { senderNodeId: string; freeMessage: FreeMessage; -} \ No newline at end of file +} + +export type FreeMessageListener = (receivedMessage: ReceivedMessage) => void; \ No newline at end of file