diff --git a/src/action-affordance.js b/src/action-affordance.js index 5402cb1..d796c87 100644 --- a/src/action-affordance.js +++ b/src/action-affordance.js @@ -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; } @@ -308,7 +308,7 @@ class ActionAffordance extends InteractionAffordance { * * @returns {Array} The current statuses of the action requests. */ - queryAllActions() { + queryAll() { return [...this.#actionRequests.values()] .reverse() .map(({ status }) => ({ ...status })) diff --git a/src/thing-server.js b/src/thing-server.js index f621e99..fd9910a 100644 --- a/src/thing-server.js +++ b/src/thing-server.js @@ -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', /** diff --git a/src/thing.js b/src/thing.js index 08c5943..f94160f 100644 --- a/src/thing.js +++ b/src/thing.js @@ -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; } @@ -417,6 +424,24 @@ class Thing { return property.read(); } + /** + * Read All Properties. + * + * @returns {Promise>} 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. * @@ -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'); } @@ -494,7 +519,7 @@ class Thing { /** @type {Record>} */ const statuses = {}; for (const [name, action] of this.actions) { - statuses[name] = action.queryAllActions(); + statuses[name] = action.queryAll(); } return statuses; } diff --git a/test/thing-server.test.js b/test/thing-server.test.js index b480a02..c0383d7 100644 --- a/test/thing-server.test.js +++ b/test/thing-server.test.js @@ -12,6 +12,10 @@ describe('ThingServer', () => { type: 'boolean', title: 'On/Off', }, + token: { + type: 'string', + writeOnly: true, + }, }, actions: { blink: { @@ -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; }); @@ -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'); diff --git a/test/thing.test.js b/test/thing.test.js index 0161edd..735b9fe 100644 --- a/test/thing.test.js +++ b/test/thing.test.js @@ -70,7 +70,10 @@ describe('Thing', () => { }, }, }, - forms: [{ href: 'actions', op: 'queryallactions' }], + forms: [ + { href: 'properties', op: ['readallproperties'] }, + { href: 'actions', op: 'queryallactions' }, + ], }); }); }); @@ -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);