X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStation.ts;h=ac5aaa93c451d8c877c9d93b0345550b74dd37e8;hb=617cad0c86c3fea53b56a738bb0d6ddc3da05237;hp=591b4644e1bdb805002f92ec9c30ef7e1ed72324;hpb=a52a644657916a3516798cbe8877a634ff8d6132;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 591b4644..ac5aaa93 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -573,7 +573,7 @@ export class ChargingStation { this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash); this.templateFileWatcher?.close(); this.sharedLRUCache.deleteChargingStationTemplate(this.stationInfo?.templateHash); - this.bootNotificationResponse = undefined; + delete this.bootNotificationResponse; this.started = false; parentPort?.postMessage(MessageChannelUtils.buildStoppedMessage(this)); this.stopping = false; @@ -652,18 +652,6 @@ export class ChargingStation { 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( @@ -676,7 +664,11 @@ export class ChargingStation { `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}` ); - this.wsConnection = new WebSocket(this.wsConnectionUrl, protocol, options); + this.wsConnection = new WebSocket( + this.wsConnectionUrl, + `ocpp${this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16}`, + options + ); // Handle WebSocket message this.wsConnection.on( @@ -995,7 +987,7 @@ export class ChargingStation { this.stationInfo = this.getStationInfo(); this.saveStationInfo(); // Avoid duplication of connectors related information in RAM - this.stationInfo?.Connectors && delete this.stationInfo.Connectors; + delete this.stationInfo?.Connectors; this.configuredSupervisionUrl = this.getConfiguredSupervisionUrl(); if (this.getEnableStatistics() === true) { this.performanceStatistics = PerformanceStatistics.getInstance( @@ -1011,27 +1003,7 @@ export class ChargingStation { // OCPP configuration this.ocppConfiguration = this.getOcppConfiguration(); this.initializeOcppConfiguration(); - const ocppVersion = this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16; - switch (ocppVersion) { - case OCPPVersion.VERSION_16: - this.ocppIncomingRequestService = - OCPP16IncomingRequestService.getInstance(); - this.ocppRequestService = OCPP16RequestService.getInstance( - OCPP16ResponseService.getInstance() - ); - break; - case OCPPVersion.VERSION_20: - case OCPPVersion.VERSION_201: - this.ocppIncomingRequestService = - OCPP20IncomingRequestService.getInstance(); - this.ocppRequestService = OCPP20RequestService.getInstance( - OCPP20ResponseService.getInstance() - ); - break; - default: - this.handleUnsupportedVersion(ocppVersion); - break; - } + this.initializeOcppServices(); if (this.stationInfo?.autoRegister === true) { this.bootNotificationResponse = { currentTime: new Date(), @@ -1059,6 +1031,30 @@ export class ChargingStation { } } + private initializeOcppServices(): void { + const ocppVersion = this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16; + switch (ocppVersion) { + case OCPPVersion.VERSION_16: + this.ocppIncomingRequestService = + OCPP16IncomingRequestService.getInstance(); + this.ocppRequestService = OCPP16RequestService.getInstance( + OCPP16ResponseService.getInstance() + ); + break; + case OCPPVersion.VERSION_20: + case OCPPVersion.VERSION_201: + this.ocppIncomingRequestService = + OCPP20IncomingRequestService.getInstance(); + this.ocppRequestService = OCPP20RequestService.getInstance( + OCPP20ResponseService.getInstance() + ); + break; + default: + this.handleUnsupportedVersion(ocppVersion); + break; + } + } + private initializeOcppConfiguration(): void { if ( !ChargingStationConfigurationUtils.getConfigurationKey( @@ -1321,7 +1317,7 @@ export class ChargingStation { this.templateFile } with connector ${connectorId} status configuration defined, undefine it` ); - connectorStatus.status = undefined; + delete connectorStatus.status; } } @@ -1503,107 +1499,107 @@ export class ChargingStation { parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this)); } + private getCachedRequest(messageType: MessageType, messageId: string): CachedRequest | undefined { + const cachedRequest = this.requests.get(messageId); + if (Array.isArray(cachedRequest) === true) { + return cachedRequest; + } + throw new OCPPError( + ErrorType.PROTOCOL_ERROR, + `Cached request for message id ${messageId} ${OCPPServiceUtils.getMessageTypeString( + messageType + )} is not an array`, + undefined, + cachedRequest as JsonType + ); + } + + private async handleIncomingMessage(request: IncomingRequest): Promise { + const [messageType, messageId, commandName, commandPayload] = request; + if (this.getEnableStatistics() === true) { + this.performanceStatistics?.addRequestStatistic(commandName, messageType); + } + logger.debug( + `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify( + request + )}` + ); + // Process the message + await this.ocppIncomingRequestService.incomingRequestHandler( + this, + messageId, + commandName, + commandPayload + ); + } + + private handleResponseMessage(response: Response): void { + const [messageType, messageId, commandPayload] = response; + if (this.requests.has(messageId) === false) { + // Error + throw new OCPPError( + ErrorType.INTERNAL_ERROR, + `Response for unknown message id ${messageId}`, + undefined, + commandPayload + ); + } + // Respond + const [responseCallback, , requestCommandName, requestPayload] = this.getCachedRequest( + messageType, + messageId + ); + logger.debug( + `${this.logPrefix()} << Command '${ + requestCommandName ?? Constants.UNKNOWN_COMMAND + }' received response payload: ${JSON.stringify(response)}` + ); + responseCallback(commandPayload, requestPayload); + } + + private handleErrorMessage(errorResponse: ErrorResponse): void { + const [messageType, messageId, errorType, errorMessage, errorDetails] = errorResponse; + if (this.requests.has(messageId) === false) { + // Error + throw new OCPPError( + ErrorType.INTERNAL_ERROR, + `Error response for unknown message id ${messageId}`, + undefined, + { errorType, errorMessage, errorDetails } + ); + } + const [, errorCallback, requestCommandName] = this.getCachedRequest(messageType, messageId); + logger.debug( + `${this.logPrefix()} << Command '${ + requestCommandName ?? Constants.UNKNOWN_COMMAND + }' received error response payload: ${JSON.stringify(errorResponse)}` + ); + errorCallback(new OCPPError(errorType, errorMessage, requestCommandName, errorDetails)); + } + private async onMessage(data: RawData): Promise { + let request: IncomingRequest | Response | ErrorResponse; let messageType: number; - let messageId: string; - let commandName: IncomingRequestCommand; - let commandPayload: JsonType; - let errorType: ErrorType; - let errorMessage: string; - let errorDetails: JsonType; - let responseCallback: ResponseCallback; - let errorCallback: ErrorCallback; - let requestCommandName: RequestCommand | IncomingRequestCommand; - let requestPayload: JsonType; - let cachedRequest: CachedRequest; let errMsg: string; try { - const request = JSON.parse(data.toString()) as IncomingRequest | Response | ErrorResponse; + request = JSON.parse(data.toString()) as IncomingRequest | Response | ErrorResponse; if (Array.isArray(request) === true) { - [messageType, messageId] = request; + [messageType] = request; // Check the type of message switch (messageType) { // Incoming Message case MessageType.CALL_MESSAGE: - [, , commandName, commandPayload] = request as IncomingRequest; - if (this.getEnableStatistics() === true) { - this.performanceStatistics?.addRequestStatistic(commandName, messageType); - } - logger.debug( - `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify( - request - )}` - ); - // Process the message - await this.ocppIncomingRequestService.incomingRequestHandler( - this, - messageId, - commandName, - commandPayload - ); + await this.handleIncomingMessage(request as IncomingRequest); break; - // Outcome Message + // Response Message case MessageType.CALL_RESULT_MESSAGE: - [, , commandPayload] = request as Response; - if (this.requests.has(messageId) === false) { - // Error - throw new OCPPError( - ErrorType.INTERNAL_ERROR, - `Response for unknown message id ${messageId}`, - undefined, - commandPayload - ); - } - // Respond - cachedRequest = this.requests.get(messageId); - if (Array.isArray(cachedRequest) === true) { - [responseCallback, errorCallback, requestCommandName, requestPayload] = cachedRequest; - } else { - throw new OCPPError( - ErrorType.PROTOCOL_ERROR, - `Cached request for message id ${messageId} response is not an array`, - undefined, - cachedRequest as unknown as JsonType - ); - } - logger.debug( - `${this.logPrefix()} << Command '${ - requestCommandName ?? Constants.UNKNOWN_COMMAND - }' received response payload: ${JSON.stringify(request)}` - ); - responseCallback(commandPayload, requestPayload); + this.handleResponseMessage(request as Response); break; // Error Message case MessageType.CALL_ERROR_MESSAGE: - [, , errorType, errorMessage, errorDetails] = request as ErrorResponse; - if (this.requests.has(messageId) === false) { - // Error - throw new OCPPError( - ErrorType.INTERNAL_ERROR, - `Error response for unknown message id ${messageId}`, - undefined, - { errorType, errorMessage, errorDetails } - ); - } - cachedRequest = this.requests.get(messageId); - if (Array.isArray(cachedRequest) === true) { - [, errorCallback, requestCommandName] = cachedRequest; - } else { - throw new OCPPError( - ErrorType.PROTOCOL_ERROR, - `Cached request for message id ${messageId} error response is not an array`, - undefined, - cachedRequest as unknown as JsonType - ); - } - logger.debug( - `${this.logPrefix()} << Command '${ - requestCommandName ?? Constants.UNKNOWN_COMMAND - }' received error response payload: ${JSON.stringify(request)}` - ); - errorCallback(new OCPPError(errorType, errorMessage, requestCommandName, errorDetails)); + this.handleErrorMessage(request as ErrorResponse); break; - // Error + // Unknown Message default: // eslint-disable-next-line @typescript-eslint/restrict-template-expressions errMsg = `Wrong message type ${messageType}`; @@ -1617,38 +1613,20 @@ export class ChargingStation { }); } } catch (error) { - // Log - logger.error( - `${this.logPrefix()} Incoming OCPP command '${ - commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND - }' message '${data.toString()}'${ - messageType !== MessageType.CALL_MESSAGE - ? ` matching cached request '${JSON.stringify(this.requests.get(messageId))}'` - : '' - } processing error:`, - error - ); - if (error instanceof OCPPError === false) { - logger.warn( - `${this.logPrefix()} Error thrown at incoming OCPP command '${ - commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND - }' message '${data.toString()}' handling is not an OCPPError:`, - error - ); - } + let commandName: IncomingRequestCommand; + let requestCommandName: RequestCommand | IncomingRequestCommand; + let errorCallback: ErrorCallback; + const [, messageId] = request; switch (messageType) { case MessageType.CALL_MESSAGE: + [, , commandName] = request as IncomingRequest; // Send error - await this.ocppRequestService.sendError( - this, - messageId, - error as OCPPError, - commandName ?? requestCommandName ?? null - ); + await this.ocppRequestService.sendError(this, messageId, error as OCPPError, commandName); break; case MessageType.CALL_RESULT_MESSAGE: case MessageType.CALL_ERROR_MESSAGE: - if (errorCallback) { + if (this.requests.has(messageId) === true) { + [, errorCallback, requestCommandName] = this.getCachedRequest(messageType, messageId); // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op) errorCallback(error as OCPPError, false); } else { @@ -1657,6 +1635,24 @@ export class ChargingStation { } break; } + if (error instanceof OCPPError === false) { + logger.warn( + `${this.logPrefix()} Error thrown at incoming OCPP command '${ + commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND + }' message '${data.toString()}' handling is not an OCPPError:`, + error + ); + } + logger.error( + `${this.logPrefix()} Incoming OCPP command '${ + commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND + }' message '${data.toString()}'${ + messageType !== MessageType.CALL_MESSAGE + ? ` matching cached request '${JSON.stringify(this.requests.get(messageId))}'` + : '' + } processing error:`, + error + ); } } @@ -1882,7 +1878,7 @@ export class ChargingStation { ConnectorStatusEnum.Unavailable ) ); - this.getConnectorStatus(connectorId).status = undefined; + delete this.getConnectorStatus(connectorId)?.status; } } }