From 551e477c2cc2461de8dcc2b263a35c0c50240bbf Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 25 Jan 2023 19:37:20 +0100 Subject: [PATCH] Strict null check fixes MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/Bootstrap.ts | 2 +- src/charging-station/ChargingStation.ts | 54 ++++++++++--------- .../ocpp/OCPPRequestService.ts | 6 +-- .../ui-server/UIHttpServer.ts | 2 +- .../ui-server/UIWebSocketServer.ts | 2 +- .../ui-services/AbstractUIService.ts | 2 +- .../ui-server/ui-services/UIServiceFactory.ts | 4 +- src/utils/CircularArray.ts | 2 +- 8 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 8a161e54..ce1290db 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -39,7 +39,7 @@ enum exitCodes { export class Bootstrap { private static instance: Bootstrap | null = null; private workerImplementation: WorkerAbstract | null; - private readonly uiServer!: AbstractUIServer; + private readonly uiServer!: AbstractUIServer | null; private readonly storage!: Storage; private numberOfChargingStationTemplates!: number | undefined; private numberOfChargingStations!: number; diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 639123ab..9d69f038 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -100,12 +100,12 @@ export default class ChargingStation { public started: boolean; public starting: boolean; public authorizedTagsCache: AuthorizedTagsCache; - public automaticTransactionGenerator!: AutomaticTransactionGenerator; - public ocppConfiguration!: ChargingStationOcppConfiguration | null; + public automaticTransactionGenerator!: AutomaticTransactionGenerator | undefined; + public ocppConfiguration!: ChargingStationOcppConfiguration | undefined; public wsConnection!: WebSocket | null; public readonly connectors: Map; public readonly requests: Map; - public performanceStatistics!: PerformanceStatistics; + public performanceStatistics!: PerformanceStatistics | undefined; public heartbeatSetInterval!: NodeJS.Timeout; public ocppRequestService!: OCPPRequestService; public bootNotificationRequest!: BootNotificationRequest; @@ -501,7 +501,7 @@ export default class ChargingStation { if (this.starting === false) { this.starting = true; if (this.getEnableStatistics() === true) { - this.performanceStatistics.start(); + this.performanceStatistics?.start(); } this.openWSConnection(); // Monitor charging station template file @@ -529,9 +529,9 @@ export default class ChargingStation { this.startAutomaticTransactionGenerator(); } if (this.getEnableStatistics() === true) { - this.performanceStatistics.restart(); + this.performanceStatistics?.restart(); } else { - this.performanceStatistics.stop(); + this.performanceStatistics?.stop(); } // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed } catch (error) { @@ -561,7 +561,7 @@ export default class ChargingStation { await this.stopMessageSequence(reason); this.closeWSConnection(); if (this.getEnableStatistics() === true) { - this.performanceStatistics.stop(); + this.performanceStatistics?.stop(); } this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash); this.templateFileWatcher?.close(); @@ -712,10 +712,10 @@ export default class ChargingStation { ); if (!Utils.isEmptyArray(connectorIds)) { for (const connectorId of connectorIds) { - this.automaticTransactionGenerator.startConnector(connectorId); + this.automaticTransactionGenerator?.startConnector(connectorId); } } else { - this.automaticTransactionGenerator.start(); + this.automaticTransactionGenerator?.start(); } parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this)); } @@ -863,14 +863,14 @@ export default class ChargingStation { stationInfo.ocppVersion = stationTemplate?.ocppVersion ?? OCPPVersion.VERSION_16; ChargingStationUtils.createSerialNumber(stationTemplate, stationInfo); if (!Utils.isEmptyArray(stationTemplate?.power)) { - stationTemplate.power = stationTemplate?.power as number[]; + stationTemplate.power = stationTemplate.power as number[]; const powerArrayRandomIndex = Math.floor(Utils.secureRandom() * stationTemplate.power.length); stationInfo.maximumPower = stationTemplate?.powerUnit === PowerUnits.KILO_WATT ? stationTemplate.power[powerArrayRandomIndex] * 1000 : stationTemplate.power[powerArrayRandomIndex]; } else { - stationTemplate.power = stationTemplate.power as number; + stationTemplate.power = stationTemplate?.power as number; stationInfo.maximumPower = stationTemplate?.powerUnit === PowerUnits.KILO_WATT ? stationTemplate.power * 1000 @@ -930,17 +930,17 @@ export default class ChargingStation { return stationInfo; } - private getStationInfoFromFile(): ChargingStationInfo | null { - let stationInfo: ChargingStationInfo | null = null; + private getStationInfoFromFile(): ChargingStationInfo | undefined { + let stationInfo: ChargingStationInfo | undefined; this.getStationInfoPersistentConfiguration() && - (stationInfo = this.getConfigurationFromFile()?.stationInfo ?? null); + (stationInfo = this.getConfigurationFromFile()?.stationInfo); stationInfo && ChargingStationUtils.createStationInfoHash(stationInfo); return stationInfo; } private getStationInfo(): ChargingStationInfo { const stationInfoFromTemplate: ChargingStationInfo = this.getStationInfoFromTemplate(); - const stationInfoFromFile: ChargingStationInfo | null = this.getStationInfoFromFile(); + const stationInfoFromFile: ChargingStationInfo | undefined = this.getStationInfoFromFile(); // Priority: charging station info from template > charging station info from configuration file > charging station info attribute if (stationInfoFromFile?.templateHash === stationInfoFromTemplate.templateHash) { if (this.stationInfo?.infoHash === stationInfoFromFile?.infoHash) { @@ -1315,8 +1315,8 @@ export default class ChargingStation { } } - private getConfigurationFromFile(): ChargingStationConfiguration | null { - let configuration: ChargingStationConfiguration | null = null; + private getConfigurationFromFile(): ChargingStationConfiguration | undefined { + let configuration: ChargingStationConfiguration | undefined; if (this.configurationFile && fs.existsSync(this.configurationFile)) { try { if (this.sharedLRUCache.hasChargingStationConfiguration(this.configurationFileHash)) { @@ -1394,12 +1394,12 @@ export default class ChargingStation { } } - private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration | null { - return this.getTemplateFromFile()?.Configuration ?? null; + private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration | undefined { + return this.getTemplateFromFile()?.Configuration; } - private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration | null { - let configuration: ChargingStationConfiguration | null = null; + private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration | undefined { + let configuration: ChargingStationConfiguration | undefined; if (this.getOcppPersistentConfiguration() === true) { const configurationFromFile = this.getConfigurationFromFile(); configuration = configurationFromFile?.configurationKey && configurationFromFile; @@ -1408,8 +1408,8 @@ export default class ChargingStation { return configuration; } - private getOcppConfiguration(): ChargingStationOcppConfiguration | null { - let ocppConfiguration: ChargingStationOcppConfiguration | null = + private getOcppConfiguration(): ChargingStationOcppConfiguration | undefined { + let ocppConfiguration: ChargingStationOcppConfiguration | undefined = this.getOcppConfigurationFromFile(); if (!ocppConfiguration) { ocppConfiguration = this.getOcppConfigurationFromTemplate(); @@ -1514,7 +1514,7 @@ export default class ChargingStation { case MessageType.CALL_MESSAGE: [, , commandName, commandPayload] = request as IncomingRequest; if (this.getEnableStatistics() === true) { - this.performanceStatistics.addRequestStatistic(commandName, messageType); + this.performanceStatistics?.addRequestStatistic(commandName, messageType); } logger.debug( `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify( @@ -2047,8 +2047,10 @@ export default class ChargingStation { } } - private getAutomaticTransactionGeneratorConfigurationFromTemplate(): AutomaticTransactionGeneratorConfiguration | null { - return this.getTemplateFromFile()?.AutomaticTransactionGenerator ?? null; + private getAutomaticTransactionGeneratorConfigurationFromTemplate(): + | AutomaticTransactionGeneratorConfiguration + | undefined { + return this.getTemplateFromFile()?.AutomaticTransactionGenerator; } private initializeConnectorStatus(connectorId: number): void { diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index fc4410f8..67faed54 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -232,7 +232,7 @@ export default abstract class OCPPRequestService { return Utils.promiseWithTimeout( new Promise((resolve, reject) => { if (chargingStation.getEnableStatistics() === true) { - chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType); + chargingStation.performanceStatistics?.addRequestStatistic(commandName, messageType); } const messageToSend = this.buildMessageToSend( chargingStation, @@ -306,7 +306,7 @@ export default abstract class OCPPRequestService { */ function responseCallback(payload: JsonType, requestPayload: JsonType): void { if (chargingStation.getEnableStatistics() === true) { - chargingStation.performanceStatistics.addRequestStatistic( + chargingStation.performanceStatistics?.addRequestStatistic( commandName, MessageType.CALL_RESULT_MESSAGE ); @@ -338,7 +338,7 @@ export default abstract class OCPPRequestService { */ function errorCallback(error: OCPPError, requestStatistic = true): void { if (requestStatistic === true && chargingStation.getEnableStatistics() === true) { - chargingStation.performanceStatistics.addRequestStatistic( + chargingStation.performanceStatistics?.addRequestStatistic( commandName, MessageType.CALL_ERROR_MESSAGE ); diff --git a/src/charging-station/ui-server/UIHttpServer.ts b/src/charging-station/ui-server/UIHttpServer.ts index 87f10a5e..f475b8ef 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -110,7 +110,7 @@ export default class UIHttpServer extends AbstractUIServer { const body = JSON.parse(Buffer.concat(bodyBuffer).toString()) as RequestPayload; this.uiServices .get(version) - .requestHandler(this.buildProtocolRequest(uuid, procedureName, body ?? {})) + ?.requestHandler(this.buildProtocolRequest(uuid, procedureName, body ?? {})) .catch(() => { /* Error caught by AbstractUIService */ }); diff --git a/src/charging-station/ui-server/UIWebSocketServer.ts b/src/charging-station/ui-server/UIWebSocketServer.ts index e217305f..d6240e31 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -49,7 +49,7 @@ export default class UIWebSocketServer extends AbstractUIServer { this.responseHandlers.set(requestId, ws); this.uiServices .get(version) - .requestHandler(request) + ?.requestHandler(request) .catch(() => { /* Error caught by AbstractUIService */ }); diff --git a/src/charging-station/ui-server/ui-services/AbstractUIService.ts b/src/charging-station/ui-server/ui-services/AbstractUIService.ts index 5a418b9d..1dd0ee27 100644 --- a/src/charging-station/ui-server/ui-services/AbstractUIService.ts +++ b/src/charging-station/ui-server/ui-services/AbstractUIService.ts @@ -92,7 +92,7 @@ export default abstract class AbstractUIService { // Log logger.error(`${this.logPrefix(moduleName, 'messageHandler')} Handle request error:`, error); responsePayload = { - hashIds: requestPayload.hashIds, + hashIds: requestPayload?.hashIds, status: ResponseStatus.FAILURE, command, requestPayload, diff --git a/src/charging-station/ui-server/ui-services/UIServiceFactory.ts b/src/charging-station/ui-server/ui-services/UIServiceFactory.ts index 551869f0..9f2d448b 100644 --- a/src/charging-station/ui-server/ui-services/UIServiceFactory.ts +++ b/src/charging-station/ui-server/ui-services/UIServiceFactory.ts @@ -11,12 +11,10 @@ export default class UIServiceFactory { public static getUIServiceImplementation( version: ProtocolVersion, uiServer: AbstractUIServer - ): AbstractUIService | null { + ): AbstractUIService { switch (version) { case ProtocolVersion['0.0.1']: return new UIService001(uiServer); - default: - return null; } } } diff --git a/src/utils/CircularArray.ts b/src/utils/CircularArray.ts index d4e965e7..5fdeeaad 100644 --- a/src/utils/CircularArray.ts +++ b/src/utils/CircularArray.ts @@ -47,7 +47,7 @@ export class CircularArray extends Array { public splice(start: number, deleteCount?: number, ...items: T[]): T[] { let itemsRemoved: T[]; - if (arguments.length >= 3 && typeof deleteCount !== 'undefined') { + if (arguments.length >= 3 && deleteCount !== undefined) { itemsRemoved = super.splice(start, deleteCount); // FIXME: that makes the items insert not in place this.push(...items); -- 2.34.1