Split up helper methods
This commit is contained in:
@@ -3,7 +3,7 @@ import express from "express";
|
|||||||
import { jsonization, types } from "@aas-core-works/aas-core3.0-typescript";
|
import { jsonization, types } from "@aas-core-works/aas-core3.0-typescript";
|
||||||
import AASInterfaceServer from "../server";
|
import AASInterfaceServer from "../server";
|
||||||
import type { OnRequestCallback } from "types/requests";
|
import type { OnRequestCallback } from "types/requests";
|
||||||
import AASHelper from "../aasHelper";
|
import Traverser from "../helper/traverser";
|
||||||
|
|
||||||
export type Config = {
|
export type Config = {
|
||||||
bindAddress: string,
|
bindAddress: string,
|
||||||
@@ -53,11 +53,11 @@ export default class HTTPInterfaceServer extends AASInterfaceServer<Config> {
|
|||||||
try {
|
try {
|
||||||
// This is what the specification says
|
// This is what the specification says
|
||||||
const decoded = Buffer.from(id, "base64").toString("utf-8");
|
const decoded = Buffer.from(id, "base64").toString("utf-8");
|
||||||
let sm = AASHelper.findSMById(this.aas, decoded);
|
let sm = Traverser.findSMById(this.aas, decoded);
|
||||||
|
|
||||||
if (sm === null) {
|
if (sm === null) {
|
||||||
// Normally, at least in BaSyx just the shortId is used, so just test if we find something like this
|
// Normally, at least in BaSyx just the shortId is used, so just test if we find something like this
|
||||||
sm = AASHelper.findSMByIdShort(this.aas, id);
|
sm = Traverser.findSMByIdShort(this.aas, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
return sm;
|
return sm;
|
||||||
@@ -134,7 +134,7 @@ export default class HTTPInterfaceServer extends AASInterfaceServer<Config> {
|
|||||||
switch (endingMatch) {
|
switch (endingMatch) {
|
||||||
case undefined: {
|
case undefined: {
|
||||||
if (req.method !== "GET") return res.status(501).end();
|
if (req.method !== "GET") return res.status(501).end();
|
||||||
const prop = AASHelper.getElementByIdPath(this.aas, sm, idShortPath);
|
const prop = Traverser.getElementByIdPath(this.aas, sm, idShortPath);
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
//! Check if mapping and live data is available!
|
//! Check if mapping and live data is available!
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
|
||||||
|
import { readdirSync, readFileSync } from "fs";
|
||||||
|
import { types, jsonization } from "@aas-core-works/aas-core3.0-typescript";
|
||||||
|
|
||||||
|
export default class FileImporter {
|
||||||
|
public static readAASByPath(path: string): types.Environment {
|
||||||
|
if (!path.endsWith(".json")) throw new Error("File must be a JSON file");
|
||||||
|
|
||||||
|
const file = readFileSync(path, { encoding: "utf-8" });
|
||||||
|
|
||||||
|
const aas = JSON.parse(file);
|
||||||
|
const aasJson = jsonization.environmentFromJsonable(aas);
|
||||||
|
if (aasJson.error) throw aasJson.error;
|
||||||
|
|
||||||
|
return aasJson.mustValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static readAllAASFromPath(path: string): types.Environment[] {
|
||||||
|
|
||||||
|
return FileImporter.getAllAASFilePaths(path).map(file => {
|
||||||
|
try {
|
||||||
|
return FileImporter.readAASByPath(file);
|
||||||
|
} catch (e) {
|
||||||
|
console.log(`Error while reading AAS from ${file}`, e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}).filter(aas => aas !== null) as types.Environment[];
|
||||||
|
}
|
||||||
|
|
||||||
|
private static getAllAASFilePaths(aasPath: string): string[] {
|
||||||
|
const files = readdirSync(aasPath);
|
||||||
|
return files.filter(file => file.endsWith(".json")).map(file => `${aasPath}/${file}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,39 +1,9 @@
|
|||||||
|
|
||||||
import { readdirSync, readFileSync } from "fs";
|
import { types } from "@aas-core-works/aas-core3.0-typescript";
|
||||||
import { types, jsonization } from "@aas-core-works/aas-core3.0-typescript";
|
|
||||||
|
|
||||||
const GLOBALLY_IDENTIFIABLES = [types.KeyTypes.GlobalReference, types.KeyTypes.AssetAdministrationShell, types.KeyTypes.ConceptDescription, types.KeyTypes.Identifiable, types.KeyTypes.Submodel]
|
const GLOBALLY_IDENTIFIABLES = [types.KeyTypes.GlobalReference, types.KeyTypes.AssetAdministrationShell, types.KeyTypes.ConceptDescription, types.KeyTypes.Identifiable, types.KeyTypes.Submodel]
|
||||||
|
|
||||||
export default class AASHelper {
|
export default class Traverser {
|
||||||
|
|
||||||
public static readAASByPath(path: string): types.Environment {
|
|
||||||
if (!path.endsWith(".json")) throw new Error("File must be a JSON file");
|
|
||||||
|
|
||||||
const file = readFileSync(path, { encoding: "utf-8" });
|
|
||||||
|
|
||||||
const aas = JSON.parse(file);
|
|
||||||
const aasJson = jsonization.environmentFromJsonable(aas);
|
|
||||||
if (aasJson.error) throw aasJson.error;
|
|
||||||
|
|
||||||
return aasJson.mustValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readAllAASFromPath(path: string): types.Environment[] {
|
|
||||||
|
|
||||||
return AASHelper.getAllAASFilePaths(path).map(file => {
|
|
||||||
try {
|
|
||||||
return AASHelper.readAASByPath(file);
|
|
||||||
} catch (e) {
|
|
||||||
console.log(`Error while reading AAS from ${file}`, e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}).filter(aas => aas !== null) as types.Environment[];
|
|
||||||
}
|
|
||||||
|
|
||||||
private static getAllAASFilePaths(aasPath: string): string[] {
|
|
||||||
const files = readdirSync(aasPath);
|
|
||||||
return files.filter(file => file.endsWith(".json")).map(file => `${aasPath}/${file}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static findAASById(environment: types.Environment, id: string): types.AssetAdministrationShell | null {
|
public static findAASById(environment: types.Environment, id: string): types.AssetAdministrationShell | null {
|
||||||
if (environment.assetAdministrationShells === null) return null;
|
if (environment.assetAdministrationShells === null) return null;
|
||||||
@@ -61,9 +31,9 @@ export default class AASHelper {
|
|||||||
if (current === null && !GLOBALLY_IDENTIFIABLES.includes(key.type)) break;
|
if (current === null && !GLOBALLY_IDENTIFIABLES.includes(key.type)) break;
|
||||||
|
|
||||||
if (current === null) {
|
if (current === null) {
|
||||||
current = AASHelper.findElement(env, element => (element as any).id === key.value)
|
current = Traverser.findElement(env, element => (element as any).id === key.value)
|
||||||
} else {
|
} else {
|
||||||
current = AASHelper.findElement(current, element => (element as any).idShort === key.value)
|
current = Traverser.findElement(current, element => (element as any).idShort === key.value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,16 +45,16 @@ export default class AASHelper {
|
|||||||
for (const shortId of shortIds) {
|
for (const shortId of shortIds) {
|
||||||
if (current === null) break;
|
if (current === null) break;
|
||||||
|
|
||||||
current = AASHelper.findChildByIdShort(current, shortId);
|
current = Traverser.findChildByIdShort(current, shortId);
|
||||||
}
|
}
|
||||||
|
|
||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static getElementByIdPath(env: types.Environment, submodelOrIdShort: string | types.Submodel, idShorts: string[]): types.Class | null {
|
public static getElementByIdPath(env: types.Environment, submodelOrIdShort: string | types.Submodel, idShorts: string[]): types.Class | null {
|
||||||
const sm = typeof submodelOrIdShort === "string" ? AASHelper.findSMByIdShort(env, submodelOrIdShort) : submodelOrIdShort;
|
const sm = typeof submodelOrIdShort === "string" ? Traverser.findSMByIdShort(env, submodelOrIdShort) : submodelOrIdShort;
|
||||||
if (sm === null) return null;
|
if (sm === null) return null;
|
||||||
return AASHelper.traverseByShortIds(sm, idShorts);
|
return Traverser.traverseByShortIds(sm, idShorts);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static findElement(start: types.Class, checkFunction: (element: types.Class) => boolean): types.Class | null {
|
public static findElement(start: types.Class, checkFunction: (element: types.Class) => boolean): types.Class | null {
|
||||||
+3
-3
@@ -1,8 +1,8 @@
|
|||||||
import AASHelper from "./aasHelper";
|
|
||||||
import MultiMessageBroker from "./multimessageBroker";
|
import MultiMessageBroker from "./multimessageBroker";
|
||||||
import HTTPInterfaceServer, { Config } from "./example_modules/httpInterfaceServer";
|
import HTTPInterfaceServer from "./example_modules/httpInterfaceServer";
|
||||||
|
import FileImporter from "helper/fileImporter";
|
||||||
|
|
||||||
const aas = AASHelper.readAASByPath("../AID-AIMC-Full-Example-20230911T1608.json");
|
const aas = FileImporter.readAASByPath("../AID-AIMC-Full-Example-20230911T1608.json");
|
||||||
|
|
||||||
const broker = new MultiMessageBroker();
|
const broker = new MultiMessageBroker();
|
||||||
broker.registerAAS({ aas, serverInterfaces: { serverInterface: HTTPInterfaceServer, config: { bindPort: 3000, bindAddress: "0.0.0.0" } } });
|
broker.registerAAS({ aas, serverInterfaces: { serverInterface: HTTPInterfaceServer, config: { bindPort: 3000, bindAddress: "0.0.0.0" } } });
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { types } from "@aas-core-works/aas-core3.0-typescript";
|
|||||||
import type AASInterfaceServer from "server";
|
import type AASInterfaceServer from "server";
|
||||||
import type { Request } from "types/requests";
|
import type { Request } from "types/requests";
|
||||||
import type InterfaceConnectionObject from "interfaceConnectionObject";
|
import type InterfaceConnectionObject from "interfaceConnectionObject";
|
||||||
import AASHelper from "./aasHelper";
|
import Traverser from "./helper/traverser";
|
||||||
import type { AIMCMapper } from "./parser/AIMCMapper";
|
import type { AIMCMapper } from "./parser/AIMCMapper";
|
||||||
import { AssetInterfaceDescription } from "types/aidConf";
|
import { AssetInterfaceDescription } from "types/aidConf";
|
||||||
import AIDParser from "./parser/AIDParser";
|
import AIDParser from "./parser/AIDParser";
|
||||||
|
|||||||
+10
-10
@@ -1,12 +1,12 @@
|
|||||||
import * as aasCore from "@aas-core-works/aas-core3.0-typescript";
|
import * as aasCore from "@aas-core-works/aas-core3.0-typescript";
|
||||||
import type { AssetInterfaceDescription, EndpointMetadata, InterfaceDescription, InterfaceMetadata, InterfaceAction, InterfaceEvent, InterfaceProperty, InterfacePropertyForm } from "types/aidConf";
|
import type { AssetInterfaceDescription, EndpointMetadata, InterfaceDescription, InterfaceMetadata, InterfaceAction, InterfaceEvent, InterfaceProperty, InterfacePropertyForm } from "types/aidConf";
|
||||||
import { AvailableEndpoint, endpointAvailable } from "../types/common";
|
import { AvailableEndpoint, endpointAvailable } from "../types/common";
|
||||||
import AASHelper from "../aasHelper";
|
import Traverser from "../helper/traverser";
|
||||||
|
|
||||||
export default class AIDParser {
|
export default class AIDParser {
|
||||||
|
|
||||||
public static parse(env: aasCore.types.Environment): AssetInterfaceDescription | null {
|
public static parse(env: aasCore.types.Environment): AssetInterfaceDescription | null {
|
||||||
const sm = AASHelper.findSMByIdShort(env, "AssetInterfaceDescription");
|
const sm = Traverser.findSMByIdShort(env, "AssetInterfaceDescription");
|
||||||
if (sm === null || sm.submodelElements === null) return null;
|
if (sm === null || sm.submodelElements === null) return null;
|
||||||
|
|
||||||
const parsed = {} as AssetInterfaceDescription;
|
const parsed = {} as AssetInterfaceDescription;
|
||||||
@@ -28,11 +28,11 @@ export default class AIDParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static parseAIDEntry(entry: aasCore.types.SubmodelElementCollection, endpointProtocol?: AvailableEndpoint): InterfaceDescription | null {
|
private static parseAIDEntry(entry: aasCore.types.SubmodelElementCollection, endpointProtocol?: AvailableEndpoint): InterfaceDescription | null {
|
||||||
const title = AASHelper.findElement(entry, element => (element as any).idShort.toLocaleLowerCase() === "title");
|
const title = Traverser.findElement(entry, element => (element as any).idShort.toLocaleLowerCase() === "title");
|
||||||
const titleString = title !== null && aasCore.types.isProperty(title) && title.value ? title.value : "";
|
const titleString = title !== null && aasCore.types.isProperty(title) && title.value ? title.value : "";
|
||||||
|
|
||||||
const ep = AASHelper.findElement(entry, element => (element as any).idShort.toLocaleLowerCase() === "endpointmetadata");
|
const ep = Traverser.findElement(entry, element => (element as any).idShort.toLocaleLowerCase() === "endpointmetadata");
|
||||||
const im = AASHelper.findElement(entry, element => (element as any).idShort.toLocaleLowerCase() === "interfacemetadata");
|
const im = Traverser.findElement(entry, element => (element as any).idShort.toLocaleLowerCase() === "interfacemetadata");
|
||||||
if (ep === null || !aasCore.types.isSubmodelElementCollection(ep) || !ep.value || im === null || !aasCore.types.isSubmodelElementCollection(im) || !im.value) return null;
|
if (ep === null || !aasCore.types.isSubmodelElementCollection(ep) || !ep.value || im === null || !aasCore.types.isSubmodelElementCollection(im) || !im.value) return null;
|
||||||
|
|
||||||
const parsedEp = AIDParser.parseEndpointMetaData(ep, endpointProtocol);
|
const parsedEp = AIDParser.parseEndpointMetaData(ep, endpointProtocol);
|
||||||
@@ -53,7 +53,7 @@ export default class AIDParser {
|
|||||||
const parsed = {} as EndpointMetadata;
|
const parsed = {} as EndpointMetadata;
|
||||||
|
|
||||||
for (const key of ["base", "contentType"]) {
|
for (const key of ["base", "contentType"]) {
|
||||||
const prop = AASHelper.findElement(endpoint, element => (element as any).idShort.toLocaleLowerCase() === key.toLocaleLowerCase());
|
const prop = Traverser.findElement(endpoint, element => (element as any).idShort.toLocaleLowerCase() === key.toLocaleLowerCase());
|
||||||
if (prop === null || !aasCore.types.isProperty(prop) || !prop.value) return null; // Mandatory
|
if (prop === null || !aasCore.types.isProperty(prop) || !prop.value) return null; // Mandatory
|
||||||
else parsed[key] = prop.value;
|
else parsed[key] = prop.value;
|
||||||
}
|
}
|
||||||
@@ -73,7 +73,7 @@ export default class AIDParser {
|
|||||||
events: []
|
events: []
|
||||||
} as InterfaceMetadata;
|
} as InterfaceMetadata;
|
||||||
|
|
||||||
const prop = AASHelper.findElement(interfaceMeta, element => (element as any).idShort.toLocaleLowerCase() === "properties");
|
const prop = Traverser.findElement(interfaceMeta, element => (element as any).idShort.toLocaleLowerCase() === "properties");
|
||||||
if (prop === null || !aasCore.types.isSubmodelElementList(prop) || !prop.value) return null; // Mandatory
|
if (prop === null || !aasCore.types.isSubmodelElementList(prop) || !prop.value) return null; // Mandatory
|
||||||
for (const element of prop.value) {
|
for (const element of prop.value) {
|
||||||
if (!aasCore.types.isSubmodelElementCollection(element)) continue;
|
if (!aasCore.types.isSubmodelElementCollection(element)) continue;
|
||||||
@@ -82,7 +82,7 @@ export default class AIDParser {
|
|||||||
parsed.properties.push(parsedProp);
|
parsed.properties.push(parsedProp);
|
||||||
}
|
}
|
||||||
|
|
||||||
const actions = AASHelper.findElement(interfaceMeta, element => (element as any).idShort.toLocaleLowerCase() === "actions");
|
const actions = Traverser.findElement(interfaceMeta, element => (element as any).idShort.toLocaleLowerCase() === "actions");
|
||||||
if (actions === null || !aasCore.types.isSubmodelElementList(actions) || !actions.value) return null; // Mandatory
|
if (actions === null || !aasCore.types.isSubmodelElementList(actions) || !actions.value) return null; // Mandatory
|
||||||
for (const element of actions.value) {
|
for (const element of actions.value) {
|
||||||
if (!aasCore.types.isSubmodelElementCollection(element)) continue;
|
if (!aasCore.types.isSubmodelElementCollection(element)) continue;
|
||||||
@@ -106,7 +106,7 @@ export default class AIDParser {
|
|||||||
else parsed[key] = prop.value;
|
else parsed[key] = prop.value;
|
||||||
} */
|
} */
|
||||||
|
|
||||||
const forms = AASHelper.findElement(prop, element => (element as any).idShort.toLocaleLowerCase() === "forms");
|
const forms = Traverser.findElement(prop, element => (element as any).idShort.toLocaleLowerCase() === "forms");
|
||||||
if (forms === null || !aasCore.types.isSubmodelElementList(forms) || !forms.value) return null; // Mandatory
|
if (forms === null || !aasCore.types.isSubmodelElementList(forms) || !forms.value) return null; // Mandatory
|
||||||
for (const element of forms.value) {
|
for (const element of forms.value) {
|
||||||
if (!aasCore.types.isSubmodelElementCollection(element)) continue;
|
if (!aasCore.types.isSubmodelElementCollection(element)) continue;
|
||||||
@@ -127,7 +127,7 @@ export default class AIDParser {
|
|||||||
const parsed = {} as any;
|
const parsed = {} as any;
|
||||||
|
|
||||||
for (const key of ["href", "contentType"]) {
|
for (const key of ["href", "contentType"]) {
|
||||||
const prop = AASHelper.findElement(form, element => (element as any).idShort.toLocaleLowerCase() === key.toLocaleLowerCase());
|
const prop = Traverser.findElement(form, element => (element as any).idShort.toLocaleLowerCase() === key.toLocaleLowerCase());
|
||||||
if (prop === null || !aasCore.types.isProperty(prop) || !prop.value) return null; // Mandatory
|
if (prop === null || !aasCore.types.isProperty(prop) || !prop.value) return null; // Mandatory
|
||||||
else parsed[key] = prop.value;
|
else parsed[key] = prop.value;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user