Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/action-affordance.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ class ActionAffordance extends InteractionAffordance {
* @param {string} actionID The ID of the action request.
* @returns {ActionStatus|undefined} The status, if the request exists.
*/
queryAction(actionID) {
query(actionID) {
const request = this.#actionRequests.get(actionID);
return request ? { ...request.status } : undefined;
}
Expand All @@ -308,7 +308,7 @@ class ActionAffordance extends InteractionAffordance {
*
* @returns {Array<ActionStatus>} The current statuses of the action requests.
*/
queryAllActions() {
queryAll() {
return [...this.#actionRequests.values()]
.reverse()
.map(({ status }) => ({ ...status }))
Expand Down
22 changes: 22 additions & 0 deletions src/thing-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@ class ThingServer {
},
);

this.app.get(
'/properties',
/**
* Read All Properties
*
* @param {Request} _request
* @param {Response} response
*/
async (_request, response) => {
if (this.thing.properties.size === 0) {
response.status(404).send();
return;
}
try {
const values = await this.thing.readAllProperties();
response.status(200).json(values);
} catch {
response.status(500).send();
}
},
);

this.app.get(
'/properties/:name',
/**
Expand Down
31 changes: 28 additions & 3 deletions src/thing.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,18 @@ class Thing {
// If properties are defined then add a properties member
if (Object.keys(properties).length > 0) {
thingDescription.properties = properties;
thingDescription.forms = [
{ href: 'properties', op: ['readallproperties'] },
];
}
// If actions are defined then add an actions member
if (Object.keys(actions).length > 0) {
thingDescription.actions = actions;
thingDescription.forms = [{ href: 'actions', op: 'queryallactions' }];
thingDescription.forms ??= [];
thingDescription.forms.push({
href: 'actions',
op: 'queryallactions',
});
}
return thingDescription;
}
Expand Down Expand Up @@ -417,6 +424,24 @@ class Thing {
return property.read();
}

/**
* Read All Properties.
*
* @returns {Promise<Record<string, any>>} The current values keyed by property name.
*/
async readAllProperties() {
const readableProperties = [...this.properties.entries()].filter(
([, property]) => property.writeOnly !== true,
);
const values = await Promise.all(
readableProperties.map(async ([name]) => [
name,
await this.readProperty(name),
]),
);
return Object.fromEntries(values);
}

/**
* Write Property.
*
Expand Down Expand Up @@ -478,7 +503,7 @@ class Thing {
*/
queryAction(name, actionID) {
const action = this.actions.get(name);
const status = action?.queryAction(actionID);
const status = action?.query(actionID);
if (!status) {
throw new Error('NotFoundError');
}
Expand All @@ -494,7 +519,7 @@ class Thing {
/** @type {Record<string, Array<ActionStatus>>} */
const statuses = {};
for (const [name, action] of this.actions) {
statuses[name] = action.queryAllActions();
statuses[name] = action.queryAll();
}
return statuses;
}
Expand Down
49 changes: 49 additions & 0 deletions test/thing-server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ describe('ThingServer', () => {
type: 'boolean',
title: 'On/Off',
},
token: {
type: 'string',
writeOnly: true,
},
},
actions: {
blink: {
Expand Down Expand Up @@ -41,6 +45,7 @@ describe('ThingServer', () => {
before(async () => {
const thing = new Thing(partialTD);
thing.setPropertyReadHandler('on', async () => currentValue);
thing.setPropertyReadHandler('token', async () => 'secret');
thing.setPropertyWriteHandler('on', async (value) => {
currentValue = value;
});
Expand Down Expand Up @@ -95,6 +100,50 @@ describe('ThingServer', () => {
});
});

describe('GET /properties', () => {
it('should return all readable property values', async () => {
const response = await fetch(baseUrl + '/properties');
assert.equal(response.status, 200);
assert.deepEqual(await response.json(), { on: true });
});

it('should return 500 if a readable property cannot be read', async () => {
const failingThing = new Thing({
title: 'Failing Thing',
properties: { unavailable: { type: 'string' } },
});
const failingServer = new ThingServer(failingThing);
await new Promise((resolve) => {
failingServer.server = failingServer.app.listen(0, resolve);
});

try {
const port = failingServer.server.address().port;
const response = await fetch(`http://localhost:${port}/properties`);
assert.equal(response.status, 500);
} finally {
failingServer.stop();
}
});

it('should return 404 when the Thing has no properties', async () => {
const emptyServer = new ThingServer(
new Thing({ title: 'No Properties' }),
);
await new Promise((resolve) => {
emptyServer.server = emptyServer.app.listen(0, resolve);
});

try {
const port = emptyServer.server.address().port;
const response = await fetch(`http://localhost:${port}/properties`);
assert.equal(response.status, 404);
} finally {
emptyServer.stop();
}
});
});

describe('GET /properties/:invalidname', () => {
it('should return 404 for an invalid property name', async () => {
const response = await fetch(baseUrl + '/properties/foo');
Expand Down
27 changes: 26 additions & 1 deletion test/thing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ describe('Thing', () => {
},
},
},
forms: [{ href: 'actions', op: 'queryallactions' }],
forms: [
{ href: 'properties', op: ['readallproperties'] },
{ href: 'actions', op: 'queryallactions' },
],
});
});
});
Expand Down Expand Up @@ -99,6 +102,28 @@ describe('Thing', () => {
});
});

describe('readAllProperties', () => {
it('should return values for readable properties only', async () => {
const thing = new Thing({
title: 'Test Thing',
properties: {
on: { type: 'boolean' },
secret: { type: 'string', writeOnly: true },
},
});
thing.setPropertyReadHandler('on', async () => true);
thing.setPropertyReadHandler('secret', async () => 'hidden');

assert.deepEqual(await thing.readAllProperties(), { on: true });
});

it('should reject when a readable property cannot be read', async () => {
const thing = new Thing(partialTD);

await assert.rejects(() => thing.readAllProperties(), /InternalError/);
});
});

describe('setPropertyWriteHandler', () => {
it('should throw when the property does not exist', () => {
const thing = new Thing(partialTD);
Expand Down
Loading