-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluetooth-classic.service.ts
420 lines (381 loc) · 12.8 KB
/
bluetooth-classic.service.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import {
Injectable,
Logger,
OnApplicationBootstrap,
OnModuleInit,
} from '@nestjs/common';
import { ConfigService } from '../../config/config.service';
import { EntitiesService } from '../../entities/entities.service';
import { ClusterService } from '../../cluster/cluster.service';
import { BluetoothClassicConfig } from './bluetooth-classic.config';
import * as util from 'util';
import { exec } from 'child_process';
import { Node } from 'democracy';
import { NewRssiEvent } from './new-rssi.event';
import _ from 'lodash';
import { SchedulerRegistry } from '@nestjs/schedule';
import { EntityCustomization } from '../../entities/entity-customization.interface';
import { SensorConfig } from '../home-assistant/sensor-config';
import {
NEW_RSSI_CHANNEL,
REQUEST_RSSI_CHANNEL,
} from './bluetooth-classic.const';
import { RoomPresenceDistanceSensor } from '../room-presence/room-presence-distance.sensor';
import { KalmanFilterable } from '../../util/filters';
import { makeId } from '../../util/id';
import { Device } from './device';
import { DISTRIBUTED_DEVICE_ID } from '../home-assistant/home-assistant.const';
import { Switch } from '../../entities/switch';
import { SwitchConfig } from '../home-assistant/switch-config';
import { DeviceTracker } from '../../entities/device-tracker';
import { RoomPresenceProxyHandler } from '../room-presence/room-presence.proxy';
import { BluetoothService } from '../bluetooth/bluetooth.service';
const execPromise = util.promisify(exec);
@Injectable()
export class BluetoothClassicService
extends KalmanFilterable(Object, 1.4, 1)
implements OnModuleInit, OnApplicationBootstrap {
private readonly config: BluetoothClassicConfig;
private rotationOffset = 0;
private inquiriesSwitch: Switch;
private deviceMap = new Map<string, Device>();
private logger: Logger;
constructor(
private readonly bluetoothService: BluetoothService,
private readonly configService: ConfigService,
private readonly entitiesService: EntitiesService,
private readonly clusterService: ClusterService,
private readonly schedulerRegistry: SchedulerRegistry
) {
super();
this.config = this.configService.get('bluetoothClassic');
this.logger = new Logger(BluetoothClassicService.name);
}
/**
* Lifecycle hook, called once the host module has been initialized.
*/
async onModuleInit(): Promise<void> {
try {
await execPromise('hcitool -h');
} catch (e) {
throw new Error('Could not find the required hcitool command');
}
}
/**
* Lifecycle hook, called once the application has started.
*/
onApplicationBootstrap(): void {
this.inquiriesSwitch = this.createInquiriesSwitch();
this.clusterService.on(
REQUEST_RSSI_CHANNEL,
this.handleRssiRequest.bind(this)
);
this.clusterService.on(NEW_RSSI_CHANNEL, this.handleNewRssi.bind(this));
this.clusterService.subscribe(NEW_RSSI_CHANNEL);
const interval = setInterval(
this.distributeInquiries.bind(this),
this.config.interval * 1000
);
this.schedulerRegistry.addInterval(
`bluetooth_classic_distribute_inquiries`,
interval
);
}
/**
* Takes a Bluetooth MAC address, determines the current RSSI and then publishes it across the cluster.
*
* @param address - Bluetooth MAC address
*/
async handleRssiRequest(address: string): Promise<void> {
if (_.isEmpty(address)) {
this.logger.error('RSSI update for empty address requested');
return;
}
if (this.shouldInquire()) {
let rssi;
try {
rssi = await this.bluetoothService.inquireClassicRssi(
this.config.hciDeviceId,
address
);
} catch (e) {
this.logger.error(
`Failed to retrieve RSSI for ${address}: ${e.message}`
);
}
if (rssi !== undefined) {
rssi = _.round(this.filterRssi(address, rssi), 1);
let device: Device;
if (this.deviceMap.has(address)) {
device = this.deviceMap.get(address);
} else {
device = await this.bluetoothService.inquireClassicDeviceInfo(
this.config.hciDeviceId,
address
);
this.deviceMap.set(address, device);
}
let minRssi: number;
if (_.isObject(this.config.minRssi)) {
minRssi = _.defaultTo(
this.config.minRssi[address],
this.config.minRssi.default
);
} else if (_.isNumber(this.config.minRssi)) {
minRssi = this.config.minRssi;
}
const event = new NewRssiEvent(
this.configService.get('global').instanceName,
device,
rssi,
rssi < minRssi
);
this.clusterService.publish(NEW_RSSI_CHANNEL, event);
this.handleNewRssi(event);
}
}
}
/**
* Processes an event with a new RSSI event by passing it along to the corresponding to sensor.
* If no sensor matching the given address was found it will create a new one.
*
* @param event - Event that contains a new RSSI value
*/
async handleNewRssi(event: NewRssiEvent): Promise<void> {
this.logger.debug(
`Received RSSI of ${event.rssi} for ${event.device.address} from ${event.instanceName}`
);
const sensorId = makeId(`bluetooth-classic ${event.device.address}`);
let sensor: RoomPresenceDistanceSensor;
if (this.entitiesService.has(sensorId)) {
sensor = this.entitiesService.get(sensorId) as RoomPresenceDistanceSensor;
} else {
sensor = await this.createSensor(sensorId, event.device);
}
sensor.timeout = this.calculateCurrentTimeout();
sensor.handleNewDistance(
event.instanceName,
event.rssi * -1,
event.outOfRange
);
}
/**
* Sends out RSSI requests to the connected nodes in the cluster by matching each one with a MAC address.
* Rotates the mapping on each call.
*/
distributeInquiries(): void {
if (this.clusterService.isMajorityLeader()) {
const nodes = this.getParticipatingNodes();
const addresses = [...this.config.addresses];
if (this.rotationOffset >= Math.max(nodes.length, addresses.length)) {
this.rotationOffset = 0;
}
const [nodeSubset, addressSubset]: [Node[], string[]] = this.mapArrays(
nodes,
addresses,
this.rotationOffset
);
nodeSubset.forEach((node, index) => {
if (addressSubset[index] == null) {
this.logger.error(
`Trying to request inquiry without MAC! Current index: ${this.rotationOffset}. Addresses in this round: ${addressSubset}. Addresses overall: ${addresses}.`
);
}
// only remote nodes have a timestamp of last contact attached
if (node.last === undefined) {
this.handleRssiRequest(addressSubset[index]);
} else {
this.clusterService.send(
REQUEST_RSSI_CHANNEL,
addressSubset[index],
node.id
);
}
});
this.rotationOffset++;
}
}
/**
* Applies the Kalman filter based on the historic values with the same address.
*
* @param address - Bluetooth MAC address
* @param rssi - Signal strength measurement for the given address
* @returns Smoothed signal strength value
*/
filterRssi(address: string, rssi: number): number {
return this.kalmanFilter(rssi, address);
}
/**
* Updates the underlying room presence sensor state.
* Called regularly.
* May emit pseudo update events to keep state correct.
*
* @param sensor - Room presence sensor that should be update
* @param device - Data of device corresponding to the sensor
*/
updateSensorState(sensor: RoomPresenceDistanceSensor, device: Device): void {
if (!this.shouldInquire() && this.config.preserveState) {
const instanceName = this.configService.get('global').instanceName;
const previousReading = sensor.distances[instanceName];
if (
previousReading &&
Date.now() <
previousReading.lastUpdatedAt.getTime() +
this.calculateCurrentTimeout() * 1000
) {
// emit pseudo update to keep local state alive
const event = new NewRssiEvent(
instanceName,
device,
previousReading.distance * -1, // "distance" needs to be converted back to the RSSI value
previousReading.outOfRange
);
this.clusterService.publish(NEW_RSSI_CHANNEL, event);
this.handleNewRssi(event);
}
} else {
sensor.updateState();
}
}
/**
* Filters the nodes in the cluster to those who have this integration loaded.
*
* @returns List of nodes with the Bluetooth Classic integration enabled
*/
getParticipatingNodes(): Node[] {
const nodes = Object.values(this.clusterService.nodes());
return nodes.filter(
(node) =>
node.state !== 'removed' && node.channels?.includes(NEW_RSSI_CHANNEL)
);
}
/**
* Checks whether this instance should send out Bluetooth Inquiries at the moment or not.
*
* @returns Bluetooth inquiries allowed?
*/
shouldInquire(): boolean {
return this.inquiriesSwitch?.state;
}
/**
* Creates and registers a new switch as a setting for whether Bluetooth queries should be made from this device or not.
*
* @returns Registered query switch
*/
protected createInquiriesSwitch(): Switch {
const instanceName = this.configService.get('global').instanceName;
const customizations: Array<EntityCustomization<any>> = [
{
for: SwitchConfig,
overrides: {
icon: 'mdi:bluetooth-audio',
},
},
];
const inquiriesSwitch = this.entitiesService.add(
new Switch(
'bluetooth-classic-inquiries-switch',
`${instanceName} Bluetooth Inquiries`
),
customizations
) as Switch;
if (this.config.inquireFromStart) {
inquiriesSwitch.turnOn();
} else {
inquiriesSwitch.turnOff();
}
return inquiriesSwitch;
}
/**
* Creates and registers a new room presence sensor.
*
* @param sensorId - Entity ID for the new sensor
* @param device - Device information of the peripheral
* @returns Registered sensor
*/
protected async createSensor(
sensorId: string,
device: Device
): Promise<RoomPresenceDistanceSensor> {
const deviceTracker = this.createDeviceTracker(
makeId(`${sensorId}-tracker`),
device.name
);
const customizations: Array<EntityCustomization<any>> = [
{
for: SensorConfig,
overrides: {
icon: 'mdi:bluetooth',
device: {
identifiers: device.address,
name: device.name,
manufacturer: device.manufacturer,
connections: [['mac', device.address]],
viaDevice: DISTRIBUTED_DEVICE_ID,
},
},
},
];
const rawSensor = new RoomPresenceDistanceSensor(
sensorId,
`${device.name} Room Presence`,
0
);
const sensorProxy = new Proxy<RoomPresenceDistanceSensor>(
rawSensor,
new RoomPresenceProxyHandler(deviceTracker)
);
const sensor = this.entitiesService.add(
sensorProxy,
customizations
) as RoomPresenceDistanceSensor;
const interval = setInterval(
() => this.updateSensorState(sensor, device),
this.config.interval * 1000
);
this.schedulerRegistry.addInterval(`${sensorId}_timeout_check`, interval);
return sensor;
}
/**
* Creates and registers a new device tracker.
*
* @param id - Entity ID for the new device tracker
* @param name - Name for the new device tracker
* @returns Registered device tracker
*/
protected createDeviceTracker(id: string, name: string): DeviceTracker {
return this.entitiesService.add(
new DeviceTracker(id, name, true)
) as DeviceTracker;
}
/**
* Calculates the current timeout value based on the time it takes for a full rotation.
*
* @returns Timeout in seconds
*/
protected calculateCurrentTimeout(): number {
const nodes = this.getParticipatingNodes();
const addresses = this.config.addresses;
return (
Math.max(nodes.length, addresses.length) *
this.config.timeoutCycles *
this.config.interval
);
}
/**
* Maps the values of two arrays based on a numeric offset.
*
* @param a1 - The first array
* @param a2 - The second array
* @param offset - Index offset for the larger of both arrays
* @returns A tuple with subsets of both input arrays with matching size
*/
protected mapArrays(a1: any[], a2: any[], offset: number): [any[], any[]] {
const [small, large] = a1.length > a2.length ? [a2, a1] : [a1, a2];
const largeSubset = large.slice(offset, offset + small.length);
if (offset + small.length > large.length) {
largeSubset.push(...large.slice(0, small.length - largeSubset.length));
}
return a1.length > a2.length ? [largeSubset, a2] : [a1, largeSubset];
}
}