Skip to content

Commit

Permalink
refactor: allow TD to AID transformation without specifying protocol …
Browse files Browse the repository at this point in the history
…prefixes

-> use *all* possible prefixes
  • Loading branch information
danielpeintner committed Sep 7, 2023
1 parent 2765dde commit 186acaf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 16 deletions.
66 changes: 51 additions & 15 deletions packages/td-tools/src/util/asset-interface-description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@ const logError = debug(`${namespace}:error`);
*
*/

/*
* TODOs
* - 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?
* - More test-data for action & events, data input and output, ...
*
*/

export class AssetInterfaceDescriptionUtil {
/** @deprecated use transformAAS2TD method instead */
public transformToTD(aid: string, template?: string, submodelRegex?: string): string {
Expand Down Expand Up @@ -91,10 +81,10 @@ export class AssetInterfaceDescriptionUtil {
* Transform WoT ThingDescription (TD) to AAS in JSON format
*
* @param td input TD
* @param protocols protocol prefixes of interest (e.g., ["http", "coap"])
* @param protocols protocol prefixes of interest (e.g., ["http", "coap"]) or optional if all
* @returns transformed AAS in JSON format
*/
public transformTD2AAS(td: string, protocols: string[]): string {
public transformTD2AAS(td: string, protocols?: string[]): string {
const submodel = this.transformTD2SM(td, protocols);
const submodelObj = JSON.parse(submodel);
const submodelId = submodelObj.id;
Expand Down Expand Up @@ -136,16 +126,21 @@ export class AssetInterfaceDescriptionUtil {
* Transform WoT ThingDescription (TD) to AID submodel definition in JSON format
*
* @param td input TD
* @param protocols protocol prefixes of interest (e.g., ["http", "coap"])
* @param protocols protocol prefixes of interest (e.g., ["http", "coap"]) or optional if all
* @returns transformed AID submodel definition in JSON format
*/
public transformTD2SM(tdAsString: string, protocols: string[]): string {
public transformTD2SM(tdAsString: string, protocols?: string[]): string {
const td: ThingDescription = TDParser.parseTD(tdAsString);

const aidID = td.id ? td.id : "ID_" + Math.random();
const aidID = td.id ? td.id : "ID" + Math.random();

console.log("TD " + td.title + " parsed...");

// collect all possible prefixes
if (protocols === undefined || protocols.length === 0) {
protocols = this.getProtocolPrefixes(td);
}

const submdelElements = [];
for (const protocol of protocols) {
// use protocol binding prefix like "http" for name
Expand Down Expand Up @@ -197,6 +192,47 @@ export class AssetInterfaceDescriptionUtil {
*
*/

private getProtocolPrefixes(td: ThingDescription): string[] {
const protocols: string[] = [];

if (td.properties) {
for (const propertyKey in td.properties) {
const property = td.properties[propertyKey];
this.updateProtocolPrefixes(property.forms, protocols);
}
}
if (td.actions) {
for (const actionKey in td.actions) {
const action = td.actions[actionKey];
this.updateProtocolPrefixes(action.forms, protocols);
}

Check warning on line 208 in packages/td-tools/src/util/asset-interface-description.ts

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L206 - L208 were not covered by tests
}
if (td.events) {
for (const eventKey in td.events) {
const event = td.events[eventKey];
this.updateProtocolPrefixes(event.forms, protocols);
}

Check warning on line 214 in packages/td-tools/src/util/asset-interface-description.ts

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L212 - L214 were not covered by tests
}

return protocols;
}

private updateProtocolPrefixes(forms: [FormElementBase, ...FormElementBase[]], protocols: string[]): void {
if (forms) {
for (const interactionForm of forms) {
if (interactionForm.href) {
const positionColon = interactionForm.href.indexOf(":");
if (positionColon > 0) {
const prefix = interactionForm.href.substring(0, positionColon);
if (!protocols.includes(prefix)) {
protocols.push(prefix);
}
}
}
}
}
}

private getBaseFromEndpointMetadata(endpointMetadata?: Record<string, unknown>): string {
if (endpointMetadata?.value && endpointMetadata.value instanceof Array) {
for (const v of endpointMetadata.value) {
Expand Down
10 changes: 9 additions & 1 deletion packages/td-tools/test/AssetInterfaceDescriptionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class AssetInterfaceDescriptionUtilTest {
};

@test async "should correctly transform sample TD into JSON submodel"() {
const sm = this.assetInterfaceDescriptionUtil.transformTD2SM(JSON.stringify(this.td1), ["http"]);
const sm = this.assetInterfaceDescriptionUtil.transformTD2SM(JSON.stringify(this.td1), ["https"]);

const smObj = JSON.parse(sm);
expect(smObj).to.have.property("idShort").that.equals("AssetInterfacesDescription");
Expand Down Expand Up @@ -367,6 +367,14 @@ class AssetInterfaceDescriptionUtilTest {
}
}
expect(hasInterfaceMetadata, "No InterfaceMetadata").to.equal(true);

// Test to use all possible prefixes -> in this case it is only https
const sm2 = this.assetInterfaceDescriptionUtil.transformTD2SM(JSON.stringify(this.td1));
const sm2Obj = JSON.parse(sm2);
// Note: id is autogenerated and needs to be exluded/removed
delete smObj.id;
delete sm2Obj.id;
expect(smObj).to.eql(sm2Obj);
}

@test
Expand Down

0 comments on commit 186acaf

Please sign in to comment.