Skip to content

Commit

Permalink
fix(coap-server): add forms to affordances in a more type-safe way
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 12, 2024
1 parent 8adf840 commit 9d04652
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/binding-coap/src/coap-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ import { Server, createServer, registerFormat, IncomingMessage, OutgoingMessage
import slugify from "slugify";
import { Readable } from "stream";
import { MdnsIntroducer } from "./mdns-introducer";
import { PropertyElement, DataSchema } from "wot-thing-description-types";
import { PropertyElement, DataSchema, ActionElement, EventElement } from "wot-thing-description-types";
import { CoapServerConfig } from "./coap";
import { DataSchemaValue } from "wot-typescript-definitions";

const { debug, warn, info, error } = createLoggers("binding-coap", "coap-server");

type CoreLinkFormatParameters = Map<string, string[] | number[]>;

type AffordanceElement = PropertyElement | ActionElement | EventElement;

// TODO: Move to core?
type AugmentedInteractionOptions = WoT.InteractionOptions & { formIndex: number };

Expand Down Expand Up @@ -228,6 +230,15 @@ export default class CoapServer implements ProtocolServer {
return opValues;
}

private addFormToAffordance(form: TD.Form, affordance: AffordanceElement): void {
const affordanceForms = affordance.forms;
if (affordanceForms == null) {
affordance.forms ??= [form];
} else {
affordanceForms.push(form);
}
}

private fillInPropertyBindingData(thing: ExposedThing, base: string, offeredMediaType: string) {
for (const [propertyName, property] of Object.entries(thing.properties)) {
const opValues = ProtocolHelpers.getPropertyOpValues(property);
Expand All @@ -241,7 +252,7 @@ export default class CoapServer implements ProtocolServer {
property.uriVariables
);

property.forms.push(form);
this.addFormToAffordance(form, property);
this.logHrefAssignment(form, "Property", propertyName);
}
}
Expand All @@ -258,7 +269,7 @@ export default class CoapServer implements ProtocolServer {
action.uriVariables
);

action.forms.push(form);
this.addFormToAffordance(form, action);
this.logHrefAssignment(form, "Action", actionName);
}
}
Expand All @@ -275,7 +286,7 @@ export default class CoapServer implements ProtocolServer {
event.uriVariables
);

event.forms.push(form);
this.addFormToAffordance(form, event);
this.logHrefAssignment(form, "Event", eventName);
}
}
Expand Down

0 comments on commit 9d04652

Please sign in to comment.