+import { ErrorResponse, Response } from '../../types/ocpp/Responses';
import {
IncomingRequestCommand,
+ OutgoingRequest,
RequestCommand,
+ RequestParams,
ResponseType,
- SendParams,
} from '../../types/ocpp/Requests';
import type ChargingStation from '../ChargingStation';
) {
this.chargingStation = chargingStation;
this.ocppResponseService = ocppResponseService;
- this.sendMessageHandler.bind(this);
+ this.requestHandler.bind(this);
this.sendResult.bind(this);
this.sendError.bind(this);
}
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,
}
(!this.chargingStation.getOcppStrictCompliance() &&
this.chargingStation.isInUnknownState()) ||
this.chargingStation.isInAcceptedState() ||
- (this.chargingStation.isInPendingState() && params.triggerMessage)
+ (this.chargingStation.isInPendingState() &&
+ (params.triggerMessage || messageType === MessageType.CALL_RESULT_MESSAGE))
) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
// FIXME: Handle sending error
this.chargingStation.wsConnection.send(messageToSend);
PerformanceStatistics.endMeasure(commandName, beginId);
+ logger.debug(
+ `${this.chargingStation.logPrefix()} >> Command '${commandName}' sent ${this.getMessageTypeString(
+ messageType
+ )} payload: ${messageToSend}`
+ );
} else if (!params.skipBufferingOnError) {
// Buffer it
this.chargingStation.bufferMessage(messageToSend);
* @param requestPayload
*/
async function responseCallback(
- payload: JsonType | string,
+ payload: JsonType,
requestPayload: JsonType
): Promise<void> {
if (self.chargingStation.getEnableStatistics()) {
}
// Handle the request's response
try {
- await self.ocppResponseService.handleResponse(
+ await self.ocppResponseService.responseHandler(
commandName as RequestCommand,
payload,
requestPayload
messagePayload: JsonType | OCPPError,
messageType: MessageType,
commandName?: RequestCommand | IncomingRequestCommand,
- responseCallback?: (payload: JsonType | string, requestPayload: JsonType) => Promise<void>,
+ responseCallback?: (payload: JsonType, requestPayload: JsonType) => Promise<void>,
rejectCallback?: (error: OCPPError, requestStatistic?: boolean) => void
): string {
let messageToSend: string;
responseCallback,
rejectCallback,
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;
}
+ private getMessageTypeString(messageType: MessageType): string {
+ switch (messageType) {
+ case MessageType.CALL_MESSAGE:
+ return 'request';
+ case MessageType.CALL_RESULT_MESSAGE:
+ return 'response';
+ case MessageType.CALL_ERROR_MESSAGE:
+ return 'error';
+ }
+ }
+
private handleRequestError(
commandName: RequestCommand | IncomingRequestCommand,
error: Error,
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
- public abstract sendMessageHandler<Request extends JsonType, Response extends JsonType>(
+ public abstract requestHandler<Request extends JsonType, Response extends JsonType>(
commandName: RequestCommand,
commandParams?: JsonType,
- params?: SendParams
+ params?: RequestParams
): Promise<Response>;
}