addDays,
addSeconds,
addWeeks,
+ differenceInDays,
+ differenceInWeeks,
+ endOfDay,
endOfWeek,
isAfter,
isBefore,
- isTomorrow,
- isYesterday,
+ startOfDay,
startOfWeek,
- subDays,
- subWeeks,
} from 'date-fns';
import type { ChargingStation } from './ChargingStation';
connectorStatus.idTagAuthorized = false;
connectorStatus.transactionRemoteStarted = false;
connectorStatus.transactionStarted = false;
+ delete connectorStatus?.transactionStart;
+ delete connectorStatus?.transactionId;
delete connectorStatus?.localAuthorizeIdTag;
delete connectorStatus?.authorizeIdTag;
- delete connectorStatus?.transactionId;
delete connectorStatus?.transactionIdTag;
connectorStatus.transactionEnergyActiveImportRegisterValue = 0;
delete connectorStatus?.transactionBeginMeterValue;
);
}
if (isNotEmptyArray(chargingProfiles)) {
- const result = getLimitFromChargingProfiles(chargingProfiles, chargingStation.logPrefix());
+ const result = getLimitFromChargingProfiles(
+ chargingStation,
+ connectorId,
+ chargingProfiles,
+ chargingStation.logPrefix(),
+ );
if (!isNullOrUndefined(result)) {
limit = result?.limit;
matchingChargingProfile = result?.matchingChargingProfile;
* @returns
*/
const getLimitFromChargingProfiles = (
+ chargingStation: ChargingStation,
+ connectorId: number,
chargingProfiles: ChargingProfile[],
logPrefix: string,
):
const chargingSchedule = chargingProfile.chargingSchedule;
if (!chargingSchedule?.startSchedule) {
logger.warn(
- `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not defined in charging profile id ${chargingProfile.chargingProfileId}`,
+ `${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(
);
chargingSchedule.startSchedule = convertToDate(chargingSchedule.startSchedule)!;
}
- // Adjust recurring schedule
+ // Adjust recurring start schedule
if (chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING) {
switch (chargingProfile.recurrencyKind) {
case RecurrencyKindType.DAILY:
- if (isYesterday(chargingSchedule.startSchedule)) {
- addDays(chargingSchedule.startSchedule, 1);
- } else if (isTomorrow(chargingSchedule.startSchedule)) {
- subDays(chargingSchedule.startSchedule, 1);
+ 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, 1);
- } else if (isAfter(chargingSchedule.startSchedule, endOfWeek(currentDate))) {
- subWeeks(chargingSchedule.startSchedule, 1);
+ 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;
}