-
- /**
- * Charging profiles should already be sorted by connectorId and stack level (highest stack level has priority)
- *
- * @param chargingProfiles -
- * @param logPrefix -
- * @returns
- */
- private static getLimitFromChargingProfiles(
- chargingProfiles: ChargingProfile[],
- logPrefix: string
- ): {
- limit: number;
- matchingChargingProfile: ChargingProfile;
- } | null {
- const debugLogMsg = `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j`;
- const currentMoment = moment();
- const currentDate = new Date();
- for (const chargingProfile of chargingProfiles) {
- // Set helpers
- const chargingSchedule = chargingProfile.chargingSchedule;
- if (!chargingSchedule?.startSchedule) {
- logger.warn(
- `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not defined in charging profile id ${chargingProfile.chargingProfileId}`
- );
+};
+
+/**
+ * Charging profiles should already be sorted by connector id and stack level (highest stack level has priority)
+ *
+ * @param chargingProfiles -
+ * @param logPrefix -
+ * @returns
+ */
+const getLimitFromChargingProfiles = (
+ chargingStation: ChargingStation,
+ connectorId: number,
+ chargingProfiles: ChargingProfile[],
+ logPrefix: string,
+):
+ | {
+ limit: number;
+ matchingChargingProfile: ChargingProfile;
+ }
+ | undefined => {
+ const debugLogMsg = `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j`;
+ const currentDate = new Date();
+ for (const chargingProfile of chargingProfiles) {
+ // Set helpers
+ const chargingSchedule = chargingProfile.chargingSchedule;
+ if (!chargingSchedule?.startSchedule) {
+ logger.warn(
+ `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not defined in charging profile id ${chargingProfile.chargingProfileId}. Trying to set it to the connector transaction start date`,
+ );
+ // OCPP specifies that if startSchedule is not defined, it should be relative to start of the connector transaction
+ chargingSchedule.startSchedule =
+ chargingStation.getConnectorStatus(connectorId)?.transactionStart;
+ }
+ if (!(chargingSchedule?.startSchedule instanceof Date)) {
+ 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)!;
+ }
+ // Adjust recurring start schedule
+ if (chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING) {
+ switch (chargingProfile.recurrencyKind) {
+ case RecurrencyKindType.DAILY:
+ if (isBefore(chargingSchedule.startSchedule, startOfDay(currentDate))) {
+ addDays(
+ chargingSchedule.startSchedule,
+ differenceInDays(chargingSchedule.startSchedule, endOfDay(currentDate)),
+ );
+ if (
+ isBefore(chargingSchedule.startSchedule, startOfDay(currentDate)) ||
+ isAfter(chargingSchedule.startSchedule, endOfDay(currentDate))
+ ) {
+ logger.error(
+ `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${
+ chargingProfile.recurrencyKind
+ } charging profile id ${
+ chargingProfile.chargingProfileId
+ } startSchedule ${chargingSchedule.startSchedule.toISOString()} is not properly translated to the current day`,
+ );
+ }
+ }
+ break;
+ case RecurrencyKindType.WEEKLY:
+ if (isBefore(chargingSchedule.startSchedule, startOfWeek(currentDate))) {
+ addWeeks(
+ chargingSchedule.startSchedule,
+ differenceInWeeks(chargingSchedule.startSchedule, endOfWeek(currentDate)),
+ );
+ if (
+ isBefore(chargingSchedule.startSchedule, startOfWeek(currentDate)) ||
+ isAfter(chargingSchedule.startSchedule, endOfWeek(currentDate))
+ ) {
+ logger.error(
+ `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${
+ chargingProfile.recurrencyKind
+ } charging profile id ${
+ chargingProfile.chargingProfileId
+ } startSchedule ${chargingSchedule.startSchedule.toISOString()} is not properly translated to the current week`,
+ );
+ }
+ }
+ break;