- this.connectorsStatus.get(connectorId).lastRunDate = new Date();
- }
- await this.stopTransaction(connectorId);
- this.connectorsStatus.get(connectorId).stoppedDate = new Date();
- logger.info(this.logPrefix(connectorId) + ' stopped on connector and lasted for ' + Utils.formatDurationMilliSeconds(this.connectorsStatus.get(connectorId).stoppedDate.getTime() - this.connectorsStatus.get(connectorId).startDate.getTime()));
- logger.debug(`${this.logPrefix(connectorId)} connector status %j`, this.connectorsStatus.get(connectorId));
- }
-
- private startConnector(connectorId: number): void {
- // Avoid hogging the event loop with a busy loop
- setImmediate(() => {
- this.internalStartConnector(connectorId).catch(() => { /* This is intentional */ });
- });
- }
-
- private stopConnector(connectorId: number): void {
- this.connectorsStatus.set(connectorId, { ...this.connectorsStatus.get(connectorId), start: false });
- }
-
- private initStartConnectorStatus(connectorId: number): void {
- this.connectorsStatus.get(connectorId).authorizeRequests = this?.connectorsStatus.get(connectorId)?.authorizeRequests ?? 0;
- this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests = this?.connectorsStatus.get(connectorId)?.acceptedAuthorizeRequests ?? 0;
- this.connectorsStatus.get(connectorId).rejectedAuthorizeRequests = this?.connectorsStatus.get(connectorId)?.rejectedAuthorizeRequests ?? 0;
- this.connectorsStatus.get(connectorId).startTransactionRequests = this?.connectorsStatus.get(connectorId)?.startTransactionRequests ?? 0;
- this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests = this?.connectorsStatus.get(connectorId)?.acceptedStartTransactionRequests ?? 0;
- this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests = this?.connectorsStatus.get(connectorId)?.rejectedStartTransactionRequests ?? 0;
- this.connectorsStatus.get(connectorId).stopTransactionRequests = this?.connectorsStatus.get(connectorId)?.stopTransactionRequests ?? 0;
- this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0;
- this.connectorsStatus.get(connectorId).skippedTransactions = this?.connectorsStatus.get(connectorId)?.skippedTransactions ?? 0;
- const previousRunDuration = (this?.connectorsStatus.get(connectorId)?.startDate && this?.connectorsStatus.get(connectorId)?.lastRunDate)
- ? (this.connectorsStatus.get(connectorId).lastRunDate.getTime() - this.connectorsStatus.get(connectorId).startDate.getTime())
- : 0;
- this.connectorsStatus.get(connectorId).startDate = new Date();
- this.connectorsStatus.get(connectorId).stopDate = new Date(this.connectorsStatus.get(connectorId).startDate.getTime()
- + (this.chargingStation.stationInfo?.AutomaticTransactionGenerator?.stopAfterHours ?? Constants.CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS) * 3600 * 1000
- - previousRunDuration);
- this.connectorsStatus.get(connectorId).start = true;
- }
-
- private async startTransaction(connectorId: number): Promise<StartTransactionResponse | AuthorizeResponse> {
- const measureId = 'StartTransaction with ATG';
- const beginId = PerformanceStatistics.beginMeasure(measureId);
- let startResponse: StartTransactionResponse;
- if (this.chargingStation.hasAuthorizedTags()) {
- const idTag = this.chargingStation.getRandomIdTag();
- if (this.chargingStation.getAutomaticTransactionGeneratorRequireAuthorize()) {
- // Authorize idTag
- const authorizeResponse = await this.chargingStation.ocppRequestService.sendAuthorize(connectorId, idTag);
- this.connectorsStatus.get(connectorId).authorizeRequests++;
- if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
- this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests++;
- logger.info(this.logPrefix(connectorId) + ' start transaction for idTag ' + idTag);
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.connectorsStatus.get(connectorId)!.lastRunDate = new Date()
+ }
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.connectorsStatus.get(connectorId)!.stoppedDate = new Date()
+ logger.info(
+ `${this.logPrefix(
+ connectorId
+ )} stopped on connector and lasted for ${formatDurationMilliSeconds(
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.connectorsStatus.get(connectorId)!.stoppedDate!.getTime() -
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.connectorsStatus.get(connectorId)!.startDate!.getTime()
+ )}`
+ )
+ logger.debug(
+ `${this.logPrefix(connectorId)} connector status: %j`,
+ this.connectorsStatus.get(connectorId)
+ )
+ }
+
+ private setStartConnectorStatus (connectorId: number): void {
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.connectorsStatus.get(connectorId)!.startDate = new Date()
+ if (
+ this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.stopAbsoluteDuration ===
+ false ||
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ !isValidDate(this.connectorsStatus.get(connectorId)!.stopDate)
+ ) {
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.connectorsStatus.get(connectorId)!.stopDate = new Date(
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.connectorsStatus.get(connectorId)!.startDate!.getTime() +
+ hoursToMilliseconds(
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.chargingStation.getAutomaticTransactionGeneratorConfiguration()!.stopAfterHours
+ )
+ )
+ }
+ delete this.connectorsStatus.get(connectorId)?.stoppedDate
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.connectorsStatus.get(connectorId)!.skippedConsecutiveTransactions = 0
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.connectorsStatus.get(connectorId)!.start = true
+ }
+
+ private canStartConnector (connectorId: number): boolean {
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ if (new Date() > this.connectorsStatus.get(connectorId)!.stopDate!) {
+ logger.info(
+ `${this.logPrefix(
+ connectorId
+ )} entered in transaction loop while the ATG stop date has been reached`
+ )
+ return false
+ }
+ if (!this.chargingStation.inAcceptedState()) {
+ logger.error(
+ `${this.logPrefix(
+ connectorId
+ )} entered in transaction loop while the charging station is not in accepted state`
+ )
+ return false
+ }
+ if (!this.chargingStation.isChargingStationAvailable()) {
+ logger.info(
+ `${this.logPrefix(
+ connectorId
+ )} entered in transaction loop while the charging station is unavailable`
+ )
+ return false
+ }
+ if (!this.chargingStation.isConnectorAvailable(connectorId)) {
+ logger.info(
+ `${this.logPrefix(
+ connectorId
+ )} entered in transaction loop while the connector ${connectorId} is unavailable`
+ )
+ return false
+ }
+ return true
+ }
+
+ private async waitChargingStationAvailable (connectorId: number): Promise<void> {
+ let logged = false
+ while (!this.chargingStation.isChargingStationAvailable()) {
+ if (!logged) {
+ logger.info(
+ `${this.logPrefix(
+ connectorId
+ )} transaction loop waiting for charging station to be available`
+ )
+ logged = true
+ }
+ await sleep(Constants.CHARGING_STATION_ATG_AVAILABILITY_TIME)
+ }
+ }
+
+ private async waitConnectorAvailable (connectorId: number): Promise<void> {
+ let logged = false
+ while (!this.chargingStation.isConnectorAvailable(connectorId)) {
+ if (!logged) {
+ logger.info(
+ `${this.logPrefix(
+ connectorId
+ )} transaction loop waiting for connector ${connectorId} to be available`
+ )
+ logged = true
+ }
+ await sleep(Constants.CHARGING_STATION_ATG_AVAILABILITY_TIME)
+ }
+ }
+
+ 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 - 1] != null
+ ? clone<Status>(
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.chargingStation.getAutomaticTransactionGeneratorStatuses()![connectorId - 1]
+ )
+ : undefined
+ if (connectorStatus != null) {
+ connectorStatus.startDate = convertToDate(connectorStatus.startDate)
+ connectorStatus.lastRunDate = convertToDate(connectorStatus.lastRunDate)
+ connectorStatus.stopDate = convertToDate(connectorStatus.stopDate)
+ connectorStatus.stoppedDate = convertToDate(connectorStatus.stoppedDate)
+ if (
+ !this.started &&
+ (connectorStatus.start ||
+ this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.enable !== true)
+ ) {
+ connectorStatus.start = false
+ }
+ }
+ 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(
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.chargingStation.getAutomaticTransactionGeneratorConfiguration()!.idTagDistribution!,
+ this.chargingStation,
+ connectorId
+ )
+ const startTransactionLogMsg = `${this.logPrefix(
+ connectorId
+ )} start transaction with an idTag '${idTag}'`
+ if (this.getRequireAuthorize()) {
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ ++this.connectorsStatus.get(connectorId)!.authorizeRequests
+ if (await isIdTagAuthorized(this.chargingStation, connectorId, idTag)) {
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ ++this.connectorsStatus.get(connectorId)!.acceptedAuthorizeRequests
+ logger.info(startTransactionLogMsg)