import { hoursToMilliseconds, secondsToMilliseconds } from 'date-fns';
import type { ChargingStation } from './ChargingStation';
-import { checkChargingStation } from './ChargingStationUtils';
+import { checkChargingStation } from './Helpers';
import { IdTagsCache } from './IdTagsCache';
+import { OCPPServiceUtils } from './ocpp';
import { BaseError } from '../exception';
import { PerformanceStatistics } from '../performance';
import {
AuthorizationStatus,
- type AuthorizeRequest,
- type AuthorizeResponse,
- ConnectorStatusEnum,
RequestCommand,
type StartTransactionRequest,
type StartTransactionResponse,
)}`,
);
while (this.connectorsStatus.get(connectorId)?.start === true) {
+ await this.waitChargingStationServiceInitialization(connectorId);
+ await this.waitChargingStationAvailable(connectorId);
+ await this.waitConnectorAvailable(connectorId);
if (!this.canStartConnector(connectorId)) {
this.stopConnector(connectorId);
break;
}
- if (!this.chargingStation?.ocppRequestService) {
- logger.info(
- `${this.logPrefix(
- connectorId,
- )} transaction loop waiting for charging station service to be initialized`,
- );
- do {
- await sleep(Constants.CHARGING_STATION_ATG_INITIALIZATION_TIME);
- } while (!this.chargingStation?.ocppRequestService);
- }
const wait = secondsToMilliseconds(
getRandomInteger(
this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
),
);
logger.info(
- `${this.logPrefix(connectorId)} transaction started with id ${this.chargingStation
- .getConnectorStatus(connectorId)
- ?.transactionId?.toString()} and will stop in ${formatDurationMilliSeconds(
- waitTrxEnd,
- )}`,
+ `${this.logPrefix(
+ connectorId,
+ )} transaction started with id ${this.chargingStation.getConnectorStatus(connectorId)
+ ?.transactionId} and will stop in ${formatDurationMilliSeconds(waitTrxEnd)}`,
);
await sleep(waitTrxEnd);
- // Stop transaction
- logger.info(
- `${this.logPrefix(connectorId)} stop transaction with id ${this.chargingStation
- .getConnectorStatus(connectorId)
- ?.transactionId?.toString()}`,
- );
await this.stopTransaction(connectorId);
}
} else {
++this.connectorsStatus.get(connectorId)!.skippedConsecutiveTransactions!;
++this.connectorsStatus.get(connectorId)!.skippedTransactions!;
logger.info(
- `${this.logPrefix(connectorId)} skipped consecutively ${this.connectorsStatus
- .get(connectorId)
- ?.skippedConsecutiveTransactions?.toString()}/${this.connectorsStatus
- .get(connectorId)
- ?.skippedTransactions?.toString()} transaction(s)`,
+ `${this.logPrefix(connectorId)} skipped consecutively ${this.connectorsStatus.get(
+ connectorId,
+ )?.skippedConsecutiveTransactions}/${this.connectorsStatus.get(connectorId)
+ ?.skippedTransactions} transaction(s)`,
);
}
this.connectorsStatus.get(connectorId)!.lastRunDate = new Date();
);
return false;
}
- if (
- this.chargingStation.getConnectorStatus(connectorId)?.status ===
- ConnectorStatusEnum.Unavailable
- ) {
- logger.info(
- `${this.logPrefix(
- connectorId,
- )} entered in transaction loop while the connector ${connectorId} status is unavailable`,
- );
- return false;
- }
return true;
}
+ private async waitChargingStationServiceInitialization(connectorId: number): Promise<void> {
+ let logged = false;
+ while (!this.chargingStation?.ocppRequestService) {
+ if (!logged) {
+ logger.info(
+ `${this.logPrefix(
+ connectorId,
+ )} transaction loop waiting for charging station service to be initialized`,
+ );
+ logged = true;
+ }
+ await sleep(Constants.CHARGING_STATION_ATG_INITIALIZATION_TIME);
+ }
+ }
+
+ 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) {
}
private getConnectorStatus(connectorId: number): Status {
- const connectorStatus = this.chargingStation.getAutomaticTransactionGeneratorStatuses()
- ? cloneObject<Status[]>(this.chargingStation.getAutomaticTransactionGeneratorStatuses()!)[
- connectorId
- ]
+ 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;
+ this.resetConnectorStatus(connectorStatus);
return (
connectorStatus ?? {
start: false,
);
}
+ private resetConnectorStatus(connectorStatus: Status | undefined): void {
+ delete connectorStatus?.startDate;
+ delete connectorStatus?.lastRunDate;
+ delete connectorStatus?.stopDate;
+ delete connectorStatus?.stoppedDate;
+ if (
+ !this.started &&
+ (connectorStatus?.start === true ||
+ this.chargingStation.getAutomaticTransactionGeneratorConfiguration().enable === false)
+ ) {
+ connectorStatus!.start = false;
+ }
+ }
+
private async startTransaction(
connectorId: number,
): Promise<StartTransactionResponse | undefined> {
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) {
+ if (await OCPPServiceUtils.isIdTagAuthorized(this.chargingStation, connectorId, idTag)) {
++this.connectorsStatus.get(connectorId)!.acceptedAuthorizeRequests!;
logger.info(startTransactionLogMsg);
// Start transaction
private async stopTransaction(
connectorId: number,
- reason: StopTransactionReason = StopTransactionReason.LOCAL,
+ reason = 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) {
+ logger.info(
+ `${this.logPrefix(
+ connectorId,
+ )} stop transaction with id ${this.chargingStation.getConnectorStatus(connectorId)
+ ?.transactionId}`,
+ );
stopResponse = await this.chargingStation.stopTransactionOnConnector(connectorId, reason);
++this.connectorsStatus.get(connectorId)!.stopTransactionRequests!;
if (stopResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
}
} else {
const transactionId = this.chargingStation.getConnectorStatus(connectorId)?.transactionId;
- logger.warn(
+ logger.debug(
`${this.logPrefix(connectorId)} stopping a not started transaction${
- !isNullOrUndefined(transactionId) ? ` with id ${transactionId?.toString()}` : ''
+ !isNullOrUndefined(transactionId) ? ` with id ${transactionId}` : ''
}`,
);
}
private logPrefix = (connectorId?: number): string => {
return logPrefix(
` ${this.chargingStation.stationInfo.chargingStationId} | ATG${
- !isNullOrUndefined(connectorId) ? ` on connector #${connectorId!.toString()}` : ''
+ !isNullOrUndefined(connectorId) ? ` on connector #${connectorId}` : ''
}:`,
);
};