X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16ResponseService.ts;h=691bf0f6c6b243465255c2c4f322c1c65917ffb8;hb=55ae7b758f478a2beb4557bbc96363fb913dcc73;hp=e65f8a8cb3f7316a6415d8697e1bb527d1a5c20e;hpb=24d15716245469249b57e5aa790147d703d60a4e;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index e65f8a8c..691bf0f6 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -51,18 +51,18 @@ import { type SetChargingProfileResponse, type UnlockConnectorResponse } from '../../../types/index.js' -import { Constants, convertToInt, logger } from '../../../utils/index.js' +import { Constants, convertToInt, isAsyncFunction, logger } from '../../../utils/index.js' import { OCPPResponseService } from '../OCPPResponseService.js' const moduleName = 'OCPP16ResponseService' export class OCPP16ResponseService extends OCPPResponseService { - public jsonSchemasIncomingRequestResponseValidateFunction: Map< + public incomingRequestResponsePayloadValidateFunctions: Map< OCPP16IncomingRequestCommand, ValidateFunction > - protected jsonSchemasValidateFunction: Map> + protected payloadValidateFunctions: Map> private readonly responseHandlers: Map public constructor () { @@ -97,7 +97,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [OCPP16RequestCommand.DATA_TRANSFER, this.emptyResponseHandler], [OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, this.emptyResponseHandler] ]) - this.jsonSchemasValidateFunction = new Map>([ + this.payloadValidateFunctions = new Map>([ [ OCPP16RequestCommand.BOOT_NOTIFICATION, this.ajv @@ -219,13 +219,13 @@ export class OCPP16ResponseService extends OCPPResponseService { .bind(this) ] ]) - this.jsonSchemasIncomingRequestResponseValidateFunction = new Map< + this.incomingRequestResponsePayloadValidateFunctions = new Map< OCPP16IncomingRequestCommand, ValidateFunction >([ [ OCPP16IncomingRequestCommand.RESET, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/ResetResponse.json', @@ -237,7 +237,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.CLEAR_CACHE, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/ClearCacheResponse.json', @@ -249,7 +249,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/ChangeAvailabilityResponse.json', @@ -261,7 +261,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.UNLOCK_CONNECTOR, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/UnlockConnectorResponse.json', @@ -273,7 +273,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.GET_CONFIGURATION, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/GetConfigurationResponse.json', @@ -285,7 +285,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/ChangeConfigurationResponse.json', @@ -297,7 +297,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.GET_COMPOSITE_SCHEDULE, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/GetCompositeScheduleResponse.json', @@ -309,7 +309,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/SetChargingProfileResponse.json', @@ -321,7 +321,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/ClearChargingProfileResponse.json', @@ -333,7 +333,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/RemoteStartTransactionResponse.json', @@ -345,7 +345,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/RemoteStopTransactionResponse.json', @@ -357,7 +357,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/GetDiagnosticsResponse.json', @@ -369,7 +369,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/TriggerMessageResponse.json', @@ -381,7 +381,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.DATA_TRANSFER, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/DataTransferResponse.json', @@ -393,7 +393,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.UPDATE_FIRMWARE, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/UpdateFirmwareResponse.json', @@ -405,7 +405,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.RESERVE_NOW, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/ReserveNowResponse.json', @@ -417,7 +417,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.CANCEL_RESERVATION, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/CancelReservationResponse.json', @@ -445,7 +445,18 @@ export class OCPP16ResponseService extends OCPPResponseService { try { this.validatePayload(chargingStation, commandName, payload) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - await this.responseHandlers.get(commandName)!(chargingStation, payload, requestPayload) + const responseHandler = this.responseHandlers.get(commandName)! + if (isAsyncFunction(responseHandler)) { + await responseHandler(chargingStation, payload, requestPayload) + } else { + ( + responseHandler as ( + chargingStation: ChargingStation, + payload: JsonType, + requestPayload?: JsonType + ) => void + )(chargingStation, payload, requestPayload) + } } catch (error) { logger.error( `${chargingStation.logPrefix()} ${moduleName}.responseHandler: Handle response error:`, @@ -473,7 +484,7 @@ export class OCPP16ResponseService extends OCPPResponseService { payload, undefined, 2 - )} while the charging station is not registered on the central server.`, + )} while the charging station is not registered on the central server`, commandName, payload ) @@ -485,7 +496,7 @@ export class OCPP16ResponseService extends OCPPResponseService { commandName: OCPP16RequestCommand, payload: JsonType ): boolean { - if (this.jsonSchemasValidateFunction.has(commandName)) { + if (this.payloadValidateFunctions.has(commandName)) { return this.validateResponsePayload(chargingStation, commandName, payload) } logger.warn( @@ -574,7 +585,7 @@ export class OCPP16ResponseService extends OCPPResponseService { logger.debug( `${chargingStation.logPrefix()} idTag '${requestPayload.idTag}' rejected with status '${ payload.idTagInfo.status - }` + }'` ) } } else {