X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStation.ts;h=92fbb80c6e21bde391c74ffccdd52100a38bd26a;hb=48c7e1d6db4b3cbf82d17e191b2255066c462a71;hp=281ce2471df76192932f6dc0691efc655ddf6300;hpb=300418e9ba42d252a1a7079dab7904f40561a7ef;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 281ce247..92fbb80c 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -134,6 +134,7 @@ import { buildUpdatedMessage, cloneObject, convertToBoolean, + convertToDate, convertToInt, exponentialDelay, formatDurationMilliSeconds, @@ -156,7 +157,7 @@ import { export class ChargingStation extends EventEmitter { public readonly index: number public readonly templateFile: string - public stationInfo!: ChargingStationInfo + public stationInfo?: ChargingStationInfo public started: boolean public starting: boolean public idTagsCache: IdTagsCache @@ -169,9 +170,9 @@ export class ChargingStation extends EventEmitter { public performanceStatistics!: PerformanceStatistics | undefined public heartbeatSetInterval?: NodeJS.Timeout public ocppRequestService!: OCPPRequestService - public bootNotificationRequest!: BootNotificationRequest - public bootNotificationResponse!: BootNotificationResponse | undefined - public powerDivider!: number + public bootNotificationRequest?: BootNotificationRequest + public bootNotificationResponse?: BootNotificationResponse + public powerDivider?: number private stopping: boolean private configurationFile!: string private configurationFileHash!: string @@ -227,19 +228,23 @@ export class ChargingStation extends EventEmitter { return new URL( `${ this.stationInfo?.supervisionUrlOcppConfiguration === true && - isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) && + isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) && // eslint-disable-next-line @typescript-eslint/no-non-null-assertion isNotEmptyString(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!)?.value) ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!)!.value : this.configuredSupervisionUrl.href - }/${this.stationInfo.chargingStationId}` + }/${this.stationInfo?.chargingStationId}` ) } public logPrefix = (): string => { - if (isNotEmptyString(this?.stationInfo?.chargingStationId)) { - return logPrefix(` ${this?.stationInfo?.chargingStationId} |`) + if ( + this instanceof ChargingStation && + this.stationInfo != null && + isNotEmptyString(this.stationInfo.chargingStationId) + ) { + return logPrefix(` ${this.stationInfo.chargingStationId} |`) } let stationTemplate: ChargingStationTemplate | undefined try { @@ -254,11 +259,12 @@ export class ChargingStation extends EventEmitter { public hasIdTags (): boolean { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - return isNotEmptyArray(this.idTagsCache.getIdTags(getIdTagsFile(this.stationInfo)!)) + return isNotEmptyArray(this.idTagsCache.getIdTags(getIdTagsFile(this.stationInfo!)!)) } public getNumberOfPhases (stationInfo?: ChargingStationInfo): number { - const localStationInfo: ChargingStationInfo = stationInfo ?? this.stationInfo + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const localStationInfo = stationInfo ?? this.stationInfo! switch (this.getCurrentOutType(stationInfo)) { case CurrentType.AC: return localStationInfo.numberOfPhases ?? 3 @@ -268,23 +274,23 @@ export class ChargingStation extends EventEmitter { } public isWebSocketConnectionOpened (): boolean { - return this?.wsConnection?.readyState === WebSocket.OPEN + return this.wsConnection?.readyState === WebSocket.OPEN } public inUnknownState (): boolean { - return this?.bootNotificationResponse?.status == null + return this.bootNotificationResponse?.status == null } public inPendingState (): boolean { - return this?.bootNotificationResponse?.status === RegistrationStatusEnumType.PENDING + return this.bootNotificationResponse?.status === RegistrationStatusEnumType.PENDING } public inAcceptedState (): boolean { - return this?.bootNotificationResponse?.status === RegistrationStatusEnumType.ACCEPTED + return this.bootNotificationResponse?.status === RegistrationStatusEnumType.ACCEPTED } public inRejectedState (): boolean { - return this?.bootNotificationResponse?.status === RegistrationStatusEnumType.REJECTED + return this.bootNotificationResponse?.status === RegistrationStatusEnumType.REJECTED } public isRegistered (): boolean { @@ -345,10 +351,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 @@ -356,16 +363,16 @@ 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()!)) / - this.powerDivider + DCElectricUtils.power(this.stationInfo!.voltageOut!, amperageLimitation)) / + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.powerDivider! } // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const connectorMaximumPower = this.stationInfo.maximumPower! / this.powerDivider + const connectorMaximumPower = this.stationInfo!.maximumPower! / this.powerDivider! const connectorChargingProfilesPowerLimit = getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId) return min( @@ -421,8 +428,10 @@ export class ChargingStation extends EventEmitter { return numberOfRunningTransactions } - public getConnectorIdByTransactionId (transactionId: number): number | undefined { - if (this.hasEvses) { + public getConnectorIdByTransactionId (transactionId: number | undefined): number | undefined { + if (transactionId == null) { + return undefined + } else if (this.hasEvses) { for (const evseStatus of this.evses.values()) { for (const [connectorId, connectorStatus] of evseStatus.connectors) { if (connectorStatus.transactionId === transactionId) { @@ -440,19 +449,18 @@ export class ChargingStation extends EventEmitter { } public getEnergyActiveImportRegisterByTransactionId ( - transactionId: number, + transactionId: number | undefined, rounded = false ): number { return this.getEnergyActiveImportRegister( // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId)!)!, + this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId)!), rounded ) } public getEnergyActiveImportRegisterByConnectorId (connectorId: number, rounded = false): number { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId)!, rounded) + return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId), rounded) } public getAuthorizeRemoteTxRequests (): boolean { @@ -494,12 +502,13 @@ export class ChargingStation extends EventEmitter { public setSupervisionUrl (url: string): void { if ( this.stationInfo?.supervisionUrlOcppConfiguration === true && - isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) + isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) ) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion setConfigurationKeyValue(this, this.stationInfo.supervisionUrlOcppKey!, url) } else { - this.stationInfo.supervisionUrls = url + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.stationInfo!.supervisionUrls = url this.saveStationInfo() this.configuredSupervisionUrl = this.getConfiguredSupervisionUrl() } @@ -510,7 +519,7 @@ export class ChargingStation extends EventEmitter { this.heartbeatSetInterval = setInterval(() => { this.ocppRequestService .requestHandler(this, RequestCommand.HEARTBEAT) - .catch((error) => { + .catch(error => { logger.error( `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`, error @@ -554,21 +563,22 @@ export class ChargingStation extends EventEmitter { logger.error(`${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}`) return } - if (this.getConnectorStatus(connectorId) == null) { + const connectorStatus = this.getConnectorStatus(connectorId) + if (connectorStatus == null) { logger.error( `${this.logPrefix()} Trying to start MeterValues on non existing connector id ${connectorId}` ) return } - if (this.getConnectorStatus(connectorId)?.transactionStarted === false) { + if (connectorStatus.transactionStarted === false) { logger.error( `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started` ) return } else if ( - this.getConnectorStatus(connectorId)?.transactionStarted === true && - this.getConnectorStatus(connectorId)?.transactionId == null + connectorStatus.transactionStarted === true && + connectorStatus.transactionId == null ) { logger.error( `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id` @@ -576,13 +586,12 @@ export class ChargingStation extends EventEmitter { return } if (interval > 0) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.getConnectorStatus(connectorId)!.transactionSetInterval = setInterval(() => { + connectorStatus.transactionSetInterval = setInterval(() => { const meterValue = buildMeterValue( this, connectorId, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.getConnectorStatus(connectorId)!.transactionId!, + connectorStatus.transactionId!, interval ) this.ocppRequestService @@ -591,11 +600,11 @@ export class ChargingStation extends EventEmitter { RequestCommand.METER_VALUES, { connectorId, - transactionId: this.getConnectorStatus(connectorId)?.transactionId, + transactionId: connectorStatus.transactionId, meterValue: [meterValue] } ) - .catch((error) => { + .catch(error => { logger.error( `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`, error @@ -612,8 +621,9 @@ export class ChargingStation extends EventEmitter { } public stopMeterValues (connectorId: number): void { - if (this.getConnectorStatus(connectorId)?.transactionSetInterval != null) { - clearInterval(this.getConnectorStatus(connectorId)?.transactionSetInterval) + const connectorStatus = this.getConnectorStatus(connectorId) + if (connectorStatus?.transactionSetInterval != null) { + clearInterval(connectorStatus.transactionSetInterval) } } @@ -643,11 +653,11 @@ export class ChargingStation extends EventEmitter { // Initialize this.initialize() // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.idTagsCache.deleteIdTags(getIdTagsFile(this.stationInfo)!) + this.idTagsCache.deleteIdTags(getIdTagsFile(this.stationInfo!)!) // Restart the ATG this.stopAutomaticTransactionGenerator() delete this.automaticTransactionGeneratorConfiguration - if (this.getAutomaticTransactionGeneratorConfiguration().enable) { + if (this.getAutomaticTransactionGeneratorConfiguration()?.enable === true) { this.startAutomaticTransactionGenerator() } if (this.stationInfo?.enableStatistics === true) { @@ -704,7 +714,7 @@ export class ChargingStation extends EventEmitter { public async reset (reason?: StopTransactionReason): Promise { await this.stop(reason) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - await sleep(this.stationInfo.resetTime!) + await sleep(this.stationInfo!.resetTime!) this.initialize() this.start() } @@ -733,13 +743,13 @@ export class ChargingStation extends EventEmitter { if (!checkChargingStation(this, this.logPrefix())) { return } - if (this.stationInfo.supervisionUser != null && this.stationInfo.supervisionPassword != null) { + if (this.stationInfo?.supervisionUser != null && this.stationInfo.supervisionPassword != null) { options.auth = `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}` } - if (params?.closeOpened === true) { + if (params.closeOpened === true) { this.closeWSConnection() } - if (params?.terminateOpened === true) { + if (params.terminateOpened === true) { this.terminateWSConnection() } @@ -790,7 +800,9 @@ export class ChargingStation extends EventEmitter { } } - public getAutomaticTransactionGeneratorConfiguration (): AutomaticTransactionGeneratorConfiguration { + public getAutomaticTransactionGeneratorConfiguration (): + | AutomaticTransactionGeneratorConfiguration + | undefined { if (this.automaticTransactionGeneratorConfiguration == null) { let automaticTransactionGeneratorConfiguration: | AutomaticTransactionGeneratorConfiguration @@ -803,7 +815,7 @@ export class ChargingStation extends EventEmitter { stationConfiguration?.automaticTransactionGenerator != null ) { automaticTransactionGeneratorConfiguration = - stationConfiguration?.automaticTransactionGenerator + stationConfiguration.automaticTransactionGenerator } else { automaticTransactionGeneratorConfiguration = stationTemplate?.AutomaticTransactionGenerator } @@ -853,14 +865,13 @@ export class ChargingStation extends EventEmitter { const transactionId = this.getConnectorStatus(connectorId)?.transactionId if ( this.stationInfo?.beginEndMeterValues === true && - this.stationInfo?.ocppStrictCompliance === true && - this.stationInfo?.outOfOrderEndMeterValues === false + this.stationInfo.ocppStrictCompliance === true && + this.stationInfo.outOfOrderEndMeterValues === false ) { 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, @@ -877,8 +888,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 }) }) } @@ -942,14 +952,14 @@ export class ChargingStation extends EventEmitter { if (this.hasEvses) { for (const evseStatus of this.evses.values()) { for (const connectorStatus of evseStatus.connectors.values()) { - if (connectorStatus?.reservation?.[filterKey] === value) { + if (connectorStatus.reservation?.[filterKey] === value) { return connectorStatus.reservation } } } } else { for (const connectorStatus of this.connectors.values()) { - if (connectorStatus?.reservation?.[filterKey] === value) { + if (connectorStatus.reservation?.[filterKey] === value) { return connectorStatus.reservation } } @@ -1084,36 +1094,36 @@ export class ChargingStation extends EventEmitter { private getStationInfoFromTemplate (): ChargingStationInfo { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const stationTemplate: ChargingStationTemplate = this.getTemplateFromFile()! + const stationTemplate = this.getTemplateFromFile()! checkTemplate(stationTemplate, this.logPrefix(), this.templateFile) const warnTemplateKeysDeprecationOnce = once(warnTemplateKeysDeprecation, this) warnTemplateKeysDeprecationOnce(stationTemplate, this.logPrefix(), this.templateFile) - if (stationTemplate?.Connectors != null) { + if (stationTemplate.Connectors != null) { checkConnectorsConfiguration(stationTemplate, this.logPrefix(), this.templateFile) } - const stationInfo: ChargingStationInfo = stationTemplateToStationInfo(stationTemplate) + const stationInfo = stationTemplateToStationInfo(stationTemplate) stationInfo.hashId = getHashId(this.index, stationTemplate) stationInfo.chargingStationId = getChargingStationId(this.index, stationTemplate) - stationInfo.ocppVersion = stationTemplate?.ocppVersion ?? OCPPVersion.VERSION_16 + stationInfo.ocppVersion = stationTemplate.ocppVersion ?? OCPPVersion.VERSION_16 createSerialNumber(stationTemplate, stationInfo) stationInfo.voltageOut = this.getVoltageOut(stationInfo) - if (isNotEmptyArray(stationTemplate?.power)) { + if (isNotEmptyArray(stationTemplate.power)) { stationTemplate.power = stationTemplate.power as number[] const powerArrayRandomIndex = Math.floor(secureRandom() * stationTemplate.power.length) stationInfo.maximumPower = - stationTemplate?.powerUnit === PowerUnits.KILO_WATT + stationTemplate.powerUnit === PowerUnits.KILO_WATT ? stationTemplate.power[powerArrayRandomIndex] * 1000 : stationTemplate.power[powerArrayRandomIndex] } else { - stationTemplate.power = stationTemplate?.power as number + stationTemplate.power = stationTemplate.power as number stationInfo.maximumPower = - stationTemplate?.powerUnit === PowerUnits.KILO_WATT + stationTemplate.powerUnit === PowerUnits.KILO_WATT ? stationTemplate.power * 1000 : stationTemplate.power } stationInfo.maximumAmperage = this.getMaximumAmperage(stationInfo) stationInfo.firmwareVersionPattern = - stationTemplate?.firmwareVersionPattern ?? Constants.SEMVER_PATTERN + stationTemplate.firmwareVersionPattern ?? Constants.SEMVER_PATTERN if ( isNotEmptyString(stationInfo.firmwareVersion) && // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -1132,10 +1142,10 @@ export class ChargingStation extends EventEmitter { }, reset: true }, - stationTemplate?.firmwareUpgrade ?? {} + stationTemplate.firmwareUpgrade ?? {} ) stationInfo.resetTime = - stationTemplate?.resetTime != null + stationTemplate.resetTime != null ? secondsToMilliseconds(stationTemplate.resetTime) : Constants.CHARGING_STATION_DEFAULT_RESET_TIME return stationInfo @@ -1148,7 +1158,7 @@ export class ChargingStation extends EventEmitter { if (stationInfoPersistentConfiguration) { stationInfo = this.getConfigurationFromFile()?.stationInfo if (stationInfo != null) { - delete stationInfo?.infoHash + delete stationInfo.infoHash } } return stationInfo @@ -1156,21 +1166,22 @@ export class ChargingStation extends EventEmitter { private getStationInfo (): ChargingStationInfo { const defaultStationInfo = Constants.DEFAULT_STATION_INFO - const stationInfoFromTemplate: ChargingStationInfo = this.getStationInfoFromTemplate() - const stationInfoFromFile: ChargingStationInfo | undefined = this.getStationInfoFromFile( - stationInfoFromTemplate?.stationInfoPersistentConfiguration + const stationInfoFromTemplate = this.getStationInfoFromTemplate() + const stationInfoFromFile = this.getStationInfoFromFile( + stationInfoFromTemplate.stationInfoPersistentConfiguration ) // 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( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.getTemplateFromFile()!, + this.getTemplateFromFile(), stationInfoFromFile, stationInfoFromTemplate ) @@ -1199,8 +1210,7 @@ export class ChargingStation extends EventEmitter { ) const stationConfiguration = this.getConfigurationFromFile() if ( - stationConfiguration?.stationInfo?.templateHash === stationTemplate?.templateHash && - // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + stationConfiguration?.stationInfo?.templateHash === stationTemplate.templateHash && (stationConfiguration?.connectorsStatus != null || stationConfiguration?.evsesStatus != null) ) { checkConfiguration(stationConfiguration, this.logPrefix(), this.configurationFile) @@ -1214,7 +1224,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 @@ -1235,26 +1245,31 @@ export class ChargingStation extends EventEmitter { } this.saveStationInfo() this.configuredSupervisionUrl = this.getConfiguredSupervisionUrl() - if (this.stationInfo?.enableStatistics === true) { + 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() this.initializeOcppConfiguration() this.initializeOcppServices() this.once(ChargingStationEvents.accepted, () => { - this.startMessageSequence().catch((error) => { + this.startMessageSequence().catch(error => { logger.error(`${this.logPrefix()} Error while starting the message sequence:`, error) }) }) - if (this.stationInfo?.autoRegister === true) { + if (this.stationInfo.autoRegister === true) { this.bootNotificationResponse = { currentTime: new Date(), interval: millisecondsToSeconds(this.getHeartbeatInterval()), @@ -1296,7 +1311,7 @@ export class ChargingStation extends EventEmitter { } if ( this.stationInfo?.supervisionUrlOcppConfiguration === true && - isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) && + isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) && // eslint-disable-next-line @typescript-eslint/no-non-null-assertion getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!) == null ) { @@ -1309,7 +1324,7 @@ export class ChargingStation extends EventEmitter { ) } else if ( this.stationInfo?.supervisionUrlOcppConfiguration === false && - isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) && + isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) && // eslint-disable-next-line @typescript-eslint/no-non-null-assertion getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!) != null ) { @@ -1324,10 +1339,10 @@ export class ChargingStation extends EventEmitter { addConfigurationKey( this, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.stationInfo.amperageLimitationOcppKey!, + this.stationInfo!.amperageLimitationOcppKey!, // prettier-ignore // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - (this.stationInfo.maximumAmperage! * getAmperageLimitationUnitDivider(this.stationInfo)).toString() + (this.stationInfo!.maximumAmperage! * getAmperageLimitationUnitDivider(this.stationInfo)).toString() ) } if (getConfigurationKey(this, StandardParametersKey.SupportedFeatureProfiles) == null) { @@ -1396,11 +1411,11 @@ export class ChargingStation extends EventEmitter { } private initializeConnectorsOrEvsesFromFile (configuration: ChargingStationConfiguration): void { - if (configuration?.connectorsStatus != null && configuration?.evsesStatus == null) { + if (configuration.connectorsStatus != null && configuration.evsesStatus == null) { for (const [connectorId, connectorStatus] of configuration.connectorsStatus.entries()) { this.connectors.set(connectorId, cloneObject(connectorStatus)) } - } else if (configuration?.evsesStatus != null && configuration?.connectorsStatus == null) { + } else if (configuration.evsesStatus != null && configuration.connectorsStatus == null) { for (const [evseId, evseStatusConfiguration] of configuration.evsesStatus.entries()) { const evseStatus = cloneObject(evseStatusConfiguration) delete evseStatus.connectorsStatus @@ -1415,7 +1430,7 @@ export class ChargingStation extends EventEmitter { ) }) } - } else if (configuration?.evsesStatus != null && configuration?.connectorsStatus != null) { + } else if (configuration.evsesStatus != null && configuration.connectorsStatus != null) { const errorMsg = `Connectors and evses defined at the same time in configuration file ${this.configurationFile}` logger.error(`${this.logPrefix()} ${errorMsg}`) throw new BaseError(errorMsg) @@ -1427,11 +1442,11 @@ export class ChargingStation extends EventEmitter { } private initializeConnectorsOrEvsesFromTemplate (stationTemplate: ChargingStationTemplate): void { - if (stationTemplate?.Connectors != null && stationTemplate?.Evses == null) { + if (stationTemplate.Connectors != null && stationTemplate.Evses == null) { this.initializeConnectorsFromTemplate(stationTemplate) - } else if (stationTemplate?.Evses != null && stationTemplate?.Connectors == null) { + } else if (stationTemplate.Evses != null && stationTemplate.Connectors == null) { this.initializeEvsesFromTemplate(stationTemplate) - } else if (stationTemplate?.Evses != null && stationTemplate?.Connectors != null) { + } else if (stationTemplate.Evses != null && stationTemplate.Connectors != null) { const errorMsg = `Connectors and evses defined at the same time in template file ${this.templateFile}` logger.error(`${this.logPrefix()} ${errorMsg}`) throw new BaseError(errorMsg) @@ -1443,45 +1458,46 @@ export class ChargingStation extends EventEmitter { } private initializeConnectorsFromTemplate (stationTemplate: ChargingStationTemplate): void { - if (stationTemplate?.Connectors == null && this.connectors.size === 0) { + if (stationTemplate.Connectors == null && this.connectors.size === 0) { const errorMsg = `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined` logger.error(`${this.logPrefix()} ${errorMsg}`) throw new BaseError(errorMsg) } - if (stationTemplate?.Connectors?.[0] == null) { + if (stationTemplate.Connectors?.[0] == null) { logger.warn( `${this.logPrefix()} Charging station information from template ${ this.templateFile } with no connector id 0 configuration` ) } - if (stationTemplate?.Connectors != null) { + if (stationTemplate.Connectors != null) { const { configuredMaxConnectors, templateMaxConnectors, templateMaxAvailableConnectors } = checkConnectorsConfiguration(stationTemplate, this.logPrefix(), this.templateFile) const connectorsConfigHash = createHash(Constants.DEFAULT_HASH_ALGORITHM) .update( - `${JSON.stringify(stationTemplate?.Connectors)}${configuredMaxConnectors.toString()}` + `${JSON.stringify(stationTemplate.Connectors)}${configuredMaxConnectors.toString()}` ) .digest('hex') const connectorsConfigChanged = - this.connectors?.size !== 0 && this.connectorsConfigurationHash !== connectorsConfigHash - if (this.connectors?.size === 0 || connectorsConfigChanged) { + this.connectors.size !== 0 && this.connectorsConfigurationHash !== connectorsConfigHash + if (this.connectors.size === 0 || connectorsConfigChanged) { connectorsConfigChanged && this.connectors.clear() this.connectorsConfigurationHash = connectorsConfigHash if (templateMaxConnectors > 0) { for (let connectorId = 0; connectorId <= configuredMaxConnectors; connectorId++) { if ( connectorId === 0 && - (stationTemplate?.Connectors?.[connectorId] == null || + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + (stationTemplate.Connectors[connectorId] == null || !this.getUseConnectorId0(stationTemplate)) ) { continue } const templateConnectorId = - connectorId > 0 && stationTemplate?.randomConnectors === true + connectorId > 0 && stationTemplate.randomConnectors === true ? getRandomInteger(templateMaxAvailableConnectors, 1) : connectorId - const connectorStatus = stationTemplate?.Connectors[templateConnectorId] + const connectorStatus = stationTemplate.Connectors[templateConnectorId] checkStationInfoConnectorStatus( templateConnectorId, connectorStatus, @@ -1510,48 +1526,48 @@ export class ChargingStation extends EventEmitter { } private initializeEvsesFromTemplate (stationTemplate: ChargingStationTemplate): void { - if (stationTemplate?.Evses == null && this.evses.size === 0) { + if (stationTemplate.Evses == null && this.evses.size === 0) { const errorMsg = `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined` logger.error(`${this.logPrefix()} ${errorMsg}`) throw new BaseError(errorMsg) } - if (stationTemplate?.Evses?.[0] == null) { + if (stationTemplate.Evses?.[0] == null) { logger.warn( `${this.logPrefix()} Charging station information from template ${ this.templateFile } with no evse id 0 configuration` ) } - if (stationTemplate?.Evses?.[0]?.Connectors?.[0] == null) { + if (stationTemplate.Evses?.[0]?.Connectors[0] == null) { logger.warn( `${this.logPrefix()} Charging station information from template ${ this.templateFile } with evse id 0 with no connector id 0 configuration` ) } - if (Object.keys(stationTemplate?.Evses?.[0]?.Connectors as object).length > 1) { + if (Object.keys(stationTemplate.Evses?.[0]?.Connectors as object).length > 1) { logger.warn( `${this.logPrefix()} Charging station information from template ${ this.templateFile } with evse id 0 with more than one connector configuration, only connector id 0 configuration will be used` ) } - if (stationTemplate?.Evses != null) { + if (stationTemplate.Evses != null) { const evsesConfigHash = createHash(Constants.DEFAULT_HASH_ALGORITHM) - .update(JSON.stringify(stationTemplate?.Evses)) + .update(JSON.stringify(stationTemplate.Evses)) .digest('hex') const evsesConfigChanged = - this.evses?.size !== 0 && this.evsesConfigurationHash !== evsesConfigHash - if (this.evses?.size === 0 || evsesConfigChanged) { + this.evses.size !== 0 && this.evsesConfigurationHash !== evsesConfigHash + if (this.evses.size === 0 || evsesConfigChanged) { evsesConfigChanged && this.evses.clear() this.evsesConfigurationHash = evsesConfigHash - const templateMaxEvses = getMaxNumberOfEvses(stationTemplate?.Evses) + const templateMaxEvses = getMaxNumberOfEvses(stationTemplate.Evses) if (templateMaxEvses > 0) { for (const evseKey in stationTemplate.Evses) { const evseId = convertToInt(evseKey) this.evses.set(evseId, { connectors: buildConnectorsMap( - stationTemplate?.Evses[evseKey]?.Connectors, + stationTemplate.Evses[evseKey].Connectors, this.logPrefix(), this.templateFile ), @@ -1629,15 +1645,12 @@ 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 && - this.stationInfo != null - ) { + if (this.stationInfo?.stationInfoPersistentConfiguration === true) { configurationData.stationInfo = this.stationInfo } else { delete configurationData.stationInfo @@ -1646,7 +1659,7 @@ export class ChargingStation extends EventEmitter { this.stationInfo?.ocppPersistentConfiguration === true && Array.isArray(this.ocppConfiguration?.configurationKey) ) { - configurationData.configurationKey = this.ocppConfiguration?.configurationKey + configurationData.configurationKey = this.ocppConfiguration.configurationKey } else { delete configurationData.configurationKey } @@ -1698,7 +1711,7 @@ export class ChargingStation extends EventEmitter { this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash) this.sharedLRUCache.setChargingStationConfiguration(configurationData) this.configurationFileHash = configurationHash - }).catch((error) => { + }).catch(error => { handleFileException( this.configurationFile, FileType.ChargingStationConfiguration, @@ -1764,10 +1777,15 @@ export class ChargingStation extends EventEmitter { >(this, RequestCommand.BOOT_NOTIFICATION, this.bootNotificationRequest, { skipBufferingOnError: true }) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.bootNotificationResponse.currentTime = convertToDate( + this.bootNotificationResponse.currentTime + )! if (!this.isRegistered()) { this.stationInfo?.registrationMaxRetries !== -1 && ++registrationRetryCount await sleep( - this?.bootNotificationResponse?.interval != null + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + this.bootNotificationResponse?.interval != null ? secondsToMilliseconds(this.bootNotificationResponse.interval) : Constants.DEFAULT_BOOT_NOTIFICATION_INTERVAL ) @@ -1775,7 +1793,7 @@ export class ChargingStation extends EventEmitter { } while ( !this.isRegistered() && // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - (registrationRetryCount <= this.stationInfo.registrationMaxRetries! || + (registrationRetryCount <= this.stationInfo!.registrationMaxRetries! || this.stationInfo?.registrationMaxRetries === -1) ) } @@ -1786,8 +1804,9 @@ export class ChargingStation extends EventEmitter { } } else { logger.error( - `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${this - .stationInfo?.registrationMaxRetries})` + `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${ + this.stationInfo?.registrationMaxRetries + })` ) } this.autoReconnectRetryCount = 0 @@ -1877,9 +1896,9 @@ export class ChargingStation extends EventEmitter { messageId )! logger.debug( - `${this.logPrefix()} << Command '${ - requestCommandName ?? Constants.UNKNOWN_COMMAND - }' received response payload: ${JSON.stringify(response)}` + `${this.logPrefix()} << Command '${requestCommandName}' received response payload: ${JSON.stringify( + response + )}` ) responseCallback(commandPayload, requestPayload) } @@ -1898,9 +1917,9 @@ export class ChargingStation extends EventEmitter { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const [, errorCallback, requestCommandName] = this.getCachedRequest(messageType, messageId)! logger.debug( - `${this.logPrefix()} << Command '${ - requestCommandName ?? Constants.UNKNOWN_COMMAND - }' received error response payload: ${JSON.stringify(errorResponse)}` + `${this.logPrefix()} << Command '${requestCommandName}' received error response payload: ${JSON.stringify( + errorResponse + )}` ) errorCallback(new OCPPError(errorType, errorMessage, requestCommandName, errorDetails)) } @@ -2006,19 +2025,24 @@ export class ChargingStation extends EventEmitter { logger.error(`${this.logPrefix()} WebSocket error:`, error) } - private getEnergyActiveImportRegister (connectorStatus: ConnectorStatus, rounded = false): number { + private getEnergyActiveImportRegister ( + connectorStatus: ConnectorStatus | undefined, + rounded = false + ): number { if (this.stationInfo?.meteringPerTransaction === true) { return ( (rounded - ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - Math.round(connectorStatus.transactionEnergyActiveImportRegisterValue!) + ? connectorStatus?.transactionEnergyActiveImportRegisterValue != null + ? Math.round(connectorStatus.transactionEnergyActiveImportRegisterValue) + : undefined : connectorStatus?.transactionEnergyActiveImportRegisterValue) ?? 0 ) } return ( (rounded - ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - Math.round(connectorStatus.energyActiveImportRegisterValue!) + ? connectorStatus?.energyActiveImportRegisterValue != null + ? Math.round(connectorStatus.energyActiveImportRegisterValue) + : undefined : connectorStatus?.energyActiveImportRegisterValue) ?? 0 ) } @@ -2052,8 +2076,7 @@ export class ChargingStation extends EventEmitter { private getConnectionTimeout (): number { if (getConfigurationKey(this, StandardParametersKey.ConnectionTimeOut) != null) { return convertToInt( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - getConfigurationKey(this, StandardParametersKey.ConnectionTimeOut)!.value! ?? + getConfigurationKey(this, StandardParametersKey.ConnectionTimeOut)?.value ?? Constants.DEFAULT_CONNECTION_TIMEOUT ) } @@ -2070,7 +2093,7 @@ export class ChargingStation extends EventEmitter { private getMaximumAmperage (stationInfo?: ChargingStationInfo): number | undefined { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const maximumPower = (stationInfo ?? this.stationInfo).maximumPower! + const maximumPower = (stationInfo ?? this.stationInfo!).maximumPower! switch (this.getCurrentOutType(stationInfo)) { case CurrentType.AC: return ACElectricUtils.amperagePerPhaseFromPower( @@ -2084,12 +2107,14 @@ export class ChargingStation extends EventEmitter { } private getCurrentOutType (stationInfo?: ChargingStationInfo): CurrentType { - return (stationInfo ?? this.stationInfo).currentOutType ?? CurrentType.AC + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return (stationInfo ?? this.stationInfo!).currentOutType ?? CurrentType.AC } private getVoltageOut (stationInfo?: ChargingStationInfo): Voltage { return ( - (stationInfo ?? this.stationInfo).voltageOut ?? + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + (stationInfo ?? this.stationInfo!).voltageOut ?? getDefaultVoltageOut(this.getCurrentOutType(stationInfo), this.logPrefix(), this.templateFile) ) } @@ -2098,13 +2123,14 @@ 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 ) { return ( convertToInt( // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - getConfigurationKey(this, this.stationInfo.amperageLimitationOcppKey!)?.value - ) / getAmperageLimitationUnitDivider(this.stationInfo) + getConfigurationKey(this, this.stationInfo!.amperageLimitationOcppKey!)!.value + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + ) / getAmperageLimitationUnitDivider(this.stationInfo!) ) } } @@ -2145,7 +2171,7 @@ export class ChargingStation extends EventEmitter { } } } - if (this.stationInfo.firmwareStatus === FirmwareStatus.Installing) { + if (this.stationInfo?.firmwareStatus === FirmwareStatus.Installing) { await this.ocppRequestService.requestHandler< FirmwareStatusNotificationRequest, FirmwareStatusNotificationResponse @@ -2156,7 +2182,7 @@ export class ChargingStation extends EventEmitter { } // Start the ATG - if (this.getAutomaticTransactionGeneratorConfiguration().enable) { + if (this.getAutomaticTransactionGeneratorConfiguration()?.enable === true) { this.startAutomaticTransactionGenerator() } this.flushMessageBuffer() @@ -2193,7 +2219,7 @@ export class ChargingStation extends EventEmitter { evseId ) ) - delete connectorStatus?.status + delete connectorStatus.status } } } @@ -2215,7 +2241,7 @@ export class ChargingStation extends EventEmitter { } private startWebSocketPing (): void { - const webSocketPingInterval: number = + const webSocketPingInterval = getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval) != null ? convertToInt( getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval)?.value @@ -2288,7 +2314,7 @@ export class ChargingStation extends EventEmitter { } const errorMsg = 'No supervision url(s) configured' logger.error(`${this.logPrefix()} ${errorMsg}`) - throw new BaseError(`${errorMsg}`) + throw new BaseError(errorMsg) } private stopHeartbeat (): void { @@ -2311,12 +2337,12 @@ export class ChargingStation extends EventEmitter { // Stop heartbeat this.stopHeartbeat() // Stop the ATG if needed - if (this.getAutomaticTransactionGeneratorConfiguration().stopOnConnectionFailure) { + if (this.getAutomaticTransactionGeneratorConfiguration()?.stopOnConnectionFailure === true) { this.stopAutomaticTransactionGenerator() } if ( // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.autoReconnectRetryCount < this.stationInfo.autoReconnectMaxRetries! || + this.autoReconnectRetryCount < this.stationInfo!.autoReconnectMaxRetries! || this.stationInfo?.autoReconnectMaxRetries === -1 ) { ++this.autoReconnectRetryCount @@ -2326,9 +2352,7 @@ export class ChargingStation extends EventEmitter { : secondsToMilliseconds(this.getConnectionTimeout()) const reconnectDelayWithdraw = 1000 const reconnectTimeout = - reconnectDelay != null && reconnectDelay - reconnectDelayWithdraw > 0 - ? reconnectDelay - reconnectDelayWithdraw - : 0 + reconnectDelay - reconnectDelayWithdraw > 0 ? reconnectDelay - reconnectDelayWithdraw : 0 logger.error( `${this.logPrefix()} WebSocket connection retry in ${roundTo( reconnectDelay,