Skip to content

Commit

Permalink
feat(plugin): add listener for wear-triggered free messages and impro…
Browse files Browse the repository at this point in the history
…vements
  • Loading branch information
matey97 committed Jul 7, 2022
1 parent fd73075 commit 7f67c31
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 15 deletions.
12 changes: 12 additions & 0 deletions src/free-message/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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<ReceivedMessage> {
export class FreeMessageAndroidClient extends CommunicationClient<ReceivedMessage> implements FreeMessageClient {

constructor(
private protocol = freeMessageProtocol,
Expand All @@ -14,6 +15,14 @@ export class FreeMessageClient extends CommunicationClient<ReceivedMessage> {
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<void> {
const message = encodeFreeMessage(freeMessage);
await this.sendMessage(node, this.protocol.withoutResponse, message);
Expand All @@ -30,4 +39,12 @@ export class FreeMessageClient extends CommunicationClient<ReceivedMessage> {
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;
}
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -9,7 +9,8 @@ import { decodeFreeMessage } from "../encoder-decoder";
export class FreeMessageResultService implements CommunicationResultService, WearableListenerServiceDelegate {

private protocol: FreeMessageProtocol;
private resolutionCallbacks = new Map<string, (freeMessage: ReceivedMessage) => void>();
private resolutionCallbacks = new Map<string, FreeMessageListener>();
private defaultListener: FreeMessageListener;

setProtocol(protocol: CommunicationProtocol): void {
this.protocol = protocol as FreeMessageProtocol;
Expand All @@ -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) {
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<void>;
sendExpectingResponse(node: Node, freeMessage: FreeMessage, timeout?: number): Promise<ReceivedMessage>;
}
16 changes: 15 additions & 1 deletion src/internal/communication/free-message/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,4 +28,6 @@ export interface FreeMessage {
export interface ReceivedMessage {
senderNodeId: string;
freeMessage: FreeMessage;
}
}

export type FreeMessageListener = (receivedMessage: ReceivedMessage) => void;

0 comments on commit 7f67c31

Please sign in to comment.