+ public openWSConnection(
+ options: WsOptions = this.stationInfo?.wsOptions ?? {},
+ params: { closeOpened?: boolean; terminateOpened?: boolean } = {
+ closeOpened: false,
+ terminateOpened: false,
+ }
+ ): void {
+ options.handshakeTimeout = options?.handshakeTimeout ?? this.getConnectionTimeout() * 1000;
+ params.closeOpened = params?.closeOpened ?? false;
+ params.terminateOpened = params?.terminateOpened ?? false;
+ if (this.started === false && this.starting === false) {
+ logger.warn(
+ `${this.logPrefix()} Cannot open OCPP connection to URL ${this.wsConnectionUrl.toString()} on stopped charging station`
+ );
+ return;
+ }
+ if (
+ !Utils.isNullOrUndefined(this.stationInfo.supervisionUser) &&
+ !Utils.isNullOrUndefined(this.stationInfo.supervisionPassword)
+ ) {
+ options.auth = `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
+ }
+ if (params?.closeOpened) {
+ this.closeWSConnection();
+ }
+ if (params?.terminateOpened) {
+ this.terminateWSConnection();
+ }
+ const ocppVersion = this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16;
+ let protocol: string;
+ switch (ocppVersion) {
+ case OCPPVersion.VERSION_16:
+ case OCPPVersion.VERSION_20:
+ case OCPPVersion.VERSION_201:
+ protocol = 'ocpp' + ocppVersion;
+ break;
+ default:
+ this.handleUnsupportedVersion(ocppVersion);
+ break;
+ }
+
+ if (this.isWebSocketConnectionOpened() === true) {
+ logger.warn(
+ `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`
+ );
+ return;
+ }
+
+ logger.info(
+ `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`
+ );
+
+ this.wsConnection = new WebSocket(this.wsConnectionUrl, protocol, options);
+
+ // Handle WebSocket message
+ this.wsConnection.on(
+ 'message',
+ this.onMessage.bind(this) as (this: WebSocket, data: RawData, isBinary: boolean) => void
+ );
+ // Handle WebSocket error
+ this.wsConnection.on(
+ 'error',
+ this.onError.bind(this) as (this: WebSocket, error: Error) => void
+ );
+ // Handle WebSocket close
+ this.wsConnection.on(
+ 'close',
+ this.onClose.bind(this) as (this: WebSocket, code: number, reason: Buffer) => void
+ );
+ // Handle WebSocket open
+ this.wsConnection.on('open', this.onOpen.bind(this) as (this: WebSocket) => void);
+ // Handle WebSocket ping
+ this.wsConnection.on('ping', this.onPing.bind(this) as (this: WebSocket, data: Buffer) => void);
+ // Handle WebSocket pong
+ this.wsConnection.on('pong', this.onPong.bind(this) as (this: WebSocket, data: Buffer) => void);
+ }
+
+ public closeWSConnection(): void {
+ if (this.isWebSocketConnectionOpened() === true) {
+ this.wsConnection.close();
+ this.wsConnection = null;
+ }
+ }
+
+ public startAutomaticTransactionGenerator(
+ connectorIds?: number[],
+ automaticTransactionGeneratorConfiguration?: AutomaticTransactionGeneratorConfiguration
+ ): void {
+ this.automaticTransactionGenerator = AutomaticTransactionGenerator.getInstance(
+ automaticTransactionGeneratorConfiguration ??
+ this.getAutomaticTransactionGeneratorConfigurationFromTemplate(),
+ this
+ );
+ if (!Utils.isEmptyArray(connectorIds)) {
+ for (const connectorId of connectorIds) {
+ this.automaticTransactionGenerator.startConnector(connectorId);
+ }
+ } else {
+ this.automaticTransactionGenerator.start();
+ }
+ parentPort.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
+ }
+
+ public stopAutomaticTransactionGenerator(connectorIds?: number[]): void {
+ if (!Utils.isEmptyArray(connectorIds)) {
+ for (const connectorId of connectorIds) {
+ this.automaticTransactionGenerator?.stopConnector(connectorId);
+ }
+ } else {
+ this.automaticTransactionGenerator?.stop();
+ }
+ parentPort.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
+ }
+
+ public async stopTransactionOnConnector(
+ connectorId: number,
+ reason = StopTransactionReason.NONE
+ ): Promise<StopTransactionResponse> {
+ const transactionId = this.getConnectorStatus(connectorId).transactionId;
+ if (
+ this.getBeginEndMeterValues() === true &&
+ this.getOcppStrictCompliance() === true &&
+ this.getOutOfOrderEndMeterValues() === false
+ ) {
+ // FIXME: Implement OCPP version agnostic helpers
+ const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue(
+ this,
+ connectorId,
+ this.getEnergyActiveImportRegisterByTransactionId(transactionId)
+ );
+ await this.ocppRequestService.requestHandler<MeterValuesRequest, MeterValuesResponse>(
+ this,
+ RequestCommand.METER_VALUES,
+ {
+ connectorId,
+ transactionId,
+ meterValue: [transactionEndMeterValue],
+ }
+ );
+ }
+ return this.ocppRequestService.requestHandler<StopTransactionRequest, StopTransactionResponse>(
+ this,
+ RequestCommand.STOP_TRANSACTION,
+ {
+ transactionId,
+ meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId, true),
+ reason,
+ }
+ );
+ }
+
+ private flushMessageBuffer(): void {