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

refactor(coap-server): simplify form generation #1084

Merged
merged 1 commit into from
Sep 15, 2023
Merged
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
59 changes: 18 additions & 41 deletions packages/binding-coap/src/coap-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ export default class CoapServer implements ProtocolServer {
for (const offeredMediaType of offeredMediaTypes) {
const base = this.createThingBase(address, port, urlPath);

this.fillInPropertyBindingData(thing, base, port, offeredMediaType);
this.fillInActionBindingData(thing, base, port, offeredMediaType);
this.fillInEventBindingData(thing, base, port, offeredMediaType);
this.fillInPropertyBindingData(thing, base, offeredMediaType);
this.fillInActionBindingData(thing, base, offeredMediaType);
this.fillInEventBindingData(thing, base, offeredMediaType);
}
}
}
Expand All @@ -182,10 +182,10 @@ export default class CoapServer implements ProtocolServer {
return `${this.scheme}://${address}:${port}/${encodeURIComponent(urlPath)}`;
}

private fillInPropertyBindingData(thing: ExposedThing, base: string, port: number, offeredMediaType: string) {
private fillInPropertyBindingData(thing: ExposedThing, base: string, offeredMediaType: string) {
for (const [propertyName, property] of Object.entries(thing.properties)) {
const opValues = ProtocolHelpers.getPropertyOpValues(property);
const [href, form] = this.createHrefAndForm(
const form = this.createAffordanceForm(
base,
this.PROPERTY_DIR,
propertyName,
Expand All @@ -196,13 +196,13 @@ export default class CoapServer implements ProtocolServer {
);

property.forms.push(form);
this.logHrefAssignment(port, href, "Property", propertyName);
this.logHrefAssignment(form, "Property", propertyName);
}
}

private fillInActionBindingData(thing: ExposedThing, base: string, port: number, offeredMediaType: string) {
private fillInActionBindingData(thing: ExposedThing, base: string, offeredMediaType: string) {
for (const [actionName, action] of Object.entries(thing.actions)) {
const [href, form] = this.createHrefAndForm(
const form = this.createAffordanceForm(
base,
this.ACTION_DIR,
actionName,
Expand All @@ -213,14 +213,13 @@ export default class CoapServer implements ProtocolServer {
);

action.forms.push(form);

this.logHrefAssignment(port, href, "Action", actionName);
this.logHrefAssignment(form, "Action", actionName);
}
}

private fillInEventBindingData(thing: ExposedThing, base: string, port: number, offeredMediaType: string) {
private fillInEventBindingData(thing: ExposedThing, base: string, offeredMediaType: string) {
for (const [eventName, event] of Object.entries(thing.events)) {
const [href, form] = this.createHrefAndForm(
const form = this.createAffordanceForm(
base,
this.EVENT_DIR,
eventName,
Expand All @@ -231,57 +230,35 @@ export default class CoapServer implements ProtocolServer {
);

event.forms.push(form);

this.logHrefAssignment(port, href, "Event", eventName);
this.logHrefAssignment(form, "Event", eventName);
}
}

private createHrefAndForm(
private createAffordanceForm(
base: string,
affordancePathSegment: string,
affordanceName: string,
offeredMediaType: string,
opValues: string | string[],
affordanceUriVariables: PropertyElement["uriVariables"] = {},
thingUriVariables: PropertyElement["uriVariables"] = {}
): [string, TD.Form] {
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,
affordanceUriVariables: PropertyElement["uriVariables"] = {},
thingUriVariables: PropertyElement["uriVariables"] = {}
) {
): TD.Form {
const affordanceNamePattern = Helpers.updateInteractionNameWithUriVariablePattern(
affordanceName,
affordanceUriVariables,
thingUriVariables
);

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

private createAffordanceForm(href: string, offeredMediaType: string, op: string[] | string) {
const form = new TD.Form(href, offeredMediaType);
form.op = op;
form.op = opValues;

return form;
}

private logHrefAssignment(port: number, href: string, affordanceType: string, affordanceName: string) {
debug(`CoapServer on port ${port} assigns '${href}' to ${affordanceType} '${affordanceName}'`);
private logHrefAssignment(form: TD.Form, affordanceType: string, affordanceName: string) {
debug(`CoapServer on port ${this.port} assigns '${form.href}' to ${affordanceType} '${affordanceName}'`);
}

private setUpIntroductionMethods(thing: ExposedThing, urlPath: string, port: number) {
Expand Down