From f28dcf5dc1d93773e9bc32fb16185294d8e61f7e Mon Sep 17 00:00:00 2001 From: danielpeintner Date: Thu, 7 Sep 2023 10:07:58 +0200 Subject: [PATCH] refactor: add support for thing metadata --- .../src/util/asset-interface-description.ts | 35 +++++++++++++------ .../test/AssetInterfaceDescriptionTest.ts | 5 +-- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/packages/td-tools/src/util/asset-interface-description.ts b/packages/td-tools/src/util/asset-interface-description.ts index db9f76ef7..eb9c40192 100644 --- a/packages/td-tools/src/util/asset-interface-description.ts +++ b/packages/td-tools/src/util/asset-interface-description.ts @@ -34,6 +34,8 @@ const logInfo = debug(`${namespace}:info`); /* * TODOs + * - Support desription(s) and title(s) + * - transformToTD without any binding prefix (Idea: collect first all possible bindings) * - what is the desired input/output? string, object, ... ? * - what are options that would be desired? (context version, id, security, ...) -> template mechanism fine? * - Fields like @context, id, .. etc representable in AID? @@ -52,6 +54,8 @@ interface SubmodelInformation { actions: Map>; events: Map>; + thing: Map>; + endpointMetadataArray: Array>; } @@ -305,6 +309,12 @@ export class AssetInterfaceDescriptionUtil { // e.g., idShort: base , contentType, securityDefinitions, alternativeEndpointDescriptor? endpointMetadata = smValue; smInformation.endpointMetadataArray.push(endpointMetadata); + } else if (smValue.idShort === "InterfaceMetadata") { + // handled later + } else if (smValue.idShort === "externalDescriptor") { + // needed? + } else { + smInformation.thing.set(smValue.idShort, smValue.value); } } } @@ -374,6 +384,7 @@ export class AssetInterfaceDescriptionUtil { events: new Map>(), properties: new Map>(), endpointMetadataArray: [], + thing: new Map>(), }; if (aidModel instanceof Object && aidModel.submodels) { @@ -390,7 +401,16 @@ export class AssetInterfaceDescriptionUtil { private _transform(smInformation: SubmodelInformation, template?: string): string { const thing: Thing = template ? JSON.parse(template) : {}; - // TODO required fields possible in AID also? + // walk over thing information and set them + for (const [key, value] of smInformation.thing) { + if (typeof value === "string") { + thing[key] = value; + } else { + // TODO what to do with non-string values? + } + } + + // required TD fields if (!thing["@context"]) { thing["@context"] = "https://www.w3.org/2022/wot/td/v1.1"; } @@ -426,9 +446,7 @@ export class AssetInterfaceDescriptionUtil { if (smInformation.properties.size > 0) { thing.properties = {}; - for (const entry of smInformation.properties.entries()) { - const key = entry[0]; - const value: AASInteraction[] = entry[1]; + for (const [key, value] of smInformation.properties.entries()) { logInfo("Property" + key + " = " + value); thing.properties[key] = {}; @@ -480,9 +498,7 @@ export class AssetInterfaceDescriptionUtil { if (smInformation.actions.size > 0) { thing.actions = {}; - for (const entry of smInformation.actions.entries()) { - const key = entry[0]; - const value: AASInteraction[] = entry[1]; + for (const [key, value] of smInformation.actions.entries()) { logInfo("Action" + key + " = " + value); thing.actions[key] = {}; @@ -503,9 +519,7 @@ export class AssetInterfaceDescriptionUtil { if (smInformation.events.size > 0) { thing.events = {}; - for (const entry of smInformation.events.entries()) { - const key = entry[0]; - const value: AASInteraction[] = entry[1]; + for (const [key, value] of smInformation.events.entries()) { logInfo("Event " + key + " = " + value); thing.events[key] = {}; @@ -558,6 +572,7 @@ export class AssetInterfaceDescriptionUtil { events: new Map>(), properties: new Map>(), endpointMetadataArray: [], + thing: new Map>(), }; this.processSubmodel(smInformation, submodel, submodelRegex); diff --git a/packages/td-tools/test/AssetInterfaceDescriptionTest.ts b/packages/td-tools/test/AssetInterfaceDescriptionTest.ts index b7a739437..d84cc5464 100644 --- a/packages/td-tools/test/AssetInterfaceDescriptionTest.ts +++ b/packages/td-tools/test/AssetInterfaceDescriptionTest.ts @@ -26,11 +26,12 @@ class AssetInterfaceDescriptionUtilTest { @test async "should correctly transform counterHTTP into a TD"() { const modelAID = (await fs.readFile("test/util/counterHTTP.json")).toString(); - const td = this.assetInterfaceDescriptionUtil.transformAAS2TD(modelAID, `{"title": "counter"}`); + const td = this.assetInterfaceDescriptionUtil.transformAAS2TD(modelAID, `{"title": "bla"}`); const tdObj = JSON.parse(td); expect(tdObj).to.have.property("@context").that.equals("https://www.w3.org/2022/wot/td/v1.1"); - expect(tdObj).to.have.property("title").that.equals("counter"); + expect(tdObj).to.have.property("title").that.equals("Counter"); // should come form AAS + expect(tdObj).to.have.property("support").that.equals("https://github.com/eclipse-thingweb/node-wot/"); expect(tdObj).to.have.property("securityDefinitions").to.be.an("object");