import Ajv, { type JSONSchemaType } from 'ajv';
import ajvFormats from 'ajv-formats';
-import { type OCPPResponseService, OCPPServiceUtils } from './internal';
+import { OCPPConstants } from './OCPPConstants';
+import type { OCPPResponseService } from './OCPPResponseService';
+import { OCPPServiceUtils } from './OCPPServiceUtils';
import type { ChargingStation } from '../../charging-station';
import { OCPPError } from '../../exception';
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.isInUnknownState() === true &&
+ (chargingStation.inUnknownState() === true &&
commandName === RequestCommand.BOOT_NOTIFICATION) ||
(chargingStation.getOcppStrictCompliance() === false &&
- chargingStation.isInUnknownState() === true) ||
- chargingStation.isInAcceptedState() === true ||
- (chargingStation.isInPendingState() === true &&
+ chargingStation.inUnknownState() === true) ||
+ chargingStation.inAcceptedState() === true ||
+ (chargingStation.inPendingState() === true &&
(params.triggerMessage === true || messageType === MessageType.CALL_RESULT_MESSAGE))
) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
return resolve(messagePayload);
}
}),
- Constants.OCPP_WEBSOCKET_TIMEOUT,
+ OCPPConstants.OCPP_WEBSOCKET_TIMEOUT,
new OCPPError(
ErrorType.GENERIC_ERROR,
`Timeout for message id '${messageId}'`,
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,