+ private initializeConnectorsStatus(): void {
+ if (this.chargingStation.hasEvses) {
+ for (const [evseId, evseStatus] of this.chargingStation.evses) {
+ if (evseId > 0) {
+ for (const connectorId of evseStatus.connectors.keys()) {
+ this.connectorsStatus.set(connectorId, this.getConnectorStatus(connectorId));
+ }
+ }
+ }
+ } else {
+ for (const connectorId of this.chargingStation.connectors.keys()) {
+ if (connectorId > 0) {
+ this.connectorsStatus.set(connectorId, this.getConnectorStatus(connectorId));
+ }
+ }
+ }
+ }
+
+ private getConnectorStatus(connectorId: number): Status {
+ const connectorStatus = this.chargingStation.getAutomaticTransactionGeneratorStatuses()?.[
+ connectorId
+ ]
+ ? cloneObject<Status>(
+ this.chargingStation.getAutomaticTransactionGeneratorStatuses()![connectorId],
+ )
+ : undefined;
+ delete connectorStatus?.startDate;
+ delete connectorStatus?.lastRunDate;
+ delete connectorStatus?.stopDate;
+ delete connectorStatus?.stoppedDate;
+ return (
+ connectorStatus ?? {
+ start: false,
+ authorizeRequests: 0,
+ acceptedAuthorizeRequests: 0,
+ rejectedAuthorizeRequests: 0,
+ startTransactionRequests: 0,
+ acceptedStartTransactionRequests: 0,
+ rejectedStartTransactionRequests: 0,
+ stopTransactionRequests: 0,
+ acceptedStopTransactionRequests: 0,
+ rejectedStopTransactionRequests: 0,
+ skippedConsecutiveTransactions: 0,
+ skippedTransactions: 0,
+ }
+ );
+ }
+
+ private async startTransaction(
+ connectorId: number,
+ ): Promise<StartTransactionResponse | undefined> {
+ const measureId = 'StartTransaction with ATG';
+ const beginId = PerformanceStatistics.beginMeasure(measureId);
+ let startResponse: StartTransactionResponse | undefined;
+ 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.connectorsStatus.get(connectorId)!.authorizeRequests!;
+ if (await OCPPServiceUtils.isIdTagAuthorized(this.chargingStation, connectorId, idTag)) {
+ ++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 | undefined> {
+ const measureId = 'StopTransaction with ATG';
+ const beginId = PerformanceStatistics.beginMeasure(measureId);
+ let stopResponse: StopTransactionResponse | undefined;
+ 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${
+ !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 logPrefix(
+ ` ${this.chargingStation.stationInfo.chargingStationId} | ATG${
+ !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!;
+ }