From d812bdcbd13b39bf895bb3e01c0556d87c35a6d1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 25 Jan 2023 22:36:43 +0100 Subject: [PATCH] Falsy/truthy test fixes MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../AutomaticTransactionGenerator.ts | 2 +- src/charging-station/Bootstrap.ts | 2 +- src/charging-station/ChargingStation.ts | 28 +++++++++---------- src/charging-station/ChargingStationUtils.ts | 6 ++-- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 21 ++++++++------ .../ocpp/1.6/OCPP16ResponseService.ts | 6 ++-- src/performance/storage/JsonFileStorage.ts | 2 +- 7 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 4603f278..0f26d3a2 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -387,7 +387,7 @@ export default class AutomaticTransactionGenerator extends AsyncResource { const transactionId = this.chargingStation.getConnectorStatus(connectorId)?.transactionId; logger.warn( `${this.logPrefix(connectorId)} stopping a not started transaction${ - transactionId ? ` ${transactionId.toString()}` : '' + !Utils.isNullOrUndefined(transactionId) ? ` ${transactionId?.toString()}` : '' }` ); } diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index ce1290db..bbf20f44 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -129,7 +129,7 @@ export class Bootstrap { ? `/${Configuration.getWorker().poolMaxSize?.toString()}` : '' } worker(s) concurrently running in '${Configuration.getWorker().processType}' mode${ - this.workerImplementation?.maxElementsPerWorker + !Utils.isNullOrUndefined(this.workerImplementation?.maxElementsPerWorker) ? ` (${this.workerImplementation?.maxElementsPerWorker} charging station(s) per worker)` : '' }` diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 5123ce1c..ddaff652 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -452,7 +452,7 @@ export default class ChargingStation { return; } else if ( this.getConnectorStatus(connectorId)?.transactionStarted === true && - !this.getConnectorStatus(connectorId)?.transactionId + Utils.isNullOrUndefined(this.getConnectorStatus(connectorId)?.transactionId) ) { logger.error( `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction id` @@ -596,12 +596,12 @@ export default class ChargingStation { this.getConnectorStatus(connectorId).idTagAuthorized = false; this.getConnectorStatus(connectorId).transactionRemoteStarted = false; this.getConnectorStatus(connectorId).transactionStarted = false; - delete this.getConnectorStatus(connectorId).localAuthorizeIdTag; - delete this.getConnectorStatus(connectorId).authorizeIdTag; - delete this.getConnectorStatus(connectorId).transactionId; - delete this.getConnectorStatus(connectorId).transactionIdTag; + delete this.getConnectorStatus(connectorId)?.localAuthorizeIdTag; + delete this.getConnectorStatus(connectorId)?.authorizeIdTag; + delete this.getConnectorStatus(connectorId)?.transactionId; + delete this.getConnectorStatus(connectorId)?.transactionIdTag; this.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue = 0; - delete this.getConnectorStatus(connectorId).transactionBeginMeterValue; + delete this.getConnectorStatus(connectorId)?.transactionBeginMeterValue; this.stopMeterValues(connectorId); parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this)); } @@ -879,7 +879,7 @@ export default class ChargingStation { stationInfo.firmwareVersionPattern = stationTemplate?.firmwareVersionPattern ?? Constants.SEMVER_PATTERN; if ( - stationInfo.firmwareVersion && + !Utils.isEmptyString(stationInfo.firmwareVersion) && new RegExp(stationInfo.firmwareVersionPattern).test(stationInfo.firmwareVersion) === false ) { logger.warn( @@ -894,7 +894,7 @@ export default class ChargingStation { }, stationTemplate?.firmwareUpgrade ?? {} ); - stationInfo.resetTime = stationTemplate?.resetTime + stationInfo.resetTime = !Utils.isNullOrUndefined(stationTemplate?.resetTime) ? stationTemplate.resetTime * 1000 : Constants.CHARGING_STATION_DEFAULT_RESET_TIME; const configuredMaxConnectors = @@ -1031,13 +1031,13 @@ export default class ChargingStation { } if ( this.stationInfo.firmwareStatus === FirmwareStatus.Installing && - this.stationInfo.firmwareVersion && - this.stationInfo.firmwareVersionPattern + !Utils.isEmptyString(this.stationInfo.firmwareVersion) && + !Utils.isEmptyString(this.stationInfo.firmwareVersionPattern) ) { const versionStep = this.stationInfo.firmwareUpgrade?.versionUpgrade?.step ?? 1; - const patternGroup: number = + const patternGroup: number | undefined = this.stationInfo.firmwareUpgrade?.versionUpgrade?.patternGroup ?? - this.stationInfo.firmwareVersion.split('.').length; + this.stationInfo.firmwareVersion?.split('.').length; const match = this.stationInfo?.firmwareVersion ?.match(new RegExp(this.stationInfo.firmwareVersionPattern)) ?.slice(1, patternGroup + 1); @@ -1096,7 +1096,7 @@ export default class ChargingStation { ); } if ( - this.stationInfo.amperageLimitationOcppKey && + !Utils.isEmptyString(this.stationInfo?.amperageLimitationOcppKey) && !ChargingStationConfigurationUtils.getConfigurationKey( this, this.stationInfo.amperageLimitationOcppKey @@ -1761,7 +1761,7 @@ export default class ChargingStation { private getAmperageLimitation(): number | undefined { if ( - this.stationInfo.amperageLimitationOcppKey && + !Utils.isEmptyString(this.stationInfo?.amperageLimitationOcppKey) && ChargingStationConfigurationUtils.getConfigurationKey( this, this.stationInfo.amperageLimitationOcppKey diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 44b7dee0..9b371d14 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -269,13 +269,13 @@ export class ChargingStationUtils { }) : ''; stationInfo.chargePointSerialNumber = - stationTemplate?.chargePointSerialNumberPrefix && + !Utils.isEmptyString(stationTemplate?.chargePointSerialNumberPrefix) && `${stationTemplate.chargePointSerialNumberPrefix}${serialNumberSuffix}`; stationInfo.chargeBoxSerialNumber = - stationTemplate?.chargeBoxSerialNumberPrefix && + !Utils.isEmptyString(stationTemplate?.chargeBoxSerialNumberPrefix) && `${stationTemplate.chargeBoxSerialNumberPrefix}${serialNumberSuffix}`; stationInfo.meterSerialNumber = - stationTemplate?.meterSerialNumberPrefix && + !Utils.isEmptyString(stationTemplate?.meterSerialNumberPrefix) && `${stationTemplate.meterSerialNumberPrefix}${serialNumberSuffix}`; } diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index f81c27ed..b9f8368b 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -572,7 +572,10 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer return OCPPConstants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; } const connectorStatus = chargingStation.getConnectorStatus(commandPayload.connectorId); - if (commandPayload.connectorId && !Utils.isEmptyArray(connectorStatus?.chargingProfiles)) { + if ( + !Utils.isNullOrUndefined(commandPayload.connectorId) && + !Utils.isEmptyArray(connectorStatus?.chargingProfiles) + ) { connectorStatus.chargingProfiles = []; logger.debug( `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${ @@ -581,7 +584,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ); return OCPPConstants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED; } - if (!commandPayload.connectorId) { + if (Utils.isNullOrUndefined(commandPayload.connectorId)) { let clearedCP = false; for (const connectorId of chargingStation.connectors.keys()) { if ( @@ -716,11 +719,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer if ( chargingStation.getLocalAuthListEnabled() === true && chargingStation.hasAuthorizedTags() === true && - chargingStation.authorizedTagsCache - .getAuthorizedTags( - ChargingStationUtils.getAuthorizationFile(chargingStation.stationInfo) - ) - ?.find((idTag) => idTag === commandPayload.idTag) + !Utils.isEmptyString( + chargingStation.authorizedTagsCache + .getAuthorizedTags( + ChargingStationUtils.getAuthorizationFile(chargingStation.stationInfo) + ) + ?.find((idTag) => idTag === commandPayload.idTag) + ) ) { connectorStatus.localAuthorizeIdTag = commandPayload.idTag; connectorStatus.idTagLocalAuthorized = true; @@ -1218,7 +1223,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer return OCPPConstants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED; case OCPP16MessageTrigger.StatusNotification: setTimeout(() => { - if (commandPayload?.connectorId) { + if (!Utils.isNullOrUndefined(commandPayload?.connectorId)) { chargingStation.ocppRequestService .requestHandler( chargingStation, diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 2b40ad41..aae1d065 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -376,7 +376,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { } else { if (authorizeConnectorIdDefined) { chargingStation.getConnectorStatus(authorizeConnectorId).idTagAuthorized = false; - delete chargingStation.getConnectorStatus(authorizeConnectorId).authorizeIdTag; + delete chargingStation.getConnectorStatus(authorizeConnectorId)?.authorizeIdTag; } logger.debug( `${chargingStation.logPrefix()} IdTag '${requestPayload.idTag}' rejected with status '${ @@ -580,7 +580,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { const transactionConnectorId = chargingStation.getConnectorIdByTransactionId( requestPayload.transactionId ); - if (!transactionConnectorId) { + if (Utils.isNullOrUndefined(transactionConnectorId)) { logger.error( `${chargingStation.logPrefix()} Trying to stop a non existing transaction ${requestPayload.transactionId.toString()}` ); @@ -635,7 +635,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { chargingStation.resetConnectorStatus(transactionConnectorId); const logMsg = `${chargingStation.logPrefix()} Transaction ${requestPayload.transactionId.toString()} STOPPED on ${ chargingStation.stationInfo.chargingStationId - }#${transactionConnectorId.toString()} with status '${ + }#${transactionConnectorId?.toString()} with status '${ payload.idTagInfo?.status ?? 'undefined' }'`; if ( diff --git a/src/performance/storage/JsonFileStorage.ts b/src/performance/storage/JsonFileStorage.ts index 63d12a43..48058340 100644 --- a/src/performance/storage/JsonFileStorage.ts +++ b/src/performance/storage/JsonFileStorage.ts @@ -51,7 +51,7 @@ export class JsonFileStorage extends Storage { public open(): void { try { - if (!this?.fd) { + if (this?.fd === undefined && this?.fd === null) { this.fd = fs.openSync(this.dbName, 'a+'); } } catch (error) { -- 2.34.1