X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16RequestService.ts;h=a4b1275f5a4c1474ccb0ac2e447a354d77eb64a8;hb=2040a613fd09c3476094e671a103131b99d08a8b;hp=69ff5c851f61f142b7c31438e89896e9424a024d;hpb=ee307db5e146a05f912fa0b8c9ea05a68e847095;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 69ff5c85..a4b1275f 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -4,30 +4,30 @@ import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; -import { JSONSchemaType } from 'ajv'; +import type { JSONSchemaType } from 'ajv'; import OCPPError from '../../../exception/OCPPError'; -import { JsonObject, JsonType } from '../../../types/JsonType'; -import { OCPP16MeterValuesRequest } from '../../../types/ocpp/1.6/MeterValues'; +import type { JsonObject, JsonType } from '../../../types/JsonType'; +import type { OCPP16MeterValuesRequest } from '../../../types/ocpp/1.6/MeterValues'; import { DiagnosticsStatusNotificationRequest, OCPP16BootNotificationRequest, + OCPP16DataTransferRequest, OCPP16HeartbeatRequest, OCPP16RequestCommand, OCPP16StatusNotificationRequest, } from '../../../types/ocpp/1.6/Requests'; -import { +import type { OCPP16AuthorizeRequest, OCPP16StartTransactionRequest, OCPP16StopTransactionRequest, } from '../../../types/ocpp/1.6/Transaction'; import { ErrorType } from '../../../types/ocpp/ErrorType'; -import { RequestParams } from '../../../types/ocpp/Requests'; +import type { RequestParams } from '../../../types/ocpp/Requests'; import Constants from '../../../utils/Constants'; import logger from '../../../utils/Logger'; import Utils from '../../../utils/Utils'; import type ChargingStation from '../../ChargingStation'; -import { ChargingStationUtils } from '../../ChargingStationUtils'; import OCPPRequestService from '../OCPPRequestService'; import type OCPPResponseService from '../OCPPResponseService'; import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; @@ -139,17 +139,31 @@ export default class OCPP16RequestService extends OCPPRequestService { ) ) as JSONSchemaType, ], + [ + OCPP16RequestCommand.DATA_TRANSFER, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], ]); + this.buildRequestPayload.bind(this); + this.validatePayload.bind(this); } - public async requestHandler( + public async requestHandler( chargingStation: ChargingStation, commandName: OCPP16RequestCommand, commandParams?: JsonType, params?: RequestParams - ): Promise { - if (ChargingStationUtils.isRequestCommandSupported(commandName, chargingStation)) { - const requestPayload = this.buildRequestPayload( + ): Promise { + if (OCPP16ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) { + const requestPayload = this.buildRequestPayload( chargingStation, commandName, commandParams @@ -161,11 +175,12 @@ export default class OCPP16RequestService extends OCPPRequestService { requestPayload, commandName, params - )) as unknown as Response; + )) as unknown as ResponseType; } + // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). throw new OCPPError( ErrorType.NOT_SUPPORTED, - `${moduleName}.requestHandler: Unsupported OCPP command '${commandName}'`, + `Unsupported OCPP command '${commandName}'`, commandName, commandParams ); @@ -177,6 +192,7 @@ export default class OCPP16RequestService extends OCPPRequestService { commandParams?: JsonType ): Request { let connectorId: number; + let energyActiveImportRegister: number; commandParams = commandParams as JsonObject; switch (commandName) { case OCPP16RequestCommand.AUTHORIZE: @@ -240,28 +256,39 @@ export default class OCPP16RequestService extends OCPPRequestService { connectorId = chargingStation.getConnectorIdByTransactionId( commandParams?.transactionId as number ); + commandParams?.meterStop && + (energyActiveImportRegister = + chargingStation.getEnergyActiveImportRegisterByTransactionId( + commandParams?.transactionId as number, + true + )); return { transactionId: commandParams?.transactionId, - ...(!Utils.isUndefined(commandParams?.idTag) && { idTag: commandParams.idTag }), - meterStop: commandParams?.meterStop, + idTag: + commandParams?.idTag ?? + chargingStation.getTransactionIdTag(commandParams?.transactionId as number), + meterStop: commandParams?.meterStop ?? energyActiveImportRegister, timestamp: new Date().toISOString(), - ...(commandParams?.reason && { reason: commandParams.reason }), + reason: commandParams?.reason, ...(chargingStation.getTransactionDataMeterValues() && { transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues( chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, OCPP16ServiceUtils.buildTransactionEndMeterValue( chargingStation, connectorId, - commandParams?.meterStop as number + (commandParams?.meterStop as number) ?? energyActiveImportRegister ) ), }), } as unknown as Request; + case OCPP16RequestCommand.DATA_TRANSFER: + return commandParams as unknown as Request; default: + // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). throw new OCPPError( ErrorType.NOT_SUPPORTED, // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - `${moduleName}.buildRequestPayload: Unsupported OCPP command '${commandName}'`, + `Unsupported OCPP command '${commandName}'`, commandName, commandParams );