From 4e3b1d6bdc767ec63949b998f0375fe2b5bc2b28 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 20 Nov 2023 17:05:31 +0100 Subject: [PATCH] refactor: stricter type checking in conditions MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 77 +++++++++++-------- src/charging-station/ConfigurationKeyUtils.ts | 8 +- .../ChargingStationWorkerBroadcastChannel.ts | 2 +- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 2 +- .../ocpp/1.6/OCPP16ResponseService.ts | 2 +- 5 files changed, 50 insertions(+), 41 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index e12e6d17..6d4b45f3 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -259,7 +259,7 @@ export class ChargingStation extends EventEmitter { private get wsConnectionUrl(): URL { return new URL( `${ - this.stationInfo?.supervisionUrlOcppConfiguration && + this.stationInfo?.supervisionUrlOcppConfiguration === true && isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) && isNotEmptyString(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!)?.value) ? getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!)!.value @@ -287,7 +287,7 @@ export class ChargingStation extends EventEmitter { const localStationInfo: ChargingStationInfo = stationInfo ?? this.stationInfo; switch (this.getCurrentOutType(stationInfo)) { case CurrentType.AC: - return !isUndefined(localStationInfo.numberOfPhases) ? localStationInfo.numberOfPhases! : 3; + return localStationInfo.numberOfPhases ?? 3; case CurrentType.DC: return 0; } @@ -483,7 +483,9 @@ export class ChargingStation extends EventEmitter { this, StandardParametersKey.AuthorizeRemoteTxRequests, ); - return authorizeRemoteTxRequests ? convertToBoolean(authorizeRemoteTxRequests.value) : false; + return authorizeRemoteTxRequests !== undefined + ? convertToBoolean(authorizeRemoteTxRequests.value) + : false; } public getLocalAuthListEnabled(): boolean { @@ -491,16 +493,18 @@ export class ChargingStation extends EventEmitter { this, StandardParametersKey.LocalAuthListEnabled, ); - return localAuthListEnabled ? convertToBoolean(localAuthListEnabled.value) : false; + return localAuthListEnabled !== undefined + ? convertToBoolean(localAuthListEnabled.value) + : false; } public getHeartbeatInterval(): number { const HeartbeatInterval = getConfigurationKey(this, StandardParametersKey.HeartbeatInterval); - if (HeartbeatInterval) { + if (HeartbeatInterval !== undefined) { return secondsToMilliseconds(convertToInt(HeartbeatInterval.value)); } const HeartBeatInterval = getConfigurationKey(this, StandardParametersKey.HeartBeatInterval); - if (HeartBeatInterval) { + if (HeartBeatInterval !== undefined) { return secondsToMilliseconds(convertToInt(HeartBeatInterval.value)); } this.stationInfo?.autoRegister === false && @@ -916,11 +920,8 @@ export class ChargingStation extends EventEmitter { public async addReservation(reservation: Reservation): Promise { const reservationFound = this.getReservationBy('reservationId', reservation.reservationId); - if (!isUndefined(reservationFound)) { - await this.removeReservation( - reservationFound!, - ReservationTerminationReason.REPLACE_EXISTING, - ); + if (reservationFound !== undefined) { + await this.removeReservation(reservationFound, ReservationTerminationReason.REPLACE_EXISTING); } this.getConnectorStatus(reservation.connectorId)!.reservation = reservation; await OCPPServiceUtils.sendAndSetConnectorStatus( @@ -1293,16 +1294,16 @@ export class ChargingStation extends EventEmitter { } private initializeOcppConfiguration(): void { - if (!getConfigurationKey(this, StandardParametersKey.HeartbeatInterval)) { + if (isNullOrUndefined(getConfigurationKey(this, StandardParametersKey.HeartbeatInterval))) { addConfigurationKey(this, StandardParametersKey.HeartbeatInterval, '0'); } - if (!getConfigurationKey(this, StandardParametersKey.HeartBeatInterval)) { + if (isNullOrUndefined(getConfigurationKey(this, StandardParametersKey.HeartBeatInterval))) { addConfigurationKey(this, StandardParametersKey.HeartBeatInterval, '0', { visible: false }); } if ( - this.stationInfo?.supervisionUrlOcppConfiguration && + this.stationInfo?.supervisionUrlOcppConfiguration === true && isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) && - !getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!) + isNullOrUndefined(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!)) ) { addConfigurationKey( this, @@ -1311,7 +1312,7 @@ export class ChargingStation extends EventEmitter { { reboot: true }, ); } else if ( - !this.stationInfo?.supervisionUrlOcppConfiguration && + this.stationInfo?.supervisionUrlOcppConfiguration === false && isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) && getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!) ) { @@ -1319,7 +1320,7 @@ export class ChargingStation extends EventEmitter { } if ( isNotEmptyString(this.stationInfo?.amperageLimitationOcppKey) && - !getConfigurationKey(this, this.stationInfo.amperageLimitationOcppKey!) + isNullOrUndefined(getConfigurationKey(this, this.stationInfo.amperageLimitationOcppKey!)) ) { addConfigurationKey( this, @@ -1329,7 +1330,9 @@ export class ChargingStation extends EventEmitter { ).toString(), ); } - if (!getConfigurationKey(this, StandardParametersKey.SupportedFeatureProfiles)) { + if ( + isNullOrUndefined(getConfigurationKey(this, StandardParametersKey.SupportedFeatureProfiles)) + ) { addConfigurationKey( this, StandardParametersKey.SupportedFeatureProfiles, @@ -1343,14 +1346,18 @@ export class ChargingStation extends EventEmitter { { readonly: true }, { overwrite: true }, ); - if (!getConfigurationKey(this, StandardParametersKey.MeterValuesSampledData)) { + if ( + isNullOrUndefined(getConfigurationKey(this, StandardParametersKey.MeterValuesSampledData)) + ) { addConfigurationKey( this, StandardParametersKey.MeterValuesSampledData, MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER, ); } - if (!getConfigurationKey(this, StandardParametersKey.ConnectorPhaseRotation)) { + if ( + isNullOrUndefined(getConfigurationKey(this, StandardParametersKey.ConnectorPhaseRotation)) + ) { const connectorsPhaseRotation: string[] = []; if (this.hasEvses) { for (const evseStatus of this.evses.values()) { @@ -1373,18 +1380,20 @@ export class ChargingStation extends EventEmitter { connectorsPhaseRotation.toString(), ); } - if (!getConfigurationKey(this, StandardParametersKey.AuthorizeRemoteTxRequests)) { + if ( + isNullOrUndefined(getConfigurationKey(this, StandardParametersKey.AuthorizeRemoteTxRequests)) + ) { addConfigurationKey(this, StandardParametersKey.AuthorizeRemoteTxRequests, 'true'); } if ( - !getConfigurationKey(this, StandardParametersKey.LocalAuthListEnabled) && + isNullOrUndefined(getConfigurationKey(this, StandardParametersKey.LocalAuthListEnabled)) && getConfigurationKey(this, StandardParametersKey.SupportedFeatureProfiles)?.value?.includes( SupportedFeatureProfiles.LocalAuthListManagement, ) ) { addConfigurationKey(this, StandardParametersKey.LocalAuthListEnabled, 'false'); } - if (!getConfigurationKey(this, StandardParametersKey.ConnectionTimeOut)) { + if (isNullOrUndefined(getConfigurationKey(this, StandardParametersKey.ConnectionTimeOut))) { addConfigurationKey( this, StandardParametersKey.ConnectionTimeOut, @@ -2035,10 +2044,10 @@ export class ChargingStation extends EventEmitter { // 0 for disabling private getConnectionTimeout(): number { - if (getConfigurationKey(this, StandardParametersKey.ConnectionTimeOut)) { - return ( - parseInt(getConfigurationKey(this, StandardParametersKey.ConnectionTimeOut)!.value!) ?? - Constants.DEFAULT_CONNECTION_TIMEOUT + if (getConfigurationKey(this, StandardParametersKey.ConnectionTimeOut) !== undefined) { + return convertToInt( + getConfigurationKey(this, StandardParametersKey.ConnectionTimeOut)!.value! ?? + Constants.DEFAULT_CONNECTION_TIMEOUT, ); } return Constants.DEFAULT_CONNECTION_TIMEOUT; @@ -2084,7 +2093,7 @@ export class ChargingStation extends EventEmitter { private getAmperageLimitation(): number | undefined { if ( isNotEmptyString(this.stationInfo?.amperageLimitationOcppKey) && - getConfigurationKey(this, this.stationInfo.amperageLimitationOcppKey!) + getConfigurationKey(this, this.stationInfo.amperageLimitationOcppKey!) !== undefined ) { return ( convertToInt( @@ -2208,12 +2217,12 @@ export class ChargingStation extends EventEmitter { } private startWebSocketPing(): void { - const webSocketPingInterval: number = getConfigurationKey( - this, - StandardParametersKey.WebSocketPingInterval, - ) - ? convertToInt(getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval)?.value) - : 0; + const webSocketPingInterval: number = + getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval) !== undefined + ? convertToInt( + getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval)?.value, + ) + : 0; if (webSocketPingInterval > 0 && !this.webSocketPingSetInterval) { this.webSocketPingSetInterval = setInterval(() => { if (this.isWebSocketConnectionOpened() === true) { diff --git a/src/charging-station/ConfigurationKeyUtils.ts b/src/charging-station/ConfigurationKeyUtils.ts index 96a13e71..d1d093f7 100644 --- a/src/charging-station/ConfigurationKeyUtils.ts +++ b/src/charging-station/ConfigurationKeyUtils.ts @@ -46,13 +46,13 @@ export const addConfigurationKey = ( }; params = { ...{ overwrite: false, save: false }, ...params }; let keyFound = getConfigurationKey(chargingStation, key); - if (keyFound && params?.overwrite) { + if (keyFound !== undefined && params?.overwrite === true) { deleteConfigurationKey(chargingStation, keyFound.key, { save: false, }); keyFound = undefined; } - if (!keyFound) { + if (keyFound === undefined) { chargingStation.ocppConfiguration?.configurationKey?.push({ key, readonly: options.readonly!, @@ -76,7 +76,7 @@ export const setConfigurationKeyValue = ( caseInsensitive = false, ): void => { const keyFound = getConfigurationKey(chargingStation, key, caseInsensitive); - if (keyFound) { + if (keyFound !== undefined) { chargingStation.ocppConfiguration!.configurationKey![ chargingStation.ocppConfiguration!.configurationKey!.indexOf(keyFound) ].value = value; @@ -96,7 +96,7 @@ export const deleteConfigurationKey = ( ): ConfigurationKey[] | undefined => { params = { ...{ save: true, caseInsensitive: false }, ...params }; const keyFound = getConfigurationKey(chargingStation, key, params?.caseInsensitive); - if (keyFound) { + if (keyFound !== undefined) { const deletedConfigurationKey = chargingStation.ocppConfiguration?.configurationKey?.splice( chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound), 1, diff --git a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts index fff823c0..5188d974 100644 --- a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts @@ -194,7 +194,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne requestPayload!.connectorId!, this.chargingStation.getConnectorStatus(requestPayload!.connectorId!)! .transactionId!, - configuredMeterValueSampleInterval + configuredMeterValueSampleInterval !== undefined ? secondsToMilliseconds(convertToInt(configuredMeterValueSampleInterval.value)) : Constants.DEFAULT_METER_VALUES_INTERVAL, ), diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 83300b94..2b34342e 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -511,7 +511,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { } else if (isNotEmptyArray(key) === true) { for (const k of key!) { const keyFound = getConfigurationKey(chargingStation, k, true); - if (keyFound) { + if (keyFound !== undefined) { if (isUndefined(keyFound.visible) === true) { keyFound.visible = true; } diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index edec1e4e..64145bb3 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -657,7 +657,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ); chargingStation.startMeterValues( connectorId, - configuredMeterValueSampleInterval + configuredMeterValueSampleInterval !== undefined ? secondsToMilliseconds(convertToInt(configuredMeterValueSampleInterval.value)) : Constants.DEFAULT_METER_VALUES_INTERVAL, ); -- 2.34.1