import { IncomingRequestCommand, RequestCommand } from '../../types/ocpp/Requests';
+import BaseError from '../../exception/BaseError';
import { ErrorType } from '../../types/ocpp/ErrorType';
-export default class OCPPError extends Error {
+export default class OCPPError extends BaseError {
code: ErrorType | IncomingRequestCommand;
command?: RequestCommand | IncomingRequestCommand;
details?: Record<string, unknown>;
constructor(code: ErrorType | IncomingRequestCommand, message: string, command?: RequestCommand | IncomingRequestCommand, details?: Record<string, unknown>) {
super(message);
- this.name = new.target.name;
this.code = code ?? ErrorType.GENERIC_ERROR;
- this.message = message ?? '';
this.command = command;
this.details = details ?? {};
-
- Object.setPrototypeOf(this, new.target.prototype);
-
- Error.captureStackTrace ? Error.captureStackTrace(this, this.constructor) : (this.stack = (new Error()).stack);
}
}
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
// Send a message through wsConnection
- return new Promise((resolve: (value?: any | PromiseLike<any>) => void, reject: (reason?: any) => void) => {
- let messageToSend: string;
- // Type of message
- switch (messageType) {
- // Request
- case MessageType.CALL_MESSAGE:
- // Build request
- this.chargingStation.requests.set(messageId, [responseCallback, rejectCallback, commandParams as Record<string, unknown>]);
- messageToSend = JSON.stringify([messageType, messageId, commandName, commandParams]);
- break;
- // Response
- case MessageType.CALL_RESULT_MESSAGE:
- // Build response
- messageToSend = JSON.stringify([messageType, messageId, commandParams]);
- break;
- // Error Message
- case MessageType.CALL_ERROR_MESSAGE:
- // Build Error Message
- messageToSend = JSON.stringify([messageType, messageId, commandParams?.code ?? ErrorType.GENERIC_ERROR, commandParams?.message ?? '', commandParams?.details ?? {}]);
- break;
- }
+ return new Promise((resolve, reject) => {
+ const messageToSend = this.buildMessageToSend(messageId, commandParams, messageType, commandName, responseCallback, rejectCallback);
if (this.chargingStation.getEnableStatistics()) {
this.chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType);
}
// Response?
if (messageType === MessageType.CALL_RESULT_MESSAGE) {
// Yes: send Ok
- resolve();
+ resolve(commandName);
} else if (messageType === MessageType.CALL_ERROR_MESSAGE) {
// Send timeout
setTimeout(() => rejectCallback(new OCPPError(commandParams?.code ?? ErrorType.GENERIC_ERROR, commandParams?.message ?? `Timeout for message id '${messageId}' with content '${messageToSend}'`, commandParams?.details ?? {})), Constants.OCPP_ERROR_TIMEOUT);
throw error;
}
+ private buildMessageToSend(messageId: string, commandParams: any, messageType: MessageType, commandName: RequestCommand | IncomingRequestCommand,
+ responseCallback: (payload: Record<string, unknown> | string, requestPayload: Record<string, unknown>) => Promise<void>, rejectCallback: (error: OCPPError) => void): string {
+ let messageToSend: string;
+ // Type of message
+ switch (messageType) {
+ // Request
+ case MessageType.CALL_MESSAGE:
+ // Build request
+ this.chargingStation.requests.set(messageId, [responseCallback, rejectCallback, commandParams as Record<string, unknown>]);
+ messageToSend = JSON.stringify([messageType, messageId, commandName, commandParams]);
+ break;
+ // Response
+ case MessageType.CALL_RESULT_MESSAGE:
+ // Build response
+ messageToSend = JSON.stringify([messageType, messageId, commandParams]);
+ break;
+ // Error Message
+ case MessageType.CALL_ERROR_MESSAGE:
+ // Build Error Message
+ messageToSend = JSON.stringify([messageType, messageId, commandParams?.code ?? ErrorType.GENERIC_ERROR, commandParams?.message ?? '', commandParams?.details ?? {}]);
+ break;
+ }
+ return messageToSend;
+ }
+
public abstract sendHeartbeat(): Promise<void>;
public abstract sendBootNotification(chargePointModel: string, chargePointVendor: string, chargeBoxSerialNumber?: string, firmwareVersion?: string, chargePointSerialNumber?: string, iccid?: string, imsi?: string, meterSerialNumber?: string, meterType?: string): Promise<BootNotificationResponse>;
public abstract sendStatusNotification(connectorId: number, status: ChargePointStatus, errorCode?: ChargePointErrorCode): Promise<void>;