From 9bb1159e3a17ace0e8502e7c4e727219a2b033f2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 7 Apr 2023 12:22:13 +0200 Subject: [PATCH] refactor(simulator): use helper for undefined or null checks MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/AutomaticTransactionGenerator.ts | 2 +- src/charging-station/ChargingStation.ts | 3 +-- .../ChargingStationWorkerBroadcastChannel.ts | 7 +++---- src/charging-station/UIServiceWorkerBroadcastChannel.ts | 8 ++++---- src/charging-station/ocpp/1.6/OCPP16ResponseService.ts | 2 +- .../ui-server/ui-services/AbstractUIService.ts | 4 ++-- src/utils/Utils.ts | 2 +- 7 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 9728108b..d058866a 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -417,7 +417,7 @@ export class AutomaticTransactionGenerator extends AsyncResource { private logPrefix = (connectorId?: number): string => { return Utils.logPrefix( ` ${this.chargingStation.stationInfo.chargingStationId} | ATG${ - connectorId !== undefined ? ` on connector #${connectorId.toString()}` : '' + !Utils.isNullOrUndefined(connectorId) ? ` on connector #${connectorId.toString()}` : '' }:` ); }; diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 61a4d962..4f65a4e6 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -1320,8 +1320,7 @@ export class ChargingStation { } if ( connectorId > 0 && - (this.getConnectorStatus(connectorId)?.transactionStarted === undefined || - this.getConnectorStatus(connectorId)?.transactionStarted === null) + Utils.isNullOrUndefined(this.getConnectorStatus(connectorId)?.transactionStarted) ) { this.initializeConnectorStatus(connectorId); } diff --git a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts index 49ce0811..c03c4acb 100644 --- a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts @@ -258,12 +258,12 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne } const [uuid, command, requestPayload] = validatedMessageEvent.data as BroadcastChannelRequest; if ( - requestPayload?.hashIds !== undefined && + !Utils.isNullOrUndefined(requestPayload?.hashIds) && requestPayload?.hashIds?.includes(this.chargingStation.stationInfo.hashId) === false ) { return; } - if (requestPayload?.hashId !== undefined) { + if (!Utils.isNullOrUndefined(requestPayload?.hashId)) { logger.error( `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' array instead` ); @@ -274,8 +274,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne try { commandResponse = await this.commandHandler(command, requestPayload); if ( - commandResponse === undefined || - commandResponse === null || + Utils.isNullOrUndefined(commandResponse) || Utils.isEmptyObject(commandResponse as CommandResponse) ) { responsePayload = { diff --git a/src/charging-station/UIServiceWorkerBroadcastChannel.ts b/src/charging-station/UIServiceWorkerBroadcastChannel.ts index 5b7ab484..5f77b0e3 100644 --- a/src/charging-station/UIServiceWorkerBroadcastChannel.ts +++ b/src/charging-station/UIServiceWorkerBroadcastChannel.ts @@ -6,7 +6,7 @@ import { type ResponsePayload, ResponseStatus, } from '../types'; -import { logger } from '../utils'; +import { Utils, logger } from '../utils'; const moduleName = 'UIServiceWorkerBroadcastChannel'; @@ -69,7 +69,7 @@ export class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel { status: responsesStatus, hashIdsSucceeded: this.responses .get(uuid) - ?.responses.filter(({ hashId }) => hashId !== undefined) + ?.responses.filter(({ hashId }) => !Utils.isNullOrUndefined(hashId)) .map(({ status, hashId }) => { if (status === ResponseStatus.SUCCESS) { return hashId; @@ -78,7 +78,7 @@ export class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel { ...(responsesStatus === ResponseStatus.FAILURE && { hashIdsFailed: this.responses .get(uuid) - ?.responses.filter(({ hashId }) => hashId !== undefined) + ?.responses.filter(({ hashId }) => !Utils.isNullOrUndefined(hashId)) .map(({ status, hashId }) => { if (status === ResponseStatus.FAILURE) { return hashId; @@ -88,7 +88,7 @@ export class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel { ...(responsesStatus === ResponseStatus.FAILURE && { responsesFailed: this.responses .get(uuid) - ?.responses.filter((response) => response !== undefined) + ?.responses.filter((response) => !Utils.isNullOrUndefined(response)) .map((response) => { if (response.status === ResponseStatus.FAILURE) { return response; diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 68e9f7c6..30ed58b0 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -404,7 +404,7 @@ export class OCPP16ResponseService extends OCPPResponseService { break; } } - const authorizeConnectorIdDefined = authorizeConnectorId !== undefined; + const authorizeConnectorIdDefined = !Utils.isNullOrUndefined(authorizeConnectorId); if (payload.idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED) { authorizeConnectorIdDefined && (chargingStation.getConnectorStatus(authorizeConnectorId).idTagAuthorized = true); diff --git a/src/charging-station/ui-server/ui-services/AbstractUIService.ts b/src/charging-station/ui-server/ui-services/AbstractUIService.ts index 0a636edb..900b79d5 100644 --- a/src/charging-station/ui-server/ui-services/AbstractUIService.ts +++ b/src/charging-station/ui-server/ui-services/AbstractUIService.ts @@ -98,7 +98,7 @@ export abstract class AbstractUIService { }; } finally { // Send response for payload not forwarded to broadcast channel - if (responsePayload !== undefined) { + if (!Utils.isNullOrUndefined(responsePayload)) { this.sendResponse(messageId, responsePayload); } } @@ -151,7 +151,7 @@ export abstract class AbstractUIService { ): void { if (Utils.isNotEmptyArray(payload.hashIds)) { payload.hashIds = payload.hashIds - .filter((hashId) => hashId !== undefined) + .filter((hashId) => !Utils.isNullOrUndefined(hashId)) .map((hashId) => { if (this.uiServer.chargingStations.has(hashId) === true) { return hashId; diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 7195ce09..058d175d 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -191,7 +191,7 @@ export class Utils { } public static isCFEnvironment(): boolean { - return process.env.VCAP_APPLICATION !== undefined; + return !Utils.isNullOrUndefined(process.env.VCAP_APPLICATION); } public static isIterable(obj: T): boolean { -- 2.34.1