nope/modules/mod-Converters/src/from-grpc-converter.ts
2020-09-10 18:21:19 +02:00

79 lines
2.6 KiB
TypeScript

/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2018-05-22 12:38:40
* @modify date 2020-09-09 16:02:48
* @desc [description]
*/
import { inject, injectable } from 'inversify';
import * as GRPC from '../../mod-GRPC-Interface/assembly/manual-assembly';
import { SystemLogger } from '../../mod-Logger/src/Unique.Logger';
import { AvailableFromConvertes, AvailableToConvertes, IConverter, IGrpcConfiguration } from '../type/interfaces';
@injectable()
export class FromGrpcConverter implements IConverter<Buffer, any, IGrpcConfiguration> {
public readonly name: AvailableFromConvertes | AvailableToConvertes;
private _logger = SystemLogger.logger.getLogger('converter.from.grpc');
/**
* Container for the Registered Topics
* @private
* @memberof BaseHTMLConverter
*/
private _convertOptions = new Map<string, any>();
/**
* Register only Endpoints.
*
* @param {string} identifier The Identifier for the Convertert
* @param {IDa3vidConfiguration} config The Config to use.
* @memberof BaseDa3vidConverter
*/
register(identifier: string, config: IGrpcConfiguration): void {
this._convertOptions.set(identifier, config);
}
/**
* Converts the Data.
*
* @param {*} data The Data to use.
* @param {string} identifier the Identifier to convert the Data
* @param {number} timeStamp the given TimeStamp.
* @returns {IDa3vidData}
* @memberof BaseDa3vidConverter
*/
convert(data: Buffer, identifier: string | null = null, timeStamp: number | null = null): any {
if (identifier != null) {
const config: IGrpcConfiguration = this._convertOptions.get(identifier);
if (config) {
return this._grpcRegistry.genMessage(config.packageName, config.messageName, data, 'decode');
}
const err = new TypeError('Type not registered for the From-GRPC-Conveter on ' + identifier);
this._logger.error('Type not registered for the From-GRPC-Conveter on "' + identifier + '"', err);
throw err;
}
const err = new TypeError('Identifier must be provided for the From-GRPC-Converter');
this._logger.error('Identifier must be provided for the From-GRPC-Converter', err);
throw err;
}
/**
* Creates an instance of FromGrpcConverter.
* @param {GRPC.Registery} _grpcRegistry
* @param {LOGGING.Logger} _logger
* @memberof FromGrpcConverter
*/
constructor(
@inject(GRPC.TYPES.Registry) private _grpcRegistry: GRPC.Registery,
) {
this.name = 'from-grpc';
}
}