Skip to content

Commit

Permalink
Centralize device functions (setDeviceParam, setDeviceFeature) (#1978)
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato authored Dec 15, 2023
1 parent b2ab4f6 commit 4fa39d6
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Promise = require('bluebird');

const logger = require('../../../../utils/logger');
const { EVENTS, WEBSOCKET_MESSAGE_TYPES } = require('../../../../utils/constants');
const { setDeviceParam } = require('../../../../utils/setDeviceParam');
const { setDeviceParam } = require('../../../../utils/device');

const { INFORMATION_SERVICES } = require('../device/bluetooth.information');
const { PARAMS } = require('../utils/bluetooth.constants');
Expand All @@ -20,7 +20,7 @@ async function scanDevice(peripheralUuid) {

const device = this.discoveredDevices[peripheralUuid];

setDeviceParam(device, PARAMS.LOADED, false);
setDeviceParam(device, PARAMS.LOADED, 'false');
this.gladys.event.emit(EVENTS.WEBSOCKET.SEND_ALL, {
type: WEBSOCKET_MESSAGE_TYPES.BLUETOOTH.DISCOVER,
payload: device,
Expand Down Expand Up @@ -61,7 +61,7 @@ async function scanDevice(peripheralUuid) {
} catch (e) {
logger.warn(`Bluetooth: unable to scan ${peripheralUuid} - ${e}`);
} finally {
setDeviceParam(device, PARAMS.LOADED, true);
setDeviceParam(device, PARAMS.LOADED, 'true');
this.gladys.event.emit(EVENTS.WEBSOCKET.SEND_ALL, {
type: WEBSOCKET_MESSAGE_TYPES.BLUETOOTH.DISCOVER,
payload: device,
Expand Down
4 changes: 2 additions & 2 deletions server/services/bluetooth/lib/device/bluetooth.information.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const {
DEVICE_POLL_FREQUENCIES,
} = require('../../../../utils/constants');
const { addSelector } = require('../../../../utils/addSelector');
const { setDeviceFeature } = require('../../../../utils/setDeviceFeature');
const { setDeviceParam } = require('../../../../utils/setDeviceParam');
const { setDeviceFeature } = require('../../../../utils/device');
const { setDeviceParam } = require('../../../../utils/device');

const { PARAMS } = require('../utils/bluetooth.constants');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { addSelector } = require('../../../../utils/addSelector');
const { setDeviceParam } = require('../../../../utils/setDeviceParam');
const { setDeviceParam } = require('../../../../utils/device');

const { PARAMS } = require('../utils/bluetooth.constants');
const { encodeParamValue } = require('./bluetooth.information');
Expand All @@ -24,7 +24,7 @@ function transformToDevice(peripheral) {
};

if (!connectable) {
setDeviceParam(device, PARAMS.LOADED, true);
setDeviceParam(device, PARAMS.LOADED, 'true');
}

addSelector(device);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('bluetooth.scanDevice', () => {
params: [
{
name: PARAMS.LOADED,
value: true,
value: 'true',
},
],
});
Expand Down Expand Up @@ -132,7 +132,7 @@ describe('bluetooth.scanDevice', () => {
params: [
{
name: PARAMS.LOADED,
value: true,
value: 'true',
},
],
});
Expand Down Expand Up @@ -169,7 +169,7 @@ describe('bluetooth.scanDevice', () => {
params: [
{
name: PARAMS.LOADED,
value: true,
value: 'true',
},
],
});
Expand Down Expand Up @@ -206,7 +206,7 @@ describe('bluetooth.scanDevice', () => {
params: [
{
name: PARAMS.LOADED,
value: true,
value: 'true',
},
{
name: PARAMS.MANUFACTURER,
Expand Down Expand Up @@ -248,7 +248,7 @@ describe('bluetooth.scanDevice', () => {
params: [
{
name: PARAMS.LOADED,
value: true,
value: 'true',
},
],
});
Expand Down Expand Up @@ -301,7 +301,7 @@ describe('bluetooth.scanDevice', () => {
params: [
{
name: PARAMS.LOADED,
value: true,
value: 'true',
},
],
});
Expand Down Expand Up @@ -350,7 +350,7 @@ describe('bluetooth.scanDevice', () => {
params: [
{
name: PARAMS.LOADED,
value: true,
value: 'true',
},
],
});
Expand Down Expand Up @@ -400,7 +400,7 @@ describe('bluetooth.scanDevice', () => {
params: [
{
name: PARAMS.LOADED,
value: true,
value: 'true',
},
],
});
Expand Down Expand Up @@ -450,7 +450,7 @@ describe('bluetooth.scanDevice', () => {
params: [
{
name: PARAMS.LOADED,
value: true,
value: 'true',
},
],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('bluetooth.transformToDevice', () => {
params: [
{
name: 'loaded',
value: true,
value: 'true',
},
],
};
Expand All @@ -61,7 +61,7 @@ describe('bluetooth.transformToDevice', () => {
params: [
{
name: 'loaded',
value: true,
value: 'true',
},
],
};
Expand All @@ -81,7 +81,7 @@ describe('bluetooth.transformToDevice', () => {
params: [
{
name: 'loaded',
value: true,
value: 'true',
},
],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('bluetooth.discover event', () => {
name: 'ADDRESS',
selector: 'bluetooth-uuid',
features: [],
params: [{ name: 'loaded', value: true }],
params: [{ name: 'loaded', value: 'true' }],
};

assert.calledWith(eventWS, { payload: expectedWSPeripheral, type: 'bluetooth.discover' });
Expand Down
49 changes: 49 additions & 0 deletions server/test/utils/device.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
getDeviceParam,
setDeviceParam,
getDeviceFeature,
setDeviceFeature,
hasDeviceChanged,
mergeFeatures,
mergeDevices,
Expand Down Expand Up @@ -135,6 +136,54 @@ describe('getDeviceFeature', () => {
});
});

describe('setDeviceFeature', () => {
it('should create array and add feature', () => {
const device = {};
const feature = {
selector: 'selector',
};

setDeviceFeature(device, feature);

expect(device).deep.eq({
features: [feature],
});
});

it('should add feature to array', () => {
const device = { features: [{ selector: 'no-match' }] };
const feature = {
selector: 'selector',
};

setDeviceFeature(device, feature);

expect(device).deep.eq({
features: [{ selector: 'no-match' }, feature],
});
});

it('should replace feature into array', () => {
const device = {
features: [
{
selector: 'selector',
},
],
};
const feature = {
name: 'name',
selector: 'selector',
};

setDeviceFeature(device, feature);

expect(device).deep.eq({
features: [feature],
});
});
});

describe('hasDeviceChanged', () => {
it('should be same device', () => {
const feature1 = buildFeature('test-1');
Expand Down
50 changes: 0 additions & 50 deletions server/test/utils/setDeviceFeature.test.js

This file was deleted.

52 changes: 0 additions & 52 deletions server/test/utils/setDeviceParam.test.js

This file was deleted.

26 changes: 26 additions & 0 deletions server/utils/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,31 @@ function getDeviceFeature(device, category, type) {
return null;
}

/**
* @description Add or update a param to a device.
* @param {object} device - Device to add parameter.
* @param {object} feature - The feature to add.
* @returns {object} The device.
* @example
* setDeviceFeature({ features: [] }, { selector: 'feature' })
*/
function setDeviceFeature(device, feature) {
let { features } = device;
if (!features) {
features = [];
device.features = features;
}

const featureIndex = features.findIndex((p) => p.selector === feature.selector);
if (featureIndex >= 0) {
features[featureIndex] = feature;
} else {
features.push(feature);
}

return device;
}

const matchFeature = (features, feature) => {
return features.findIndex((f) => f.external_id === feature.external_id);
};
Expand Down Expand Up @@ -204,6 +229,7 @@ module.exports = {
getDeviceParam,
setDeviceParam,
getDeviceFeature,
setDeviceFeature,
hasDeviceChanged,
mergeFeatures,
mergeDevices,
Expand Down
Loading

0 comments on commit 4fa39d6

Please sign in to comment.