Skip to content

Commit

Permalink
refactor: replace tasks with plugin API in commands from smartwatch
Browse files Browse the repository at this point in the history
  • Loading branch information
matey97 committed Jun 23, 2022
1 parent 3f145cd commit b7a0eb5
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 183 deletions.
18 changes: 0 additions & 18 deletions demo/app/home/tasks/command-logger-task.ts

This file was deleted.

44 changes: 0 additions & 44 deletions demo/app/home/tasks/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,6 @@ class DemoTaskGraph implements TaskGraph {
on: EventListenerGenerator,
run: RunnableTaskDescriptor
): Promise<void> {
// Commands logs
on("startAllCommand", run("commandLoggerTask"));
on("stopAllCommand", run("commandLoggerTask"));
on("startAccelerometerCommand", run("commandLoggerTask"));
on("stopAccelerometerCommand", run("commandLoggerTask"));
on("startGyroscopeCommand", run("commandLoggerTask"));
on("stopGyroscopeCommand", run("commandLoggerTask"));
on("startMagnetometerCommand", run("commandLoggerTask"));
on("stopMagnetometerCommand", run("commandLoggerTask"));
on("startHeartRateCommand", run("commandLoggerTask"));
on("stopHeartRateCommand", run("commandLoggerTask"));
on("startLocationCommand", run("commandLoggerTask"));
on("stopLocationCommand", run("commandLoggerTask"));

// Accelerometer
on("startAllCommand", run("accelerometerStartSensorTask"));
on("startAccelerometerCommand", run("accelerometerStartSensorTask"));
on("stopAccelerometerCommand", run("accelerometerStopSensorTask"));
on("stopAllCommand", run("accelerometerStopSensorTask"));

// Gyroscope
on("startAllCommand", run("gyroscopeStartSensorTask"));
on("startGyroscopeCommand", run("gyroscopeStartSensorTask"));
on("stopGyroscopeCommand", run("gyroscopeStopSensorTask"));
on("stopAllCommand", run("gyroscopeStopSensorTask"));

// Magnetometer
on("startAllCommand", run("magnetometerStartSensorTask"));
on("startMagnetometerCommand", run("magnetometerStartSensorTask"));
on("stopMagnetometerCommand", run("magnetometerStopSensorTask"));
on("stopAllCommand", run("magnetometerStopSensorTask"));

// Heart Rate
on("startAllCommand", run("heartRateStartSensorTask"));
on("startHeartRateCommand", run("heartRateStartSensorTask"));
on("stopHeartRateCommand", run("heartRateStopSensorTask"));
on("stopAllCommand", run("heartRateStopSensorTask"));

// Location
on("startAllCommand", run("locationStartSensorTask"));
on("startLocationCommand", run("locationStartSensorTask"));
on("stopLocationCommand", run("locationStopSensorTask"));
on("stopAllCommand", run("locationStopSensorTask"));

// Records logs
on("accelerometerRecordsAcquired", run("recordLoggerTask"));
on("gyroscopeRecordsAcquired", run("recordLoggerTask"));
Expand Down
2 changes: 0 additions & 2 deletions demo/app/home/tasks/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Task } from "nativescript-task-dispatcher/tasks";
import { CommandLoggerTask } from "~/home/tasks/command-logger-task";
import { RecordLoggerTask } from "~/home/tasks/record-logger-task";

export const appTasks: Array<Task> = [
new CommandLoggerTask("commandLoggerTask"),
new RecordLoggerTask("recordLoggerTask"),
]
114 changes: 84 additions & 30 deletions src/internal/communication/command/command-service.android.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,105 @@
import WearableListenerServiceDelegate = es.uji.geotec.wearos_sensors.messaging.WearableListenerServiceDelegate;
import { wearOS } from "../../utils/android/wear-os-types.android";
import { decodeMessage } from "../encoder-decoder";
import { taskDispatcher } from "nativescript-task-dispatcher";
import { camelCase } from "../../utils/strings";
import { fromString, SensorType } from "../../sensors/sensor-type";
import { getSensorCollector } from "../../sensors";
import { Node } from "../../node";
import { CollectionConfiguration } from "../../collection/collection-configuration";

const ALL_SENSORS = [
SensorType.ACCELEROMETER,
SensorType.GYROSCOPE,
SensorType.MAGNETOMETER,
SensorType.HEART_RATE,
SensorType.LOCATION,
];

export class CommandService implements WearableListenerServiceDelegate {

onMessageReceived(message: wearOS.MessageEvent): void {
async onMessageReceived(message: wearOS.MessageEvent): Promise<void> {
const path = message.getPath();

if (path !== "command") {
return;
}
if (path !== "command") return;

if (message.getData() === null) {
return;
}
if (message.getData() === null) return;

const parameters = this.extractCommandParameters(
const parameters = extractCommandParameters(
decodeMessage(message.getData())
);
taskDispatcher.emitEvent(
camelCase(parameters.commandName) + "Command",
{
deviceId: message.getSourceNodeId(),
config: parameters.config,

if (!parameters) return;

if (parameters.sensorType !== null) {
await executeAction(
message.getSourceNodeId(),
parameters.action,
parameters.sensorType,
parameters.config
);
} else {
for (const sensorType of ALL_SENSORS) {
await executeAction(
message.getSourceNodeId(),
parameters.action,
sensorType,
parameters.config
);
}
);
}
}
}

enum CommandAction {
START, STOP
}

function extractCommandParameters(command: string) {
const paramList = command.split("#");

private extractCommandParameters(command: string) {
const paramList = command.split("#");

if (paramList.length === 3) {
return {
commandName: paramList[0],
config: {
sensorDelay: paramList[1],
batchSize: paramList[2],
}
};
if (paramList.length === 0) return undefined;

const actionAndSensorType = extractActionAndSensorType(paramList[0]);

return paramList.length === 3
? { ...actionAndSensorType,
config: {
sensorInterval: parseInt(paramList[1]),
batchSize: parseInt(paramList[2]),
}
}
: { ...actionAndSensorType, config: undefined};
}

function extractActionAndSensorType(command: string) {
const actionAndSensor = command.split("-");
const action = actionAndSensor[0] === 'start' ? CommandAction.START : CommandAction.STOP;
const sensorType = fromString(actionAndSensor[1]);

return {
action: action,
sensorType: sensorType
};
}

return {
commandName: paramList[0]
};
async function executeAction(nodeId: string, action: CommandAction, sensorType: SensorType, config: CollectionConfiguration): Promise<void> {
const collector = getSensorCollector(sensorType);
const node = new Node(nodeId, "", [sensorType]);

if (action === CommandAction.STOP) {
await collector.stopCollecting(node);
return;
}

const isReady = await collector.isReady(node);
if (!isReady) {
const errors = await collector.prepare(node);
if (errors) {
console.log(`[Command Service] - prepare error: ${JSON.stringify(errors)}`);
return;
}
}

await collector.startCollecting(node, config);
}

let _instance: CommandService;
Expand Down
15 changes: 0 additions & 15 deletions src/internal/tasks/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import { Task } from "nativescript-task-dispatcher/tasks";
import { StartSensorTask } from "./start-sensor-task";
import { SensorType } from "../sensors/sensor-type";
import { StopSensorTask } from "./stop-sensor-task";
import { SendSingleMessageTask } from "./send-single-message-task";

export const internalTasks: Array<Task> = [
new StartSensorTask(SensorType.ACCELEROMETER),
new StartSensorTask(SensorType.GYROSCOPE),
new StartSensorTask(SensorType.MAGNETOMETER),
new StartSensorTask(SensorType.HEART_RATE),
new StartSensorTask(SensorType.LOCATION),

new StopSensorTask(SensorType.ACCELEROMETER),
new StopSensorTask(SensorType.GYROSCOPE),
new StopSensorTask(SensorType.MAGNETOMETER),
new StopSensorTask(SensorType.HEART_RATE),
new StopSensorTask(SensorType.LOCATION),

new SendSingleMessageTask(),
];
49 changes: 0 additions & 49 deletions src/internal/tasks/start-sensor-task.ts

This file was deleted.

25 changes: 0 additions & 25 deletions src/internal/tasks/stop-sensor-task.ts

This file was deleted.

0 comments on commit b7a0eb5

Please sign in to comment.