Skip to content

Commit

Permalink
fix(bluetooth-classic): Publish RSSI even for local inquiries
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mKeRix committed Jan 25, 2020
1 parent 03812f3 commit 025ceb1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -228,28 +228,28 @@ 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',
'def'
);

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',
'def'
);

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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down
11 changes: 8 additions & 3 deletions src/integrations/bluetooth-classic/bluetooth-classic.service.ts
Expand Up @@ -94,14 +94,18 @@ export class BluetoothClassicService
* @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.address} from ${event.instanceName}`
);

const sensorId = slugify(
_.lowerCase(`bluetooth-classic ${event.address} room presence`)
);
let sensor: RoomPresenceDistanceSensor;
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();
Expand Down Expand Up @@ -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,
Expand All @@ -152,6 +156,7 @@ export class BluetoothClassicService
async inquireRssi(address: string): Promise<number> {
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}"`
);
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 025ceb1

Please sign in to comment.