-// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
+// Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
import { createHash } from 'node:crypto'
import { EventEmitter } from 'node:events'
buildStartedMessage,
buildStoppedMessage,
buildUpdatedMessage,
- cloneObject,
+ clone,
convertToBoolean,
+ convertToDate,
convertToInt,
exponentialDelay,
formatDurationMilliSeconds,
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
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
return new URL(
`${
this.stationInfo?.supervisionUrlOcppConfiguration === true &&
- 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
+ isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) &&
+ isNotEmptyString(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value)
+ ? 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 {
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
}
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 {
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
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(
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) {
}
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 {
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)
+ 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()
}
this.heartbeatSetInterval = setInterval(() => {
this.ocppRequestService
.requestHandler<HeartbeatRequest, HeartbeatResponse>(this, RequestCommand.HEARTBEAT)
- .catch((error) => {
+ .catch(error => {
logger.error(
`${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
error
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`
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
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
}
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)
}
}
// 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) {
public async reset (reason?: StopTransactionReason): Promise<void> {
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()
}
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()
}
}
}
- public getAutomaticTransactionGeneratorConfiguration (): AutomaticTransactionGeneratorConfiguration {
+ public getAutomaticTransactionGeneratorConfiguration ():
+ | AutomaticTransactionGeneratorConfiguration
+ | undefined {
if (this.automaticTransactionGeneratorConfiguration == null) {
let automaticTransactionGeneratorConfiguration:
| AutomaticTransactionGeneratorConfiguration
stationConfiguration?.automaticTransactionGenerator != null
) {
automaticTransactionGeneratorConfiguration =
- stationConfiguration?.automaticTransactionGenerator
+ stationConfiguration.automaticTransactionGenerator
} else {
automaticTransactionGeneratorConfiguration = stationTemplate?.AutomaticTransactionGenerator
}
public startAutomaticTransactionGenerator (connectorIds?: number[]): void {
this.automaticTransactionGenerator = AutomaticTransactionGenerator.getInstance(this)
if (isNotEmptyArray(connectorIds)) {
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- for (const connectorId of connectorIds!) {
+ for (const connectorId of connectorIds) {
this.automaticTransactionGenerator?.startConnector(connectorId)
}
} else {
public stopAutomaticTransactionGenerator (connectorIds?: number[]): void {
if (isNotEmptyArray(connectorIds)) {
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- for (const connectorId of connectorIds!) {
+ for (const connectorId of connectorIds) {
this.automaticTransactionGenerator?.stopConnector(connectorId)
}
} else {
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<MeterValuesRequest, MeterValuesResponse>(
this,
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 })
})
}
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
}
}
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)) {
- stationTemplate.power = stationTemplate.power as number[]
+ if (isNotEmptyArray(stationTemplate.power)) {
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
stationInfo.maximumPower =
- stationTemplate?.powerUnit === PowerUnits.KILO_WATT
- ? stationTemplate.power * 1000
+ stationTemplate.powerUnit === PowerUnits.KILO_WATT
+ ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ 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
- !new RegExp(stationInfo.firmwareVersionPattern).test(stationInfo.firmwareVersion!)
+ !new RegExp(stationInfo.firmwareVersionPattern).test(stationInfo.firmwareVersion)
) {
logger.warn(
`${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
},
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
if (stationInfoPersistentConfiguration) {
stationInfo = this.getConfigurationFromFile()?.stationInfo
if (stationInfo != null) {
- delete stationInfo?.infoHash
+ delete stationInfo.infoHash
}
}
return stationInfo
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
)
)
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)
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
- const match = new RegExp(this.stationInfo.firmwareVersionPattern!)
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- .exec(this.stationInfo.firmwareVersion!)
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- ?.slice(1, patternGroup! + 1)
+ this.stationInfo.firmwareVersion.split('.').length
+ const match = new RegExp(this.stationInfo.firmwareVersionPattern)
+ .exec(this.stationInfo.firmwareVersion)
+ ?.slice(1, patternGroup + 1)
if (match != null) {
const patchLevelIndex = match.length - 1
match[patchLevelIndex] = (
}
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()),
}
if (
this.stationInfo?.supervisionUrlOcppConfiguration === true &&
- isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) &&
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!) == null
+ isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) &&
+ getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey) == null
) {
addConfigurationKey(
this,
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- this.stationInfo.supervisionUrlOcppKey!,
+ this.stationInfo.supervisionUrlOcppKey,
this.configuredSupervisionUrl.href,
{ reboot: true }
)
} else if (
this.stationInfo?.supervisionUrlOcppConfiguration === false &&
- isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) &&
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!) != null
+ isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) &&
+ getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey) != null
) {
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- deleteConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!, { save: false })
+ deleteConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey, { save: false })
}
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,
- // 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()
}
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>(connectorStatus))
+ this.connectors.set(connectorId, clone<ConnectorStatus>(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>(evseStatusConfiguration)
+ const evseStatus = clone<EvseStatusConfiguration>(evseStatusConfiguration)
delete evseStatus.connectorsStatus
this.evses.set(evseId, {
...(evseStatus as EvseStatus),
)
})
}
- } 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)
}
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)
}
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,
this.logPrefix(),
this.templateFile
)
- this.connectors.set(connectorId, cloneObject<ConnectorStatus>(connectorStatus))
+ this.connectors.set(connectorId, clone<ConnectorStatus>(connectorStatus))
}
initializeConnectorsMapStatus(this.connectors, this.logPrefix())
this.saveConnectorsStatus()
}
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
),
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<ChargingStationConfiguration>(this.getConfigurationFromFile()!)
+ configurationFromFile != null
+ ? clone<ChargingStationConfiguration>(configurationFromFile)
: {}
- if (
- this.stationInfo?.stationInfoPersistentConfiguration === true &&
- this.stationInfo != null
- ) {
+ if (this.stationInfo?.stationInfoPersistentConfiguration === true) {
configurationData.stationInfo = this.stationInfo
} else {
delete configurationData.stationInfo
this.stationInfo?.ocppPersistentConfiguration === true &&
Array.isArray(this.ocppConfiguration?.configurationKey)
) {
- configurationData.configurationKey = this.ocppConfiguration?.configurationKey
+ configurationData.configurationKey = this.ocppConfiguration.configurationKey
} else {
delete configurationData.configurationKey
}
this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash)
this.sharedLRUCache.setChargingStationConfiguration(configurationData)
this.configurationFileHash = configurationHash
- }).catch((error) => {
+ }).catch(error => {
handleFileException(
this.configurationFile,
FileType.ChargingStationConfiguration,
>(this, RequestCommand.BOOT_NOTIFICATION, this.bootNotificationRequest, {
skipBufferingOnError: true
})
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.bootNotificationResponse.currentTime = convertToDate(
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+ 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
)
} 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)
)
}
}
} 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
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)
}
// 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))
}
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
)
}
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
)
}
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(
}
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)
)
}
private getAmperageLimitation (): number | undefined {
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)
+ convertToInt(getConfigurationKey(this, this.stationInfo.amperageLimitationOcppKey)?.value) /
+ getAmperageLimitationUnitDivider(this.stationInfo)
)
}
}
}
}
}
- if (this.stationInfo.firmwareStatus === FirmwareStatus.Installing) {
+ if (this.stationInfo?.firmwareStatus === FirmwareStatus.Installing) {
await this.ocppRequestService.requestHandler<
FirmwareStatusNotificationRequest,
FirmwareStatusNotificationResponse
}
// Start the ATG
- if (this.getAutomaticTransactionGeneratorConfiguration().enable) {
+ if (this.getAutomaticTransactionGeneratorConfiguration()?.enable === true) {
this.startAutomaticTransactionGenerator()
}
this.flushMessageBuffer()
evseId
)
)
- delete connectorStatus?.status
+ delete connectorStatus.status
}
}
}
}
private startWebSocketPing (): void {
- const webSocketPingInterval: number =
+ const webSocketPingInterval =
getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval) != null
? convertToInt(
getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval)?.value
let configuredSupervisionUrlIndex: number
switch (Configuration.getSupervisionUrlDistribution()) {
case SupervisionUrlDistribution.RANDOM:
- configuredSupervisionUrlIndex = Math.floor(
- secureRandom() * (supervisionUrls as string[]).length
- )
+ configuredSupervisionUrlIndex = Math.floor(secureRandom() * supervisionUrls.length)
break
case SupervisionUrlDistribution.ROUND_ROBIN:
case SupervisionUrlDistribution.CHARGING_STATION_AFFINITY:
SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
}`
)
- configuredSupervisionUrlIndex = (this.index - 1) % (supervisionUrls as string[]).length
+ configuredSupervisionUrlIndex = (this.index - 1) % supervisionUrls.length
break
}
- configuredSupervisionUrl = (supervisionUrls as string[])[configuredSupervisionUrlIndex]
+ configuredSupervisionUrl = supervisionUrls[configuredSupervisionUrlIndex]
} else {
- configuredSupervisionUrl = supervisionUrls as string
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ configuredSupervisionUrl = supervisionUrls!
}
if (isNotEmptyString(configuredSupervisionUrl)) {
return new URL(configuredSupervisionUrl)
}
const errorMsg = 'No supervision url(s) configured'
logger.error(`${this.logPrefix()} ${errorMsg}`)
- throw new BaseError(`${errorMsg}`)
+ throw new BaseError(errorMsg)
}
private stopHeartbeat (): void {
// 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
: 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,