}
const wait = secondsToMilliseconds(
randomInt(
- this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
- ?.minDelayBetweenTwoTransactions,
- this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
- ?.maxDelayBetweenTwoTransactions
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.chargingStation.getAutomaticTransactionGeneratorConfiguration()!
+ .minDelayBetweenTwoTransactions,
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.chargingStation.getAutomaticTransactionGeneratorConfiguration()!
+ .maxDelayBetweenTwoTransactions
)
)
logger.info(`${this.logPrefix(connectorId)} waiting for ${formatDurationMilliSeconds(wait)}`)
// Wait until end of transaction
const waitTrxEnd = secondsToMilliseconds(
randomInt(
- this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.minDuration,
- this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.maxDuration
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.chargingStation.getAutomaticTransactionGeneratorConfiguration()!.minDuration,
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.chargingStation.getAutomaticTransactionGeneratorConfiguration()!.maxDuration
)
)
logger.info(
}
export const validateStationInfo = (chargingStation: ChargingStation): void => {
- if (isEmpty(chargingStation.stationInfo)) {
+ if (chargingStation.stationInfo == null || isEmpty(chargingStation.stationInfo)) {
throw new BaseError('Missing charging station information')
}
- if (isEmpty(chargingStation.stationInfo?.chargingStationId?.trim())) {
+ if (
+ chargingStation.stationInfo.chargingStationId == null ||
+ isEmpty(chargingStation.stationInfo.chargingStationId.trim())
+ ) {
throw new BaseError('Missing chargingStationId in stationInfo properties')
}
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- const chargingStationId: string = chargingStation.stationInfo!.chargingStationId!
- if (isEmpty(chargingStation.stationInfo?.hashId.trim())) {
+ const chargingStationId = chargingStation.stationInfo.chargingStationId
+ if (
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+ chargingStation.stationInfo.hashId == null ||
+ isEmpty(chargingStation.stationInfo.hashId.trim())
+ ) {
throw new BaseError(`${chargingStationId}: Missing hashId in stationInfo properties`)
}
- if (isEmpty(chargingStation.stationInfo?.templateIndex)) {
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+ if (chargingStation.stationInfo.templateIndex == null) {
throw new BaseError(`${chargingStationId}: Missing templateIndex in stationInfo properties`)
}
- if (isEmpty(chargingStation.stationInfo?.templateName.trim())) {
+ if (chargingStation.stationInfo.templateIndex <= 0) {
+ throw new BaseError(
+ `${chargingStationId}: Invalid templateIndex value in stationInfo properties`
+ )
+ }
+ if (
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+ chargingStation.stationInfo.templateName == null ||
+ isEmpty(chargingStation.stationInfo.templateName.trim())
+ ) {
throw new BaseError(`${chargingStationId}: Missing templateName in stationInfo properties`)
}
- if (isEmpty(chargingStation.stationInfo?.maximumPower)) {
+ if (chargingStation.stationInfo.maximumPower == null) {
throw new BaseError(`${chargingStationId}: Missing maximumPower in stationInfo properties`)
}
- if (
- chargingStation.stationInfo?.maximumPower != null &&
- chargingStation.stationInfo.maximumPower <= 0
- ) {
+ if (chargingStation.stationInfo.maximumPower <= 0) {
throw new RangeError(
`${chargingStationId}: Invalid maximumPower value in stationInfo properties`
)
}
- if (isEmpty(chargingStation.stationInfo?.maximumAmperage)) {
+ if (chargingStation.stationInfo.maximumAmperage == null) {
throw new BaseError(`${chargingStationId}: Missing maximumAmperage in stationInfo properties`)
}
- if (
- chargingStation.stationInfo?.maximumAmperage != null &&
- chargingStation.stationInfo.maximumAmperage <= 0
- ) {
+ if (chargingStation.stationInfo.maximumAmperage <= 0) {
throw new RangeError(
`${chargingStationId}: Invalid maximumAmperage value in stationInfo properties`
)
}
- switch (chargingStation.stationInfo?.ocppVersion) {
+ switch (chargingStation.stationInfo.ocppVersion) {
case OCPPVersion.VERSION_20:
case OCPPVersion.VERSION_201:
if (chargingStation.evses.size === 0) {
--- /dev/null
+import { describe, it } from 'node:test'
+
+import { expect } from 'expect'
+
+import {
+ getChargingStationId,
+ getHashId,
+ validateStationInfo,
+} from '../../src/charging-station/Helpers.js'
+import type { ChargingStation } from '../../src/charging-station/index.js'
+import { BaseError } from '../../src/exception/index.js'
+import {
+ type ChargingStationInfo,
+ type ChargingStationTemplate,
+ type EvseStatus,
+ OCPPVersion,
+} from '../../src/types/index.js'
+
+await describe('Helpers test suite', async () => {
+ const baseName = 'CS-TEST'
+ const chargingStationTemplate = {
+ baseName,
+ } as ChargingStationTemplate
+ const chargingStation = {} as ChargingStation
+
+ await it('Verify getChargingStationId()', t => {
+ expect(getChargingStationId(1, chargingStationTemplate)).toBe(`${baseName}-00001`)
+ })
+
+ await it('Verify getHashId()', t => {
+ expect(getHashId(1, chargingStationTemplate)).toBe(
+ 'b4b1e8ec4fca79091d99ea9a7ea5901548010e6c0e98be9296f604b9d68734444dfdae73d7d406b6124b42815214d088'
+ )
+ })
+
+ await it('Verify validateStationInfo()', t => {
+ expect(() => {
+ validateStationInfo(chargingStation)
+ }).toThrow(new BaseError('Missing charging station information'))
+ chargingStation.stationInfo = {} as ChargingStationInfo
+ expect(() => {
+ validateStationInfo(chargingStation)
+ }).toThrow(new BaseError('Missing charging station information'))
+ chargingStation.stationInfo.baseName = baseName
+ expect(() => {
+ validateStationInfo(chargingStation)
+ }).toThrow(new BaseError('Missing chargingStationId in stationInfo properties'))
+ chargingStation.stationInfo.chargingStationId = ''
+ expect(() => {
+ validateStationInfo(chargingStation)
+ }).toThrow(new BaseError('Missing chargingStationId in stationInfo properties'))
+ chargingStation.stationInfo.chargingStationId = getChargingStationId(1, chargingStationTemplate)
+ expect(() => {
+ validateStationInfo(chargingStation)
+ }).toThrow(new BaseError(`${baseName}-00001: Missing hashId in stationInfo properties`))
+ chargingStation.stationInfo.hashId = ''
+ expect(() => {
+ validateStationInfo(chargingStation)
+ }).toThrow(new BaseError(`${baseName}-00001: Missing hashId in stationInfo properties`))
+ chargingStation.stationInfo.hashId = getHashId(1, chargingStationTemplate)
+ expect(() => {
+ validateStationInfo(chargingStation)
+ }).toThrow(new BaseError(`${baseName}-00001: Missing templateIndex in stationInfo properties`))
+ chargingStation.stationInfo.templateIndex = 0
+ expect(() => {
+ validateStationInfo(chargingStation)
+ }).toThrow(
+ new BaseError(`${baseName}-00001: Invalid templateIndex value in stationInfo properties`)
+ )
+ chargingStation.stationInfo.templateIndex = 1
+ expect(() => {
+ validateStationInfo(chargingStation)
+ }).toThrow(new BaseError(`${baseName}-00001: Missing templateName in stationInfo properties`))
+ chargingStation.stationInfo.templateName = ''
+ expect(() => {
+ validateStationInfo(chargingStation)
+ }).toThrow(new BaseError(`${baseName}-00001: Missing templateName in stationInfo properties`))
+ chargingStation.stationInfo.templateName = 'test-template.json'
+ expect(() => {
+ validateStationInfo(chargingStation)
+ }).toThrow(new BaseError(`${baseName}-00001: Missing maximumPower in stationInfo properties`))
+ chargingStation.stationInfo.maximumPower = 0
+ expect(() => {
+ validateStationInfo(chargingStation)
+ }).toThrow(
+ new BaseError(`${baseName}-00001: Invalid maximumPower value in stationInfo properties`)
+ )
+ chargingStation.stationInfo.maximumPower = 12000
+ expect(() => {
+ validateStationInfo(chargingStation)
+ }).toThrow(
+ new BaseError(`${baseName}-00001: Missing maximumAmperage in stationInfo properties`)
+ )
+ chargingStation.stationInfo.maximumAmperage = 0
+ expect(() => {
+ validateStationInfo(chargingStation)
+ }).toThrow(
+ new BaseError(`${baseName}-00001: Invalid maximumAmperage value in stationInfo properties`)
+ )
+ chargingStation.stationInfo.maximumAmperage = 16
+ expect(() => {
+ validateStationInfo(chargingStation)
+ }).not.toThrow()
+ chargingStation.evses = new Map<number, EvseStatus>()
+ chargingStation.stationInfo.ocppVersion = OCPPVersion.VERSION_201
+ expect(() => {
+ validateStationInfo(chargingStation)
+ }).toThrow(
+ new BaseError(
+ `${baseName}-00001: OCPP 2.0 or superior requires at least one EVSE defined in the charging station template/configuration`
+ )
+ )
+ })
+})