-
Notifications
You must be signed in to change notification settings - Fork 2
/
heart-rate.component.ts
executable file
·120 lines (105 loc) · 4.15 KB
/
heart-rate.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import {Component, OnInit, OnDestroy, Input, ChangeDetectionStrategy, ChangeDetectorRef} from "@angular/core";
import { Observable as RxObservable, Subscription, BehaviorSubject } from "rxjs/Rx";
import "rxjs/add/operator/map";
import 'rxjs/add/operator/do';
import {NgZone} from "@angular/core";
// app
import {MicrosoftBandService} from '@xmlking/nativescript-ngx-microsoftband';
import {View} from "ui/core/view";
@Component({
selector: 'heart-rate',
template: `
<StackLayout minHeight="100" class="red" [row]="row" [col]="col" (tap)="toggle(iconLabel)" >
<Label row="1" [text]="errorMsg" horizontalAlignment="center" class="text-muted" textWrap="true"></Label>
<Label row="2" text="\uf004" horizontalAlignment="center" class="fa h2" #iconLabel></Label>
<Label row="3" [text]="heartRate?.heartRate" horizontalAlignment="center" class="font-weight-bold"></Label>
<Label row="4" [text]="quality" horizontalAlignment="center" class="font-weight-normal"></Label>
<Label row="5" [text]="rrInterval?.interval" horizontalAlignment="center" class="font-weight-normal"></Label>
</StackLayout>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class HeartRateComponent implements OnInit, OnDestroy {
@Input() connected: boolean;
@Input() row: number;
@Input() col: number;
private hrSub: Subscription;
private rrSub: Subscription;
public heartRate: HeartRateData;
public rrInterval: RRIntervalData;
public errorMsg: string;
public started: boolean = false;
get quality() {
if(this.heartRate === undefined) return "Unknown";
switch(this.heartRate.quality) {
case Quality.Acquiring: return "Acquiring";
case Quality.Locked: return "Locked";
default: return "Acquiring";
}
}
constructor(private zone: NgZone, private cd: ChangeDetectorRef, private msband: MicrosoftBandService) {
}
ngOnInit() {
console.log("HeartRateComponent: ngOnInit: connected?", this.connected);
this.startSensorUpdates()
};
ngOnDestroy() {
console.log("HeartRateComponent: ngOnDestroy: ");
this.stopSensorUpdates();
}
public toggle(target: View) {
if(this.started) {
this.stopSensorUpdates();
target.animate({rotate: 180, duration: 500})
} else {
this.startSensorUpdates();
target.animate({rotate: 360, duration: 500})
}
}
private startSensorUpdates() {
this.msband.requestUserConsent((isGranted) => {
if (isGranted) {
this.hrSub = this.msband.heartrate$.subscribe(
(data: HeartRateData) => {
this.zone.run(() => {
this.heartRate = data;
this.cd.markForCheck();
});
},
(error: NSError) => {
console.log('Error: HeartRateData', error);
this.zone.run(() => {
this.errorMsg = error.localizedDescription;
this.cd.markForCheck();
});
}
);
this.rrSub = this.msband.rrInterval$.subscribe(
(data: RRIntervalData) => {
this.zone.run(() => {
this.rrInterval = data;
this.cd.markForCheck();
});
},
(error: NSError) => {
console.log('Error: RRIntervalData', error);
this.zone.run(() => {
this.errorMsg = error.localizedDescription;
this.cd.markForCheck();
});
}
);
}
this.started = true;
})
}
private stopSensorUpdates() {
if(this.hrSub) {
this.hrSub.unsubscribe();
}
if(this.rrSub) {
this.rrSub.unsubscribe();
}
this.started = false;
}
}