Falsy/truthy test fixes
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 25 Jan 2023 21:36:43 +0000 (22:36 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 25 Jan 2023 21:36:43 +0000 (22:36 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/Bootstrap.ts
src/charging-station/ChargingStation.ts
src/charging-station/ChargingStationUtils.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/performance/storage/JsonFileStorage.ts

index 4603f2788f72cce2e9161b85a9e2e0ef11904ca0..0f26d3a2e2e7dd888a741c61d02057f88f2b2e8b 100644 (file)
@@ -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()}` : ''
         }`
       );
     }
index ce1290dbc27462ad1d7cfc746954bb1191184f63..bbf20f443ee67685ac0aa8ab62e06ccdec98dc2d 100644 (file)
@@ -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)`
                   : ''
               }`
index 5123ce1cab3dc576cd395f25a88db780e482b3ef..ddaff652478e6d6898eef112dd3fcc6b2713add9 100644 (file)
@@ -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
index 44b7dee067209a25ed9e71a1cda0e139f795e057..9b371d140732db0bbcb7cff08b864ea9f2a1cd8d 100644 (file)
@@ -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}`;
   }
 
index f81c27edeae3ed70cf8e71e81ee91e37443f51c2..b9f8368b36aa4bf882154efe0fd2dc6caef29031 100644 (file)
@@ -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<OCPP16StatusNotificationRequest, OCPP16StatusNotificationResponse>(
                   chargingStation,
index 2b40ad4165556f510ec83d09628ea526ea29b8fa..aae1d0655a39fd52f1d38d61c3043975974aee61 100644 (file)
@@ -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 (
index 63d12a439a0aa6a54283b4717ce93ce0e1ba596a..480583402656f94fed6864196953a79b4d356634 100644 (file)
@@ -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) {