From 2466918c081644f9a1dc4722efbd002d7baf2925 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 4 Jan 2024 14:18:29 +0100 Subject: [PATCH] refactor: cleanup some unneeded conditions MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 48 +++++++++++-------- src/charging-station/Helpers.ts | 29 +++++------ .../ocpp/1.6/OCPP16IncomingRequestService.ts | 15 ++---- .../ocpp/1.6/OCPP16ServiceUtils.ts | 5 +- src/charging-station/ocpp/OCPPServiceUtils.ts | 3 +- src/performance/PerformanceStatistics.ts | 29 ++++++++--- 6 files changed, 68 insertions(+), 61 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 6513f68b..a57b5bd4 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -346,10 +346,11 @@ export class ChargingStation extends EventEmitter { public getConnectorMaximumAvailablePower (connectorId: number): number { let connectorAmperageLimitationPowerLimit: number | undefined + const amperageLimitation = this.getAmperageLimitation() if ( - this.getAmperageLimitation() != null && + amperageLimitation != null && // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.getAmperageLimitation()! < this.stationInfo!.maximumAmperage! + amperageLimitation < this.stationInfo!.maximumAmperage! ) { connectorAmperageLimitationPowerLimit = (this.stationInfo?.currentOutType === CurrentType.AC @@ -357,12 +358,11 @@ export class ChargingStation extends EventEmitter { this.getNumberOfPhases(), // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.stationInfo.voltageOut!, - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.getAmperageLimitation()! * + amperageLimitation * (this.hasEvses ? this.getNumberOfEvses() : this.getNumberOfConnectors()) ) : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - DCElectricUtils.power(this.stationInfo!.voltageOut!, this.getAmperageLimitation()!)) / + DCElectricUtils.power(this.stationInfo!.voltageOut!, amperageLimitation)) / // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.powerDivider! } @@ -855,7 +855,8 @@ export class ChargingStation extends EventEmitter { connectorId: number, reason?: StopTransactionReason ): Promise { - const transactionId = this.getConnectorStatus(connectorId)?.transactionId + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const transactionId = this.getConnectorStatus(connectorId)!.transactionId! if ( this.stationInfo?.beginEndMeterValues === true && this.stationInfo.ocppStrictCompliance === true && @@ -864,8 +865,7 @@ export class ChargingStation extends EventEmitter { const transactionEndMeterValue = buildTransactionEndMeterValue( this, connectorId, - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.getEnergyActiveImportRegisterByTransactionId(transactionId!) + this.getEnergyActiveImportRegisterByTransactionId(transactionId) ) await this.ocppRequestService.requestHandler( this, @@ -882,8 +882,7 @@ export class ChargingStation extends EventEmitter { StopTransactionResponse >(this, RequestCommand.STOP_TRANSACTION, { transactionId, - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId!, true), + meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId, true), ...(reason != null && { reason }) }) } @@ -1168,9 +1167,11 @@ export class ChargingStation extends EventEmitter { // Priority: // 1. charging station info from template // 2. charging station info from configuration file - if (stationInfoFromFile?.templateHash === stationInfoFromTemplate.templateHash) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - return { ...defaultStationInfo, ...stationInfoFromFile! } + if ( + stationInfoFromFile != null && + stationInfoFromFile.templateHash === stationInfoFromTemplate.templateHash + ) { + return { ...defaultStationInfo, ...stationInfoFromFile } } stationInfoFromFile != null && propagateSerialNumber( @@ -1217,7 +1218,7 @@ export class ChargingStation extends EventEmitter { isNotEmptyString(this.stationInfo.firmwareVersion) && isNotEmptyString(this.stationInfo.firmwareVersionPattern) ) { - const patternGroup: number | undefined = + const patternGroup = this.stationInfo.firmwareUpgrade?.versionUpgrade?.patternGroup ?? this.stationInfo.firmwareVersion?.split('.').length // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -1241,12 +1242,17 @@ export class ChargingStation extends EventEmitter { if (this.stationInfo.enableStatistics === true) { this.performanceStatistics = PerformanceStatistics.getInstance( this.stationInfo.hashId, - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.stationInfo.chargingStationId!, + this.stationInfo.chargingStationId, this.configuredSupervisionUrl ) } - this.bootNotificationRequest = createBootNotificationRequest(this.stationInfo) + const bootNotificationRequest = createBootNotificationRequest(this.stationInfo) + if (bootNotificationRequest == null) { + const errorMsg = 'Error while creating boot notification request' + logger.error(`${this.logPrefix()} ${errorMsg}`) + throw new BaseError(errorMsg) + } + this.bootNotificationRequest = bootNotificationRequest this.powerDivider = this.getPowerDivider() // OCPP configuration this.ocppConfiguration = this.getOcppConfiguration() @@ -1322,7 +1328,7 @@ export class ChargingStation extends EventEmitter { if ( isNotEmptyString(this.stationInfo?.amperageLimitationOcppKey) && // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - getConfigurationKey(this, this.stationInfo!.amperageLimitationOcppKey!) == null + getConfigurationKey(this, this.stationInfo.amperageLimitationOcppKey!) == null ) { addConfigurationKey( this, @@ -1633,10 +1639,10 @@ export class ChargingStation extends EventEmitter { if (!existsSync(dirname(this.configurationFile))) { mkdirSync(dirname(this.configurationFile), { recursive: true }) } + const configurationFromFile = this.getConfigurationFromFile() let configurationData: ChargingStationConfiguration = - this.getConfigurationFromFile() != null - ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - cloneObject(this.getConfigurationFromFile()!) + configurationFromFile != null + ? cloneObject(configurationFromFile) : {} if (this.stationInfo?.stationInfoPersistentConfiguration === true) { configurationData.stationInfo = this.stationInfo diff --git a/src/charging-station/Helpers.ts b/src/charging-station/Helpers.ts index 9a5eeb6e..ab3a6f6a 100644 --- a/src/charging-station/Helpers.ts +++ b/src/charging-station/Helpers.ts @@ -390,9 +390,8 @@ export const resetConnectorStatus = (connectorStatus: ConnectorStatus): void => export const createBootNotificationRequest = ( stationInfo: ChargingStationInfo, bootReason: BootReasonEnumType = BootReasonEnumType.PowerUp -): BootNotificationRequest => { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const ocppVersion = stationInfo.ocppVersion! +): BootNotificationRequest | undefined => { + const ocppVersion = stationInfo.ocppVersion switch (ocppVersion) { case OCPPVersion.VERSION_16: return { @@ -800,21 +799,21 @@ const getLimitFromChargingProfiles = ( const connectorStatus = chargingStation.getConnectorStatus(connectorId)! for (const chargingProfile of chargingProfiles) { const chargingSchedule = chargingProfile.chargingSchedule - if (chargingSchedule.startSchedule == null && connectorStatus.transactionStarted === true) { + if (chargingSchedule.startSchedule == null) { logger.debug( `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${chargingProfile.chargingProfileId} has no startSchedule defined. Trying to set it to the connector current transaction start date` ) // OCPP specifies that if startSchedule is not defined, it should be relative to start of the connector transaction chargingSchedule.startSchedule = connectorStatus.transactionStart } - if (chargingSchedule.startSchedule != null && !isDate(chargingSchedule.startSchedule)) { + if (!isDate(chargingSchedule.startSchedule)) { logger.warn( `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${chargingProfile.chargingProfileId} startSchedule property is not a Date instance. Trying to convert it to a Date instance` ) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion chargingSchedule.startSchedule = convertToDate(chargingSchedule.startSchedule)! } - if (chargingSchedule.startSchedule != null && chargingSchedule.duration == null) { + if (chargingSchedule.duration == null) { logger.debug( `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${chargingProfile.chargingProfileId} has no duration defined and will be set to the maximum time allowed` ) @@ -830,10 +829,8 @@ const getLimitFromChargingProfiles = ( // Check if the charging profile is active if ( isWithinInterval(currentDate, { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - start: chargingSchedule.startSchedule!, - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - end: addSeconds(chargingSchedule.startSchedule!, chargingSchedule.duration!) + start: chargingSchedule.startSchedule, + end: addSeconds(chargingSchedule.startSchedule, chargingSchedule.duration) }) ) { if (isNotEmptyArray(chargingSchedule.chargingSchedulePeriod)) { @@ -877,8 +874,7 @@ const getLimitFromChargingProfiles = ( // Find the right schedule period if ( isAfter( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - addSeconds(chargingSchedule.startSchedule!, chargingSchedulePeriod.startPeriod), + addSeconds(chargingSchedule.startSchedule, chargingSchedulePeriod.startPeriod), currentDate ) ) { @@ -899,14 +895,11 @@ const getLimitFromChargingProfiles = ( (index < chargingSchedule.chargingSchedulePeriod.length - 1 && differenceInSeconds( addSeconds( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - chargingSchedule.startSchedule!, + chargingSchedule.startSchedule, chargingSchedule.chargingSchedulePeriod[index + 1].startPeriod ), - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - chargingSchedule.startSchedule! - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - ) > chargingSchedule.duration!) + chargingSchedule.startSchedule + ) > chargingSchedule.duration) ) { const result: ChargingProfilesLimit = { limit: previousChargingSchedulePeriod.limit, diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 0cd67647..e0785d43 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -712,10 +712,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { let previousCompositeSchedule: OCPP16ChargingSchedule | undefined let compositeSchedule: OCPP16ChargingSchedule | undefined for (const chargingProfile of chargingProfiles) { - if ( - chargingProfile.chargingSchedule.startSchedule == null && - connectorStatus.transactionStarted === true - ) { + if (chargingProfile.chargingSchedule.startSchedule == null) { logger.debug( `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${ chargingProfile.chargingProfileId @@ -724,10 +721,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { // OCPP specifies that if startSchedule is not defined, it should be relative to start of the connector transaction chargingProfile.chargingSchedule.startSchedule = connectorStatus.transactionStart } - if ( - chargingProfile.chargingSchedule.startSchedule != null && - !isDate(chargingProfile.chargingSchedule.startSchedule) - ) { + if (!isDate(chargingProfile.chargingSchedule.startSchedule)) { logger.warn( `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${ chargingProfile.chargingProfileId @@ -738,10 +732,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { chargingProfile.chargingSchedule.startSchedule )! } - if ( - chargingProfile.chargingSchedule.startSchedule != null && - chargingProfile.chargingSchedule.duration == null - ) { + if (chargingProfile.chargingSchedule.duration == null) { logger.debug( `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${ chargingProfile.chargingProfileId diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index db964227..570708b7 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -166,9 +166,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { } let cpReplaced = false if (isNotEmptyArray(chargingStation.getConnectorStatus(connectorId)?.chargingProfiles)) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion for (const [index, chargingProfile] of chargingStation - .getConnectorStatus(connectorId) - ?.chargingProfiles?.entries() ?? []) { + .getConnectorStatus(connectorId)! + .chargingProfiles!.entries()) { if ( chargingProfile.chargingProfileId === cp.chargingProfileId || (chargingProfile.stackLevel === cp.stackLevel && diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index cb802d12..51a9948e 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -20,7 +20,6 @@ import { type AuthorizeResponse, ChargePointErrorCode, ChargingStationEvents, - type ConnectorStatus, type ConnectorStatusEnum, CurrentType, ErrorType, @@ -123,7 +122,7 @@ export const isIdTagAuthorized = async ( } if (chargingStation.getLocalAuthListEnabled() && isIdTagLocalAuthorized(chargingStation, idTag)) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const connectorStatus: ConnectorStatus = chargingStation.getConnectorStatus(connectorId)! + const connectorStatus = chargingStation.getConnectorStatus(connectorId)! connectorStatus.localAuthorizeIdTag = idTag connectorStatus.idTagLocalAuthorized = true return true diff --git a/src/performance/PerformanceStatistics.ts b/src/performance/PerformanceStatistics.ts index 88ea748c..2f109606 100644 --- a/src/performance/PerformanceStatistics.ts +++ b/src/performance/PerformanceStatistics.ts @@ -6,6 +6,7 @@ import { parentPort } from 'node:worker_threads' import { secondsToMilliseconds } from 'date-fns' +import { BaseError } from '../exception/index.js' import { ConfigurationSection, type IncomingRequestCommand, @@ -47,9 +48,9 @@ export class PerformanceStatistics { private readonly statistics: Statistics private displayInterval?: NodeJS.Timeout - private constructor (objId: string | undefined, objName: string | undefined, uri: URL) { - this.objId = objId ?? 'Object id not specified' - this.objName = objName ?? 'Object name not specified' + private constructor (objId: string, objName: string, uri: URL) { + this.objId = objId + this.objName = objName this.initializePerformanceObserver() this.statistics = { id: this.objId, @@ -61,10 +62,26 @@ export class PerformanceStatistics { } public static getInstance ( - objId: string, - objName: string, - uri: URL + objId: string | undefined, + objName: string | undefined, + uri: URL | undefined ): PerformanceStatistics | undefined { + const logPfx = logPrefix(' Performance statistics') + if (objId == null) { + const errMsg = 'Cannot get performance statistics instance without specifying object id' + logger.error(`${logPfx} ${errMsg}`) + throw new BaseError(errMsg) + } + if (objName == null) { + const errMsg = 'Cannot get performance statistics instance without specifying object name' + logger.error(`${logPfx} ${errMsg}`) + throw new BaseError(errMsg) + } + if (uri == null) { + const errMsg = 'Cannot get performance statistics instance without specifying object uri' + logger.error(`${logPfx} ${errMsg}`) + throw new BaseError(errMsg) + } if (!PerformanceStatistics.instances.has(objId)) { PerformanceStatistics.instances.set(objId, new PerformanceStatistics(objId, objName, uri)) } -- 2.34.1