X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16RequestService.ts;h=415254d194d637113279a12526273fa3ef51150c;hb=f938317f2902366a85a8f76de55ee51f1d4a662a;hp=efbd603c98e170dce3f4a3192d7f62c758351f34;hpb=268a74bb051fcbbad532fd833f0d8fd2b33b6c64;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 efbd603c..415254d1 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -1,15 +1,18 @@ // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. -import type { JSONSchemaType } from 'ajv'; +import type { JSONSchemaType } from 'ajv' -import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; -import { OCPPError } from '../../../exception'; +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 { ErrorType, type JsonObject, type JsonType, type OCPP16AuthorizeRequest, type OCPP16BootNotificationRequest, + OCPP16ChargePointStatus, type OCPP16DataTransferRequest, type OCPP16DiagnosticsStatusNotificationRequest, type OCPP16FirmwareStatusNotificationRequest, @@ -20,108 +23,109 @@ import { type OCPP16StatusNotificationRequest, type OCPP16StopTransactionRequest, OCPPVersion, - type RequestParams, -} from '../../../types'; -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'; + type RequestParams +} from '../../../types/index.js' +import { Constants, generateUUID } from '../../../utils/index.js' +import { OCPPRequestService } from '../OCPPRequestService.js' +import type { OCPPResponseService } from '../OCPPResponseService.js' -const moduleName = 'OCPP16RequestService'; +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>([ + 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>([ [ OCPP16RequestCommand.AUTHORIZE, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/Authorize.json', + 'assets/json-schemas/ocpp/1.6/Authorize.json', moduleName, 'constructor' - ), + ) ], [ OCPP16RequestCommand.BOOT_NOTIFICATION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/BootNotification.json', + 'assets/json-schemas/ocpp/1.6/BootNotification.json', moduleName, 'constructor' - ), + ) ], [ OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json', + 'assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json', moduleName, 'constructor' - ), + ) ], [ OCPP16RequestCommand.HEARTBEAT, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/Heartbeat.json', + 'assets/json-schemas/ocpp/1.6/Heartbeat.json', moduleName, 'constructor' - ), + ) ], [ OCPP16RequestCommand.METER_VALUES, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/MeterValues.json', + 'assets/json-schemas/ocpp/1.6/MeterValues.json', moduleName, 'constructor' - ), + ) ], [ OCPP16RequestCommand.STATUS_NOTIFICATION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/StatusNotification.json', + 'assets/json-schemas/ocpp/1.6/StatusNotification.json', moduleName, 'constructor' - ), + ) ], [ OCPP16RequestCommand.START_TRANSACTION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/StartTransaction.json', + 'assets/json-schemas/ocpp/1.6/StartTransaction.json', moduleName, 'constructor' - ), + ) ], [ OCPP16RequestCommand.STOP_TRANSACTION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/StopTransaction.json', + 'assets/json-schemas/ocpp/1.6/StopTransaction.json', moduleName, 'constructor' - ), + ) ], [ OCPP16RequestCommand.DATA_TRANSFER, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json', + 'assets/json-schemas/ocpp/1.6/DataTransfer.json', moduleName, 'constructor' - ), + ) ], [ OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json', + 'assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json', moduleName, 'constructor' - ), - ], - ]); - this.buildRequestPayload.bind(this); + ) + ] + ]) + this.buildRequestPayload = this.buildRequestPayload.bind(this) as ( + chargingStation: ChargingStation, + commandName: OCPP16RequestCommand, + commandParams?: JsonType + ) => Request } public async requestHandler( @@ -130,14 +134,15 @@ export class OCPP16RequestService extends OCPPRequestService { commandParams?: JsonType, params?: RequestParams ): Promise { - if (OCPP16ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) { + // FIXME?: add sanity checks on charging station availability, connector availability, connector status, etc. + if (OCPP16ServiceUtils.isRequestCommandSupported(chargingStation, commandName)) { return (await this.sendMessage( chargingStation, - Utils.generateUUID(), + generateUUID(), this.buildRequestPayload(chargingStation, commandName, commandParams), commandName, params - )) as unknown as ResponseType; + )) as ResponseType } // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). throw new OCPPError( @@ -145,7 +150,7 @@ export class OCPP16RequestService extends OCPPRequestService { `Unsupported OCPP command '${commandName}'`, commandName, commandParams - ); + ) } private buildRequestPayload( @@ -153,9 +158,9 @@ export class OCPP16RequestService extends OCPPRequestService { commandName: OCPP16RequestCommand, commandParams?: JsonType ): Request { - let connectorId: number; - let energyActiveImportRegister: number; - commandParams = commandParams as JsonObject; + let connectorId: number | undefined + let energyActiveImportRegister: number + commandParams = commandParams as JsonObject switch (commandName) { case OCPP16RequestCommand.BOOT_NOTIFICATION: case OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION: @@ -163,49 +168,64 @@ export class OCPP16RequestService extends OCPPRequestService { case OCPP16RequestCommand.METER_VALUES: case OCPP16RequestCommand.STATUS_NOTIFICATION: case OCPP16RequestCommand.DATA_TRANSFER: - return commandParams as unknown as Request; + return commandParams as unknown as Request case OCPP16RequestCommand.AUTHORIZE: return { idTag: Constants.DEFAULT_IDTAG, - ...commandParams, - } as unknown as Request; + ...commandParams + } as unknown as Request case OCPP16RequestCommand.HEARTBEAT: - return OCPPConstants.OCPP_REQUEST_EMPTY as unknown as Request; + return OCPP16Constants.OCPP_REQUEST_EMPTY as unknown as Request case OCPP16RequestCommand.START_TRANSACTION: return { idTag: Constants.DEFAULT_IDTAG, meterStart: chargingStation.getEnergyActiveImportRegisterByConnectorId( - commandParams?.connectorId as number, + commandParams.connectorId as number, true ), timestamp: new Date(), - ...commandParams, - } as unknown as Request; + ...(OCPP16ServiceUtils.hasReservation( + chargingStation, + commandParams.connectorId as number, + commandParams.idTag as string + ) && { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + 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 === true && (connectorId = chargingStation.getConnectorIdByTransactionId( - commandParams?.transactionId as number - )); + commandParams.transactionId as number + )) energyActiveImportRegister = chargingStation.getEnergyActiveImportRegisterByTransactionId( - commandParams?.transactionId as number, + commandParams.transactionId as number, true - ); + ) return { - idTag: chargingStation.getTransactionIdTag(commandParams?.transactionId as number), + idTag: chargingStation.getTransactionIdTag(commandParams.transactionId as number), meterStop: energyActiveImportRegister, timestamp: new Date(), - ...(chargingStation.getTransactionDataMeterValues() && { + ...(chargingStation.stationInfo?.transactionDataMeterValues === true && { transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues( - chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + chargingStation.getConnectorStatus(connectorId!)!.transactionBeginMeterValue!, OCPP16ServiceUtils.buildTransactionEndMeterValue( chargingStation, - connectorId, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + connectorId!, energyActiveImportRegister ) - ), + ) }), - ...commandParams, - } as unknown as Request; + ...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( @@ -214,7 +234,7 @@ export class OCPP16RequestService extends OCPPRequestService { `Unsupported OCPP command '${commandName}'`, commandName, commandParams - ); + ) } } }