X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16RequestService.ts;h=15b2f2dd64e8ba130d8a31268d04ff82fa501f19;hb=2cace1a546e371a9326f84179d0e2384275048bf;hp=2a4165c491471064aca600a321b838ecfe2259ff;hpb=e3822d6f1b40477f7308ad70c290ed2c4106c585;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 2a4165c4..15b2f2dd 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -1,142 +1,214 @@ -// 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 type ChargingStation from '../../ChargingStation'; -import Constants from '../../../utils/Constants'; -import { ErrorType } from '../../../types/ocpp/ErrorType'; -import { JsonObject } from '../../../types/JsonType'; -import { OCPP16RequestCommand } from '../../../types/ocpp/1.6/Requests'; import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; import OCPPError from '../../../exception/OCPPError'; +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 { 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 OCPPConstants from '../OCPPConstants'; import OCPPRequestService from '../OCPPRequestService'; import type OCPPResponseService from '../OCPPResponseService'; -import { RequestParams } from '../../../types/ocpp/Requests'; -import Utils from '../../../utils/Utils'; const moduleName = 'OCPP16RequestService'; export default class OCPP16RequestService extends OCPPRequestService { - public constructor(chargingStation: ChargingStation, ocppResponseService: OCPPResponseService) { + protected jsonSchemas: Map>; + + public constructor(ocppResponseService: OCPPResponseService) { if (new.target?.name === moduleName) { throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); } - super(chargingStation, 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?: JsonObject, + commandParams?: JsonType, params?: RequestParams - ): Promise { - if (Object.values(OCPP16RequestCommand).includes(commandName)) { + ): Promise { + if (OCPP16ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) { return (await this.sendMessage( + chargingStation, Utils.generateUUID(), - this.buildRequestPayload(commandName, commandParams), + this.buildRequestPayload(chargingStation, commandName, commandParams), 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, - { commandName } + commandParams ); } - private buildRequestPayload( + private buildRequestPayload( + chargingStation: ChargingStation, commandName: OCPP16RequestCommand, - commandParams?: JsonObject + commandParams?: JsonType ): Request { let connectorId: number; + let energyActiveImportRegister: number; + commandParams = commandParams as JsonObject; switch (commandName) { - case OCPP16RequestCommand.AUTHORIZE: - return { - ...(!Utils.isUndefined(commandParams?.idTag) - ? { idTag: commandParams.idTag } - : { idTag: Constants.DEFAULT_IDTAG }), - } as unknown as Request; case OCPP16RequestCommand.BOOT_NOTIFICATION: - return { - chargePointModel: commandParams?.chargePointModel, - chargePointVendor: commandParams?.chargePointVendor, - ...(!Utils.isUndefined(commandParams?.chargeBoxSerialNumber) && { - chargeBoxSerialNumber: commandParams.chargeBoxSerialNumber, - }), - ...(!Utils.isUndefined(commandParams?.chargePointSerialNumber) && { - chargePointSerialNumber: commandParams.chargePointSerialNumber, - }), - ...(!Utils.isUndefined(commandParams?.firmwareVersion) && { - firmwareVersion: commandParams.firmwareVersion, - }), - ...(!Utils.isUndefined(commandParams?.iccid) && { iccid: commandParams.iccid }), - ...(!Utils.isUndefined(commandParams?.imsi) && { imsi: commandParams.imsi }), - ...(!Utils.isUndefined(commandParams?.meterSerialNumber) && { - meterSerialNumber: commandParams.meterSerialNumber, - }), - ...(!Utils.isUndefined(commandParams?.meterType) && { - meterType: commandParams.meterType, - }), - } as unknown as Request; case OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION: - return { - status: commandParams?.diagnosticsStatus, - } as unknown as Request; - case OCPP16RequestCommand.HEARTBEAT: - return {} as unknown as Request; + case OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION: case OCPP16RequestCommand.METER_VALUES: - return { - connectorId: commandParams?.connectorId, - transactionId: commandParams?.transactionId, - meterValue: Array.isArray(commandParams?.meterValue) - ? commandParams?.meterValue - : [commandParams?.meterValue], - } as unknown as Request; case OCPP16RequestCommand.STATUS_NOTIFICATION: + case OCPP16RequestCommand.DATA_TRANSFER: + return commandParams as unknown as Request; + case OCPP16RequestCommand.AUTHORIZE: return { - connectorId: commandParams?.connectorId, - status: commandParams?.status, - errorCode: commandParams?.errorCode, + idTag: Constants.DEFAULT_IDTAG, + ...commandParams, } as unknown as Request; + case OCPP16RequestCommand.HEARTBEAT: + return OCPPConstants.OCPP_REQUEST_EMPTY as unknown as Request; case OCPP16RequestCommand.START_TRANSACTION: return { - connectorId: commandParams?.connectorId, - ...(!Utils.isUndefined(commandParams?.idTag) - ? { idTag: commandParams?.idTag } - : { idTag: Constants.DEFAULT_IDTAG }), - meterStart: this.chargingStation.getEnergyActiveImportRegisterByConnectorId( - commandParams?.connectorId as number + idTag: Constants.DEFAULT_IDTAG, + meterStart: chargingStation.getEnergyActiveImportRegisterByConnectorId( + commandParams?.connectorId as number, + true ), - timestamp: new Date().toISOString(), + timestamp: new Date(), + ...commandParams, } as unknown as Request; case OCPP16RequestCommand.STOP_TRANSACTION: - connectorId = this.chargingStation.getConnectorIdByTransactionId( - commandParams?.transactionId as number + chargingStation.getTransactionDataMeterValues() && + (connectorId = chargingStation.getConnectorIdByTransactionId( + commandParams?.transactionId as number + )); + 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 }), - ...(this.chargingStation.getTransactionDataMeterValues() && { + idTag: chargingStation.getTransactionIdTag(commandParams?.transactionId as number), + meterStop: energyActiveImportRegister, + timestamp: new Date(), + ...(chargingStation.getTransactionDataMeterValues() && { transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues( - this.chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, + chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, OCPP16ServiceUtils.buildTransactionEndMeterValue( - this.chargingStation, + chargingStation, connectorId, - commandParams?.meterStop as number + energyActiveImportRegister ) ), }), + ...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, - { commandName } + commandParams ); } } + + private parseJsonSchemaFile(relativePath: string): JSONSchemaType { + return JSON.parse( + fs.readFileSync( + path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath), + 'utf8' + ) + ) as JSONSchemaType; + } }