X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPRequestService.ts;h=e4890a91d8a6a88e2c8d282fcd603c2b2c31baef;hb=98fc1389a2464ce8738047f8990731ae31938ee5;hp=921945e82cdea1bb92bee6373092151d12f8771c;hpb=07561812b72072b6d9f20997be86a42ee88e15a2;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 921945e8..e4890a91 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -1,5 +1,4 @@ -import type { JSONSchemaType } from 'ajv'; -import Ajv from 'ajv-draft-04'; +import Ajv, { type JSONSchemaType } from 'ajv'; import ajvFormats from 'ajv-formats'; import OCPPError from '../../exception/OCPPError'; @@ -9,12 +8,15 @@ import type { HandleErrorParams } from '../../types/Error'; import type { JsonObject, JsonType } from '../../types/JsonType'; import { ErrorType } from '../../types/ocpp/ErrorType'; import { MessageType } from '../../types/ocpp/MessageType'; +import type { OCPPVersion } from '../../types/ocpp/OCPPVersion'; import { - IncomingRequestCommand, - OutgoingRequest, + type ErrorCallback, + type IncomingRequestCommand, + type OutgoingRequest, RequestCommand, - RequestParams, - ResponseType, + type RequestParams, + type ResponseCallback, + type ResponseType, } from '../../types/ocpp/Requests'; import type { ErrorResponse, Response } from '../../types/ocpp/Responses'; import Constants from '../../utils/Constants'; @@ -28,15 +30,21 @@ const moduleName = 'OCPPRequestService'; export default abstract class OCPPRequestService { private static instance: OCPPRequestService | null = null; - private ajv: Ajv; + private readonly version: OCPPVersion; + private readonly ajv: Ajv; private readonly ocppResponseService: OCPPResponseService; - protected constructor(ocppResponseService: OCPPResponseService) { - this.ocppResponseService = ocppResponseService; - this.ajv = new Ajv(); + protected constructor(version: OCPPVersion, ocppResponseService: OCPPResponseService) { + this.version = version; + this.ajv = new Ajv({ + keywords: ['javaType'], + multipleOfPrecision: 2, + }); ajvFormats(this.ajv); + this.ocppResponseService = ocppResponseService; this.requestHandler.bind(this); + this.sendMessage.bind(this); this.sendResponse.bind(this); this.sendError.bind(this); this.internalSendMessage.bind(this); @@ -122,19 +130,23 @@ export default abstract class OCPPRequestService { protected validateRequestPayload( chargingStation: ChargingStation, - commandName: RequestCommand, - schema: JSONSchemaType, + commandName: RequestCommand | IncomingRequestCommand, payload: T ): boolean { if (chargingStation.getPayloadSchemaValidation() === false) { return true; } + const schema = this.getRequestPayloadValidationSchema(chargingStation, commandName); + if (schema === false) { + return true; + } const validate = this.ajv.compile(schema); + OCPPServiceUtils.convertDateToISOString(payload); if (validate(payload)) { return true; } logger.error( - `${chargingStation.logPrefix()} ${moduleName}.validateRequestPayload: Request PDU is invalid: %j`, + `${chargingStation.logPrefix()} ${moduleName}.validateRequestPayload: Command '${commandName}' request PDU is invalid: %j`, validate.errors ); // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). @@ -158,11 +170,13 @@ export default abstract class OCPPRequestService { } ): Promise { if ( - (chargingStation.isInUnknownState() && commandName === RequestCommand.BOOT_NOTIFICATION) || - (!chargingStation.getOcppStrictCompliance() && chargingStation.isInUnknownState()) || - chargingStation.isInAcceptedState() || - (chargingStation.isInPendingState() && - (params.triggerMessage || messageType === MessageType.CALL_RESULT_MESSAGE)) + (chargingStation.isInUnknownState() === true && + commandName === RequestCommand.BOOT_NOTIFICATION) || + (chargingStation.getOcppStrictCompliance() === false && + chargingStation.isInUnknownState() === true) || + chargingStation.isInAcceptedState() === true || + (chargingStation.isInPendingState() === true && + (params.triggerMessage === true || messageType === MessageType.CALL_RESULT_MESSAGE)) ) { // eslint-disable-next-line @typescript-eslint/no-this-alias const self = this; @@ -184,10 +198,10 @@ export default abstract class OCPPRequestService { // Check if wsConnection opened if (chargingStation.isWebSocketConnectionOpened() === true) { // Yes: Send Message - const beginId = PerformanceStatistics.beginMeasure(commandName); + const beginId = PerformanceStatistics.beginMeasure(commandName as string); // FIXME: Handle sending error chargingStation.wsConnection.send(messageToSend); - PerformanceStatistics.endMeasure(commandName, beginId); + PerformanceStatistics.endMeasure(commandName as string, beginId); logger.debug( `${chargingStation.logPrefix()} >> Command '${commandName}' sent ${this.getMessageTypeString( messageType @@ -228,13 +242,10 @@ export default abstract class OCPPRequestService { /** * Function that will receive the request's response * - * @param payload - * @param requestPayload + * @param payload - + * @param requestPayload - */ - async function responseCallback( - payload: JsonType, - requestPayload: JsonType - ): Promise { + function responseCallback(payload: JsonType, requestPayload: JsonType): void { if (chargingStation.getEnableStatistics() === true) { chargingStation.performanceStatistics.addRequestStatistic( commandName, @@ -242,26 +253,29 @@ export default abstract class OCPPRequestService { ); } // Handle the request's response - try { - await self.ocppResponseService.responseHandler( + self.ocppResponseService + .responseHandler( chargingStation, commandName as RequestCommand, payload, requestPayload - ); - resolve(payload); - } catch (error) { - reject(error); - } finally { - chargingStation.requests.delete(messageId); - } + ) + .then(() => { + resolve(payload); + }) + .catch((error) => { + reject(error); + }) + .finally(() => { + chargingStation.requests.delete(messageId); + }); } /** * Function that will receive the request's error response * - * @param error - * @param requestStatistic + * @param error - + * @param requestStatistic - */ function errorCallback(error: OCPPError, requestStatistic = true): void { if (requestStatistic === true && chargingStation.getEnableStatistics() === true) { @@ -305,8 +319,8 @@ export default abstract class OCPPRequestService { messagePayload: JsonType | OCPPError, messageType: MessageType, commandName?: RequestCommand | IncomingRequestCommand, - responseCallback?: (payload: JsonType, requestPayload: JsonType) => Promise, - errorCallback?: (error: OCPPError, requestStatistic?: boolean) => void + responseCallback?: ResponseCallback, + errorCallback?: ErrorCallback ): string { let messageToSend: string; // Type of message @@ -320,6 +334,7 @@ export default abstract class OCPPRequestService { commandName, messagePayload as JsonType, ]); + this.validateRequestPayload(chargingStation, commandName, messagePayload as JsonType); messageToSend = JSON.stringify([ messageType, messageId, @@ -330,6 +345,7 @@ export default abstract class OCPPRequestService { // Response case MessageType.CALL_RESULT_MESSAGE: // Build response + // FIXME: Validate response payload messageToSend = JSON.stringify([messageType, messageId, messagePayload] as Response); break; // Error Message @@ -371,10 +387,15 @@ export default abstract class OCPPRequestService { } // eslint-disable-next-line @typescript-eslint/no-unused-vars - public abstract requestHandler( + public abstract requestHandler( chargingStation: ChargingStation, commandName: RequestCommand, commandParams?: JsonType, params?: RequestParams - ): Promise; + ): Promise; + + protected abstract getRequestPayloadValidationSchema( + chargingStation: ChargingStation, + commandName: RequestCommand | IncomingRequestCommand + ): JSONSchemaType | false; }