Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Improve OCPP incoming requests handling
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
  • Loading branch information
Jérôme Benoit committed May 1, 2022
1 parent bd31d7f commit a2d1c0f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
44 changes: 21 additions & 23 deletions src/charging-station/ChargingStation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1577,7 +1577,7 @@ export default class ChargingStation {
let errorMessage: string;
let errorDetails: JsonType;
let responseCallback: (payload: JsonType, requestPayload: JsonType) => void;
let rejectCallback: (error: OCPPError, requestStatistic?: boolean) => void;
let errorCallback: (error: OCPPError, requestStatistic?: boolean) => void;
let requestCommandName: RequestCommand | IncomingRequestCommand;
let requestPayload: JsonType;
let cachedRequest: CachedRequest;
Expand Down Expand Up @@ -1609,6 +1609,15 @@ export default class ChargingStation {
// Outcome Message
case MessageType.CALL_RESULT_MESSAGE:
[, , commandPayload] = request as Response;
if (!this.requests.has(messageId)) {
// Error
throw new OCPPError(
ErrorType.INTERNAL_ERROR,
`Response for unknown message id ${messageId}`,
null,
commandPayload
);
}
// Respond
cachedRequest = this.requests.get(messageId);
if (Utils.isIterable(cachedRequest)) {
Expand All @@ -1626,23 +1635,23 @@ export default class ChargingStation {
requestCommandName ?? ''
}' received response payload: ${JSON.stringify(request)}`
);
if (!responseCallback) {
responseCallback(commandPayload, requestPayload);
break;
// Error Message
case MessageType.CALL_ERROR_MESSAGE:
[, , errorType, errorMessage, errorDetails] = request as ErrorResponse;
if (!this.requests.has(messageId)) {
// Error
throw new OCPPError(
ErrorType.INTERNAL_ERROR,
`Response for unknown message id ${messageId}`,
`Error response for unknown message id ${messageId}`,
null,
commandPayload
{ errorType, errorMessage, errorDetails }
);
}
responseCallback(commandPayload, requestPayload);
break;
// Error Message
case MessageType.CALL_ERROR_MESSAGE:
[, , errorType, errorMessage, errorDetails] = request as ErrorResponse;
cachedRequest = this.requests.get(messageId);
if (Utils.isIterable(cachedRequest)) {
[, rejectCallback, requestCommandName] = cachedRequest;
[, errorCallback, requestCommandName] = cachedRequest;
} else {
throw new OCPPError(
ErrorType.PROTOCOL_ERROR,
Expand All @@ -1656,18 +1665,7 @@ export default class ChargingStation {
requestCommandName ?? ''
}' received error payload: ${JSON.stringify(request)}`
);
if (!rejectCallback) {
// Error
throw new OCPPError(
ErrorType.INTERNAL_ERROR,
`Error response for unknown message id ${messageId}`,
null,
{ errorType, errorMessage, errorDetails }
);
}
rejectCallback(
new OCPPError(errorType, errorMessage, requestCommandName, errorDetails)
);
errorCallback(new OCPPError(errorType, errorMessage, requestCommandName, errorDetails));
break;
// Error
default:
Expand Down Expand Up @@ -1695,7 +1693,7 @@ export default class ChargingStation {
(await this.ocppRequestService.sendError(
messageId,
error as OCPPError,
Utils.isString(commandName) ? commandName : requestCommandName ?? null
commandName ?? requestCommandName ?? null
));
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/charging-station/ocpp/OCPPRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export default abstract class OCPPRequestService {
messageType,
commandName,
responseCallback,
rejectCallback
errorCallback
);
if (this.chargingStation.getEnableStatistics()) {
this.chargingStation.performanceStatistics.addRequestStatistic(
Expand Down Expand Up @@ -175,10 +175,10 @@ export default abstract class OCPPRequestService {
// 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}'`,
Expand Down Expand Up @@ -231,7 +231,7 @@ export default abstract class OCPPRequestService {
* @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,
Expand Down Expand Up @@ -274,7 +274,7 @@ export default abstract class OCPPRequestService {
messageType: MessageType,
commandName?: RequestCommand | IncomingRequestCommand,
responseCallback?: (payload: JsonType, requestPayload: JsonType) => Promise<void>,
rejectCallback?: (error: OCPPError, requestStatistic?: boolean) => void
errorCallback?: (error: OCPPError, requestStatistic?: boolean) => void
): string {
let messageToSend: string;
// Type of message
Expand All @@ -284,7 +284,7 @@ export default abstract class OCPPRequestService {
// Build request
this.chargingStation.requests.set(messageId, [
responseCallback,
rejectCallback,
errorCallback,
commandName,
messagePayload as JsonType,
]);
Expand Down

0 comments on commit a2d1c0f

Please sign in to comment.