Skip to content

Commit

Permalink
#233 search datapoints case-insensitive, fix case problems in API
Browse files Browse the repository at this point in the history
  • Loading branch information
ka-vaNu committed Dec 26, 2024
1 parent 9952e19 commit e926739
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/gatebox.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const datapoints = {
type: 'state',
common: { name: 'IP-Adress', type: 'string', role: 'text', read: true, write: false },
},
'gatebox#upTimeS': {
'gatebox#uptimeS': {
path: 'uptimeS',
type: 'state',
common: { name: 'Uptime in seconds', type: 'number', role: 'value', read: true, write: false },
Expand Down
4 changes: 2 additions & 2 deletions lib/multisensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const datapoints = {
type: 'state',
common: { name: 'IP-Adress', type: 'string', role: 'text', read: true, write: false },
},
'multisensor#upTimeS': {
path: 'upTimeS',
'multisensor#uptimeS': {
path: 'uptimeS',
type: 'state',
common: { name: 'Uptime in seconds', type: 'number', role: 'value', read: true, write: false },
},
Expand Down
5 changes: 0 additions & 5 deletions lib/shutterbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ const datapoints = {
type: 'state',
common: { name: 'IP-Adress', type: 'string', role: 'text', read: true, write: false },
},
'shutterbox#upTimeS': {
path: 'uptimeS',
type: 'state',
common: { name: 'Uptime in seconds', type: 'number', role: 'value', read: true, write: false },
},
'shutterbox#uptimeS': {
path: 'uptimeS',
type: 'state',
Expand Down
20 changes: 19 additions & 1 deletion lib/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ function isArray(it) {
return Object.prototype.toString.call(it) === '[object Array]';
}

/**
* Finds a value in an object by a property name, case-insensitive.
*
* @param obj - The object to search in.
* @param prop - The property name to search for.
* @returns The value of the found property, or undefined if not found.
*/
function findVal(obj, prop) {
prop = `${prop}`.toLowerCase();
for (let p in obj) {
if (Object.prototype.hasOwnProperty.call(obj, p) && prop == `${p}`.toLowerCase()) {
return obj[p];
}
}
}

/**
*
* @param device object of the device to request data
Expand Down Expand Up @@ -120,7 +136,8 @@ async function setIobStates(device, values) {
}
if (Object.prototype.hasOwnProperty.call(iob.datapoints[device.dev_name], search)) {
const deviceDatapoints = iob.datapoints[device.dev_name];
const path = deviceDatapoints[search].path;
const datapoint = findVal(deviceDatapoints, search);
const path = datapoint.path;
if (iob.extLog) {
iob.log.info(`setIobStates path: ${path}`);
}
Expand Down Expand Up @@ -179,6 +196,7 @@ async function initCommon(name, apiType) {
module.exports = {
isArray,
isObject,
findVal,
setIob,
initCommon,
getBleboxData,
Expand Down
32 changes: 32 additions & 0 deletions lib/tools.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ const datapoints = {
},
};

const objCaseInsensitive = {
testlower: 'lower',
TESTUPPER: 'upper',
TestMixed: 'mixed',
};

const responseData = {
status: 'value1',
details: {
Expand Down Expand Up @@ -85,6 +91,32 @@ describe('Test tools.isArray', function () {
});
});

describe('Test tools.findVal', function () {
it('search lower same case', function () {
expect(tools.findVal(objCaseInsensitive, 'testlower')).to.equal('lower');
});

it('search lower different case', function () {
expect(tools.findVal(objCaseInsensitive, 'TESTLOWER')).to.equal('lower');
});

it('search UPPER same case', function () {
expect(tools.findVal(objCaseInsensitive, 'TESTUPPER')).to.equal('upper');
});

it('search UPPER different case', function () {
expect(tools.findVal(objCaseInsensitive, 'testupper')).to.equal('upper');
});

it('search Mixed', function () {
expect(tools.findVal(objCaseInsensitive, 'testmixed')).to.equal('mixed');
});

it('search not found', function () {
expect(tools.findVal(objCaseInsensitive, 'test')).to.be.undefined;
});
});

describe('Test tools.simpleObjectUrlGetter', () => {
let axiosGetStub;

Expand Down

0 comments on commit e926739

Please sign in to comment.