X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16RequestService.ts;h=77b5cb586d90ed9752ef911c1b9aa25ba53502cd;hb=b2a0452d368cfc43bbacc4991de59afa64f662a4;hp=78ff888890826396efe75610dfa6e3f50a632e3f;hpb=65554cc3fc240ee17d57c57ec60a2a0da4d757ba;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 78ff8888..77b5cb58 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -1,49 +1,137 @@ -// Partial Copyright Jerome Benoit. 2021. All Rights Reserved. +// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +import type { JSONSchemaType } from 'ajv'; + +import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; import OCPPError from '../../../exception/OCPPError'; -import { JsonObject, JsonType } from '../../../types/JsonType'; -import { OCPP16RequestCommand } from '../../../types/ocpp/1.6/Requests'; +import type { JsonObject, JsonType } from '../../../types/JsonType'; +import type { OCPP16MeterValuesRequest } from '../../../types/ocpp/1.6/MeterValues'; +import { + type OCPP16BootNotificationRequest, + type OCPP16DataTransferRequest, + type OCPP16DiagnosticsStatusNotificationRequest, + type OCPP16FirmwareStatusNotificationRequest, + type OCPP16HeartbeatRequest, + OCPP16RequestCommand, + type OCPP16StatusNotificationRequest, +} from '../../../types/ocpp/1.6/Requests'; +import type { + OCPP16AuthorizeRequest, + OCPP16StartTransactionRequest, + OCPP16StopTransactionRequest, +} from '../../../types/ocpp/1.6/Transaction'; import { ErrorType } from '../../../types/ocpp/ErrorType'; -import { RequestParams } from '../../../types/ocpp/Requests'; +import { OCPPVersion } from '../../../types/ocpp/OCPPVersion'; +import type { RequestParams } from '../../../types/ocpp/Requests'; import Constants from '../../../utils/Constants'; 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'; const moduleName = 'OCPP16RequestService'; export default class OCPP16RequestService extends OCPPRequestService { + protected jsonSchemas: Map>; + public constructor(ocppResponseService: OCPPResponseService) { if (new.target?.name === moduleName) { throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); } - super(ocppResponseService); + super(OCPPVersion.VERSION_16, ocppResponseService); + this.jsonSchemas = new Map>([ + [ + OCPP16RequestCommand.AUTHORIZE, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/Authorize.json' + ), + ], + [ + OCPP16RequestCommand.BOOT_NOTIFICATION, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/BootNotification.json' + ), + ], + [ + OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json' + ), + ], + [ + OCPP16RequestCommand.HEARTBEAT, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/Heartbeat.json' + ), + ], + [ + OCPP16RequestCommand.METER_VALUES, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/MeterValues.json' + ), + ], + [ + OCPP16RequestCommand.STATUS_NOTIFICATION, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/StatusNotification.json' + ), + ], + [ + OCPP16RequestCommand.START_TRANSACTION, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/StartTransaction.json' + ), + ], + [ + OCPP16RequestCommand.STOP_TRANSACTION, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/StopTransaction.json' + ), + ], + [ + OCPP16RequestCommand.DATA_TRANSFER, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json' + ), + ], + [ + OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json' + ), + ], + ]); + this.buildRequestPayload.bind(this); } - public async requestHandler( + public async requestHandler( chargingStation: ChargingStation, commandName: OCPP16RequestCommand, commandParams?: JsonType, params?: RequestParams - ): Promise { - if ( - Object.values(OCPP16RequestCommand).includes(commandName) && - ChargingStationUtils.isCommandSupported(commandName, chargingStation.stationInfo) - ) { + ): Promise { + if (OCPP16ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) { + const requestPayload = this.buildRequestPayload( + chargingStation, + commandName, + commandParams + ); return (await this.sendMessage( chargingStation, Utils.generateUUID(), - this.buildRequestPayload(chargingStation, commandName, commandParams), + 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 ); @@ -55,6 +143,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: @@ -86,21 +175,13 @@ export default class OCPP16RequestService extends OCPPRequestService { }), } as unknown as Request; case OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION: + case OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION: return { - status: commandParams?.diagnosticsStatus, + status: commandParams?.status, } as unknown as Request; case OCPP16RequestCommand.HEARTBEAT: return {} as unknown as Request; case OCPP16RequestCommand.METER_VALUES: - // Sanity check - if (!Array.isArray(commandParams?.meterValue)) { - throw new OCPPError( - ErrorType.TYPERAINT_VIOLATION, - `${moduleName}.buildRequestPayload ${commandName}: Invalid array type for meterValue payload field`, - commandName, - commandParams - ); - } return { connectorId: commandParams?.connectorId, transactionId: commandParams?.transactionId, @@ -121,37 +202,57 @@ export default class OCPP16RequestService extends OCPPRequestService { meterStart: chargingStation.getEnergyActiveImportRegisterByConnectorId( commandParams?.connectorId as number ), - timestamp: new Date().toISOString(), + timestamp: commandParams?.timestamp ?? new Date(), } as unknown as Request; case OCPP16RequestCommand.STOP_TRANSACTION: 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, - timestamp: new Date().toISOString(), - ...(commandParams?.reason && { reason: commandParams.reason }), + idTag: + commandParams?.idTag ?? + chargingStation.getTransactionIdTag(commandParams?.transactionId as number), + meterStop: commandParams?.meterStop ?? energyActiveImportRegister, + timestamp: commandParams?.timestamp ?? new Date(), + 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 ); } } + + private parseJsonSchemaFile(relativePath: string): JSONSchemaType { + return JSON.parse( + fs.readFileSync( + path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath), + 'utf8' + ) + ) as JSONSchemaType; + } }