- public async handleResponse(commandName: OCPP16RequestCommand, payload: Record<string, unknown> | string, requestPayload: Record<string, unknown>): Promise<void> {
- const responseCallbackMethodName = `handleResponse${commandName}`;
- if (typeof this[responseCallbackMethodName] === 'function') {
- await this[responseCallbackMethodName](payload, requestPayload);
+ private responseHandlers: Map<OCPP16RequestCommand, ResponseHandler>;
+
+ constructor(chargingStation: ChargingStation) {
+ super(chargingStation);
+ this.responseHandlers = new Map<OCPP16RequestCommand, ResponseHandler>([
+ [OCPP16RequestCommand.BOOT_NOTIFICATION, this.handleResponseBootNotification.bind(this)],
+ [OCPP16RequestCommand.HEARTBEAT, this.handleResponseHeartbeat.bind(this)],
+ [OCPP16RequestCommand.AUTHORIZE, this.handleResponseAuthorize.bind(this)],
+ [OCPP16RequestCommand.START_TRANSACTION, this.handleResponseStartTransaction.bind(this)],
+ [OCPP16RequestCommand.STOP_TRANSACTION, this.handleResponseStopTransaction.bind(this)],
+ [OCPP16RequestCommand.STATUS_NOTIFICATION, this.handleResponseStatusNotification.bind(this)],
+ [OCPP16RequestCommand.METER_VALUES, this.handleResponseMeterValues.bind(this)]
+ ]);
+ }
+
+ public async handleResponse(commandName: OCPP16RequestCommand, payload: JsonType | string, requestPayload: JsonType): Promise<void> {
+ if (this.chargingStation.isRegistered() || commandName === OCPP16RequestCommand.BOOT_NOTIFICATION) {
+ if (this.responseHandlers.has(commandName)) {
+ try {
+ await this.responseHandlers.get(commandName)(payload, requestPayload);
+ } catch (error) {
+ logger.error(this.chargingStation.logPrefix() + ' Handle request response error: %j', error);
+ throw error;
+ }
+ } else {
+ // Throw exception
+ throw new OCPPError(ErrorType.NOT_IMPLEMENTED, `${commandName} is not implemented to handle request response payload ${JSON.stringify(payload, null, 2)}`, commandName);
+ }