+ protected validateRequestPayload<T extends JsonType>(
+ chargingStation: ChargingStation,
+ commandName: RequestCommand,
+ schema: JSONSchemaType<T>,
+ payload: T
+ ): boolean {
+ if (chargingStation.getPayloadSchemaValidation() === false) {
+ return true;
+ }
+ const validate = this.ajv.compile(schema);
+ if (validate(payload)) {
+ return true;
+ }
+ logger.error(
+ `${chargingStation.logPrefix()} ${moduleName}.validateRequestPayload: Request PDU is invalid: %j`,
+ validate.errors
+ );
+ throw new OCPPError(
+ OCPPServiceUtils.ajvErrorsToErrorType(validate.errors),
+ 'Request PDU is invalid',
+ commandName,
+ JSON.stringify(validate.errors, null, 2)
+ );
+ }
+
+ private async internalSendMessage(
+ chargingStation: ChargingStation,
+ messageId: string,
+ messagePayload: JsonType | OCPPError,
+ messageType: MessageType,
+ commandName?: RequestCommand | IncomingRequestCommand,
+ params: RequestParams = {
+ skipBufferingOnError: false,
+ triggerMessage: false,
+ }
+ ): Promise<ResponseType> {
+ if (
+ (chargingStation.isInUnknownState() && commandName === RequestCommand.BOOT_NOTIFICATION) ||
+ (!chargingStation.getOcppStrictCompliance() && chargingStation.isInUnknownState()) ||
+ chargingStation.isInAcceptedState() ||
+ (chargingStation.isInPendingState() &&
+ (params.triggerMessage || messageType === MessageType.CALL_RESULT_MESSAGE))
+ ) {
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
+ const self = this;
+ // Send a message through wsConnection
+ return Utils.promiseWithTimeout(
+ new Promise((resolve, reject) => {
+ const messageToSend = this.buildMessageToSend(
+ chargingStation,
+ messageId,
+ messagePayload,
+ messageType,
+ commandName,
+ responseCallback,
+ errorCallback
+ );
+ if (chargingStation.getEnableStatistics() === true) {
+ chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType);
+ }
+ // Check if wsConnection opened
+ if (chargingStation.isWebSocketConnectionOpened() === true) {
+ // Yes: Send Message
+ const beginId = PerformanceStatistics.beginMeasure(commandName);
+ // FIXME: Handle sending error
+ chargingStation.wsConnection.send(messageToSend);
+ PerformanceStatistics.endMeasure(commandName, beginId);
+ logger.debug(
+ `${chargingStation.logPrefix()} >> Command '${commandName}' sent ${this.getMessageTypeString(
+ messageType
+ )} payload: ${messageToSend}`
+ );
+ } else if (params.skipBufferingOnError === false) {
+ // Buffer it
+ chargingStation.bufferMessage(messageToSend);
+ const ocppError = new OCPPError(
+ ErrorType.GENERIC_ERROR,
+ `WebSocket closed for buffered message id '${messageId}' with content '${messageToSend}'`,
+ commandName,
+ (messagePayload as JsonObject)?.details ?? {}
+ );
+ if (messageType === MessageType.CALL_MESSAGE) {
+ // Reject it but keep the request in the cache
+ return reject(ocppError);
+ }
+ return errorCallback(ocppError, false);
+ } else {
+ // Reject it
+ return errorCallback(
+ new OCPPError(
+ ErrorType.GENERIC_ERROR,
+ `WebSocket closed for non buffered message id '${messageId}' with content '${messageToSend}'`,
+ commandName,
+ (messagePayload as JsonObject)?.details ?? {}
+ ),
+ false
+ );
+ }
+ // Response?
+ if (messageType !== MessageType.CALL_MESSAGE) {
+ // Yes: send Ok
+ return resolve(messagePayload);
+ }
+
+ /**
+ * Function that will receive the request's response
+ *
+ * @param payload
+ * @param requestPayload
+ */
+ async function responseCallback(
+ payload: JsonType,
+ requestPayload: JsonType
+ ): Promise<void> {
+ if (chargingStation.getEnableStatistics() === true) {
+ chargingStation.performanceStatistics.addRequestStatistic(
+ commandName,
+ MessageType.CALL_RESULT_MESSAGE
+ );
+ }
+ // Handle the request's response
+ try {
+ await self.ocppResponseService.responseHandler(
+ chargingStation,
+ commandName as RequestCommand,
+ payload,
+ requestPayload
+ );
+ resolve(payload);
+ } catch (error) {
+ reject(error);
+ } finally {
+ chargingStation.requests.delete(messageId);
+ }
+ }
+
+ /**
+ * Function that will receive the request's error response
+ *
+ * @param error
+ * @param requestStatistic
+ */
+ function errorCallback(error: OCPPError, requestStatistic = true): void {
+ if (requestStatistic === true && chargingStation.getEnableStatistics() === true) {
+ chargingStation.performanceStatistics.addRequestStatistic(
+ commandName,
+ MessageType.CALL_ERROR_MESSAGE
+ );
+ }
+ logger.error(
+ `${chargingStation.logPrefix()} Error occurred when calling command ${commandName} with message data ${JSON.stringify(
+ messagePayload
+ )}:`,
+ error
+ );
+ chargingStation.requests.delete(messageId);
+ reject(error);
+ }
+ }),
+ Constants.OCPP_WEBSOCKET_TIMEOUT,
+ new OCPPError(
+ ErrorType.GENERIC_ERROR,
+ `Timeout for message id '${messageId}'`,
+ commandName,
+ (messagePayload as JsonObject)?.details ?? {}
+ ),
+ () => {
+ messageType === MessageType.CALL_MESSAGE && chargingStation.requests.delete(messageId);
+ }
+ );
+ }
+ throw new OCPPError(
+ ErrorType.SECURITY_ERROR,
+ `Cannot send command ${commandName} PDU when the charging station is in ${chargingStation.getRegistrationStatus()} state on the central server`,
+ commandName
+ );