+ private async startTransaction(
+ connectorId: number
+ ): Promise<StartTransactionResponse | undefined> {
+ const measureId = 'StartTransaction with ATG';
+ const beginId = PerformanceStatistics.beginMeasure(measureId);
+ let startResponse: StartTransactionResponse;
+ if (this.chargingStation.hasIdTags()) {
+ const idTag = IdTagsCache.getInstance().getIdTag(
+ this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.idTagDistribution,
+ this.chargingStation,
+ connectorId
+ );
+ const startTransactionLogMsg = `${this.logPrefix(
+ connectorId
+ )} start transaction with an idTag '${idTag}'`;
+ if (this.getRequireAuthorize()) {
+ this.chargingStation.getConnectorStatus(connectorId).authorizeIdTag = idTag;
+ // Authorize idTag
+ const authorizeResponse: AuthorizeResponse =
+ await this.chargingStation.ocppRequestService.requestHandler<
+ AuthorizeRequest,
+ AuthorizeResponse
+ >(this.chargingStation, RequestCommand.AUTHORIZE, {
+ idTag,
+ });
+ this.connectorsStatus.get(connectorId).authorizeRequests++;
+ if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
+ this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests++;
+ logger.info(startTransactionLogMsg);
+ // Start transaction
+ startResponse = await this.chargingStation.ocppRequestService.requestHandler<
+ StartTransactionRequest,
+ StartTransactionResponse
+ >(this.chargingStation, RequestCommand.START_TRANSACTION, {
+ connectorId,
+ idTag,
+ });
+ this.handleStartTransactionResponse(connectorId, startResponse);
+ PerformanceStatistics.endMeasure(measureId, beginId);
+ return startResponse;
+ }
+ this.connectorsStatus.get(connectorId).rejectedAuthorizeRequests++;
+ PerformanceStatistics.endMeasure(measureId, beginId);
+ return startResponse;
+ }
+ logger.info(startTransactionLogMsg);
+ // Start transaction
+ startResponse = await this.chargingStation.ocppRequestService.requestHandler<
+ StartTransactionRequest,
+ StartTransactionResponse
+ >(this.chargingStation, RequestCommand.START_TRANSACTION, {
+ connectorId,
+ idTag,
+ });
+ this.handleStartTransactionResponse(connectorId, startResponse);
+ PerformanceStatistics.endMeasure(measureId, beginId);
+ return startResponse;
+ }
+ logger.info(`${this.logPrefix(connectorId)} start transaction without an idTag`);
+ startResponse = await this.chargingStation.ocppRequestService.requestHandler<
+ StartTransactionRequest,
+ StartTransactionResponse
+ >(this.chargingStation, RequestCommand.START_TRANSACTION, { connectorId });
+ this.handleStartTransactionResponse(connectorId, startResponse);
+ PerformanceStatistics.endMeasure(measureId, beginId);
+ return startResponse;
+ }
+
+ private async stopTransaction(
+ connectorId: number,
+ reason: StopTransactionReason = StopTransactionReason.LOCAL
+ ): Promise<StopTransactionResponse> {
+ const measureId = 'StopTransaction with ATG';
+ const beginId = PerformanceStatistics.beginMeasure(measureId);
+ let stopResponse: StopTransactionResponse;
+ if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted === true) {
+ stopResponse = await this.chargingStation.stopTransactionOnConnector(connectorId, reason);
+ this.connectorsStatus.get(connectorId).stopTransactionRequests++;
+ if (stopResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
+ this.connectorsStatus.get(connectorId).acceptedStopTransactionRequests++;
+ } else {
+ this.connectorsStatus.get(connectorId).rejectedStopTransactionRequests++;
+ }
+ } else {
+ const transactionId = this.chargingStation.getConnectorStatus(connectorId)?.transactionId;
+ logger.warn(
+ `${this.logPrefix(connectorId)} stopping a not started transaction${
+ !Utils.isNullOrUndefined(transactionId) ? ` with id ${transactionId?.toString()}` : ''
+ }`
+ );
+ }
+ PerformanceStatistics.endMeasure(measureId, beginId);
+ return stopResponse;
+ }
+
+ private getRequireAuthorize(): boolean {
+ return (
+ this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.requireAuthorize ?? true
+ );
+ }
+
+ private logPrefix = (connectorId?: number): string => {
+ return Utils.logPrefix(
+ ` ${this.chargingStation.stationInfo.chargingStationId} | ATG${
+ !Utils.isNullOrUndefined(connectorId) ? ` on connector #${connectorId.toString()}` : ''
+ }:`
+ );
+ };
+
+ private handleStartTransactionResponse(
+ connectorId: number,
+ startResponse: StartTransactionResponse
+ ): void {
+ this.connectorsStatus.get(connectorId).startTransactionRequests++;
+ if (startResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
+ this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests++;
+ } else {
+ logger.warn(`${this.logPrefix(connectorId)} start transaction rejected`);
+ this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests++;
+ }