Skip to content

Commit

Permalink
refactor(demo): use plugin API instead of tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
matey97 committed Jun 23, 2022
1 parent 8f1d686 commit 16404e4
Showing 1 changed file with 43 additions and 37 deletions.
80 changes: 43 additions & 37 deletions demo/app/home/device/device-view-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,6 +38,8 @@ export class DeviceViewModel extends Observable {

private batchSize = 50;

private listeners = new Map<SensorType, number>();

constructor(
private node: Node
) {
Expand All @@ -54,7 +55,7 @@ export class DeviceViewModel extends Observable {
message: this.status["availableInDevice"],
iconColorBg: this.bgColors["availableInDevice"]
},
icon: this.iconForSensor(sensor),
icon: iconForSensor(sensor),
};
});
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -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 {
Expand All @@ -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));
}

0 comments on commit 16404e4

Please sign in to comment.