From 025ceb1496e3d7613285c3db95ac65b1ba2e295f Mon Sep 17 00:00:00 2001 From: Heiko Rothe Date: Sun, 26 Jan 2020 00:30:16 +0100 Subject: [PATCH] fix(bluetooth-classic): Publish RSSI even for local inquiries The wrong method was used for handling local RSSI requests and the results were just discarded. Now they are published to the cluster like they should be. --- .../bluetooth-classic.service.spec.ts | 40 +++++++++---------- .../bluetooth-classic.service.ts | 11 +++-- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/integrations/bluetooth-classic/bluetooth-classic.service.spec.ts b/src/integrations/bluetooth-classic/bluetooth-classic.service.spec.ts index b835991..284acb0 100644 --- a/src/integrations/bluetooth-classic/bluetooth-classic.service.spec.ts +++ b/src/integrations/bluetooth-classic/bluetooth-classic.service.spec.ts @@ -191,7 +191,7 @@ describe('BluetoothClassicService', () => { 'test-instance', 10 ); - expect(sensorInstance.timeout).toBe(20); + expect(sensorInstance.timeout).toBe(30); }); it('should not distribute inquiries if not the leader', () => { @@ -208,18 +208,18 @@ describe('BluetoothClassicService', () => { abcd: { channels: [NEW_RSSI_CHANNEL] } }); clusterService.isLeader.mockReturnValue(true); - const inquireSpy = jest - .spyOn(service, 'inquireRssi') + const rssiRequestSpy = jest + .spyOn(service, 'handleRssiRequest') .mockImplementation(() => undefined); service.distributeInquiries(); - expect(inquireSpy).toHaveBeenLastCalledWith('8d:ad:e3:e2:7a:01'); + expect(rssiRequestSpy).toHaveBeenLastCalledWith('8d:ad:e3:e2:7a:01'); service.distributeInquiries(); - expect(inquireSpy).toHaveBeenLastCalledWith('f7:6c:e3:10:55:b5'); + expect(rssiRequestSpy).toHaveBeenLastCalledWith('f7:6c:e3:10:55:b5'); service.distributeInquiries(); - expect(inquireSpy).toHaveBeenLastCalledWith('8d:ad:e3:e2:7a:01'); + expect(rssiRequestSpy).toHaveBeenLastCalledWith('8d:ad:e3:e2:7a:01'); }); it('should rotate inquiries correctly when there are exactly as many addresses as nodes', () => { @@ -228,12 +228,12 @@ describe('BluetoothClassicService', () => { def: { id: 'def', channels: [NEW_RSSI_CHANNEL], last: new Date() } }); clusterService.isLeader.mockReturnValue(true); - const inquireSpy = jest - .spyOn(service, 'inquireRssi') + const handleRssiRequest = jest + .spyOn(service, 'handleRssiRequest') .mockImplementation(() => undefined); service.distributeInquiries(); - expect(inquireSpy).toHaveBeenLastCalledWith('8d:ad:e3:e2:7a:01'); + expect(handleRssiRequest).toHaveBeenLastCalledWith('8d:ad:e3:e2:7a:01'); expect(clusterService.send).toHaveBeenLastCalledWith( REQUEST_RSSI_CHANNEL, 'f7:6c:e3:10:55:b5', @@ -241,7 +241,7 @@ describe('BluetoothClassicService', () => { ); service.distributeInquiries(); - expect(inquireSpy).toHaveBeenLastCalledWith('f7:6c:e3:10:55:b5'); + expect(handleRssiRequest).toHaveBeenLastCalledWith('f7:6c:e3:10:55:b5'); expect(clusterService.send).toHaveBeenLastCalledWith( REQUEST_RSSI_CHANNEL, '8d:ad:e3:e2:7a:01', @@ -249,7 +249,7 @@ describe('BluetoothClassicService', () => { ); service.distributeInquiries(); - expect(inquireSpy).toHaveBeenLastCalledWith('8d:ad:e3:e2:7a:01'); + expect(handleRssiRequest).toHaveBeenLastCalledWith('8d:ad:e3:e2:7a:01'); expect(clusterService.send).toHaveBeenLastCalledWith( REQUEST_RSSI_CHANNEL, 'f7:6c:e3:10:55:b5', @@ -264,23 +264,23 @@ describe('BluetoothClassicService', () => { xyz: { id: 'xyz', channels: [NEW_RSSI_CHANNEL], last: new Date() } }); clusterService.isLeader.mockReturnValue(true); - const inquireSpy = jest - .spyOn(service, 'inquireRssi') + const handleRssiRequest = jest + .spyOn(service, 'handleRssiRequest') .mockImplementation(() => undefined); service.distributeInquiries(); - expect(inquireSpy).toHaveBeenLastCalledWith('8d:ad:e3:e2:7a:01'); + expect(handleRssiRequest).toHaveBeenLastCalledWith('8d:ad:e3:e2:7a:01'); expect(clusterService.send).toHaveBeenCalledWith( REQUEST_RSSI_CHANNEL, 'f7:6c:e3:10:55:b5', 'def' ); expect(clusterService.send).toHaveBeenCalledTimes(1); - inquireSpy.mockClear(); + handleRssiRequest.mockClear(); clusterService.send.mockClear(); service.distributeInquiries(); - expect(inquireSpy).not.toHaveBeenCalled(); + expect(handleRssiRequest).not.toHaveBeenCalled(); expect(clusterService.send).toHaveBeenCalledWith( REQUEST_RSSI_CHANNEL, '8d:ad:e3:e2:7a:01', @@ -292,22 +292,22 @@ describe('BluetoothClassicService', () => { 'xyz' ); expect(clusterService.send).toHaveBeenCalledTimes(2); - inquireSpy.mockClear(); + handleRssiRequest.mockClear(); clusterService.send.mockClear(); service.distributeInquiries(); - expect(inquireSpy).toHaveBeenCalledWith('f7:6c:e3:10:55:b5'); + expect(handleRssiRequest).toHaveBeenCalledWith('f7:6c:e3:10:55:b5'); expect(clusterService.send).toHaveBeenCalledWith( REQUEST_RSSI_CHANNEL, '8d:ad:e3:e2:7a:01', 'xyz' ); expect(clusterService.send).toHaveBeenCalledTimes(1); - inquireSpy.mockClear(); + handleRssiRequest.mockClear(); clusterService.send.mockClear(); service.distributeInquiries(); - expect(inquireSpy).toHaveBeenLastCalledWith('8d:ad:e3:e2:7a:01'); + expect(handleRssiRequest).toHaveBeenLastCalledWith('8d:ad:e3:e2:7a:01'); expect(clusterService.send).toHaveBeenCalledWith( REQUEST_RSSI_CHANNEL, 'f7:6c:e3:10:55:b5', diff --git a/src/integrations/bluetooth-classic/bluetooth-classic.service.ts b/src/integrations/bluetooth-classic/bluetooth-classic.service.ts index d2b08e6..263d776 100644 --- a/src/integrations/bluetooth-classic/bluetooth-classic.service.ts +++ b/src/integrations/bluetooth-classic/bluetooth-classic.service.ts @@ -94,6 +94,10 @@ export class BluetoothClassicService * @param event - Event that contains a new RSSI value */ async handleNewRssi(event: NewRssiEvent): Promise { + this.logger.debug( + `Received RSSI of ${event.rssi} for ${event.address} from ${event.instanceName}` + ); + const sensorId = slugify( _.lowerCase(`bluetooth-classic ${event.address} room presence`) ); @@ -101,7 +105,7 @@ export class BluetoothClassicService if (this.entitiesService.has(sensorId)) { sensor = this.entitiesService.get(sensorId) as RoomPresenceDistanceSensor; } else { - sensor = await this.createSensor(event.address, sensorId); + sensor = await this.createSensor(sensorId, event.address); } sensor.timeout = this.calculateCurrentTimeout(); @@ -129,7 +133,7 @@ export class BluetoothClassicService nodeSubset.forEach((node, index) => { // only remote nodes have a timestamp of last contact attached if (node.last === undefined) { - this.inquireRssi(addressSubset[index]); + this.handleRssiRequest(addressSubset[index]); } else { this.clusterService.send( REQUEST_RSSI_CHANNEL, @@ -152,6 +156,7 @@ export class BluetoothClassicService async inquireRssi(address: string): Promise { const execPromise = util.promisify(exec); + this.logger.debug(`Querying for RSSI of ${address} using hcitool`); const output = await execPromise( `hcitool cc "${address}" && hcitool rssi "${address}"` ); @@ -236,7 +241,7 @@ export class BluetoothClassicService protected calculateCurrentTimeout(): number { const nodes = this.getParticipatingNodes(); const addresses = Object.values(this.config.addresses); // workaround for node-config deserializing to an Array-like object - return Math.max(nodes.length, addresses.length) * 10; + return (Math.max(nodes.length, addresses.length) + 1) * 10; } /**