From ae725be37ef803522ad393a6a5d9d8f15616c6bb Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 30 Jul 2023 23:30:56 +0200 Subject: [PATCH] fix: fix authorize response handling MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../AutomaticTransactionGenerator.ts | 21 +------ .../ocpp/1.6/OCPP16ResponseService.ts | 4 +- .../ocpp/1.6/OCPP16ServiceUtils.ts | 56 +----------------- src/charging-station/ocpp/OCPPServiceUtils.ts | 57 ++++++++++++++++++- 4 files changed, 61 insertions(+), 77 deletions(-) diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 1dc0a809..ae4beec8 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -7,12 +7,11 @@ import { hoursToMilliseconds, secondsToMilliseconds } from 'date-fns'; import type { ChargingStation } from './ChargingStation'; 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, @@ -397,24 +396,8 @@ export class AutomaticTransactionGenerator extends AsyncResource { connectorId, )} start transaction with an idTag '${idTag}'`; if (this.getRequireAuthorize()) { - // 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 ( - isNullOrUndefined(this.chargingStation.getConnectorStatus(connectorId)?.authorizeIdTag) - ) { - logger.warn( - `${this.chargingStation.logPrefix()} IdTag ${idTag} is not set as authorized remotely, applying deferred initialization`, - ); - this.chargingStation.getConnectorStatus(connectorId)!.authorizeIdTag = idTag; - } + if (await OCPPServiceUtils.isIdTagAuthorized(this.chargingStation, connectorId, idTag)) { ++this.connectorsStatus.get(connectorId)!.acceptedAuthorizeRequests!; logger.info(startTransactionLogMsg); // Start transaction diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 262f3b53..3ec3c159 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -472,8 +472,8 @@ export class OCPP16ResponseService extends OCPPResponseService { const authorizeConnectorIdDefined = !isNullOrUndefined(authorizeConnectorId); if (payload.idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED) { if (authorizeConnectorIdDefined) { - chargingStation.getConnectorStatus(authorizeConnectorId!)!.authorizeIdTag = - requestPayload.idTag; + // chargingStation.getConnectorStatus(authorizeConnectorId!)!.authorizeIdTag = + // requestPayload.idTag; chargingStation.getConnectorStatus(authorizeConnectorId!)!.idTagAuthorized = true; } logger.debug( diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index bf019cee..00594e1c 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -2,11 +2,10 @@ import type { JSONSchemaType } from 'ajv'; -import { type ChargingStation, getIdTagsFile, hasFeatureProfile } from '../../../charging-station'; +import { type ChargingStation, hasFeatureProfile } from '../../../charging-station'; import { OCPPError } from '../../../exception'; import { type ClearChargingProfileRequest, - type ConnectorStatus, CurrentType, ErrorType, type JsonType, @@ -15,9 +14,6 @@ import { MeterValueContext, MeterValueLocation, MeterValueUnit, - OCPP16AuthorizationStatus, - type OCPP16AuthorizeRequest, - type OCPP16AuthorizeResponse, type OCPP16ChargingProfile, type OCPP16IncomingRequestCommand, type OCPP16MeterValue, @@ -41,7 +37,6 @@ import { getRandomFloatRounded, getRandomInteger, isNotEmptyArray, - isNotEmptyString, isNullOrUndefined, isUndefined, logger, @@ -886,29 +881,6 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { ); } - public static async isIdTagAuthorized( - chargingStation: ChargingStation, - connectorId: number, - idTag: string, - ): Promise { - let authorized = false; - const connectorStatus: ConnectorStatus = chargingStation.getConnectorStatus(connectorId)!; - if (OCPP16ServiceUtils.isIdTagLocalAuthorized(chargingStation, idTag)) { - connectorStatus.localAuthorizeIdTag = idTag; - connectorStatus.idTagLocalAuthorized = true; - authorized = true; - } else { - authorized = await OCPP16ServiceUtils.isIdTagRemoteAuthorized(chargingStation, idTag); - if (authorized && isNullOrUndefined(connectorStatus.authorizeIdTag)) { - logger.warn( - `${chargingStation.logPrefix()} IdTag ${idTag} is not set as authorized remotely, applying deferred initialization`, - ); - connectorStatus.authorizeIdTag = idTag; - } - } - return authorized; - } - private static buildSampledValue( sampledValueTemplate: SampledValueTemplate, value: number, @@ -984,30 +956,4 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { return MeterValueUnit.VOLT; } } - - private static isIdTagLocalAuthorized(chargingStation: ChargingStation, idTag: string): boolean { - return ( - chargingStation.getLocalAuthListEnabled() === true && - chargingStation.hasIdTags() === true && - isNotEmptyString( - chargingStation.idTagsCache - .getIdTags(getIdTagsFile(chargingStation.stationInfo)!) - ?.find((tag) => tag === idTag), - ) - ); - } - - private static async isIdTagRemoteAuthorized( - chargingStation: ChargingStation, - idTag: string, - ): Promise { - const authorizeResponse: OCPP16AuthorizeResponse = - await chargingStation.ocppRequestService.requestHandler< - OCPP16AuthorizeRequest, - OCPP16AuthorizeResponse - >(chargingStation, OCPP16RequestCommand.AUTHORIZE, { - idTag, - }); - return authorizeResponse?.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED; - } } diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index d09d8b1a..64fe38ea 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -8,10 +8,14 @@ import { isDate } from 'date-fns'; import { OCPP16Constants } from './1.6/OCPP16Constants'; import { OCPP20Constants } from './2.0/OCPP20Constants'; import { OCPPConstants } from './OCPPConstants'; -import { type ChargingStation, getConfigurationKey } from '../../charging-station'; +import { type ChargingStation, getConfigurationKey, getIdTagsFile } from '../../charging-station'; import { BaseError } from '../../exception'; import { + AuthorizationStatus, + type AuthorizeRequest, + type AuthorizeResponse, ChargePointErrorCode, + type ConnectorStatus, type ConnectorStatusEnum, ErrorType, FileType, @@ -218,6 +222,27 @@ export class OCPPServiceUtils { chargingStation.getConnectorStatus(connectorId)!.status = status; } + public static async isIdTagAuthorized( + chargingStation: ChargingStation, + connectorId: number, + idTag: string, + ): Promise { + let authorized = false; + if (OCPPServiceUtils.isIdTagLocalAuthorized(chargingStation, idTag)) { + const connectorStatus: ConnectorStatus = chargingStation.getConnectorStatus(connectorId)!; + connectorStatus.localAuthorizeIdTag = idTag; + connectorStatus.idTagLocalAuthorized = true; + authorized = true; + } else { + authorized = await OCPPServiceUtils.isIdTagRemoteAuthorized( + chargingStation, + connectorId, + idTag, + ); + } + return authorized; + } + protected static checkConnectorStatusTransition( chargingStation: ChargingStation, connectorId: number, @@ -397,6 +422,36 @@ export class OCPPServiceUtils { : numberValue * options.unitMultiplier!; } + private static isIdTagLocalAuthorized(chargingStation: ChargingStation, idTag: string): boolean { + return ( + chargingStation.getLocalAuthListEnabled() === true && + chargingStation.hasIdTags() === true && + isNotEmptyString( + chargingStation.idTagsCache + .getIdTags(getIdTagsFile(chargingStation.stationInfo)!) + ?.find((tag) => tag === idTag), + ) + ); + } + + private static async isIdTagRemoteAuthorized( + chargingStation: ChargingStation, + connectorId: number, + idTag: string, + ): Promise { + chargingStation.getConnectorStatus(connectorId)!.authorizeIdTag = idTag; + return ( + ( + await chargingStation.ocppRequestService.requestHandler< + AuthorizeRequest, + AuthorizeResponse + >(chargingStation, RequestCommand.AUTHORIZE, { + idTag, + }) + )?.idTagInfo?.status === AuthorizationStatus.ACCEPTED + ); + } + private static logPrefix = ( ocppVersion: OCPPVersion, moduleName?: string, -- 2.34.1