Skip to content

Commit

Permalink
refactor: add required AID terms (if missing) based on op (#1164)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpeintner authored Nov 21, 2023
1 parent a494687 commit 8554b5c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
79 changes: 79 additions & 0 deletions packages/td-tools/src/util/asset-interface-description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,82 @@ export class AssetInterfaceDescriptionUtil {
return formElementPicked;
}

private hasOp(form: FormElementBase, op: string): boolean {
if (form.op != null) {
if (typeof form.op === "string" && form.op === op) {
return true;
} else if (Array.isArray(form.op) && form.op.includes(op)) {
return true;
}
}
return false;
}

private addRequiredAidTermsForForm(form: FormElementBase, protocol: string): void {
if (form == null || protocol == null) {
return;
}
if (protocol.startsWith("http")) {
// HTTP: href, htv_methodName
// default for htv:methodName depending on op, see https://w3c.github.io/wot-binding-templates/bindings/protocols/http/index.html#http-default-vocabulary-terms
const htvKey = "htv:methodName";
if (form[htvKey] == null) {
if (
this.hasOp(form, "readproperty") ||
this.hasOp(form, "readallproperties") ||
this.hasOp(form, "readmultipleproperties")
) {
form[htvKey] = "GET";
} else if (
this.hasOp(form, "writeproperty") ||
this.hasOp(form, "writeallproperties") ||
this.hasOp(form, "writemultipleproperties")
) {
form[htvKey] = "PUT";
} else if (this.hasOp(form, "invokeaction")) {
form[htvKey] = "POST";
}
}
} else if (protocol.startsWith("modbus")) {
// Modbus: href, modbus_function
// default for modbus:function depending on op, see https://w3c.github.io/wot-binding-templates/bindings/protocols/modbus/index.html#default-mappings
const mbKey = "modbus:function";
if (form[mbKey] == null) {
if (this.hasOp(form, "writeproperty") || this.hasOp(form, "invokeaction")) {
form[mbKey] = "writeSingleCoil";
} else if (this.hasOp(form, "readallproperties") || this.hasOp(form, "readmultipleproperties")) {
form[mbKey] = "readHoldingRegisters";
} else if (this.hasOp(form, "writeallproperties") || this.hasOp(form, "writemultipleproperties")) {
form[mbKey] = "writeMultipleHoldingRegisters";
}
}
} else if (protocol.startsWith("mqtt")) {
// MQTT: href, mqv_controlPacket
// default for mqv:controlPacket depending on op, see https://w3c.github.io/wot-binding-templates/bindings/protocols/mqtt/index.html#default-mappings
const mqvKey = "mqv:controlPacket";
if (form[mqvKey] == null) {
if (
this.hasOp(form, "readproperty") ||
this.hasOp(form, "observeproperty") ||
this.hasOp(form, "readallproperties") ||
this.hasOp(form, "readmultipleproperties") ||
this.hasOp(form, "subscribeevent")
) {
form[mqvKey] = "subscribe";
} else if (
this.hasOp(form, "writeproperty") ||
this.hasOp(form, "writeallproperties") ||
this.hasOp(form, "writemultipleproperties") ||
this.hasOp(form, "invokeaction")
) {
form[mqvKey] = "publish";
} else if (this.hasOp(form, "unobserveproperty") || this.hasOp(form, "unsubscribeevent")) {
form[mqvKey] = "unsubscribe";
}
}
}
}

private createInterfaceMetadata(td: ThingDescription, protocol: string): Record<string, unknown> {
const properties: Array<unknown> = [];
const actions: Array<unknown> = [];
Expand Down Expand Up @@ -1264,6 +1340,9 @@ export class AssetInterfaceDescriptionUtil {
// TODO AID for now supports just *one* href/form
// --> pick the first one that matches protocol (other means in future?)

// AID has required terms that need to be present always for a given interface
this.addRequiredAidTermsForForm(formElementPicked, protocol);

// walk over string values like: "href", "contentType", "htv:methodName", ...
for (let formTerm in formElementPicked) {
let formValue = formElementPicked[formTerm];
Expand Down
1 change: 0 additions & 1 deletion packages/td-tools/test/AssetInterfaceDescriptionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,6 @@ class AssetInterfaceDescriptionUtilTest {
{
href: "stat",
contentType: "application/json",
"htv:methodName": "GET",
op: ["readproperty"],
},
],
Expand Down

0 comments on commit 8554b5c

Please sign in to comment.