Skip to content

Commit

Permalink
refactor: validate station information at CS init
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <[email protected]>
  • Loading branch information
jerome-benoit committed Jul 24, 2024
1 parent 45087a8 commit b55f94b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/charging-station/AutomaticTransactionGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
sleep,
} from '../utils/index.js'
import type { ChargingStation } from './ChargingStation.js'
import { checkChargingStation } from './Helpers.js'
import { checkChargingStationState } from './Helpers.js'
import { IdTagsCache } from './IdTagsCache.js'
import { isIdTagAuthorized } from './ocpp/index.js'

Expand Down Expand Up @@ -74,7 +74,7 @@ export class AutomaticTransactionGenerator {
}

public start (stopAbsoluteDuration?: boolean): void {
if (!checkChargingStation(this.chargingStation, this.logPrefix())) {
if (!checkChargingStationState(this.chargingStation, this.logPrefix())) {
return
}
if (this.started) {
Expand Down Expand Up @@ -107,7 +107,7 @@ export class AutomaticTransactionGenerator {
}

public startConnector (connectorId: number, stopAbsoluteDuration?: boolean): void {
if (!checkChargingStation(this.chargingStation, this.logPrefix(connectorId))) {
if (!checkChargingStationState(this.chargingStation, this.logPrefix(connectorId))) {
return
}
if (!this.connectorsStatus.has(connectorId)) {
Expand Down
6 changes: 4 additions & 2 deletions src/charging-station/ChargingStation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ import {
import {
buildConnectorsMap,
buildTemplateName,
checkChargingStation,
checkChargingStationState,
checkConfiguration,
checkConnectorsConfiguration,
checkStationInfoConnectorStatus,
Expand All @@ -136,6 +136,7 @@ import {
propagateSerialNumber,
setChargingStationOptions,
stationTemplateToStationInfo,
validateStationInfo,
warnTemplateKeysDeprecation,
} from './Helpers.js'
import { IdTagsCache } from './IdTagsCache.js'
Expand Down Expand Up @@ -827,7 +828,7 @@ export class ChargingStation extends EventEmitter {
...options,
}
params = { ...{ closeOpened: false, terminateOpened: false }, ...params }
if (!checkChargingStation(this, this.logPrefix())) {
if (!checkChargingStationState(this, this.logPrefix())) {
return
}
if (this.stationInfo?.supervisionUser != null && this.stationInfo.supervisionPassword != null) {
Expand Down Expand Up @@ -1304,6 +1305,7 @@ export class ChargingStation extends EventEmitter {
this.initializeConnectorsOrEvsesFromTemplate(stationTemplate)
}
this.stationInfo = this.getStationInfo(options)
validateStationInfo(this)
if (
this.stationInfo.firmwareStatus === FirmwareStatus.Installing &&
isNotEmptyString(this.stationInfo.firmwareVersionPattern) &&
Expand Down
39 changes: 38 additions & 1 deletion src/charging-station/Helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,44 @@ export const getHashId = (index: number, stationTemplate: ChargingStationTemplat
.digest('hex')
}

export const checkChargingStation = (
export const validateStationInfo = (chargingStation: ChargingStation): void => {
if (isEmpty(chargingStation.stationInfo)) {
throw new BaseError('Missing charging station information')
}
if (isEmpty(chargingStation.stationInfo?.hashId.trim())) {
throw new BaseError('Missing hashId in stationInfo properties')
}
if (isEmpty(chargingStation.stationInfo?.templateIndex)) {
throw new BaseError('Missing templateIndex in stationInfo properties')
}
if (isEmpty(chargingStation.stationInfo?.templateName.trim())) {
throw new BaseError('Missing templateName in stationInfo properties')
}
if (isEmpty(chargingStation.stationInfo?.chargingStationId?.trim())) {
throw new BaseError('Missing chargingStationId in stationInfo properties')
}
if (isEmpty(chargingStation.stationInfo?.maximumPower)) {
throw new BaseError('Missing maximumPower in stationInfo properties')
}
if (chargingStation.stationInfo?.maximumPower != null && chargingStation.stationInfo.maximumPower <= 0) {
throw new RangeError('Invalid maximumPower value in stationInfo properties')
}
if (isEmpty(chargingStation.stationInfo?.maximumAmperage)) {
throw new BaseError('Missing maximumAmperage in stationInfo properties')
}
if (chargingStation.stationInfo?.maximumAmperage != null && chargingStation.stationInfo.maximumAmperage <= 0) {
throw new RangeError('Invalid maximumAmperage value in stationInfo properties')
}
switch (chargingStation.stationInfo?.ocppVersion) {
case OCPPVersion.VERSION_20:
case OCPPVersion.VERSION_201:
if (chargingStation.evses.size === 0) {
throw new BaseError('OCPP 2.0 or superior requires at least one EVSE defined in the charging station template/configuration')
}
}
}

export const checkChargingStationState = (
chargingStation: ChargingStation,
logPrefix: string
): boolean => {
Expand Down
2 changes: 1 addition & 1 deletion src/charging-station/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export {
} from './ConfigurationKeyUtils.js'
export {
canProceedChargingProfile,
checkChargingStation,
checkChargingStationState,
getConnectorChargingProfiles,
getIdTagsFile,
hasFeatureProfile,
Expand Down
6 changes: 3 additions & 3 deletions src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { create } from 'tar'
import {
canProceedChargingProfile,
type ChargingStation,
checkChargingStation,
checkChargingStationState,
getConfigurationKey,
getConnectorChargingProfiles,
prepareChargingProfileKind,
Expand Down Expand Up @@ -1353,7 +1353,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
maxDelay = 30,
minDelay = 15
): Promise<void> {
if (!checkChargingStation(chargingStation, chargingStation.logPrefix())) {
if (!checkChargingStationState(chargingStation, chargingStation.logPrefix())) {
return
}
if (chargingStation.hasEvses) {
Expand Down Expand Up @@ -1464,7 +1464,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
}
} while (transactionsStarted)
!wasTransactionsStarted && (await sleep(secondsToMilliseconds(randomInt(minDelay, maxDelay))))
if (!checkChargingStation(chargingStation, chargingStation.logPrefix())) {
if (!checkChargingStationState(chargingStation, chargingStation.logPrefix())) {
return
}
await chargingStation.ocppRequestService.requestHandler<
Expand Down

0 comments on commit b55f94b

Please sign in to comment.