nope/lib/communication/getLayer.browser.ts

70 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

2021-03-24 06:50:36 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2021-03-23 07:30:19
2022-01-03 18:13:51 +00:00
* @modify date 2022-01-03 17:33:53
2021-03-24 06:50:36 +00:00
* @desc [description]
*/
import { generateId } from "../helpers/idMethods";
2022-01-03 18:13:51 +00:00
import { ValidLoggerDefinition } from "../logger/getLogger";
2021-03-24 06:50:36 +00:00
import { LoggerLevel } from "../logger/nopeLogger";
import { ICommunicationBridge } from "../types/nope/nopeCommunication.interface";
import { Bridge } from "./bridge";
import { IoSocketClientLayer } from "./layers/index.browser";
2021-03-24 06:50:36 +00:00
// Define the Valid Layers
export type validLayerOrMirror = "event" | "io-client" | "mqtt";
2021-03-24 06:50:36 +00:00
export const validLayers = {
event: Bridge,
"io-client": IoSocketClientLayer,
2021-03-24 06:50:36 +00:00
};
export const layerDefaultParameters = {
2021-12-04 07:25:26 +00:00
"io-client": "http://localhost:7000",
2021-03-24 06:50:36 +00:00
};
/**
* 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=false] the level of the debugger
2021-03-24 06:50:36 +00:00
* @return {*} {ICommunicationBridge}
*/
export function getLayer(
layer: validLayerOrMirror,
parameter: number | string = null,
logger: ValidLoggerDefinition = false
2021-03-24 06:50:36 +00:00
): ICommunicationBridge {
// Create the Bridge
2022-01-03 18:13:51 +00:00
const communicationBridge = new Bridge(generateId(), logger);
2021-03-24 06:50:36 +00:00
// Assign the Default Setting for the Channel.
const params = parameter !== null ? parameter : layerDefaultParameters[layer];
switch (layer) {
case "event":
break;
2021-09-03 05:42:37 +00:00
case "io-client":
communicationBridge.addCommunicationLayer(
new IoSocketClientLayer(params, logger),
2021-09-03 05:42:37 +00:00
true
);
2021-08-04 12:56:37 +00:00
break;
2021-03-24 06:50:36 +00:00
}
2022-01-03 18:13:51 +00:00
// Now that we have added a connection, we will
// update the value.
communicationBridge.connected.forcePublish();
2021-03-24 06:50:36 +00:00
// Return the Bridge
2021-09-02 06:20:26 +00:00
return communicationBridge as any as ICommunicationBridge;
2021-03-24 06:50:36 +00:00
}