Adding Mirror-Link

This commit is contained in:
Martin Karkowski 2021-03-22 13:54:28 +01:00
parent 90ec4741b6
commit 768d21f63c
4 changed files with 162 additions and 1 deletions

View File

@ -305,7 +305,11 @@ export interface ICommunicationBridge extends ICommunicationInterface {
* @param {boolean} forward Flag, that enables forwarding the Information to other Layers.
* @memberof ICommunicationBridge
*/
addLayer(layer: ICommunicationInterface, forward: boolean): void;
addLayer(
layer: ICommunicationInterface,
forwardData?: boolean,
considerConnection?: boolean
): void;
/**
* Function, to remove the Layer again.
*

0
modules/mirror/README.md Normal file
View File

View File

@ -0,0 +1,109 @@
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2021-03-22 13:53:54
* @modify date 2021-03-22 13:53:54
* @desc [description]
*/
import { IoSocketClient } from "../../../lib/communication/IoSocketClient";
import { exportProperty } from "../../../lib/decorators/moduleDecorators";
import { LoggerLevel } from "../../../lib/logger/nopeLogger";
import { InjectableNopeBaseModule } from "../../../lib/module/BaseModule.injectable";
import { NopeObservable } from "../../../lib/observables/nopeObservable";
import { ICommunicationInterface } from "../../../lib/types/nope/nopeCommunication.interface";
export class MirrorLinkModule extends InjectableNopeBaseModule {
/**
* Attribute holding a Flag, whether the Element is connected or not.
*
* @memberof MirrorLinkModule
*/
@exportProperty({
mode: "publish",
schema: {
description:
"An Observable containing the Value, whether the added connection is connected.",
type: "boolean"
},
topic: "connected"
})
public connected = new NopeObservable<boolean>();
protected _layer: ICommunicationInterface;
/**
* Initialize the client
*
* @param {string} uri URI of the Broker
* @param {IMqttSettings} [settings] Settings used to define the Element.
* @memberof MirrorLinkModule
*/
async init(
uri: "http://localhost:7001",
level: LoggerLevel = "info",
considerConnection = false,
waitForConnection = true
) {
const _this = this;
// Define the Author.
this.author = {
forename: "Martin",
mail: "m.karkowski@zema.de",
surename: "karkowski"
};
this.description = "A Module to connect to an mqtt-Broker";
this.version = {
date: new Date("22.03.2021"),
version: 1
};
await super.init();
// Define the Layer.
this._layer = (new IoSocketClient(
uri,
"mirror",
level
) as any) as ICommunicationInterface;
this._layer.considerConnection = considerConnection;
// Add the layer
this._dispatcher.communicator.addLayer(
this._layer,
true,
considerConnection
);
this._layer.connected.subscribe(() => _this.connected.forcePublish());
this.connected.getter = (value) => {
return _this._layer.connected.getContent();
};
this.connected.subscribe((connected) => {
// Every-Time we establish a connection, we say hello.
if (connected) {
_this._dispatcher.emitBonjour();
}
});
if (waitForConnection) {
await this.connected.waitFor((value) => value);
}
}
protected _id: string;
public async dispose() {
this._dispatcher.communicator.removeLayer(this._layer);
if (this._layer) {
await this._layer.dispose();
}
await super.dispose();
}
}

View File

@ -0,0 +1,48 @@
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-11-10 16:53:43
* @modify date 2021-02-12 17:12:09
* @desc [description]
*/
import { hostname } from "os";
import { IPackageDescription } from "../../../lib/types/nope/nopePackage.interface";
import { MirrorModule } from "./bridge.module";
const TYPES = {
mirrorLink: Symbol.for("mirrorLink")
};
export const DESCRIPTION: IPackageDescription<typeof TYPES> = {
activationHandlers: [],
autostart: {},
defaultInstances: [
{
options: {
identifier: hostname() + "-bridge-client",
params: ["io-client", "http://localhost:7000"],
type: MirrorModule.prototype.constructor.name.toString()
},
selector: MirrorModule.prototype.constructor.name.toString()
}
],
nameOfPackage: "bridgeLayer",
providedClasses: [
{
description: {
name: MirrorModule.prototype.constructor.name.toString(),
selector: TYPES.mirrorLink,
type: MirrorModule
},
settings: {
allowInstanceGeneration: true
}
}
],
providedFunctions: [],
requiredPackages: [],
types: TYPES
};
export default DESCRIPTION;