Skip to content

Commit

Permalink
feat(coap-server): add support for URI variables
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Sep 11, 2023
1 parent a6d62fa commit 797b60e
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions packages/binding-coap/src/coap-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ export default class CoapServer implements ProtocolServer {
this.PROPERTY_DIR,
propertyName,
offeredMediaType,
opValues
opValues,
property.uriVariables,
thing.uriVariables
);

property.forms.push(form);
Expand All @@ -204,7 +206,9 @@ export default class CoapServer implements ProtocolServer {
this.ACTION_DIR,
actionName,
offeredMediaType,
"invokeaction"
"invokeaction",
action.uriVariables,
thing.uriVariables
);

action.forms.push(form);
Expand All @@ -215,10 +219,15 @@ export default class CoapServer implements ProtocolServer {

private fillInEventBindingData(thing: ExposedThing, base: string, port: number, offeredMediaType: string) {
for (const [eventName, event] of Object.entries(thing.events)) {
const [href, form] = this.createHrefAndForm(base, this.EVENT_DIR, eventName, offeredMediaType, [
"subscribeevent",
"unsubscribeevent",
]);
const [href, form] = this.createHrefAndForm(
base,
this.EVENT_DIR,
eventName,
offeredMediaType,
["subscribeevent", "unsubscribeevent"],
event.uriVariables,
thing.uriVariables
);

event.forms.push(form);

Expand All @@ -231,16 +240,36 @@ export default class CoapServer implements ProtocolServer {
affordancePathSegment: string,
affordanceName: string,
offeredMediaType: string,
opValues: string | string[]
opValues: string | string[],
affordanceUriVariables,
thingUriVariables
): [string, TD.Form] {
const href = this.createFormHref(base, affordancePathSegment, affordanceName);
const href = this.createFormHref(
base,
affordancePathSegment,
affordanceName,
affordanceUriVariables,
thingUriVariables
);
const form = this.createAffordanceForm(href, offeredMediaType, opValues);

return [href, form];
}

private createFormHref(base: string, affordancePathSegment: string, affordanceName: string) {
return `${base}/${affordancePathSegment}/${encodeURIComponent(affordanceName)}`;
private createFormHref(
base: string,
affordancePathSegment: string,
affordanceName: string,
affordanceUriVariables,
thingUriVariables
) {
const affordanceNamePattern = Helpers.updateInteractionNameWithUriVariablePattern(
affordanceName,
affordanceUriVariables,
thingUriVariables
);

return `${base}/${affordancePathSegment}/${encodeURIComponent(affordanceNamePattern)}`;
}

private createAffordanceForm(href: string, offeredMediaType: string, op: string[] | string) {
Expand Down

0 comments on commit 797b60e

Please sign in to comment.