- public async handleRequest(messageId: string, commandName: OCPP16IncomingRequestCommand, commandPayload: Record<string, unknown>): Promise<void> {
- let response;
- const methodName = `handleRequest${commandName}`;
- // Call
- if (typeof this[methodName] === 'function') {
- try {
- // Call the method to build the response
- response = await this[methodName](commandPayload);
- } catch (error) {
- // Log
- logger.error(this.chargingStation.logPrefix() + ' Handle request error: %j', error);
- // Send back an error response to inform backend
- await this.chargingStation.ocppRequestService.sendError(messageId, error, commandName);
- throw error;
+ private incomingRequestHandlers: Map<OCPP16IncomingRequestCommand, IncomingRequestHandler>;
+
+ public constructor(chargingStation: ChargingStation) {
+ if (new.target?.name === moduleName) {
+ throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
+ }
+ super(chargingStation);
+ this.incomingRequestHandlers = new Map<OCPP16IncomingRequestCommand, IncomingRequestHandler>([
+ [OCPP16IncomingRequestCommand.RESET, this.handleRequestReset.bind(this)],
+ [OCPP16IncomingRequestCommand.CLEAR_CACHE, this.handleRequestClearCache.bind(this)],
+ [OCPP16IncomingRequestCommand.UNLOCK_CONNECTOR, this.handleRequestUnlockConnector.bind(this)],
+ [
+ OCPP16IncomingRequestCommand.GET_CONFIGURATION,
+ this.handleRequestGetConfiguration.bind(this),
+ ],
+ [
+ OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION,
+ this.handleRequestChangeConfiguration.bind(this),
+ ],
+ [
+ OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE,
+ this.handleRequestSetChargingProfile.bind(this),
+ ],
+ [
+ OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE,
+ this.handleRequestClearChargingProfile.bind(this),
+ ],
+ [
+ OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY,
+ this.handleRequestChangeAvailability.bind(this),
+ ],
+ [
+ OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION,
+ this.handleRequestRemoteStartTransaction.bind(this),
+ ],
+ [
+ OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION,
+ this.handleRequestRemoteStopTransaction.bind(this),
+ ],
+ [OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, this.handleRequestGetDiagnostics.bind(this)],
+ [OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, this.handleRequestTriggerMessage.bind(this)],
+ ]);
+ }
+
+ public async handleRequest(
+ messageId: string,
+ commandName: OCPP16IncomingRequestCommand,
+ commandPayload: JsonType
+ ): Promise<void> {
+ let result: JsonType;
+ if (
+ this.chargingStation.getOcppStrictCompliance() &&
+ this.chargingStation.isInPendingState() &&
+ (commandName === OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION ||
+ commandName === OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION)
+ ) {
+ throw new OCPPError(
+ ErrorType.SECURITY_ERROR,
+ `${commandName} cannot be issued to handle request payload ${JSON.stringify(
+ commandPayload,
+ null,
+ 2
+ )} while the charging station is in pending state on the central server`,
+ commandName
+ );
+ }
+ if (
+ this.chargingStation.isRegistered() ||
+ (!this.chargingStation.getOcppStrictCompliance() && this.chargingStation.isInUnknownState())
+ ) {
+ if (this.incomingRequestHandlers.has(commandName)) {
+ try {
+ // Call the method to build the result
+ result = await this.incomingRequestHandlers.get(commandName)(commandPayload);
+ } catch (error) {
+ // Log
+ logger.error(this.chargingStation.logPrefix() + ' Handle request error: %j', error);
+ throw error;
+ }
+ } else {
+ // Throw exception
+ throw new OCPPError(
+ ErrorType.NOT_IMPLEMENTED,
+ `${commandName} is not implemented to handle request payload ${JSON.stringify(
+ commandPayload,
+ null,
+ 2
+ )}`,
+ commandName
+ );