Skip to content

Commit

Permalink
refactor: rename free messaging to plain messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
matey97 committed Aug 2, 2022
1 parent 06bdbad commit b6ed1b9
Show file tree
Hide file tree
Showing 29 changed files with 401 additions and 400 deletions.
77 changes: 38 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ The plugin offers two features:

- [Sensor data collection](#sensor-data-collection) from the paired smartwatch: it can be started/stopped from the smartphone and from the smartwatch.
The smartwatch is able to start and stop the data collection thanks to the WearCommands feature.
- [FreeMessaging](#freemessaging): it allows to send and receive simple messages between both devices.
- [Plain Messaging](#plainmessaging): it allows to send and receive simple messages between both devices.

In first place, you need to initialize the plugin with a [`WearosSensorsConfig`](#wearossensorsconfig) in your app.ts (TypeScript app) or main.ts (Angular app) file:

Expand All @@ -51,12 +51,11 @@ import { platformNativeScriptDynamic } from "nativescript-angular/platform";
import { AppModule } from "./app/app.module";

// WearOSSensors import
import { wearosSensors } from "nativescript-wearos-sensors";
import { allSensors } from "nativescript-wearos-sensors/wearos-sensors.common";
import { wearosSensors, allSensors } from "nativescript-wearos-sensors";

wearosSensors.init({
sensors: allSensors,
disableFreeMessaging: false,
disablePlainMessaging: false,
disableWearCommands: false
});

Expand All @@ -66,8 +65,8 @@ Application.run({ moduleName: "app-root" });
platformNativeScriptDynamic().bootstrapModule(AppModule);
```

The initialization parameter is optional, and it allows specifying which sensors are enabled (`sensors`), and if FreeMessaging
(`disableFreeMessaging`) and WearCommands (`disableWearCommands`) features are enabled. The default configuration is
The initialization parameter is optional, and it allows specifying which sensors are enabled (`sensors`), and if PlainMessaging
(`disablePlainMessaging`) and WearCommands (`disableWearCommands`) features are enabled. The default configuration is
the one shown in the example above: all sensors and features enabled.

> **Note**: the configuration allows conditionally wire up native components with the core of the plugin. This allows
Expand Down Expand Up @@ -196,37 +195,37 @@ the collected data.
> **Warning**: the WearCommands feature must be enabled at plugin initialization.

### FreeMessaging
### PlainMessaging
With a system composed by several devices, it is important to have a way to communicate. We provide the
[`FreeMessageClient`](#freemessageclient), which allows to send and
[`PlainMessageClient`](#plainmessageclient), which allows to send and
receive string based messages. There are two types of received messages: the ones which require a response and the
ones which don't. Here's an example on how to use the messaging feature:

```typescript
import { getFreeMessageClient } from "nativescript-wearos-sensors/internal/communication/free-message";
import { getPlainMessageClient } from "src/internal/communication/plain-message";

function registerListener(): void {
// Register a listener to receive messages from the smartwatch
getFreeMessageClient().registerListener((receivedMessage) => {
getPlainMessageClient().registerListener((receivedMessage) => {
console.log(`received single message ${JSON.stringify(receivedMessage)}`);
});
}

async function sendMessage(node: Node, message: string): void {
// Send a message to the smartwatch
const freeMessage = { message: "You don't have to reply :)" };
await getFreeMessageClient().send(node, freeMessage);
const plainMessage = {message: "You don't have to reply :)"};
await getPlainMessageClient().send(node, plainMessage);
}

async function sendMessageAndWaitResponse(node: Node, message: string): void {
// Send a message to the smartwatch and wait for a response
const freeMessage = { message: "PING!"};
const receivedMessage = await getFreeMessageClient().sendExpectingResponse(node, freeMessage);
const plainMessage = {message: "PING!"};
const receivedMessage = await getPlainMessageClient().sendExpectingResponse(node, plainMessage);
console.log(`response received: ${JSON.stringify(receivedMessage)}`);
}
```

> **Warning**: the FreeMessaging feature must be enabled at plugin initialization.
> **Warning**: the PlainMessaging feature must be enabled at plugin initialization.
## API

Expand All @@ -238,18 +237,18 @@ async function sendMessageAndWaitResponse(node: Node, message: string): void {

#### [`WearosSensorsConfig`](src/wearos-sensors.common.ts)

| Property | Type | Description |
|-------------------------|----------------|----------------------------------------------------------|
| `sensors?` | `SensorType[]` | Sensors that are going to be used. Default: all sensors. |
| `disableFreeMessaging?` | `boolean` | Disable free messaging feature. Default: false. |
| `disableWearCommands?` | `boolean` | Disable wear commands feature. Default: false. |
| Property | Type | Description |
|--------------------------|----------------|----------------------------------------------------------|
| `sensors?` | `SensorType[]` | Sensors that are going to be used. Default: all sensors. |
| `disablePlainMessaging?` | `boolean` | Disable plain messaging feature. Default: false. |
| `disableWearCommands?` | `boolean` | Disable wear commands feature. Default: false. |

##### `defaultConfig`

```typescript
export const defaultConfig = {
sensors: allSensors, // Constant containing all the sensors
disableFreeMessaging: false,
disablePlainMessaging: false,
disableWearCommands: false
};
```
Expand Down Expand Up @@ -368,31 +367,31 @@ export const defaultConfig = {
| `longitude` | `number` | Longitude coordinate component. |
| `altitude` | `number` | Altitude coordinate component. |
### [`FreeMessageClient`](src/internal/communication/free-message/android/free-message-client.android.ts)
### [`PlainMessageClient`](src/internal/communication/plain-message/android/plain-message-client.android.ts)
| Function | Return type | Description |
|---------------------------------------------------------------------------------|----------------------------|-----------------------------------------------------------------------------------|
| `enabled()` | `boolean` | Returns true if the free message feature is enabled in the initial configuration. |
| `registerListener(listener: FreeMessageListener)` | `void` | Registers the listener for the feature. |
| `unregisterListener()` | `void` | Unregisters the listener for the feature. |
| `send(node: Node, freeMessage: FreeMessage)` | `Promise<void>` | Sends a message to the specified Node. |
| `sendExpectingResponse(node: Node, freeMessage: FreeMessage, timeout?: number)` | `Promise<ReceivedMessage>` | Sends a message to the specified Node and wait `timeout` ms for a response. |
| Function | Return type | Description |
|-----------------------------------------------------------------------------------|----------------------------|------------------------------------------------------------------------------------|
| `enabled()` | `boolean` | Returns true if the plain message feature is enabled in the initial configuration. |
| `registerListener(listener: PlainMessageListener)` | `void` | Registers the listener for the feature. |
| `unregisterListener()` | `void` | Unregisters the listener for the feature. |
| `send(node: Node, plainMessage: PlainMessage)` | `Promise<void>` | Sends a message to the specified Node. |
| `sendExpectingResponse(node: Node, plainMessage: PlainMessage, timeout?: number)` | `Promise<ReceivedMessage>` | Sends a message to the specified Node and wait `timeout` ms for a response. |
#### `FreeMessage`
#### `PlainMessage`
| Property | Type | Description |
|-----------------|---------------|-----------------------------------------------------------------------------------------------------------------------------------------|
| `message` | `string` | Content of the message. |
| `inResponseTo?` | `FreeMessage` | Contains the message at which the current message is responding. `undefined` means that the message is not responding to other message. |
| Property | Type | Description |
|-----------------|----------------|-----------------------------------------------------------------------------------------------------------------------------------------|
| `message` | `string` | Content of the message. |
| `inResponseTo?` | `PlainMessage` | Contains the message at which the current message is responding. `undefined` means that the message is not responding to other message. |
#### `ReceivedMessage`
| Property | Type | Description |
|----------------|---------------|--------------------------------------|
| `senderNodeId` | `string` | Id of the node who sent the message. |
| `freeMessage` | `FreeMessage` | Message received. |
| Property | Type | Description |
|----------------|----------------|--------------------------------------|
| `senderNodeId` | `string` | Id of the node who sent the message. |
| `PlainMessage` | `PlainMessage` | Message received. |
#### `FreeMessageListener`
#### `PlainMessageListener`
`(receivedMessage: ReceivedMessage) => void`
Expand Down
4 changes: 2 additions & 2 deletions demo/app/home/device-list/device-list-view-model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Observable } from "@nativescript/core";
import { getLogger } from "~/home/logger/logger-view-model";
import { getNodeDiscoverer } from "nativescript-wearos-sensors/node";
import { getFreeMessageClient } from "nativescript-wearos-sensors/internal/communication/free-message";
import { getPlainMessageClient } from "../../../../src/internal/communication/plain-message";
import { getCollectorManager } from "nativescript-wearos-sensors/internal/collection";
import { getStore } from "~/home/store";

Expand All @@ -23,7 +23,7 @@ export class DeviceListViewModel extends Observable {
getStore().addRecord(sensorRecord);
this.logger.logResultForNode(deviceId, JSON.stringify(samples));
});
getFreeMessageClient().registerListener((receivedMessage) => {
getPlainMessageClient().registerListener((receivedMessage) => {
this.logger.logInfo(`received single message ${JSON.stringify(receivedMessage)}`);
});
}
Expand Down
6 changes: 3 additions & 3 deletions demo/app/home/device/device-page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@

<StackLayout orientation="vertical">
<GridLayout rows="auto, auto" columns="*, *" class="card" height="100">
<Label row="0" col="0" colspan="2" class="bold" text="FreeMessages"/>
<Label row="0" col="0" colspan="2" class="bold" text="PlainMessages"/>
<Button row="1" col="0"
backgroundColor="deepskyblue"
tap="{{ onTestFreeMessage }}"
tap="{{ onTestPlainMessage }}"
text="Single"/>
<Button row="1" col="1"
backgroundColor="deepskyblue"
tap="{{ onTestFreeMessageWithResponse }}"
tap="{{ onTestPlainMessageWithResponse }}"
text="Single with response"/>
</GridLayout>
<GridLayout class="card" rows="auto, auto" columns="2*, *" height="80">
Expand Down
32 changes: 17 additions & 15 deletions demo/app/home/device/device-view-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { Node } from "nativescript-wearos-sensors/node";
import { getCollectorManager, PrepareError, NativeSensorDelay } from "nativescript-wearos-sensors/collection";
import { SensorType } from "nativescript-wearos-sensors/sensors";
import { getStore } from "~/home/store";
import { getFreeMessageClient, FreeMessageClient } from "nativescript-wearos-sensors/free-message";
import {
getPlainMessageClient, PlainMessageClient
} from "nativescript-wearos-sensors/plain-message";


export class DeviceViewModel extends Observable {
Expand Down Expand Up @@ -41,7 +43,7 @@ export class DeviceViewModel extends Observable {

private batchSize = 50;

private freeMessageClient: FreeMessageClient;
private plainMessageClient: PlainMessageClient;

constructor(
private node: Node
Expand All @@ -61,28 +63,28 @@ export class DeviceViewModel extends Observable {
};
});

this.freeMessageClient = getFreeMessageClient();
this.plainMessageClient = getPlainMessageClient();
}

async onTestFreeMessage() {
if (!this.freeMessageClient.enabled()) {
this.logger.logInfo(`Free messages are not enabled`);
async onTestPlainMessage() {
if (!this.plainMessageClient.enabled()) {
this.logger.logInfo(`Plain messages are not enabled`);
return;
}
const freeMessage = { message: "You don't have to reply :)"};
this.logger.logInfoForNode(this.node.name, `sending ${JSON.stringify(freeMessage)}`);
await this.freeMessageClient.send(this.node, freeMessage);
const plainMessage = { message: "You don't have to reply :)"};
this.logger.logInfoForNode(this.node.name, `sending ${JSON.stringify(plainMessage)}`);
await this.plainMessageClient.send(this.node, plainMessage);
}

async onTestFreeMessageWithResponse() {
if (!this.freeMessageClient.enabled()) {
this.logger.logInfo(`Free messages are not enabled`);
async onTestPlainMessageWithResponse() {
if (!this.plainMessageClient.enabled()) {
this.logger.logInfo(`Plain messages are not enabled`);
return;
}

const freeMessage = { message: "PING!"};
this.logger.logInfoForNode(this.node.name, `sending ${JSON.stringify(freeMessage)} and awaiting for response`);
const receivedMessage = await this.freeMessageClient.sendExpectingResponse(this.node, freeMessage);
const plainMessage = { message: "PING!"};
this.logger.logInfoForNode(this.node.name, `sending ${JSON.stringify(plainMessage)} and awaiting for response`);
const receivedMessage = await this.plainMessageClient.sendExpectingResponse(this.node, plainMessage);
this.logger.logResultForNode(this.node.name, `response received: ${JSON.stringify(receivedMessage)}`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MessagingClient } from "nativescript-wearos-sensors/internal/communicat
import { CollectorManagerImpl } from "nativescript-wearos-sensors/internal/collection/android/collector-manager-impl.android";
import { buildFakeResolutionResult } from "~/tests/internal/index.spec";
import { CollectorManager } from "nativescript-wearos-sensors/internal/collection/collector-manager";
import { allSensors } from "nativescript-wearos-sensors/wearos-sensors.common";

describe("Collector manager implementation", () => {
const node1 = new Node("node1", "node1", [SensorType.ACCELEROMETER, SensorType.GYROSCOPE]);
Expand All @@ -15,7 +16,7 @@ describe("Collector manager implementation", () => {

beforeEach(() => {
messagingClient = buildFakeMessagingClient();
collectorManager = new CollectorManagerImpl((_) => messagingClient, null);
collectorManager = new CollectorManagerImpl((_) => messagingClient, null, allSensors);
});

it("isReady returns true when a node has a determined capability and false otherwise", async() => {
Expand Down
Loading

0 comments on commit b6ed1b9

Please sign in to comment.