X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16RequestService.ts;h=2b0375a21b0d0cb7aa44f95761a38f818ce1eb98;hb=a6ef1ece74c0d08e86a905571f4f6045c28131cb;hp=845506958fb633500504a04ca3eff118df2382cb;hpb=130783a74f495abcb198b7f01abe19dab4f7fb47;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 84550695..2b0375a2 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -2,139 +2,163 @@ import type { JSONSchemaType } from 'ajv'; -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 { 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, type OCPP16HeartbeatRequest, + type OCPP16MeterValuesRequest, OCPP16RequestCommand, + type OCPP16StartTransactionRequest, 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'; + type OCPP16StopTransactionRequest, + OCPPVersion, + 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'; -export default class OCPP16RequestService extends OCPPRequestService { - protected jsonSchemas: Map>; +export 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`); - } + // 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' + '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( 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 - )) as unknown as ResponseType; + params, + )) 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, `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) { @@ -151,38 +175,50 @@ export default class OCPP16RequestService extends OCPPRequestService { ...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, - 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, @@ -194,7 +230,7 @@ export default class OCPP16RequestService extends OCPPRequestService { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `Unsupported OCPP command '${commandName}'`, commandName, - commandParams + commandParams, ); } }