X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPRequestService.ts;h=125baf4073366623f05c67797d6e375ef532f148;hb=edd134392e237a3242dc2093341df70244c51472;hp=9fa50b4f2f53a7dc7f2b6f982208f4441451d87f;hpb=012ae1a9ba663125ec972fa560bb9ee3691aa57a;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 9fa50b4f..125baf40 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -10,11 +10,13 @@ import type { JsonObject, JsonType } from '../../types/JsonType'; import { ErrorType } from '../../types/ocpp/ErrorType'; import { MessageType } from '../../types/ocpp/MessageType'; import { - IncomingRequestCommand, - OutgoingRequest, + type ErrorCallback, + type IncomingRequestCommand, + type OutgoingRequest, RequestCommand, - RequestParams, - ResponseType, + type RequestParams, + type ResponseCallback, + type ResponseType, } from '../../types/ocpp/Requests'; import type { ErrorResponse, Response } from '../../types/ocpp/Responses'; import Constants from '../../utils/Constants'; @@ -159,11 +161,13 @@ export default abstract class OCPPRequestService { } ): Promise { if ( - (chargingStation.isInUnknownState() && commandName === RequestCommand.BOOT_NOTIFICATION) || - (!chargingStation.getOcppStrictCompliance() && chargingStation.isInUnknownState()) || - chargingStation.isInAcceptedState() || - (chargingStation.isInPendingState() && - (params.triggerMessage || messageType === MessageType.CALL_RESULT_MESSAGE)) + (chargingStation.isInUnknownState() === true && + commandName === RequestCommand.BOOT_NOTIFICATION) || + (chargingStation.getOcppStrictCompliance() === false && + chargingStation.isInUnknownState() === true) || + chargingStation.isInAcceptedState() === true || + (chargingStation.isInPendingState() === true && + (params.triggerMessage === true || messageType === MessageType.CALL_RESULT_MESSAGE)) ) { // eslint-disable-next-line @typescript-eslint/no-this-alias const self = this; @@ -185,10 +189,10 @@ export default abstract class OCPPRequestService { // Check if wsConnection opened if (chargingStation.isWebSocketConnectionOpened() === true) { // Yes: Send Message - const beginId = PerformanceStatistics.beginMeasure(commandName); + const beginId = PerformanceStatistics.beginMeasure(commandName as string); // FIXME: Handle sending error chargingStation.wsConnection.send(messageToSend); - PerformanceStatistics.endMeasure(commandName, beginId); + PerformanceStatistics.endMeasure(commandName as string, beginId); logger.debug( `${chargingStation.logPrefix()} >> Command '${commandName}' sent ${this.getMessageTypeString( messageType @@ -229,13 +233,10 @@ export default abstract class OCPPRequestService { /** * Function that will receive the request's response * - * @param payload - * @param requestPayload + * @param payload - + * @param requestPayload - */ - async function responseCallback( - payload: JsonType, - requestPayload: JsonType - ): Promise { + function responseCallback(payload: JsonType, requestPayload: JsonType): void { if (chargingStation.getEnableStatistics() === true) { chargingStation.performanceStatistics.addRequestStatistic( commandName, @@ -243,26 +244,29 @@ export default abstract class OCPPRequestService { ); } // Handle the request's response - try { - await self.ocppResponseService.responseHandler( + self.ocppResponseService + .responseHandler( chargingStation, commandName as RequestCommand, payload, requestPayload - ); - resolve(payload); - } catch (error) { - reject(error); - } finally { - chargingStation.requests.delete(messageId); - } + ) + .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 + * @param error - + * @param requestStatistic - */ function errorCallback(error: OCPPError, requestStatistic = true): void { if (requestStatistic === true && chargingStation.getEnableStatistics() === true) { @@ -306,8 +310,8 @@ export default abstract class OCPPRequestService { messagePayload: JsonType | OCPPError, messageType: MessageType, commandName?: RequestCommand | IncomingRequestCommand, - responseCallback?: (payload: JsonType, requestPayload: JsonType) => Promise, - errorCallback?: (error: OCPPError, requestStatistic?: boolean) => void + responseCallback?: ResponseCallback, + errorCallback?: ErrorCallback ): string { let messageToSend: string; // Type of message @@ -372,10 +376,10 @@ export default abstract class OCPPRequestService { } // eslint-disable-next-line @typescript-eslint/no-unused-vars - public abstract requestHandler( + public abstract requestHandler( chargingStation: ChargingStation, commandName: RequestCommand, commandParams?: JsonType, params?: RequestParams - ): Promise; + ): Promise; }