@citrineos/util
Version:
The OCPP util module which supplies helpful utilities like cache and queue connectors, etc.
145 lines (144 loc) • 6.76 kB
TypeScript
import type { IAuthenticator, ICache, IConnectionManager, IMessageRouter, INetworkConnection, SystemConfig, WebsocketServerConfig } from '@citrineos/base';
import type { ILogObj } from 'tslog';
import { Logger } from 'tslog';
export declare class WebsocketNetworkConnection implements INetworkConnection {
protected _cache: ICache;
protected _config: SystemConfig;
protected _logger: Logger<ILogObj>;
private _identifierConnections;
private _pingTimers;
private _pongTimeouts;
private _closeHandlers;
private _tenantConnectionCounts;
private _httpServersMap;
private _authenticator;
private _router;
private _connectionManager?;
private _doesChargingStationExistByStationId?;
private _getMaxChargingStationsForTenant?;
constructor(config: SystemConfig, cache: ICache, authenticator: IAuthenticator, router: IMessageRouter, logger?: Logger<ILogObj>, doesChargingStationExistByStationId?: (tenantId: number, stationId: string) => Promise<boolean>, getMaxChargingStationsForTenant?: (tenantId: number) => Promise<number | null>, connectionManager?: IConnectionManager);
/**
* Send a message to the charging station specified by the identifier.
*
* @param {string} identifier - The identifier of the client.
* @param {string} message - The message to send.
* @return {void} rejects the promise if message fails to send, otherwise returns void.
*/
sendMessage(identifier: string, message: string): Promise<void>;
bindNetworkHook(): (identifier: string, message: string) => Promise<void>;
disconnect(tenantId: number, stationId: string): Promise<boolean>;
shutdown(): Promise<void>;
/**
* Updates certificates for a specific server with the provided TLS key, certificate chain, and optional
* root CA.
*
* @param {string} serverId - The ID of the server to update.
* @param {string} tlsKey - The TLS key to set.
* @param {string} tlsCertificateChain - The TLS certificate chain to set.
* @param {string} [rootCA] - The root CA to set (optional).
* @return {void} void
*/
updateTlsCertificates(serverId: string, tlsKey: string, tlsCertificateChain: string, rootCA?: string): void;
/**
* Dynamically adds a new websocket server at runtime and starts it.
*
* @param {WebsocketServerConfig} websocketServerConfig
* @returns {Promise<void>}
*/
addWebsocketServer(websocketServerConfig: WebsocketServerConfig): Promise<void>;
private _onHttpRequest;
/**
* Method to validate websocket upgrade requests and pass them to the socket server.
*
* @param {IncomingMessage} req - The request object.
* @param {Duplex} socket - Websocket duplex stream.
* @param {Buffer} head - Websocket buffer.
* @param {WebSocketServer} wss - Websocket server.
* @param {WebsocketServerConfig} websocketServerConfig - websocket server config.
*/
private _upgradeRequest;
/**
* Utility function to reject websocket upgrade requests with 500 status code.
* @param socket - Websocket duplex stream.
*/
private _terminateConnectionInternalError;
private _terminateConnectionServiceUnavailable;
/**
* Internal method to handle new client connection and ensures supported protocols are used.
*
* @param {Set<string>} protocols - The set of protocols to handle.
* @param {IncomingMessage} _req - The request object.
* @param {string} wsServerProtocol - The websocket server protocol.
* @return {boolean|string} - Returns the protocol version if successful, otherwise false.
*/
private _handleProtocols;
/**
* Internal method to handle the connection event when a WebSocket connection is established.
* This happens after successful protocol exchange with client.
*
* @param {WebSocket} ws - The WebSocket object representing the connection.
* @param {WebsocketServerConfig} websocketServerConfig - The websocket server configuration.
* @param {number} pingInterval - The ping interval in seconds.
* @param {IncomingMessage} req - The request object associated with the connection.
* @return {void}
*/
private _onConnection;
/**
* Internal method to register event listeners for the WebSocket connection.
*
* @param {string} identifier - The unique identifier of the connection, i.e. the combination of tenantId and stationId.
* @param {WebSocket} ws - The WebSocket object representing the connection.
* @param {number} pingInterval - The ping interval in seconds.
* @return {void} This function does not return anything.
*/
private _registerWebsocketEvents;
/**
* Internal method to handle the incoming message from the websocket client.
*
* @param {string} identifier - The client identifier.
* @param {string} message - The incoming message from the client.
* @param {OCPPVersionType} protocol - The OCPP protocol version of the client, 'ocpp1.6' or 'ocpp2.0.1'.
* @return {void} This function does not return anything.
*/
private _onMessage;
private _handleWebsocketClose;
/**
* Internal method to handle the error event for the WebSocket server.
*
* @param {WebSocketServer} wss - The WebSocket server instance.
* @param {Error} error - The error object.
* @return {void} This function does not return anything.
*/
private _onError;
/**
* Internal method to handle the event when the WebSocketServer is closed.
*
* @param {WebSocketServer} wss - The WebSocketServer instance.
* @return {void} This function does not return anything.
*/
private _onClose;
/**
* Internal method to execute a ping operation on a WebSocket connection after a delay of 60 seconds.
*
* @param {string} identifier - The identifier of the client connection.
* @param {WebSocket} ws - The WebSocket connection to ping.
* @param {number} pingInterval - The ping interval in seconds.
* @param {boolean} applyJitter - Whether to apply jitter to the ping interval.
* @return {void} This function does not return anything.
*/
private _ping;
/**
*
* @param url Http upgrade request url used by charger
* @returns Charger identifier
*/
private _getClientIdFromUrl;
/**
* Extract tenant id from the incoming upgrade request.
* Supported sources (in order): query `tenant`/`tenantId`, header `x-tenant-id`,
* path segment (second-last segment if URL is `/tenant/station`).
*/
private _extractTenantIdFromRequest;
private _generateServerOptions;
private _createAndStartWebsocketServer;
}