@citrineos/ocpprouter
Version:
The ocpprouter module for OCPP v2.0.1. This module is not intended to be used directly, but rather as a dependency for other modules.
140 lines (139 loc) • 8.74 kB
TypeScript
import type { BootstrapConfig, Call, CallAction, CallError, CallResult, ICache, IMessageConfirmation, IMessageHandler, IMessageRouter, IMessageSender, OcppRequest, OcppResponse, OCPPVersionType, SystemConfig } from '@citrineos/base';
import { AbstractMessageRouter, MessageOrigin, OcppError, OCPPValidator, OCPPVersion } from '@citrineos/base';
import type { ILocationRepository } from '@citrineos/data';
import type { ILogObj } from 'tslog';
import { Logger } from 'tslog';
import { WebhookDispatcher } from './webhook.dispatcher.js';
/**
* Implementation of the ocpp router
*/
export declare class MessageRouterImpl extends AbstractMessageRouter implements IMessageRouter {
/**
* Fields
*/
private _webhookDispatcher;
protected _cache: ICache;
protected _sender: IMessageSender;
protected _handler: IMessageHandler;
protected _networkHook: (identifier: string, message: string) => Promise<void>;
protected _locationRepository: ILocationRepository;
private readonly _oidcTokenProvider?;
/**
* Constructor for the class.
*
* @param {BootstrapConfig & SystemConfig} config - the system configuration
* @param {ICache} cache - the cache object
* @param {IMessageSender} [sender] - the message sender
* @param {IMessageHandler} [handler] - the message handler
* @param {WebhookDispatcher} [dispatcher] - the webhook dispatcher
* @param {Function} networkHook - the network hook needed to send messages to chargers
* @param {ILocationRepository} [locationRepository] - An optional parameter of type {@link ILocationRepository} which
* represents a repository for accessing and manipulating variable data.
* If no `locationRepository` is provided, a default {@link locationRepository} instance is created and used.
* @param {Logger<ILogObj>} [logger] - the logger object (optional)
* @param {OCPPValidator} [ocppValidator] - the OCPPValidator instance, for message validation (optional)
*/
constructor(config: BootstrapConfig & SystemConfig, cache: ICache, sender: IMessageSender, handler: IMessageHandler, dispatcher: WebhookDispatcher, networkHook: (identifier: string, message: string) => Promise<void>, logger?: Logger<ILogObj>, ocppValidator?: OCPPValidator, locationRepository?: ILocationRepository);
doesChargingStationExistByStationId(tenantId: number, stationId: string): Promise<boolean>;
registerConnection(tenantId: number, stationId: string, protocol: OCPPVersion): Promise<boolean>;
deregisterConnection(tenantId: number, stationId: string): Promise<boolean>;
onMessage(identifier: string, message: string, timestamp: Date, protocol: OCPPVersionType): Promise<boolean>;
/**
* Sends a Call message to a charging station with given identifier.
*
* @param {string} stationId - The identifier of the station.
* @param {number} tenantId - The identifier of the tenant.
* @param {OCPPVersionType} protocol The OCPP protocol version of the message.
* @param {CallAction} action - The action to be called.
* @param {OcppRequest} payload - The payload of the call.
* @param {string} correlationId - The correlation ID of the message.
* @param {MessageOrigin} _origin - The origin of the message.
* @return {Promise<boolean>} A promise that resolves to a boolean indicating if the call was sent successfully.
*/
sendCall(stationId: string, tenantId: number, protocol: OCPPVersionType, action: CallAction, payload: OcppRequest, correlationId?: string, _origin?: MessageOrigin): Promise<IMessageConfirmation>;
/**
* Sends the CallResult to a charging station with given identifier.
*
* @param {string} correlationId - The correlation ID of the message.
* @param {string} stationId - The identifier of the charging station.
* @param {number} tenantId - The identifier of the tenant.
* @param {OCPPVersionType} protocol The OCPP protocol version of the message.
* @param {CallAction} action - The action to be called.
* @param {OcppRequest} payload - The payload of the call.
* @param {MessageOrigin} _origin - The origin of the message.
* @return {Promise<boolean>} A promise that resolves to true if the call result was sent successfully, or false otherwise.
*/
sendCallResult(correlationId: string, stationId: string, tenantId: number, protocol: OCPPVersionType, action: CallAction, payload: OcppResponse, _origin?: MessageOrigin): Promise<IMessageConfirmation>;
/**
* Sends a CallError message to a charging station with given identifier.
*
* @param {string} correlationId - The correlation ID of the message.
* @param {string} stationId - The identifier of the charging station.
* @param {number} tenantId - The identifier of the tenant.
* @param {OCPPVersionType} protocol The OCPP protocol version of the message.
* @param {CallAction} _action - The action to be called.
* @param {OcppError} error - The error of the call.
* @param {MessageOrigin} _origin - The origin of the message.
* @return {Promise<boolean>} - A promise that resolves to true if the message was sent successfully.
*/
sendCallError(correlationId: string, stationId: string, tenantId: number, protocol: OCPPVersionType, action: CallAction, error: OcppError, _origin?: MessageOrigin | undefined): Promise<IMessageConfirmation>;
shutdown(): Promise<void>;
/**
* Private Methods
*/
/**
* Handles an incoming Call message from a client connection.
*
* @param {string} identifier - The client identifier.
* @param {Call} message - The Call message received.
* @param {Date} timestamp Time at which the message was received from the charger.
* @param {string} protocol The OCPP protocol version of the message
* @return {void}
*/
_onCall(identifier: string, message: Call, timestamp: Date, protocol: OCPPVersionType): Promise<void>;
/**
* Handles a CallResult made by the client.
*
* @param {string} identifier - The client identifier that made the call.
* @param {CallResult} message - The OCPP CallResult message.
* @param {Date} timestamp Time at which the message was received from the charger.
* @param {OCPPVersionType} protocol The OCPP protocol version of the message
*/
_onCallResult(identifier: string, message: CallResult, timestamp: Date, protocol: OCPPVersionType): Promise<void>;
/**
* Handles the CallError that may have occured during a Call exchange.
*
* @param {string} identifier - The client identifier.
* @param {CallError} message - The error message.
* @param {Date} timestamp Time at which the message was received from the charger.
* @param {OCPPVersionType} protocol The OCPP protocol version of the message
*/
_onCallError(identifier: string, message: CallError, timestamp: Date, protocol: OCPPVersionType): Promise<void>;
/**
* Determine if the given action for identifier is allowed.
*
* @param {CallAction} action - The action to be checked.
* @param {string} identifier - The identifier to be checked.
* @return {Promise<boolean>} A promise that resolves to a boolean indicating if the action and identifier are allowed.
*/
private _onCallIsAllowed;
/**
*
* @param {string} identifier - The identifier of the client, e.g. "tenantId:stationId".
* @param {OCPPVersionType} protocol - The OCPP protocol version.
* @param {string} action - The OCPP CallAction to be sent. See {@link CallAction}.
* @param {MessageState} state - The state of the message. Used for dispatching in webhook.
* @param {string} rawMessage - The raw message string to be sent, i.e. the stringified version of the rpc message. Used for sending in webhook and logging.
* @param {any} rpcMessage - the rpc message json object, i.e. [MessageTypeId, messageId, action, payload] for Call or [MessageTypeId, messageId, payload] for CallResult. Used for logging and dispatching in webhook.
* @param {string} receivedIsoTimestamp - The ISO timestamp of when the Call was received, if this is a response to a Call. Used for logging the time taken for the message to be sent since it was received.
* @returns {Promise<Date | undefined>} A promise that resolves to the timestamp of when the message was sent or undefined if the message failed to send.
*/
private _sendMessage;
private _sendCallIsAllowed;
private _routeCall;
private _routeCallResult;
private _routeCallError;
private emitMessage;
private _handleMessageApiCallback;
private getActionFromIncompletelyParsedRpcMessage;
}