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,
)}`,
);
while (this.connectorsStatus.get(connectorId)?.start === true) {
- if (new Date() > this.connectorsStatus.get(connectorId)!.stopDate!) {
- this.stopConnector(connectorId);
- break;
- }
- if (this.chargingStation.inAcceptedState() === false) {
- logger.error(
- `${this.logPrefix(
- connectorId,
- )} entered in transaction loop while the charging station is not in accepted state`,
- );
- this.stopConnector(connectorId);
- break;
- }
- if (this.chargingStation.isChargingStationAvailable() === false) {
- logger.info(
- `${this.logPrefix(
- connectorId,
- )} entered in transaction loop while the charging station is unavailable`,
- );
- this.stopConnector(connectorId);
- break;
- }
- if (this.chargingStation.isConnectorAvailable(connectorId) === false) {
- logger.info(
- `${this.logPrefix(
- connectorId,
- )} entered in transaction loop while the connector ${connectorId} is unavailable`,
- );
- this.stopConnector(connectorId);
- break;
- }
- if (
- this.chargingStation.getConnectorStatus(connectorId)?.status ===
- ConnectorStatusEnum.Unavailable
- ) {
- logger.info(
- `${this.logPrefix(
- connectorId,
- )} entered in transaction loop while the connector ${connectorId} status is unavailable`,
- );
+ if (!this.canStartConnector(connectorId)) {
this.stopConnector(connectorId);
break;
}
this.connectorsStatus.get(connectorId)!.start = true;
}
+ private canStartConnector(connectorId: number): boolean {
+ if (new Date() > this.connectorsStatus.get(connectorId)!.stopDate!) {
+ return false;
+ }
+ if (this.chargingStation.inAcceptedState() === false) {
+ logger.error(
+ `${this.logPrefix(
+ connectorId,
+ )} entered in transaction loop while the charging station is not in accepted state`,
+ );
+ return false;
+ }
+ if (this.chargingStation.isChargingStationAvailable() === false) {
+ logger.info(
+ `${this.logPrefix(
+ connectorId,
+ )} entered in transaction loop while the charging station is unavailable`,
+ );
+ return false;
+ }
+ if (this.chargingStation.isConnectorAvailable(connectorId) === false) {
+ logger.info(
+ `${this.logPrefix(
+ connectorId,
+ )} entered in transaction loop while the connector ${connectorId} is unavailable`,
+ );
+ 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 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(this.chargingStation.getAutomaticTransactionGeneratorStatuses()!)[connectorId]
+ const connectorStatus = this.chargingStation.getAutomaticTransactionGeneratorStatuses()?.[
+ connectorId
+ ]
+ ? cloneObject<Status[]>(this.chargingStation.getAutomaticTransactionGeneratorStatuses()!)[
+ connectorId
+ ]
: undefined;
delete connectorStatus?.startDate;
delete connectorStatus?.lastRunDate;
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