Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SSE to event forms #1

Merged
merged 2 commits into from
Aug 27, 2021
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
5 changes: 5 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ export enum LogSeverity {
PROMPT = 4,
}

export enum WoTOperation {
READ_ALL_PROPERTIES_OP = 'readallproperties',
SUBSCRIBE_ALL_EVENTS_OP = 'subscribeallevents',
UNSUBSCRIBE_ALL_EVENTS_OP = 'unsubscribeallevents',
}
export interface LogMessage {
severity: LogSeverity;
message: string;
Expand Down
55 changes: 44 additions & 11 deletions src/models/thing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export default class Thing extends EventEmitter {
this.events = description.events || {};
this.connected = false;
this.eventsDispatched = [];
this.forms = [];

if (description.properties) {
for (const propertyName in description.properties) {
Expand Down Expand Up @@ -172,14 +173,28 @@ export default class Thing extends EventEmitter {

this.properties[propertyName] = property;
}

// If there are properties, add a top level form for them
if (Object.keys(description.properties).length > 0) {
this.forms.push({
href: `${this.href}/properties`,
op: Constants.WoTOperation.READ_ALL_PROPERTIES_OP,
});
}
}

// If there are events, add a top level form for them
if (Object.keys(description.events ?? {}).length > 0) {
this.forms.push({
href: `${this.href}/events`,
op: [
Constants.WoTOperation.SUBSCRIBE_ALL_EVENTS_OP,
Constants.WoTOperation.UNSUBSCRIBE_ALL_EVENTS_OP,
],
subprotocol: 'sse',
});
}

this.forms = [
{
href: `${this.href}/properties`,
op: ['readallproperties', 'writeallproperties'],
},
];
this.floorplanVisibility = description.floorplanVisibility;
this.floorplanX = description.floorplanX;
this.floorplanY = description.floorplanY;
Expand All @@ -190,10 +205,6 @@ export default class Thing extends EventEmitter {
rel: 'actions',
href: `${this.href}/actions`,
},
{
rel: 'events',
href: `${this.href}/events`,
},
];

const uiLink = {
Expand Down Expand Up @@ -281,6 +292,7 @@ export default class Thing extends EventEmitter {
// Give the event a URL
event.forms.push({
href: `${this.href}${Constants.EVENTS_PATH}/${encodeURIComponent(eventName)}`,
subprotocol: 'sse',
});
}

Expand Down Expand Up @@ -668,7 +680,7 @@ export default class Thing extends EventEmitter {

// Update description
this.description = description.description || '';

this.forms = [];
// Update properties
this.properties = {};
if (description.properties) {
Expand Down Expand Up @@ -700,6 +712,14 @@ export default class Thing extends EventEmitter {
});
this.properties[propertyName] = property;
}

// If there are properties, add a top level form for them
if (Object.keys(description.properties).length > 0) {
this.forms.push({
href: `${this.href}/properties`,
op: Constants.WoTOperation.READ_ALL_PROPERTIES_OP,
});
}
}

// Update actions
Expand Down Expand Up @@ -756,6 +776,19 @@ export default class Thing extends EventEmitter {
// Give the event a URL
event.forms.push({
href: `${this.href}${Constants.EVENTS_PATH}/${encodeURIComponent(eventName)}`,
subprotocol: 'sse',
});
}

// If there are events, add a top level form for them
if (Object.keys(description.events ?? {}).length > 0) {
this.forms.push({
href: `${this.href}/events`,
op: [
Constants.WoTOperation.SUBSCRIBE_ALL_EVENTS_OP,
Constants.WoTOperation.UNSUBSCRIBE_ALL_EVENTS_OP,
],
subprotocol: 'sse',
});
}

Expand Down