X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPRequestService.ts;h=704838e960c0638eeef967f3480a303bca78f9c3;hb=b3ec7bc1553759e915e590df6a91f0849f03514d;hp=9dd0e7bf9c8fdd5744d824846d190ced6e6d5247;hpb=625b0fd5a433f3f9a65a03633ca9ebbd01c5e875;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 9dd0e7bf..704838e9 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -1,8 +1,10 @@ +import { ErrorResponse, Response } from '../../types/ocpp/Responses'; import { IncomingRequestCommand, + OutgoingRequest, RequestCommand, + RequestParams, ResponseType, - SendParams, } from '../../types/ocpp/Requests'; import type ChargingStation from '../ChargingStation'; @@ -16,7 +18,6 @@ import OCPPError from '../../exception/OCPPError'; import type OCPPResponseService from './OCPPResponseService'; import PerformanceStatistics from '../../performance/PerformanceStatistics'; import Utils from '../../utils/Utils'; -import chalk from 'chalk'; import logger from '../../utils/Logger'; export default abstract class OCPPRequestService { @@ -34,7 +35,7 @@ export default abstract class OCPPRequestService { ) { this.chargingStation = chargingStation; this.ocppResponseService = ocppResponseService; - this.sendMessageHandler.bind(this); + this.requestHandler.bind(this); this.sendResult.bind(this); this.sendError.bind(this); } @@ -74,7 +75,7 @@ export default abstract class OCPPRequestService { public async sendError( messageId: string, ocppError: OCPPError, - commandName: IncomingRequestCommand + commandName: RequestCommand | IncomingRequestCommand ): Promise { try { // Send error message @@ -93,7 +94,7 @@ export default abstract class OCPPRequestService { messageId: string, messagePayload: JsonType, commandName: RequestCommand, - params: SendParams = { + params: RequestParams = { skipBufferingOnError: false, triggerMessage: false, } @@ -116,7 +117,7 @@ export default abstract class OCPPRequestService { messagePayload: JsonType | OCPPError, messageType: MessageType, commandName?: RequestCommand | IncomingRequestCommand, - params: SendParams = { + params: RequestParams = { skipBufferingOnError: false, triggerMessage: false, } @@ -127,7 +128,8 @@ export default abstract class OCPPRequestService { (!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; @@ -152,10 +154,14 @@ export default abstract class OCPPRequestService { if (this.chargingStation.isWebSocketConnectionOpened()) { // Yes: Send Message const beginId = PerformanceStatistics.beginMeasure(commandName); - console.log(chalk`{blue >> Sending message = ${messageToSend}}`); // 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); @@ -195,7 +201,7 @@ export default abstract class OCPPRequestService { * @param requestPayload */ async function responseCallback( - payload: JsonType | string, + payload: JsonType, requestPayload: JsonType ): Promise { if (self.chargingStation.getEnableStatistics()) { @@ -206,7 +212,7 @@ export default abstract class OCPPRequestService { } // Handle the request's response try { - await self.ocppResponseService.handleResponse( + await self.ocppResponseService.responseHandler( commandName as RequestCommand, payload, requestPayload @@ -267,7 +273,7 @@ export default abstract class OCPPRequestService { messagePayload: JsonType | OCPPError, messageType: MessageType, commandName?: RequestCommand | IncomingRequestCommand, - responseCallback?: (payload: JsonType | string, requestPayload: JsonType) => Promise, + responseCallback?: (payload: JsonType, requestPayload: JsonType) => Promise, rejectCallback?: (error: OCPPError, requestStatistic?: boolean) => void ): string { let messageToSend: string; @@ -280,14 +286,19 @@ export default abstract class OCPPRequestService { 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: @@ -295,15 +306,26 @@ export default abstract class OCPPRequestService { 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, @@ -319,9 +341,10 @@ export default abstract class OCPPRequestService { } } - public abstract sendMessageHandler( + // eslint-disable-next-line @typescript-eslint/no-unused-vars + public abstract requestHandler( commandName: RequestCommand, commandParams?: JsonType, - params?: SendParams + params?: RequestParams ): Promise; }