import { OCPPServiceUtils } from './OCPPServiceUtils';
import type { ChargingStation } from '../../charging-station';
import { OCPPError } from '../../exception';
-// import { PerformanceStatistics } from '../../performance';
-import { PerformanceStatistics } from '../../performance/PerformanceStatistics';
+import { PerformanceStatistics } from '../../performance';
import {
- type EmptyObject,
type ErrorCallback,
type ErrorResponse,
ErrorType,
- type HandleErrorParams,
type IncomingRequestCommand,
type JsonObject,
type JsonType,
type ResponseCallback,
type ResponseType,
} from '../../types';
-import { Constants, Utils, logger } from '../../utils';
+import { Constants, Utils, handleSendMessageError, logger } from '../../utils';
const moduleName = 'OCPPRequestService';
+const defaultRequestParams: RequestParams = {
+ skipBufferingOnError: false,
+ triggerMessage: false,
+ throwError: false,
+};
+
export abstract class OCPPRequestService {
private static instance: OCPPRequestService | null = null;
private readonly version: OCPPVersion;
commandName
);
} catch (error) {
- this.handleSendMessageError(chargingStation, commandName, error as Error, {
+ handleSendMessageError(chargingStation, commandName, error as Error, {
throwError: true,
});
}
commandName
);
} catch (error) {
- this.handleSendMessageError(chargingStation, commandName, error as Error);
+ handleSendMessageError(chargingStation, commandName, error as Error);
}
}
messageId: string,
messagePayload: JsonType,
commandName: RequestCommand,
- params: RequestParams = {
- skipBufferingOnError: false,
- triggerMessage: false,
- throwError: false,
- }
+ params: RequestParams = defaultRequestParams
): Promise<ResponseType> {
+ params = {
+ ...defaultRequestParams,
+ ...params,
+ };
try {
return await this.internalSendMessage(
chargingStation,
params
);
} catch (error) {
- this.handleSendMessageError(chargingStation, commandName, error as Error, {
+ handleSendMessageError(chargingStation, commandName, error as Error, {
throwError: params.throwError,
});
}
messagePayload: JsonType | OCPPError,
messageType: MessageType,
commandName: RequestCommand | IncomingRequestCommand,
- params: RequestParams = {
- skipBufferingOnError: false,
- triggerMessage: false,
- }
+ params: RequestParams = defaultRequestParams
): Promise<ResponseType> {
+ params = {
+ ...defaultRequestParams,
+ ...params,
+ };
if (
(chargingStation.inUnknownState() === true &&
commandName === RequestCommand.BOOT_NOTIFICATION) ||
return messageToSend;
}
- private handleSendMessageError(
- chargingStation: ChargingStation,
- commandName: RequestCommand | IncomingRequestCommand,
- error: Error,
- params: HandleErrorParams<EmptyObject> = { throwError: false }
- ): void {
- logger.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error);
- if (params?.throwError === true) {
- throw error;
- }
- }
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public abstract requestHandler<ReqType extends JsonType, ResType extends JsonType>(
chargingStation: ChargingStation,