+import { ErrorResponse, Response } from '../../types/ocpp/Responses';
import {
IncomingRequestCommand,
+ OutgoingRequest,
RequestCommand,
+ RequestParams,
ResponseType,
- SendParams,
} from '../../types/ocpp/Requests';
+import { JsonObject, JsonType } from '../../types/JsonType';
import type ChargingStation from '../ChargingStation';
import Constants from '../../utils/Constants';
import { EmptyObject } from '../../types/EmptyObject';
import { ErrorType } from '../../types/ocpp/ErrorType';
import { HandleErrorParams } from '../../types/Error';
-import { JsonType } from '../../types/JsonType';
import { MessageType } from '../../types/ocpp/MessageType';
import OCPPError from '../../exception/OCPPError';
import type OCPPResponseService from './OCPPResponseService';
this.chargingStation = chargingStation;
this.ocppResponseService = ocppResponseService;
this.requestHandler.bind(this);
- this.sendResult.bind(this);
+ this.sendResponse.bind(this);
this.sendError.bind(this);
}
return OCPPRequestService.instances.get(chargingStation.hashId) as T;
}
- public async sendResult(
+ public async sendResponse(
messageId: string,
messagePayload: JsonType,
commandName: IncomingRequestCommand
): Promise<ResponseType> {
try {
- // Send result message
+ // Send response message
return await this.internalSendMessage(
messageId,
messagePayload,
public async sendError(
messageId: string,
ocppError: OCPPError,
- commandName: IncomingRequestCommand
+ commandName: RequestCommand | IncomingRequestCommand
): Promise<ResponseType> {
try {
// Send error message
messageId: string,
messagePayload: JsonType,
commandName: RequestCommand,
- params: SendParams = {
+ params: RequestParams = {
skipBufferingOnError: false,
triggerMessage: false,
}
messagePayload: JsonType | OCPPError,
messageType: MessageType,
commandName?: RequestCommand | IncomingRequestCommand,
- params: SendParams = {
+ params: RequestParams = {
skipBufferingOnError: false,
triggerMessage: false,
}
messageType,
commandName,
responseCallback,
- rejectCallback
+ errorCallback
);
if (this.chargingStation.getEnableStatistics()) {
this.chargingStation.performanceStatistics.addRequestStatistic(
ErrorType.GENERIC_ERROR,
`WebSocket closed for buffered message id '${messageId}' with content '${messageToSend}'`,
commandName,
- (messagePayload?.details as JsonType) ?? {}
+ (messagePayload as JsonObject)?.details ?? {}
);
if (messageType === MessageType.CALL_MESSAGE) {
// Reject it but keep the request in the cache
return reject(ocppError);
}
- return rejectCallback(ocppError, false);
+ return errorCallback(ocppError, false);
} else {
// Reject it
- return rejectCallback(
+ return errorCallback(
new OCPPError(
ErrorType.GENERIC_ERROR,
`WebSocket closed for non buffered message id '${messageId}' with content '${messageToSend}'`,
commandName,
- (messagePayload?.details as JsonType) ?? {}
+ (messagePayload as JsonObject)?.details ?? {}
),
false
);
* @param requestPayload
*/
async function responseCallback(
- payload: JsonType | string,
+ payload: JsonType,
requestPayload: JsonType
): Promise<void> {
if (self.chargingStation.getEnableStatistics()) {
* @param error
* @param requestStatistic
*/
- function rejectCallback(error: OCPPError, requestStatistic = true): void {
+ function errorCallback(error: OCPPError, requestStatistic = true): void {
if (requestStatistic && self.chargingStation.getEnableStatistics()) {
self.chargingStation.performanceStatistics.addRequestStatistic(
commandName,
ErrorType.GENERIC_ERROR,
`Timeout for message id '${messageId}'`,
commandName,
- (messagePayload?.details as JsonType) ?? {}
+ (messagePayload as JsonObject)?.details ?? {}
),
() => {
messageType === MessageType.CALL_MESSAGE &&
messagePayload: JsonType | OCPPError,
messageType: MessageType,
commandName?: RequestCommand | IncomingRequestCommand,
- responseCallback?: (payload: JsonType | string, requestPayload: JsonType) => Promise<void>,
- rejectCallback?: (error: OCPPError, requestStatistic?: boolean) => void
+ responseCallback?: (payload: JsonType, requestPayload: JsonType) => Promise<void>,
+ errorCallback?: (error: OCPPError, requestStatistic?: boolean) => void
): string {
let messageToSend: string;
// Type of message
// Build request
this.chargingStation.requests.set(messageId, [
responseCallback,
- rejectCallback,
+ errorCallback,
commandName,
- messagePayload,
+ messagePayload as JsonType,
]);
- messageToSend = JSON.stringify([messageType, messageId, commandName, messagePayload]);
+ messageToSend = JSON.stringify([
+ messageType,
+ messageId,
+ commandName,
+ messagePayload,
+ ] as OutgoingRequest);
break;
// Response
case MessageType.CALL_RESULT_MESSAGE:
// Build response
- messageToSend = JSON.stringify([messageType, messageId, messagePayload]);
+ messageToSend = JSON.stringify([messageType, messageId, messagePayload] as Response);
break;
// Error Message
case MessageType.CALL_ERROR_MESSAGE:
messageToSend = JSON.stringify([
messageType,
messageId,
- messagePayload?.code ?? ErrorType.GENERIC_ERROR,
- messagePayload?.message ?? '',
- messagePayload?.details ?? { commandName },
- ]);
+ (messagePayload as OCPPError)?.code ?? ErrorType.GENERIC_ERROR,
+ (messagePayload as OCPPError)?.message ?? '',
+ (messagePayload as OCPPError)?.details ?? { commandName },
+ ] as ErrorResponse);
break;
}
return messageToSend;
public abstract requestHandler<Request extends JsonType, Response extends JsonType>(
commandName: RequestCommand,
commandParams?: JsonType,
- params?: SendParams
+ params?: RequestParams
): Promise<Response>;
}