- // Add active charging profiles into chargingProfiles array
- if (
- isValidTime(storedChargingProfile.chargingSchedule?.startSchedule) &&
- isWithinInterval(storedChargingProfile.chargingSchedule.startSchedule!, interval)
- ) {
- if (isEmptyArray(chargingProfiles)) {
- if (
- isAfter(
- addSeconds(
- storedChargingProfile.chargingSchedule.startSchedule!,
- storedChargingProfile.chargingSchedule.duration!,
- ),
- interval.end,
- )
- ) {
- storedChargingProfile.chargingSchedule.chargingSchedulePeriod =
- storedChargingProfile.chargingSchedule.chargingSchedulePeriod.filter(
- (schedulePeriod) =>
- isWithinInterval(
- addSeconds(
- storedChargingProfile.chargingSchedule.startSchedule!,
- schedulePeriod.startPeriod,
- )!,
- interval,
- ),
- );
- storedChargingProfile.chargingSchedule.duration = differenceInSeconds(
- interval.end,
- storedChargingProfile.chargingSchedule.startSchedule!,
- );
- }
- chargingProfiles.push(storedChargingProfile);
- } else if (isNotEmptyArray(chargingProfiles)) {
- const chargingProfilesInterval: Interval = {
- start: min(
- chargingProfiles.map(
- (chargingProfile) => chargingProfile.chargingSchedule.startSchedule ?? maxTime,
- ),
- ),
- end: max(
- chargingProfiles.map(
- (chargingProfile) =>
- addSeconds(
- chargingProfile.chargingSchedule.startSchedule!,
- chargingProfile.chargingSchedule.duration!,
- ) ?? minTime,
- ),
- ),
- };
- let addChargingProfile = false;
- if (
- isBefore(interval.start, chargingProfilesInterval.start) &&
- isBefore(
- storedChargingProfile.chargingSchedule.startSchedule!,
- chargingProfilesInterval.start,
- )
- ) {
- // Remove charging schedule periods that are after the start of the active profiles interval
- storedChargingProfile.chargingSchedule.chargingSchedulePeriod =
- storedChargingProfile.chargingSchedule.chargingSchedulePeriod.filter(
- (schedulePeriod) =>
- isWithinInterval(
- addSeconds(
- storedChargingProfile.chargingSchedule.startSchedule!,
- schedulePeriod.startPeriod,
- ),
- {
- start: interval.start,
- end: chargingProfilesInterval.start,
- },
- ),
- );
- addChargingProfile = true;
- }
- if (
- isBefore(chargingProfilesInterval.end, interval.end) &&
- isAfter(
- addSeconds(
- storedChargingProfile.chargingSchedule.startSchedule!,
- storedChargingProfile.chargingSchedule.duration!,
- ),
- chargingProfilesInterval.end,
- )
- ) {
- // Remove charging schedule periods that are before the end of the active profiles interval
- storedChargingProfile.chargingSchedule.chargingSchedulePeriod =
- storedChargingProfile.chargingSchedule.chargingSchedulePeriod.filter(
- (schedulePeriod, index) => {
- if (
- isWithinInterval(
- addSeconds(
- storedChargingProfile.chargingSchedule.startSchedule!,
- schedulePeriod.startPeriod,
- ),
- {
- start: chargingProfilesInterval.end,
- end: interval.end,
- },
- )
- ) {
- return true;
- }
- if (
- !isWithinInterval(
- addSeconds(
- storedChargingProfile.chargingSchedule.startSchedule!,
- schedulePeriod.startPeriod,
- ),
- {
- start: chargingProfilesInterval.end,
- end: interval.end,
- },
- ) &&
- index <
- storedChargingProfile.chargingSchedule.chargingSchedulePeriod.length - 1 &&
- isWithinInterval(
- addSeconds(
- storedChargingProfile.chargingSchedule.startSchedule!,
- storedChargingProfile.chargingSchedule.chargingSchedulePeriod[index + 1]
- .startPeriod,
- ),
- {
- start: chargingProfilesInterval.end,
- end: interval.end,
- },
- )
- ) {
- return true;
- }
- return false;
- },
- );
- addChargingProfile = true;
- }
- addChargingProfile && chargingProfiles.push(storedChargingProfile);
- }
- }
- }
- const compositeScheduleStart: Date = min(
- chargingProfiles.map(
- (chargingProfile) => chargingProfile.chargingSchedule.startSchedule ?? maxTime,
- ),
- );
- const compositeScheduleDuration: number = Math.max(
- ...chargingProfiles.map((chargingProfile) =>
- isNaN(chargingProfile.chargingSchedule.duration!)
- ? -Infinity
- : chargingProfile.chargingSchedule.duration!,
- ),
- );
- const compositeSchedulePeriods: OCPP16ChargingSchedulePeriod[] = chargingProfiles
- .map((chargingProfile) => chargingProfile.chargingSchedule.chargingSchedulePeriod)
- .reduce(
- (accumulator, value) =>
- accumulator.concat(value).sort((a, b) => a.startPeriod - b.startPeriod),
- [],