refactor: cleanup log messages
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 25 Jul 2023 20:54:13 +0000 (22:54 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 25 Jul 2023 20:54:13 +0000 (22:54 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStationUtils.ts
src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts
src/utils/Utils.ts

index 1af1266110bd62d2e49f42e5ec801cc34fac8546..de29639b719888a7315edf3ef89ebd51c7e2cf29 100644 (file)
@@ -712,7 +712,7 @@ const getLimitFromChargingProfiles = (
       logger.warn(
         `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not a Date object in charging profile id ${chargingProfile.chargingProfileId}. Trying to convert it to a Date object`,
       );
-      chargingSchedule.startSchedule = convertToDate(chargingSchedule.startSchedule)!;
+      chargingSchedule.startSchedule = convertToDate(chargingSchedule?.startSchedule)!;
     }
     if (
       chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING &&
@@ -733,7 +733,7 @@ const getLimitFromChargingProfiles = (
     }
     // Check if the charging profile is active
     if (
-      isValidDate(chargingSchedule.startSchedule) &&
+      isValidDate(chargingSchedule?.startSchedule) &&
       isWithinInterval(currentDate, {
         start: chargingSchedule.startSchedule!,
         end: addSeconds(chargingSchedule.startSchedule!, chargingSchedule.duration!),
@@ -762,7 +762,7 @@ const getLimitFromChargingProfiles = (
           );
           continue;
         }
-        // Handling of only one schedule period
+        // Handle only one schedule period
         if (chargingSchedule.chargingSchedulePeriod.length === 1) {
           const result: ChargingProfilesLimit = {
             limit: chargingSchedule.chargingSchedulePeriod[0].limit,
@@ -840,10 +840,10 @@ const prepareRecurringChargingProfile = (
       checkRecurringChargingProfileDuration(chargingProfile, recurringInterval, logPrefix);
       if (
         !isWithinInterval(currentDate, recurringInterval) &&
-        isBefore(chargingSchedule.startSchedule!, currentDate)
+        isBefore(recurringInterval.end, currentDate)
       ) {
         chargingSchedule.startSchedule = addDays(
-          chargingSchedule.startSchedule!,
+          recurringInterval.start,
           differenceInDays(currentDate, recurringInterval.start),
         );
         recurringInterval = {
@@ -860,10 +860,10 @@ const prepareRecurringChargingProfile = (
       checkRecurringChargingProfileDuration(chargingProfile, recurringInterval, logPrefix);
       if (
         !isWithinInterval(currentDate, recurringInterval) &&
-        isBefore(chargingSchedule.startSchedule!, currentDate)
+        isBefore(recurringInterval.end, currentDate)
       ) {
         chargingSchedule.startSchedule = addWeeks(
-          chargingSchedule.startSchedule!,
+          recurringInterval.start,
           differenceInWeeks(currentDate, recurringInterval.start),
         );
         recurringInterval = {
@@ -877,11 +877,11 @@ const prepareRecurringChargingProfile = (
     logger.error(
       `${logPrefix} ${moduleName}.prepareRecurringChargingProfile: Recurring ${
         chargingProfile.recurrencyKind
-      } charging profile id ${
-        chargingProfile.chargingProfileId
-      } startSchedule ${chargingSchedule.startSchedule!.toISOString()} is not properly translated to current recurrency time interval [${toDate(
+      } charging profile id ${chargingProfile.chargingProfileId} recurrency time interval [${toDate(
         recurringInterval!.start,
-      ).toISOString()}, ${toDate(recurringInterval!.end).toISOString()}]`,
+      ).toISOString()}, ${toDate(
+        recurringInterval!.end,
+      ).toISOString()}] is not properly translated to current date ${currentDate.toISOString()} `,
     );
   }
 };
index b2f1c400f3b0217cbc613b815393f67c9ecd0a08..e3899cd5db97ffca80caaa8dc2cbe0b88ddcb829 100644 (file)
@@ -37,6 +37,7 @@ import {
   DCElectricUtils,
   convertToFloat,
   convertToInt,
+  formatDurationMilliSeconds,
   getRandomFloatFluctuatedRounded,
   getRandomFloatRounded,
   getRandomInteger,
@@ -726,10 +727,9 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
           `${chargingStation.logPrefix()} MeterValues measurand ${
             meterValue.sampledValue[sampledValuesIndex].measurand ??
             OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER
-          }: connector id ${connectorId}, transaction id ${connector?.transactionId}, value: ${energyValueRounded}/${connectorMaximumEnergyRounded}, duration: ${roundTo(
-            interval / (3600 * 1000),
-            4,
-          )}h`,
+          }: connector id ${connectorId}, transaction id ${connector?.transactionId}, value: ${energyValueRounded}/${connectorMaximumEnergyRounded}, duration: ${formatDurationMilliSeconds(
+            interval,
+          )}(${roundTo(interval, 4)}ms)`,
         );
       }
     }
index 14869b0d35cee8bb2b9a429de17499196ac38437..54b0751022130ba18e3a238cc58a78d741acb39e 100644 (file)
@@ -355,9 +355,9 @@ export const getWebSocketCloseEventStatusString = (code: number): string => {
   return '(Unknown)';
 };
 
-export const isArraySorted = <T>(elements: T[], compareFn: (a: T, b: T) => number): boolean => {
-  for (let i = 0; i < elements.length - 1; ++i) {
-    if (compareFn(elements[i], elements[i + 1]) > 0) {
+export const isArraySorted = <T>(array: T[], compareFn: (a: T, b: T) => number): boolean => {
+  for (let index = 0; index < array.length - 1; ++index) {
+    if (compareFn(array[index], array[index + 1]) > 0) {
       return false;
     }
   }