It lives!
This commit is contained in:
@@ -2,57 +2,70 @@ 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 AASStore from "aasStore";
|
||||
import AASHelper from "./aasHelper";
|
||||
import type { AIMCMapper } from "./parser/AIMCMapper";
|
||||
import { AssetInterfaceDescription } from "types/aidConf";
|
||||
import AIDParser from "./parser/AIDParser";
|
||||
|
||||
class MultiMessageBroker {
|
||||
private store = AASStore.getInstance();
|
||||
type AASRegistration = {
|
||||
aas: types.Environment,
|
||||
serverInterfaces: ServerInterfaceEntry<any> | ServerInterfaceEntry<any>[],
|
||||
}
|
||||
|
||||
private serverInterfaces: Array<typeof AASInterfaceServer> = [];
|
||||
private serverInterfaceConfigs: Array<any> = [];
|
||||
private serverInstances: Array<AASInterfaceServer<typeof this.serverInterfaceConfigs[number]>|null> = [];
|
||||
type AASRegistrationPrepared = AASRegistration & {
|
||||
serverInstaces?: AASInterfaceServer<any>[];
|
||||
connectorInterfaces?: InterfaceConnectionObject<any>[];
|
||||
mappingConfiguration?: AIMCMapper;
|
||||
}
|
||||
|
||||
private connectorInterfaces: Array<typeof InterfaceConnectionObject> = [];
|
||||
private connectorConfigs: Array<any> = [];
|
||||
private connectorInstances: Array<InterfaceConnectionObject<typeof this.connectorConfigs[keyof typeof this.connectorConfigs]>|null> = [];
|
||||
type ServerInterfaceEntry<ConfigInterface> = {
|
||||
serverInterface: typeof AASInterfaceServer<ConfigInterface>,
|
||||
config: ConfigInterface
|
||||
}
|
||||
|
||||
export default class MultiMessageBroker {
|
||||
private prepared: boolean = false;
|
||||
|
||||
private aasRegistrations: AASRegistrationPrepared[] = [];
|
||||
|
||||
public constructor() {}
|
||||
|
||||
public registerServerInterface(serverInterface: typeof AASInterfaceServer, config: any): void {
|
||||
this.serverInterfaces.push(serverInterface);
|
||||
this.serverInterfaceConfigs.push(config);
|
||||
public registerAAS(registration: AASRegistration): void {
|
||||
this.aasRegistrations.push(registration as AASRegistrationPrepared);
|
||||
}
|
||||
|
||||
public registerConnectorInterface(connectorInterface: typeof InterfaceConnectionObject, config: any): void {
|
||||
this.connectorInterfaces.push(connectorInterface);
|
||||
this.connectorConfigs.push(config);
|
||||
public prepare(): void {
|
||||
for (const registration of this.aasRegistrations) {
|
||||
|
||||
if (!Array.isArray(registration.serverInterfaces)) registration.serverInterfaces = [registration.serverInterfaces];
|
||||
|
||||
registration.serverInstaces = registration.serverInterfaces.map(serverInterface => {
|
||||
try {
|
||||
// @ts-ignore
|
||||
const server = new serverInterface.serverInterface<typeof serverInterface.config>(serverInterface.config, registration.aas, this.onInterfaceRequest.bind(this));
|
||||
server.prepare();
|
||||
return server;
|
||||
} catch (e) {
|
||||
console.error("Error while creating server interface instance", e);
|
||||
return null;
|
||||
}
|
||||
}).filter(i => i !== null);
|
||||
|
||||
const interfaceDescription = AIDParser.parse(registration.aas);
|
||||
|
||||
// TODO
|
||||
// Create connectors and mapping configuration
|
||||
}
|
||||
|
||||
this.prepared = true;
|
||||
}
|
||||
|
||||
private createServerInstances(aas: types.Environment): void {
|
||||
this.serverInstances = this.serverInterfaces.map((serverInterface, index) => {
|
||||
try {
|
||||
// @ts-ignore
|
||||
return new serverInterface(this.serverInterfaceConfigs[index], aas, this.onInterfaceRequest);
|
||||
} catch (e) {
|
||||
console.error(`Error while creating server instance for ${serverInterface.name}: ${e}`);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
public start(): void {
|
||||
if (!this.prepared) this.prepare();
|
||||
|
||||
private createConnectorInstances(): void {
|
||||
this.connectorInstances = this.connectorInterfaces.map((connectorInterface, index) => {
|
||||
try {
|
||||
// @ts-ignore
|
||||
return new connectorInterface(this.connectorConfigs[index], this.onConnectorEvent);
|
||||
} catch (e) {
|
||||
console.error(`Error while creating connector instance for ${connectorInterface.name}: ${e}`);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private connectorConfigsFromAAS(aas: types.Environment): void {
|
||||
// TODO
|
||||
for (const registration of this.aasRegistrations) {
|
||||
registration.serverInstaces?.forEach(server => server?.run());
|
||||
}
|
||||
}
|
||||
|
||||
private onInterfaceRequest(request: Request) {
|
||||
@@ -60,8 +73,10 @@ class MultiMessageBroker {
|
||||
}
|
||||
|
||||
private onConnectorEvent(response: any) {
|
||||
this.serverInstances.forEach(server => {
|
||||
if (server) server.notify(response);
|
||||
});
|
||||
for (const aas of this.aasRegistrations) {
|
||||
for (const server of aas.serverInstaces ?? []) {
|
||||
server?.notify(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user