X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPRequestService.ts;h=4115eff1144f1c50a91e79f7197632f47fd6e553;hb=910c3f0cae3d7e9a9fb82608c3a26df37ff19b29;hp=c885a68e52a6f535886e103686f021f6b87bce89;hpb=18bf82749012da310723996912f5239c20bdc4e7;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index c885a68e..4115eff1 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -1,34 +1,33 @@ import Ajv, { type JSONSchemaType } from 'ajv'; import ajvFormats from 'ajv-formats'; -import type OCPPResponseService from './OCPPResponseService'; -import { OCPPServiceUtils } from './OCPPServiceUtils'; -import OCPPError from '../../exception/OCPPError'; -import PerformanceStatistics from '../../performance/PerformanceStatistics'; -import type { EmptyObject } from '../../types/EmptyObject'; -import type { HandleErrorParams } from '../../types/Error'; -import type { JsonObject, JsonType } from '../../types/JsonType'; -import { ErrorType } from '../../types/ocpp/ErrorType'; -import { MessageType } from '../../types/ocpp/MessageType'; -import type { OCPPVersion } from '../../types/ocpp/OCPPVersion'; +import { type OCPPResponseService, OCPPServiceUtils } from './internal'; +import type { ChargingStation } from '../../charging-station'; +import { OCPPError } from '../../exception'; +import { PerformanceStatistics } from '../../performance'; import { + type EmptyObject, type ErrorCallback, + type ErrorResponse, + ErrorType, + type HandleErrorParams, type IncomingRequestCommand, + type JsonObject, + type JsonType, + MessageType, + type OCPPVersion, type OutgoingRequest, RequestCommand, type RequestParams, + type Response, type ResponseCallback, type ResponseType, -} from '../../types/ocpp/Requests'; -import type { ErrorResponse, Response } from '../../types/ocpp/Responses'; -import Constants from '../../utils/Constants'; -import logger from '../../utils/Logger'; -import Utils from '../../utils/Utils'; -import type ChargingStation from '../ChargingStation'; +} from '../../types'; +import { Constants, Utils, logger } from '../../utils'; const moduleName = 'OCPPRequestService'; -export default abstract class OCPPRequestService { +export abstract class OCPPRequestService { private static instance: OCPPRequestService | null = null; private readonly version: OCPPVersion; private readonly ajv: Ajv; @@ -211,7 +210,7 @@ export default abstract class OCPPRequestService { messageId: string, messagePayload: JsonType | OCPPError, messageType: MessageType, - commandName?: RequestCommand | IncomingRequestCommand, + commandName: RequestCommand | IncomingRequestCommand, params: RequestParams = { skipBufferingOnError: false, triggerMessage: false, @@ -231,6 +230,9 @@ export default abstract class OCPPRequestService { // Send a message through wsConnection return Utils.promiseWithTimeout( new Promise((resolve, reject) => { + if (chargingStation.getEnableStatistics() === true) { + chargingStation.performanceStatistics?.addRequestStatistic(commandName, messageType); + } const messageToSend = this.buildMessageToSend( chargingStation, messageId, @@ -240,33 +242,30 @@ export default abstract class OCPPRequestService { responseCallback, errorCallback ); - if (chargingStation.getEnableStatistics() === true) { - chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType); - } let sendError = false; // Check if wsConnection opened - if (chargingStation.isWebSocketConnectionOpened() === true) { - const beginId = PerformanceStatistics.beginMeasure(commandName as string); + const wsOpened = chargingStation.isWebSocketConnectionOpened() === true; + if (wsOpened) { + const beginId = PerformanceStatistics.beginMeasure(commandName); try { - chargingStation.wsConnection.send(messageToSend); + chargingStation.wsConnection?.send(messageToSend); logger.debug( - `${chargingStation.logPrefix()} >> Command '${commandName}' sent ${this.getMessageTypeString( + `${chargingStation.logPrefix()} >> Command '${commandName}' sent ${OCPPServiceUtils.getMessageTypeString( messageType )} payload: ${messageToSend}` ); } catch (error) { logger.error( - `${chargingStation.logPrefix()} >> Command '${commandName}' failed to send ${this.getMessageTypeString( + `${chargingStation.logPrefix()} >> Command '${commandName}' failed to send ${OCPPServiceUtils.getMessageTypeString( messageType )} payload: ${messageToSend}:`, error ); sendError = true; } - PerformanceStatistics.endMeasure(commandName as string, beginId); + PerformanceStatistics.endMeasure(commandName, beginId); } - const wsClosedOrErrored = - chargingStation.isWebSocketConnectionOpened() === false || sendError === true; + const wsClosedOrErrored = !wsOpened || sendError === true; if (wsClosedOrErrored && params.skipBufferingOnError === false) { // Buffer chargingStation.bufferMessage(messageToSend); @@ -276,7 +275,7 @@ export default abstract class OCPPRequestService { ErrorType.GENERIC_ERROR, `WebSocket closed or errored for buffered message id '${messageId}' with content '${messageToSend}'`, commandName, - (messagePayload as JsonObject)?.details ?? {} + (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FREEZED_OBJECT ) ); } else if (wsClosedOrErrored) { @@ -284,7 +283,7 @@ export default abstract class OCPPRequestService { ErrorType.GENERIC_ERROR, `WebSocket closed or errored for non buffered message id '${messageId}' with content '${messageToSend}'`, commandName, - (messagePayload as JsonObject)?.details ?? {} + (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FREEZED_OBJECT ); // Reject response if (messageType !== MessageType.CALL_MESSAGE) { @@ -306,7 +305,7 @@ export default abstract class OCPPRequestService { */ function responseCallback(payload: JsonType, requestPayload: JsonType): void { if (chargingStation.getEnableStatistics() === true) { - chargingStation.performanceStatistics.addRequestStatistic( + chargingStation.performanceStatistics?.addRequestStatistic( commandName, MessageType.CALL_RESULT_MESSAGE ); @@ -338,13 +337,13 @@ export default abstract class OCPPRequestService { */ function errorCallback(error: OCPPError, requestStatistic = true): void { if (requestStatistic === true && chargingStation.getEnableStatistics() === true) { - chargingStation.performanceStatistics.addRequestStatistic( + chargingStation.performanceStatistics?.addRequestStatistic( commandName, MessageType.CALL_ERROR_MESSAGE ); } logger.error( - `${chargingStation.logPrefix()} Error occurred at ${self.getMessageTypeString( + `${chargingStation.logPrefix()} Error occurred at ${OCPPServiceUtils.getMessageTypeString( messageType )} command ${commandName} with PDU %j:`, messagePayload, @@ -359,7 +358,7 @@ export default abstract class OCPPRequestService { ErrorType.GENERIC_ERROR, `Timeout for message id '${messageId}'`, commandName, - (messagePayload as JsonObject)?.details ?? {} + (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FREEZED_OBJECT ), () => { messageType === MessageType.CALL_MESSAGE && chargingStation.requests.delete(messageId); @@ -378,9 +377,9 @@ export default abstract class OCPPRequestService { messageId: string, messagePayload: JsonType | OCPPError, messageType: MessageType, - commandName?: RequestCommand | IncomingRequestCommand, - responseCallback?: ResponseCallback, - errorCallback?: ErrorCallback + commandName: RequestCommand | IncomingRequestCommand, + responseCallback: ResponseCallback, + errorCallback: ErrorCallback ): string { let messageToSend: string; // Type of message @@ -427,17 +426,6 @@ export default abstract class OCPPRequestService { 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 handleSendMessageError( chargingStation: ChargingStation, commandName: RequestCommand | IncomingRequestCommand,