Skip to content

Commit

Permalink
feat(bluetooth-low-energy): allow tag names to be overriden
Browse files Browse the repository at this point in the history
As mentioned in #102, the default sensor names are unweildy. This allows
users to give tags customized names via their id.
  • Loading branch information
mKeRix committed Jan 27, 2020
1 parent f3c7c46 commit 649d207
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
Expand Up @@ -11,5 +11,6 @@ export class BluetoothLowEnergyConfig {
}

class TagOverride {
measuredPower: number;
name?: string;
measuredPower?: number;
}
Expand Up @@ -243,7 +243,7 @@ describe('BluetoothLowEnergyService', () => {
);
});

it('should apply tag overrides if they exist', () => {
it('should apply tag distance override if it exists', () => {
const sensor = new Sensor('testid', 'Test');
entitiesService.has.mockReturnValue(true);
entitiesService.get.mockReturnValue(sensor);
Expand Down Expand Up @@ -295,6 +295,35 @@ describe('BluetoothLowEnergyService', () => {
);
});

it('should apply a tag name override if it exists', () => {
const sensor = new Sensor('testid', 'Test');
entitiesService.has.mockReturnValue(false);
entitiesService.add.mockReturnValue(sensor);
jest
.spyOn(service, 'handleNewDistance')
.mockImplementation(() => undefined);
mockConfig.tagOverrides = {
abcd: {
name: 'better name'
}
};

service.handleDiscovery({
id: 'abcd',
rssi: -12,
advertisement: {
localName: 'Test BLE Device'
}
} as Peripheral);

expect(entitiesService.add).toHaveBeenCalledWith(
expect.objectContaining({
name: expect.stringContaining('better name')
}),
expect.any(Array)
);
});

it('should create a new distance sensor if there is not a matching one yet', () => {
const sensor = new Sensor('new', 'New');
entitiesService.has.mockReturnValue(false);
Expand Down
Expand Up @@ -248,6 +248,9 @@ export class BluetoothLowEnergyService
protected applyOverrides(tag: Tag): Tag {
if (this.config.tagOverrides.hasOwnProperty(tag.id)) {
const overrides = this.config.tagOverrides[tag.id];
if (overrides.name !== undefined) {
tag.name = overrides.name;
}
if (overrides.measuredPower !== undefined) {
tag.measuredPower = overrides.measuredPower;
}
Expand Down
8 changes: 7 additions & 1 deletion src/integrations/bluetooth-low-energy/tag.ts
Expand Up @@ -2,6 +2,8 @@ import { Peripheral } from '@abandonware/noble';
import _ from 'lodash';

export class Tag {
private _name;

constructor(peripheral: Peripheral) {
this.peripheral = peripheral;
this.rssi = peripheral.rssi;
Expand All @@ -18,7 +20,11 @@ export class Tag {
}

get name(): string {
return this.peripheral.advertisement.localName || this.id;
return this._name || this.peripheral.advertisement.localName || this.id;
}

set name(name: string) {
this._name = name;
}

get distance(): number {
Expand Down

0 comments on commit 649d207

Please sign in to comment.