Skip to content

Commit

Permalink
Add profile member to Thing Descriptions - closes #2909
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrancis committed Apr 24, 2023
1 parent 7b5282e commit e30d64f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,19 @@ export enum ActionStatusValues {
COMPLETED = 'completed',
FAILED = 'failed',
}

// Contexts
export const MOZ_IOT_CONTEXT = 'https://iot.mozilla.org/schemas';
export const WEBTHINGS_CONTEXT = 'https://webthings.io/schemas';
export const WOT_TD_NS_CONTEXT = 'http://www.w3.org/ns/td';
export const WOT_TD_1_CONTEXT = 'https://www.w3.org/2019/wot/td/v1';
export const WOT_TD_1_1_CONTEXT = 'https://www.w3.org/2022/wot/td/v1.1';
export const DEFAULT_CONTEXT = [WOT_TD_1_1_CONTEXT, WEBTHINGS_CONTEXT];

// Profiles
export const WOT_HTTP_BASIC_PROFILE = 'https://www.w3.org/2022/wot/profile/http-basic/v1';
export const WOT_HTTP_SSE_PROFILE = 'https://www.w3.org/2022/wot/profile/http-sse/v1';

export interface LogMessage {
severity: LogSeverity;
message: string;
Expand Down
28 changes: 26 additions & 2 deletions src/models/thing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface ThingDescription {
title: string;
'@context': string | string[];
'@type': string[];
profile: string | string[];
description: string;
base: string;
baseHref: string;
Expand Down Expand Up @@ -83,6 +84,8 @@ export default class Thing extends EventEmitter {

private '@type': string[];

private profile: string | string[];

private description: string;

private href: string;
Expand Down Expand Up @@ -133,12 +136,32 @@ export default class Thing extends EventEmitter {
// Parse the Thing Description
this.id = id;
this.title = description.title || (<Record<string, string>>(<unknown>description)).name || '';

// Set @context
if (description['@context']) {
this['@context'] = Utils.standardizeContext(description['@context']);
} else {
this['@context'] = Constants.DEFAULT_CONTEXT;
}

// Set @type
this['@type'] = description['@type'] || [];

// Set profile (based on which interaction affordances are exposed)
this.profile = [];
if (
Object.keys(description.properties ?? {}).length > 0 ||
Object.keys(description.actions ?? {}).length > 0
) {
this.profile.push(Constants.WOT_HTTP_BASIC_PROFILE);
}
if (
Object.keys(description.properties ?? {}).length > 0 ||
Object.keys(description.events ?? {}).length > 0
) {
this.profile.push(Constants.WOT_HTTP_SSE_PROFILE);
}

this.description = description.description || '';
this.href = `${Constants.THINGS_PATH}/${encodeURIComponent(this.id)}`;
this.properties = {};
Expand Down Expand Up @@ -190,8 +213,8 @@ export default class Thing extends EventEmitter {
this.properties[propertyName] = property;
}

// If there are properties, add top level forms for them
if (Object.keys(description.properties).length > 0) {
// If there are properties, add a top level form for them
if (Object.keys(description.properties ?? {}).length > 0) {
let ops;
// If there are writable properties then add readallproperties and
// writemultipleproperties operations as an array
Expand Down Expand Up @@ -627,6 +650,7 @@ export default class Thing extends EventEmitter {
title: this.title,
'@context': this['@context'],
'@type': this['@type'],
profile: this.profile,
description: this.description,
href: this.href,
properties: this.properties,
Expand Down
4 changes: 4 additions & 0 deletions src/test/integration/things-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const piDescr = {
title: 'pi-1',
'@context': ['https://www.w3.org/2022/wot/td/v1.1', 'https://webthings.io/schemas'],
'@type': ['OnOffSwitch'],
profile: [
'https://www.w3.org/2022/wot/profile/http-basic/v1',
'https://www.w3.org/2022/wot/profile/http-sse/v1',
],
properties: {
power: {
'@type': 'OnOffProperty',
Expand Down

0 comments on commit e30d64f

Please sign in to comment.