Split lib from modules
This commit is contained in:
Generated
+2
-2402
File diff suppressed because it is too large
Load Diff
+1
-4
@@ -9,17 +9,14 @@
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"types": "dist/index.d.ts",
|
||||
"devDependencies": {
|
||||
"@types/express": "^4.17.19",
|
||||
"@types/node": "^20.8.2",
|
||||
"@types/uuid": "^9.0.6",
|
||||
"ts-node": "^10.9.1",
|
||||
"typescript": "^5.2.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@aas-core-works/aas-core3.0-typescript": "^1.0.0-rc.3",
|
||||
"express": "^4.18.2",
|
||||
"mqtt": "^5.1.2",
|
||||
"uuid": "^9.0.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { types } from "@aas-core-works/aas-core3.0-typescript";
|
||||
import { ConnectionConfiguration, AIMCMap } from "../types/aimcConf";
|
||||
import AIMCParser from "./AIMCParser";
|
||||
import Traverser from "../helper/traverser";
|
||||
import AIDParser from "./AIDParser";
|
||||
import { ConnectionConfiguration, AIMCMap } from "./types/aimcConf";
|
||||
import AIMCParser from "./parser/AIMCParser";
|
||||
import Traverser from "./helper/traverser";
|
||||
import AIDParser from "./parser/AIDParser";
|
||||
import { SubmodelElementCollection } from "@aas-core-works/aas-core3.0-typescript/dist/types/types";
|
||||
|
||||
export default class AIMCMapper {
|
||||
@@ -1,221 +0,0 @@
|
||||
import type { Express } from "express";
|
||||
import express from "express";
|
||||
import { jsonization, types } from "@aas-core-works/aas-core3.0-typescript";
|
||||
import AASInterfaceServer from "../server";
|
||||
import type { OnRequestCallback, Request } from "../types/requests";
|
||||
import Traverser from "../helper/traverser";
|
||||
|
||||
export type Config = {
|
||||
bindAddress: string,
|
||||
bindPort: number,
|
||||
}
|
||||
|
||||
export default class HTTPInterfaceServer extends AASInterfaceServer<Config> {
|
||||
public static serverInterfaceName: string = "HTTPInterfaceServer";
|
||||
public static supportsSubscriptions: boolean = false; // TODO, longpolling?
|
||||
|
||||
private app: Express = express();
|
||||
private server: any = null;
|
||||
private observers: any[] = [];
|
||||
|
||||
public prepare(): void {
|
||||
this.app.use(express.json());
|
||||
this.createRoutes();
|
||||
}
|
||||
|
||||
public run(): void {
|
||||
this.server = this.app.listen(this.config.bindPort, this.config.bindAddress, () => {
|
||||
console.log(`Listening on ${this.config.bindAddress}:${this.config.bindPort}`);
|
||||
});
|
||||
}
|
||||
|
||||
public stop(): void {
|
||||
if (this.server && this.server.close) this.server.close();
|
||||
}
|
||||
|
||||
public notify(event: any): void {
|
||||
this.observers.forEach(observer => {
|
||||
// TODO
|
||||
// Notify long pollers
|
||||
});
|
||||
}
|
||||
|
||||
private createRoutes(): void {
|
||||
const NOT_IMPLEMENTED = (_req: express.Request, res: express.Response) => res.status(501).end();
|
||||
|
||||
const getSM = (id: string) => {
|
||||
if (!id) return null;
|
||||
|
||||
try {
|
||||
// This is what the specification says
|
||||
const decoded = Buffer.from(id, "base64").toString("utf-8");
|
||||
let sm = Traverser.findSMById(this.aas, decoded);
|
||||
|
||||
if (sm === null) {
|
||||
// Normally, at least in BaSyx just the shortId is used, so just test if we find something like this
|
||||
sm = Traverser.findSMByIdShort(this.aas, id);
|
||||
}
|
||||
|
||||
return sm;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
this.app.use("/*", (req, res, next) => {
|
||||
console.log(req.originalUrl); next();
|
||||
});
|
||||
|
||||
// /aas
|
||||
this.app.get("/aas", (_, res) => res.json(jsonization.toJsonable(this.aas)).end());
|
||||
this.app.put("/aas", NOT_IMPLEMENTED);
|
||||
this.app.delete("/aas", NOT_IMPLEMENTED);
|
||||
// /aas/$reference
|
||||
this.app.get("/aas/$reference", NOT_IMPLEMENTED);
|
||||
// /aas/asset-information
|
||||
this.app.get("/aas/asset-information", NOT_IMPLEMENTED);
|
||||
this.app.put("/aas/asset-information", NOT_IMPLEMENTED);
|
||||
this.app.get("/aas/asset-information/thumbnail", NOT_IMPLEMENTED);
|
||||
this.app.put("/aas/asset-information/thumbnail", NOT_IMPLEMENTED);
|
||||
this.app.delete("/aas/asset-information/thumbnail", NOT_IMPLEMENTED);
|
||||
// /aas/submodel-refs
|
||||
this.app.get("/aas/submodel-refs", NOT_IMPLEMENTED);
|
||||
this.app.post("/aas/submodel-refs", NOT_IMPLEMENTED);
|
||||
this.app.delete("/aas/submodel-refs/:submodelIdentifier", NOT_IMPLEMENTED);
|
||||
// /aas/submodels
|
||||
this.app.get("/aas/submodels/:smId", (req, res) => {
|
||||
const sm = getSM(req.params.smId);
|
||||
|
||||
if (sm === null) return res.status(404).end();
|
||||
return res.json(jsonization.toJsonable(sm)).end()
|
||||
});
|
||||
this.app.put("/aas/submodels/:smId", NOT_IMPLEMENTED);
|
||||
this.app.patch("/aas/submodels/:smId", NOT_IMPLEMENTED);
|
||||
this.app.delete("/aas/submodels/:smId", NOT_IMPLEMENTED);
|
||||
|
||||
// /aas/submodels/:smId/submodel-elements
|
||||
this.app.use("/aas/submodels/:smId/submodel-elements", (req, res, next) => {
|
||||
if (req.path !== "/") return next();
|
||||
if (req.method !== "GET") return res.status(501).end();
|
||||
|
||||
const sm = getSM(req.params.smId);
|
||||
|
||||
if (sm === null || sm.submodelElements === null) return res.status(404).end();
|
||||
return res.json(sm.submodelElements.map(element => jsonization.toJsonable(element))).end();
|
||||
});
|
||||
|
||||
// Main function
|
||||
this.app.use("/aas/submodels/:smId/submodel-elements/*", (req, res) => {
|
||||
const idShortPathString = (req.params as any)[0];
|
||||
if (idShortPathString.endsWith("/")) res.status(400).end();
|
||||
|
||||
const sm = getSM(req.params.smId);
|
||||
if (sm === null || sm.submodelElements === null) return res.status(404).end();
|
||||
|
||||
if (idShortPathString === "") {
|
||||
if (req.method === "GET") {
|
||||
const json = sm.submodelElements.map(element => jsonization.toJsonable(element));
|
||||
return res.json(json).end();
|
||||
}
|
||||
}
|
||||
|
||||
// Currently we don't support these operations
|
||||
if (idShortPathString.endsWith("$value") ||
|
||||
idShortPathString.endsWith("$reference") ||
|
||||
idShortPathString.endsWith("$metadata") ||
|
||||
idShortPathString.endsWith("$path")) return res.status(501).end();
|
||||
|
||||
// If operation ids are changed, you can change the format here
|
||||
const endingMatchSearch = idShortPathString.match(/(\/value$|\/attachment$|\/invoke$|\/invoke-async$|\/operation-status\/[a-zA-Z0-9\-]+$|\/operation-result\/[a-zA-Z0-9\-]+$)/)
|
||||
const endingMatch = endingMatchSearch?.[0];
|
||||
|
||||
const idShortPath = endingMatch ? idShortPathString.replace(new RegExp(`${endingMatch}$`), "").split("/") : idShortPathString.split("/");
|
||||
|
||||
console.log(idShortPath, endingMatch)
|
||||
|
||||
switch (endingMatch) {
|
||||
case undefined: {
|
||||
if (req.method !== "GET") return res.status(501).end();
|
||||
const prop = Traverser.getElementByIdPath(this.aas, sm, idShortPath);
|
||||
if (prop === null || !types.isProperty(prop)) return res.status(404).end();
|
||||
|
||||
return res.json(jsonization.toJsonable(prop)).end();
|
||||
}
|
||||
case "/value":
|
||||
if (req.method !== "GET" && req.method !== "PUT") return res.status(501).end();
|
||||
const prop = Traverser.getElementByIdPath(this.aas, sm, idShortPath);
|
||||
if (prop === null || !types.isProperty(prop)) return res.status(404).end();
|
||||
|
||||
const request: Request = req.method === "GET" ? { type: "READ", target: prop } : { type: "WRITE", target: prop, extraData: { value: req.body } };
|
||||
|
||||
const value = this.onRequestCallback(request);
|
||||
|
||||
if (req.method === "PUT") res.status(204).end();
|
||||
|
||||
if (value === null) return res.status(500).end();
|
||||
return res.json(value).end();
|
||||
case "/attachment":
|
||||
return res.status(501).end();
|
||||
case "/invoke": {
|
||||
if (req.method !== "POST") return res.status(405).end();
|
||||
const op = Traverser.getElementByIdPath(this.aas, sm, idShortPath);
|
||||
if (op === null || !types.isOperation(op)) return res.status(404).end();
|
||||
|
||||
// TODO
|
||||
// Parameter?
|
||||
const success = this.onRequestCallback({ type: "CALL", target: op, extraData: { args: req.body } });
|
||||
|
||||
return res.send(success ? 204 : 500).end();
|
||||
}
|
||||
case "/invoke-async": {
|
||||
if (req.method !== "POST") return res.status(405).end();
|
||||
const op = Traverser.getElementByIdPath(this.aas, sm, idShortPath);
|
||||
if (op === null || !types.isOperation(op)) return res.status(404).end();
|
||||
|
||||
// TODO
|
||||
// Invoke operation and get handle
|
||||
const request: Request = { type: "CALL-ASYNC", target: op, extraData: { args: req.body } };
|
||||
|
||||
const handle = this.onRequestCallback(request);
|
||||
|
||||
const operationPaths = [`${idShortPath.join("/")}/operation-status/${handle}`, `${idShortPath.join("/")}/operation-result/${handle}`];
|
||||
return res.status(202).header("Location", operationPaths).end();
|
||||
}
|
||||
default: {
|
||||
// operation-status, operation-result or anything else
|
||||
const [_, operation, handle] = endingMatch.split("/");
|
||||
|
||||
switch (operation) {
|
||||
case "operation-status": {
|
||||
if (req.method !== "GET") return res.status(405).end();
|
||||
|
||||
const state = this.onRequestCallback({ type: "GET-OP-STATE", target: handle });
|
||||
|
||||
if (state === undefined) return res.status(404).end();
|
||||
|
||||
return res.json({
|
||||
finished: state,
|
||||
handle
|
||||
}).end();
|
||||
}
|
||||
case "operation-result": {
|
||||
if (req.method !== "GET") return res.status(405).end();
|
||||
|
||||
const result = this.onRequestCallback({ type: "GET-OP-RESULT", target: handle });
|
||||
if (result === null) return res.status(404).end();
|
||||
|
||||
return res.json({
|
||||
result,
|
||||
handle
|
||||
}).end();
|
||||
}
|
||||
default: {
|
||||
return res.status(404).end();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
import * as mqtt from "mqtt";
|
||||
import InterfaceConnectionObject from "../interfaceConnectionObject";
|
||||
import type { types } from "@aas-core-works/aas-core3.0-typescript";
|
||||
|
||||
export default class MQTTConnector extends InterfaceConnectionObject<mqtt.IClientOptions> {
|
||||
public static readonly connectorName: string = "MQTT Connector";
|
||||
public static readonly uriProtocol: string[] = ["mqtt", "mqtts"];
|
||||
public static readonly connectionType: "ON_DEMAND" | "PERMANENT" = "PERMANENT";
|
||||
public static readonly supportsSubscriptions: boolean = true;
|
||||
|
||||
private client: mqtt.MqttClient|null = null;
|
||||
|
||||
private readonly messageStore: Record<string, any> = {};
|
||||
|
||||
public connect(): boolean {
|
||||
if (!this.client) this.client = mqtt.connect(this.endpointMetadata.base, this.connectionParameter);
|
||||
this.client.on("message", (topic, message) => {
|
||||
//console.log(`${topic}: ${message.toString("utf-8")}`)
|
||||
// If json is expected you could parse it here
|
||||
this.messageStore[topic] = message.toString("utf-8");
|
||||
// Notify observers
|
||||
if (this.observerStore[topic] !== undefined) {
|
||||
this.observerStore[topic].forEach(cb => cb(message.toString("utf-8")));
|
||||
}
|
||||
});
|
||||
this.client.subscribe("#");
|
||||
return this.client.connected;
|
||||
}
|
||||
|
||||
public disconnect(): void {
|
||||
if (this.client) this.client.end();
|
||||
this.client = null;
|
||||
}
|
||||
|
||||
public readProperty(prop: types.Property): any {
|
||||
console.log(prop);
|
||||
const cc = this.mapper.get(prop);
|
||||
console.log(cc);
|
||||
if (cc === undefined) return null;
|
||||
|
||||
const errorReturn = cc.default ?? null;
|
||||
if (!this.client || !this.client.connected) return errorReturn;
|
||||
|
||||
console.log(this.messageStore)
|
||||
|
||||
const value = this.messageStore[cc.forms.href.substring(1)];
|
||||
console.log(value)
|
||||
if (value === undefined) return errorReturn;
|
||||
switch (cc.type) {
|
||||
case "integer":
|
||||
return Number.parseInt(value);
|
||||
case "float":
|
||||
return Number.parseFloat(value);
|
||||
case "boolean":
|
||||
return !!value;
|
||||
case "string":
|
||||
default:
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public writeProperty(prop: types.Property, value: any): boolean {
|
||||
const cc = this.mapper.get(prop);
|
||||
if (cc === undefined || !this.client || !this.client.connected) return false;
|
||||
|
||||
const topic = cc.forms.href.substring(1);
|
||||
if (topic === undefined) return false;
|
||||
|
||||
this.client.publish(topic, value.toString());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public observeProperty(prop: types.Property, callback: (value: any) => void): boolean {
|
||||
const cc = this.mapper.get(prop);
|
||||
if (cc === undefined || !cc.observable || !this.client || !this.client.connected) return false;
|
||||
|
||||
const topic = cc.forms.href.substring(1);
|
||||
if (this.observerStore[topic] === undefined) this.observerStore[topic] = [callback];
|
||||
else this.observerStore[topic].push(callback);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public callActionSync(action: types.Operation, args: Record<string, any>): any {
|
||||
// TODO
|
||||
// Vorher mal ne ordentliche Mapping-Definition
|
||||
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
public callActionAsync(action: types.Operation, args: Record<string, any>): string | null {
|
||||
// TODO
|
||||
// Vorher mal ne ordentliche Mapping-Definition
|
||||
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
public subscribeEvent(event: types.BasicEventElement, callback: (event: types.BasicEventElement) => void): boolean {
|
||||
// TODO
|
||||
// Vorher mal ne ordentliche Mapping-Definition
|
||||
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
public unsubscribeEvent(event: types.BasicEventElement): void {
|
||||
// TODO
|
||||
// Vorher mal ne ordentliche Mapping-Definition
|
||||
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export { default as FileImporter } from "./fileImporter";
|
||||
export { default as Traverser } from "./traverser";
|
||||
+8
-11
@@ -1,13 +1,10 @@
|
||||
import MultiMessageBroker from "./multimessageBroker";
|
||||
import HTTPInterfaceServer from "./example_modules/httpInterfaceServer";
|
||||
import FileImporter from "./helper/fileImporter";
|
||||
import MQTTConnector from "./example_modules/mqttConnector";
|
||||
export { default as MultiMessageBroker } from "./multimessageBroker";
|
||||
export { default as AbstractConnectionObject } from "./interfaceConnectionObject";
|
||||
export { default as AbstractInterfaceServer } from "./server";
|
||||
export { default as AIMCMapper } from "./AIMCMapper";
|
||||
|
||||
const aas = FileImporter.readAASByPath("../owntest.json");
|
||||
export * as Types from "./types";
|
||||
export { Traverser, FileImporter } from "./helper";
|
||||
export { AIDParser, AIMCParser } from "./parser";
|
||||
|
||||
const broker = new MultiMessageBroker();
|
||||
broker.registerInterfaceConnection({ interfaceConnection: MQTTConnector, config: { reconnectPeriod: 1000 }})
|
||||
broker.registerAAS({ aas, serverInterfaces: { serverInterface: HTTPInterfaceServer, config: { bindPort: 3000, bindAddress: "0.0.0.0" } } });
|
||||
|
||||
broker.prepare();
|
||||
broker.start();
|
||||
export { jsonization as AASCoreJsonization, types as AASCoreTypes } from "@aas-core-works/aas-core3.0-typescript";
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { types } from "@aas-core-works/aas-core3.0-typescript";
|
||||
import AIMCMapper from "./parser/AIMCMapper";
|
||||
import AIMCMapper from "./AIMCMapper";
|
||||
import { v4 } from "uuid";
|
||||
import type { EndpointMetadata } from "./types/aidConf";
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import { types } from "@aas-core-works/aas-core3.0-typescript";
|
||||
import type AASInterfaceServer from "./server";
|
||||
import type { Request } from "./types/requests";
|
||||
import type InterfaceConnectionObject from "interfaceConnectionObject";
|
||||
import AIMCMapper from "./parser/AIMCMapper";
|
||||
import AIMCMapper from "./AIMCMapper";
|
||||
import AIDParser from "./parser/AIDParser";
|
||||
import { EndpointMetadata } from "types/aidConf";
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
export { default as AIDParser } from "./AIDParser";
|
||||
export { default as AIMCParser } from "./AIMCParser";
|
||||
@@ -0,0 +1,4 @@
|
||||
export * as AIDTypes from "./aidConf";
|
||||
export * as AIMCTypes from "./aimcConf"
|
||||
export * as CommonTypes from "./common";
|
||||
export * as RequestTypes from "./requests";
|
||||
+3
-3
@@ -51,10 +51,10 @@
|
||||
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
||||
|
||||
/* Emit */
|
||||
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
||||
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
||||
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
||||
"declarationMap": true, /* Create sourcemaps for d.ts files. */
|
||||
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
||||
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
||||
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
||||
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
||||
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
||||
"outDir": "./dist", /* Specify an output folder for all emitted files. */
|
||||
|
||||
Reference in New Issue
Block a user