@citrineos/util
Version:
The OCPP util module which supplies helpful utilities like cache and queue connectors, etc.
85 lines (84 loc) • 3.82 kB
TypeScript
import { AbstractMessageSender, IMessage, IMessageConfirmation, IMessageSender, MessageState, OcppError, OcppRequest, OcppResponse, SystemConfig } from '@citrineos/base';
import * as amqplib from 'amqplib';
import { ILogObj, Logger } from 'tslog';
/**
* Implementation of a {@link IMessageSender} using RabbitMQ as the underlying transport.
*/
export declare class RabbitMqSender extends AbstractMessageSender implements IMessageSender {
/**
* Constants
*/
private static readonly QUEUE_PREFIX;
private static readonly RECONNECT_DELAY;
/**
* Fields
*/
protected _connection?: amqplib.Connection;
protected _channel?: amqplib.Channel;
private _reconnecting;
private _abortReconnectController?;
/**
* Constructor for the class.
*
* @param {SystemConfig} config - The system configuration.
* @param {Logger<ILogObj>} [logger] - The logger object.
*/
constructor(config: SystemConfig, logger?: Logger<ILogObj>);
/**
* Methods
*/
/**
* Sends a request message with an optional payload and returns a promise that resolves to the confirmation message.
*
* @param {IMessage<OcppRequest>} message - The message to be sent.
* @param {OcppRequest | undefined} payload - The optional payload to be sent with the message.
* @return {Promise<IMessageConfirmation>} A promise that resolves to the confirmation message.
*/
sendRequest(message: IMessage<OcppRequest>, payload?: OcppRequest | undefined): Promise<IMessageConfirmation>;
/**
* Sends a response message and returns a promise of the message confirmation.
*
* @param {IMessage<OcppResponse | OcppError>} message - The message to send.
* @param {OcppResponse | OcppError} payload - The payload to include in the response.
* @return {Promise<IMessageConfirmation>} - A promise that resolves to the message confirmation.
*/
sendResponse(message: IMessage<OcppResponse | OcppError>, payload?: OcppResponse | OcppError): Promise<IMessageConfirmation>;
/**
* Sends a message and returns a promise that resolves to a message confirmation.
*
* @param {IMessage<OcppRequest | OcppResponse | OcppError>} message - The message to be sent.
* @param {OcppRequest | OcppResponse | OcppError} [payload] - The payload to be included in the message.
* @param {MessageState} [state] - The state of the message.
* @return {Promise<IMessageConfirmation>} - A promise that resolves to a message confirmation.
*/
send(message: IMessage<OcppRequest | OcppResponse | OcppError>, payload?: OcppRequest | OcppResponse | OcppError, state?: MessageState): Promise<IMessageConfirmation>;
/**
* Shuts down the sender by closing the client.
*
* @return {Promise<void>} A promise that resolves when the client is closed.
*/
shutdown(): Promise<void>;
/**
* Protected Methods
*/
/**
* Connect to RabbitMQ with retry logic.
* This method will keep trying to connect until successful, unless aborted.
*
* @param {AbortSignal} [abortSignal] - Optional abort signal to stop retrying.
* @return {Promise<amqplib.Channel>} A promise that resolves to the AMQP channel.
*/
protected _connectWithRetry(abortSignal?: AbortSignal): Promise<amqplib.Channel>;
/**
* Setup listeners for connection and channel events.
* This will handle disconnections and errors.
* Ensures listeners are not attached multiple times to the same connection.
*/
private _setupConnectionListeners;
/**
* Handle RabbitMQ disconnection.
* This method will attempt to reconnect to RabbitMQ when the connection is lost.
* Debounces concurrent reconnects.
*/
private _handleDisconnect;
}