Skip to content

Commit

Permalink
refactor: allow custom value for sensors' reporting interval
Browse files Browse the repository at this point in the history
  • Loading branch information
matey97 committed Jun 23, 2022
1 parent 16404e4 commit 0f6ffc2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 33 deletions.
37 changes: 21 additions & 16 deletions demo/app/home/device/device-page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,51 @@
<ActionBar>
<Label class="title-med">
<FormattedString>
<Span text="{{ node.name }} " fontWeight="bold"></Span>
<Span text="{{ ' (' + node.id + ')' }}"></Span>
<Span text="{{ node.name }} " fontWeight="bold"/>
<Span text="{{ ' (' + node.id + ')' }}"/>
</FormattedString>
</Label>
<ActionItem
text="Store collected data"
tap="{{ onStoreData }}"
android.position="popup"
></ActionItem>
/>
<ActionItem
text="Clear collected data"
tap="{{ onClearData }}"
android.position="popup"
></ActionItem>
/>
</ActionBar>

<StackLayout orientation="vertical">
<GridLayout class="card" rows="auto, auto" columns="*, *" height="80">
<Label row="0" col="0" text="Sensor Delay: "></Label>
<dd:DropDown row="1" col="0"
items="{{ sensorDelays }}" selectedIndex="{{ selectedDelayIndex }}"
itemsTextAlignment="center" itemsPadding="5" class="drop-down"/>
<Label row="0" col="1" text="Batch size: "></Label>
<TextField row="1" col="1" text="{{ batchSize }}" keyboardType="number" textAlignment="center"></TextField>
<GridLayout class="card" rows="auto, auto" columns="2*, *" height="80">
<Label row="0" col="0" text="Sensor interval: "/>
<StackLayout row="1" col="0" orientation="horizontal">
<dd:DropDown items="{{ sensorIntervals }}" selectedIndex="{{ selectedDelayIndex }}"
itemsTextAlignment="center" itemsPadding="5" class="drop-down"/>
<Label text="or" verticalAlignment="center"/>
<TextField text="{{ customSensorInterval }}" hint="custom" style="placeholder-color: grey;"
keyboardType="number" textAlignment="center" width="50"/>
<Label text="ms" verticalAlignment="center"/>
</StackLayout>
<Label row="0" col="1" text="Batch size: "/>
<TextField row="1" col="1" text="{{ batchSize }}" keyboardType="number" textAlignment="center"/>
</GridLayout>
<ScrollView verticalAlignment="top">
<Repeater id="repeater" items="{{ sensorDescription }}" margin="10">
<Repeater.itemsLayout>
<StackLayout orientation="vertical"></StackLayout>
<StackLayout orientation="vertical"/>
</Repeater.itemsLayout>
<Repeater.itemTemplate>
<GridLayout rows="auto, auto, *" columns="1*, 3*" class="card">
<Label row="0" rowSpan="3" col="0" text="{{ $value.icon }}"
class="{{ $value.status.id, $value.status.id === 'listening' ? 'mdi-ro mdi-50 recording' : 'mdi-ro mdi-50'}}"
backgroundColor="{{ $value.status.iconColorBg }}" horizontalAlignment="center" verticalAlignment="center"></Label>
<Label row="0" col="1" text="{{ $value.sensor }}" class="title-med bold"></Label>
backgroundColor="{{ $value.status.iconColorBg }}" horizontalAlignment="center" verticalAlignment="center"/>
<Label row="0" col="1" text="{{ $value.sensor }}" class="title-med bold"/>
<Label row="1" col="1" class="bold">
<FormattedString>
<Span text="Status: "></Span>
<Span text="{{ $value.status.message }}"></Span>
<Span text="Status: "/>
<Span text="{{ $value.status.message }}"/>
</FormattedString>
</Label>
<StackLayout row="3" col="1" orientation="horizontal" horizontalAlignment="center">
Expand Down
15 changes: 8 additions & 7 deletions demo/app/home/device/device-view-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Button, Color, EventData, Observable, Repeater } from "@nativescript/co
import { getLogger } from "~/home/logger/logger-view-model";
import { ValueList } from "nativescript-drop-down";
import { Node } from "nativescript-wearos-sensors/node";
import { CollectorManager, PrepareError, SensorDelay } from "nativescript-wearos-sensors/collection";
import { CollectorManager, PrepareError, NativeSensorDelay } from "nativescript-wearos-sensors/collection";
import { getSensorCollector, SensorType } from "nativescript-wearos-sensors/sensors";
import { wearosSensors } from "nativescript-wearos-sensors";

Expand All @@ -28,11 +28,12 @@ export class DeviceViewModel extends Observable {

private repeater: Repeater;

private sensorDelays = new ValueList([
{ value: SensorDelay.NORMAL, display: "NORMAL" },
{ value: SensorDelay.UI, display: "UI" },
{ value: SensorDelay.GAME, display: "GAME" },
{ value: SensorDelay.FASTEST, display: "FASTEST" }
private customSensorInterval: number;
private sensorIntervals = new ValueList([
{ value: NativeSensorDelay.NORMAL, display: "NORMAL" },
{ value: NativeSensorDelay.UI, display: "UI" },
{ value: NativeSensorDelay.GAME, display: "GAME" },
{ value: NativeSensorDelay.FASTEST, display: "FASTEST" }
]);
private selectedDelayIndex = 0;

Expand Down Expand Up @@ -148,7 +149,7 @@ export class DeviceViewModel extends Observable {
collector.startCollecting(
this.node,
{
sensorInterval: this.sensorDelays.getValue(this.selectedDelayIndex),
sensorInterval: this.customSensorInterval ?? this.sensorIntervals.getValue(this.selectedDelayIndex),
batchSize: this.batchSize
}
);
Expand Down
5 changes: 4 additions & 1 deletion src/collection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ export type PrepareError = PE;
import { CollectionConfiguration as CC } from "../internal/collection/collection-configuration";
export type CollectionConfiguration = CC;

export { SensorDelay } from "../internal/collection/collection-configuration";
import { SensorInterval as SI } from "../internal/collection/collection-configuration";
export type SensorInterval = SI;

export { NativeSensorDelay } from "../internal/collection/collection-configuration";
33 changes: 24 additions & 9 deletions src/internal/collection/collection-configuration.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,50 @@
import {SensorType} from "../sensors/sensor-type";
import { SensorType } from "../sensors/sensor-type";

export type SensorInterval = NativeSensorDelay | number;

export interface CollectionConfiguration {
sensorDelay: SensorDelay;
sensorInterval: SensorInterval;
batchSize: number;
}

export enum SensorDelay {
export enum NativeSensorDelay {
UI = android.hardware.SensorManager.SENSOR_DELAY_UI,
NORMAL = android.hardware.SensorManager.SENSOR_DELAY_NORMAL,
GAME = android.hardware.SensorManager.SENSOR_DELAY_GAME,
FASTEST = android.hardware.SensorManager.SENSOR_DELAY_FASTEST,
}

export function configAsString(collectionConfiguration: CollectionConfiguration) {
return `${collectionConfiguration.sensorDelay}#${collectionConfiguration.batchSize}`;
}

export function defaultCollectionConfiguration(sensorType: SensorType): CollectionConfiguration {
switch (sensorType) {
case SensorType.ACCELEROMETER:
case SensorType.GYROSCOPE:
case SensorType.MAGNETOMETER:
return {
sensorDelay: SensorDelay.NORMAL,
sensorInterval: NativeSensorDelay.NORMAL,
batchSize: 50
};
case SensorType.LOCATION:
case SensorType.HEART_RATE:
return {
sensorDelay: SensorDelay.NORMAL,
sensorInterval: NativeSensorDelay.NORMAL,
batchSize: 5
};
}
}

export function configAsString(collectionConfiguration: CollectionConfiguration) {
const intervalValue = sensorIntervalToNativeValue(collectionConfiguration.sensorInterval);
return `${intervalValue}#${collectionConfiguration.batchSize}`;
}

function sensorIntervalToNativeValue(sensorInterval: SensorInterval) {
switch (sensorInterval) {
case NativeSensorDelay.UI:
case NativeSensorDelay.NORMAL:
case NativeSensorDelay.GAME:
case NativeSensorDelay.FASTEST:
return sensorInterval;
default:
return sensorInterval * 1000; // Native API expects interval in microseconds
}
}

0 comments on commit 0f6ffc2

Please sign in to comment.