Skip to content

Commit

Permalink
Merge pull request #237 from ka-vaNu/233-tempsensorac-elapsedtimes-bl…
Browse files Browse the repository at this point in the history
…eibt-auf-1-uptime-wird-nicht-aktualisiert

233 tempsensorac elapsedtimes bleibt auf 1 uptime wird nicht aktualisiert
  • Loading branch information
ka-vaNu authored Dec 26, 2024
2 parents b285eb8 + e926739 commit 15e71a7
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 48 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
21 changes: 20 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 All @@ -65,6 +81,7 @@ async function simpleObjectUrlGetter(device, url) {
}
} catch (error) {
iob.log.error(`simpleObjectUrlGetter: ${error}`);
states = {};
}
return states;
}
Expand Down Expand Up @@ -119,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 @@ -178,6 +196,7 @@ async function initCommon(name, apiType) {
module.exports = {
isArray,
isObject,
findVal,
setIob,
initCommon,
getBleboxData,
Expand Down
96 changes: 94 additions & 2 deletions lib/tools.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable no-undef */
const expect = require('chai').expect;
const tools = require('../lib/tools');
const axios = require('axios');
const sinon = require('sinon');

const datapoints = {
'gatebox#command.move': {
Expand All @@ -17,10 +19,51 @@ const datapoints = {
},
};

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

const responseData = {
status: 'value1',
details: {
description: 'value3',
isActive: true,
additionalInfo: null,
},
items: [
{
id: 1,
name: 'Item 1',
},
{
id: 2,
name: 'Item 2',
},
],
message: 'Ein einfacher String',
userId: 12345,
balance: 123.45,
};

const expectedResponse =
'{"status":"value1","details.description":"value3","details.isActive":true,"details.additionalInfo":null,"items[0].id":1,"items[0].name":"Item 1","items[1].id":2,"items[1].name":"Item 2","message":"Ein einfacher String","userId":12345,"balance":123.45}';

const device = {
dev_name: 'fakeName',
smart_name: 'fakeSmartName',
dev_ip: 'localhost',
dev_port: 3000,
dev_type: 'shutterbox',
polling: 60,
api_type: 'shutterbox',
};

const fruits = [];
fruits.push('banana', 'apple', 'peach');

describe('Test isObject', function () {
describe('Test tools.isObject', function () {
it('expect false', function () {
expect(tools.isObject(null)).to.be.false;
});
Expand All @@ -34,7 +77,7 @@ describe('Test isObject', function () {
});
});

describe('Test isArray', function () {
describe('Test tools.isArray', function () {
it('expect true', function () {
expect(tools.isArray(fruits)).to.be.true;
});
Expand All @@ -47,3 +90,52 @@ describe('Test isArray', function () {
expect(tools.isArray(datapoints)).to.be.false;
});
});

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;

beforeEach(() => {
axiosGetStub = sinon.stub(axios.default, 'get');
});

afterEach(() => {
sinon.restore();
});

it('should fetch data and return the dot-notated object', async () => {
const url = '/test';

axiosGetStub.resolves({ data: responseData });

const result = await tools.simpleObjectUrlGetter(device, url);

expect(axiosGetStub).to.have.been.calledOnceWithExactly('http://localhost:3000/test');
expect(JSON.stringify(result)).to.be.equal(expectedResponse);
});
});
35 changes: 0 additions & 35 deletions main.test.js

This file was deleted.

Loading

0 comments on commit 15e71a7

Please sign in to comment.