import Ajv, { type JSONSchemaType } from 'ajv';
import ajvFormats from 'ajv-formats';
-import type OCPPResponseService from './OCPPResponseService';
-import { OCPPServiceUtils } from './OCPPServiceUtils';
-import OCPPError from '../../exception/OCPPError';
-import PerformanceStatistics from '../../performance/PerformanceStatistics';
-import type { EmptyObject } from '../../types/EmptyObject';
-import type { HandleErrorParams } from '../../types/Error';
-import type { JsonObject, JsonType } from '../../types/JsonType';
-import { ErrorType } from '../../types/ocpp/ErrorType';
-import { MessageType } from '../../types/ocpp/MessageType';
-import type { OCPPVersion } from '../../types/ocpp/OCPPVersion';
+import { type OCPPResponseService, OCPPServiceUtils } from './internal';
+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,
+ MessageType,
+ type OCPPVersion,
type OutgoingRequest,
RequestCommand,
type RequestParams,
+ type Response,
type ResponseCallback,
type ResponseType,
-} from '../../types/ocpp/Requests';
-import type { ErrorResponse, Response } from '../../types/ocpp/Responses';
-import Constants from '../../utils/Constants';
-import logger from '../../utils/Logger';
-import Utils from '../../utils/Utils';
-import type ChargingStation from '../ChargingStation';
+} from '../../types';
+import { Constants, Utils, logger } from '../../utils';
const moduleName = 'OCPPRequestService';
-export default abstract class OCPPRequestService {
+export abstract class OCPPRequestService {
private static instance: OCPPRequestService | null = null;
private readonly version: OCPPVersion;
private readonly ajv: Ajv;
messageId: string,
messagePayload: JsonType,
commandName: IncomingRequestCommand
- ): Promise<ResponseType | undefined> {
+ ): Promise<ResponseType> {
try {
// Send response message
return await this.internalSendMessage(
// Send a message through wsConnection
return Utils.promiseWithTimeout(
new Promise((resolve, reject) => {
+ /**
+ * Function that will receive the request's response
+ *
+ * @param payload -
+ * @param requestPayload -
+ */
+ const responseCallback = (payload: JsonType, requestPayload: JsonType): void => {
+ if (chargingStation.getEnableStatistics() === true) {
+ chargingStation.performanceStatistics?.addRequestStatistic(
+ commandName,
+ MessageType.CALL_RESULT_MESSAGE
+ );
+ }
+ // Handle the request's response
+ self.ocppResponseService
+ .responseHandler(
+ chargingStation,
+ commandName as RequestCommand,
+ payload,
+ requestPayload
+ )
+ .then(() => {
+ resolve(payload);
+ })
+ .catch((error) => {
+ reject(error);
+ })
+ .finally(() => {
+ chargingStation.requests.delete(messageId);
+ });
+ };
+
+ /**
+ * Function that will receive the request's error response
+ *
+ * @param error -
+ * @param requestStatistic -
+ */
+ const errorCallback = (error: OCPPError, requestStatistic = true): void => {
+ if (requestStatistic === true && chargingStation.getEnableStatistics() === true) {
+ chargingStation.performanceStatistics?.addRequestStatistic(
+ commandName,
+ MessageType.CALL_ERROR_MESSAGE
+ );
+ }
+ logger.error(
+ `${chargingStation.logPrefix()} Error occurred at ${OCPPServiceUtils.getMessageTypeString(
+ messageType
+ )} command ${commandName} with PDU %j:`,
+ messagePayload,
+ error
+ );
+ chargingStation.requests.delete(messageId);
+ reject(error);
+ };
+
if (chargingStation.getEnableStatistics() === true) {
- chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType);
+ chargingStation.performanceStatistics?.addRequestStatistic(commandName, messageType);
}
const messageToSend = this.buildMessageToSend(
chargingStation,
ErrorType.GENERIC_ERROR,
`WebSocket closed or errored for buffered message id '${messageId}' with content '${messageToSend}'`,
commandName,
- (messagePayload as JsonObject)?.details ?? {}
+ (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FREEZED_OBJECT
)
);
} else if (wsClosedOrErrored) {
ErrorType.GENERIC_ERROR,
`WebSocket closed or errored for non buffered message id '${messageId}' with content '${messageToSend}'`,
commandName,
- (messagePayload as JsonObject)?.details ?? {}
+ (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FREEZED_OBJECT
);
// Reject response
if (messageType !== MessageType.CALL_MESSAGE) {
if (messageType !== MessageType.CALL_MESSAGE) {
return resolve(messagePayload);
}
-
- /**
- * Function that will receive the request's response
- *
- * @param payload -
- * @param requestPayload -
- */
- function responseCallback(payload: JsonType, requestPayload: JsonType): void {
- if (chargingStation.getEnableStatistics() === true) {
- chargingStation.performanceStatistics.addRequestStatistic(
- commandName,
- MessageType.CALL_RESULT_MESSAGE
- );
- }
- // Handle the request's response
- self.ocppResponseService
- .responseHandler(
- chargingStation,
- commandName as RequestCommand,
- payload,
- requestPayload
- )
- .then(() => {
- resolve(payload);
- })
- .catch((error) => {
- reject(error);
- })
- .finally(() => {
- chargingStation.requests.delete(messageId);
- });
- }
-
- /**
- * Function that will receive the request's error response
- *
- * @param error -
- * @param requestStatistic -
- */
- function errorCallback(error: OCPPError, requestStatistic = true): void {
- if (requestStatistic === true && chargingStation.getEnableStatistics() === true) {
- chargingStation.performanceStatistics.addRequestStatistic(
- commandName,
- MessageType.CALL_ERROR_MESSAGE
- );
- }
- logger.error(
- `${chargingStation.logPrefix()} Error occurred at ${OCPPServiceUtils.getMessageTypeString(
- messageType
- )} command ${commandName} with PDU %j:`,
- messagePayload,
- error
- );
- chargingStation.requests.delete(messageId);
- reject(error);
- }
}),
Constants.OCPP_WEBSOCKET_TIMEOUT,
new OCPPError(
ErrorType.GENERIC_ERROR,
`Timeout for message id '${messageId}'`,
commandName,
- (messagePayload as JsonObject)?.details ?? {}
+ (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FREEZED_OBJECT
),
() => {
messageType === MessageType.CALL_MESSAGE && chargingStation.requests.delete(messageId);