refactor: remove isUndefined() leftover
[e-mobility-charging-stations-simulator.git] / src / charging-station / Helpers.ts
index cc630d332c49b8533c417412949ae23ca268e0c2..49ec116ec6c1441fd501111e2ccd94130890c84d 100644 (file)
@@ -65,8 +65,6 @@ import {
   isEmptyString,
   isNotEmptyArray,
   isNotEmptyString,
-  isNullOrUndefined,
-  isUndefined,
   isValidTime,
   logger,
   secureRandom
@@ -78,14 +76,14 @@ export const getChargingStationId = (
   index: number,
   stationTemplate: ChargingStationTemplate | undefined
 ): string => {
-  if (stationTemplate === undefined) {
+  if (stationTemplate == null) {
     return "Unknown 'chargingStationId'"
   }
   // In case of multiple instances: add instance index to charging station id
   const instanceIndex = env.CF_INSTANCE_INDEX ?? 0
   const idSuffix = stationTemplate?.nameSuffix ?? ''
   const idStr = `000000000${index.toString()}`
-  return stationTemplate?.fixedName != null
+  return stationTemplate?.fixedName === true
     ? stationTemplate.baseName
     : `${stationTemplate.baseName}-${instanceIndex.toString()}${idStr.substring(
         idStr.length - 4
@@ -147,16 +145,16 @@ export const getHashId = (index: number, stationTemplate: ChargingStationTemplat
   const chargingStationInfo = {
     chargePointModel: stationTemplate.chargePointModel,
     chargePointVendor: stationTemplate.chargePointVendor,
-    ...(!isUndefined(stationTemplate.chargeBoxSerialNumberPrefix) && {
+    ...(stationTemplate.chargeBoxSerialNumberPrefix !== undefined && {
       chargeBoxSerialNumber: stationTemplate.chargeBoxSerialNumberPrefix
     }),
-    ...(!isUndefined(stationTemplate.chargePointSerialNumberPrefix) && {
+    ...(stationTemplate.chargePointSerialNumberPrefix !== undefined && {
       chargePointSerialNumber: stationTemplate.chargePointSerialNumberPrefix
     }),
-    ...(!isUndefined(stationTemplate.meterSerialNumberPrefix) && {
+    ...(stationTemplate.meterSerialNumberPrefix !== undefined && {
       meterSerialNumber: stationTemplate.meterSerialNumberPrefix
     }),
-    ...(!isUndefined(stationTemplate.meterType) && {
+    ...(stationTemplate.meterType !== undefined && {
       meterType: stationTemplate.meterType
     })
   }
@@ -255,7 +253,7 @@ export const checkTemplate = (
       Constants.DEFAULT_ATG_CONFIGURATION
     )
   }
-  if (isNullOrUndefined(stationTemplate.idTagsFile) || isEmptyString(stationTemplate.idTagsFile)) {
+  if (stationTemplate.idTagsFile == null || isEmptyString(stationTemplate.idTagsFile)) {
     logger.warn(
       `${logPrefix} Missing id tags file in template file ${templateFile}. That can lead to issues with the Automatic Transaction Generator`
     )
@@ -267,13 +265,12 @@ export const checkConfiguration = (
   logPrefix: string,
   configurationFile: string
 ): void => {
-  if (isNullOrUndefined(stationConfiguration)) {
+  if (stationConfiguration == null) {
     const errorMsg = `Failed to read charging station configuration file ${configurationFile}`
     logger.error(`${logPrefix} ${errorMsg}`)
     throw new BaseError(errorMsg)
   }
-  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-  if (isEmptyObject(stationConfiguration!)) {
+  if (isEmptyObject(stationConfiguration)) {
     const errorMsg = `Empty charging station configuration from file ${configurationFile}`
     logger.error(`${logPrefix} ${errorMsg}`)
     throw new BaseError(errorMsg)
@@ -314,7 +311,7 @@ export const checkStationInfoConnectorStatus = (
   logPrefix: string,
   templateFile: string
 ): void => {
-  if (!isNullOrUndefined(connectorStatus?.status)) {
+  if (connectorStatus?.status != null) {
     logger.warn(
       `${logPrefix} Charging station information from template ${templateFile} with connector id ${connectorId} status configuration defined, undefine it`
     )
@@ -358,14 +355,11 @@ export const initializeConnectorsMapStatus = (
     if (connectorId === 0) {
       // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
       connectors.get(connectorId)!.availability = AvailabilityType.Operative
-      if (isUndefined(connectors.get(connectorId)?.chargingProfiles)) {
+      if (connectors.get(connectorId)?.chargingProfiles == null) {
         // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
         connectors.get(connectorId)!.chargingProfiles = []
       }
-    } else if (
-      connectorId > 0 &&
-      isNullOrUndefined(connectors.get(connectorId)?.transactionStarted)
-    ) {
+    } else if (connectorId > 0 && connectors.get(connectorId)?.transactionStarted == null) {
       // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
       initializeConnectorStatus(connectors.get(connectorId)!)
     }
@@ -403,21 +397,21 @@ export const createBootNotificationRequest = (
       return {
         chargePointModel: stationInfo.chargePointModel,
         chargePointVendor: stationInfo.chargePointVendor,
-        ...(!isUndefined(stationInfo.chargeBoxSerialNumber) && {
+        ...(stationInfo.chargeBoxSerialNumber !== undefined && {
           chargeBoxSerialNumber: stationInfo.chargeBoxSerialNumber
         }),
-        ...(!isUndefined(stationInfo.chargePointSerialNumber) && {
+        ...(stationInfo.chargePointSerialNumber !== undefined && {
           chargePointSerialNumber: stationInfo.chargePointSerialNumber
         }),
-        ...(!isUndefined(stationInfo.firmwareVersion) && {
+        ...(stationInfo.firmwareVersion !== undefined && {
           firmwareVersion: stationInfo.firmwareVersion
         }),
-        ...(!isUndefined(stationInfo.iccid) && { iccid: stationInfo.iccid }),
-        ...(!isUndefined(stationInfo.imsi) && { imsi: stationInfo.imsi }),
-        ...(!isUndefined(stationInfo.meterSerialNumber) && {
+        ...(stationInfo.iccid !== undefined && { iccid: stationInfo.iccid }),
+        ...(stationInfo.imsi !== undefined && { imsi: stationInfo.imsi }),
+        ...(stationInfo.meterSerialNumber !== undefined && {
           meterSerialNumber: stationInfo.meterSerialNumber
         }),
-        ...(!isUndefined(stationInfo.meterType) && {
+        ...(stationInfo.meterType !== undefined && {
           meterType: stationInfo.meterType
         })
       } satisfies OCPP16BootNotificationRequest
@@ -428,16 +422,16 @@ export const createBootNotificationRequest = (
         chargingStation: {
           model: stationInfo.chargePointModel,
           vendorName: stationInfo.chargePointVendor,
-          ...(!isUndefined(stationInfo.firmwareVersion) && {
+          ...(stationInfo.firmwareVersion !== undefined && {
             firmwareVersion: stationInfo.firmwareVersion
           }),
-          ...(!isUndefined(stationInfo.chargeBoxSerialNumber) && {
+          ...(stationInfo.chargeBoxSerialNumber !== undefined && {
             serialNumber: stationInfo.chargeBoxSerialNumber
           }),
-          ...((!isUndefined(stationInfo.iccid) || !isUndefined(stationInfo.imsi)) && {
+          ...((stationInfo.iccid !== undefined || stationInfo.imsi !== undefined) && {
             modem: {
-              ...(!isUndefined(stationInfo.iccid) && { iccid: stationInfo.iccid }),
-              ...(!isUndefined(stationInfo.imsi) && { imsi: stationInfo.imsi })
+              ...(stationInfo.iccid !== undefined && { iccid: stationInfo.iccid }),
+              ...(stationInfo.imsi !== undefined && { imsi: stationInfo.imsi })
             }
           })
         }
@@ -462,7 +456,7 @@ export const warnTemplateKeysDeprecation = (
       templateKey.deprecatedKey,
       logPrefix,
       templateFile,
-      !isUndefined(templateKey.key) ? `Use '${templateKey.key}' instead` : undefined
+      templateKey.key !== undefined ? `Use '${templateKey.key}' instead` : undefined
     )
     convertDeprecatedTemplateKey(stationTemplate, templateKey.deprecatedKey, templateKey.key)
   }
@@ -594,7 +588,7 @@ export const getChargingStationConnectorChargingProfilesPowerLimit = (
       chargingProfiles,
       chargingStation.logPrefix()
     )
-    if (!isNullOrUndefined(result)) {
+    if (result != null) {
       limit = result?.limit
       chargingProfile = result?.chargingProfile
       switch (chargingStation.stationInfo?.currentOutType) {
@@ -606,8 +600,7 @@ export const getChargingStationConnectorChargingProfilesPowerLimit = (
                 chargingStation.getNumberOfPhases(),
                 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
                 chargingStation.stationInfo.voltageOut!,
-                // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-                limit!
+                limit
               )
           break
         case CurrentType.DC:
@@ -615,13 +608,12 @@ export const getChargingStationConnectorChargingProfilesPowerLimit = (
             chargingProfile?.chargingSchedule?.chargingRateUnit === ChargingRateUnitType.WATT
               ? limit
               : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-              DCElectricUtils.power(chargingStation.stationInfo.voltageOut!, limit!)
+              DCElectricUtils.power(chargingStation.stationInfo.voltageOut!, limit)
       }
       const connectorMaximumPower =
         // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
         chargingStation.stationInfo.maximumPower! / chargingStation.powerDivider
-      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-      if (limit! > connectorMaximumPower) {
+      if (limit > connectorMaximumPower) {
         logger.error(
           `${chargingStation.logPrefix()} ${moduleName}.getChargingStationConnectorChargingProfilesPowerLimit: Charging profile id ${chargingProfile?.chargingProfileId} limit ${limit} is greater than connector id ${connectorId} maximum ${connectorMaximumPower}: %j`,
           result
@@ -686,7 +678,7 @@ const getConfiguredMaxNumberOfConnectors = (stationTemplate: ChargingStationTemp
     const numberOfConnectors = stationTemplate.numberOfConnectors as number[]
     configuredMaxNumberOfConnectors =
       numberOfConnectors[Math.floor(secureRandom() * numberOfConnectors.length)]
-  } else if (!isUndefined(stationTemplate.numberOfConnectors)) {
+  } else if (stationTemplate.numberOfConnectors != null) {
     configuredMaxNumberOfConnectors = stationTemplate.numberOfConnectors as number
   } else if (stationTemplate.Connectors != null && stationTemplate.Evses == null) {
     configuredMaxNumberOfConnectors =
@@ -742,7 +734,7 @@ const initializeConnectorStatus = (connectorStatus: ConnectorStatus): void => {
   connectorStatus.transactionStarted = false
   connectorStatus.energyActiveImportRegisterValue = 0
   connectorStatus.transactionEnergyActiveImportRegisterValue = 0
-  if (isUndefined(connectorStatus.chargingProfiles)) {
+  if (connectorStatus.chargingProfiles == null) {
     connectorStatus.chargingProfiles = []
   }
 }
@@ -754,7 +746,7 @@ const warnDeprecatedTemplateKey = (
   templateFile: string,
   logMsgToAppend = ''
 ): void => {
-  if (!isUndefined(template?.[key as keyof ChargingStationTemplate])) {
+  if (template?.[key as keyof ChargingStationTemplate] !== undefined) {
     const logMsg = `Deprecated template key '${key}' usage in file '${templateFile}'${
       isNotEmptyString(logMsgToAppend) ? `. ${logMsgToAppend}` : ''
     }`
@@ -768,10 +760,9 @@ const convertDeprecatedTemplateKey = (
   deprecatedKey: string,
   key?: string
 ): void => {
-  if (!isUndefined(template?.[deprecatedKey as keyof ChargingStationTemplate])) {
-    if (!isUndefined(key)) {
-      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-      (template as unknown as Record<string, unknown>)[key!] =
+  if (template?.[deprecatedKey as keyof ChargingStationTemplate] !== undefined) {
+    if (key !== undefined) {
+      (template as unknown as Record<string, unknown>)[key] =
         template[deprecatedKey as keyof ChargingStationTemplate]
     }
     // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
@@ -805,36 +796,26 @@ const getLimitFromChargingProfiles = (
   const connectorStatus = chargingStation.getConnectorStatus(connectorId)!
   for (const chargingProfile of chargingProfiles) {
     const chargingSchedule = chargingProfile.chargingSchedule
-    if (
-      isNullOrUndefined(chargingSchedule?.startSchedule) &&
-      connectorStatus?.transactionStarted === true
-    ) {
+    if (chargingSchedule?.startSchedule == null && connectorStatus?.transactionStarted === true) {
       logger.debug(
         `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${chargingProfile.chargingProfileId} has no startSchedule defined. Trying to set it to the connector current transaction start date`
       )
       // OCPP specifies that if startSchedule is not defined, it should be relative to start of the connector transaction
       chargingSchedule.startSchedule = connectorStatus?.transactionStart
     }
-    if (
-      !isNullOrUndefined(chargingSchedule?.startSchedule) &&
-      !isDate(chargingSchedule?.startSchedule)
-    ) {
+    if (chargingSchedule?.startSchedule != null && !isDate(chargingSchedule?.startSchedule)) {
       logger.warn(
         `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${chargingProfile.chargingProfileId} startSchedule property is not a Date instance. Trying to convert it to a Date instance`
       )
       // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
       chargingSchedule.startSchedule = convertToDate(chargingSchedule?.startSchedule)!
     }
-    if (
-      !isNullOrUndefined(chargingSchedule?.startSchedule) &&
-      isNullOrUndefined(chargingSchedule?.duration)
-    ) {
+    if (chargingSchedule?.startSchedule != null && chargingSchedule?.duration == null) {
       logger.debug(
         `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${chargingProfile.chargingProfileId} has no duration defined and will be set to the maximum time allowed`
       )
       // OCPP specifies that if duration is not defined, it should be infinite
-      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-      chargingSchedule.duration = differenceInSeconds(maxTime, chargingSchedule.startSchedule!)
+      chargingSchedule.duration = differenceInSeconds(maxTime, chargingSchedule.startSchedule)
     }
     if (!prepareChargingProfileKind(connectorStatus, chargingProfile, currentDate, logPrefix)) {
       continue
@@ -950,7 +931,7 @@ export const prepareChargingProfileKind = (
       prepareRecurringChargingProfile(chargingProfile, currentDate, logPrefix)
       break
     case ChargingProfileKindType.RELATIVE:
-      if (!isNullOrUndefined(chargingProfile.chargingSchedule.startSchedule)) {
+      if (chargingProfile.chargingSchedule.startSchedule != null) {
         logger.warn(
           `${logPrefix} ${moduleName}.prepareChargingProfileKind: Relative charging profile id ${chargingProfile.chargingProfileId} has a startSchedule property defined. It will be ignored or used if the connector has a transaction started`
         )
@@ -984,8 +965,8 @@ export const canProceedChargingProfile = (
     return false
   }
   if (
-    isNullOrUndefined(chargingProfile.chargingSchedule.startSchedule) ||
-    isNullOrUndefined(chargingProfile.chargingSchedule.duration)
+    chargingProfile.chargingSchedule.startSchedule == null ||
+    chargingProfile.chargingSchedule.duration == null
   ) {
     logger.error(
       `${logPrefix} ${moduleName}.canProceedChargingProfile: Charging profile id ${chargingProfile.chargingProfileId} has no startSchedule or duration defined`
@@ -993,7 +974,7 @@ export const canProceedChargingProfile = (
     return false
   }
   if (
-    !isNullOrUndefined(chargingProfile.chargingSchedule.startSchedule) &&
+    chargingProfile.chargingSchedule.startSchedule != null &&
     !isValidTime(chargingProfile.chargingSchedule.startSchedule)
   ) {
     logger.error(
@@ -1002,7 +983,7 @@ export const canProceedChargingProfile = (
     return false
   }
   if (
-    !isNullOrUndefined(chargingProfile.chargingSchedule.duration) &&
+    chargingProfile.chargingSchedule.duration != null &&
     !Number.isSafeInteger(chargingProfile.chargingSchedule.duration)
   ) {
     logger.error(
@@ -1019,7 +1000,7 @@ const canProceedRecurringChargingProfile = (
 ): boolean => {
   if (
     chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING &&
-    isNullOrUndefined(chargingProfile.recurrencyKind)
+    chargingProfile.recurrencyKind == null
   ) {
     logger.error(
       `${logPrefix} ${moduleName}.canProceedRecurringChargingProfile: Recurring charging profile id ${chargingProfile.chargingProfileId} has no recurrencyKind defined`
@@ -1028,7 +1009,7 @@ const canProceedRecurringChargingProfile = (
   }
   if (
     chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING &&
-    isNullOrUndefined(chargingProfile.chargingSchedule.startSchedule)
+    chargingProfile.chargingSchedule.startSchedule == null
   ) {
     logger.error(
       `${logPrefix} ${moduleName}.canProceedRecurringChargingProfile: Recurring charging profile id ${chargingProfile.chargingProfileId} has no startSchedule defined`
@@ -1127,7 +1108,7 @@ const checkRecurringChargingProfileDuration = (
   interval: Interval,
   logPrefix: string
 ): void => {
-  if (isNullOrUndefined(chargingProfile.chargingSchedule.duration)) {
+  if (chargingProfile.chargingSchedule.duration == null) {
     logger.warn(
       `${logPrefix} ${moduleName}.checkRecurringChargingProfileDuration: Recurring ${
         chargingProfile.chargingProfileKind
@@ -1140,8 +1121,7 @@ const checkRecurringChargingProfileDuration = (
     )
     chargingProfile.chargingSchedule.duration = differenceInSeconds(interval.end, interval.start)
   } else if (
-    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-    chargingProfile.chargingSchedule.duration! > differenceInSeconds(interval.end, interval.start)
+    chargingProfile.chargingSchedule.duration > differenceInSeconds(interval.end, interval.start)
   ) {
     logger.warn(
       `${logPrefix} ${moduleName}.checkRecurringChargingProfileDuration: Recurring ${