X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16RequestService.ts;h=a7bacae9b2814fd5cc813fcb77d43db2964125b8;hb=298be10c5c800e43b0b9dcd6c50f6d49e2cb786b;hp=37e7019c1a4f3f9f7a870a29ceb338ebe1606fff;hpb=66a7748ddeda8c94d7562a1ce58d440319654a4c;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index 37e7019c..a7bacae9 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -1,6 +1,6 @@ -// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. +// Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved. -import type { JSONSchemaType } from 'ajv' +import type { ValidateFunction } from 'ajv' import { OCPP16Constants } from './OCPP16Constants.js' import { OCPP16ServiceUtils } from './OCPP16ServiceUtils.js' @@ -32,100 +32,136 @@ import type { OCPPResponseService } from '../OCPPResponseService.js' const moduleName = 'OCPP16RequestService' export class OCPP16RequestService extends OCPPRequestService { - protected jsonSchemas: Map> + protected jsonSchemasValidateFunction: Map> public constructor (ocppResponseService: OCPPResponseService) { - // if (new.target?.name === moduleName) { - // throw new TypeError(`Cannot construct ${new.target?.name} instances directly`) + // if (new.target.name === moduleName) { + // throw new TypeError(`Cannot construct ${new.target.name} instances directly`) // } super(OCPPVersion.VERSION_16, ocppResponseService) - this.jsonSchemas = new Map>([ + this.jsonSchemasValidateFunction = new Map>([ [ OCPP16RequestCommand.AUTHORIZE, - OCPP16ServiceUtils.parseJsonSchemaFile( - 'assets/json-schemas/ocpp/1.6/Authorize.json', - moduleName, - 'constructor' - ) + this.ajv + .compile( + OCPP16ServiceUtils.parseJsonSchemaFile( + 'assets/json-schemas/ocpp/1.6/Authorize.json', + moduleName, + 'constructor' + ) + ) + .bind(this) ], [ OCPP16RequestCommand.BOOT_NOTIFICATION, - OCPP16ServiceUtils.parseJsonSchemaFile( - 'assets/json-schemas/ocpp/1.6/BootNotification.json', - moduleName, - 'constructor' - ) + this.ajv + .compile( + OCPP16ServiceUtils.parseJsonSchemaFile( + 'assets/json-schemas/ocpp/1.6/BootNotification.json', + moduleName, + 'constructor' + ) + ) + .bind(this) ], [ OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, - OCPP16ServiceUtils.parseJsonSchemaFile( - 'assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json', - moduleName, - 'constructor' - ) + this.ajv + .compile( + OCPP16ServiceUtils.parseJsonSchemaFile( + 'assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json', + moduleName, + 'constructor' + ) + ) + .bind(this) ], [ OCPP16RequestCommand.HEARTBEAT, - OCPP16ServiceUtils.parseJsonSchemaFile( - 'assets/json-schemas/ocpp/1.6/Heartbeat.json', - moduleName, - 'constructor' - ) + this.ajv + .compile( + OCPP16ServiceUtils.parseJsonSchemaFile( + 'assets/json-schemas/ocpp/1.6/Heartbeat.json', + moduleName, + 'constructor' + ) + ) + .bind(this) ], [ OCPP16RequestCommand.METER_VALUES, - OCPP16ServiceUtils.parseJsonSchemaFile( - 'assets/json-schemas/ocpp/1.6/MeterValues.json', - moduleName, - 'constructor' - ) + this.ajv + .compile( + OCPP16ServiceUtils.parseJsonSchemaFile( + 'assets/json-schemas/ocpp/1.6/MeterValues.json', + moduleName, + 'constructor' + ) + ) + .bind(this) ], [ OCPP16RequestCommand.STATUS_NOTIFICATION, - OCPP16ServiceUtils.parseJsonSchemaFile( - 'assets/json-schemas/ocpp/1.6/StatusNotification.json', - moduleName, - 'constructor' - ) + this.ajv + .compile( + OCPP16ServiceUtils.parseJsonSchemaFile( + 'assets/json-schemas/ocpp/1.6/StatusNotification.json', + moduleName, + 'constructor' + ) + ) + .bind(this) ], [ OCPP16RequestCommand.START_TRANSACTION, - OCPP16ServiceUtils.parseJsonSchemaFile( - 'assets/json-schemas/ocpp/1.6/StartTransaction.json', - moduleName, - 'constructor' - ) + this.ajv + .compile( + OCPP16ServiceUtils.parseJsonSchemaFile( + 'assets/json-schemas/ocpp/1.6/StartTransaction.json', + moduleName, + 'constructor' + ) + ) + .bind(this) ], [ OCPP16RequestCommand.STOP_TRANSACTION, - OCPP16ServiceUtils.parseJsonSchemaFile( - 'assets/json-schemas/ocpp/1.6/StopTransaction.json', - moduleName, - 'constructor' - ) + this.ajv + .compile( + OCPP16ServiceUtils.parseJsonSchemaFile( + 'assets/json-schemas/ocpp/1.6/StopTransaction.json', + moduleName, + 'constructor' + ) + ) + .bind(this) ], [ OCPP16RequestCommand.DATA_TRANSFER, - OCPP16ServiceUtils.parseJsonSchemaFile( - 'assets/json-schemas/ocpp/1.6/DataTransfer.json', - moduleName, - 'constructor' - ) + this.ajv + .compile( + OCPP16ServiceUtils.parseJsonSchemaFile( + 'assets/json-schemas/ocpp/1.6/DataTransfer.json', + moduleName, + 'constructor' + ) + ) + .bind(this) ], [ OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, - OCPP16ServiceUtils.parseJsonSchemaFile( - 'assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json', - moduleName, - 'constructor' - ) + this.ajv + .compile( + OCPP16ServiceUtils.parseJsonSchemaFile( + 'assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json', + moduleName, + 'constructor' + ) + ) + .bind(this) ] ]) - this.buildRequestPayload = this.buildRequestPayload.bind(this) as ( - chargingStation: ChargingStation, - commandName: OCPP16RequestCommand, - commandParams?: JsonType - ) => Request + this.buildRequestPayload = this.buildRequestPayload.bind(this) } public async requestHandler( @@ -180,37 +216,36 @@ export class OCPP16RequestService extends OCPPRequestService { return { idTag: Constants.DEFAULT_IDTAG, meterStart: chargingStation.getEnergyActiveImportRegisterByConnectorId( - commandParams?.connectorId as number, + commandParams.connectorId as number, true ), timestamp: new Date(), ...(OCPP16ServiceUtils.hasReservation( chargingStation, - commandParams?.connectorId as number, - commandParams?.idTag as string + commandParams.connectorId as number, + commandParams.idTag as string ) && { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion reservationId: chargingStation.getReservationBy( 'connectorId', chargingStation.getConnectorStatus(0)?.status === OCPP16ChargePointStatus.Reserved ? 0 - : (commandParams?.connectorId as number) + : (commandParams.connectorId as number) )!.reservationId }), ...commandParams } as unknown as Request case OCPP16RequestCommand.STOP_TRANSACTION: chargingStation.stationInfo?.transactionDataMeterValues === true && - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion (connectorId = chargingStation.getConnectorIdByTransactionId( - commandParams?.transactionId as number - )!) + commandParams.transactionId as number + )) energyActiveImportRegister = chargingStation.getEnergyActiveImportRegisterByTransactionId( - commandParams?.transactionId as number, + commandParams.transactionId as number, true ) return { - idTag: chargingStation.getTransactionIdTag(commandParams?.transactionId as number), + idTag: chargingStation.getTransactionIdTag(commandParams.transactionId as number), meterStop: energyActiveImportRegister, timestamp: new Date(), ...(chargingStation.stationInfo?.transactionDataMeterValues === true && {