import { type CallAction, type HandlerProperties, type OcppRequest, type OcppResponse, type OCPPVersionType, type SystemConfig, EventGroup, MessageOrigin } from '@citrineos/types'; import type { ICache } from '../cache/cache.js'; import type { AbstractHandler } from '../handlers/abstract-handler.js'; import type { IOcppSender } from '../handlers/i-ocpp-sender.js'; import type { IMessage, IMessageConfirmation, IMessageHandler, IMessageSender } from '../messages/index.js'; import type { IModule } from './module.js'; import { OcppError } from '../../ocpp/rpc/message.js'; import 'reflect-metadata'; import type { ILogObj } from 'tslog'; import { Logger } from 'tslog'; import { OCPPValidator } from './ocpp-validator.js'; /** * The dependencies every OCPP module receives through the container. Each concrete * module extends this with its own repositories and internal services. */ export interface OcppModuleDependencies { config: SystemConfig; cache: ICache; sender: IMessageSender; handler: IMessageHandler; logger: Logger; ocppValidator: OCPPValidator; ocppSender: IOcppSender; } export declare abstract class AbstractModule implements IModule { static readonly CALLBACK_URL_CACHE_PREFIX: string; protected _config: SystemConfig; protected _ocppValidator: OCPPValidator; protected readonly _cache: ICache; protected readonly _handler: IMessageHandler; protected readonly _sender: IMessageSender; protected readonly _eventGroup: EventGroup; protected readonly _logger: Logger; protected readonly _ocppSender: IOcppSender; /** * What this module subscribes to, derived in the constructor from its handlers' declared actions * minus any config exclusions. */ private readonly _requests; private readonly _responses; private startTime; private readonly _handlerInstancesByKey; private readonly _declaredRequests; private readonly _declaredResponses; constructor(config: SystemConfig, cache: ICache, handler: IMessageHandler, sender: IMessageSender, eventGroup: EventGroup, ocppSender: IOcppSender, logger?: Logger, ocppValidator?: OCPPValidator, handlers?: AbstractHandler[], excludedActions?: { requests?: CallAction[]; responses?: CallAction[]; }); /** * Builds the lookup key used by {@link _handlerInstancesByKey}, keyed on protocol, action, * and request/response type so a module can't accidentally dispatch a response to a * handler registered for the request side of the same action (or vice versa). */ private static _handlerKey; /** * The actions this module's handlers declare for one direction. Protocol agnostic */ private _declaredActions; /** * Getters & Setters */ get ocppValidator(): OCPPValidator; get cache(): ICache; get sender(): IMessageSender; get handler(): IMessageHandler; get config(): SystemConfig; /** * Sets the system configuration for the module. * * @param {SystemConfig} config - The new configuration to set. */ set config(config: SystemConfig); /** * Interface methods. */ /** * Handles a message with an OcppRequest or OcppResponse payload. * * @param {IMessage} message - The message to handle. * @param {HandlerProperties} props - Optional properties for the handler. * @return {void} This function does not return anything. */ handle(message: IMessage, props?: HandlerProperties): Promise; /** * Calls shutdown on the handler and sender. * * Note: To be overwritten by subclass if other logic is necessary. * TODO shutdown necessary for AbstractHandlers? */ shutdown(): Promise; /** * Sends a call with the specified identifier, tenantId, protocol, action, payload, and origin. * * @param ocppConnectionName - The connection name of the charging station * @param {number} tenantId - The identifier of the tenant. * @param {string} protocol - The subprotocol of the Websocket, i.e. "ocpp1.6" or "ocpp2.0.1". * @param {CallAction} action - The action to be performed. * @param {OcppRequest} payload - The payload of the call. * @param {string} [callbackUrl] - The callback URL for the call. * @param {string} [correlationId] - The correlation ID of the call. * @param {MessageOrigin} [origin] - The origin of the call. * @return {Promise} A promise that resolves to the message confirmation. */ sendCall(ocppConnectionName: string, tenantId: number, protocol: OCPPVersionType, action: CallAction, payload: OcppRequest, callbackUrl?: string, correlationId?: string, origin?: MessageOrigin): Promise; /** * Sends the call result message and returns a Promise that resolves with the confirmation message. * * @param {string} correlationId - The correlation ID of the message. * @param ocppConnectionName - The connection name of the charging station * @param {number} tenantId - The identifier of the tenant. * @param {string} protocol - The subprotocol of the Websocket, i.e. "ocpp1.6" or "ocpp2.0.1". * @param {CallAction} action - The call action. * @param {OcppResponse} payload - The payload of the call result message. * @param {MessageOrigin} origin - (optional) The origin of the message. * @return {Promise} A Promise that resolves with the confirmation message. */ sendCallResult(correlationId: string, ocppConnectionName: string, tenantId: number, protocol: OCPPVersionType, action: CallAction, payload: OcppResponse, origin?: MessageOrigin): Promise; /** * Sends the call result using the request message's fields. * Payload will overwrite message.payload. * * @param {IMessage} message - The request message object. * @param {OcppResponse} payload - The payload to send. * @return {Promise} A promise that resolves to the message confirmation. */ sendCallResultWithMessage(message: IMessage, payload: OcppResponse): Promise; /** * Sends the call error message and returns a Promise that resolves with the confirmation message. * * @param {string} correlationId - The correlation ID of the message. * @param ocppConnectionName - The connection name of the charging station * @param {number} tenantId - The identifier of the tenant. * @param {string} protocol - The subprotocol of the Websocket, i.e. "ocpp1.6" or "ocpp2.0.1". * @param {CallAction} action - The call action. * @param {OcppError} payload - The payload of the call error message. * @param {MessageOrigin} origin - (optional) The origin of the message. * @return {Promise} A Promise that resolves with the confirmation message. */ sendCallError(correlationId: string, ocppConnectionName: string, tenantId: number, protocol: OCPPVersionType, action: CallAction, payload: OcppError, origin?: MessageOrigin): Promise; /** * Sends the call error using the request message's fields. * Payload will overwrite message.payload. * * @param {IMessage} message - The request message object. * @param {OcppResponse} payload - The payload to send. * @return {Promise} A promise that resolves to the message confirmation. */ sendCallErrorWithMessage(message: IMessage, payload: OcppError): Promise; /** * Initializes the logger for the class. * * @return {Logger} The initialized logger. */ protected _initLogger(baseLogger?: Logger): Logger; /** * Initializes the handler for handling requests and responses. */ initHandlers(): Promise; /** * Initializes the handler for handling requests and responses. * * @param {CallAction[]} requests - The array of call actions for requests. * @param {CallAction[]} responses - The array of call actions for responses. * @return {Promise} Returns a promise that resolves to a boolean indicating if the initialization was successful. */ private _initHandler; }