UNPKG

@citrineos/util

Version:

The OCPP util module which supplies helpful utilities like cache and queue connectors, etc.

109 lines 4.3 kB
// SPDX-FileCopyrightText: 2025 Contributors to the CitrineOS Project // // SPDX-License-Identifier: Apache-2.0 import { AbstractMessageSender, MessageState, OcppError } from '@citrineos/base'; import { instanceToPlain } from 'class-transformer'; import { Logger } from 'tslog'; import { RabbitMQChannelManager } from './ChannelManager.js'; import { RabbitMQConnectionManager } from './ConnectionManager.js'; /** * Implementation of a {@link IMessageSender} using RabbitMQ as the underlying transport. */ export class RabbitMqSender extends AbstractMessageSender { exchange; /** * Constants */ static CHANNEL_ID = 'sender'; /** * Fields */ _connectionManager; _channelManager; /** * Constructor for the class. * * @param {Logger<ILogObj>} [logger] - The logger object. */ constructor(exchange, connectionManager, channelManager, logger) { super(logger); this.exchange = exchange; this._connectionManager = connectionManager; this._channelManager = channelManager; } /** * 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, payload) { return this.send(message, payload, MessageState.Request); } /** * 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, payload) { return this.send(message, payload, MessageState.Response); } /** * 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. */ async send(message, payload, state) { if (this._connectionManager.isConnected() === false) { return { success: false, payload: 'RabbitMQ disconnected. Cannot send message.' }; } if (payload) { message.payload = payload; } if (state) { message.state = state; } if (!message.state) { return { success: false, payload: 'Message state must be set' }; } if (!message.payload) { return { success: false, payload: 'Message payload must be set' }; } const channel = await this._channelManager.getChannel(RabbitMqSender.CHANNEL_ID); if (!channel) { throw new Error('RabbitMQ is down: cannot send message.'); } this._logger.debug(`Publishing to ${this.exchange}:`, message); const success = channel.publish(this.exchange || '', '', Buffer.from(JSON.stringify(instanceToPlain(message)), 'utf-8'), { contentEncoding: 'utf-8', contentType: 'application/json', headers: { origin: message.origin.toString(), eventGroup: message.eventGroup.toString(), action: message.action.toString(), state: message.state.toString(), ...message.context, tenantId: message.context.tenantId.toString(), }, }); return { success }; } /** * Shuts down the sender by closing the client. * * @return {Promise<void>} A promise that resolves when the client is closed. */ async shutdown() { return; } } //# sourceMappingURL=sender.js.map