X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16RequestService.ts;h=b9643469a791831937d9774e4ef0b9fb68abacbf;hb=923ac728c8636e5d8e7cc387ade07990385c6fdd;hp=37a5b2ba2811c36610fa1b8002e89ab5c64b3081;hpb=ba9a56a613727d96757690a8b52af6731f3fd8a8;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 37a5b2ba..b9643469 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -1,9 +1,7 @@ // 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' import type { ChargingStation } from '../../../charging-station/index.js' import { OCPPError } from '../../../exception/index.js' import { @@ -28,97 +26,139 @@ import { import { Constants, generateUUID } from '../../../utils/index.js' import { OCPPRequestService } from '../OCPPRequestService.js' import type { OCPPResponseService } from '../OCPPResponseService.js' +import { OCPP16Constants } from './OCPP16Constants.js' +import { OCPP16ServiceUtils } from './OCPP16ServiceUtils.js' const moduleName = 'OCPP16RequestService' export class OCPP16RequestService extends OCPPRequestService { - protected jsonSchemas: Map> + protected payloadValidateFunctions: Map> public constructor (ocppResponseService: OCPPResponseService) { // 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.payloadValidateFunctions = 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) @@ -127,11 +167,21 @@ export class OCPP16RequestService extends OCPPRequestService { public async requestHandler( chargingStation: ChargingStation, commandName: OCPP16RequestCommand, - commandParams?: JsonType, + commandParams?: RequestType, params?: RequestParams ): Promise { // FIXME?: add sanity checks on charging station availability, connector availability, connector status, etc. if (OCPP16ServiceUtils.isRequestCommandSupported(chargingStation, commandName)) { + // Post request actions hook + switch (commandName) { + case OCPP16RequestCommand.START_TRANSACTION: + await OCPP16ServiceUtils.sendAndSetConnectorStatus( + chargingStation, + (commandParams as OCPP16StartTransactionRequest).connectorId, + OCPP16ChargePointStatus.Preparing + ) + break + } return (await this.sendMessage( chargingStation, generateUUID(),