Skip to content

Commit 112dce2

Browse files
refactor: use base in hrefs for AID (#1135)
1 parent 247b0d4 commit 112dce2

2 files changed

Lines changed: 32 additions & 29 deletions

File tree

packages/td-tools/src/util/asset-interface-description.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,17 @@ export class AssetInterfaceDescriptionUtil {
258258
return ""; // TODO what is the right value if information cannot be found
259259
}
260260

261-
private getSecurityDefinitionsFromEndpointMetadata(endpointMetadata?: Record<string, unknown>): {
262-
[k: string]: SecurityScheme;
263-
} {
261+
private updateRootMetadata(thing: Thing, endpointMetadata?: Record<string, unknown>) {
264262
const securityDefinitions: {
265263
[k: string]: SecurityScheme;
266264
} = {};
265+
const security: string[] = [];
267266

268267
if (endpointMetadata?.value instanceof Array) {
269268
for (const v of endpointMetadata.value) {
270-
if (v.idShort === "securityDefinitions") {
271-
// const securitySchemes: Array<SecurityScheme> = [];
269+
if (v.idShort === "base") {
270+
thing.base = v.value;
271+
} else if (v.idShort === "securityDefinitions") {
272272
if (v.value instanceof Array) {
273273
for (const securityDefinitionsValues of v.value) {
274274
if (securityDefinitionsValues.idShort != null) {
@@ -286,19 +286,7 @@ export class AssetInterfaceDescriptionUtil {
286286
}
287287
}
288288
}
289-
}
290-
}
291-
}
292-
return securityDefinitions;
293-
}
294-
295-
private getSecurityFromEndpointMetadata(
296-
endpointMetadata?: Record<string, unknown>
297-
): string | [string, ...string[]] {
298-
const security: string[] = [];
299-
if (endpointMetadata?.value instanceof Array) {
300-
for (const v of endpointMetadata.value) {
301-
if (v.idShort === "security") {
289+
} else if (v.idShort === "security") {
302290
if (v.value instanceof Array) {
303291
for (const securityValue of v.value) {
304292
if (securityValue.value != null) {
@@ -310,7 +298,8 @@ export class AssetInterfaceDescriptionUtil {
310298
}
311299
}
312300

313-
return security as string | [string, ...string[]];
301+
thing.securityDefinitions = securityDefinitions;
302+
thing.security = security as string | [string, ...string[]];
314303
}
315304

316305
private createInteractionForm(vi: AASInteraction, addSecurity: boolean): TD.Form {
@@ -568,8 +557,8 @@ export class AssetInterfaceDescriptionUtil {
568557
const secNamesForEndpointMetadata = new Map<Record<string, unknown>, string[]>();
569558
for (const endpointMetadata of smInformation.endpointMetadataArray) {
570559
const secNames: Array<string> = [];
571-
thing.securityDefinitions = this.getSecurityDefinitionsFromEndpointMetadata(endpointMetadata);
572-
thing.security = this.getSecurityFromEndpointMetadata(endpointMetadata);
560+
// update base, securityDefinitions, security, ...
561+
this.updateRootMetadata(thing, endpointMetadata);
573562
// iterate over securitySchemes
574563
// eslint-disable-next-line unused-imports/no-unused-vars
575564
for (const [key, value] of Object.entries(thing.securityDefinitions)) {
@@ -925,7 +914,20 @@ export class AssetInterfaceDescriptionUtil {
925914

926915
// walk over string values like: "href", "contentType", "htv:methodName", ...
927916
for (let formTerm in formElementPicked) {
928-
const formValue = formElementPicked[formTerm];
917+
let formValue = formElementPicked[formTerm];
918+
919+
// Note: node-wot uses absolute URIs *almost* everywhere but we want to use "base" in AID
920+
// --> try to create relative href's as much as possible
921+
if (
922+
formTerm === "href" &&
923+
td.base != null &&
924+
td.base.length > 0 &&
925+
typeof formValue === "string" &&
926+
formValue.startsWith(td.base)
927+
) {
928+
formValue = formValue.substring(td.base.length);
929+
console.log("dsadsa: " + formValue);
930+
}
929931

930932
// Note: AID does not allow idShort to contain values with colon (i.e., ":") --> "_" used instead
931933
// TODO are there more characters we need to deal with?

packages/td-tools/test/AssetInterfaceDescriptionTest.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,14 @@ class AssetInterfaceDescriptionUtilTest {
264264
hasEndpointMetadata = true;
265265
const endpointMetadata = smValue;
266266
expect(endpointMetadata).to.have.property("value").to.be.an("array").to.have.lengthOf.greaterThan(0);
267+
let hasBase = false;
267268
let hasSecurity = false;
268269
let hasSecurityDefinitions = false;
269270
for (const endpointMetadataValue of endpointMetadata.value) {
270-
if (endpointMetadataValue.idShort === "security") {
271+
if (endpointMetadataValue.idShort === "base") {
272+
hasBase = true;
273+
expect(endpointMetadataValue.value).to.equal("modbus+tcp://192.168.178.146:502/");
274+
} else if (endpointMetadataValue.idShort === "security") {
271275
hasSecurity = true;
272276
expect(endpointMetadataValue)
273277
.to.have.property("value")
@@ -301,6 +305,7 @@ class AssetInterfaceDescriptionUtilTest {
301305
expect(hasBasicSC).to.equal(true);
302306
}
303307
}
308+
expect(hasBase).to.equal(true);
304309
expect(hasSecurity).to.equal(true);
305310
expect(hasSecurityDefinitions).to.equal(true);
306311
}
@@ -358,9 +363,7 @@ class AssetInterfaceDescriptionUtilTest {
358363
for (const formEntry of propProperty.value) {
359364
if (formEntry.idShort === "href") {
360365
hasHref = true;
361-
expect(formEntry.value).to.equal(
362-
"modbus+tcp://192.168.178.146:502/1/40020?quantity=16"
363-
);
366+
expect(formEntry.value).to.equal("1/40020?quantity=16");
364367
} else if (formEntry.idShort === "op") {
365368
hasOp = true;
366369
expect(formEntry.value).to.equal("readproperty");
@@ -425,9 +428,7 @@ class AssetInterfaceDescriptionUtilTest {
425428
for (const formEntry of propProperty.value) {
426429
if (formEntry.idShort === "href") {
427430
hasHref = true;
428-
expect(formEntry.value).to.equal(
429-
"modbus+tcp://192.168.178.146:502/40361?quantity=1"
430-
);
431+
expect(formEntry.value).to.equal("40361?quantity=1"); // use base
431432
} else if (formEntry.idShort === "op") {
432433
hasOp = true;
433434
expect(formEntry.value).to.equal("readproperty");

0 commit comments

Comments
 (0)