-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluetooth.health.ts
51 lines (46 loc) · 1.51 KB
/
bluetooth.health.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
import {
HealthCheckError,
HealthIndicator,
HealthIndicatorResult,
} from '@nestjs/terminus';
import { Injectable, Optional } from '@nestjs/common';
import { HealthIndicatorService } from '../../status/health-indicator.service';
import { BluetoothService } from './bluetooth.service';
@Injectable()
export class BluetoothHealthIndicator extends HealthIndicator {
constructor(
private bluetoothService: BluetoothService,
@Optional() healthIndicatorService?: HealthIndicatorService
) {
super();
healthIndicatorService?.registerHealthIndicator(async () =>
this.lowEnergyAdvertisementTimeoutCheck(2 * 60 * 1000)
);
healthIndicatorService?.registerHealthIndicator(async () =>
this.successiveErrorCheck(3)
);
}
lowEnergyAdvertisementTimeoutCheck(threshold: number): HealthIndicatorResult {
const isHealthy = this.bluetoothService.timeSinceLastDiscovery < threshold;
const result = this.getStatus('ble_advertisement_timeout', isHealthy);
if (isHealthy) {
return result;
}
throw new HealthCheckError(
`No BLE advertisements recorded in over ${threshold}ms`,
result
);
}
successiveErrorCheck(threshold: number): HealthIndicatorResult {
const isHealthy =
this.bluetoothService.successiveErrorsOccurred < threshold;
const result = this.getStatus(`bt_successive_errors`, isHealthy);
if (isHealthy) {
return result;
}
throw new HealthCheckError(
'BT Classic successive error check failed',
result
);
}
}