@citrineos/util
Version:
The OCPP util module which supplies helpful utilities like cache and queue connectors, etc.
117 lines (116 loc) • 5.12 kB
TypeScript
import { IAuthenticator, ICache, IMessageRouter, SystemConfig } from '@citrineos/base';
import { ILogObj, Logger } from 'tslog';
export declare class WebsocketNetworkConnection {
protected _cache: ICache;
protected _config: SystemConfig;
protected _logger: Logger<ILogObj>;
private _identifierConnections;
private _httpServersMap;
private _authenticator;
private _router;
constructor(config: SystemConfig, cache: ICache, authenticator: IAuthenticator, router: IMessageRouter, logger?: Logger<ILogObj>);
/**
* 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>;
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;
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;
/**
* 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 {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 for the connection.
* @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;
/**
* 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 milliseconds.
* @return {void} This function does not return anything.
*/
private _ping;
/**
*
* @param url Http upgrade request url used by charger
* @returns Charger identifier
*/
private _getClientIdFromUrl;
private _generateServerOptions;
}