Skip to content

Commit

Permalink
feat(bluetooth-low-energy): adapts closest instance detection
Browse files Browse the repository at this point in the history
Makes the BLE integration use the same new feature as the Bluetooth
Classic integration.
  • Loading branch information
mKeRix committed Feb 14, 2020
1 parent 3966ab6 commit 655c51f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
Expand Up @@ -407,7 +407,7 @@ describe('BluetoothLowEnergyService', () => {
);
});

it('should report peripherals that are closer than the max distance', () => {
it('should report peripherals that are closer than the max distance normally', () => {
const handleDistanceSpy = jest
.spyOn(service, 'handleNewDistance')
.mockImplementation(() => undefined);
Expand All @@ -421,11 +421,20 @@ describe('BluetoothLowEnergyService', () => {
localName: 'Test BLE Device'
}
} as Peripheral);
expect(handleDistanceSpy).toHaveBeenCalled();
expect(clusterService.publish).toHaveBeenCalled();
expect(handleDistanceSpy).toHaveBeenCalledWith(
expect.objectContaining({
outOfRange: false
})
);
expect(clusterService.publish).toHaveBeenCalledWith(
NEW_DISTANCE_CHANNEL,
expect.objectContaining({
outOfRange: false
})
);
});

it('should ignore peripherals that are further away than max distance', () => {
it('should mark peripherals that are further away than max distance as out of range', () => {
const handleDistanceSpy = jest
.spyOn(service, 'handleNewDistance')
.mockImplementation(() => undefined);
Expand All @@ -439,8 +448,17 @@ describe('BluetoothLowEnergyService', () => {
localName: 'Test BLE Device'
}
} as Peripheral);
expect(handleDistanceSpy).not.toHaveBeenCalled();
expect(clusterService.publish).not.toHaveBeenCalled();
expect(handleDistanceSpy).toHaveBeenCalledWith(
expect.objectContaining({
outOfRange: true
})
);
expect(clusterService.publish).toHaveBeenCalledWith(
NEW_DISTANCE_CHANNEL,
expect.objectContaining({
outOfRange: true
})
);
});

it('should reuse existing Kalman filters for the same id', () => {
Expand Down Expand Up @@ -481,7 +499,7 @@ describe('BluetoothLowEnergyService', () => {
new NewDistanceEvent('test-instance', 'test', 'Test', 2)
);

expect(sensorHandleSpy).toHaveBeenCalledWith('test-instance', 2);
expect(sensorHandleSpy).toHaveBeenCalledWith('test-instance', 2, false);
});

it('should add new room presence sensor if no matching ones exist yet', () => {
Expand All @@ -505,7 +523,7 @@ describe('BluetoothLowEnergyService', () => {
expect.any(Array)
);
expect(setInterval).toHaveBeenCalledWith(expect.any(Function), 20 * 1000);
expect(sensorHandleSpy).toHaveBeenCalledWith('test-instance', 1.3);
expect(sensorHandleSpy).toHaveBeenCalledWith('test-instance', 1.3, false);
});

it('should log the id of new peripherals that are found', () => {
Expand Down
Expand Up @@ -87,20 +87,16 @@ export class BluetoothLowEnergyService extends KalmanFilterable(Object, 0.8, 15)
tag = this.applyOverrides(tag);
tag.rssi = this.filterRssi(tag.id, tag.rssi);

if (
this.config.maxDistance == undefined ||
tag.distance <= this.config.maxDistance
) {
const globalSettings = this.configService.get('global');
const event = new NewDistanceEvent(
globalSettings.instanceName,
tag.id,
tag.name,
tag.distance
);
this.handleNewDistance(event);
this.clusterService.publish(NEW_DISTANCE_CHANNEL, event);
}
const globalSettings = this.configService.get('global');
const event = new NewDistanceEvent(
globalSettings.instanceName,
tag.id,
tag.name,
tag.distance,
tag.distance > this.config.maxDistance
);
this.handleNewDistance(event);
this.clusterService.publish(NEW_DISTANCE_CHANNEL, event);
}
}

Expand All @@ -122,7 +118,11 @@ export class BluetoothLowEnergyService extends KalmanFilterable(Object, 0.8, 15)
);
}

sensor.handleNewDistance(event.instanceName, event.distance);
sensor.handleNewDistance(
event.instanceName,
event.distance,
event.outOfRange
);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/integrations/bluetooth-low-energy/new-distance.event.ts
Expand Up @@ -3,16 +3,19 @@ export class NewDistanceEvent {
instanceName: string,
tagId: string,
tagName: string,
distance: number
distance: number,
outOfRange = false
) {
this.instanceName = instanceName;
this.tagId = tagId;
this.tagName = tagName;
this.distance = distance;
this.outOfRange = outOfRange;
}

instanceName: string;
tagId: string;
tagName: string;
distance: number;
outOfRange: boolean;
}

0 comments on commit 655c51f

Please sign in to comment.