X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16RequestService.ts;h=b238dc66018a50563217065e08f42cfcaf427888;hb=33276ba6a447f0beb92b934d2b8d5cb08eababb4;hp=7ff1fdb8d43b27da48147a789a1612c87644d8a4;hpb=51022aa0d811eec79514fbeb56cb9556e7168cd7;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 7ff1fdb8..b238dc66 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -12,6 +12,7 @@ import { type JsonType, type OCPP16AuthorizeRequest, type OCPP16BootNotificationRequest, + OCPP16ChargePointStatus, type OCPP16DataTransferRequest, type OCPP16DiagnosticsStatusNotificationRequest, type OCPP16FirmwareStatusNotificationRequest, @@ -24,27 +25,27 @@ import { OCPPVersion, type RequestParams, } from '../../../types'; -import { Constants, Utils } from '../../../utils'; +import { Constants, generateUUID } from '../../../utils'; import { OCPPRequestService } from '../OCPPRequestService'; import type { OCPPResponseService } from '../OCPPResponseService'; const moduleName = 'OCPP16RequestService'; export class OCPP16RequestService extends OCPPRequestService { - protected jsonSchemas: Map>; + protected jsonSchemas: 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.jsonSchemas = new Map>([ [ OCPP16RequestCommand.AUTHORIZE, OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/Authorize.json', moduleName, - 'constructor' + 'constructor', ), ], [ @@ -52,7 +53,7 @@ export class OCPP16RequestService extends OCPPRequestService { OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/BootNotification.json', moduleName, - 'constructor' + 'constructor', ), ], [ @@ -60,7 +61,7 @@ export class OCPP16RequestService extends OCPPRequestService { OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json', moduleName, - 'constructor' + 'constructor', ), ], [ @@ -68,7 +69,7 @@ export class OCPP16RequestService extends OCPPRequestService { OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/Heartbeat.json', moduleName, - 'constructor' + 'constructor', ), ], [ @@ -76,7 +77,7 @@ export class OCPP16RequestService extends OCPPRequestService { OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/MeterValues.json', moduleName, - 'constructor' + 'constructor', ), ], [ @@ -84,7 +85,7 @@ export class OCPP16RequestService extends OCPPRequestService { OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/StatusNotification.json', moduleName, - 'constructor' + 'constructor', ), ], [ @@ -92,7 +93,7 @@ export class OCPP16RequestService extends OCPPRequestService { OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/StartTransaction.json', moduleName, - 'constructor' + 'constructor', ), ], [ @@ -100,7 +101,7 @@ export class OCPP16RequestService extends OCPPRequestService { OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/StopTransaction.json', moduleName, - 'constructor' + 'constructor', ), ], [ @@ -108,7 +109,7 @@ export class OCPP16RequestService extends OCPPRequestService { OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/DataTransfer.json', moduleName, - 'constructor' + 'constructor', ), ], [ @@ -116,14 +117,14 @@ export class OCPP16RequestService extends OCPPRequestService { OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json', moduleName, - 'constructor' + 'constructor', ), ], ]); this.buildRequestPayload = this.buildRequestPayload.bind(this) as ( chargingStation: ChargingStation, commandName: OCPP16RequestCommand, - commandParams?: JsonType + commandParams?: JsonType, ) => Request; } @@ -131,16 +132,16 @@ export class OCPP16RequestService extends OCPPRequestService { chargingStation: ChargingStation, commandName: OCPP16RequestCommand, commandParams?: JsonType, - params?: RequestParams + params?: RequestParams, ): Promise { // FIXME?: add sanity checks on charging station availability, connector availability, connector status, etc. if (OCPP16ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) { return (await this.sendMessage( chargingStation, - Utils.generateUUID(), + generateUUID(), this.buildRequestPayload(chargingStation, commandName, commandParams), commandName, - params + params, )) as ResponseType; } // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). @@ -148,16 +149,16 @@ export class OCPP16RequestService extends OCPPRequestService { ErrorType.NOT_SUPPORTED, `Unsupported OCPP command '${commandName}'`, commandName, - commandParams + commandParams, ); } private buildRequestPayload( chargingStation: ChargingStation, commandName: OCPP16RequestCommand, - commandParams?: JsonType + commandParams?: JsonType, ): Request { - let connectorId: number; + let connectorId: number | undefined; let energyActiveImportRegister: number; commandParams = commandParams as JsonObject; switch (commandName) { @@ -180,32 +181,44 @@ export class OCPP16RequestService extends OCPPRequestService { idTag: Constants.DEFAULT_IDTAG, meterStart: chargingStation.getEnergyActiveImportRegisterByConnectorId( commandParams?.connectorId as number, - true + true, ), timestamp: new Date(), + ...(OCPP16ServiceUtils.hasReservation( + chargingStation, + commandParams?.connectorId as number, + commandParams?.idTag as string, + ) && { + reservationId: chargingStation.getReservationBy( + 'connectorId', + chargingStation.getConnectorStatus(0)?.status === OCPP16ChargePointStatus.Reserved + ? 0 + : (commandParams?.connectorId as number), + )!.reservationId, + }), ...commandParams, } as unknown as Request; case OCPP16RequestCommand.STOP_TRANSACTION: - chargingStation.getTransactionDataMeterValues() && + chargingStation.stationInfo?.transactionDataMeterValues && (connectorId = chargingStation.getConnectorIdByTransactionId( - commandParams?.transactionId as number - )); + commandParams?.transactionId as number, + )!); energyActiveImportRegister = chargingStation.getEnergyActiveImportRegisterByTransactionId( commandParams?.transactionId as number, - true + true, ); return { idTag: chargingStation.getTransactionIdTag(commandParams?.transactionId as number), meterStop: energyActiveImportRegister, timestamp: new Date(), - ...(chargingStation.getTransactionDataMeterValues() && { + ...(chargingStation.stationInfo?.transactionDataMeterValues && { transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues( - chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, + chargingStation.getConnectorStatus(connectorId!)!.transactionBeginMeterValue!, OCPP16ServiceUtils.buildTransactionEndMeterValue( chargingStation, - connectorId, - energyActiveImportRegister - ) + connectorId!, + energyActiveImportRegister, + ), ), }), ...commandParams, @@ -217,7 +230,7 @@ export class OCPP16RequestService extends OCPPRequestService { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `Unsupported OCPP command '${commandName}'`, commandName, - commandParams + commandParams, ); } }