fix: ensure persistence configuration override is taken in all case
[e-mobility-charging-stations-simulator.git] / src / charging-station / Helpers.ts
index 9a5eeb6ea97822f9946d36b672ea82bc1770d51b..8870ce86ab3882a89e0c34364026495eaf115d84 100644 (file)
@@ -36,6 +36,7 @@ import {
   type ChargingSchedulePeriod,
   type ChargingStationConfiguration,
   type ChargingStationInfo,
+  type ChargingStationOptions,
   type ChargingStationTemplate,
   type ChargingStationWorkerMessageEvents,
   ConnectorPhaseRotation,
@@ -57,7 +58,7 @@ import {
   ACElectricUtils,
   Constants,
   DCElectricUtils,
-  cloneObject,
+  clone,
   convertToDate,
   convertToInt,
   isArraySorted,
@@ -65,7 +66,7 @@ import {
   isEmptyString,
   isNotEmptyArray,
   isNotEmptyString,
-  isValidTime,
+  isValidDate,
   logger,
   secureRandom
 } from '../utils/index.js'
@@ -247,14 +248,6 @@ export const checkTemplate = (
     logger.error(`${logPrefix} ${errorMsg}`)
     throw new BaseError(errorMsg)
   }
-  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-  if (isEmptyObject(stationTemplate.AutomaticTransactionGenerator!)) {
-    stationTemplate.AutomaticTransactionGenerator = Constants.DEFAULT_ATG_CONFIGURATION
-    logger.warn(
-      `${logPrefix} Empty automatic transaction generator configuration from template file ${templateFile}, set to default: %j`,
-      Constants.DEFAULT_ATG_CONFIGURATION
-    )
-  }
   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`
@@ -331,7 +324,7 @@ export const buildConnectorsMap = (
       const connectorStatus = connectors[connector]
       const connectorId = convertToInt(connector)
       checkStationInfoConnectorStatus(connectorId, connectorStatus, logPrefix, templateFile)
-      connectorsMap.set(connectorId, cloneObject<ConnectorStatus>(connectorStatus))
+      connectorsMap.set(connectorId, clone<ConnectorStatus>(connectorStatus))
     }
   } else {
     logger.warn(
@@ -341,6 +334,31 @@ export const buildConnectorsMap = (
   return connectorsMap
 }
 
+export const setChargingStationOptions = (
+  stationInfo: ChargingStationInfo,
+  options?: ChargingStationOptions
+): ChargingStationInfo => {
+  if (options?.persistentConfiguration != null) {
+    stationInfo.stationInfoPersistentConfiguration = options.persistentConfiguration
+    stationInfo.ocppPersistentConfiguration = options.persistentConfiguration
+    stationInfo.automaticTransactionGeneratorPersistentConfiguration =
+      options.persistentConfiguration
+  }
+  if (options?.autoRegister != null) {
+    stationInfo.autoRegister = options.autoRegister
+  }
+  if (options?.enableStatistics != null) {
+    stationInfo.enableStatistics = options.enableStatistics
+  }
+  if (options?.ocppStrictCompliance != null) {
+    stationInfo.ocppStrictCompliance = options.ocppStrictCompliance
+  }
+  if (options?.stopTransactionsOnStopped != null) {
+    stationInfo.stopTransactionsOnStopped = options.stopTransactionsOnStopped
+  }
+  return stationInfo
+}
+
 export const initializeConnectorsMapStatus = (
   connectors: Map<number, ConnectorStatus>,
   logPrefix: string
@@ -348,9 +366,9 @@ export const initializeConnectorsMapStatus = (
   for (const connectorId of connectors.keys()) {
     if (connectorId > 0 && connectors.get(connectorId)?.transactionStarted === true) {
       logger.warn(
-        `${logPrefix} Connector id ${connectorId} at initialization has a transaction started with id ${connectors.get(
-          connectorId
-        )?.transactionId}`
+        `${logPrefix} Connector id ${connectorId} at initialization has a transaction started with id ${
+          connectors.get(connectorId)?.transactionId
+        }`
       )
     }
     if (connectorId === 0) {
@@ -367,11 +385,14 @@ export const initializeConnectorsMapStatus = (
   }
 }
 
-export const resetConnectorStatus = (connectorStatus: ConnectorStatus): void => {
+export const resetConnectorStatus = (connectorStatus: ConnectorStatus | undefined): void => {
+  if (connectorStatus == null) {
+    return
+  }
   connectorStatus.chargingProfiles =
     connectorStatus.transactionId != null && isNotEmptyArray(connectorStatus.chargingProfiles)
-      ? connectorStatus.chargingProfiles?.filter(
-        (chargingProfile) => chargingProfile.transactionId !== connectorStatus.transactionId
+      ? connectorStatus.chargingProfiles.filter(
+        chargingProfile => chargingProfile.transactionId !== connectorStatus.transactionId
       )
       : []
   connectorStatus.idTagLocalAuthorized = false
@@ -390,9 +411,8 @@ export const resetConnectorStatus = (connectorStatus: ConnectorStatus): void =>
 export const createBootNotificationRequest = (
   stationInfo: ChargingStationInfo,
   bootReason: BootReasonEnumType = BootReasonEnumType.PowerUp
-): BootNotificationRequest => {
-  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-  const ocppVersion = stationInfo.ocppVersion!
+): BootNotificationRequest | undefined => {
+  const ocppVersion = stationInfo.ocppVersion
   switch (ocppVersion) {
     case OCPPVersion.VERSION_16:
       return {
@@ -466,7 +486,7 @@ export const warnTemplateKeysDeprecation = (
 export const stationTemplateToStationInfo = (
   stationTemplate: ChargingStationTemplate
 ): ChargingStationInfo => {
-  stationTemplate = cloneObject<ChargingStationTemplate>(stationTemplate)
+  stationTemplate = clone<ChargingStationTemplate>(stationTemplate)
   delete stationTemplate.power
   delete stationTemplate.powerUnit
   delete stationTemplate.Connectors
@@ -564,7 +584,7 @@ export const getConnectorChargingProfiles = (
   chargingStation: ChargingStation,
   connectorId: number
 ): ChargingProfile[] => {
-  return cloneObject<ChargingProfile[]>(
+  return clone<ChargingProfile[]>(
     (chargingStation.getConnectorStatus(connectorId)?.chargingProfiles ?? [])
       .sort((a, b) => b.stackLevel - a.stackLevel)
       .concat(
@@ -660,7 +680,7 @@ export const waitChargingStationEvents = async (
   event: ChargingStationWorkerMessageEvents,
   eventsToWait: number
 ): Promise<number> => {
-  return await new Promise<number>((resolve) => {
+  return await new Promise<number>(resolve => {
     let events = 0
     if (eventsToWait === 0) {
       resolve(events)
@@ -678,11 +698,11 @@ export const waitChargingStationEvents = async (
 const getConfiguredMaxNumberOfConnectors = (stationTemplate: ChargingStationTemplate): number => {
   let configuredMaxNumberOfConnectors = 0
   if (isNotEmptyArray(stationTemplate.numberOfConnectors)) {
-    const numberOfConnectors = stationTemplate.numberOfConnectors as number[]
+    const numberOfConnectors = stationTemplate.numberOfConnectors
     configuredMaxNumberOfConnectors =
       numberOfConnectors[Math.floor(secureRandom() * numberOfConnectors.length)]
   } else if (stationTemplate.numberOfConnectors != null) {
-    configuredMaxNumberOfConnectors = stationTemplate.numberOfConnectors as number
+    configuredMaxNumberOfConnectors = stationTemplate.numberOfConnectors
   } else if (stationTemplate.Connectors != null && stationTemplate.Evses == null) {
     configuredMaxNumberOfConnectors =
       // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
@@ -796,25 +816,24 @@ const getLimitFromChargingProfiles = (
 ): ChargingProfilesLimit | undefined => {
   const debugLogMsg = `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j`
   const currentDate = new Date()
-  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-  const connectorStatus = chargingStation.getConnectorStatus(connectorId)!
+  const connectorStatus = chargingStation.getConnectorStatus(connectorId)
   for (const chargingProfile of chargingProfiles) {
     const chargingSchedule = chargingProfile.chargingSchedule
-    if (chargingSchedule.startSchedule == null && connectorStatus.transactionStarted === true) {
+    if (chargingSchedule.startSchedule == null) {
       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
+      chargingSchedule.startSchedule = connectorStatus?.transactionStart
     }
-    if (chargingSchedule.startSchedule != null && !isDate(chargingSchedule.startSchedule)) {
+    if (!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 (chargingSchedule.startSchedule != null && chargingSchedule.duration == null) {
+    if (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`
       )
@@ -830,10 +849,8 @@ const getLimitFromChargingProfiles = (
     // Check if the charging profile is active
     if (
       isWithinInterval(currentDate, {
-        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-        start: chargingSchedule.startSchedule!,
-        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-        end: addSeconds(chargingSchedule.startSchedule!, chargingSchedule.duration!)
+        start: chargingSchedule.startSchedule,
+        end: addSeconds(chargingSchedule.startSchedule, chargingSchedule.duration)
       })
     ) {
       if (isNotEmptyArray(chargingSchedule.chargingSchedulePeriod)) {
@@ -877,8 +894,7 @@ const getLimitFromChargingProfiles = (
           // Find the right schedule period
           if (
             isAfter(
-              // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-              addSeconds(chargingSchedule.startSchedule!, chargingSchedulePeriod.startPeriod),
+              addSeconds(chargingSchedule.startSchedule, chargingSchedulePeriod.startPeriod),
               currentDate
             )
           ) {
@@ -899,14 +915,11 @@ const getLimitFromChargingProfiles = (
             (index < chargingSchedule.chargingSchedulePeriod.length - 1 &&
               differenceInSeconds(
                 addSeconds(
-                  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-                  chargingSchedule.startSchedule!,
+                  chargingSchedule.startSchedule,
                   chargingSchedule.chargingSchedulePeriod[index + 1].startPeriod
                 ),
-                // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-                chargingSchedule.startSchedule!
-                // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-              ) > chargingSchedule.duration!)
+                chargingSchedule.startSchedule
+              ) > chargingSchedule.duration)
           ) {
             const result: ChargingProfilesLimit = {
               limit: previousChargingSchedulePeriod.limit,
@@ -922,9 +935,9 @@ const getLimitFromChargingProfiles = (
 }
 
 export const prepareChargingProfileKind = (
-  connectorStatus: ConnectorStatus,
+  connectorStatus: ConnectorStatus | undefined,
   chargingProfile: ChargingProfile,
-  currentDate: Date,
+  currentDate: string | number | Date,
   logPrefix: string
 ): boolean => {
   switch (chargingProfile.chargingProfileKind) {
@@ -941,7 +954,7 @@ export const prepareChargingProfileKind = (
         )
         delete chargingProfile.chargingSchedule.startSchedule
       }
-      if (connectorStatus.transactionStarted === true) {
+      if (connectorStatus?.transactionStarted === true) {
         chargingProfile.chargingSchedule.startSchedule = connectorStatus.transactionStart
       }
       // FIXME: Handle relative charging profile duration
@@ -952,19 +965,19 @@ export const prepareChargingProfileKind = (
 
 export const canProceedChargingProfile = (
   chargingProfile: ChargingProfile,
-  currentDate: Date,
+  currentDate: string | number | Date,
   logPrefix: string
 ): boolean => {
   if (
-    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-    (isValidTime(chargingProfile.validFrom) && isBefore(currentDate, chargingProfile.validFrom!)) ||
-    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-    (isValidTime(chargingProfile.validTo) && isAfter(currentDate, chargingProfile.validTo!))
+    (isValidDate(chargingProfile.validFrom) && isBefore(currentDate, chargingProfile.validFrom)) ||
+    (isValidDate(chargingProfile.validTo) && isAfter(currentDate, chargingProfile.validTo))
   ) {
     logger.debug(
       `${logPrefix} ${moduleName}.canProceedChargingProfile: Charging profile id ${
         chargingProfile.chargingProfileId
-      } is not valid for the current date ${currentDate.toISOString()}`
+      } is not valid for the current date ${
+        isDate(currentDate) ? currentDate.toISOString() : currentDate
+      }`
     )
     return false
   }
@@ -977,7 +990,7 @@ export const canProceedChargingProfile = (
     )
     return false
   }
-  if (!isValidTime(chargingProfile.chargingSchedule.startSchedule)) {
+  if (!isValidDate(chargingProfile.chargingSchedule.startSchedule)) {
     logger.error(
       `${logPrefix} ${moduleName}.canProceedChargingProfile: Charging profile id ${chargingProfile.chargingProfileId} has an invalid startSchedule date defined`
     )
@@ -1026,12 +1039,12 @@ const canProceedRecurringChargingProfile = (
  */
 const prepareRecurringChargingProfile = (
   chargingProfile: ChargingProfile,
-  currentDate: Date,
+  currentDate: string | number | Date,
   logPrefix: string
 ): boolean => {
   const chargingSchedule = chargingProfile.chargingSchedule
   let recurringIntervalTranslated = false
-  let recurringInterval: Interval
+  let recurringInterval: Interval | undefined
   switch (chargingProfile.recurrencyKind) {
     case RecurrencyKindType.DAILY:
       recurringInterval = {
@@ -1090,12 +1103,12 @@ const prepareRecurringChargingProfile = (
       `${logPrefix} ${moduleName}.prepareRecurringChargingProfile: Recurring ${
         chargingProfile.recurrencyKind
       } charging profile id ${chargingProfile.chargingProfileId} recurrency time interval [${toDate(
-        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-        recurringInterval!.start
+        recurringInterval?.start as Date
       ).toISOString()}, ${toDate(
-        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-        recurringInterval!.end
-      ).toISOString()}] has not been properly translated to current date ${currentDate.toISOString()} `
+        recurringInterval?.end as Date
+      ).toISOString()}] has not been properly translated to current date ${
+        isDate(currentDate) ? currentDate.toISOString() : currentDate
+      } `
     )
   }
   return recurringIntervalTranslated