Skip to content

Commit

Permalink
fix(bluetooth-low-energy): handle id override for app properly
Browse files Browse the repository at this point in the history
Closes #649
  • Loading branch information
mKeRix committed May 2, 2021
1 parent 3d08928 commit aab8757
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 45 deletions.
Expand Up @@ -190,7 +190,6 @@ describe('BluetoothLowEnergyService', () => {
'2f234454cf6d4a0fadf2f4911ba9ffa6-1-2',
'Test Beacon',
'abcd1234',
false,
-50,
-52,
0.7
Expand Down Expand Up @@ -225,7 +224,6 @@ describe('BluetoothLowEnergyService', () => {
'abcd1234',
'Test Beacon',
'abcd1234',
false,
-59,
-59,
1
Expand Down Expand Up @@ -257,7 +255,6 @@ describe('BluetoothLowEnergyService', () => {
'123-123',
'Test BLE Device',
'123-123',
false,
-81,
-59,
10.5
Expand Down Expand Up @@ -373,7 +370,6 @@ describe('BluetoothLowEnergyService', () => {
'abcd',
'Test BLE Device',
'abcd',
false,
-81,
-80,
1.1
Expand Down Expand Up @@ -813,16 +809,7 @@ describe('BluetoothLowEnergyService', () => {
const sensorHandleSpy = jest.spyOn(sensor, 'handleNewDistance');

service.handleNewDistance(
new NewDistanceEvent(
'test-instance',
'test',
'Test',
'test',
false,
-80,
-50,
2
)
new NewDistanceEvent('test-instance', 'test', 'Test', 'test', -80, -50, 2)
);

expect(sensorHandleSpy).toHaveBeenCalledWith('test-instance', 2, false);
Expand All @@ -841,7 +828,6 @@ describe('BluetoothLowEnergyService', () => {
'new',
'New Tag',
'new',
false,
-80,
-50,
1.3
Expand Down Expand Up @@ -889,11 +875,11 @@ describe('BluetoothLowEnergyService', () => {
'new',
'New Tag',
'new',
false,
-80,
-50,
1.3,
false,
undefined,
99
)
);
Expand Down Expand Up @@ -922,16 +908,7 @@ describe('BluetoothLowEnergyService', () => {
entitiesService.get.mockReturnValue(sensor);

service.handleNewDistance(
new NewDistanceEvent(
'test-instance',
'test',
'Test',
'test',
false,
-80,
-50,
2
)
new NewDistanceEvent('test-instance', 'test', 'Test', 'test', -80, -50, 2)
);

expect(sensor.measuredValues['test-instance'].rssi).toBe(-80);
Expand All @@ -943,23 +920,13 @@ describe('BluetoothLowEnergyService', () => {
'test',
'Test',
'test',
false,
-40,
-45,
2
)
);
service.handleNewDistance(
new NewDistanceEvent(
'test-instance',
'test',
'Test',
'test',
false,
-70,
-50,
2
)
new NewDistanceEvent('test-instance', 'test', 'Test', 'test', -70, -50, 2)
);

expect(sensor.measuredValues['test-instance-2'].rssi).toBe(-40);
Expand Down Expand Up @@ -1251,16 +1218,18 @@ describe('BluetoothLowEnergyService', () => {
await service.handleNewDistance(
new NewDistanceEvent(
'test-instance',
'app-id',
'tag-id',
'Test',
'peripheral-id',
true,
-80,
-50,
2
2,
false,
'app-id'
)
);

const newDistanceSpy = jest.spyOn(service, 'handleNewDistance');
await service.handleDiscovery({
id: 'peripheral-id',
rssi: -50,
Expand All @@ -1271,6 +1240,12 @@ describe('BluetoothLowEnergyService', () => {
} as Peripheral);

expect(discoverSpy).not.toHaveBeenCalled();
expect(newDistanceSpy).toHaveBeenCalledWith(
expect.objectContaining({
peripheralId: 'peripheral-id',
appId: 'app-id',
})
);
});

it('should temporarily denylist devices that error out from discovery attempts', async () => {
Expand Down
Expand Up @@ -106,6 +106,7 @@ export class BluetoothLowEnergyService
(!this.isAllowlistEnabled() && this.isDenylistEnabled())) &&
!this.isOnDenylist(tag.id)
) {
const appId = tag.isApp ? tag.id : undefined;
tag = this.applyOverrides(tag);
tag.rssi = tag.rssi * this.config.rssiFactor;
tag.rssi = this.filterRssi(tag.id, tag.rssi);
Expand All @@ -116,11 +117,11 @@ export class BluetoothLowEnergyService
tag.id,
tag.name,
tag.peripheral.id,
tag.isApp,
tag.rssi,
tag.measuredPower,
tag.distance,
tag.distance > this.config.maxDistance,
appId,
tag instanceof IBeacon ? tag.batteryLevel : undefined
);

Expand Down Expand Up @@ -190,7 +191,7 @@ export class BluetoothLowEnergyService
const hasBattery = event.batteryLevel !== undefined;

if (event.isApp) {
this.handleAppDiscovery(event.peripheralId, event.tagId);
this.handleAppDiscovery(event.peripheralId, event.appId);
}

if (this.entitiesService.has(sensorId)) {
Expand Down
10 changes: 7 additions & 3 deletions src/integrations/bluetooth-low-energy/new-distance.event.ts
Expand Up @@ -4,33 +4,37 @@ export class NewDistanceEvent {
tagId: string,
tagName: string,
peripheralId: string,
isApp: boolean,
rssi: number,
measuredPower: number,
distance: number,
outOfRange = false,
appId?: string,
batteryLevel?: number
) {
this.instanceName = instanceName;
this.tagId = tagId;
this.tagName = tagName;
this.peripheralId = peripheralId;
this.isApp = isApp;
this.rssi = rssi;
this.measuredPower = measuredPower;
this.distance = distance;
this.outOfRange = outOfRange;
this.appId = appId;
this.batteryLevel = batteryLevel;
}

get isApp(): boolean {
return this.appId != undefined;
}

instanceName: string;
tagId: string;
tagName: string;
peripheralId: string;
isApp: boolean;
rssi: number;
measuredPower: number;
distance: number;
outOfRange: boolean;
appId?: string;
batteryLevel?: number;
}

0 comments on commit aab8757

Please sign in to comment.