nope/lib/communication/getLayer.nodejs.ts
2021-12-04 08:25:26 +01:00

91 lines
2.6 KiB
TypeScript

/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2021-03-23 07:30:19
* @modify date 2021-04-09 09:28:02
* @desc [description]
*/
import { generateId } from "../helpers/idMethods";
import { LoggerLevel } from "../logger/nopeLogger";
import { ICommunicationBridge } from "../types/nope/nopeCommunication.interface";
import { Bridge } from "./bridge";
import { AmqpLayer } from "./layers/amqpLayer";
import { MQTTLayer } from "./layers/mqttLayer";
import { IoSocketMirrorClient } from "./mirrors/ioSocketMirrorClient";
import { IoSocketMirrorServer } from "./mirrors/ioSocketMirrorServer";
// Define the Valid Layers
export type validLayerOrMirror =
| "event"
| "amqp"
| "io-server"
| "io-client"
| "mqtt";
export const validLayers = {
event: Bridge,
amqp: AmqpLayer,
"io-server": IoSocketMirrorServer,
"io-client": IoSocketMirrorClient,
mqtt: MQTTLayer,
};
export const layerDefaultParameters = {
amqp: "localhost",
"io-server": 7000,
"io-client": "http://localhost:7000",
mqtt: "mqtt://localhost:1883",
};
/**
* Function, that will create a Bridge, based on the provided function.
* Based on the parameter "layer", a corresponding layer or mirror will
* be added to the bridge. You can provide custom parameters using the
* parameter "parameter". This will receive either the uri or ports.
* Additionally you are able to assign a log-level to the bridge.
*
* @export
* @param {validLayerOrMirror} layer the layer to add
* @param {(number | string)} [parameter=null] the parameter required for the additonal layer / mirror
* @param {LoggerLevel} level the level of the debugger
* @return {*} {ICommunicationBridge}
*/
export function getLayer(
layer: validLayerOrMirror,
parameter: number | string = null,
level: LoggerLevel
): ICommunicationBridge {
// Create the Bridge
const communicationBridge = new Bridge(generateId(), "communication", level);
// Assign the Default Setting for the Channel.
const params = parameter !== null ? parameter : layerDefaultParameters[layer];
switch (layer) {
case "amqp":
communicationBridge.addLayer(new AmqpLayer(params));
break;
case "event":
break;
case "io-client":
communicationBridge.addMirror(
new IoSocketMirrorClient(params, level),
true
);
break;
case "io-server":
communicationBridge.addMirror(
new IoSocketMirrorServer(params, level),
true
);
break;
case "mqtt":
communicationBridge.addLayer(new MQTTLayer(params) as any);
break;
}
// Return the Bridge
return communicationBridge as any as ICommunicationBridge;
}