X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPRequestService.ts;h=8eaeb833009be16deaeaece0da07609f84c65448;hb=9f2e313013116428f5bce2be59e2f5c07502c026;hp=ca1363929872f0c9209ca2d1e815fbbd91822845;hpb=672fed6e70e94e37ba8db689d8517f42ae0f4477;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index ca136392..8eaeb833 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -4,28 +4,36 @@ import { DiagnosticsStatus, IncomingRequestCommand, RequestCommand, SendParams } import { BootNotificationResponse } from '../../types/ocpp/Responses'; import { ChargePointErrorCode } from '../../types/ocpp/ChargePointErrorCode'; import { ChargePointStatus } from '../../types/ocpp/ChargePointStatus'; -import ChargingStation from '../ChargingStation'; +import type ChargingStation from '../ChargingStation'; import Constants from '../../utils/Constants'; import { ErrorType } from '../../types/ocpp/ErrorType'; import { JsonType } from '../../types/JsonType'; import { MessageType } from '../../types/ocpp/MessageType'; import { MeterValue } from '../../types/ocpp/MeterValues'; import OCPPError from '../../exception/OCPPError'; -import OCPPResponseService from './OCPPResponseService'; +import type OCPPResponseService from './OCPPResponseService'; import PerformanceStatistics from '../../performance/PerformanceStatistics'; import Utils from '../../utils/Utils'; import logger from '../../utils/Logger'; export default abstract class OCPPRequestService { - public chargingStation: ChargingStation; - protected ocppResponseService: OCPPResponseService; + private static readonly instances: Map = new Map(); + protected readonly chargingStation: ChargingStation; + private readonly ocppResponseService: OCPPResponseService; - constructor(chargingStation: ChargingStation, ocppResponseService: OCPPResponseService) { + protected constructor(chargingStation: ChargingStation, ocppResponseService: OCPPResponseService) { this.chargingStation = chargingStation; this.ocppResponseService = ocppResponseService; } - public async sendMessage(messageId: string, messageData: JsonType | OCPPError, messageType: MessageType, commandName: RequestCommand | IncomingRequestCommand, + public static getInstance(this: new (chargingStation: ChargingStation, ocppResponseService: OCPPResponseService) => T, chargingStation: ChargingStation, ocppResponseService: OCPPResponseService): T { + if (!OCPPRequestService.instances.has(chargingStation.id)) { + OCPPRequestService.instances.set(chargingStation.id, new this(chargingStation, ocppResponseService)); + } + return OCPPRequestService.instances.get(chargingStation.id) as T; + } + + protected async sendMessage(messageId: string, messageData: JsonType | OCPPError, messageType: MessageType, commandName: RequestCommand | IncomingRequestCommand, params: SendParams = { skipBufferingOnError: false, triggerMessage: false @@ -111,7 +119,7 @@ export default abstract class OCPPRequestService { } protected handleRequestError(commandName: RequestCommand, error: Error): void { - logger.error(this.chargingStation.logPrefix() + ' Request command ' + commandName + ' error: %j', error); + logger.error(this.chargingStation.logPrefix() + ' Request command %s error: %j', commandName, error); throw error; }